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
28 #define TEST(r, f, x, m) ( \
29 ((r) = (f)) == (x) || \
30 (printf(__FILE__ ":%d: %s failed (" m ")\n", __LINE__, #f, r, x), err++, 0) )
31
32 #define TEST_S(s, x, m) ( \
33 !strcmp((s),(x)) || \
34 (printf(__FILE__ ":%d: [%s] != [%s] (%s)\n", __LINE__, s, x, m), err++, 0) )
35
36 #define TEST_F(x) ( \
37 TEST(i, sscanf(# x, "%lf", &d), 1, "got %d fields, expected %d"), \
38 TEST(t, d, (double)x, "%g != %g") )
39
40 #pragma GCC diagnostic ignored "-Wpragmas"
41 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
42 #pragma GCC diagnostic ignored "-Wunused-value"
43 #pragma GCC diagnostic ignored "-Woverflow"
44 #pragma GCC diagnostic ignored "-Wliteral-range"
45 #pragma GCC diagnostic ignored "-Wformat-extra-args"
46
test_sscanf(void)47 int test_sscanf(void)
48 {
49 int i;
50 int err=0;
51 char a[100], b[100];
52 int x, y, z, u, v;
53 #ifdef _WANT_IO_PERCENT_B
54 int w;
55 #endif
56 double d, t;
57 // long lo[10];
58
59 TEST(i, sscanf("hello, world\n", "%s %s", a, b), 2, "only %d fields, expected %d");
60 TEST_S(a, "hello,", "");
61 TEST_S(b, "world", "");
62
63 TEST(i, sscanf("hello, world\n", "%[hel]%s", a, b), 2, "only %d fields, expected %d");
64 TEST_S(a, "hell", "");
65 TEST_S(b, "o,", "");
66
67 TEST(i, sscanf("hello, world\n", "%[hel] %s", a, b), 2, "only %d fields, expected %d");
68 TEST_S(a, "hell", "");
69 TEST_S(b, "o,", "");
70
71 #ifdef TINY_STDIO
72 a[8] = 'X';
73 a[9] = 0;
74 /* legacy stdio fails this test */
75 TEST(i, sscanf("hello, world\n", "%8c%8c", a, b), 1, "%d fields, expected %d");
76 TEST_S(a, "hello, wX", "");
77 #endif
78
79 TEST(i, sscanf("56789 0123 56a72", "%2d%d%*d %[0123456789]\n", &x, &y, a), 3, "only %d fields, expected %d");
80 TEST(i, x, 56, "%d != %d");
81 TEST(i, y, 789, "%d != %d");
82 TEST_S(a, "56", "");
83
84 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");
85 TEST(i, x, 9, "%d != %d");
86 TEST(i, y, 256, "%d != %d");
87 TEST(i, z, 9, "%d != %d");
88 TEST(i, u, 256, "%d != %d");
89 TEST(i, v, 256, "%d != %d");
90
91 #pragma GCC diagnostic ignored "-Wformat"
92 #pragma GCC diagnostic ignored "-Wformat-extra-args"
93
94 #ifdef _WANT_IO_PERCENT_B
95 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");
96 TEST(i, x, 9, "%d != %d");
97 TEST(i, y, 256, "%d != %d");
98 TEST(i, z, 5, "%d != %d");
99 TEST(i, u, 9, "%d != %d");
100 TEST(i, v, 256, "%d != %d");
101 TEST(i, w, 5, "%d != %d");
102 #endif
103
104 TEST(i, sscanf("20 xyz", "%d %d\n", &x, &y), 1, "only %d fields, expected %d");
105 TEST(i, x, 20, "%d != %d");
106
107 TEST(i, sscanf("xyz", "%d\n", &x, &y), 0, "got %d fields, expected no match (%d)");
108
109 TEST(i, sscanf("", "%d\n", &x, &y), -1, "got %d fields, expected input failure (%d)");
110
111 TEST(i, sscanf(" 12345 6", "%2d%d%d", &x, &y, &z), 3, "only %d fields, expected %d");
112 TEST(i, x, 12, "%d != %d");
113 TEST(i, y, 345, "%d != %d");
114 TEST(i, z, 6, "%d != %d");
115
116 // tinystdio only has one ungetc spot, so it can't unparse numbers
117 // TEST(i, sscanf(" 0x12 0x34", "%5i%2i", &x, &y), 1, "got %d fields, expected %d");
118 // TEST(i, x, 0x12, "%d != %d");
119
120 (void) d;
121 (void) t;
122 #if !defined(NO_FLOATING_POINT) && defined(_IO_FLOAT_EXACT)
123 TEST_F(123);
124 TEST_F(123.0);
125 TEST_F(123.0e+0);
126 TEST_F(123.0e+4);
127 TEST_F(1.234e1234);
128 TEST_F(1.234e-1234);
129 TEST_F(1.234e56789);
130 TEST_F(1.234e-56789);
131 TEST_F(-0.5);
132 TEST_F(0.1);
133 TEST_F(0.2);
134 TEST_F(0.1e-10);
135 TEST_F(0x1234p56);
136
137 #ifndef __PICOLIBC__
138 /* both tinystdio and legacy stdio fail this test */
139 TEST(i, sscanf("10e", "%lf", &d), 0, "got %d fields, expected no match (%d)");
140 #endif
141 TEST(i, sscanf("", "%lf\n", &d), -1, "got %d fields, expected input failure (%d)");
142 #endif
143
144
145 return err;
146 }
147
148 #define TEST_NAME sscanf
149 #include "testcase.h"
150