math: Fix isnanf128 static build (BZ 31774)
[glibc.git] / stdio-common / tst-scanf-intn-main.c
blob5964e4246d70e9b80534ea4d16d04d186663361f
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/>. */
19 #include <errno.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <wchar.h>
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");
33 #endif
35 #define CHECK_SCANF1(EXPECTED, STR, FMT) \
36 do \
37 { \
38 var = ((typeof (var)) 0xabababab); \
39 int ret = SSCANF (L_(STR), L_(FMT), &var); \
40 TEST_COMPARE (var, (EXPECTED)); \
41 TEST_COMPARE (ret, 1); \
42 } \
43 while (0)
45 #define CHECK_SCANF1N(EXPECTED, STR, FMT) \
46 do \
47 { \
48 var = ((typeof (var)) 0xabababab); \
49 n = 123; \
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); \
54 } \
55 while (0)
57 #define CHECK_SCANF_ERR(OK, STR, FMT, ...) \
58 do \
59 { \
60 int ret = SSCANF (L_(STR), L_(FMT), __VA_ARGS__); \
61 TEST_VERIFY (ret == (OK)); \
62 TEST_COMPARE (errno, EINVAL); \
63 } \
64 while (0)
66 static void
67 test_w8 (void)
70 int8_t var, n;
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");
81 uint8_t var;
82 int8_t n;
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");
91 int_least8_t var, n;
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");
102 uint_least8_t var;
103 int_least8_t n;
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");
113 static void
114 test_wf8 (void)
117 int_fast8_t var, n;
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");
128 uint_fast8_t var;
129 int_fast8_t n;
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");
139 static void
140 test_w16 (void)
143 int16_t var, n;
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");
154 uint16_t var;
155 int16_t n;
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");
175 uint_least16_t var;
176 int_least16_t n;
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");
186 static void
187 test_wf16 (void)
190 int_fast16_t var, n;
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");
201 uint_fast16_t var;
202 int_fast16_t n;
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
212 int_fast16_t var, n;
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");
223 uint_fast16_t var;
224 int_fast16_t n;
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");
232 #endif
233 #if INT_FAST16_MAX >= INT64_MAX
235 int_fast16_t var, n;
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",
242 "%wf16i");
243 CHECK_SCANF1 (1LL << 41, "0x20000000000", "%wf16i");
244 CHECK_SCANF1 (-(1LL << 42), "-0100000000000000", "%wf16i");
247 uint_fast16_t var;
248 int_fast16_t n;
249 CHECK_SCANF1 (123456789012ULL, "1110010111110100110010001101000010100",
250 "%wf16b");
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");
257 #endif
260 static void
261 test_w32 (void)
264 int32_t var, n;
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");
275 uint32_t var;
276 int32_t n;
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");
296 uint_least32_t var;
297 int_least32_t n;
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");
307 static void
308 test_wf32 (void)
311 int_fast32_t var, n;
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");
322 uint_fast32_t var;
323 int_fast32_t n;
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
333 int_fast32_t var, n;
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",
340 "%wf32i");
341 CHECK_SCANF1 (1LL << 41, "0x20000000000", "%wf32i");
342 CHECK_SCANF1 (-(1LL << 42), "-0100000000000000", "%wf32i");
345 uint_fast32_t var;
346 int_fast32_t n;
347 CHECK_SCANF1 (123456789012ULL, "1110010111110100110010001101000010100",
348 "%wf32b");
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");
355 #endif
358 static void
359 test_w64 (void)
362 int64_t var, n;
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",
369 "%w64i");
370 CHECK_SCANF1 (1LL << 41, "0x20000000000", "%w64i");
371 CHECK_SCANF1 (-(1LL << 42), "-0100000000000000", "%w64i");
374 uint64_t var;
375 int64_t n;
376 CHECK_SCANF1 (123456789012ULL, "1110010111110100110010001101000010100",
377 "%w64b");
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",
392 "%w64i");
393 CHECK_SCANF1 (1LL << 41, "0x20000000000", "%w64i");
394 CHECK_SCANF1 (-(1LL << 42), "-0100000000000000", "%w64i");
397 uint_least64_t var;
398 int_least64_t n;
399 CHECK_SCANF1 (123456789012ULL, "1110010111110100110010001101000010100",
400 "%w64b");
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");
409 static void
410 test_wf64 (void)
413 int_fast64_t var, n;
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",
420 "%wf64i");
421 CHECK_SCANF1 (1LL << 41, "0x20000000000", "%wf64i");
422 CHECK_SCANF1 (-(1LL << 42), "-0100000000000000", "%wf64i");
425 uint_fast64_t var;
426 int_fast64_t n;
427 CHECK_SCANF1 (123456789012ULL, "1110010111110100110010001101000010100",
428 "%wf64b");
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");
437 static int
438 do_test (void)
440 int a, b;
441 test_w8 ();
442 test_wf8 ();
443 test_w16 ();
444 test_wf16 ();
445 test_w32 ();
446 test_wf32 ();
447 test_w64 ();
448 test_wf64 ();
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;
474 return 0;
477 DIAG_POP_NEEDS_COMMENT;
479 #include <support/test-driver.c>