1 /* Test of u32_v[a]s[n]printf() function.
2 Copyright (C) 2007, 2009-2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
20 test_xfunction (uint32_t * (*my_xasprintf) (const char *, ...))
22 /* Test support of size specifiers as in C99. */
26 my_xasprintf ("%ju %d", (uintmax_t) 12345671, 33, 44, 55);
27 static const uint32_t expected[] =
28 { '1', '2', '3', '4', '5', '6', '7', '1', ' ', '3', '3', 0 };
29 ASSERT (result != NULL);
30 ASSERT (u32_strcmp (result, expected) == 0);
36 my_xasprintf ("%zu %d", (size_t) 12345672, 33, 44, 55);
37 static const uint32_t expected[] =
38 { '1', '2', '3', '4', '5', '6', '7', '2', ' ', '3', '3', 0 };
39 ASSERT (result != NULL);
40 ASSERT (u32_strcmp (result, expected) == 0);
46 my_xasprintf ("%tu %d", (ptrdiff_t) 12345673, 33, 44, 55);
47 static const uint32_t expected[] =
48 { '1', '2', '3', '4', '5', '6', '7', '3', ' ', '3', '3', 0 };
49 ASSERT (result != NULL);
50 ASSERT (u32_strcmp (result, expected) == 0);
56 my_xasprintf ("%Lg %d", (long double) 1.5, 33, 44, 55);
57 static const uint32_t expected[] =
58 { '1', '.', '5', ' ', '3', '3', 0 };
59 ASSERT (result != NULL);
60 ASSERT (u32_strcmp (result, expected) == 0);
64 /* Test the support of the 'U' conversion specifier for Unicode strings. */
67 static const uint8_t unicode_string[] = "Hello";
70 my_xasprintf ("%U %d", unicode_string, 33, 44, 55);
71 static const uint32_t expected[] =
72 { 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
73 ASSERT (result != NULL);
74 ASSERT (u32_strcmp (result, expected) == 0);
79 my_xasprintf ("%10U %d", unicode_string, 33, 44, 55);
80 static const uint32_t expected[] =
81 { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
82 ASSERT (result != NULL);
83 ASSERT (u32_strcmp (result, expected) == 0);
88 my_xasprintf ("%-10U %d", unicode_string, 33, 44, 55);
89 static const uint32_t expected[] =
90 { 'H', 'e', 'l', 'l', 'o', ' ', ' ', ' ', ' ', ' ', ' ', '3', '3', 0 };
91 ASSERT (result != NULL);
92 ASSERT (u32_strcmp (result, expected) == 0);
95 { /* FLAG_ZERO: no effect. */
97 my_xasprintf ("%010U %d", unicode_string, 33, 44, 55);
98 static const uint32_t expected[] =
99 { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
100 ASSERT (result != NULL);
101 ASSERT (u32_strcmp (result, expected) == 0);
107 static const uint16_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 };
110 my_xasprintf ("%lU %d", unicode_string, 33, 44, 55);
111 static const uint32_t expected[] =
112 { 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
113 ASSERT (result != NULL);
114 ASSERT (u32_strcmp (result, expected) == 0);
119 my_xasprintf ("%10lU %d", unicode_string, 33, 44, 55);
120 static const uint32_t expected[] =
121 { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
122 ASSERT (result != NULL);
123 ASSERT (u32_strcmp (result, expected) == 0);
128 my_xasprintf ("%-10lU %d", unicode_string, 33, 44, 55);
129 static const uint32_t expected[] =
130 { 'H', 'e', 'l', 'l', 'o', ' ', ' ', ' ', ' ', ' ', ' ', '3', '3', 0 };
131 ASSERT (result != NULL);
132 ASSERT (u32_strcmp (result, expected) == 0);
135 { /* FLAG_ZERO: no effect. */
137 my_xasprintf ("%010lU %d", unicode_string, 33, 44, 55);
138 static const uint32_t expected[] =
139 { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
140 ASSERT (result != NULL);
141 ASSERT (u32_strcmp (result, expected) == 0);
147 static const uint32_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 };
150 my_xasprintf ("%llU %d", unicode_string, 33, 44, 55);
151 static const uint32_t expected[] =
152 { 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
153 ASSERT (result != NULL);
154 ASSERT (u32_strcmp (result, expected) == 0);
159 my_xasprintf ("%10llU %d", unicode_string, 33, 44, 55);
160 static const uint32_t expected[] =
161 { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
162 ASSERT (result != NULL);
163 ASSERT (u32_strcmp (result, expected) == 0);
168 my_xasprintf ("%-10llU %d", unicode_string, 33, 44, 55);
169 static const uint32_t expected[] =
170 { 'H', 'e', 'l', 'l', 'o', ' ', ' ', ' ', ' ', ' ', ' ', '3', '3', 0 };
171 ASSERT (result != NULL);
172 ASSERT (u32_strcmp (result, expected) == 0);
175 { /* FLAG_ZERO: no effect. */
177 my_xasprintf ("%010llU %d", unicode_string, 33, 44, 55);
178 static const uint32_t expected[] =
179 { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
180 ASSERT (result != NULL);
181 ASSERT (u32_strcmp (result, expected) == 0);
186 /* Test the support of the 's' conversion specifier for strings. */
190 my_xasprintf ("Mr. %s %d", "Ronald Reagan", 33, 44, 55);
191 static const uint32_t expected[] =
192 { 'M', 'r', '.', ' ', 'R', 'o', 'n', 'a', 'l', 'd',
193 ' ', 'R', 'e', 'a', 'g', 'a', 'n', ' ', '3', '3',
196 ASSERT (result != NULL);
197 ASSERT (u32_strcmp (result, expected) == 0);
203 my_xasprintf ("Mr. %20s %d", "Ronald Reagan", 33, 44, 55);
204 static const uint32_t expected[] =
205 { 'M', 'r', '.', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
206 ' ', 'R', 'o', 'n', 'a', 'l', 'd', ' ', 'R', 'e',
207 'a', 'g', 'a', 'n', ' ', '3', '3', 0
209 ASSERT (result != NULL);
210 ASSERT (u32_strcmp (result, expected) == 0);
216 my_xasprintf ("Mr. %-20s %d", "Ronald Reagan", 33, 44, 55);
217 static const uint32_t expected[] =
218 { 'M', 'r', '.', ' ', 'R', 'o', 'n', 'a', 'l', 'd',
219 ' ', 'R', 'e', 'a', 'g', 'a', 'n', ' ', ' ', ' ',
220 ' ', ' ', ' ', ' ', ' ', '3', '3', 0
222 ASSERT (result != NULL);
223 ASSERT (u32_strcmp (result, expected) == 0);
227 { /* FLAG_ZERO: no effect. */
229 my_xasprintf ("Mr. %020s %d", "Ronald Reagan", 33, 44, 55);
230 static const uint32_t expected[] =
231 { 'M', 'r', '.', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
232 ' ', 'R', 'o', 'n', 'a', 'l', 'd', ' ', 'R', 'e',
233 'a', 'g', 'a', 'n', ' ', '3', '3', 0
235 ASSERT (result != NULL);
236 ASSERT (u32_strcmp (result, expected) == 0);
240 /* Test the support of the 'a' and 'A' conversion specifier for hexadecimal
241 output of floating-point numbers. */
243 { /* A positive number. */
245 my_xasprintf ("%a %d", 3.1416015625, 33, 44, 55);
246 static const uint32_t expected1[] =
247 { '0', 'x', '1', '.', '9', '2', '2', 'p', '+', '1', ' ', '3', '3', 0 };
248 static const uint32_t expected2[] =
249 { '0', 'x', '3', '.', '2', '4', '4', 'p', '+', '0', ' ', '3', '3', 0 };
250 static const uint32_t expected3[] =
251 { '0', 'x', '6', '.', '4', '8', '8', 'p', '-', '1', ' ', '3', '3', 0 };
252 static const uint32_t expected4[] =
253 { '0', 'x', 'c', '.', '9', '1', 'p', '-', '2', ' ', '3', '3', 0 };
254 ASSERT (result != NULL);
255 ASSERT (u32_strcmp (result, expected1) == 0
256 || u32_strcmp (result, expected2) == 0
257 || u32_strcmp (result, expected3) == 0
258 || u32_strcmp (result, expected4) == 0);
264 my_xasprintf ("%10a %d", 1.75, 33, 44, 55);
265 static const uint32_t expected1[] =
266 { ' ', ' ', '0', 'x', '1', '.', 'c', 'p', '+', '0', ' ', '3', '3', 0 };
267 static const uint32_t expected2[] =
268 { ' ', ' ', '0', 'x', '3', '.', '8', 'p', '-', '1', ' ', '3', '3', 0 };
269 static const uint32_t expected3[] =
270 { ' ', ' ', ' ', ' ', '0', 'x', '7', 'p', '-', '2', ' ', '3', '3', 0 };
271 static const uint32_t expected4[] =
272 { ' ', ' ', ' ', ' ', '0', 'x', 'e', 'p', '-', '3', ' ', '3', '3', 0 };
273 ASSERT (result != NULL);
274 ASSERT (u32_strcmp (result, expected1) == 0
275 || u32_strcmp (result, expected2) == 0
276 || u32_strcmp (result, expected3) == 0
277 || u32_strcmp (result, expected4) == 0);
281 { /* Small precision. */
283 my_xasprintf ("%.10a %d", 1.75, 33, 44, 55);
284 static const uint32_t expected1[] =
285 { '0', 'x', '1', '.', 'c', '0', '0', '0', '0', '0',
286 '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3',
289 static const uint32_t expected2[] =
290 { '0', 'x', '3', '.', '8', '0', '0', '0', '0', '0',
291 '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3',
294 static const uint32_t expected3[] =
295 { '0', 'x', '7', '.', '0', '0', '0', '0', '0', '0',
296 '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3',
299 static const uint32_t expected4[] =
300 { '0', 'x', 'e', '.', '0', '0', '0', '0', '0', '0',
301 '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3',
304 ASSERT (result != NULL);
305 ASSERT (u32_strcmp (result, expected1) == 0
306 || u32_strcmp (result, expected2) == 0
307 || u32_strcmp (result, expected3) == 0
308 || u32_strcmp (result, expected4) == 0);
312 { /* Large precision. */
314 my_xasprintf ("%.50a %d", 1.75, 33, 44, 55);
315 static const uint32_t expected1[] =
316 { '0', 'x', '1', '.', 'c', '0', '0', '0', '0', '0',
317 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
318 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
319 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
320 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
321 '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3',
324 static const uint32_t expected2[] =
325 { '0', 'x', '3', '.', '8', '0', '0', '0', '0', '0',
326 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
327 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
328 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
329 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
330 '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3',
333 static const uint32_t expected3[] =
334 { '0', 'x', '7', '.', '0', '0', '0', '0', '0', '0',
335 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
336 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
337 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
338 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
339 '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3',
342 static const uint32_t expected4[] =
343 { '0', 'x', 'e', '.', '0', '0', '0', '0', '0', '0',
344 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
345 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
346 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
347 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
348 '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3',
351 ASSERT (result != NULL);
352 ASSERT (u32_strcmp (result, expected1) == 0
353 || u32_strcmp (result, expected2) == 0
354 || u32_strcmp (result, expected3) == 0
355 || u32_strcmp (result, expected4) == 0);
359 { /* A positive number. */
361 my_xasprintf ("%La %d", 3.1416015625L, 33, 44, 55);
362 static const uint32_t expected1[] =
363 { '0', 'x', '1', '.', '9', '2', '2', 'p', '+', '1',
366 static const uint32_t expected2[] =
367 { '0', 'x', '3', '.', '2', '4', '4', 'p', '+', '0',
370 static const uint32_t expected3[] =
371 { '0', 'x', '6', '.', '4', '8', '8', 'p', '-', '1',
374 static const uint32_t expected4[] =
375 { '0', 'x', 'c', '.', '9', '1', 'p', '-', '2', ' ',
378 ASSERT (result != NULL);
379 ASSERT (u32_strcmp (result, expected1) == 0
380 || u32_strcmp (result, expected2) == 0
381 || u32_strcmp (result, expected3) == 0
382 || u32_strcmp (result, expected4) == 0);
388 my_xasprintf ("%10La %d", 1.75L, 33, 44, 55);
389 static const uint32_t expected1[] =
390 { ' ', ' ', '0', 'x', '1', '.', 'c', 'p', '+', '0', ' ', '3', '3', 0 };
391 static const uint32_t expected2[] =
392 { ' ', ' ', '0', 'x', '3', '.', '8', 'p', '-', '1', ' ', '3', '3', 0 };
393 static const uint32_t expected3[] =
394 { ' ', ' ', ' ', ' ', '0', 'x', '7', 'p', '-', '2', ' ', '3', '3', 0 };
395 static const uint32_t expected4[] =
396 { ' ', ' ', ' ', ' ', '0', 'x', 'e', 'p', '-', '3', ' ', '3', '3', 0 };
397 ASSERT (result != NULL);
398 ASSERT (u32_strcmp (result, expected1) == 0
399 || u32_strcmp (result, expected2) == 0
400 || u32_strcmp (result, expected3) == 0
401 || u32_strcmp (result, expected4) == 0);
405 { /* Small precision. */
407 my_xasprintf ("%.10La %d", 1.75L, 33, 44, 55);
408 static const uint32_t expected1[] =
409 { '0', 'x', '1', '.', 'c', '0', '0', '0', '0', '0',
410 '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3',
413 static const uint32_t expected2[] =
414 { '0', 'x', '3', '.', '8', '0', '0', '0', '0', '0',
415 '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3',
418 static const uint32_t expected3[] =
419 { '0', 'x', '7', '.', '0', '0', '0', '0', '0', '0',
420 '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3',
423 static const uint32_t expected4[] =
424 { '0', 'x', 'e', '.', '0', '0', '0', '0', '0', '0',
425 '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3',
428 ASSERT (result != NULL);
429 ASSERT (u32_strcmp (result, expected1) == 0
430 || u32_strcmp (result, expected2) == 0
431 || u32_strcmp (result, expected3) == 0
432 || u32_strcmp (result, expected4) == 0);
436 { /* Large precision. */
438 my_xasprintf ("%.50La %d", 1.75L, 33, 44, 55);
439 static const uint32_t expected1[] =
440 { '0', 'x', '1', '.', 'c', '0', '0', '0', '0', '0',
441 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
442 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
443 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
444 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
445 '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3',
448 static const uint32_t expected2[] =
449 { '0', 'x', '3', '.', '8', '0', '0', '0', '0', '0',
450 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
451 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
452 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
453 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
454 '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3',
457 static const uint32_t expected3[] =
458 { '0', 'x', '7', '.', '0', '0', '0', '0', '0', '0',
459 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
460 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
461 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
462 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
463 '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3',
466 static const uint32_t expected4[] =
467 { '0', 'x', 'e', '.', '0', '0', '0', '0', '0', '0',
468 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
469 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
470 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
471 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
472 '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3',
475 ASSERT (result != NULL);
476 ASSERT (u32_strcmp (result, expected1) == 0
477 || u32_strcmp (result, expected2) == 0
478 || u32_strcmp (result, expected3) == 0
479 || u32_strcmp (result, expected4) == 0);
483 /* Test the support of the %f format directive. */
485 { /* A positive number. */
487 my_xasprintf ("%f %d", 12.75, 33, 44, 55);
488 static const uint32_t expected[] =
489 { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 };
490 ASSERT (result != NULL);
491 ASSERT (u32_strcmp (result, expected) == 0);
497 my_xasprintf ("%10f %d", 1.75, 33, 44, 55);
498 static const uint32_t expected[] =
499 { ' ', ' ', '1', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 };
500 ASSERT (result != NULL);
501 ASSERT (u32_strcmp (result, expected) == 0);
507 my_xasprintf ("%.f %d", 1234.0, 33, 44, 55);
508 static const uint32_t expected[] =
509 { '1', '2', '3', '4', ' ', '3', '3', 0 };
510 ASSERT (result != NULL);
511 ASSERT (u32_strcmp (result, expected) == 0);
515 { /* A positive number. */
517 my_xasprintf ("%Lf %d", 12.75L, 33, 44, 55);
518 static const uint32_t expected[] =
519 { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 };
520 ASSERT (result != NULL);
521 ASSERT (u32_strcmp (result, expected) == 0);
527 my_xasprintf ("%10Lf %d", 1.75L, 33, 44, 55);
528 static const uint32_t expected[] =
529 { ' ', ' ', '1', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 };
530 ASSERT (result != NULL);
531 ASSERT (u32_strcmp (result, expected) == 0);
537 my_xasprintf ("%.Lf %d", 1234.0L, 33, 44, 55);
538 static const uint32_t expected[] =
539 { '1', '2', '3', '4', ' ', '3', '3', 0 };
540 ASSERT (result != NULL);
541 ASSERT (u32_strcmp (result, expected) == 0);
545 /* Test the support of the %F format directive. */
547 { /* A positive number. */
549 my_xasprintf ("%F %d", 12.75, 33, 44, 55);
550 static const uint32_t expected[] =
551 { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 };
552 ASSERT (result != NULL);
553 ASSERT (u32_strcmp (result, expected) == 0);
559 my_xasprintf ("%.F %d", 1234.0, 33, 44, 55);
560 static const uint32_t expected[] =
561 { '1', '2', '3', '4', ' ', '3', '3', 0 };
562 ASSERT (result != NULL);
563 ASSERT (u32_strcmp (result, expected) == 0);
567 { /* A positive number. */
569 my_xasprintf ("%LF %d", 12.75L, 33, 44, 55);
570 static const uint32_t expected[] =
571 { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 };
572 ASSERT (result != NULL);
573 ASSERT (u32_strcmp (result, expected) == 0);
579 my_xasprintf ("%.LF %d", 1234.0L, 33, 44, 55);
580 static const uint32_t expected[] =
581 { '1', '2', '3', '4', ' ', '3', '3', 0 };
582 ASSERT (result != NULL);
583 ASSERT (u32_strcmp (result, expected) == 0);
587 /* Test the support of the %e format directive. */
589 { /* A positive number. */
591 my_xasprintf ("%e %d", 12.75, 33, 44, 55);
592 static const uint32_t expected1[] =
593 { '1', '.', '2', '7', '5', '0', '0', '0', 'e', '+',
594 '0', '1', ' ', '3', '3', 0
596 static const uint32_t expected2[] =
597 { '1', '.', '2', '7', '5', '0', '0', '0', 'e', '+',
598 '0', '0', '1', ' ', '3', '3', 0
600 ASSERT (result != NULL);
601 ASSERT (u32_strcmp (result, expected1) == 0
602 || u32_strcmp (result, expected2) == 0);
608 my_xasprintf ("%15e %d", 1.75, 33, 44, 55);
609 static const uint32_t expected1[] =
610 { ' ', ' ', ' ', '1', '.', '7', '5', '0', '0', '0',
611 '0', 'e', '+', '0', '0', ' ', '3', '3', 0
613 static const uint32_t expected2[] =
614 { ' ', ' ', '1', '.', '7', '5', '0', '0', '0', '0',
615 'e', '+', '0', '0', '0', ' ', '3', '3', 0
617 ASSERT (result != NULL);
618 ASSERT (u32_strcmp (result, expected1) == 0
619 || u32_strcmp (result, expected2) == 0);
625 my_xasprintf ("%.e %d", 1234.0, 33, 44, 55);
626 static const uint32_t expected1[] =
627 { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 };
628 static const uint32_t expected2[] =
629 { '1', 'e', '+', '0', '0', '3', ' ', '3', '3', 0 };
630 ASSERT (result != NULL);
631 ASSERT (u32_strcmp (result, expected1) == 0
632 || u32_strcmp (result, expected2) == 0);
636 { /* A positive number. */
638 my_xasprintf ("%Le %d", 12.75L, 33, 44, 55);
639 static const uint32_t expected[] =
640 { '1', '.', '2', '7', '5', '0', '0', '0', 'e', '+',
641 '0', '1', ' ', '3', '3', 0
643 ASSERT (result != NULL);
644 ASSERT (u32_strcmp (result, expected) == 0);
650 my_xasprintf ("%15Le %d", 1.75L, 33, 44, 55);
651 static const uint32_t expected[] =
652 { ' ', ' ', ' ', '1', '.', '7', '5', '0', '0', '0',
653 '0', 'e', '+', '0', '0', ' ', '3', '3', 0
655 ASSERT (result != NULL);
656 ASSERT (u32_strcmp (result, expected) == 0);
662 my_xasprintf ("%.Le %d", 1234.0L, 33, 44, 55);
663 static const uint32_t expected[] =
664 { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 };
665 ASSERT (result != NULL);
666 ASSERT (u32_strcmp (result, expected) == 0);
670 /* Test the support of the %g format directive. */
672 { /* A positive number. */
674 my_xasprintf ("%g %d", 12.75, 33, 44, 55);
675 static const uint32_t expected[] =
676 { '1', '2', '.', '7', '5', ' ', '3', '3', 0 };
677 ASSERT (result != NULL);
678 ASSERT (u32_strcmp (result, expected) == 0);
684 my_xasprintf ("%10g %d", 1.75, 33, 44, 55);
685 static const uint32_t expected[] =
686 { ' ', ' ', ' ', ' ', ' ', ' ', '1', '.', '7', '5', ' ', '3', '3', 0 };
687 ASSERT (result != NULL);
688 ASSERT (u32_strcmp (result, expected) == 0);
694 my_xasprintf ("%.g %d", 1234.0, 33, 44, 55);
695 static const uint32_t expected1[] =
696 { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 };
697 static const uint32_t expected2[] =
698 { '1', 'e', '+', '0', '0', '3', ' ', '3', '3', 0 };
699 ASSERT (result != NULL);
700 ASSERT (u32_strcmp (result, expected1) == 0
701 || u32_strcmp (result, expected2) == 0);
705 { /* A positive number. */
707 my_xasprintf ("%Lg %d", 12.75L, 33, 44, 55);
708 static const uint32_t expected[] =
709 { '1', '2', '.', '7', '5', ' ', '3', '3', 0 };
710 ASSERT (result != NULL);
711 ASSERT (u32_strcmp (result, expected) == 0);
717 my_xasprintf ("%10Lg %d", 1.75L, 33, 44, 55);
718 static const uint32_t expected[] =
719 { ' ', ' ', ' ', ' ', ' ', ' ', '1', '.', '7', '5', ' ', '3', '3', 0 };
720 ASSERT (result != NULL);
721 ASSERT (u32_strcmp (result, expected) == 0);
727 my_xasprintf ("%.Lg %d", 1234.0L, 33, 44, 55);
728 static const uint32_t expected[] =
729 { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 };
730 ASSERT (result != NULL);
731 ASSERT (u32_strcmp (result, expected) == 0);
735 /* Test the support of the %n format directive. */
740 my_xasprintf ("%d %n", 123, &count, 33, 44, 55);
741 static const uint32_t expected[] =
742 { '1', '2', '3', ' ', 0 };
743 ASSERT (result != NULL);
744 ASSERT (u32_strcmp (result, expected) == 0);
749 /* Test the support of the POSIX/XSI format strings with positions. */
753 my_xasprintf ("%2$d %1$d", 33, 55);
754 static const uint32_t expected[] =
755 { '5', '5', ' ', '3', '3', 0 };
756 ASSERT (result != NULL);
757 ASSERT (u32_strcmp (result, expected) == 0);
761 /* Test the support of the grouping flag. */
765 my_xasprintf ("%'d %d", 1234567, 99);
766 ASSERT (result != NULL);
767 ASSERT (result[u32_strlen (result) - 1] == '9');
771 /* Test the support of the 'U' conversion specifier for Unicode strings. */
774 static const uint8_t unicode_string[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
777 my_xasprintf ("%U %d", unicode_string, 33, 44, 55);
778 static const uint32_t expected[] =
779 { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z',
780 'k', 'o', 'w', 's', 'k', 'i', ' ', '3', '3', 0
782 ASSERT (result != NULL);
783 ASSERT (u32_strcmp (result, expected) == 0);
788 my_xasprintf ("%20U %d", unicode_string, 33, 44, 55);
789 static const uint32_t expected[] =
790 { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ',
791 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i',
794 ASSERT (result != NULL);
795 ASSERT (u32_strcmp (result, expected) == 0);
800 my_xasprintf ("%-20U %d", unicode_string, 33, 44, 55);
801 static const uint32_t expected[] =
802 { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z',
803 'k', 'o', 'w', 's', 'k', 'i', ' ', ' ', ' ', ' ',
806 ASSERT (result != NULL);
807 ASSERT (u32_strcmp (result, expected) == 0);
810 { /* FLAG_ZERO: no effect. */
812 my_xasprintf ("%020U %d", unicode_string, 33, 44, 55);
813 static const uint32_t expected[] =
814 { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ',
815 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i',
818 ASSERT (result != NULL);
819 ASSERT (u32_strcmp (result, expected) == 0);
825 static const uint16_t unicode_string[] = /* Rafał Maszkowski */
827 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
832 my_xasprintf ("%lU %d", unicode_string, 33, 44, 55);
833 static const uint32_t expected[] =
834 { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z',
835 'k', 'o', 'w', 's', 'k', 'i', ' ', '3', '3', 0
837 ASSERT (result != NULL);
838 ASSERT (u32_strcmp (result, expected) == 0);
843 my_xasprintf ("%20lU %d", unicode_string, 33, 44, 55);
844 static const uint32_t expected[] =
845 { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ',
846 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i',
849 ASSERT (result != NULL);
850 ASSERT (u32_strcmp (result, expected) == 0);
855 my_xasprintf ("%-20lU %d", unicode_string, 33, 44, 55);
856 static const uint32_t expected[] =
857 { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z',
858 'k', 'o', 'w', 's', 'k', 'i', ' ', ' ', ' ', ' ',
861 ASSERT (result != NULL);
862 ASSERT (u32_strcmp (result, expected) == 0);
865 { /* FLAG_ZERO: no effect. */
867 my_xasprintf ("%020lU %d", unicode_string, 33, 44, 55);
868 static const uint32_t expected[] =
869 { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ',
870 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i',
873 ASSERT (result != NULL);
874 ASSERT (u32_strcmp (result, expected) == 0);
880 static const uint32_t unicode_string[] = /* Rafał Maszkowski */
882 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
887 my_xasprintf ("%llU %d", unicode_string, 33, 44, 55);
888 static const uint32_t expected[] =
889 { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z',
890 'k', 'o', 'w', 's', 'k', 'i', ' ', '3', '3', 0
892 ASSERT (result != NULL);
893 ASSERT (u32_strcmp (result, expected) == 0);
898 my_xasprintf ("%20llU %d", unicode_string, 33, 44, 55);
899 static const uint32_t expected[] =
900 { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ',
901 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i',
904 ASSERT (result != NULL);
905 ASSERT (u32_strcmp (result, expected) == 0);
910 my_xasprintf ("%-20llU %d", unicode_string, 33, 44, 55);
911 static const uint32_t expected[] =
912 { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z',
913 'k', 'o', 'w', 's', 'k', 'i', ' ', ' ', ' ', ' ',
916 ASSERT (result != NULL);
917 ASSERT (u32_strcmp (result, expected) == 0);
920 { /* FLAG_ZERO: no effect. */
922 my_xasprintf ("%020llU %d", unicode_string, 33, 44, 55);
923 static const uint32_t expected[] =
924 { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ',
925 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i',
928 ASSERT (result != NULL);
929 ASSERT (u32_strcmp (result, expected) == 0);
934 /* Test non-ASCII characters in the format string. */
938 my_xasprintf ("\304rger", 33, 44, 55);
939 ASSERT (result == NULL && errno == EINVAL);