CS318 - Pintos
Pintos source browser for JHU CS318 course
stdio.c
Go to the documentation of this file.
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 #include "threads/test.h"
18 
19 /** Number of failures so far. */
20 static int failure_cnt;
21 
22 static void
23 checkf (const char *expect, const char *format, ...)
24 {
25  char output[128];
26  va_list args;
27 
28  printf ("\"%s\" -> \"%s\": ", format, expect);
29 
30  va_start (args, format);
31  vsnprintf (output, sizeof output, format, args);
32  va_end (args);
33 
34  if (strcmp (expect, output))
35  {
36  printf ("\nFAIL: actual output \"%s\"\n", output);
37  failure_cnt++;
38  }
39  else
40  printf ("okay\n");
41 }
42 
43 /** Test printf() implementation. */
44 void
45 test (void)
46 {
47  printf ("Testing formats:");
48 
49  /* Check that commas show up in the right places, for positive
50  numbers. */
51  checkf ("1", "%'d", 1);
52  checkf ("12", "%'d", 12);
53  checkf ("123", "%'d", 123);
54  checkf ("1,234", "%'d", 1234);
55  checkf ("12,345", "%'d", 12345);
56  checkf ("123,456", "%'ld", 123456L);
57  checkf ("1,234,567", "%'ld", 1234567L);
58  checkf ("12,345,678", "%'ld", 12345678L);
59  checkf ("123,456,789", "%'ld", 123456789L);
60  checkf ("1,234,567,890", "%'ld", 1234567890L);
61  checkf ("12,345,678,901", "%'lld", 12345678901LL);
62  checkf ("123,456,789,012", "%'lld", 123456789012LL);
63  checkf ("1,234,567,890,123", "%'lld", 1234567890123LL);
64  checkf ("12,345,678,901,234", "%'lld", 12345678901234LL);
65  checkf ("123,456,789,012,345", "%'lld", 123456789012345LL);
66  checkf ("1,234,567,890,123,456", "%'lld", 1234567890123456LL);
67  checkf ("12,345,678,901,234,567", "%'lld", 12345678901234567LL);
68  checkf ("123,456,789,012,345,678", "%'lld", 123456789012345678LL);
69  checkf ("1,234,567,890,123,456,789", "%'lld", 1234567890123456789LL);
70 
71  /* Check that commas show up in the right places, for positive
72  numbers. */
73  checkf ("-1", "%'d", -1);
74  checkf ("-12", "%'d", -12);
75  checkf ("-123", "%'d", -123);
76  checkf ("-1,234", "%'d", -1234);
77  checkf ("-12,345", "%'d", -12345);
78  checkf ("-123,456", "%'ld", -123456L);
79  checkf ("-1,234,567", "%'ld", -1234567L);
80  checkf ("-12,345,678", "%'ld", -12345678L);
81  checkf ("-123,456,789", "%'ld", -123456789L);
82  checkf ("-1,234,567,890", "%'ld", -1234567890L);
83  checkf ("-12,345,678,901", "%'lld", -12345678901LL);
84  checkf ("-123,456,789,012", "%'lld", -123456789012LL);
85  checkf ("-1,234,567,890,123", "%'lld", -1234567890123LL);
86  checkf ("-12,345,678,901,234", "%'lld", -12345678901234LL);
87  checkf ("-123,456,789,012,345", "%'lld", -123456789012345LL);
88  checkf ("-1,234,567,890,123,456", "%'lld", -1234567890123456LL);
89  checkf ("-12,345,678,901,234,567", "%'lld", -12345678901234567LL);
90  checkf ("-123,456,789,012,345,678", "%'lld", -123456789012345678LL);
91  checkf ("-1,234,567,890,123,456,789", "%'lld", -1234567890123456789LL);
92 
93  /* Check signed integer conversions. */
94  checkf (" 0", "%5d", 0);
95  checkf ("0 ", "%-5d", 0);
96  checkf (" +0", "%+5d", 0);
97  checkf ("+0 ", "%+-5d", 0);
98  checkf (" 0", "% 5d", 0);
99  checkf ("00000", "%05d", 0);
100  checkf (" ", "%5.0d", 0);
101  checkf (" 00", "%5.2d", 0);
102  checkf ("0", "%d", 0);
103 
104  checkf (" 1", "%5d", 1);
105  checkf ("1 ", "%-5d", 1);
106  checkf (" +1", "%+5d", 1);
107  checkf ("+1 ", "%+-5d", 1);
108  checkf (" 1", "% 5d", 1);
109  checkf ("00001", "%05d", 1);
110  checkf (" 1", "%5.0d", 1);
111  checkf (" 01", "%5.2d", 1);
112  checkf ("1", "%d", 1);
113 
114  checkf (" -1", "%5d", -1);
115  checkf ("-1 ", "%-5d", -1);
116  checkf (" -1", "%+5d", -1);
117  checkf ("-1 ", "%+-5d", -1);
118  checkf (" -1", "% 5d", -1);
119  checkf ("-0001", "%05d", -1);
120  checkf (" -1", "%5.0d", -1);
121  checkf (" -01", "%5.2d", -1);
122  checkf ("-1", "%d", -1);
123 
124  checkf ("12345", "%5d", 12345);
125  checkf ("12345", "%-5d", 12345);
126  checkf ("+12345", "%+5d", 12345);
127  checkf ("+12345", "%+-5d", 12345);
128  checkf (" 12345", "% 5d", 12345);
129  checkf ("12345", "%05d", 12345);
130  checkf ("12345", "%5.0d", 12345);
131  checkf ("12345", "%5.2d", 12345);
132  checkf ("12345", "%d", 12345);
133 
134  checkf ("123456", "%5d", 123456);
135  checkf ("123456", "%-5d", 123456);
136  checkf ("+123456", "%+5d", 123456);
137  checkf ("+123456", "%+-5d", 123456);
138  checkf (" 123456", "% 5d", 123456);
139  checkf ("123456", "%05d", 123456);
140  checkf ("123456", "%5.0d", 123456);
141  checkf ("123456", "%5.2d", 123456);
142  checkf ("123456", "%d", 123456);
143 
144  /* Check unsigned integer conversions. */
145  checkf (" 0", "%5u", 0);
146  checkf (" 0", "%5o", 0);
147  checkf (" 0", "%5x", 0);
148  checkf (" 0", "%5X", 0);
149  checkf (" 0", "%#5o", 0);
150  checkf (" 0", "%#5x", 0);
151  checkf (" 0", "%#5X", 0);
152  checkf (" 00000000", "%#10.8x", 0);
153 
154  checkf (" 1", "%5u", 1);
155  checkf (" 1", "%5o", 1);
156  checkf (" 1", "%5x", 1);
157  checkf (" 1", "%5X", 1);
158  checkf (" 01", "%#5o", 1);
159  checkf (" 0x1", "%#5x", 1);
160  checkf (" 0X1", "%#5X", 1);
161  checkf ("0x00000001", "%#10.8x", 1);
162 
163  checkf ("123456", "%5u", 123456);
164  checkf ("361100", "%5o", 123456);
165  checkf ("1e240", "%5x", 123456);
166  checkf ("1E240", "%5X", 123456);
167  checkf ("0361100", "%#5o", 123456);
168  checkf ("0x1e240", "%#5x", 123456);
169  checkf ("0X1E240", "%#5X", 123456);
170  checkf ("0x0001e240", "%#10.8x", 123456);
171 
172  /* Character and string conversions. */
173  checkf ("foobar", "%c%c%c%c%c%c", 'f', 'o', 'o', 'b', 'a', 'r');
174  checkf (" left-right ", "%6s%s%-7s", "left", "-", "right");
175  checkf ("trim", "%.4s", "trimoff");
176  checkf ("%%", "%%%%");
177 
178  /* From Cristian Cadar's automatic test case generator. */
179  checkf (" abcdefgh", "%9s", "abcdefgh");
180  checkf ("36657730000", "%- o", (unsigned) 036657730000);
181  checkf ("4139757568", "%- u", (unsigned) 4139757568UL);
182  checkf ("f6bfb000", "%- x", (unsigned) 0xf6bfb000);
183  checkf ("36657730000", "%-to", (ptrdiff_t) 036657730000);
184  checkf ("4139757568", "%-tu", (ptrdiff_t) 4139757568UL);
185  checkf ("-155209728", "%-zi", (size_t) -155209728);
186  checkf ("-155209728", "%-zd", (size_t) -155209728);
187  checkf ("036657730000", "%+#o", (unsigned) 036657730000);
188  checkf ("0xf6bfb000", "%+#x", (unsigned) 0xf6bfb000);
189  checkf ("-155209728", "% zi", (size_t) -155209728);
190  checkf ("-155209728", "% zd", (size_t) -155209728);
191  checkf ("4139757568", "% tu", (ptrdiff_t) 4139757568UL);
192  checkf ("036657730000", "% #o", (unsigned) 036657730000);
193  checkf ("0xf6bfb000", "% #x", (unsigned) 0xf6bfb000);
194  checkf ("0xf6bfb000", "%# x", (unsigned) 0xf6bfb000);
195  checkf ("-155209728", "%#zd", (size_t) -155209728);
196  checkf ("-155209728", "%0zi", (size_t) -155209728);
197  checkf ("4,139,757,568", "%'tu", (ptrdiff_t) 4139757568UL);
198  checkf ("-155,209,728", "%-'d", -155209728);
199  checkf ("-155209728", "%.zi", (size_t) -155209728);
200  checkf ("-155209728", "%zi", (size_t) -155209728);
201  checkf ("-155209728", "%zd", (size_t) -155209728);
202  checkf ("-155209728", "%+zi", (size_t) -155209728);
203 
204  if (failure_cnt == 0)
205  printf ("\nstdio: PASS\n");
206  else
207  printf ("\nstdio: FAIL: %d tests failed\n", failure_cnt);
208 }
va_end
#define va_end(LIST)
Definition: stdarg.h:10
va_start
#define va_start(LIST, ARG)
Definition: stdarg.h:9
string.h
strcmp
int strcmp(const char *a_, const char *b_)
Finds the first differing characters in strings A and B.
Definition: string.c:73
failure_cnt
static int failure_cnt
Test program for printf() in lib/stdio.c.
Definition: stdio.c:20
test
void test(void)
Test printf() implementation.
Definition: stdio.c:45
ptrdiff_t
__PTRDIFF_TYPE__ ptrdiff_t
GCC predefines the types we need for ptrdiff_t and size_t, so that we don't have to guess.
Definition: stddef.h:9
limits.h
stdarg.h
printf
int printf(const char *format,...)
Writes formatted output to the console.
Definition: stdio.c:79
va_list
__builtin_va_list va_list
GCC has <stdarg.h> functionality as built-ins, so all we need is to use it.
Definition: stdarg.h:7
checkf
static void checkf(const char *expect, const char *format,...)
Definition: stdio.c:23
vsnprintf
int vsnprintf(char *buffer, size_t buf_size, const char *format, va_list args)
Like vprintf(), except that output is stored into BUFFER, which must have space for BUF_SIZE characte...
Definition: stdio.c:26
stdlib.h
stddef.h