1 /*
2 * Copyright © 2005-2020 Rich Felker
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include <stdio.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <locale.h>
28 #include <wchar.h>
29 #include <math.h>
30
31 #define TEST(r, f, x, m) ( \
32 ((r) = (f)) == (x) || \
33 (printf(__FILE__ ":%d: %s failed (" m ")\n", __LINE__, #f, r, x), err++, 0) )
34
35 #define TEST_S(s, x, m) ( \
36 !strcmp((s),(x)) || \
37 (printf(__FILE__ ":%d: [%s] != [%s] (%s)\n", __LINE__, s, x, m), err++, 0) )
38
39 #define TEST_WS(s, x, m) ( \
40 !wcscmp((s),(x)) || \
41 (printf(__FILE__ ":%d: [%ls] != [%ls] (%ls)\n", __LINE__, s, x, m), err++, 0) )
42
43 #define TEST_F(x) ( \
44 TEST(i, sscanf(# x, "%lf", &d), 1, "got %d fields, expected %d"), \
45 TEST(t, d, (double)x, "%a != %a") )
46
47 #define TEST_FV(x,v) (\
48 TEST(i, sscanf(v, "%lf", &d), 1, "got %d fields, expected %d"), \
49 TEST(t, d, (double)x, "%a != %a") )
50
51 #pragma GCC diagnostic ignored "-Wpragmas"
52 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
53 #pragma GCC diagnostic ignored "-Wunused-value"
54 #pragma GCC diagnostic ignored "-Woverflow"
55 #pragma GCC diagnostic ignored "-Wliteral-range"
56 #pragma GCC diagnostic ignored "-Wformat-extra-args"
57
58 #if defined(__PICOLIBC__)
59
60 #if !defined(TINY_STDIO) && __SIZEOF_DOUBLE__ != 8
61 #define NO_FLOATING_POINT
62 #endif
63
64 #if !defined(_MB_CAPABLE) || !defined(TINY_STDIO)
65 #define NO_MULTI_BYTE
66 #endif
67
68 #endif
69
test_sscanf(void)70 static int test_sscanf(void)
71 {
72 int i;
73 int err=0;
74 char a[100], b[100], c[100];
75 wchar_t aw[100], bw[100], cw[100];
76 int x, y, z, u, v;
77 #ifdef _WANT_IO_PERCENT_B
78 int w;
79 #endif
80 double d, t;
81 // long lo[10];
82
83 #if !defined(__PICOLIBC__) || defined(_MB_CAPABLE)
84 if (!setlocale(LC_CTYPE, "C.UTF-8")) {
85 printf("setlocale(LC_CTYPE, \"C.UTF-8\") failed\n");
86 return 1;
87 }
88 #endif
89 (void) aw;
90 (void) bw;
91 (void) cw;
92
93 /* sscanf with char results */
94
95 TEST(i, sscanf("hello, world\n", "%s %s", a, b), 2, "only %d fields, expected %d");
96 TEST_S(a, "hello,", "");
97 TEST_S(b, "world", "");
98
99 memset(a, 0, sizeof(a));
100 memset(b, 0, sizeof(b));
101 memset(c, 0, sizeof(c));
102 TEST(i, sscanf("hello, world\n", "%6c %5c%c", a, b, c), 3, "only %d fields, expected %d");
103 TEST_S(a, "hello,", "");
104 TEST_S(b, "world", "");
105 TEST_S(c, "\n", "");
106
107 TEST(i, sscanf("hello, world\n", "%[hel]%s", a, b), 2, "only %d fields, expected %d");
108 TEST_S(a, "hell", "");
109 TEST_S(b, "o,", "");
110
111 TEST(i, sscanf("hello, world\n", "%[hel] %s", a, b), 2, "only %d fields, expected %d");
112 TEST_S(a, "hell", "");
113 TEST_S(b, "o,", "");
114
115 TEST(i, sscanf("elloworld", "%1[abcdefg]%s", a, b), 2, "only %d fields, expected %d");
116 TEST_S(a, "e", "");
117 TEST_S(b, "lloworld", "");
118
119 TEST(i, sscanf("elloworld", "%5[a-z]%s", a, b), 2, "only %d fields, expected %d");
120 TEST_S(a, "ellow", "");
121 TEST_S(b, "orld", "");
122
123 #ifndef NO_MULTI_BYTE
124 /* sscanf with mb results */
125
126 TEST(i, sscanf("㌰ello, ✕orld\n", "%s %s", a, b), 2, "only %d fields, expected %d");
127 TEST_S(a, "㌰ello,", "");
128 TEST_S(b, "✕orld", "");
129
130 memset(a, 0, sizeof(a));
131 memset(b, 0, sizeof(b));
132 memset(c, 0, sizeof(c));
133 TEST(i, sscanf("㌰ello, ✕orld\n", "%8c %7c%c", a, b, c), 3, "only %d fields, expected %d");
134 TEST_S(a, "㌰ello,", "");
135 TEST_S(b, "✕orld", "");
136 TEST_S(c, "\n", "");
137
138 TEST(i, sscanf("㌰ello, ✕orld\n", "%[㌰el]%s", a, b), 2, "only %d fields, expected %d");
139 TEST_S(a, "㌰ell", "");
140 TEST_S(b, "o,", "");
141
142 TEST(i, sscanf("㌰ello, ✕orld\n", "%[㌰el] %s", a, b), 2, "only %d fields, expected %d");
143 TEST_S(a, "㌰ell", "");
144 TEST_S(b, "o,", "");
145
146 /* sscanf with wchar results */
147
148 TEST(i, sscanf("㌰ello, ✕orld\n", "%ls %ls", aw, bw), 2, "only %d fields, expected %d");
149 TEST_WS(aw, L"㌰ello,", L"");
150 TEST_WS(bw, L"✕orld", L"");
151
152 memset(aw, 0, sizeof(aw));
153 memset(bw, 0, sizeof(bw));
154 memset(cw, 0, sizeof(cw));
155 TEST(i, sscanf("㌰ello, ✕orld\n", "%6lc %5lc%lc", aw, bw, cw), 3, "only %d fields, expected %d");
156 TEST_WS(aw, L"㌰ello,", L"");
157 TEST_WS(bw, L"✕orld", L"");
158 TEST_WS(cw, L"\n", L"");
159
160 TEST(i, sscanf("㌰ello, ✕orld\n", "%l[^o]%ls", aw, bw), 2, "only %d fields, expected %d");
161 TEST_WS(aw, L"㌰ell", L"");
162 TEST_WS(bw, L"o,", L"");
163
164 TEST(i, sscanf("㌰ello, ✕orld\n", "%l[^o] %ls", aw, bw), 2, "only %d fields, expected %d");
165 TEST_S(a, "㌰ell", "");
166 TEST_S(b, "o,", "");
167
168 /* swscanf with mb results */
169
170 TEST(i, swscanf(L"hello, world\n", L"%s %s", a, b), 2, "only %d fields, expected %d");
171 TEST_S(a, "hello,", "");
172 TEST_S(b, "world", "");
173
174 TEST(i, swscanf(L"㌰ello, ✕orld\n", L"%s %s", a, b), 2, "only %d fields, expected %d");
175 TEST_S(a, "㌰ello,", "");
176 TEST_S(b, "✕orld", "");
177
178 memset(a, 0, sizeof(a));
179 memset(b, 0, sizeof(b));
180 memset(c, 0, sizeof(c));
181 TEST(i, swscanf(L"hello, world\n", L"%6c %5c%c", a, b, c), 3, "only %d fields, expected %d");
182 TEST_S(a, "hello,", "");
183 TEST_S(b, "world", "");
184 TEST_S(c, "\n", "");
185
186 memset(a, 0, sizeof(a));
187 memset(b, 0, sizeof(b));
188 memset(c, 0, sizeof(c));
189 TEST(i, swscanf(L"㌰ello, ✕orld\n", L"%6c %5c%c", a, b, c), 3, "only %d fields, expected %d");
190 TEST_S(a, "㌰ello,", "");
191 TEST_S(b, "✕orld", "");
192 TEST_S(c, "\n", "");
193
194 /* swscanf with wchar results */
195
196 TEST(i, swscanf(L"㌰ello, ✕orld\n", L"%ls %ls", aw, bw), 2, "only %d fields, expected %d");
197 TEST_WS(aw, L"㌰ello,", L"");
198 TEST_WS(bw, L"✕orld", L"");
199
200 memset(aw, 0, sizeof(aw));
201 memset(bw, 0, sizeof(bw));
202 memset(cw, 0, sizeof(cw));
203 TEST(i, swscanf(L"㌰ello, ✕orld\n", L"%6lc %5lc%lc", aw, bw, cw), 3, "only %d fields, expected %d");
204 TEST_WS(aw, L"㌰ello,", L"");
205 TEST_WS(bw, L"✕orld", L"");
206 TEST_WS(cw, L"\n", L"");
207 #endif
208
209 #ifdef TINY_STDIO
210 a[8] = 'X';
211 a[9] = 0;
212 /* legacy stdio fails this test */
213 TEST(i, sscanf("hello, world\n", "%8c%8c", a, b), 1, "%d fields, expected %d");
214 TEST_S(a, "hello, wX", "");
215
216 /* testing nan(n-seq-char) in the expected form */
217 a[0] = '#';
218 a[1] = '\0';
219 TEST(i, sscanf(" Nan(98b)", "%lf%c", &d, a), 1, "got %d fields, expected %d");
220 TEST(i, isnan(d), 1, "isnan %d expected %d");
221 TEST_S(a, "#", "");
222
223 /* testing nan(n-seq-char) missing closing paren */
224 a[0] = '#';
225 a[1] = '\0';
226 d = 1.0;
227 TEST(i, sscanf("NaN(abcdefg", "%lf%c", &d, a), -1, "got %d fields, expected %d");
228 TEST(i, d, 1.0, "%d expected %lf");
229 TEST_S(a, "#", "");
230
231 /* testing nan(n-seq-char) invalid character inside parens */
232 a[0] = '#';
233 a[1] = '\0';
234 d = 1.0;
235 TEST(i, sscanf("NaN(12:b)", "%lf%c", &d, a), 0, "got %d fields, expected %d");
236 TEST(i, d, 1.0, "%d expected %lf");
237 TEST_S(a, "#", "");
238
239 /* testing nan(n-seq-char) overrunning a field width value */
240 a[0] = '#';
241 a[1] = '\0';
242 d = 1.0;
243 TEST(i, sscanf("Nan(12345)", "%9lf%c", &d, a), 0, "got %d fields, expected %d");
244 TEST(i, d, 1.0, "%d expected %lf");
245 TEST_S(a, "#", "");
246
247 /* testing inf(n-seq-char) should 'inf' should be evaluated sperately */
248 a[0] = '#';
249 a[1] = '\0';
250 d = 1.0;
251 TEST(i, sscanf("inf(12b)", "%lf%c", &d, a), 2, "got %d fields, expected %d");
252 TEST(i, isinf(d), 1, "%d expected %d");
253 TEST_S(a, "(", "");
254
255 /* testing infinity(n-seq-char) should 'infinity' should be evaluated sperately */
256 a[0] = '#';
257 a[1] = '\0';
258 d = 1.0;
259 TEST(i, sscanf("infinity(abcd)", "%lf%c", &d, a), 2, "got %d fields, expected %d");
260 TEST(i, isinf(d), 1, "%d expected %d");
261 TEST_S(a, "(", "");
262 #endif
263
264 TEST(i, sscanf("56789 0123 56a72", "%2d%d%*d %[0123456789]\n", &x, &y, a), 3, "only %d fields, expected %d");
265 TEST(i, x, 56, "%d != %d");
266 TEST(i, y, 789, "%d != %d");
267 TEST_S(a, "56", "");
268
269 TEST(i, sscanf("011 0x100 11 0x100 100", "%i %i %o %x %x\n", &x, &y, &z, &u, &v), 5, "only %d fields, expected %d");
270 TEST(i, x, 9, "%d != %d");
271 TEST(i, y, 256, "%d != %d");
272 TEST(i, z, 9, "%d != %d");
273 TEST(i, u, 256, "%d != %d");
274 TEST(i, v, 256, "%d != %d");
275
276 #pragma GCC diagnostic ignored "-Wformat"
277 #pragma GCC diagnostic ignored "-Wformat-extra-args"
278
279 #ifdef _WANT_IO_PERCENT_B
280 TEST(i, sscanf("011 0x100 0b101 11 100 101", "%i %i %i %o %x %b\n", &x, &y, &z, &u, &v, &w), 6, "only %d fields, expected %d");
281 TEST(i, x, 9, "%d != %d");
282 TEST(i, y, 256, "%d != %d");
283 TEST(i, z, 5, "%d != %d");
284 TEST(i, u, 9, "%d != %d");
285 TEST(i, v, 256, "%d != %d");
286 TEST(i, w, 5, "%d != %d");
287 #endif
288
289 TEST(i, sscanf("20 xyz", "%d %d\n", &x, &y), 1, "only %d fields, expected %d");
290 TEST(i, x, 20, "%d != %d");
291
292 TEST(i, sscanf("xyz", "%d\n", &x, &y), 0, "got %d fields, expected no match (%d)");
293
294 TEST(i, sscanf("", "%d\n", &x, &y), -1, "got %d fields, expected input failure (%d)");
295
296 TEST(i, sscanf(" 12345 6", "%2d%d%d", &x, &y, &z), 3, "only %d fields, expected %d");
297 TEST(i, x, 12, "%d != %d");
298 TEST(i, y, 345, "%d != %d");
299 TEST(i, z, 6, "%d != %d");
300
301 // tinystdio only has one ungetc spot, so it can't unparse numbers
302 // TEST(i, sscanf(" 0x12 0x34", "%5i%2i", &x, &y), 1, "got %d fields, expected %d");
303 // TEST(i, x, 0x12, "%d != %d");
304
305 (void) d;
306 (void) t;
307 #if !defined(NO_FLOATING_POINT) && defined(_IO_FLOAT_EXACT)
308 TEST_F(123);
309 TEST_F(123.0);
310 TEST_F(123.0e+0);
311 TEST_F(123.0e+4);
312 TEST_F(1.234e1234);
313 TEST_F(1.234e-1234);
314 TEST_F(1.234e56789);
315 TEST_F(1.234e-56789);
316 TEST_F(-0.5);
317 TEST_F(0.1);
318 TEST_F(0.2);
319 TEST_F(0.1e-10);
320 TEST_F(0x1234p56);
321 TEST_F(3752432815e-39);
322 TEST(i, sscanf("nan", "%lg", &d), 1, "got %d fields, expected %d");
323 TEST(i, isnan(d), 1, "isnan %d expected %d");
324 TEST(i, !!signbit(d), 0, "signbit %d expected %d");
325 TEST(i, sscanf("-nan", "%lg", &d), 1, "got %d fields, expected %d");
326 TEST(i, isnan(d), 1, "isnan %d expected %d");
327 TEST(i, !!signbit(d), 1, "signbit %d expected %d");
328 TEST_FV(INFINITY, "inf");
329 TEST_FV(-INFINITY, "-inf");
330
331 d = 1.0;
332 TEST(i, sscanf("-inf", "%3lg", &d), 0, "got %d fields, expected %d");
333 TEST(i, d, 1.0, "%g expected %g");
334
335 TEST(i, sscanf("-inf", "%4lg", &d), 1, "got %d fields, expected %d");
336 TEST(i, isinf(d), 1, "isinf %d expected %d");
337 TEST(i, !!signbit(d), 1, "signbit %d expected %d");
338
339 #ifndef __PICOLIBC__
340 /* both tinystdio and legacy stdio fail this test */
341 TEST(i, sscanf("10e", "%lf", &d), 0, "got %d fields, expected no match (%d)");
342 #endif
343 TEST(i, sscanf("", "%lf\n", &d), -1, "got %d fields, expected input failure (%d)");
344 #endif
345
346
347 return err;
348 }
349
350 #define TEST_NAME sscanf
351 #include "testcase.h"
352