1 /*-
2 * Copyright (c) 2023 Dag-Erling Smørgrav
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7 #include <limits.h>
8 #include <locale.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <wchar.h>
12
13 #include <atf-c.h>
14
15 #define L(s) L ## s
16
17 static const struct swscanf_test_case {
18 wchar_t input[8];
19 struct {
20 int ret, val, len;
21 } b, o, d, x, i;
22 } swscanf_test_cases[] = {
23 // input binary octal decimal hexadecimal automatic
24 // all digits
25 { L"0", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, },
26 { L"1", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, },
27 { L"2", { 0, 0, 0 }, { 1, 2, 1 }, { 1, 2, 1 }, { 1, 2, 1 }, { 1, 2, 1 }, },
28 { L"3", { 0, 0, 0 }, { 1, 3, 1 }, { 1, 3, 1 }, { 1, 3, 1 }, { 1, 3, 1 }, },
29 { L"4", { 0, 0, 0 }, { 1, 4, 1 }, { 1, 4, 1 }, { 1, 4, 1 }, { 1, 4, 1 }, },
30 { L"5", { 0, 0, 0 }, { 1, 5, 1 }, { 1, 5, 1 }, { 1, 5, 1 }, { 1, 5, 1 }, },
31 { L"6", { 0, 0, 0 }, { 1, 6, 1 }, { 1, 6, 1 }, { 1, 6, 1 }, { 1, 6, 1 }, },
32 { L"7", { 0, 0, 0 }, { 1, 7, 1 }, { 1, 7, 1 }, { 1, 7, 1 }, { 1, 7, 1 }, },
33 { L"8", { 0, 0, 0 }, { 0, 0, 0 }, { 1, 8, 1 }, { 1, 8, 1 }, { 1, 8, 1 }, },
34 { L"9", { 0, 0, 0 }, { 0, 0, 0 }, { 1, 9, 1 }, { 1, 9, 1 }, { 1, 9, 1 }, },
35 { L"A", { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 1, 10, 1 }, { 0, 0, 0 }, },
36 { L"B", { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 1, 11, 1 }, { 0, 0, 0 }, },
37 { L"C", { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 1, 12, 1 }, { 0, 0, 0 }, },
38 { L"D", { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 1, 13, 1 }, { 0, 0, 0 }, },
39 { L"E", { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 1, 14, 1 }, { 0, 0, 0 }, },
40 { L"F", { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 1, 15, 1 }, { 0, 0, 0 }, },
41 { L"X", { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, },
42 { L"a", { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 1, 10, 1 }, { 0, 0, 0 }, },
43 { L"b", { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 1, 11, 1 }, { 0, 0, 0 }, },
44 { L"c", { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 1, 12, 1 }, { 0, 0, 0 }, },
45 { L"d", { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 1, 13, 1 }, { 0, 0, 0 }, },
46 { L"e", { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 1, 14, 1 }, { 0, 0, 0 }, },
47 { L"f", { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 1, 15, 1 }, { 0, 0, 0 }, },
48 { L"x", { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, },
49 // all digits with leading zero
50 { L"00", { 1, 0, 2 }, { 1, 0, 2 }, { 1, 0, 2 }, { 1, 0, 2 }, { 1, 0, 2 }, },
51 { L"01", { 1, 1, 2 }, { 1, 1, 2 }, { 1, 1, 2 }, { 1, 1, 2 }, { 1, 1, 2 }, },
52 { L"02", { 1, 0, 1 }, { 1, 2, 2 }, { 1, 2, 2 }, { 1, 2, 2 }, { 1, 2, 2 }, },
53 { L"03", { 1, 0, 1 }, { 1, 3, 2 }, { 1, 3, 2 }, { 1, 3, 2 }, { 1, 3, 2 }, },
54 { L"04", { 1, 0, 1 }, { 1, 4, 2 }, { 1, 4, 2 }, { 1, 4, 2 }, { 1, 4, 2 }, },
55 { L"05", { 1, 0, 1 }, { 1, 5, 2 }, { 1, 5, 2 }, { 1, 5, 2 }, { 1, 5, 2 }, },
56 { L"06", { 1, 0, 1 }, { 1, 6, 2 }, { 1, 6, 2 }, { 1, 6, 2 }, { 1, 6, 2 }, },
57 { L"07", { 1, 0, 1 }, { 1, 7, 2 }, { 1, 7, 2 }, { 1, 7, 2 }, { 1, 7, 2 }, },
58 { L"08", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 8, 2 }, { 1, 8, 2 }, { 1, 0, 1 }, },
59 { L"09", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 9, 2 }, { 1, 9, 2 }, { 1, 0, 1 }, },
60 { L"0A", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 10, 2 }, { 1, 0, 1 }, },
61 { L"0B", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 11, 2 }, { 1, 0, 1 }, },
62 { L"0C", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 12, 2 }, { 1, 0, 1 }, },
63 { L"0D", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 13, 2 }, { 1, 0, 1 }, },
64 { L"0E", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 14, 2 }, { 1, 0, 1 }, },
65 { L"0F", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 15, 2 }, { 1, 0, 1 }, },
66 { L"0X", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, },
67 { L"0a", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 10, 2 }, { 1, 0, 1 }, },
68 { L"0b", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 11, 2 }, { 1, 0, 1 }, },
69 { L"0c", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 12, 2 }, { 1, 0, 1 }, },
70 { L"0d", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 13, 2 }, { 1, 0, 1 }, },
71 { L"0e", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 14, 2 }, { 1, 0, 1 }, },
72 { L"0f", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 15, 2 }, { 1, 0, 1 }, },
73 { L"0x", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, },
74 // all digits with leading one
75 { L"10", { 1, 2, 2 }, { 1, 8, 2 }, { 1, 10, 2 }, { 1, 16, 2 }, { 1, 10, 2 }, },
76 { L"11", { 1, 3, 2 }, { 1, 9, 2 }, { 1, 11, 2 }, { 1, 17, 2 }, { 1, 11, 2 }, },
77 { L"12", { 1, 1, 1 }, { 1, 10, 2 }, { 1, 12, 2 }, { 1, 18, 2 }, { 1, 12, 2 }, },
78 { L"13", { 1, 1, 1 }, { 1, 11, 2 }, { 1, 13, 2 }, { 1, 19, 2 }, { 1, 13, 2 }, },
79 { L"14", { 1, 1, 1 }, { 1, 12, 2 }, { 1, 14, 2 }, { 1, 20, 2 }, { 1, 14, 2 }, },
80 { L"15", { 1, 1, 1 }, { 1, 13, 2 }, { 1, 15, 2 }, { 1, 21, 2 }, { 1, 15, 2 }, },
81 { L"16", { 1, 1, 1 }, { 1, 14, 2 }, { 1, 16, 2 }, { 1, 22, 2 }, { 1, 16, 2 }, },
82 { L"17", { 1, 1, 1 }, { 1, 15, 2 }, { 1, 17, 2 }, { 1, 23, 2 }, { 1, 17, 2 }, },
83 { L"18", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 18, 2 }, { 1, 24, 2 }, { 1, 18, 2 }, },
84 { L"19", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 19, 2 }, { 1, 25, 2 }, { 1, 19, 2 }, },
85 { L"1A", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 26, 2 }, { 1, 1, 1 }, },
86 { L"1B", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 27, 2 }, { 1, 1, 1 }, },
87 { L"1C", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 28, 2 }, { 1, 1, 1 }, },
88 { L"1D", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 29, 2 }, { 1, 1, 1 }, },
89 { L"1E", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 30, 2 }, { 1, 1, 1 }, },
90 { L"1F", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 31, 2 }, { 1, 1, 1 }, },
91 { L"1X", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, },
92 { L"1a", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 26, 2 }, { 1, 1, 1 }, },
93 { L"1b", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 27, 2 }, { 1, 1, 1 }, },
94 { L"1c", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 28, 2 }, { 1, 1, 1 }, },
95 { L"1d", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 29, 2 }, { 1, 1, 1 }, },
96 { L"1e", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 30, 2 }, { 1, 1, 1 }, },
97 { L"1f", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 31, 2 }, { 1, 1, 1 }, },
98 { L"1x", { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, },
99 // all digits with leading binary prefix
100 { L"0b0", { 1, 0, 3 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 176, 3 }, { 1, 0, 3 }, },
101 { L"0b1", { 1, 1, 3 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 177, 3 }, { 1, 1, 3 }, },
102 { L"0b2", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 178, 3 }, { 1, 0, 1 }, },
103 { L"0b3", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 179, 3 }, { 1, 0, 1 }, },
104 { L"0b4", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 180, 3 }, { 1, 0, 1 }, },
105 { L"0b5", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 181, 3 }, { 1, 0, 1 }, },
106 { L"0b6", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 182, 3 }, { 1, 0, 1 }, },
107 { L"0b7", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 183, 3 }, { 1, 0, 1 }, },
108 { L"0b8", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 184, 3 }, { 1, 0, 1 }, },
109 { L"0b9", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 185, 3 }, { 1, 0, 1 }, },
110 { L"0bA", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 186, 3 }, { 1, 0, 1 }, },
111 { L"0bB", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 187, 3 }, { 1, 0, 1 }, },
112 { L"0bC", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 188, 3 }, { 1, 0, 1 }, },
113 { L"0bD", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 189, 3 }, { 1, 0, 1 }, },
114 { L"0bE", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 190, 3 }, { 1, 0, 1 }, },
115 { L"0bF", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 191, 3 }, { 1, 0, 1 }, },
116 { L"0bX", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 11, 2 }, { 1, 0, 1 }, },
117 { L"0ba", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 186, 3 }, { 1, 0, 1 }, },
118 { L"0bb", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 187, 3 }, { 1, 0, 1 }, },
119 { L"0bc", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 188, 3 }, { 1, 0, 1 }, },
120 { L"0bd", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 189, 3 }, { 1, 0, 1 }, },
121 { L"0be", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 190, 3 }, { 1, 0, 1 }, },
122 { L"0bf", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 191, 3 }, { 1, 0, 1 }, },
123 { L"0bx", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 11, 2 }, { 1, 0, 1 }, },
124 // all digits with leading hexadecimal prefix
125 { L"0x0", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 3 }, { 1, 0, 3 }, },
126 { L"0x1", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 1, 3 }, { 1, 1, 3 }, },
127 { L"0x2", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 2, 3 }, { 1, 2, 3 }, },
128 { L"0x3", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 3, 3 }, { 1, 3, 3 }, },
129 { L"0x4", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 4, 3 }, { 1, 4, 3 }, },
130 { L"0x5", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 5, 3 }, { 1, 5, 3 }, },
131 { L"0x6", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 6, 3 }, { 1, 6, 3 }, },
132 { L"0x7", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 7, 3 }, { 1, 7, 3 }, },
133 { L"0x8", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 8, 3 }, { 1, 8, 3 }, },
134 { L"0x9", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 9, 3 }, { 1, 9, 3 }, },
135 { L"0xA", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 10, 3 }, { 1, 10, 3 }, },
136 { L"0xB", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 11, 3 }, { 1, 11, 3 }, },
137 { L"0xC", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 12, 3 }, { 1, 12, 3 }, },
138 { L"0xD", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 13, 3 }, { 1, 13, 3 }, },
139 { L"0xE", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 14, 3 }, { 1, 14, 3 }, },
140 { L"0xF", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 15, 3 }, { 1, 15, 3 }, },
141 { L"0xX", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, },
142 { L"0xa", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 10, 3 }, { 1, 10, 3 }, },
143 { L"0xb", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 11, 3 }, { 1, 11, 3 }, },
144 { L"0xc", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 12, 3 }, { 1, 12, 3 }, },
145 { L"0xd", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 13, 3 }, { 1, 13, 3 }, },
146 { L"0xe", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 14, 3 }, { 1, 14, 3 }, },
147 { L"0xf", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 15, 3 }, { 1, 15, 3 }, },
148 { L"0xX", { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 }, },
149 // terminator
150 { L"" }
151 };
152
153 #define SWSCANF_TEST(string, format, expret, expval, explen) \
154 do { \
155 int ret = 0, val = 0, len = 0; \
156 ret = swscanf(string, format L"%n", &val, &len); \
157 ATF_CHECK_EQ(expret, ret); \
158 if (expret && ret) { \
159 ATF_CHECK_EQ(expval, val); \
160 ATF_CHECK_EQ(explen, len); \
161 } \
162 } while (0)
163
164 ATF_TC_WITHOUT_HEAD(swscanf_b);
ATF_TC_BODY(swscanf_b,tc)165 ATF_TC_BODY(swscanf_b, tc)
166 {
167 const struct swscanf_test_case *stc;
168 wchar_t input[16];
169
170 for (stc = swscanf_test_cases; *stc->input; stc++) {
171 wcscpy(input + 1, stc->input);
172 SWSCANF_TEST(input + 1, L"%b", stc->b.ret, stc->b.val, stc->b.len);
173 input[0] = L'+';
174 SWSCANF_TEST(input, L"%b", stc->b.ret, stc->b.val, stc->b.len ? stc->b.len + 1 : 0);
175 input[0] = L'-';
176 SWSCANF_TEST(input, L"%b", stc->b.ret, -stc->b.val, stc->b.len ? stc->b.len + 1 : 0);
177 }
178 }
179
180 ATF_TC_WITHOUT_HEAD(swscanf_o);
ATF_TC_BODY(swscanf_o,tc)181 ATF_TC_BODY(swscanf_o, tc)
182 {
183 const struct swscanf_test_case *stc;
184 wchar_t input[16];
185
186 for (stc = swscanf_test_cases; *stc->input; stc++) {
187 wcscpy(input + 1, stc->input);
188 SWSCANF_TEST(input + 1, L"%o", stc->o.ret, stc->o.val, stc->o.len);
189 input[0] = L'+';
190 SWSCANF_TEST(input, L"%o", stc->o.ret, stc->o.val, stc->o.len ? stc->o.len + 1 : 0);
191 input[0] = L'-';
192 SWSCANF_TEST(input, L"%o", stc->o.ret, -stc->o.val, stc->o.len ? stc->o.len + 1 : 0);
193 }
194 }
195
196 ATF_TC_WITHOUT_HEAD(swscanf_d);
ATF_TC_BODY(swscanf_d,tc)197 ATF_TC_BODY(swscanf_d, tc)
198 {
199 const struct swscanf_test_case *stc;
200 wchar_t input[16];
201
202 for (stc = swscanf_test_cases; *stc->input; stc++) {
203 wcscpy(input + 1, stc->input);
204 SWSCANF_TEST(input + 1, L"%d", stc->d.ret, stc->d.val, stc->d.len);
205 input[0] = L'+';
206 SWSCANF_TEST(input, L"%d", stc->d.ret, stc->d.val, stc->d.len ? stc->d.len + 1 : 0);
207 input[0] = L'-';
208 SWSCANF_TEST(input, L"%d", stc->d.ret, -stc->d.val, stc->d.len ? stc->d.len + 1 : 0);
209 }
210 }
211
212 ATF_TC_WITHOUT_HEAD(swscanf_x);
ATF_TC_BODY(swscanf_x,tc)213 ATF_TC_BODY(swscanf_x, tc)
214 {
215 const struct swscanf_test_case *stc;
216 wchar_t input[16];
217
218 for (stc = swscanf_test_cases; *stc->input; stc++) {
219 wcscpy(input + 1, stc->input);
220 SWSCANF_TEST(input + 1, L"%x", stc->x.ret, stc->x.val, stc->x.len);
221 input[0] = L'+';
222 SWSCANF_TEST(input, L"%x", stc->x.ret, stc->x.val, stc->x.len ? stc->x.len + 1 : 0);
223 input[0] = L'-';
224 SWSCANF_TEST(input, L"%x", stc->x.ret, -stc->x.val, stc->x.len ? stc->x.len + 1 : 0);
225 }
226 }
227
228 ATF_TC_WITHOUT_HEAD(swscanf_i);
ATF_TC_BODY(swscanf_i,tc)229 ATF_TC_BODY(swscanf_i, tc)
230 {
231 const struct swscanf_test_case *stc;
232 wchar_t input[16];
233
234 for (stc = swscanf_test_cases; *stc->input; stc++) {
235 wcscpy(input + 1, stc->input);
236 SWSCANF_TEST(input + 1, L"%i", stc->i.ret, stc->i.val, stc->i.len);
237 input[0] = L'+';
238 SWSCANF_TEST(input, L"%i", stc->i.ret, stc->i.val, stc->i.len ? stc->i.len + 1 : 0);
239 input[0] = L'-';
240 SWSCANF_TEST(input, L"%i", stc->i.ret, -stc->i.val, stc->i.len ? stc->i.len + 1 : 0);
241 }
242 }
243
244 ATF_TC_WITHOUT_HEAD(swscanf_wN);
ATF_TC_BODY(swscanf_wN,tc)245 ATF_TC_BODY(swscanf_wN, tc)
246 {
247 const wchar_t x00[] = L"0x00";
248 const wchar_t x7f[] = L"0x7fffffffffffffff";
249 const wchar_t xff[] = L"0xffffffffffffffff";
250
251 #define SWSCANF_WN_TEST(N, imin, umax) \
252 do { \
253 int##N##_t i; \
254 uint##N##_t u; \
255 ATF_CHECK_EQ(1, swscanf(x00, L"%w" L(#N) L"i", &i)); \
256 ATF_CHECK_EQ(0, i); \
257 ATF_CHECK_EQ(1, swscanf(x7f, L"%w" L(#N) L"i", &i)); \
258 ATF_CHECK_EQ(imin, i); \
259 ATF_CHECK_EQ(1, swscanf(x00, L"%w" L(#N) L"x", &u)); \
260 ATF_CHECK_EQ(0, u); \
261 ATF_CHECK_EQ(1, swscanf(xff, L"%w" L(#N) L"x", &u)); \
262 ATF_CHECK_EQ(umax, u); \
263 } while (0)
264 SWSCANF_WN_TEST(8, -1, UCHAR_MAX);
265 SWSCANF_WN_TEST(16, -1, USHRT_MAX);
266 SWSCANF_WN_TEST(32, -1, UINT_MAX);
267 SWSCANF_WN_TEST(64, LLONG_MAX, ULLONG_MAX);
268 #undef SWSCANF_WN_TEST
269
270 ATF_CHECK_EQ(0, swscanf(x00, L"%wi", (int *)NULL));
271 ATF_CHECK_EQ(0, swscanf(x00, L"%w1i", (int *)NULL));
272 ATF_CHECK_EQ(0, swscanf(x00, L"%w128i", (int *)NULL));
273 }
274
275 ATF_TC_WITHOUT_HEAD(swscanf_wfN);
ATF_TC_BODY(swscanf_wfN,tc)276 ATF_TC_BODY(swscanf_wfN, tc)
277 {
278 const wchar_t x00[] = L"0x00";
279 const wchar_t x7f[] = L"0x7fffffffffffffff";
280 const wchar_t xff[] = L"0xffffffffffffffff";
281
282 #define SWSCANF_WFN_TEST(N, imin, umax) \
283 do { \
284 int_fast##N##_t i; \
285 uint_fast##N##_t u; \
286 ATF_CHECK_EQ(1, swscanf(x00, L"%wf" L(#N) L"i", &i)); \
287 ATF_CHECK_EQ(0, i); \
288 ATF_CHECK_EQ(1, swscanf(x7f, L"%wf" L(#N) L"i", &i)); \
289 ATF_CHECK_EQ(imin, i); \
290 ATF_CHECK_EQ(1, swscanf(x00, L"%wf" L(#N) L"x", &u)); \
291 ATF_CHECK_EQ(0, u); \
292 ATF_CHECK_EQ(1, swscanf(xff, L"%wf" L(#N) L"x", &u)); \
293 ATF_CHECK_EQ(umax, u); \
294 } while (0)
295 SWSCANF_WFN_TEST(8, -1, UINT_MAX);
296 SWSCANF_WFN_TEST(16, -1, UINT_MAX);
297 SWSCANF_WFN_TEST(32, -1, UINT_MAX);
298 SWSCANF_WFN_TEST(64, LLONG_MAX, ULLONG_MAX);
299 #undef SWSCANF_WFN_TEST
300
301 ATF_CHECK_EQ(0, swscanf(x00, L"%wfi", (int *)NULL));
302 ATF_CHECK_EQ(0, swscanf(x00, L"%wf1i", (int *)NULL));
303 ATF_CHECK_EQ(0, swscanf(x00, L"%wf128i", (int *)NULL));
304 }
305
306 /*
307 * Test termination cases: non-numeric character, fixed width, EOF
308 */
309 ATF_TC_WITHOUT_HEAD(swscanf_termination);
ATF_TC_BODY(swscanf_termination,tc)310 ATF_TC_BODY(swscanf_termination, tc)
311 {
312 int a = 0, b = 0, c = 0;
313 wchar_t d = 0;
314
315 ATF_CHECK_EQ(4, swscanf(L"3.1415", L"%d%lc%2d%d", &a, &d, &b, &c));
316 ATF_CHECK_EQ(3, a);
317 ATF_CHECK_EQ(14, b);
318 ATF_CHECK_EQ(15, c);
319 ATF_CHECK_EQ(L'.', d);
320 }
321
ATF_TP_ADD_TCS(tp)322 ATF_TP_ADD_TCS(tp)
323 {
324 setlocale(LC_NUMERIC, "en_US.UTF-8");
325 ATF_TP_ADD_TC(tp, swscanf_b);
326 ATF_TP_ADD_TC(tp, swscanf_o);
327 ATF_TP_ADD_TC(tp, swscanf_d);
328 ATF_TP_ADD_TC(tp, swscanf_x);
329 ATF_TP_ADD_TC(tp, swscanf_i);
330 ATF_TP_ADD_TC(tp, swscanf_wN);
331 ATF_TP_ADD_TC(tp, swscanf_wfN);
332 ATF_TP_ADD_TC(tp, swscanf_termination);
333 return (atf_no_error());
334 }
335