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