1bf779974d812a448bedf134c50de931747b48bd
[pintos-anon] / src / tests / threads / stdio.c
1 /* Test program for printf() in lib/stdio.c.
2
3    Attempts to test printf() functionality that is not
4    sufficiently tested elsewhere in Pintos.
5
6    This is not a test we will run on your submitted projects.
7    It is here for completeness.
8 */
9
10 #undef NDEBUG
11 #include <limits.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17
18 void
19 checkf (const char *expect, const char *format, ...) 
20 {
21   char output[128];
22   va_list args;
23
24   printf (" [%s]", format);
25   
26   va_start (args, format);
27   vsnprintf (output, sizeof output, format, args);
28   va_end (args);
29
30   if (strcmp (expect, output)) 
31     {
32       printf ("FAIL: format string \"%s\": "
33               "actual output \"%s\", expected \"%s\"\n",
34               format, output, expect);
35       exit (1);
36     }
37 }
38
39 /* Test printf() implementation. */
40 void
41 test (void) 
42 {
43   long long x;
44   int i;
45
46   printf ("Testing formats:");
47
48   /* Check that commas show up in the right places, for positive
49      numbers. */
50   checkf ("1", "%'d", 1);
51   checkf ("12", "%'d", 12);
52   checkf ("123", "%'d", 123);
53   checkf ("1,234", "%'d", 1234);
54   checkf ("12,345", "%'d", 12345);
55   checkf ("123,456", "%'ld", 123456L);
56   checkf ("1,234,567", "%'ld", 1234567L);
57   checkf ("12,345,678", "%'ld", 12345678L);
58   checkf ("123,456,789", "%'ld", 123456789L);
59   checkf ("1,234,567,890", "%'ld", 1234567890L);
60   checkf ("12,345,678,901", "%'lld", 12345678901LL);
61   checkf ("123,456,789,012", "%'lld", 123456789012LL);
62   checkf ("1,234,567,890,123", "%'lld", 1234567890123LL);
63   checkf ("12,345,678,901,234", "%'lld", 12345678901234LL);
64   checkf ("123,456,789,012,345", "%'lld", 123456789012345LL);
65   checkf ("1,234,567,890,123,456", "%'lld", 1234567890123456LL);
66   checkf ("12,345,678,901,234,567", "%'lld", 12345678901234567LL);
67   checkf ("123,456,789,012,345,678", "%'lld", 123456789012345678LL);
68   checkf ("1,234,567,890,123,456,789", "%'lld", 1234567890123456789LL);
69
70   /* Check that commas show up in the right places, for positive
71      numbers. */
72   checkf ("-1", "%'d", -1);
73   checkf ("-12", "%'d", -12);
74   checkf ("-123", "%'d", -123);
75   checkf ("-1,234", "%'d", -1234);
76   checkf ("-12,345", "%'d", -12345);
77   checkf ("-123,456", "%'ld", -123456L);
78   checkf ("-1,234,567", "%'ld", -1234567L);
79   checkf ("-12,345,678", "%'ld", -12345678L);
80   checkf ("-123,456,789", "%'ld", -123456789L);
81   checkf ("-1,234,567,890", "%'ld", -1234567890L);
82   checkf ("-12,345,678,901", "%'lld", -12345678901LL);
83   checkf ("-123,456,789,012", "%'lld", -123456789012LL);
84   checkf ("-1,234,567,890,123", "%'lld", -1234567890123LL);
85   checkf ("-12,345,678,901,234", "%'lld", -12345678901234LL);
86   checkf ("-123,456,789,012,345", "%'lld", -123456789012345LL);
87   checkf ("-1,234,567,890,123,456", "%'lld", -1234567890123456LL);
88   checkf ("-12,345,678,901,234,567", "%'lld", -12345678901234567LL);
89   checkf ("-123,456,789,012,345,678", "%'lld", -123456789012345678LL);
90   checkf ("-1,234,567,890,123,456,789", "%'lld", -1234567890123456789LL);
91   
92   /* Check signed integer conversions. */
93   checkf ("    0", "%5d", 0);
94   checkf ("0    ", "%-5d", 0);
95   checkf ("   +0", "%+5d", 0);
96   checkf ("+0   ", "%+-5d", 0);
97   checkf ("    0", "% 5d", 0);
98   checkf ("00000", "%05d", 0);
99   checkf ("     ", "%5.0d", 0);
100   checkf ("   00", "%5.2d", 0);
101   checkf ("0", "%d", 0);
102
103   checkf ("    1", "%5d", 1);
104   checkf ("1    ", "%-5d", 1);
105   checkf ("   +1", "%+5d", 1);
106   checkf ("+1   ", "%+-5d", 1);
107   checkf ("    1", "% 5d", 1);
108   checkf ("00001", "%05d", 1);
109   checkf ("    1", "%5.0d", 1);
110   checkf ("   01", "%5.2d", 1);
111   checkf ("1", "%d", 1);
112
113   checkf ("   -1", "%5d", -1);
114   checkf ("-1   ", "%-5d", -1);
115   checkf ("   -1", "%+5d", -1);
116   checkf ("-1   ", "%+-5d", -1);
117   checkf ("   -1", "% 5d", -1);
118   checkf ("-0001", "%05d", -1);
119   checkf ("   -1", "%5.0d", -1);
120   checkf ("  -01", "%5.2d", -1);
121   checkf ("-1", "%d", -1);
122
123   checkf ("12345", "%5d", 12345);
124   checkf ("12345", "%-5d", 12345);
125   checkf ("+12345", "%+5d", 12345);
126   checkf ("+12345", "%+-5d", 12345);
127   checkf (" 12345", "% 5d", 12345);
128   checkf ("12345", "%05d", 12345);
129   checkf ("12345", "%5.0d", 12345);
130   checkf ("12345", "%5.2d", 12345);
131   checkf ("12345", "%d", 12345);
132
133   checkf ("123456", "%5d", 123456);
134   checkf ("123456", "%-5d", 123456);
135   checkf ("+123456", "%+5d", 123456);
136   checkf ("+123456", "%+-5d", 123456);
137   checkf (" 123456", "% 5d", 123456);
138   checkf ("123456", "%05d", 123456);
139   checkf ("123456", "%5.0d", 123456);
140   checkf ("123456", "%5.2d", 123456);
141   checkf ("123456", "%d", 123456);
142
143   /* Check unsigned integer conversions. */
144   checkf ("    0", "%5u", 0);
145   checkf ("    0", "%5o", 0);
146   checkf ("    0", "%5x", 0);
147   checkf ("    0", "%5X", 0);
148   checkf ("    0", "%#5o", 0);
149   checkf ("    0", "%#5x", 0);
150   checkf ("    0", "%#5X", 0);
151   checkf ("  00000000", "%#10.8x", 0);
152   
153   checkf ("    1", "%5u", 1);
154   checkf ("    1", "%5o", 1);
155   checkf ("    1", "%5x", 1);
156   checkf ("    1", "%5X", 1);
157   checkf ("   01", "%#5o", 1);
158   checkf ("  0x1", "%#5x", 1);
159   checkf ("  0X1", "%#5X", 1);
160   checkf ("0x00000001", "%#10.8x", 1);
161
162   checkf ("123456", "%5u", 123456);
163   checkf ("361100", "%5o", 123456);
164   checkf ("1e240", "%5x", 123456);
165   checkf ("1E240", "%5X", 123456);
166   checkf ("0361100", "%#5o", 123456);
167   checkf ("0x1e240", "%#5x", 123456);
168   checkf ("0X1E240", "%#5X", 123456);
169   checkf ("0x0001e240", "%#10.8x", 123456);
170
171   /* Character and string conversions. */
172   checkf ("foobar", "%c%c%c%c%c%c", 'f', 'o', 'o', 'b', 'a', 'r');
173   checkf ("  left-right  ", "%6s%s%-7s", "left", "-", "right");
174   checkf ("trim", "%.4s", "trimoff");
175   checkf ("%%", "%%%%");
176
177   /* From Cristian Cadar's automatic test case generator. */
178   checkf (" abcdefgh", "%9s", "abcdefgh");
179   checkf ("36657730000", "%- o", 036657730000);
180   checkf ("4139757568", "%- u", 4139757568);
181   checkf ("f6bfb000", "%- x", 0xf6bfb000);
182   checkf ("36657730000", "%-to", (ptrdiff_t) 036657730000);
183   checkf ("4139757568", "%-tu", (ptrdiff_t) 4139757568);
184   checkf ("-155209728", "%-zi", (size_t) -155209728);
185   checkf ("-155209728", "%-zd", (size_t) -155209728);
186   checkf ("036657730000", "%+#o", 036657730000);
187   checkf ("0xf6bfb000", "%+#x", 0xf6bfb000);
188   checkf ("-155209728", "% zi", (size_t) -155209728);
189   checkf ("-155209728", "% zd", (size_t) -155209728);
190   checkf ("4139757568", "% tu", (ptrdiff_t) 4139757568);
191   checkf ("036657730000", "% #o", 036657730000);
192   checkf ("0xf6bfb000", "% #x", 0xf6bfb000);
193   checkf ("0xf6bfb000", "%# x", 0xf6bfb000);
194   checkf ("-155209728", "%#zd", (size_t) -155209728);
195   checkf ("-155209728", "%0zi", (size_t) -155209728);
196   checkf ("4,139,757,568", "%'tu", (ptrdiff_t) 4139757568);
197   checkf ("-155,209,728", "%-'d", -155209728);
198   checkf ("-155209728", "%.zi", (size_t) -155209728);
199   checkf ("-155209728", "%zi", (size_t) -155209728);
200   checkf ("-155209728", "%zd", (size_t) -155209728);
201   checkf ("-155209728", "%+zi", (size_t) -155209728);
202
203   printf ("\nstdio: PASS\n");
204 }