1 /* Test scanf formats for intN_t, int_leastN_t and int_fastN_t types.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
25 #include <libc-diag.h>
26 #include <support/check.h>
28 /* GCC does not know the %wN or %wfN length modifiers before GCC 13. */
29 DIAG_PUSH_NEEDS_COMMENT
;
30 #if !__GNUC_PREREQ (13, 0)
31 DIAG_IGNORE_NEEDS_COMMENT (12, "-Wformat");
32 DIAG_IGNORE_NEEDS_COMMENT (12, "-Wformat-extra-args");
35 #define CHECK_SCANF1(EXPECTED, STR, FMT) \
38 var = ((typeof (var)) 0xabababab); \
39 int ret = SSCANF (L_(STR), L_(FMT), &var); \
40 TEST_COMPARE (var, (EXPECTED)); \
41 TEST_COMPARE (ret, 1); \
45 #define CHECK_SCANF1N(EXPECTED, STR, FMT) \
48 var = ((typeof (var)) 0xabababab); \
50 int ret = SSCANF (L_(STR), L_(FMT), &var, &n); \
51 TEST_COMPARE (var, (EXPECTED)); \
52 TEST_COMPARE (n, STRLEN (L_(STR))); \
53 TEST_COMPARE (ret, 1); \
57 #define CHECK_SCANF_ERR(OK, STR, FMT, ...) \
60 int ret = SSCANF (L_(STR), L_(FMT), __VA_ARGS__); \
61 TEST_VERIFY (ret == (OK)); \
62 TEST_COMPARE (errno, EINVAL); \
71 CHECK_SCANF1 (42, "42", "%w8d");
72 CHECK_SCANF1N (42, "42", "%w8d%w8n");
73 CHECK_SCANF1 (-43, "-43", "%w8d");
74 CHECK_SCANF1 (42, "42", "%w8i");
75 CHECK_SCANF1 (-43, "-43", "%w8i");
76 CHECK_SCANF1 (123, "0b1111011", "%w8i");
77 CHECK_SCANF1 (127, "0x7f", "%w8i");
78 CHECK_SCANF1 (-19, "-023", "%w8i");
83 CHECK_SCANF1 (123, "1111011", "%w8b");
84 CHECK_SCANF1 (19, "023", "%w8o");
85 CHECK_SCANF1 (50, "50", "%w8u");
86 CHECK_SCANF1 (65, "41", "%w8x");
87 CHECK_SCANF1N (65, "41", "%w8x%w8n");
88 CHECK_SCANF1 (66, "42", "%w8X");
92 CHECK_SCANF1 (42, "42", "%w8d");
93 CHECK_SCANF1N (42, "42", "%w8d%w8n");
94 CHECK_SCANF1 (-43, "-43", "%w8d");
95 CHECK_SCANF1 (42, "42", "%w8i");
96 CHECK_SCANF1 (-43, "-43", "%w8i");
97 CHECK_SCANF1 (123, "0b1111011", "%w8i");
98 CHECK_SCANF1 (127, "0x7f", "%w8i");
99 CHECK_SCANF1 (-19, "-023", "%w8i");
104 CHECK_SCANF1 (123, "1111011", "%w8b");
105 CHECK_SCANF1 (19, "023", "%w8o");
106 CHECK_SCANF1 (50, "50", "%w8u");
107 CHECK_SCANF1 (65, "41", "%w8x");
108 CHECK_SCANF1N (65, "41", "%w8x%w8n");
109 CHECK_SCANF1 (66, "42", "%w8X");
118 CHECK_SCANF1 (42, "42", "%wf8d");
119 CHECK_SCANF1N (42, "42", "%wf8d%wf8n");
120 CHECK_SCANF1 (-43, "-43", "%wf8d");
121 CHECK_SCANF1 (42, "42", "%wf8i");
122 CHECK_SCANF1 (-43, "-43", "%wf8i");
123 CHECK_SCANF1 (123, "0b1111011", "%wf8i");
124 CHECK_SCANF1 (127, "0x7f", "%wf8i");
125 CHECK_SCANF1 (-19, "-023", "%wf8i");
130 CHECK_SCANF1 (123, "1111011", "%wf8b");
131 CHECK_SCANF1 (19, "023", "%wf8o");
132 CHECK_SCANF1 (50, "50", "%wf8u");
133 CHECK_SCANF1 (65, "41", "%wf8x");
134 CHECK_SCANF1N (65, "41", "%wf8x%wf8n");
135 CHECK_SCANF1 (66, "42", "%wf8X");
144 CHECK_SCANF1 (12345, "12345", "%w16d");
145 CHECK_SCANF1N (23456, "23456", "%w16d%w16n");
146 CHECK_SCANF1 (-10101, "-10101", "%w16d");
147 CHECK_SCANF1 (30000, "30000", "%w16i");
148 CHECK_SCANF1 (-19876, "-19876", "%w16i");
149 CHECK_SCANF1 (16384, "0b100000000000000", "%w16i");
150 CHECK_SCANF1 (32767, "0x7fff", "%w16i");
151 CHECK_SCANF1 (-16383, "-037777", "%w16i");
156 CHECK_SCANF1 (32767, "111111111111111", "%w16b");
157 CHECK_SCANF1 (4095, "07777", "%w16o");
158 CHECK_SCANF1 (9999, "9999", "%w16u");
159 CHECK_SCANF1 (23456, "5ba0", "%w16x");
160 CHECK_SCANF1N (23456, "5ba0", "%w16x%w16n");
161 CHECK_SCANF1 (23457, "5ba1", "%w16X");
164 int_least16_t var
, n
;
165 CHECK_SCANF1 (12345, "12345", "%w16d");
166 CHECK_SCANF1N (23456, "23456", "%w16d%w16n");
167 CHECK_SCANF1 (-10101, "-10101", "%w16d");
168 CHECK_SCANF1 (30000, "30000", "%w16i");
169 CHECK_SCANF1 (-19876, "-19876", "%w16i");
170 CHECK_SCANF1 (16384, "0b100000000000000", "%w16i");
171 CHECK_SCANF1 (32767, "0x7fff", "%w16i");
172 CHECK_SCANF1 (-16383, "-037777", "%w16i");
177 CHECK_SCANF1 (32767, "111111111111111", "%w16b");
178 CHECK_SCANF1 (4095, "07777", "%w16o");
179 CHECK_SCANF1 (9999, "9999", "%w16u");
180 CHECK_SCANF1 (23456, "5ba0", "%w16x");
181 CHECK_SCANF1N (23456, "5ba0", "%w16x%w16n");
182 CHECK_SCANF1 (23457, "5ba1", "%w16X");
191 CHECK_SCANF1 (12345, "12345", "%wf16d");
192 CHECK_SCANF1N (23456, "23456", "%wf16d%wf16n");
193 CHECK_SCANF1 (-10101, "-10101", "%wf16d");
194 CHECK_SCANF1 (30000, "30000", "%wf16i");
195 CHECK_SCANF1 (-19876, "-19876", "%wf16i");
196 CHECK_SCANF1 (16384, "0b100000000000000", "%wf16i");
197 CHECK_SCANF1 (32767, "0x7fff", "%wf16i");
198 CHECK_SCANF1 (-16383, "-037777", "%wf16i");
203 CHECK_SCANF1 (32767, "111111111111111", "%wf16b");
204 CHECK_SCANF1 (4095, "07777", "%wf16o");
205 CHECK_SCANF1 (9999, "9999", "%wf16u");
206 CHECK_SCANF1 (23456, "5ba0", "%wf16x");
207 CHECK_SCANF1N (23456, "5ba0", "%wf16x%wf16n");
208 CHECK_SCANF1 (23457, "5ba1", "%wf16X");
210 #if INT_FAST16_MAX >= INT32_MAX
213 CHECK_SCANF1 (1234567, "1234567", "%wf16d");
214 CHECK_SCANF1N (2345678, "2345678", "%wf16d%wf16n");
215 CHECK_SCANF1 (-1010101, "-1010101", "%wf16d");
216 CHECK_SCANF1 (3000000, "3000000", "%wf16i");
217 CHECK_SCANF1 (-98765432, "-98765432", "%wf16i");
218 CHECK_SCANF1 (1048576, "0b100000000000000000000", "%wf16i");
219 CHECK_SCANF1 (1048575, "0xfffff", "%wf16i");
220 CHECK_SCANF1 (-1048575, "-03777777", "%wf16i");
225 CHECK_SCANF1 (1234567, "100101101011010000111", "%wf16b");
226 CHECK_SCANF1 (1048575, "03777777", "%wf16o");
227 CHECK_SCANF1 (999999, "999999", "%wf16u");
228 CHECK_SCANF1 (987654, "f1206", "%wf16x");
229 CHECK_SCANF1N (987654, "f1206", "%wf16x%wf16n");
230 CHECK_SCANF1 (987655, "f1207", "%wf16X");
233 #if INT_FAST16_MAX >= INT64_MAX
236 CHECK_SCANF1 (123456789012LL, "123456789012", "%wf16d");
237 CHECK_SCANF1N (234567890123LL, "234567890123", "%wf16d%wf16n");
238 CHECK_SCANF1 (-10101010101LL, "-10101010101", "%wf16d");
239 CHECK_SCANF1 (3000000000000000LL, "3000000000000000", "%wf16i");
240 CHECK_SCANF1 (-9876543210LL, "-9876543210", "%wf16i");
241 CHECK_SCANF1 (1LL << 40, "0b10000000000000000000000000000000000000000",
243 CHECK_SCANF1 (1LL << 41, "0x20000000000", "%wf16i");
244 CHECK_SCANF1 (-(1LL << 42), "-0100000000000000", "%wf16i");
249 CHECK_SCANF1 (123456789012ULL, "1110010111110100110010001101000010100",
251 CHECK_SCANF1 (1ULL << 40, "20000000000000", "%wf16o");
252 CHECK_SCANF1 (999999999999ULL, "999999999999", "%wf16u");
253 CHECK_SCANF1 (9876543210ULL, "24cb016ea", "%wf16x");
254 CHECK_SCANF1N (9876543210ULL, "24cb016ea", "%wf16x%wf16n");
255 CHECK_SCANF1 (9876543211ULL, "24cb016eb", "%wf16X");
265 CHECK_SCANF1 (1234567, "1234567", "%w32d");
266 CHECK_SCANF1N (2345678, "2345678", "%w32d%w32n");
267 CHECK_SCANF1 (-1010101, "-1010101", "%w32d");
268 CHECK_SCANF1 (3000000, "3000000", "%w32i");
269 CHECK_SCANF1 (-98765432, "-98765432", "%w32i");
270 CHECK_SCANF1 (1048576, "0b100000000000000000000", "%w32i");
271 CHECK_SCANF1 (1048575, "0xfffff", "%w32i");
272 CHECK_SCANF1 (-1048575, "-03777777", "%w32i");
277 CHECK_SCANF1 (1234567, "100101101011010000111", "%w32b");
278 CHECK_SCANF1 (1048575, "03777777", "%w32o");
279 CHECK_SCANF1 (999999, "999999", "%w32u");
280 CHECK_SCANF1 (987654, "f1206", "%w32x");
281 CHECK_SCANF1N (987654, "f1206", "%w32x%w32n");
282 CHECK_SCANF1 (987655, "f1207", "%w32X");
285 int_least32_t var
, n
;
286 CHECK_SCANF1 (1234567, "1234567", "%w32d");
287 CHECK_SCANF1N (2345678, "2345678", "%w32d%w32n");
288 CHECK_SCANF1 (-1010101, "-1010101", "%w32d");
289 CHECK_SCANF1 (3000000, "3000000", "%w32i");
290 CHECK_SCANF1 (-98765432, "-98765432", "%w32i");
291 CHECK_SCANF1 (1048576, "0b100000000000000000000", "%w32i");
292 CHECK_SCANF1 (1048575, "0xfffff", "%w32i");
293 CHECK_SCANF1 (-1048575, "-03777777", "%w32i");
298 CHECK_SCANF1 (1234567, "100101101011010000111", "%w32b");
299 CHECK_SCANF1 (1048575, "03777777", "%w32o");
300 CHECK_SCANF1 (999999, "999999", "%w32u");
301 CHECK_SCANF1 (987654, "f1206", "%w32x");
302 CHECK_SCANF1N (987654, "f1206", "%w32x%w32n");
303 CHECK_SCANF1 (987655, "f1207", "%w32X");
312 CHECK_SCANF1 (1234567, "1234567", "%wf32d");
313 CHECK_SCANF1N (2345678, "2345678", "%wf32d%wf32n");
314 CHECK_SCANF1 (-1010101, "-1010101", "%wf32d");
315 CHECK_SCANF1 (3000000, "3000000", "%wf32i");
316 CHECK_SCANF1 (-98765432, "-98765432", "%wf32i");
317 CHECK_SCANF1 (1048576, "0b100000000000000000000", "%wf32i");
318 CHECK_SCANF1 (1048575, "0xfffff", "%wf32i");
319 CHECK_SCANF1 (-1048575, "-03777777", "%wf32i");
324 CHECK_SCANF1 (1234567, "100101101011010000111", "%wf32b");
325 CHECK_SCANF1 (1048575, "03777777", "%wf32o");
326 CHECK_SCANF1 (999999, "999999", "%wf32u");
327 CHECK_SCANF1 (987654, "f1206", "%wf32x");
328 CHECK_SCANF1N (987654, "f1206", "%wf32x%wf32n");
329 CHECK_SCANF1 (987655, "f1207", "%wf32X");
331 #if INT_FAST32_MAX >= INT64_MAX
334 CHECK_SCANF1 (123456789012LL, "123456789012", "%wf32d");
335 CHECK_SCANF1N (234567890123LL, "234567890123", "%wf32d%wf32n");
336 CHECK_SCANF1 (-10101010101LL, "-10101010101", "%wf32d");
337 CHECK_SCANF1 (3000000000000000LL, "3000000000000000", "%wf32i");
338 CHECK_SCANF1 (-9876543210LL, "-9876543210", "%wf32i");
339 CHECK_SCANF1 (1LL << 40, "0b10000000000000000000000000000000000000000",
341 CHECK_SCANF1 (1LL << 41, "0x20000000000", "%wf32i");
342 CHECK_SCANF1 (-(1LL << 42), "-0100000000000000", "%wf32i");
347 CHECK_SCANF1 (123456789012ULL, "1110010111110100110010001101000010100",
349 CHECK_SCANF1 (1ULL << 40, "20000000000000", "%wf32o");
350 CHECK_SCANF1 (999999999999ULL, "999999999999", "%wf32u");
351 CHECK_SCANF1 (9876543210ULL, "24cb016ea", "%wf32x");
352 CHECK_SCANF1N (9876543210ULL, "24cb016ea", "%wf32x%wf32n");
353 CHECK_SCANF1 (9876543211ULL, "24cb016eb", "%wf32X");
363 CHECK_SCANF1 (123456789012LL, "123456789012", "%w64d");
364 CHECK_SCANF1N (234567890123LL, "234567890123", "%w64d%w64n");
365 CHECK_SCANF1 (-10101010101LL, "-10101010101", "%w64d");
366 CHECK_SCANF1 (3000000000000000LL, "3000000000000000", "%w64i");
367 CHECK_SCANF1 (-9876543210LL, "-9876543210", "%w64i");
368 CHECK_SCANF1 (1LL << 40, "0b10000000000000000000000000000000000000000",
370 CHECK_SCANF1 (1LL << 41, "0x20000000000", "%w64i");
371 CHECK_SCANF1 (-(1LL << 42), "-0100000000000000", "%w64i");
376 CHECK_SCANF1 (123456789012ULL, "1110010111110100110010001101000010100",
378 CHECK_SCANF1 (1ULL << 40, "20000000000000", "%w64o");
379 CHECK_SCANF1 (999999999999ULL, "999999999999", "%w64u");
380 CHECK_SCANF1 (9876543210ULL, "24cb016ea", "%w64x");
381 CHECK_SCANF1N (9876543210ULL, "24cb016ea", "%w64x%w64n");
382 CHECK_SCANF1 (9876543211ULL, "24cb016eb", "%w64X");
385 int_least64_t var
, n
;
386 CHECK_SCANF1 (123456789012LL, "123456789012", "%w64d");
387 CHECK_SCANF1N (234567890123LL, "234567890123", "%w64d%w64n");
388 CHECK_SCANF1 (-10101010101LL, "-10101010101", "%w64d");
389 CHECK_SCANF1 (3000000000000000LL, "3000000000000000", "%w64i");
390 CHECK_SCANF1 (-9876543210LL, "-9876543210", "%w64i");
391 CHECK_SCANF1 (1LL << 40, "0b10000000000000000000000000000000000000000",
393 CHECK_SCANF1 (1LL << 41, "0x20000000000", "%w64i");
394 CHECK_SCANF1 (-(1LL << 42), "-0100000000000000", "%w64i");
399 CHECK_SCANF1 (123456789012ULL, "1110010111110100110010001101000010100",
401 CHECK_SCANF1 (1ULL << 40, "20000000000000", "%w64o");
402 CHECK_SCANF1 (999999999999ULL, "999999999999", "%w64u");
403 CHECK_SCANF1 (9876543210ULL, "24cb016ea", "%w64x");
404 CHECK_SCANF1N (9876543210ULL, "24cb016ea", "%w64x%w64n");
405 CHECK_SCANF1 (9876543211ULL, "24cb016eb", "%w64X");
414 CHECK_SCANF1 (123456789012LL, "123456789012", "%wf64d");
415 CHECK_SCANF1N (234567890123LL, "234567890123", "%wf64d%wf64n");
416 CHECK_SCANF1 (-10101010101LL, "-10101010101", "%wf64d");
417 CHECK_SCANF1 (3000000000000000LL, "3000000000000000", "%wf64i");
418 CHECK_SCANF1 (-9876543210LL, "-9876543210", "%wf64i");
419 CHECK_SCANF1 (1LL << 40, "0b10000000000000000000000000000000000000000",
421 CHECK_SCANF1 (1LL << 41, "0x20000000000", "%wf64i");
422 CHECK_SCANF1 (-(1LL << 42), "-0100000000000000", "%wf64i");
427 CHECK_SCANF1 (123456789012ULL, "1110010111110100110010001101000010100",
429 CHECK_SCANF1 (1ULL << 40, "20000000000000", "%wf64o");
430 CHECK_SCANF1 (999999999999ULL, "999999999999", "%wf64u");
431 CHECK_SCANF1 (9876543210ULL, "24cb016ea", "%wf64x");
432 CHECK_SCANF1N (9876543210ULL, "24cb016ea", "%wf64x%wf64n");
433 CHECK_SCANF1 (9876543211ULL, "24cb016eb", "%wf64X");
449 /* Bad N in %wN and %wfN are required to produce an error return (of
450 the number of input items assigned) from scanf functions (and can
451 also be seen to be invalid at compile time). */
452 DIAG_PUSH_NEEDS_COMMENT
;
453 DIAG_IGNORE_NEEDS_COMMENT (13, "-Wformat");
454 DIAG_IGNORE_NEEDS_COMMENT (13, "-Wformat-extra-args");
455 CHECK_SCANF_ERR (0, "1", "%w1d", &a
);
456 CHECK_SCANF_ERR (0, "1", "%w123d", &a
);
457 CHECK_SCANF_ERR (0, "1", "%w99999999999999999999d", &a
);
458 CHECK_SCANF_ERR (0, "1", "%wf1d", &a
);
459 CHECK_SCANF_ERR (0, "1", "%wf123d", &a
);
460 CHECK_SCANF_ERR (0, "1", "%wf99999999999999999999d", &a
);
461 CHECK_SCANF_ERR (1, "1 1", "%d %w1d", &a
, &b
);
462 CHECK_SCANF_ERR (1, "1 1", "%d %w123d", &a
, &b
);
463 CHECK_SCANF_ERR (1, "1 1", "%d %w99999999999999999999d", &a
, &b
);
464 CHECK_SCANF_ERR (1, "1 1", "%d %wf1d", &a
, &b
);
465 CHECK_SCANF_ERR (1, "1 1", "%d %wf123d", &a
, &b
);
466 CHECK_SCANF_ERR (1, "1 1", "%d %wf99999999999999999999d", &a
, &b
);
467 CHECK_SCANF_ERR (0, "1", "%1$w1d", &a
);
468 CHECK_SCANF_ERR (0, "1", "%1$w123d", &a
);
469 CHECK_SCANF_ERR (0, "1", "%1$w99999999999999999999d", &a
);
470 CHECK_SCANF_ERR (0, "1", "%1$wf1d", &a
);
471 CHECK_SCANF_ERR (0, "1", "%1$wf123d", &a
);
472 CHECK_SCANF_ERR (0, "1", "%1$wf99999999999999999999d", &a
);
473 DIAG_POP_NEEDS_COMMENT
;
477 DIAG_POP_NEEDS_COMMENT
;
479 #include <support/test-driver.c>