chiark / gitweb /
lib/printf.h, libtests/t-printf.c: Bodge to inhibit warning from GCC 9.
[disorder] / libtests / t-printf.c
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 3 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,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU 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, see <http://www.gnu.org/licenses/>.
17  */
18 #define INHIBIT_PRINTF_FORMAT_CHECKING
19 #include "test.h"
20
21 /* launder a string constant to stop gcc warnings */
22 static const char *L(const char *s) {
23   return s;
24 }
25
26 static void test_printf(void) {
27   char c;
28   short s;
29   int i;
30   long l;
31   long long ll;
32   intmax_t m;
33   ssize_t ssz;
34   ptrdiff_t p;
35   char *cp;
36   char buffer[16];
37   FILE *fp;
38   
39   check_string(do_printf("%d", 999), "999");
40   check_string(do_printf("%d", -999), "-999");
41   check_string(do_printf("%+d", 999), "+999");
42   check_string(do_printf("%+d", -999), "-999");
43   check_string(do_printf("%i", 999), "999");
44   check_string(do_printf("%i", -999), "-999");
45   check_string(do_printf("%u", 999), "999");
46   check_string(do_printf("%2u", 999), "999");
47   check_string(do_printf("%10u", 999), "       999");
48   check_string(do_printf("%-10u", 999), "999       ");
49   check_string(do_printf("%010u", 999), "0000000999");
50   check_string(do_printf("%-10d", -999), "-999      ");
51   check_string(do_printf("%-010d", -999), "-999      "); /* "-" beats "0" */
52   check_string(do_printf("%66u", 999), "                                                               999");
53   check_string(do_printf("%o", 999), "1747");
54   check_string(do_printf("%#o", 999), "01747");
55   check_string(do_printf("%#o", 0), "0");
56   check_string(do_printf("%x", 999), "3e7");
57   check_string(do_printf("%#x", 999), "0x3e7");
58   check_string(do_printf("%#X", 999), "0X3E7");
59   check_string(do_printf("%#x", 0), "0");
60   check_string(do_printf("%hd", (short)999), "999");
61   check_string(do_printf("%hhd", (short)99), "99");
62   check_string(do_printf("%ld", 100000L), "100000");
63   check_string(do_printf("%lld", 10000000000LL), "10000000000");
64   check_string(do_printf("%qd", 10000000000LL), "10000000000");
65   check_string(do_printf("%jd", (intmax_t)10000000000LL), "10000000000");
66   check_string(do_printf("%zd", (ssize_t)2000000000), "2000000000");
67   check_string(do_printf("%td", (ptrdiff_t)2000000000), "2000000000");
68   check_string(do_printf("%hu", (short)999), "999");
69   check_string(do_printf("%hhu", (short)99), "99");
70   check_string(do_printf("%lu", 100000L), "100000");
71   check_string(do_printf("%llu", 10000000000LL), "10000000000");
72   check_string(do_printf("%ju", (uintmax_t)10000000000LL), "10000000000");
73   check_string(do_printf("%zu", (size_t)2000000000), "2000000000");
74   check_string(do_printf("%tu", (ptrdiff_t)2000000000), "2000000000");
75   check_string(do_printf("%p", (void *)0x100), "0x100");
76   check_string(do_printf("%s", "wibble"), "wibble");
77   check_string(do_printf("%s-%s", "wibble", "wobble"), "wibble-wobble");
78   check_string(do_printf("%10s", "wibble"), "    wibble");
79   check_string(do_printf("%010s", "wibble"), "    wibble"); /* 0 ignored for %s */
80   check_string(do_printf("%-10s", "wibble"), "wibble    ");
81   check_string(do_printf("%2s", "wibble"), "wibble");
82   check_string(do_printf("%.2s", "wibble"), "wi");
83   check_string(do_printf("%.2s", "w"), "w");
84   check_string(do_printf("%4.2s", "wibble"), "  wi");
85   check_string(do_printf("%c", 'a'), "a");
86   check_string(do_printf("%4c", 'a'), "   a");
87   check_string(do_printf("%-4c", 'a'), "a   ");
88   check_string(do_printf("%*c", 0, 'a'), "a");
89   check_string(do_printf("x%hhny", &c), "xy");
90   insist(c == 1);
91   check_string(do_printf("xx%hnyy", &s), "xxyy");
92   insist(s == 2);
93   check_string(do_printf("xxx%nyyy", &i), "xxxyyy");
94   insist(i == 3);
95   check_string(do_printf("xxxx%lnyyyy", &l), "xxxxyyyy");
96   insist(l == 4);
97   check_string(do_printf("xxxxx%llnyyyyy", &ll), "xxxxxyyyyy");
98   insist(ll == 5);
99   check_string(do_printf("xxxxxx%jnyyyyyy", &m), "xxxxxxyyyyyy");
100   insist(m == 6);
101   check_string(do_printf("xxxxxxx%znyyyyyyy", &ssz), "xxxxxxxyyyyyyy");
102   insist(ssz == 7);
103   check_string(do_printf("xxxxxxxx%tnyyyyyyyy", &p), "xxxxxxxxyyyyyyyy");
104   insist(p == 8);
105   check_string(do_printf("%*d", 5, 99), "   99");
106   check_string(do_printf("%*d", -5, 99), "99   ");
107   check_string(do_printf("%.*d", 5, 99), "00099");
108   check_string(do_printf("%.*d", -5, 99), "99");
109   check_string(do_printf("%.0d", 0), "");
110   check_string(do_printf("%.d", 0), "");
111   check_string(do_printf("%.d", 0), "");
112   check_string(do_printf("%%"), "%");
113   check_string(do_printf("wibble"), "wibble");
114   insist(do_printf("%") == 0);
115   insist(do_printf("%=") == 0);
116   i = byte_asprintf(&cp, "xyzzy %d", 999);
117   insist(i == 9);
118   check_string(cp, "xyzzy 999");
119   i = byte_snprintf(buffer, sizeof buffer, "xyzzy %d", 999);
120   insist(i == 9);
121   check_string(buffer, "xyzzy 999");
122   i = byte_snprintf(buffer, sizeof buffer, "%*d", 32, 99);
123   insist(i == 32);
124   check_string(buffer, "               ");
125   {
126     /* bizarre workaround for compiler checking of format strings */
127     char f[] = "xyzzy %";
128     i = byte_asprintf(&cp, f, 0);
129     insist(i == -1);
130   }
131
132   fp = tmpfile();
133   insist(byte_fprintf(fp, "%10s\n", "wibble") == 11);
134   rewind(fp);
135   insist(fgets(buffer, sizeof buffer, fp) == buffer);
136   check_string(buffer, "    wibble\n");
137   fclose(fp);
138   check_integer(byte_snprintf(buffer, sizeof buffer,
139                               "%18446744073709551616d", 10), -1);
140   check_integer(byte_snprintf(buffer, sizeof buffer,
141                               "%.18446744073709551616d", 10), -1);
142   check_integer(byte_snprintf(buffer, sizeof buffer, L("%hs"), ""), -1);
143   check_integer(byte_snprintf(buffer, sizeof buffer, L("%qs"), ""), -1);
144   check_integer(byte_snprintf(buffer, sizeof buffer, L("%js"), ""), -1);
145   check_integer(byte_snprintf(buffer, sizeof buffer, L("%zs"), ""), -1);
146   check_integer(byte_snprintf(buffer, sizeof buffer, L("%ts"), ""), -1);
147   check_integer(byte_snprintf(buffer, sizeof buffer, L("%Ls"), ""), -1);
148   check_integer(byte_snprintf(buffer, sizeof buffer, L("%hp"), (void *)0), -1);
149   check_integer(byte_snprintf(buffer, sizeof buffer, L("%lp"), (void *)0), -1);
150   check_integer(byte_snprintf(buffer, sizeof buffer, L("%qp"), (void *)0), -1);
151   check_integer(byte_snprintf(buffer, sizeof buffer, L("%jp"), (void *)0), -1);
152   check_integer(byte_snprintf(buffer, sizeof buffer, L("%zp"), (void *)0), -1);
153   check_integer(byte_snprintf(buffer, sizeof buffer, L("%tp"), (void *)0), -1);
154   check_integer(byte_snprintf(buffer, sizeof buffer, L("%Lp"), (void *)0), -1);
155   check_integer(byte_snprintf(buffer, sizeof buffer, L("%h%"), 0), -1);
156   check_integer(byte_snprintf(buffer, sizeof buffer, L("%l%"), 0), -1);
157   check_integer(byte_snprintf(buffer, sizeof buffer, L("%q%"), 0), -1);
158   check_integer(byte_snprintf(buffer, sizeof buffer, L("%j%"), 0), -1);
159   check_integer(byte_snprintf(buffer, sizeof buffer, L("%z%"), 0), -1);
160   check_integer(byte_snprintf(buffer, sizeof buffer, L("%t%"), 0), -1);
161   check_integer(byte_snprintf(buffer, sizeof buffer, L("%L%"), 0), -1);
162   check_integer(byte_snprintf(buffer, sizeof buffer, "%2147483647s%2147483647s", "", ""), -1);
163   check_integer(byte_sinkprintf(sink_error(), ""), 0);
164   check_integer(byte_sinkprintf(sink_error(), "%5s", ""), -1);
165   check_integer(byte_sinkprintf(sink_error(), "%d", 0), -1);
166   check_integer(byte_sinkprintf(sink_error(), "%d", 1), -1);
167   check_integer(byte_sinkprintf(sink_error(), "%2d", 0), -1);
168   check_integer(byte_sinkprintf(sink_error(), "%d", -1), -1);
169   check_integer(byte_sinkprintf(sink_error(), "%#x", 10), -1);
170   check_integer(byte_sinkprintf(sink_error(), "%-d", 0), -1);
171   check_integer(byte_sinkprintf(sink_error(), "%-d", 1), -1);
172   check_integer(byte_sinkprintf(sink_error(), "%-2d", 0), -1);
173   check_integer(byte_sinkprintf(sink_error(), "%-d", -1), -1);
174   check_integer(byte_sinkprintf(sink_error(), "%-#x", 10), -1);
175
176 }
177
178 TEST(printf);
179
180 /*
181 Local Variables:
182 c-basic-offset:2
183 comment-column:40
184 fill-column:79
185 indent-tabs-mode:nil
186 End:
187 */