]> git.sur5r.net Git - cc65/blob - testcode/lib/scanf-test.c
Added scanf test program contributed by Greg King
[cc65] / testcode / lib / scanf-test.c
1 /*
2 ** scanf-test.c
3 **
4 ** Tests that the scanf family of functions scans and converts its input data
5 ** correctly.
6 **
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
9 ** "short" screens.
10 **
11 ** 2005-01-26, Greg King
12 */
13
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.
16 */
17 /*
18 #define USE_STDIO
19 */
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #ifdef USE_STDIO
25 # define SCANF scanf
26 # define PRINTF printf
27
28 #else
29 # include <conio.h>
30
31 /* Unlike other conio input functions, cscanf() echoes what you type. */
32 # define SCANF cscanf
33 # define PRINTF cprintf
34 #endif
35
36 #define ARRAYSIZE(a) (sizeof (a) / sizeof (a)[0])
37
38 static const struct {
39         const char *input, *format;
40         int rvalue;
41         enum TYPE {
42                INT,
43                CHAR
44                } type1;
45         union {
46                int nvalue;
47                const char *svalue;
48                } v1;
49         enum TYPE type2;
50         union {
51                int nvalue;
52                const char *svalue;
53                } v2;
54         } test_data[] = {
55 /* Input sequences for character specifiers must be less than 80 characters
56 ** long.  These format strings are allowwed a maximum of two assignment
57 ** specifications.
58 */
59         /* Test that literals match, and that they aren't seen as conversions.
60         ** Test that integer specifiers can handle end-of-file.
61         */
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}},
65
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}},
80
81         /* (String pointers are cast as (int),
82          * in order to avoid cc65 warnings.) */
83
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)""}},
92
93         /* Test that character specifiers scan properly. */
94         {"123456qwertyasdfghzxcvbn!@#$%^QWERTYASDFGHZXCV"
95             "BN7890-=uiop[]\\jkl;'m,./&*()_+UIOP{}|JKL:\"M<>?",
96             "%79s%79s", 2,
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-"}},
107
108         /* Test the space-separation of strings. */
109         {"qwerty qwerty"       , "qwerty%s%s", 1, CHAR, {(int)"qwerty"},
110             CHAR, {(int)""}},
111         {"qwerty qwerty Dvorak", "qwerty%s%s", 2, CHAR, {(int)"qwerty"},
112             CHAR, {(int)"Dvorak"}},
113
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 ,
119             {0xabc3}},
120         {"qwerty  3abc", "qwerty%i%[ a-z]" , 2, INT , {3}            , CHAR,
121             {(int)"abc"}},
122         {"qwerty  3abc", "qwerty%i%[ A-Z]" , 1, INT , {3}            , CHAR,
123             {(int)""}},
124
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}},
131
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}},
139         };
140
141 /* Test the char, short, and long specification-modifiers. */
142 static const struct {
143         const char *input, *format;
144         long value;
145         } type_data[] = {
146         {"+123456789", "%hhd", (signed char)123456789L},
147         {" 123456789", "%hd" , (unsigned short)123456789L},
148         {" 123456789", "%ld" ,  123456789L},
149         {"-123456789", "%lld", -123456789L},
150         };
151
152 static void Pause(void) {
153 #ifdef USE_STDIO
154         printf("\n");
155 #else
156         cprintf("\r\nTap a key to see the next test. ");
157         cgetc();
158         clrscr();
159 #endif
160         }
161
162 int main(void) {
163         long n0;
164         unsigned t;
165         int c, n1 = 12345, n2, n3;
166         char s1[80], s2[80];
167         void *p1 = main, *p2 = main, *p3 = main, *p4 = main;
168
169 #ifndef USE_STDIO
170         clrscr();
171         cursor(1);
172 #endif
173
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).
177         */
178         sscanf("%  \r\n\f\v\t  0", "%%%i", &n1);
179         if (n1 != 0)
180                PRINTF("sscanf()'s \"%%%%%%i\" couldn't scan either a \"%%\" "
181                       "or a single zero digit.\r\n\n");
182
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
186         ** of arguments.
187         */
188         PRINTF("Testing scanf()'s return-value,\r\nconversions, and assignments...\r\n");
189         for (t = 0; t < ARRAYSIZE(test_data); ++t) {
190
191                /* Prefill the arguments with zeroes. */
192                n1 = n2 = 0;
193                memset(s1, '\0', sizeof s1);
194                memset(s2, '\0', sizeof s2);
195
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.
199                       */
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);
205
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);
211                      }
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);
218                      }
219
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);
225                      }
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);
232                      }
233                }
234         Pause();
235
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) {
239                n0 = 0L;
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);
244                }
245         Pause();
246
247         /* Test that the pointer specification
248         ** can convert what printf() generates.
249         */
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",
260                       p1, p2, NULL,
261                       p3, Pause,
262                       p4, &c);
263
264         /* Test that scanf() can scan typed input.
265         ** Retest that "%i" can decode radix prefixxes.
266         */
267         do {
268                Pause();
269                PRINTF("Type 3 signed numbers,\r\n"
270                       "separated by white-space:\r\n"
271                       "octal decimal hexadecimal\r\n"
272                       "? ");
273                c = SCANF("%i %i %i", &n1, &n2, &n3);
274                PRINTF("\r\n\nscanf() returned %i.\r\n"
275                       "The numbers are:\r\n"
276                       " %+o octal,\r\n"
277                       " %+d decimal,\r\n"
278                       " %+#X hexadecimal.\r\n",
279                       c, n1, n2, n3);
280                } while (c > 0);
281         return 0;
282         }