chiark / gitweb /
Use Automake's simple test infrastructure for running Python-based
[disorder] / lib / t-printf.c
CommitLineData
b90f122b
RK
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20#include "test.h"
21
22void test_printf(void) {
23 char c;
24 short s;
25 int i;
26 long l;
27 long long ll;
28 intmax_t m;
29 ssize_t ssz;
30 ptrdiff_t p;
31 char *cp;
32 char buffer[16];
f61b995d 33 FILE *fp;
b90f122b
RK
34
35 fprintf(stderr, "test_printf\n");
36 check_string(do_printf("%d", 999), "999");
37 check_string(do_printf("%d", -999), "-999");
38 check_string(do_printf("%i", 999), "999");
39 check_string(do_printf("%i", -999), "-999");
40 check_string(do_printf("%u", 999), "999");
41 check_string(do_printf("%2u", 999), "999");
42 check_string(do_printf("%10u", 999), " 999");
43 check_string(do_printf("%-10u", 999), "999 ");
44 check_string(do_printf("%010u", 999), "0000000999");
45 check_string(do_printf("%-10d", -999), "-999 ");
46 check_string(do_printf("%-010d", -999), "-999 "); /* "-" beats "0" */
47 check_string(do_printf("%66u", 999), " 999");
48 check_string(do_printf("%o", 999), "1747");
49 check_string(do_printf("%#o", 999), "01747");
50 check_string(do_printf("%#o", 0), "0");
51 check_string(do_printf("%x", 999), "3e7");
52 check_string(do_printf("%#x", 999), "0x3e7");
53 check_string(do_printf("%#X", 999), "0X3E7");
54 check_string(do_printf("%#x", 0), "0");
55 check_string(do_printf("%hd", (short)999), "999");
56 check_string(do_printf("%hhd", (short)99), "99");
57 check_string(do_printf("%ld", 100000L), "100000");
58 check_string(do_printf("%lld", 10000000000LL), "10000000000");
59 check_string(do_printf("%qd", 10000000000LL), "10000000000");
60 check_string(do_printf("%jd", (intmax_t)10000000000LL), "10000000000");
61 check_string(do_printf("%zd", (ssize_t)2000000000), "2000000000");
62 check_string(do_printf("%td", (ptrdiff_t)2000000000), "2000000000");
63 check_string(do_printf("%hu", (short)999), "999");
64 check_string(do_printf("%hhu", (short)99), "99");
65 check_string(do_printf("%lu", 100000L), "100000");
66 check_string(do_printf("%llu", 10000000000LL), "10000000000");
67 check_string(do_printf("%ju", (uintmax_t)10000000000LL), "10000000000");
68 check_string(do_printf("%zu", (size_t)2000000000), "2000000000");
69 check_string(do_printf("%tu", (ptrdiff_t)2000000000), "2000000000");
70 check_string(do_printf("%p", (void *)0x100), "0x100");
71 check_string(do_printf("%s", "wibble"), "wibble");
72 check_string(do_printf("%s-%s", "wibble", "wobble"), "wibble-wobble");
73 check_string(do_printf("%10s", "wibble"), " wibble");
74 check_string(do_printf("%010s", "wibble"), " wibble"); /* 0 ignored for %s */
75 check_string(do_printf("%-10s", "wibble"), "wibble ");
76 check_string(do_printf("%2s", "wibble"), "wibble");
77 check_string(do_printf("%.2s", "wibble"), "wi");
78 check_string(do_printf("%.2s", "w"), "w");
79 check_string(do_printf("%4.2s", "wibble"), " wi");
80 check_string(do_printf("%c", 'a'), "a");
81 check_string(do_printf("%4c", 'a'), " a");
82 check_string(do_printf("%-4c", 'a'), "a ");
83 check_string(do_printf("%*c", 0, 'a'), "a");
84 check_string(do_printf("x%hhny", &c), "xy");
85 insist(c == 1);
86 check_string(do_printf("xx%hnyy", &s), "xxyy");
87 insist(s == 2);
88 check_string(do_printf("xxx%nyyy", &i), "xxxyyy");
89 insist(i == 3);
90 check_string(do_printf("xxxx%lnyyyy", &l), "xxxxyyyy");
91 insist(l == 4);
92 check_string(do_printf("xxxxx%llnyyyyy", &ll), "xxxxxyyyyy");
93 insist(ll == 5);
94 check_string(do_printf("xxxxxx%jnyyyyyy", &m), "xxxxxxyyyyyy");
95 insist(m == 6);
96 check_string(do_printf("xxxxxxx%znyyyyyyy", &ssz), "xxxxxxxyyyyyyy");
97 insist(ssz == 7);
98 check_string(do_printf("xxxxxxxx%tnyyyyyyyy", &p), "xxxxxxxxyyyyyyyy");
99 insist(p == 8);
100 check_string(do_printf("%*d", 5, 99), " 99");
101 check_string(do_printf("%*d", -5, 99), "99 ");
102 check_string(do_printf("%.*d", 5, 99), "00099");
103 check_string(do_printf("%.*d", -5, 99), "99");
104 check_string(do_printf("%.0d", 0), "");
105 check_string(do_printf("%.d", 0), "");
106 check_string(do_printf("%.d", 0), "");
107 check_string(do_printf("%%"), "%");
108 check_string(do_printf("wibble"), "wibble");
109 insist(do_printf("%") == 0);
110 insist(do_printf("%=") == 0);
111 i = byte_asprintf(&cp, "xyzzy %d", 999);
112 insist(i == 9);
113 check_string(cp, "xyzzy 999");
114 i = byte_snprintf(buffer, sizeof buffer, "xyzzy %d", 999);
115 insist(i == 9);
116 check_string(buffer, "xyzzy 999");
117 i = byte_snprintf(buffer, sizeof buffer, "%*d", 32, 99);
118 insist(i == 32);
119 check_string(buffer, " ");
120 {
121 /* bizarre workaround for compiler checking of format strings */
122 char f[] = "xyzzy %";
123 i = byte_asprintf(&cp, f);
124 insist(i == -1);
125 }
f61b995d
RK
126
127 fp = tmpfile();
128 insist(byte_fprintf(fp, "%10s\n", "wibble") == 11);
129 rewind(fp);
130 insist(fgets(buffer, sizeof buffer, fp) == buffer);
131 check_string(buffer, " wibble\n");
132 fclose(fp);
b90f122b
RK
133}
134
135/*
136Local Variables:
137c-basic-offset:2
138comment-column:40
139fill-column:79
140indent-tabs-mode:nil
141End:
142*/