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