]> git.sur5r.net Git - cc65/blob - testcode/lib/sprintf-test.c
un-remove TABs in doc/using-make.sgml
[cc65] / testcode / lib / sprintf-test.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdarg.h>
4 #if defined(__CC65__)
5 #include <conio.h>
6 #endif
7
8
9 /* Flag to #ifdef the tests out that crash the old implementation */
10 /*#define NOCRASH         1 */
11
12
13
14 /*****************************************************************************/
15 /*                                   Code                                    */
16 /*****************************************************************************/
17
18
19
20 /* Struct used to test the 'n' conversion specifier. It is machine dependent /
21 ** not portable.
22 */
23 typedef union WriteCount WriteCount;
24 union WriteCount {
25     signed char C;
26     int         I;
27     long        L;
28 };
29
30 /* Count the number of tests and the number of failures */
31 static unsigned Tests = 0;
32 static unsigned Failures = 0;
33
34
35
36 /*****************************************************************************/
37 /*                                   Code                                    */
38 /*****************************************************************************/
39
40
41
42 static void OneTest (int Line, const char* RString, int RCount, const char* Format, ...)
43 /* Test one conversion. Line is the line number (to make the life of the
44 ** tester easier), RString the expected result and RCount the expected return
45 ** count. The other parameters are used for formatting.
46 */
47 {
48     int Count;
49     char Buf[128];
50     va_list ap;
51
52     /* Count the number of tests */
53     ++Tests;
54
55     /* Format the string using the given arguments */
56     va_start (ap, Format);
57     Count = vsprintf (Buf, Format, ap);
58     va_end (ap);
59
60     /* Check the result */
61     if (Count != RCount || strcmp (Buf, RString) != 0) {
62         ++Failures;
63         printf ("%3d: \"%s\" (%d)\n"
64                 "     \"%s\" (%d)\n",
65                 Line, Buf, Count, RString, RCount);
66     }
67 }
68
69
70
71 static void WriteTest (int Line, const char* Format, WriteCount* W, long Count)
72 /* Test one write conversion. Line is the line number (to make the life of the
73 ** tester easier), Format is the format specification. W is a WriteCount
74 ** variable and Count is the expected result.
75 */
76 {
77     /* Clear the counter in full length */
78     W->L = 0x5A5A5A5AL;
79
80     /* Format the string using the given arguments */
81     OneTest (Line, "4200", 4, Format, 4200, W);
82
83     /* Check the counter */
84     if (W->L != Count) {
85         ++Failures;
86         printf ("%3d: n = 0x%08lX\n"
87                 "     n = 0x%08lX\n",
88                 Line, W->L, Count);
89     }
90 }
91
92
93
94 int main (void)
95 {
96     WriteCount W;
97
98
99     /* The one and only starter to begin with ... */
100     OneTest (__LINE__, "hello, world",            12, "hello, world");
101
102     /* Duplicate percent signs are used to output single ones */
103     OneTest (__LINE__, "hello % %",                9, "hello %% %%");
104
105     /* Simple conversions */
106     OneTest (__LINE__, "u",                        1, "%c", 'u');
107     OneTest (__LINE__, "4200",                     4, "%d", 4200);
108     OneTest (__LINE__, "-4200",                    5, "%d", -4200);
109     OneTest (__LINE__, "4200",                     4, "%i", 4200);
110     OneTest (__LINE__, "-4200",                    5, "%i", -4200);
111     OneTest (__LINE__, "10150",                    5, "%o", 4200U);
112     OneTest (__LINE__, "167630",                   6, "%o", -4200U);
113     OneTest (__LINE__, "hello, world",            12, "hello, %s", "world");
114     OneTest (__LINE__, "4200",                     4, "%u", 4200U);
115     OneTest (__LINE__, "61336",                    5, "%u", -4200U);
116     OneTest (__LINE__, "1068",                     4, "%x", 4200U);
117     OneTest (__LINE__, "ef98",                     4, "%x", -4200U);
118     OneTest (__LINE__, "1068",                     4, "%X", 4200U);
119     OneTest (__LINE__, "EF98",                     4, "%X", -4200U);
120
121     /* Simple conversions with special values */
122     OneTest (__LINE__, "\0",                       1, "%c", '\0');
123     OneTest (__LINE__, "0",                        1, "%d", 0);
124     OneTest (__LINE__, "0",                        1, "%o", 0U);
125     OneTest (__LINE__, "hello, ",                  7, "hello, %s", "");
126     OneTest (__LINE__, "0",                        1, "%u", 0U);
127     OneTest (__LINE__, "0",                        1, "%x", 0U);
128
129     /* 'h' modifier */
130     OneTest (__LINE__, "4200",                     4, "%hd", 4200);
131     OneTest (__LINE__, "-4200",                    5, "%hd", -4200);
132     OneTest (__LINE__, "4200",                     4, "%hi", 4200);
133     OneTest (__LINE__, "-4200",                    5, "%hi", -4200);
134     OneTest (__LINE__, "10150",                    5, "%ho", 4200U);
135     OneTest (__LINE__, "167630",                   6, "%ho", -4200U);
136     OneTest (__LINE__, "4200",                     4, "%hu", 4200U);
137     OneTest (__LINE__, "61336",                    5, "%hu", -4200U);
138     OneTest (__LINE__, "1068",                     4, "%hx", 4200U);
139     OneTest (__LINE__, "ef98",                     4, "%hx", -4200U);
140     OneTest (__LINE__, "1068",                     4, "%hX", 4200U);
141     OneTest (__LINE__, "EF98",                     4, "%hX", -4200U);
142
143     /* 'hh' modifier */
144     OneTest (__LINE__, "104",                      3, "%hhd", 4200);
145     OneTest (__LINE__, "-104",                     4, "%hhd", -4200);
146     OneTest (__LINE__, "104",                      3, "%hhi", 4200);
147     OneTest (__LINE__, "-104",                     4, "%hhi", -4200);
148     OneTest (__LINE__, "150",                      3, "%hho", 4200U);
149     OneTest (__LINE__, "230",                      3, "%hho", -4200U);
150     OneTest (__LINE__, "104",                      3, "%hhu", 4200U);
151     OneTest (__LINE__, "152",                      3, "%hhu", -4200U);
152     OneTest (__LINE__, "68",                       2, "%hhx", 4200U);
153     OneTest (__LINE__, "98",                       2, "%hhx", -4200U);
154     OneTest (__LINE__, "68",                       2, "%hhX", 4200U);
155     OneTest (__LINE__, "98",                       2, "%hhX", -4200U);
156
157     /* 'j' modifier */
158     OneTest (__LINE__, "4200123",                  7, "%jd", 4200123L);
159     OneTest (__LINE__, "-4200123",                 8, "%jd", -4200123L);
160     OneTest (__LINE__, "4200123",                  7, "%ji", 4200123L);
161     OneTest (__LINE__, "-4200123",                 8, "%ji", -4200123L);
162     OneTest (__LINE__, "20013273",                 8, "%jo", 4200123UL);
163     OneTest (__LINE__, "37757764505",             11, "%jo", -4200123UL);
164     OneTest (__LINE__, "4200123",                  7, "%ju", 4200123UL);
165     OneTest (__LINE__, "4290767173",              10, "%ju", -4200123UL);
166     OneTest (__LINE__, "4016bb",                   6, "%jx", 4200123UL);
167     OneTest (__LINE__, "ffbfe945",                 8, "%jx", -4200123UL);
168     OneTest (__LINE__, "4016BB",                   6, "%jX", 4200123UL);
169     OneTest (__LINE__, "FFBFE945",                 8, "%jX", -4200123UL);
170
171     /* 'l' modifier */
172     OneTest (__LINE__, "4200123",                  7, "%ld", 4200123L);
173     OneTest (__LINE__, "-4200123",                 8, "%ld", -4200123L);
174     OneTest (__LINE__, "4200123",                  7, "%li", 4200123L);
175     OneTest (__LINE__, "-4200123",                 8, "%li", -4200123L);
176     OneTest (__LINE__, "20013273",                 8, "%lo", 4200123UL);
177     OneTest (__LINE__, "37757764505",             11, "%lo", -4200123UL);
178     OneTest (__LINE__, "4200123",                  7, "%lu", 4200123UL);
179     OneTest (__LINE__, "4290767173",              10, "%lu", -4200123UL);
180     OneTest (__LINE__, "4016bb",                   6, "%lx", 4200123UL);
181     OneTest (__LINE__, "ffbfe945",                 8, "%lx", -4200123UL);
182     OneTest (__LINE__, "4016BB",                   6, "%lX", 4200123UL);
183     OneTest (__LINE__, "FFBFE945",                 8, "%lX", -4200123UL);
184
185     /* 't' modifier */
186     OneTest (__LINE__, "4200",                     4, "%td", 4200);
187     OneTest (__LINE__, "-4200",                    5, "%td", -4200);
188     OneTest (__LINE__, "4200",                     4, "%ti", 4200);
189     OneTest (__LINE__, "-4200",                    5, "%ti", -4200);
190     OneTest (__LINE__, "10150",                    5, "%to", 4200U);
191     OneTest (__LINE__, "167630",                   6, "%to", -4200U);
192     OneTest (__LINE__, "4200",                     4, "%tu", 4200U);
193     OneTest (__LINE__, "61336",                    5, "%tu", -4200U);
194     OneTest (__LINE__, "1068",                     4, "%tx", 4200U);
195     OneTest (__LINE__, "ef98",                     4, "%tx", -4200U);
196     OneTest (__LINE__, "1068",                     4, "%tX", 4200U);
197     OneTest (__LINE__, "EF98",                     4, "%tX", -4200U);
198
199     /* 'z' modifier */
200     OneTest (__LINE__, "4200",                     4, "%zd", 4200);
201     OneTest (__LINE__, "-4200",                    5, "%zd", -4200);
202     OneTest (__LINE__, "4200",                     4, "%zi", 4200);
203     OneTest (__LINE__, "-4200",                    5, "%zi", -4200);
204     OneTest (__LINE__, "10150",                    5, "%zo", 4200U);
205     OneTest (__LINE__, "167630",                   6, "%zo", -4200U);
206     OneTest (__LINE__, "4200",                     4, "%zu", 4200U);
207     OneTest (__LINE__, "61336",                    5, "%zu", -4200U);
208     OneTest (__LINE__, "1068",                     4, "%zx", 4200U);
209     OneTest (__LINE__, "ef98",                     4, "%zx", -4200U);
210     OneTest (__LINE__, "1068",                     4, "%zX", 4200U);
211     OneTest (__LINE__, "EF98",                     4, "%zX", -4200U);
212
213     /* '+' forces a sign for signed conversions */
214     OneTest (__LINE__, "u",                        1, "%+c", 'u');
215     OneTest (__LINE__, "+4200",                    5, "%+d", 4200);
216     OneTest (__LINE__, "-4200",                    5, "%+d", -4200);
217     OneTest (__LINE__, "+4200",                    5, "%+i", 4200);
218     OneTest (__LINE__, "-4200",                    5, "%+i", -4200);
219     OneTest (__LINE__, "10150",                    5, "%+o", 4200U);
220     OneTest (__LINE__, "167630",                   6, "%+o", -4200U);
221     OneTest (__LINE__, "hello, world",            12, "%+s", "hello, world");
222     OneTest (__LINE__, "4200",                     4, "%+u", 4200U);
223     OneTest (__LINE__, "61336",                    5, "%+u", -4200U);
224     OneTest (__LINE__, "1068",                     4, "%+x", 4200U);
225     OneTest (__LINE__, "ef98",                     4, "%+x", -4200U);
226     OneTest (__LINE__, "1068",                     4, "%+X", 4200U);
227     OneTest (__LINE__, "EF98",                     4, "%+X", -4200U);
228
229     /* ' ': If the first character of a signed conversion is not a sign, or if
230     **      a signed conversion results in no characters, a space is prefixed
231     **      to the result.
232     */
233     OneTest (__LINE__, "u",                        1, "% c", 'u');
234     OneTest (__LINE__, " 4200",                    5, "% d", 4200);
235     OneTest (__LINE__, "-4200",                    5, "% d", -4200);
236     OneTest (__LINE__, " 4200",                    5, "% i", 4200);
237     OneTest (__LINE__, "-4200",                    5, "% i", -4200);
238     OneTest (__LINE__, "10150",                    5, "% o", 4200U);
239     OneTest (__LINE__, "167630",                   6, "% o", -4200U);
240     OneTest (__LINE__, "hello, world",            12, "% s", "hello, world");
241     OneTest (__LINE__, "4200",                     4, "% u", 4200U);
242     OneTest (__LINE__, "61336",                    5, "% u", -4200U);
243     OneTest (__LINE__, "1068",                     4, "% x", 4200U);
244     OneTest (__LINE__, "ef98",                     4, "% x", -4200U);
245     OneTest (__LINE__, "1068",                     4, "% X", 4200U);
246     OneTest (__LINE__, "EF98",                     4, "% X", -4200U);
247
248     /* If the ' ' and '+' flags both appear, the ' ' flag is ignored */
249     OneTest (__LINE__, "u",                        1, "% +c", 'u');
250     OneTest (__LINE__, "+4200",                    5, "% +d", 4200);
251     OneTest (__LINE__, "-4200",                    5, "% +d", -4200);
252     OneTest (__LINE__, "+4200",                    5, "% +i", 4200);
253     OneTest (__LINE__, "-4200",                    5, "% +i", -4200);
254     OneTest (__LINE__, "10150",                    5, "% +o", 4200U);
255     OneTest (__LINE__, "167630",                   6, "% +o", -4200U);
256     OneTest (__LINE__, "hello, world",            12, "% +s", "hello, world");
257     OneTest (__LINE__, "4200",                     4, "% +u", 4200U);
258     OneTest (__LINE__, "61336",                    5, "% +u", -4200U);
259     OneTest (__LINE__, "1068",                     4, "% +x", 4200U);
260     OneTest (__LINE__, "ef98",                     4, "% +x", -4200U);
261     OneTest (__LINE__, "1068",                     4, "% +X", 4200U);
262     OneTest (__LINE__, "EF98",                     4, "% +X", -4200U);
263
264     /* Field width given */
265     OneTest (__LINE__, "              u",         15, "%15c", 'u');
266     OneTest (__LINE__, "           4200",         15, "%15d", 4200);
267     OneTest (__LINE__, "          -4200",         15, "%15d", -4200);
268     OneTest (__LINE__, "           4200",         15, "%15i", 4200);
269     OneTest (__LINE__, "          -4200",         15, "%15i", -4200);
270     OneTest (__LINE__, "          10150",         15, "%15o", 4200U);
271     OneTest (__LINE__, "         167630",         15, "%15o", -4200U);
272     OneTest (__LINE__, "   hello, world",         15, "%15s", "hello, world");
273     OneTest (__LINE__, "           4200",         15, "%15u", 4200U);
274     OneTest (__LINE__, "          61336",         15, "%15u", -4200U);
275     OneTest (__LINE__, "           1068",         15, "%15x", 4200U);
276     OneTest (__LINE__, "           ef98",         15, "%15x", -4200U);
277     OneTest (__LINE__, "           1068",         15, "%15X", 4200U);
278     OneTest (__LINE__, "           EF98",         15, "%15X", -4200U);
279
280     /* Field width given as separate argument */
281     OneTest (__LINE__, "              u",         15, "%*c", 15, 'u');
282     OneTest (__LINE__, "           4200",         15, "%*d", 15, 4200);
283     OneTest (__LINE__, "          -4200",         15, "%*d", 15, -4200);
284     OneTest (__LINE__, "           4200",         15, "%*i", 15, 4200);
285     OneTest (__LINE__, "          -4200",         15, "%*i", 15, -4200);
286     OneTest (__LINE__, "          10150",         15, "%*o", 15, 4200U);
287     OneTest (__LINE__, "         167630",         15, "%*o", 15, -4200U);
288     OneTest (__LINE__, "   hello, world",         15, "%*s", 15, "hello, world");
289     OneTest (__LINE__, "           4200",         15, "%*u", 15, 4200U);
290     OneTest (__LINE__, "          61336",         15, "%*u", 15, -4200U);
291     OneTest (__LINE__, "           1068",         15, "%*x", 15, 4200U);
292     OneTest (__LINE__, "           ef98",         15, "%*x", 15, -4200U);
293     OneTest (__LINE__, "           1068",         15, "%*X", 15, 4200U);
294     OneTest (__LINE__, "           EF98",         15, "%*X", 15, -4200U);
295
296     /* Field width and '-' flag given */
297     OneTest (__LINE__, "u              ",         15, "%-15c", 'u');
298     OneTest (__LINE__, "4200           ",         15, "%-15d", 4200);
299     OneTest (__LINE__, "-4200          ",         15, "%-15d", -4200);
300     OneTest (__LINE__, "4200           ",         15, "%-15i", 4200);
301     OneTest (__LINE__, "-4200          ",         15, "%-15i", -4200);
302     OneTest (__LINE__, "10150          ",         15, "%-15o", 4200U);
303     OneTest (__LINE__, "167630         ",         15, "%-15o", -4200U);
304     OneTest (__LINE__, "hello, world   ",         15, "%-15s", "hello, world");
305     OneTest (__LINE__, "4200           ",         15, "%-15u", 4200U);
306     OneTest (__LINE__, "61336          ",         15, "%-15u", -4200U);
307     OneTest (__LINE__, "1068           ",         15, "%-15x", 4200U);
308     OneTest (__LINE__, "ef98           ",         15, "%-15x", -4200U);
309     OneTest (__LINE__, "1068           ",         15, "%-15X", 4200U);
310     OneTest (__LINE__, "EF98           ",         15, "%-15X", -4200U);
311
312     /* A negative field width specified via an argument is treated as if the
313     ** '-' flag and a positive field width were given.
314     **
315     ** Beware: These tests will crash the old printf routine!
316     */
317 #ifndef NOCRASH
318     OneTest (__LINE__, "u              ",         15, "%*c", -15, 'u');
319     OneTest (__LINE__, "4200           ",         15, "%*d", -15, 4200);
320     OneTest (__LINE__, "-4200          ",         15, "%*d", -15, -4200);
321     OneTest (__LINE__, "4200           ",         15, "%*i", -15, 4200);
322     OneTest (__LINE__, "-4200          ",         15, "%*i", -15, -4200);
323     OneTest (__LINE__, "10150          ",         15, "%*o", -15, 4200U);
324     OneTest (__LINE__, "167630         ",         15, "%*o", -15, -4200U);
325     OneTest (__LINE__, "hello, world   ",         15, "%*s", -15, "hello, world");
326     OneTest (__LINE__, "4200           ",         15, "%*u", -15, 4200U);
327     OneTest (__LINE__, "61336          ",         15, "%*u", -15, -4200U);
328     OneTest (__LINE__, "1068           ",         15, "%*x", -15, 4200U);
329     OneTest (__LINE__, "ef98           ",         15, "%*x", -15, -4200U);
330     OneTest (__LINE__, "1068           ",         15, "%*X", -15, 4200U);
331     OneTest (__LINE__, "EF98           ",         15, "%*X", -15, -4200U);
332 #endif
333
334     /* Field width smaller than converted value */
335     OneTest (__LINE__, "u",                        1, "%1c", 'u');
336     OneTest (__LINE__, "4200",                     4, "%1d", 4200);
337     OneTest (__LINE__, "-4200",                    5, "%1d", -4200);
338     OneTest (__LINE__, "4200",                     4, "%1i", 4200);
339     OneTest (__LINE__, "-4200",                    5, "%1i", -4200);
340     OneTest (__LINE__, "10150",                    5, "%1o", 4200U);
341     OneTest (__LINE__, "167630",                   6, "%1o", -4200U);
342     OneTest (__LINE__, "hello, world",            12, "%1s", "hello, world");
343     OneTest (__LINE__, "4200",                     4, "%1u", 4200U);
344     OneTest (__LINE__, "61336",                    5, "%1u", -4200U);
345     OneTest (__LINE__, "1068",                     4, "%1x", 4200U);
346     OneTest (__LINE__, "ef98",                     4, "%1x", -4200U);
347     OneTest (__LINE__, "1068",                     4, "%1X", 4200U);
348     OneTest (__LINE__, "EF98",                     4, "%1X", -4200U);
349
350     /* Field width specified and '0' flag given */
351     OneTest (__LINE__, "000000000004200",         15, "%015d", 4200);
352     OneTest (__LINE__, "-00000000004200",         15, "%015d", -4200);
353     OneTest (__LINE__, "000000000004200",         15, "%015i", 4200);
354     OneTest (__LINE__, "-00000000004200",         15, "%015i", -4200);
355     OneTest (__LINE__, "000000000010150",         15, "%015o", 4200U);
356     OneTest (__LINE__, "000000000167630",         15, "%015o", -4200U);
357     OneTest (__LINE__, "000000000004200",         15, "%015u", 4200U);
358     OneTest (__LINE__, "000000000061336",         15, "%015u", -4200U);
359     OneTest (__LINE__, "000000000001068",         15, "%015x", 4200U);
360     OneTest (__LINE__, "00000000000ef98",         15, "%015x", -4200U);
361     OneTest (__LINE__, "000000000001068",         15, "%015X", 4200U);
362     OneTest (__LINE__, "00000000000EF98",         15, "%015X", -4200U);
363
364     /* If the '-' and '0' flags are both specified, '0' is ignored */
365     OneTest (__LINE__, "4200           ",         15, "%-015d", 4200);
366     OneTest (__LINE__, "-4200          ",         15, "%-015d", -4200);
367     OneTest (__LINE__, "4200           ",         15, "%-015i", 4200);
368     OneTest (__LINE__, "-4200          ",         15, "%-015i", -4200);
369     OneTest (__LINE__, "10150          ",         15, "%-015o", 4200U);
370     OneTest (__LINE__, "167630         ",         15, "%-015o", -4200U);
371     OneTest (__LINE__, "4200           ",         15, "%-015u", 4200U);
372     OneTest (__LINE__, "61336          ",         15, "%-015u", -4200U);
373     OneTest (__LINE__, "1068           ",         15, "%-015x", 4200U);
374     OneTest (__LINE__, "ef98           ",         15, "%-015x", -4200U);
375     OneTest (__LINE__, "1068           ",         15, "%-015X", 4200U);
376     OneTest (__LINE__, "EF98           ",         15, "%-015X", -4200U);
377
378     /* Precision given */
379     OneTest (__LINE__, "u",                        1, "%.15c", 'u');
380     OneTest (__LINE__, "000000000004200",         15, "%.15d", 4200);
381     OneTest (__LINE__, "-000000000004200",        16, "%.15d", -4200);
382     OneTest (__LINE__, "000000000004200",         15, "%.15i", 4200);
383     OneTest (__LINE__, "-000000000004200",        16, "%.15i", -4200);
384     OneTest (__LINE__, "000000000010150",         15, "%.15o", 4200U);
385     OneTest (__LINE__, "000000000167630",         15, "%.15o", -4200U);
386     OneTest (__LINE__, "hello, world",            12, "%.15s", "hello, world");
387     OneTest (__LINE__, "000000000004200",         15, "%.15u", 4200U);
388     OneTest (__LINE__, "000000000061336",         15, "%.15u", -4200U);
389     OneTest (__LINE__, "000000000001068",         15, "%.15x", 4200U);
390     OneTest (__LINE__, "00000000000ef98",         15, "%.15x", -4200U);
391     OneTest (__LINE__, "000000000001068",         15, "%.15X", 4200U);
392     OneTest (__LINE__, "00000000000EF98",         15, "%.15X", -4200U);
393
394     /* Precision given via argument */
395     OneTest (__LINE__, "u",                        1, "%.*c", 15, 'u');
396     OneTest (__LINE__, "000000000004200",         15, "%.*d", 15, 4200);
397     OneTest (__LINE__, "-000000000004200",        16, "%.*d", 15, -4200);
398     OneTest (__LINE__, "000000000004200",         15, "%.*i", 15, 4200);
399     OneTest (__LINE__, "-000000000004200",        16, "%.*i", 15, -4200);
400     OneTest (__LINE__, "000000000010150",         15, "%.*o", 15, 4200U);
401     OneTest (__LINE__, "000000000167630",         15, "%.*o", 15, -4200U);
402     OneTest (__LINE__, "hello, world",            12, "%.*s", 15, "hello, world");
403     OneTest (__LINE__, "000000000004200",         15, "%.*u", 15, 4200U);
404     OneTest (__LINE__, "000000000061336",         15, "%.*u", 15, -4200U);
405     OneTest (__LINE__, "000000000001068",         15, "%.*x", 15, 4200U);
406     OneTest (__LINE__, "00000000000ef98",         15, "%.*x", 15, -4200U);
407     OneTest (__LINE__, "000000000001068",         15, "%.*X", 15, 4200U);
408     OneTest (__LINE__, "00000000000EF98",         15, "%.*X", 15, -4200U);
409
410     /* Negative precision is treated as if the precision were omitted */
411 #ifndef NOCRASH
412     OneTest (__LINE__, "u",                        1, "%.*c", -15, 'u');
413     OneTest (__LINE__, "4200",                     4, "%.*d", -15, 4200);
414     OneTest (__LINE__, "-4200",                    5, "%.*d", -15, -4200);
415     OneTest (__LINE__, "4200",                     4, "%.*i", -15, 4200);
416     OneTest (__LINE__, "-4200",                    5, "%.*i", -15, -4200);
417     OneTest (__LINE__, "10150",                    5, "%.*o", -15, 4200U);
418     OneTest (__LINE__, "167630",                   6, "%.*o", -15, -4200U);
419     OneTest (__LINE__, "hello, world",            12, "%.*s", -15, "hello, world");
420     OneTest (__LINE__, "4200",                     4, "%.*u", -15, 4200U);
421     OneTest (__LINE__, "61336",                    5, "%.*u", -15, -4200U);
422     OneTest (__LINE__, "1068",                     4, "%.*x", -15, 4200U);
423     OneTest (__LINE__, "ef98",                     4, "%.*x", -15, -4200U);
424     OneTest (__LINE__, "1068",                     4, "%.*X", -15, 4200U);
425     OneTest (__LINE__, "EF98",                     4, "%.*X", -15, -4200U);
426 #endif
427
428     /* Field width and precision given */
429     OneTest (__LINE__, "              u",         15, "%15.10c", 'u');
430     OneTest (__LINE__, "     0000004200",         15, "%15.10d", 4200);
431     OneTest (__LINE__, "    -0000004200",         15, "%15.10d", -4200);
432     OneTest (__LINE__, "     0000004200",         15, "%15.10i", 4200);
433     OneTest (__LINE__, "    -0000004200",         15, "%15.10i", -4200);
434     OneTest (__LINE__, "     0000010150",         15, "%15.10o", 4200U);
435     OneTest (__LINE__, "     0000167630",         15, "%15.10o", -4200U);
436     OneTest (__LINE__, "     hello, wor",         15, "%15.10s", "hello, world");
437     OneTest (__LINE__, "     0000004200",         15, "%15.10u", 4200U);
438     OneTest (__LINE__, "     0000061336",         15, "%15.10u", -4200U);
439     OneTest (__LINE__, "     0000001068",         15, "%15.10x", 4200U);
440     OneTest (__LINE__, "     000000ef98",         15, "%15.10x", -4200U);
441     OneTest (__LINE__, "     0000001068",         15, "%15.10X", 4200U);
442     OneTest (__LINE__, "     000000EF98",         15, "%15.10X", -4200U);
443
444     /* For d, i, o, u, x and X conversions, if a precision is specified, the
445     ** '0' flag is ignored.
446     */
447     OneTest (__LINE__, "     0000004200",         15, "%015.10d", 4200);
448     OneTest (__LINE__, "    -0000004200",         15, "%015.10d", -4200);
449     OneTest (__LINE__, "     0000004200",         15, "%015.10i", 4200);
450     OneTest (__LINE__, "    -0000004200",         15, "%015.10i", -4200);
451     OneTest (__LINE__, "     0000010150",         15, "%015.10o", 4200U);
452     OneTest (__LINE__, "     0000167630",         15, "%015.10o", -4200U);
453     OneTest (__LINE__, "     0000004200",         15, "%015.10u", 4200U);
454     OneTest (__LINE__, "     0000061336",         15, "%015.10u", -4200U);
455     OneTest (__LINE__, "     0000001068",         15, "%015.10x", 4200U);
456     OneTest (__LINE__, "     000000ef98",         15, "%015.10x", -4200U);
457     OneTest (__LINE__, "     0000001068",         15, "%015.10X", 4200U);
458     OneTest (__LINE__, "     000000EF98",         15, "%015.10X", -4200U);
459
460     /* Zero precision, explicitly specified */
461     OneTest (__LINE__, "u",                        1, "%.0c", 'u');
462     OneTest (__LINE__, "4200",                     4, "%.0d", 4200);
463     OneTest (__LINE__, "-4200",                    5, "%.0d", -4200);
464     OneTest (__LINE__, "4200",                     4, "%.0i", 4200);
465     OneTest (__LINE__, "-4200",                    5, "%.0i", -4200);
466     OneTest (__LINE__, "10150",                    5, "%.0o", 4200U);
467     OneTest (__LINE__, "167630",                   6, "%.0o", -4200U);
468     OneTest (__LINE__, "",                         0, "%.0s", "hello, world");
469     OneTest (__LINE__, "4200",                     4, "%.0u", 4200U);
470     OneTest (__LINE__, "61336",                    5, "%.0u", -4200U);
471     OneTest (__LINE__, "1068",                     4, "%.0x", 4200U);
472     OneTest (__LINE__, "ef98",                     4, "%.0x", -4200U);
473     OneTest (__LINE__, "1068",                     4, "%.0X", 4200U);
474     OneTest (__LINE__, "EF98",                     4, "%.0X", -4200U);
475
476     /* Zero precision, dot only */
477     OneTest (__LINE__, "u",                        1, "%.c", 'u');
478     OneTest (__LINE__, "4200",                     4, "%.d", 4200);
479     OneTest (__LINE__, "-4200",                    5, "%.d", -4200);
480     OneTest (__LINE__, "4200",                     4, "%.i", 4200);
481     OneTest (__LINE__, "-4200",                    5, "%.i", -4200);
482     OneTest (__LINE__, "10150",                    5, "%.o", 4200U);
483     OneTest (__LINE__, "167630",                   6, "%.o", -4200U);
484     OneTest (__LINE__, "",                         0, "%.s", "hello, world");
485     OneTest (__LINE__, "4200",                     4, "%.u", 4200U);
486     OneTest (__LINE__, "61336",                    5, "%.u", -4200U);
487     OneTest (__LINE__, "1068",                     4, "%.x", 4200U);
488     OneTest (__LINE__, "ef98",                     4, "%.x", -4200U);
489     OneTest (__LINE__, "1068",                     4, "%.X", 4200U);
490     OneTest (__LINE__, "EF98",                     4, "%.X", -4200U);
491
492     /* Zero precision, zero value */
493     OneTest (__LINE__, "",                         0, "%.0d", 0);
494     OneTest (__LINE__, "",                         0, "%.0i", 0);
495     OneTest (__LINE__, "",                         0, "%.0o", 0U);
496     OneTest (__LINE__, "",                         0, "%.0u", 0U);
497     OneTest (__LINE__, "",                         0, "%.0x", 0U);
498     OneTest (__LINE__, "",                         0, "%.0X", 0U);
499
500     /* Field width and zero precision given */
501     OneTest (__LINE__, "              u",         15, "%15.0c", 'u');
502     OneTest (__LINE__, "           4200",         15, "%15.0d", 4200);
503     OneTest (__LINE__, "          -4200",         15, "%15.0d", -4200);
504     OneTest (__LINE__, "           4200",         15, "%15.0i", 4200);
505     OneTest (__LINE__, "          -4200",         15, "%15.0i", -4200);
506     OneTest (__LINE__, "          10150",         15, "%15.0o", 4200U);
507     OneTest (__LINE__, "         167630",         15, "%15.0o", -4200U);
508     OneTest (__LINE__, "               ",         15, "%15.0s", "hello, world");
509     OneTest (__LINE__, "           4200",         15, "%15.0u", 4200U);
510     OneTest (__LINE__, "          61336",         15, "%15.0u", -4200U);
511     OneTest (__LINE__, "           1068",         15, "%15.0x", 4200U);
512     OneTest (__LINE__, "           ef98",         15, "%15.0x", -4200U);
513     OneTest (__LINE__, "           1068",         15, "%15.0X", 4200U);
514     OneTest (__LINE__, "           EF98",         15, "%15.0X", -4200U);
515
516     /* Field width, zero precision and zero value */
517     OneTest (__LINE__, "               ",         15, "%15.0d", 0);
518     OneTest (__LINE__, "               ",         15, "%15.0i", 0);
519     OneTest (__LINE__, "               ",         15, "%15.0o", 0U);
520     OneTest (__LINE__, "               ",         15, "%15.0u", 0U);
521     OneTest (__LINE__, "               ",         15, "%15.0x", 0U);
522     OneTest (__LINE__, "               ",         15, "%15.0X", 0U);
523
524     /* Alternative form */
525     OneTest (__LINE__, "010150",                   6, "%#o", 4200U);
526     OneTest (__LINE__, "0167630",                  7, "%#o", -4200U);
527     OneTest (__LINE__, "0x1068",                   6, "%#x", 4200U);
528     OneTest (__LINE__, "0xef98",                   6, "%#x", -4200U);
529     OneTest (__LINE__, "0X1068",                   6, "%#X", 4200U);
530     OneTest (__LINE__, "0XEF98",                   6, "%#X", -4200U);
531
532     /* Alternative form with zero value */
533 #ifndef NOCRASH
534     OneTest (__LINE__, "0",                        1, "%#o", 0U);
535     OneTest (__LINE__, "0",                        1, "%#x", 0U);
536     OneTest (__LINE__, "0",                        1, "%#X", 0U);
537 #endif
538
539     /* Alternative form with zero value and precision 1 */
540     OneTest (__LINE__, "0",                        1, "%#.1o", 0U);
541     OneTest (__LINE__, "0",                        1, "%#.1x", 0U);
542     OneTest (__LINE__, "0",                        1, "%#.1X", 0U);
543
544     /* Alternative form with non zero value and precision 1 */
545     OneTest (__LINE__, "01",                       2, "%#.1o", 1U);
546     OneTest (__LINE__, "0x1",                      3, "%#.1x", 1U);
547     OneTest (__LINE__, "0X1",                      3, "%#.1X", 1U);
548
549     /* Alternative form and width given */
550     OneTest (__LINE__, "         010150",         15, "%#15o", 4200U);
551     OneTest (__LINE__, "         0x1068",         15, "%#15x", 4200U);
552     OneTest (__LINE__, "         0X1068",         15, "%#15X", 4200U);
553
554     /* Alternative form, width and zero padding */
555     OneTest (__LINE__, "000000000010150",         15, "%#015o", 4200U);
556     OneTest (__LINE__, "0x0000000001068",         15, "%#015x", 4200U);
557     OneTest (__LINE__, "0X0000000001068",         15, "%#015X", 4200U);
558
559     /* n conversion specifier */
560     WriteTest (__LINE__, "%d%n",        &W, 0x5A5A0004L);
561     WriteTest (__LINE__, "%d%hn",       &W, 0x5A5A0004L);
562     WriteTest (__LINE__, "%d%hhn",      &W, 0x5A5A5A04L);
563     WriteTest (__LINE__, "%d%ln",       &W, 0x00000004L);
564
565     /* Output the result */
566     if (Failures) {
567         printf ("%u tests, %u failures\n", Tests, Failures);
568     } else {
569         printf ("%u tests: Ok\n", Tests);
570     }
571
572     /* Wait for a key so we can read the result */
573 #if defined(__CC65__)
574     cgetc ();
575 #endif
576     return 0;
577 }