Update syscall lists for Linux 6.6
[glibc.git] / stdio-common / tst-printf-intn-main.c
blobbd70e9de739b4a1337e29e3bf03c6a286def717b
1 /* Test printf formats for intN_t, int_leastN_t and int_fastN_t types.
2 Copyright (C) 2023 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 <inttypes.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <wchar.h>
26 #include <libc-diag.h>
27 #include <support/check.h>
29 /* GCC does not know the %wN or %wfN length modifiers before GCC 13. */
30 DIAG_PUSH_NEEDS_COMMENT;
31 #if !__GNUC_PREREQ (13, 0)
32 DIAG_IGNORE_NEEDS_COMMENT (12, "-Wformat");
33 DIAG_IGNORE_NEEDS_COMMENT (12, "-Wformat-extra-args");
34 #endif
36 #define CHECK_PRINTF(EXPECTED, FMT, ...) \
37 do \
38 { \
39 int ret = SNPRINTF (buf, sizeof buf / sizeof buf[0], L_(FMT), \
40 __VA_ARGS__); \
41 TEST_COMPARE_STRING_MACRO (buf, L_(EXPECTED)); \
42 TEST_COMPARE (ret, STRLEN (L_(EXPECTED))); \
43 } \
44 while (0)
46 #define CHECK_PRINTF_ERR(FMT, ...) \
47 do \
48 { \
49 int ret = SNPRINTF (buf, sizeof buf / sizeof buf[0], L_(FMT), \
50 __VA_ARGS__); \
51 TEST_VERIFY (ret < 0); \
52 TEST_COMPARE (errno, EINVAL); \
53 } \
54 while (0)
56 static void
57 test_w8 (void)
59 CHAR buf[1024];
60 CHECK_PRINTF ("123", "%w8d", (int8_t) 123);
61 CHECK_PRINTF ("-123", "%w8d", (int8_t) -123);
62 CHECK_PRINTF ("123", "%w8i", (int8_t) 123);
63 CHECK_PRINTF ("-123", "%w8i", (int8_t) -123);
64 CHECK_PRINTF ("1111011", "%w8b", (uint8_t) 123);
65 CHECK_PRINTF ("1111011", "%w8B", (uint8_t) 123);
66 CHECK_PRINTF ("173", "%w8o", (uint8_t) 123);
67 CHECK_PRINTF ("123", "%w8u", (uint8_t) 123);
68 CHECK_PRINTF ("7b", "%w8x", (uint8_t) 123);
69 CHECK_PRINTF ("7B", "%w8X", (uint8_t) 123);
70 CHECK_PRINTF (" 123", "%5w8d", (int8_t) 123);
71 CHECK_PRINTF (" 123", "%*w8d", 5, (int8_t) 123);
72 CHECK_PRINTF ("0x7b", "%#w8x", (uint8_t) 123);
73 CHECK_PRINTF ("00123", "%.5w8d", (int8_t) 123);
74 CHECK_PRINTF ("00123", "%.*w8d", 5, (int8_t) 123);
75 CHECK_PRINTF (" 00123", "%8.5w8d", (int8_t) 123);
76 CHECK_PRINTF (" 00123", "%*.5w8d", 8, (int8_t) 123);
77 CHECK_PRINTF (" 00123", "%8.*w8d", 5, (int8_t) 123);
78 CHECK_PRINTF (" 00123", "%*.*w8d", 8, 5, (int8_t) 123);
79 CHECK_PRINTF ("00123 ", "%-8.5w8d", (int8_t) 123);
80 CHECK_PRINTF ("00123 ", "%-*.5w8d", 8, (int8_t) 123);
81 CHECK_PRINTF ("00123 ", "%-8.*w8d", 5, (int8_t) 123);
82 CHECK_PRINTF ("00123 ", "%-*.*w8d", 8, 5, (int8_t) 123);
84 int8_t n = -1;
85 CHECK_PRINTF ("12345", "%d%w8n", 12345, &n);
86 TEST_COMPARE (n, 5);
88 CHECK_PRINTF ("123", "%w8d", (int_least8_t) 123);
89 CHECK_PRINTF ("-123", "%w8d", (int_least8_t) -123);
90 CHECK_PRINTF ("123", "%w8i", (int_least8_t) 123);
91 CHECK_PRINTF ("-123", "%w8i", (int_least8_t) -123);
92 CHECK_PRINTF ("1111011", "%w8b", (uint_least8_t) 123);
93 CHECK_PRINTF ("1111011", "%w8B", (uint_least8_t) 123);
94 CHECK_PRINTF ("173", "%w8o", (uint_least8_t) 123);
95 CHECK_PRINTF ("123", "%w8u", (uint_least8_t) 123);
96 CHECK_PRINTF ("7b", "%w8x", (uint_least8_t) 123);
97 CHECK_PRINTF ("7B", "%w8X", (uint_least8_t) 123);
98 CHECK_PRINTF (" 123", "%5w8d", (int_least8_t) 123);
99 CHECK_PRINTF (" 123", "%*w8d", 5, (int_least8_t) 123);
100 CHECK_PRINTF ("0x7b", "%#w8x", (uint_least8_t) 123);
101 CHECK_PRINTF ("00123", "%.5w8d", (int_least8_t) 123);
102 CHECK_PRINTF ("00123", "%.*w8d", 5, (int_least8_t) 123);
103 CHECK_PRINTF (" 00123", "%8.5w8d", (int_least8_t) 123);
104 CHECK_PRINTF (" 00123", "%*.5w8d", 8, (int_least8_t) 123);
105 CHECK_PRINTF (" 00123", "%8.*w8d", 5, (int_least8_t) 123);
106 CHECK_PRINTF (" 00123", "%*.*w8d", 8, 5, (int_least8_t) 123);
107 CHECK_PRINTF ("00123 ", "%-8.5w8d", (int_least8_t) 123);
108 CHECK_PRINTF ("00123 ", "%-*.5w8d", 8, (int_least8_t) 123);
109 CHECK_PRINTF ("00123 ", "%-8.*w8d", 5, (int_least8_t) 123);
110 CHECK_PRINTF ("00123 ", "%-*.*w8d", 8, 5, (int_least8_t) 123);
112 int_least8_t ln = -1;
113 CHECK_PRINTF ("12345", "%d%w8n", 12345, &ln);
114 TEST_COMPARE (ln, 5);
116 /* Test truncation of value in promoted type not representable in
117 narrower type. */
118 CHECK_PRINTF ("57", "%w8d", 12345);
119 CHECK_PRINTF ("-57", "%w8d", -12345);
120 CHECK_PRINTF ("-121", "%w8d", 1234567);
121 CHECK_PRINTF ("121", "%w8d", -1234567);
122 CHECK_PRINTF ("135", "%w8u", 1234567);
123 /* Test positional argument handling. */
124 CHECK_PRINTF ("test 10 test2 20", "%4$s %3$w8d %2$s %1$w8d",
125 276, "test2", 266, "test");
128 static void
129 test_wf8 (void)
131 CHAR buf[1024];
132 _Static_assert (sizeof (int_fast8_t) == sizeof (char),
133 "test assumes size of int_fast8_t");
134 CHECK_PRINTF ("123", "%wf8d", (int_fast8_t) 123);
135 CHECK_PRINTF ("-123", "%wf8d", (int_fast8_t) -123);
136 CHECK_PRINTF ("123", "%wf8i", (int_fast8_t) 123);
137 CHECK_PRINTF ("-123", "%wf8i", (int_fast8_t) -123);
138 CHECK_PRINTF ("1111011", "%wf8b", (uint_fast8_t) 123);
139 CHECK_PRINTF ("1111011", "%wf8B", (uint_fast8_t) 123);
140 CHECK_PRINTF ("173", "%wf8o", (uint_fast8_t) 123);
141 CHECK_PRINTF ("123", "%wf8u", (uint_fast8_t) 123);
142 CHECK_PRINTF ("7b", "%wf8x", (uint_fast8_t) 123);
143 CHECK_PRINTF ("7B", "%wf8X", (uint_fast8_t) 123);
144 CHECK_PRINTF (" 123", "%5w8d", (int_fast8_t) 123);
145 CHECK_PRINTF (" 123", "%*w8d", 5, (int_fast8_t) 123);
146 CHECK_PRINTF ("0x7b", "%#w8x", (uint_fast8_t) 123);
147 CHECK_PRINTF ("00123", "%.5w8d", (int_fast8_t) 123);
148 CHECK_PRINTF ("00123", "%.*w8d", 5, (int_fast8_t) 123);
149 CHECK_PRINTF (" 00123", "%8.5w8d", (int_fast8_t) 123);
150 CHECK_PRINTF (" 00123", "%*.5w8d", 8, (int_fast8_t) 123);
151 CHECK_PRINTF (" 00123", "%8.*w8d", 5, (int_fast8_t) 123);
152 CHECK_PRINTF (" 00123", "%*.*w8d", 8, 5, (int_fast8_t) 123);
153 CHECK_PRINTF ("00123 ", "%-8.5w8d", (int_fast8_t) 123);
154 CHECK_PRINTF ("00123 ", "%-*.5w8d", 8, (int_fast8_t) 123);
155 CHECK_PRINTF ("00123 ", "%-8.*w8d", 5, (int_fast8_t) 123);
156 CHECK_PRINTF ("00123 ", "%-*.*w8d", 8, 5, (int_fast8_t) 123);
158 int_fast8_t n = -1;
159 CHECK_PRINTF ("12345", "%d%wf8n", 12345, &n);
160 TEST_COMPARE (n, 5);
162 /* Test truncation of value in promoted type not representable in
163 narrower type. */
164 CHECK_PRINTF ("57", "%wf8d", 12345);
165 CHECK_PRINTF ("-57", "%wf8d", -12345);
166 CHECK_PRINTF ("-121", "%wf8d", 1234567);
167 CHECK_PRINTF ("121", "%wf8d", -1234567);
168 CHECK_PRINTF ("135", "%wf8u", 1234567);
169 /* Test positional argument handling. */
170 CHECK_PRINTF ("test 10 test2 20", "%4$s %3$wf8d %2$s %1$wf8d",
171 276, "test2", 266, "test");
174 static void
175 test_w16 (void)
177 CHAR buf[1024];
178 CHECK_PRINTF ("12345", "%w16d", (int16_t) 12345);
179 CHECK_PRINTF ("-12345", "%w16d", (int16_t) -12345);
180 CHECK_PRINTF ("12345", "%w16i", (int16_t) 12345);
181 CHECK_PRINTF ("-12345", "%w16i", (int16_t) -12345);
182 CHECK_PRINTF ("11000000111001", "%w16b", (uint16_t) 12345);
183 CHECK_PRINTF ("11000000111001", "%w16B", (uint16_t) 12345);
184 CHECK_PRINTF ("30071", "%w16o", (uint16_t) 12345);
185 CHECK_PRINTF ("12345", "%w16u", (uint16_t) 12345);
186 CHECK_PRINTF ("303a", "%w16x", (uint16_t) 12346);
187 CHECK_PRINTF ("303A", "%w16X", (uint16_t) 12346);
188 CHECK_PRINTF (" 12345", "%7w16d", (int16_t) 12345);
189 CHECK_PRINTF (" 12345", "%*w16d", 7, (int16_t) 12345);
190 CHECK_PRINTF ("0x3039", "%#w16x", (uint16_t) 12345);
191 CHECK_PRINTF ("0012345", "%.7w16d", (int16_t) 12345);
192 CHECK_PRINTF ("0012345", "%.*w16d", 7, (int16_t) 12345);
193 CHECK_PRINTF (" 0012345", "%10.7w16d", (int16_t) 12345);
194 CHECK_PRINTF (" 0012345", "%*.7w16d", 10, (int16_t) 12345);
195 CHECK_PRINTF (" 0012345", "%10.*w16d", 7, (int16_t) 12345);
196 CHECK_PRINTF (" 0012345", "%*.*w16d", 10, 7, (int16_t) 12345);
197 CHECK_PRINTF ("0012345 ", "%-10.7w16d", (int16_t) 12345);
198 CHECK_PRINTF ("0012345 ", "%-*.7w16d", 10, (int16_t) 12345);
199 CHECK_PRINTF ("0012345 ", "%-10.*w16d", 7, (int16_t) 12345);
200 CHECK_PRINTF ("0012345 ", "%-*.*w16d", 10, 7, (int16_t) 12345);
202 int16_t n = -1;
203 CHECK_PRINTF ("12345", "%d%w16n", 12345, &n);
204 TEST_COMPARE (n, 5);
206 CHECK_PRINTF ("12345", "%w16d", (int_least16_t) 12345);
207 CHECK_PRINTF ("-12345", "%w16d", (int_least16_t) -12345);
208 CHECK_PRINTF ("12345", "%w16i", (int_least16_t) 12345);
209 CHECK_PRINTF ("-12345", "%w16i", (int_least16_t) -12345);
210 CHECK_PRINTF ("11000000111001", "%w16b", (uint_least16_t) 12345);
211 CHECK_PRINTF ("11000000111001", "%w16B", (uint_least16_t) 12345);
212 CHECK_PRINTF ("30071", "%w16o", (uint_least16_t) 12345);
213 CHECK_PRINTF ("12345", "%w16u", (uint_least16_t) 12345);
214 CHECK_PRINTF ("303a", "%w16x", (uint_least16_t) 12346);
215 CHECK_PRINTF ("303A", "%w16X", (uint_least16_t) 12346);
216 CHECK_PRINTF (" 12345", "%7w16d", (int_least16_t) 12345);
217 CHECK_PRINTF (" 12345", "%*w16d", 7, (int_least16_t) 12345);
218 CHECK_PRINTF ("0x3039", "%#w16x", (uint_least16_t) 12345);
219 CHECK_PRINTF ("0012345", "%.7w16d", (int_least16_t) 12345);
220 CHECK_PRINTF ("0012345", "%.*w16d", 7, (int_least16_t) 12345);
221 CHECK_PRINTF (" 0012345", "%10.7w16d", (int_least16_t) 12345);
222 CHECK_PRINTF (" 0012345", "%*.7w16d", 10, (int_least16_t) 12345);
223 CHECK_PRINTF (" 0012345", "%10.*w16d", 7, (int_least16_t) 12345);
224 CHECK_PRINTF (" 0012345", "%*.*w16d", 10, 7, (int_least16_t) 12345);
225 CHECK_PRINTF ("0012345 ", "%-10.7w16d", (int_least16_t) 12345);
226 CHECK_PRINTF ("0012345 ", "%-*.7w16d", 10, (int_least16_t) 12345);
227 CHECK_PRINTF ("0012345 ", "%-10.*w16d", 7, (int_least16_t) 12345);
228 CHECK_PRINTF ("0012345 ", "%-*.*w16d", 10, 7, (int_least16_t) 12345);
230 int_least16_t ln = -1;
231 CHECK_PRINTF ("12345", "%d%w16n", 12345, &ln);
232 TEST_COMPARE (ln, 5);
234 /* Test truncation of value in promoted type not representable in
235 narrower type. */
236 CHECK_PRINTF ("4464", "%w16d", 70000);
237 CHECK_PRINTF ("-4464", "%w16d", -70000);
238 CHECK_PRINTF ("-7616", "%w16d", 123456);
239 CHECK_PRINTF ("7616", "%w16d", -123456);
240 CHECK_PRINTF ("57920", "%w16u", 123456);
241 /* Test positional argument handling. */
242 CHECK_PRINTF ("test 10 test2 20", "%4$s %3$w16d %2$s %1$w16d",
243 65556, "test2", 65546, "test");
246 static void
247 test_wf16 (void)
249 CHAR buf[1024];
250 _Static_assert (sizeof (int_fast16_t) == sizeof (long int),
251 "test assumes size of int_fast16_t");
252 CHECK_PRINTF ("1234567", "%wf16d", (int_fast16_t) 1234567);
253 CHECK_PRINTF ("-1234567", "%wf16d", (int_fast16_t) -1234567);
254 CHECK_PRINTF ("1234567", "%wf16i", (int_fast16_t) 1234567);
255 CHECK_PRINTF ("-1234567", "%wf16i", (int_fast16_t) -1234567);
256 CHECK_PRINTF ("100101101011010000111", "%wf16b", (uint_fast16_t) 1234567);
257 CHECK_PRINTF ("100101101011010000111", "%wf16B", (uint_fast16_t) 1234567);
258 CHECK_PRINTF ("4553207", "%wf16o", (uint_fast16_t) 1234567);
259 CHECK_PRINTF ("1234567", "%wf16u", (uint_fast16_t) 1234567);
260 CHECK_PRINTF ("12d687", "%wf16x", (uint_fast16_t) 1234567);
261 CHECK_PRINTF ("12D687", "%wf16X", (uint_fast16_t) 1234567);
262 CHECK_PRINTF (" 1234567", "%9wf16d", (int_fast16_t) 1234567);
263 CHECK_PRINTF (" 1234567", "%*wf16d", 9, (int_fast16_t) 1234567);
264 CHECK_PRINTF ("0x12d687", "%#wf16x", (uint_fast16_t) 1234567);
265 CHECK_PRINTF ("001234567", "%.9wf16d", (int_fast16_t) 1234567);
266 CHECK_PRINTF ("001234567", "%.*wf16d", 9, (int_fast16_t) 1234567);
267 CHECK_PRINTF (" 001234567", "%12.9wf16d", (int_fast16_t) 1234567);
268 CHECK_PRINTF (" 001234567", "%*.9wf16d", 12, (int_fast16_t) 1234567);
269 CHECK_PRINTF (" 001234567", "%12.*wf16d", 9, (int_fast16_t) 1234567);
270 CHECK_PRINTF (" 001234567", "%*.*wf16d", 12, 9, (int_fast16_t) 1234567);
271 CHECK_PRINTF ("001234567 ", "%-12.9wf16d", (int_fast16_t) 1234567);
272 CHECK_PRINTF ("001234567 ", "%-*.9wf16d", 12, (int_fast16_t) 1234567);
273 CHECK_PRINTF ("001234567 ", "%-12.*wf16d", 9, (int_fast16_t) 1234567);
274 CHECK_PRINTF ("001234567 ", "%-*.*wf16d", 12, 9, (int_fast16_t) 1234567);
276 int_fast16_t n = -1;
277 CHECK_PRINTF ("12345", "%d%wf16n", 12345, &n);
278 TEST_COMPARE (n, 5);
280 /* Test positional argument handling. */
281 CHECK_PRINTF ("test 123456 test2 234567", "%4$s %3$wf16d %2$s %1$wf16d",
282 (int_fast16_t) 234567, "test2", (int_fast16_t) 123456, "test");
283 #if INT_FAST16_MAX > 0x7fffffff
284 CHECK_PRINTF ("12345678901", "%wf16d", (int_fast16_t) 12345678901LL);
285 CHECK_PRINTF ("-12345678901", "%wf16d", (int_fast16_t) -12345678901LL);
286 CHECK_PRINTF ("12345678901", "%wf16i", (int_fast16_t) 12345678901LL);
287 CHECK_PRINTF ("-12345678901", "%wf16i", (int_fast16_t) -12345678901LL);
288 CHECK_PRINTF ("1011011111110111000001110000110101", "%wf16b",
289 (uint_fast16_t) 12345678901LL);
290 CHECK_PRINTF ("1011011111110111000001110000110101", "%wf16B",
291 (uint_fast16_t) 12345678901LL);
292 CHECK_PRINTF ("133767016065", "%wf16o", (uint_fast16_t) 12345678901LL);
293 CHECK_PRINTF ("12345678901", "%wf16u", (uint_fast16_t) 12345678901LL);
294 CHECK_PRINTF ("2dfdc1c35", "%wf16x", (uint_fast16_t) 12345678901LL);
295 CHECK_PRINTF ("2DFDC1C35", "%wf16X", (uint_fast16_t) 12345678901LL);
296 CHECK_PRINTF (" 12345678901", "%13wf16d", (int_fast16_t) 12345678901LL);
297 CHECK_PRINTF (" 12345678901", "%*wf16d", 13, (int_fast16_t) 12345678901LL);
298 CHECK_PRINTF ("0x2dfdc1c35", "%#wf16x", (uint_fast16_t) 12345678901LL);
299 CHECK_PRINTF ("0012345678901", "%.13wf16d", (int_fast16_t) 12345678901LL);
300 CHECK_PRINTF ("0012345678901", "%.*wf16d", 13, (int_fast16_t) 12345678901LL);
301 CHECK_PRINTF (" 0012345678901", "%16.13wf16d",
302 (int_fast16_t) 12345678901LL);
303 CHECK_PRINTF (" 0012345678901", "%*.13wf16d", 16,
304 (int_fast16_t) 12345678901LL);
305 CHECK_PRINTF (" 0012345678901", "%16.*wf16d", 13,
306 (int_fast16_t) 12345678901LL);
307 CHECK_PRINTF (" 0012345678901", "%*.*wf16d", 16, 13,
308 (int_fast16_t) 12345678901LL);
309 CHECK_PRINTF ("0012345678901 ", "%-16.13wf16d",
310 (int_fast16_t) 12345678901LL);
311 CHECK_PRINTF ("0012345678901 ", "%-*.13wf16d", 16,
312 (int_fast16_t) 12345678901LL);
313 CHECK_PRINTF ("0012345678901 ", "%-16.*wf16d", 13,
314 (int_fast16_t) 12345678901LL);
315 CHECK_PRINTF ("0012345678901 ", "%-*.*wf16d", 16, 13,
316 (int_fast16_t) 12345678901LL);
317 /* Test positional argument handling. */
318 CHECK_PRINTF ("test 123456789012 test2 234567890123",
319 "%4$s %3$wf16d %2$s %1$wf16d",
320 (int_fast16_t) 234567890123ULL, "test2",
321 (int_fast16_t) 123456789012ULL, "test");
322 #endif
325 static void
326 test_w32 (void)
328 CHAR buf[1024];
329 CHECK_PRINTF ("1234567", "%w32d", (int32_t) 1234567);
330 CHECK_PRINTF ("-1234567", "%w32d", (int32_t) -1234567);
331 CHECK_PRINTF ("1234567", "%w32i", (int32_t) 1234567);
332 CHECK_PRINTF ("-1234567", "%w32i", (int32_t) -1234567);
333 CHECK_PRINTF ("100101101011010000111", "%w32b", (uint32_t) 1234567);
334 CHECK_PRINTF ("100101101011010000111", "%w32B", (uint32_t) 1234567);
335 CHECK_PRINTF ("4553207", "%w32o", (uint32_t) 1234567);
336 CHECK_PRINTF ("1234567", "%w32u", (uint32_t) 1234567);
337 CHECK_PRINTF ("12d687", "%w32x", (uint32_t) 1234567);
338 CHECK_PRINTF ("12D687", "%w32X", (uint32_t) 1234567);
339 CHECK_PRINTF (" 1234567", "%9w32d", (int32_t) 1234567);
340 CHECK_PRINTF (" 1234567", "%*w32d", 9, (int32_t) 1234567);
341 CHECK_PRINTF ("0x12d687", "%#w32x", (uint32_t) 1234567);
342 CHECK_PRINTF ("001234567", "%.9w32d", (int32_t) 1234567);
343 CHECK_PRINTF ("001234567", "%.*w32d", 9, (int32_t) 1234567);
344 CHECK_PRINTF (" 001234567", "%12.9w32d", (int32_t) 1234567);
345 CHECK_PRINTF (" 001234567", "%*.9w32d", 12, (int32_t) 1234567);
346 CHECK_PRINTF (" 001234567", "%12.*w32d", 9, (int32_t) 1234567);
347 CHECK_PRINTF (" 001234567", "%*.*w32d", 12, 9, (int32_t) 1234567);
348 CHECK_PRINTF ("001234567 ", "%-12.9w32d", (int32_t) 1234567);
349 CHECK_PRINTF ("001234567 ", "%-*.9w32d", 12, (int32_t) 1234567);
350 CHECK_PRINTF ("001234567 ", "%-12.*w32d", 9, (int32_t) 1234567);
351 CHECK_PRINTF ("001234567 ", "%-*.*w32d", 12, 9, (int32_t) 1234567);
353 int32_t n = -1;
354 CHECK_PRINTF ("12345", "%d%w32n", 12345, &n);
355 TEST_COMPARE (n, 5);
357 CHECK_PRINTF ("1234567", "%w32d", (int_least32_t) 1234567);
358 CHECK_PRINTF ("-1234567", "%w32d", (int_least32_t) -1234567);
359 CHECK_PRINTF ("1234567", "%w32i", (int_least32_t) 1234567);
360 CHECK_PRINTF ("-1234567", "%w32i", (int_least32_t) -1234567);
361 CHECK_PRINTF ("100101101011010000111", "%w32b", (uint_least32_t) 1234567);
362 CHECK_PRINTF ("100101101011010000111", "%w32B", (uint_least32_t) 1234567);
363 CHECK_PRINTF ("4553207", "%w32o", (uint_least32_t) 1234567);
364 CHECK_PRINTF ("1234567", "%w32u", (uint_least32_t) 1234567);
365 CHECK_PRINTF ("12d687", "%w32x", (uint_least32_t) 1234567);
366 CHECK_PRINTF ("12D687", "%w32X", (uint_least32_t) 1234567);
367 CHECK_PRINTF (" 1234567", "%9w32d", (int_least32_t) 1234567);
368 CHECK_PRINTF (" 1234567", "%*w32d", 9, (int_least32_t) 1234567);
369 CHECK_PRINTF ("0x12d687", "%#w32x", (uint_least32_t) 1234567);
370 CHECK_PRINTF ("001234567", "%.9w32d", (int_least32_t) 1234567);
371 CHECK_PRINTF ("001234567", "%.*w32d", 9, (int_least32_t) 1234567);
372 CHECK_PRINTF (" 001234567", "%12.9w32d", (int_least32_t) 1234567);
373 CHECK_PRINTF (" 001234567", "%*.9w32d", 12, (int_least32_t) 1234567);
374 CHECK_PRINTF (" 001234567", "%12.*w32d", 9, (int_least32_t) 1234567);
375 CHECK_PRINTF (" 001234567", "%*.*w32d", 12, 9, (int_least32_t) 1234567);
376 CHECK_PRINTF ("001234567 ", "%-12.9w32d", (int_least32_t) 1234567);
377 CHECK_PRINTF ("001234567 ", "%-*.9w32d", 12, (int_least32_t) 1234567);
378 CHECK_PRINTF ("001234567 ", "%-12.*w32d", 9, (int_least32_t) 1234567);
379 CHECK_PRINTF ("001234567 ", "%-*.*w32d", 12, 9, (int_least32_t) 1234567);
381 int_least32_t ln = -1;
382 CHECK_PRINTF ("12345", "%d%w32n", 12345, &ln);
383 TEST_COMPARE (ln, 5);
385 /* Test positional argument handling. */
386 CHECK_PRINTF ("test 123456 test2 234567", "%4$s %3$w32d %2$s %1$w32d",
387 INT32_C (234567), "test2", INT32_C (123456), "test");
390 static void
391 test_wf32 (void)
393 CHAR buf[1024];
394 _Static_assert (sizeof (int_fast32_t) == sizeof (long int),
395 "test assumes size of int_fast32_t");
396 CHECK_PRINTF ("1234567", "%wf32d", (int_fast32_t) 1234567);
397 CHECK_PRINTF ("-1234567", "%wf32d", (int_fast32_t) -1234567);
398 CHECK_PRINTF ("1234567", "%wf32i", (int_fast32_t) 1234567);
399 CHECK_PRINTF ("-1234567", "%wf32i", (int_fast32_t) -1234567);
400 CHECK_PRINTF ("100101101011010000111", "%wf32b", (uint_fast32_t) 1234567);
401 CHECK_PRINTF ("100101101011010000111", "%wf32B", (uint_fast32_t) 1234567);
402 CHECK_PRINTF ("4553207", "%wf32o", (uint_fast32_t) 1234567);
403 CHECK_PRINTF ("1234567", "%wf32u", (uint_fast32_t) 1234567);
404 CHECK_PRINTF ("12d687", "%wf32x", (uint_fast32_t) 1234567);
405 CHECK_PRINTF ("12D687", "%wf32X", (uint_fast32_t) 1234567);
406 CHECK_PRINTF (" 1234567", "%9wf32d", (int_fast32_t) 1234567);
407 CHECK_PRINTF (" 1234567", "%*wf32d", 9, (int_fast32_t) 1234567);
408 CHECK_PRINTF ("0x12d687", "%#wf32x", (uint_fast32_t) 1234567);
409 CHECK_PRINTF ("001234567", "%.9wf32d", (int_fast32_t) 1234567);
410 CHECK_PRINTF ("001234567", "%.*wf32d", 9, (int_fast32_t) 1234567);
411 CHECK_PRINTF (" 001234567", "%12.9wf32d", (int_fast32_t) 1234567);
412 CHECK_PRINTF (" 001234567", "%*.9wf32d", 12, (int_fast32_t) 1234567);
413 CHECK_PRINTF (" 001234567", "%12.*wf32d", 9, (int_fast32_t) 1234567);
414 CHECK_PRINTF (" 001234567", "%*.*wf32d", 12, 9, (int_fast32_t) 1234567);
415 CHECK_PRINTF ("001234567 ", "%-12.9wf32d", (int_fast32_t) 1234567);
416 CHECK_PRINTF ("001234567 ", "%-*.9wf32d", 12, (int_fast32_t) 1234567);
417 CHECK_PRINTF ("001234567 ", "%-12.*wf32d", 9, (int_fast32_t) 1234567);
418 CHECK_PRINTF ("001234567 ", "%-*.*wf32d", 12, 9, (int_fast32_t) 1234567);
420 int_fast32_t n = -1;
421 CHECK_PRINTF ("12345", "%d%wf32n", 12345, &n);
422 TEST_COMPARE (n, 5);
424 /* Test positional argument handling. */
425 CHECK_PRINTF ("test 123456 test2 234567", "%4$s %3$wf32d %2$s %1$wf32d",
426 (int_fast32_t) 234567, "test2", (int_fast32_t) 123456, "test");
427 #if INT_FAST32_MAX > 0x7fffffff
428 CHECK_PRINTF ("12345678901", "%wf32d", (int_fast32_t) 12345678901LL);
429 CHECK_PRINTF ("-12345678901", "%wf32d", (int_fast32_t) -12345678901LL);
430 CHECK_PRINTF ("12345678901", "%wf32i", (int_fast32_t) 12345678901LL);
431 CHECK_PRINTF ("-12345678901", "%wf32i", (int_fast32_t) -12345678901LL);
432 CHECK_PRINTF ("1011011111110111000001110000110101", "%wf32b",
433 (uint_fast32_t) 12345678901LL);
434 CHECK_PRINTF ("1011011111110111000001110000110101", "%wf32B",
435 (uint_fast32_t) 12345678901LL);
436 CHECK_PRINTF ("133767016065", "%wf32o", (uint_fast32_t) 12345678901LL);
437 CHECK_PRINTF ("12345678901", "%wf32u", (uint_fast32_t) 12345678901LL);
438 CHECK_PRINTF ("2dfdc1c35", "%wf32x", (uint_fast32_t) 12345678901LL);
439 CHECK_PRINTF ("2DFDC1C35", "%wf32X", (uint_fast32_t) 12345678901LL);
440 CHECK_PRINTF (" 12345678901", "%13wf32d", (int_fast32_t) 12345678901LL);
441 CHECK_PRINTF (" 12345678901", "%*wf32d", 13, (int_fast32_t) 12345678901LL);
442 CHECK_PRINTF ("0x2dfdc1c35", "%#wf32x", (uint_fast32_t) 12345678901LL);
443 CHECK_PRINTF ("0012345678901", "%.13wf32d", (int_fast32_t) 12345678901LL);
444 CHECK_PRINTF ("0012345678901", "%.*wf32d", 13, (int_fast32_t) 12345678901LL);
445 CHECK_PRINTF (" 0012345678901", "%16.13wf32d",
446 (int_fast32_t) 12345678901LL);
447 CHECK_PRINTF (" 0012345678901", "%*.13wf32d", 16,
448 (int_fast32_t) 12345678901LL);
449 CHECK_PRINTF (" 0012345678901", "%16.*wf32d", 13,
450 (int_fast32_t) 12345678901LL);
451 CHECK_PRINTF (" 0012345678901", "%*.*wf32d", 16, 13,
452 (int_fast32_t) 12345678901LL);
453 CHECK_PRINTF ("0012345678901 ", "%-16.13wf32d",
454 (int_fast32_t) 12345678901LL);
455 CHECK_PRINTF ("0012345678901 ", "%-*.13wf32d", 16,
456 (int_fast32_t) 12345678901LL);
457 CHECK_PRINTF ("0012345678901 ", "%-16.*wf32d", 13,
458 (int_fast32_t) 12345678901LL);
459 CHECK_PRINTF ("0012345678901 ", "%-*.*wf32d", 16, 13,
460 (int_fast32_t) 12345678901LL);
461 /* Test positional argument handling. */
462 CHECK_PRINTF ("test 123456789012 test2 234567890123",
463 "%4$s %3$wf32d %2$s %1$wf32d",
464 (int_fast32_t) 234567890123ULL, "test2",
465 (int_fast32_t) 123456789012ULL, "test");
466 #endif
469 static void
470 test_w64 (void)
472 CHAR buf[1024];
473 CHECK_PRINTF ("12345678901", "%w64d", (int64_t) 12345678901LL);
474 CHECK_PRINTF ("-12345678901", "%w64d", (int64_t) -12345678901LL);
475 CHECK_PRINTF ("12345678901", "%w64i", (int64_t) 12345678901LL);
476 CHECK_PRINTF ("-12345678901", "%w64i", (int64_t) -12345678901LL);
477 CHECK_PRINTF ("1011011111110111000001110000110101", "%w64b",
478 (uint64_t) 12345678901LL);
479 CHECK_PRINTF ("1011011111110111000001110000110101", "%w64B",
480 (uint64_t) 12345678901LL);
481 CHECK_PRINTF ("133767016065", "%w64o", (uint64_t) 12345678901LL);
482 CHECK_PRINTF ("12345678901", "%w64u", (uint64_t) 12345678901LL);
483 CHECK_PRINTF ("2dfdc1c35", "%w64x", (uint64_t) 12345678901LL);
484 CHECK_PRINTF ("2DFDC1C35", "%w64X", (uint64_t) 12345678901LL);
485 CHECK_PRINTF (" 12345678901", "%13w64d", (int64_t) 12345678901LL);
486 CHECK_PRINTF (" 12345678901", "%*w64d", 13, (int64_t) 12345678901LL);
487 CHECK_PRINTF ("0x2dfdc1c35", "%#w64x", (uint64_t) 12345678901LL);
488 CHECK_PRINTF ("0012345678901", "%.13w64d", (int64_t) 12345678901LL);
489 CHECK_PRINTF ("0012345678901", "%.*w64d", 13, (int64_t) 12345678901LL);
490 CHECK_PRINTF (" 0012345678901", "%16.13w64d", (int64_t) 12345678901LL);
491 CHECK_PRINTF (" 0012345678901", "%*.13w64d", 16, (int64_t) 12345678901LL);
492 CHECK_PRINTF (" 0012345678901", "%16.*w64d", 13, (int64_t) 12345678901LL);
493 CHECK_PRINTF (" 0012345678901", "%*.*w64d", 16, 13,
494 (int64_t) 12345678901LL);
495 CHECK_PRINTF ("0012345678901 ", "%-16.13w64d", (int64_t) 12345678901LL);
496 CHECK_PRINTF ("0012345678901 ", "%-*.13w64d", 16, (int64_t) 12345678901LL);
497 CHECK_PRINTF ("0012345678901 ", "%-16.*w64d", 13, (int64_t) 12345678901LL);
498 CHECK_PRINTF ("0012345678901 ", "%-*.*w64d", 16, 13,
499 (int64_t) 12345678901LL);
501 int64_t n = -1;
502 CHECK_PRINTF ("12345", "%d%w64n", 12345, &n);
503 TEST_COMPARE (n, 5);
505 CHECK_PRINTF ("12345678901", "%w64d", (int_least64_t) 12345678901LL);
506 CHECK_PRINTF ("-12345678901", "%w64d", (int_least64_t) -12345678901LL);
507 CHECK_PRINTF ("12345678901", "%w64i", (int_least64_t) 12345678901LL);
508 CHECK_PRINTF ("-12345678901", "%w64i", (int_least64_t) -12345678901LL);
509 CHECK_PRINTF ("1011011111110111000001110000110101", "%w64b",
510 (uint_least64_t) 12345678901LL);
511 CHECK_PRINTF ("1011011111110111000001110000110101", "%w64B",
512 (uint_least64_t) 12345678901LL);
513 CHECK_PRINTF ("133767016065", "%w64o", (uint_least64_t) 12345678901LL);
514 CHECK_PRINTF ("12345678901", "%w64u", (uint_least64_t) 12345678901LL);
515 CHECK_PRINTF ("2dfdc1c35", "%w64x", (uint_least64_t) 12345678901LL);
516 CHECK_PRINTF ("2DFDC1C35", "%w64X", (uint_least64_t) 12345678901LL);
517 CHECK_PRINTF (" 12345678901", "%13w64d", (int_least64_t) 12345678901LL);
518 CHECK_PRINTF (" 12345678901", "%*w64d", 13, (int_least64_t) 12345678901LL);
519 CHECK_PRINTF ("0x2dfdc1c35", "%#w64x", (uint_least64_t) 12345678901LL);
520 CHECK_PRINTF ("0012345678901", "%.13w64d", (int_least64_t) 12345678901LL);
521 CHECK_PRINTF ("0012345678901", "%.*w64d", 13, (int_least64_t) 12345678901LL);
522 CHECK_PRINTF (" 0012345678901", "%16.13w64d",
523 (int_least64_t) 12345678901LL);
524 CHECK_PRINTF (" 0012345678901", "%*.13w64d", 16,
525 (int_least64_t) 12345678901LL);
526 CHECK_PRINTF (" 0012345678901", "%16.*w64d", 13,
527 (int_least64_t) 12345678901LL);
528 CHECK_PRINTF (" 0012345678901", "%*.*w64d", 16, 13,
529 (int_least64_t) 12345678901LL);
530 CHECK_PRINTF ("0012345678901 ", "%-16.13w64d",
531 (int_least64_t) 12345678901LL);
532 CHECK_PRINTF ("0012345678901 ", "%-*.13w64d", 16,
533 (int_least64_t) 12345678901LL);
534 CHECK_PRINTF ("0012345678901 ", "%-16.*w64d", 13,
535 (int_least64_t) 12345678901LL);
536 CHECK_PRINTF ("0012345678901 ", "%-*.*w64d", 16, 13,
537 (int_least64_t) 12345678901LL);
539 int_least64_t ln = -1;
540 CHECK_PRINTF ("12345", "%d%w64n", 12345, &ln);
541 TEST_COMPARE (ln, 5);
543 /* Test positional argument handling. */
544 CHECK_PRINTF ("test 123456789012 test2 234567890123",
545 "%4$s %3$w64d %2$s %1$w64d",
546 INT64_C (234567890123), "test2",
547 INT64_C (123456789012), "test");
550 static void
551 test_wf64 (void)
553 CHAR buf[1024];
554 _Static_assert (sizeof (int_fast64_t) == sizeof (long long int),
555 "test assumes size of int_fast64_t");
556 CHECK_PRINTF ("12345678901", "%wf64d", (int_fast64_t) 12345678901LL);
557 CHECK_PRINTF ("-12345678901", "%wf64d", (int_fast64_t) -12345678901LL);
558 CHECK_PRINTF ("12345678901", "%wf64i", (int_fast64_t) 12345678901LL);
559 CHECK_PRINTF ("-12345678901", "%wf64i", (int_fast64_t) -12345678901LL);
560 CHECK_PRINTF ("1011011111110111000001110000110101", "%wf64b",
561 (uint_fast64_t) 12345678901LL);
562 CHECK_PRINTF ("1011011111110111000001110000110101", "%wf64B",
563 (uint_fast64_t) 12345678901LL);
564 CHECK_PRINTF ("133767016065", "%wf64o", (uint_fast64_t) 12345678901LL);
565 CHECK_PRINTF ("12345678901", "%wf64u", (uint_fast64_t) 12345678901LL);
566 CHECK_PRINTF ("2dfdc1c35", "%wf64x", (uint_fast64_t) 12345678901LL);
567 CHECK_PRINTF ("2DFDC1C35", "%wf64X", (uint_fast64_t) 12345678901LL);
568 CHECK_PRINTF (" 12345678901", "%13wf64d", (int_fast64_t) 12345678901LL);
569 CHECK_PRINTF (" 12345678901", "%*wf64d", 13, (int_fast64_t) 12345678901LL);
570 CHECK_PRINTF ("0x2dfdc1c35", "%#wf64x", (uint_fast64_t) 12345678901LL);
571 CHECK_PRINTF ("0012345678901", "%.13wf64d", (int_fast64_t) 12345678901LL);
572 CHECK_PRINTF ("0012345678901", "%.*wf64d", 13, (int_fast64_t) 12345678901LL);
573 CHECK_PRINTF (" 0012345678901", "%16.13wf64d",
574 (int_fast64_t) 12345678901LL);
575 CHECK_PRINTF (" 0012345678901", "%*.13wf64d", 16,
576 (int_fast64_t) 12345678901LL);
577 CHECK_PRINTF (" 0012345678901", "%16.*wf64d", 13,
578 (int_fast64_t) 12345678901LL);
579 CHECK_PRINTF (" 0012345678901", "%*.*wf64d", 16, 13,
580 (int_fast64_t) 12345678901LL);
581 CHECK_PRINTF ("0012345678901 ", "%-16.13wf64d",
582 (int_fast64_t) 12345678901LL);
583 CHECK_PRINTF ("0012345678901 ", "%-*.13wf64d", 16,
584 (int_fast64_t) 12345678901LL);
585 CHECK_PRINTF ("0012345678901 ", "%-16.*wf64d", 13,
586 (int_fast64_t) 12345678901LL);
587 CHECK_PRINTF ("0012345678901 ", "%-*.*wf64d", 16, 13,
588 (int_fast64_t) 12345678901LL);
590 int_fast64_t n = -1;
591 CHECK_PRINTF ("12345", "%d%wf64n", 12345, &n);
592 TEST_COMPARE (n, 5);
594 /* Test positional argument handling. */
595 CHECK_PRINTF ("test 123456789012 test2 234567890123",
596 "%4$s %3$wf64d %2$s %1$wf64d",
597 (int_fast64_t) 234567890123ULL, "test2",
598 (int_fast64_t) 123456789012ULL, "test");
601 static int
602 do_test (void)
604 test_w8 ();
605 test_wf8 ();
606 test_w16 ();
607 test_wf16 ();
608 test_w32 ();
609 test_wf32 ();
610 test_w64 ();
611 test_wf64 ();
612 /* Bad N in %wN and %wfN are required to produce an error return
613 from printf functions (and can also be seen to be invalid at
614 compile time). */
615 DIAG_PUSH_NEEDS_COMMENT;
616 DIAG_IGNORE_NEEDS_COMMENT (13, "-Wformat");
617 DIAG_IGNORE_NEEDS_COMMENT (13, "-Wformat-extra-args");
618 CHAR buf[1024];
619 CHECK_PRINTF_ERR ("%w1d", 123);
620 CHECK_PRINTF_ERR ("%w123d", 123);
621 CHECK_PRINTF_ERR ("%w99999999999999999999d", 123);
622 CHECK_PRINTF_ERR ("%wf1d", 123);
623 CHECK_PRINTF_ERR ("%wf123d", 123);
624 CHECK_PRINTF_ERR ("%wf99999999999999999999d", 123);
625 CHECK_PRINTF_ERR ("%1$w1d", 123);
626 CHECK_PRINTF_ERR ("%1$w123d", 123);
627 CHECK_PRINTF_ERR ("%1$w99999999999999999999d", 123);
628 CHECK_PRINTF_ERR ("%1$wf1d", 123);
629 CHECK_PRINTF_ERR ("%1$wf123d", 123);
630 CHECK_PRINTF_ERR ("%1$wf99999999999999999999d", 123);
631 DIAG_POP_NEEDS_COMMENT;
632 return 0;
635 DIAG_POP_NEEDS_COMMENT;
637 #include <support/test-driver.c>