4 ** Tests that the scanf family of functions scans and converts its input data
7 ** Note: When this program uses conio, it doesn't guard against printing off
8 ** the bottom of the screen. So, you might have a problem on platforms with
11 ** 2005-01-26, Greg King
14 /* Define USE_STDIO, when you want to use the stdio functions.
15 ** Do not define it, when you want to use the conio functions.
26 # define PRINTF printf
31 /* Unlike other conio input functions, cscanf() echoes what you type. */
33 # define PRINTF cprintf
36 #define ARRAYSIZE(a) (sizeof (a) / sizeof (a)[0])
39 const char *input, *format;
55 /* Input sequences for character specifiers must be less than 80 characters
56 ** long. These format strings are allowwed a maximum of two assignment
59 /* Test that literals match, and that they aren't seen as conversions.
60 ** Test that integer specifiers can handle end-of-file.
62 {"qwerty Dvorak", "qwerty Dvorak", 0 , INT, {0}, INT, {0}},
63 {"qwerty" , "qwerty %d%i" , EOF, INT, {0}, INT, {0}},
64 {"qwerty " , "qwerty %d%i" , EOF, INT, {0}, INT, {0}},
66 /* Test that integer specifiers scan properly. */
67 {"qwerty a" , "qwerty %d%i", 0, INT, {0} , INT, {0}},
68 {"qwerty -" , "qwerty %d%i", 0, INT, {0} , INT, {0}},
69 {"qwerty -9" , "qwerty %d%i", 1, INT, {-9} , INT, {0}},
70 {"qwerty -95" , "qwerty %d%i", 1, INT, {-95} , INT, {0}},
71 {"qwerty -95a" , "qwerty %d%i", 1, INT, {-95} , INT, {0}},
72 {"qwerty -95a 1", "qwerty %d%i", 1, INT, {-95} , INT, {0}},
73 {"qwerty -a" , "qwerty %d%i", 0, INT, {0} , INT, {0}},
74 {"qwerty -95 1" , "qwerty %d%i", 2, INT, {-95} , INT, {1}},
75 {"qwerty 95 2", "qwerty %i" , 1, INT, {95} , INT, {0}},
76 {"qwerty -95 +2", "qwerty %x%o", 2, INT, {-0x95}, INT, {02}},
77 {"qwerty 0X9E 02", "qwerty %i%i", 2, INT, {0x9e} , INT, {2}},
78 {"qwerty 095 2", "qwerty %i%i", 2, INT, {0} , INT, {95}},
79 {"qwerty 0e5 2", "qwerty %i%i", 1, INT, {0} , INT, {0}},
81 /* (String pointers are cast as (int),
82 * in order to avoid cc65 warnings.) */
84 /* Test that character specifiers can handle end-of-file. */
85 {"qwerty" , "qwerty %s%s" , EOF, CHAR, {(int)""}, CHAR, {(int)""}},
86 {"qwerty ", "qwerty %s%s" , EOF, CHAR, {(int)""}, CHAR, {(int)""}},
87 {"qwerty" , "qwerty %c%c" , EOF, CHAR, {(int)""}, CHAR, {(int)""}},
88 {"qwerty ", "qwerty %c%c" , EOF, CHAR, {(int)""}, CHAR, {(int)""}},
89 {"qwerty" , "qwerty %[ a-z]%c", EOF, CHAR, {(int)""}, CHAR, {(int)""}},
90 {"qwerty ", "qwerty %[ a-z]%c", EOF, CHAR, {(int)""}, CHAR, {(int)""}},
91 {"qwerty ", "qwerty%s%s" , EOF, CHAR, {(int)""}, CHAR, {(int)""}},
93 /* Test that character specifiers scan properly. */
94 {"123456qwertyasdfghzxcvbn!@#$%^QWERTYASDFGHZXCV"
95 "BN7890-=uiop[]\\jkl;'m,./&*()_+UIOP{}|JKL:\"M<>?",
97 CHAR, {(int)"123456qwertyasdfghzxcvbn!@#$%^QWERTYASDFGHZXCV"
98 "BN7890-=uiop[]\\jkl;'m,./&*()_+UIO"}, CHAR, {(int)"P{}|JKL:\"M<>?"}},
99 {"qwerty ", "qwerty%c%c" , 2, CHAR, {(int)" "} , CHAR, {(int)" "}},
100 {"qwerty ", "qwerty%2c%c" , 2, CHAR, {(int)" "} , CHAR, {(int)" "}},
101 {"qwerty ", "qwerty%2c%2c" , 1, CHAR, {(int)" "} , CHAR, {(int)" "}},
102 {"qwerty ", "qwerty%[ a-z]%c", 1, CHAR, {(int)" "}, CHAR, {(int)""}},
103 {"qwerty q", "qwerty%[ a-z]%c", 1, CHAR, {(int)" q"}, CHAR, {(int)""}},
104 {"qwerty Q", "qwerty%[ a-z]%c", 2, CHAR, {(int)" "}, CHAR, {(int)"Q"}},
105 {"qwerty-QWERTY-", "%[q-ze-]%[-A-Z]" , 2, CHAR, {(int)"qwerty-"},
106 CHAR, {(int)"QWERTY-"}},
108 /* Test the space-separation of strings. */
109 {"qwerty qwerty" , "qwerty%s%s", 1, CHAR, {(int)"qwerty"},
111 {"qwerty qwerty Dvorak", "qwerty%s%s", 2, CHAR, {(int)"qwerty"},
112 CHAR, {(int)"Dvorak"}},
114 /* Test the mixxing of types. */
115 {"qwerty abc3", "qwerty%s%X" , 1, CHAR, {(int)"abc3"} , INT , {0}},
116 {"qwerty abc3", "qwerty%[ a-z]%X" , 2, CHAR, {(int)" abc"} , INT , {3}},
117 {"qwerty abc3", "qwerty%[ a-z3]%X", 1, CHAR, {(int)" abc3"}, INT , {0}},
118 {"qwerty abc3", "qwerty%[ A-Z]%X" , 2, CHAR, {(int)" "} , INT ,
120 {"qwerty 3abc", "qwerty%i%[ a-z]" , 2, INT , {3} , CHAR,
122 {"qwerty 3abc", "qwerty%i%[ A-Z]" , 1, INT , {3} , CHAR,
125 /* Test the character-count specifier. */
126 {" 95 5", "%n%i" , 1, INT , {0} , INT, {95}},
127 {" a5 5", "%n%i" , 0, INT , {0} , INT, {0}},
128 {" a5 5", "%x%n" , 1, INT , {0xa5} , INT, {4}},
129 {" a5 5", " %4c%n", 1, CHAR, {(int)"a5 5"}, INT, {6}},
130 {" 05a9" , "%i%n" , 1, INT , {5} , INT, {3}},
132 /* Test assignment-suppression. */
133 {" 95 6", "%n%*i" , 0, INT , {0} , INT, {0}},
134 {" a5 6", "%*x%n" , 0, INT , {4} , INT, {0}},
135 {" a5 6", "%*x%n%o", 1, INT , {4} , INT, {6}},
136 {" a5 6", " %*4c%d", 0, CHAR, {(int)""}, INT, {0}},
137 {"The first number is 7. The second number is 8.\n",
138 "%*[ .A-Za-z]%d%*[ .A-Za-z]%d", 2, INT, {7}, INT, {8}},
141 /* Test the char, short, and long specification-modifiers. */
142 static const struct {
143 const char *input, *format;
146 {"+123456789", "%hhd", (signed char)123456789L},
147 {" 123456789", "%hd" , (unsigned short)123456789L},
148 {" 123456789", "%ld" , 123456789L},
149 {"-123456789", "%lld", -123456789L},
152 static void Pause(void) {
156 cprintf("\r\nTap a key to see the next test. ");
165 int c, n1 = 12345, n2, n3;
167 void *p1 = main, *p2 = main, *p3 = main, *p4 = main;
174 /* Test that scanf() can recognize percent-signs in the input.
175 ** Test that integer converters skip white-space.
176 ** Test that "%i" can scan a single zero digit (followed by EOF).
178 sscanf("% \r\n\f\v\t 0", "%%%i", &n1);
180 PRINTF("sscanf()'s \"%%%%%%i\" couldn't scan either a \"%%\" "
181 "or a single zero digit.\r\n\n");
183 /* Test scanf()'s return-value: EOF if input ends before the first
184 ** conversion-attempt begins; an assignment-count, otherwise.
185 ** Test that scanf() properly converts and assigns the correct number
188 PRINTF("Testing scanf()'s return-value,\r\nconversions, and assignments...\r\n");
189 for (t = 0; t < ARRAYSIZE(test_data); ++t) {
191 /* Prefill the arguments with zeroes. */
193 memset(s1, '\0', sizeof s1);
194 memset(s2, '\0', sizeof s2);
196 c=sscanf(test_data[t].input, test_data[t].format,
197 /* Avoid warning messages about different
198 ** pointer-types, by casting them to void-pointers.
200 test_data[t].type1 == INT ? (void *)&n1 : (void *)s1,
201 test_data[t].type2 == INT ? (void *)&n2 : (void *)s2);
202 if (c != test_data[t].rvalue)
203 PRINTF("Test #%u returned %d instead of %d.\r\n",
204 t + 1, c, test_data[t].rvalue);
206 if (test_data[t].type1 == INT) {
207 if (test_data[t].v1.nvalue != n1)
208 PRINTF("Test #%u assigned %i, instead of %i,\r\n"
209 "\tto the first argument.\r\n\n",
210 t + 1, n1, test_data[t].v1.nvalue);
212 else { /* test_data[t].type1 == CHAR */
213 if (strcmp(test_data[t].v1.svalue, s1))
214 PRINTF("Test #%u assigned\r\n\"%s\",\r\n"
215 "\tinstead of\r\n\"%s\",\r\n"
216 "\tto the first argument.\r\n\n",
217 t + 1, s1, test_data[t].v1.svalue);
220 if (test_data[t].type2 == INT) {
221 if (test_data[t].v2.nvalue != n2)
222 PRINTF("Test #%u assigned %i, instead of %i,\r\n"
223 "\tto the second argument.\r\n\n",
224 t + 1, n2, test_data[t].v2.nvalue);
226 else { /* test_data[t].type2 == CHAR */
227 if (strcmp(test_data[t].v2.svalue, s2))
228 PRINTF("Test #%u assigned\r\n\"%s\",\r\n"
229 "\tinstead of\r\n\"%s\",\r\n"
230 "\tto the second argument.\r\n\n",
231 t + 1, s2, test_data[t].v2.svalue);
236 /* Test the char, short, and long specification-modifiers. */
237 PRINTF("Testing scanf()'s type-modifiers...\r\n");
238 for (t = 0; t < ARRAYSIZE(type_data); ++t) {
240 sscanf(type_data[t].input, type_data[t].format, &n0);
241 if (type_data[t].value != n0)
242 PRINTF("Test #%u assigned %li instead of %li.\r\n",
243 t + 1, n0, type_data[t].value);
247 /* Test that the pointer specification
248 ** can convert what printf() generates.
250 PRINTF("Testing \"%%p\"...\r\n");
251 sprintf(s1, "%p %p %p %p", NULL, NULL,
252 Pause, /* static (program) storage */
253 &c); /* automatic (stack) storage */
254 sscanf(s1, "%p%p%p %p", &p1, &p2, &p3, &p4);
255 if (p1 != NULL || p2 != NULL ||
256 p3 != (void *)Pause || p4 != (void *)&c)
257 PRINTF("p1 is %p, p2 is %p; they should be %p.\r\n"
258 "scanf() assigned %p to p3, instead of %p.\r\n"
259 "scanf() assigned %p to p4, instead of %p.\r\n",
264 /* Test that scanf() can scan typed input.
265 ** Retest that "%i" can decode radix prefixxes.
269 PRINTF("Type 3 signed numbers,\r\n"
270 "separated by white-space:\r\n"
271 "octal decimal hexadecimal\r\n"
273 c = SCANF("%i %i %i", &n1, &n2, &n3);
274 PRINTF("\r\n\nscanf() returned %i.\r\n"
275 "The numbers are:\r\n"
278 " %+#X hexadecimal.\r\n",