Formatted output functions for Unicode strings.
[gnulib.git] / tests / unistdio / test-u16-printf1.h
1 /* Test of u16_v[a]s[n]printf() function.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
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 2, or (at your option)
7    any later version.
8
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.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 static void
21 test_xfunction (uint16_t * (*my_xasprintf) (const char *, ...))
22 {
23   /* Test support of size specifiers as in C99.  */
24
25   {
26     uint16_t *result =
27       my_xasprintf ("%ju %d", (uintmax_t) 12345671, 33, 44, 55);
28     static const uint16_t expected[] =
29       { '1', '2', '3', '4', '5', '6', '7', '1', ' ', '3', '3', 0 };
30     ASSERT (result != NULL);
31     ASSERT (u16_strcmp (result, expected) == 0);
32     free (result);
33   }
34
35   {
36     uint16_t *result =
37       my_xasprintf ("%zu %d", (size_t) 12345672, 33, 44, 55);
38     static const uint16_t expected[] =
39       { '1', '2', '3', '4', '5', '6', '7', '2', ' ', '3', '3', 0 };
40     ASSERT (result != NULL);
41     ASSERT (u16_strcmp (result, expected) == 0);
42     free (result);
43   }
44
45   {
46     uint16_t *result =
47       my_xasprintf ("%tu %d", (ptrdiff_t) 12345673, 33, 44, 55);
48     static const uint16_t expected[] =
49       { '1', '2', '3', '4', '5', '6', '7', '3', ' ', '3', '3', 0 };
50     ASSERT (result != NULL);
51     ASSERT (u16_strcmp (result, expected) == 0);
52     free (result);
53   }
54
55   {
56     uint16_t *result =
57       my_xasprintf ("%Lg %d", (long double) 1.5, 33, 44, 55);
58     static const uint16_t expected[] =
59       { '1', '.', '5', ' ', '3', '3', 0 };
60     ASSERT (result != NULL);
61     ASSERT (u16_strcmp (result, expected) == 0);
62     free (result);
63   }
64
65   /* Test the support of the 'U' conversion specifier for Unicode strings.  */
66
67   {
68     static const uint8_t unicode_string[] = "Hello";
69     {
70       uint16_t *result =
71         my_xasprintf ("%U %d", unicode_string, 33, 44, 55);
72       static const uint16_t expected[] =
73         { 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
74       ASSERT (result != NULL);
75       ASSERT (u16_strcmp (result, expected) == 0);
76       free (result);
77     }
78     { /* Width.  */
79       uint16_t *result =
80         my_xasprintf ("%10U %d", unicode_string, 33, 44, 55);
81       static const uint16_t expected[] =
82         { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
83       ASSERT (result != NULL);
84       ASSERT (u16_strcmp (result, expected) == 0);
85       free (result);
86     }
87     { /* FLAG_LEFT.  */
88       uint16_t *result =
89         my_xasprintf ("%-10U %d", unicode_string, 33, 44, 55);
90       static const uint16_t expected[] =
91         { 'H', 'e', 'l', 'l', 'o', ' ', ' ', ' ', ' ', ' ', ' ', '3', '3', 0 };
92       ASSERT (result != NULL);
93       ASSERT (u16_strcmp (result, expected) == 0);
94       free (result);
95     }
96     { /* FLAG_ZERO: no effect.  */
97       uint16_t *result =
98         my_xasprintf ("%010U %d", unicode_string, 33, 44, 55);
99       static const uint16_t expected[] =
100         { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
101       ASSERT (result != NULL);
102       ASSERT (u16_strcmp (result, expected) == 0);
103       free (result);
104     }
105   }
106
107   {
108     static const uint16_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 };
109     {
110       uint16_t *result =
111         my_xasprintf ("%lU %d", unicode_string, 33, 44, 55);
112       static const uint16_t expected[] =
113         { 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
114       ASSERT (result != NULL);
115       ASSERT (u16_strcmp (result, expected) == 0);
116       free (result);
117     }
118     { /* Width.  */
119       uint16_t *result =
120         my_xasprintf ("%10lU %d", unicode_string, 33, 44, 55);
121       static const uint16_t expected[] =
122         { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
123       ASSERT (result != NULL);
124       ASSERT (u16_strcmp (result, expected) == 0);
125       free (result);
126     }
127     { /* FLAG_LEFT.  */
128       uint16_t *result =
129         my_xasprintf ("%-10lU %d", unicode_string, 33, 44, 55);
130       static const uint16_t expected[] =
131         { 'H', 'e', 'l', 'l', 'o', ' ', ' ', ' ', ' ', ' ', ' ', '3', '3', 0 };
132       ASSERT (result != NULL);
133       ASSERT (u16_strcmp (result, expected) == 0);
134       free (result);
135     }
136     { /* FLAG_ZERO: no effect.  */
137       uint16_t *result =
138         my_xasprintf ("%010lU %d", unicode_string, 33, 44, 55);
139       static const uint16_t expected[] =
140         { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
141       ASSERT (result != NULL);
142       ASSERT (u16_strcmp (result, expected) == 0);
143       free (result);
144     }
145   }
146
147   {
148     static const uint32_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 };
149     {
150       uint16_t *result =
151         my_xasprintf ("%llU %d", unicode_string, 33, 44, 55);
152       static const uint16_t expected[] =
153         { 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
154       ASSERT (result != NULL);
155       ASSERT (u16_strcmp (result, expected) == 0);
156       free (result);
157     }
158     { /* Width.  */
159       uint16_t *result =
160         my_xasprintf ("%10llU %d", unicode_string, 33, 44, 55);
161       static const uint16_t expected[] =
162         { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
163       ASSERT (result != NULL);
164       ASSERT (u16_strcmp (result, expected) == 0);
165       free (result);
166     }
167     { /* FLAG_LEFT.  */
168       uint16_t *result =
169         my_xasprintf ("%-10llU %d", unicode_string, 33, 44, 55);
170       static const uint16_t expected[] =
171         { 'H', 'e', 'l', 'l', 'o', ' ', ' ', ' ', ' ', ' ', ' ', '3', '3', 0 };
172       ASSERT (result != NULL);
173       ASSERT (u16_strcmp (result, expected) == 0);
174       free (result);
175     }
176     { /* FLAG_ZERO: no effect.  */
177       uint16_t *result =
178         my_xasprintf ("%010llU %d", unicode_string, 33, 44, 55);
179       static const uint16_t expected[] =
180         { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 };
181       ASSERT (result != NULL);
182       ASSERT (u16_strcmp (result, expected) == 0);
183       free (result);
184     }
185   }
186
187   /* Test the support of the 's' conversion specifier for strings.  */
188
189   {
190     uint16_t *result =
191       my_xasprintf ("Mr. %s %d", "Ronald Reagan", 33, 44, 55);
192     static const uint16_t expected[] =
193       { 'M', 'r', '.', ' ', 'R', 'o', 'n', 'a', 'l', 'd',
194         ' ', 'R', 'e', 'a', 'g', 'a', 'n', ' ', '3', '3',
195         0
196       };
197     ASSERT (result != NULL);
198     ASSERT (u16_strcmp (result, expected) == 0);
199     free (result);
200   }
201
202   { /* Width.  */
203     uint16_t *result =
204       my_xasprintf ("Mr. %20s %d", "Ronald Reagan", 33, 44, 55);
205     static const uint16_t expected[] =
206       { 'M', 'r', '.', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
207         ' ', 'R', 'o', 'n', 'a', 'l', 'd', ' ', 'R', 'e',
208         'a', 'g', 'a', 'n', ' ', '3', '3', 0
209       };
210     ASSERT (result != NULL);
211     ASSERT (u16_strcmp (result, expected) == 0);
212     free (result);
213   }
214
215   { /* FLAG_LEFT.  */
216     uint16_t *result =
217       my_xasprintf ("Mr. %-20s %d", "Ronald Reagan", 33, 44, 55);
218     static const uint16_t expected[] =
219       { 'M', 'r', '.', ' ', 'R', 'o', 'n', 'a', 'l', 'd',
220         ' ', 'R', 'e', 'a', 'g', 'a', 'n', ' ', ' ', ' ',
221         ' ', ' ', ' ', ' ', ' ', '3', '3', 0
222       };
223     ASSERT (result != NULL);
224     ASSERT (u16_strcmp (result, expected) == 0);
225     free (result);
226   }
227
228   { /* FLAG_ZERO: no effect.  */
229     uint16_t *result =
230       my_xasprintf ("Mr. %020s %d", "Ronald Reagan", 33, 44, 55);
231     static const uint16_t expected[] =
232       { 'M', 'r', '.', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
233         ' ', 'R', 'o', 'n', 'a', 'l', 'd', ' ', 'R', 'e',
234         'a', 'g', 'a', 'n', ' ', '3', '3', 0
235       };
236     ASSERT (result != NULL);
237     ASSERT (u16_strcmp (result, expected) == 0);
238     free (result);
239   }
240
241   /* Test the support of the 'a' and 'A' conversion specifier for hexadecimal
242      output of floating-point numbers.  */
243
244   { /* A positive number.  */
245     uint16_t *result =
246       my_xasprintf ("%a %d", 3.1416015625, 33, 44, 55);
247     static const uint16_t expected1[] =
248       { '0', 'x', '1', '.', '9', '2', '2', 'p', '+', '1', ' ', '3', '3', 0 };
249     static const uint16_t expected2[] =
250       { '0', 'x', '3', '.', '2', '4', '4', 'p', '+', '0', ' ', '3', '3', 0 };
251     static const uint16_t expected3[] =
252       { '0', 'x', '6', '.', '4', '8', '8', 'p', '-', '1', ' ', '3', '3', 0 };
253     static const uint16_t expected4[] =
254       { '0', 'x', 'c', '.', '9', '1', 'p', '-', '2', ' ', '3', '3', 0 };
255     ASSERT (result != NULL);
256     ASSERT (u16_strcmp (result, expected1) == 0
257             || u16_strcmp (result, expected2) == 0
258             || u16_strcmp (result, expected3) == 0
259             || u16_strcmp (result, expected4) == 0);
260     free (result);
261   }
262
263   { /* Width.  */
264     uint16_t *result =
265       my_xasprintf ("%10a %d", 1.75, 33, 44, 55);
266     static const uint16_t expected1[] =
267       { ' ', ' ', '0', 'x', '1', '.', 'c', 'p', '+', '0', ' ', '3', '3', 0 };
268     static const uint16_t expected2[] =
269       { ' ', ' ', '0', 'x', '3', '.', '8', 'p', '-', '1', ' ', '3', '3', 0 };
270     static const uint16_t expected3[] =
271       { ' ', ' ', ' ', ' ', '0', 'x', '7', 'p', '-', '2', ' ', '3', '3', 0 };
272     static const uint16_t expected4[] =
273       { ' ', ' ', ' ', ' ', '0', 'x', 'e', 'p', '-', '3', ' ', '3', '3', 0 };
274     ASSERT (result != NULL);
275     ASSERT (u16_strcmp (result, expected1) == 0
276             || u16_strcmp (result, expected2) == 0
277             || u16_strcmp (result, expected3) == 0
278             || u16_strcmp (result, expected4) == 0);
279     free (result);
280   }
281
282   { /* Small precision.  */
283     uint16_t *result =
284       my_xasprintf ("%.10a %d", 1.75, 33, 44, 55);
285     static const uint16_t expected1[] =
286       { '0', 'x', '1', '.', 'c', '0', '0', '0', '0', '0',
287         '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3',
288         0
289       };
290     static const uint16_t expected2[] =
291       { '0', 'x', '3', '.', '8', '0', '0', '0', '0', '0',
292         '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3',
293         0
294       };
295     static const uint16_t expected3[] =
296       { '0', 'x', '7', '.', '0', '0', '0', '0', '0', '0',
297         '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3',
298         0
299       };
300     static const uint16_t expected4[] =
301       { '0', 'x', 'e', '.', '0', '0', '0', '0', '0', '0',
302         '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3',
303         0
304       };
305     ASSERT (result != NULL);
306     ASSERT (u16_strcmp (result, expected1) == 0
307             || u16_strcmp (result, expected2) == 0
308             || u16_strcmp (result, expected3) == 0
309             || u16_strcmp (result, expected4) == 0);
310     free (result);
311   }
312
313   { /* Large precision.  */
314     uint16_t *result =
315       my_xasprintf ("%.50a %d", 1.75, 33, 44, 55);
316     static const uint16_t expected1[] =
317       { '0', 'x', '1', '.', 'c', '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', '0', '0', '0', '0', '0', '0',
322         '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3',
323         0
324       };
325     static const uint16_t expected2[] =
326       { '0', 'x', '3', '.', '8', '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', '0', '0', '0', '0', '0', '0',
331         '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3',
332         0
333       };
334     static const uint16_t expected3[] =
335       { '0', 'x', '7', '.', '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', '0', '0', '0', '0', '0', '0',
340         '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3',
341         0
342       };
343     static const uint16_t expected4[] =
344       { '0', 'x', 'e', '.', '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', '0', '0', '0', '0', '0', '0',
349         '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3',
350         0
351       };
352     ASSERT (result != NULL);
353     ASSERT (u16_strcmp (result, expected1) == 0
354             || u16_strcmp (result, expected2) == 0
355             || u16_strcmp (result, expected3) == 0
356             || u16_strcmp (result, expected4) == 0);
357     free (result);
358   }
359
360   { /* A positive number.  */
361     uint16_t *result =
362       my_xasprintf ("%La %d", 3.1416015625L, 33, 44, 55);
363     static const uint16_t expected1[] =
364       { '0', 'x', '1', '.', '9', '2', '2', 'p', '+', '1',
365         ' ', '3', '3', 0
366       };
367     static const uint16_t expected2[] =
368       { '0', 'x', '3', '.', '2', '4', '4', 'p', '+', '0',
369         ' ', '3', '3', 0
370       };
371     static const uint16_t expected3[] =
372       { '0', 'x', '6', '.', '4', '8', '8', 'p', '-', '1',
373         ' ', '3', '3', 0
374       };
375     static const uint16_t expected4[] =
376       { '0', 'x', 'c', '.', '9', '1', 'p', '-', '2', ' ',
377         '3', '3', 0
378       };
379     ASSERT (result != NULL);
380     ASSERT (u16_strcmp (result, expected1) == 0
381             || u16_strcmp (result, expected2) == 0
382             || u16_strcmp (result, expected3) == 0
383             || u16_strcmp (result, expected4) == 0);
384     free (result);
385   }
386
387   { /* Width.  */
388     uint16_t *result =
389       my_xasprintf ("%10La %d", 1.75L, 33, 44, 55);
390     static const uint16_t expected1[] =
391       { ' ', ' ', '0', 'x', '1', '.', 'c', 'p', '+', '0', ' ', '3', '3', 0 };
392     static const uint16_t expected2[] =
393       { ' ', ' ', '0', 'x', '3', '.', '8', 'p', '-', '1', ' ', '3', '3', 0 };
394     static const uint16_t expected3[] =
395       { ' ', ' ', ' ', ' ', '0', 'x', '7', 'p', '-', '2', ' ', '3', '3', 0 };
396     static const uint16_t expected4[] =
397       { ' ', ' ', ' ', ' ', '0', 'x', 'e', 'p', '-', '3', ' ', '3', '3', 0 };
398     ASSERT (result != NULL);
399     ASSERT (u16_strcmp (result, expected1) == 0
400             || u16_strcmp (result, expected2) == 0
401             || u16_strcmp (result, expected3) == 0
402             || u16_strcmp (result, expected4) == 0);
403     free (result);
404   }
405
406   { /* Small precision.  */
407     uint16_t *result =
408       my_xasprintf ("%.10La %d", 1.75L, 33, 44, 55);
409     static const uint16_t expected1[] =
410       { '0', 'x', '1', '.', 'c', '0', '0', '0', '0', '0',
411         '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3',
412         0
413       };
414     static const uint16_t expected2[] =
415       { '0', 'x', '3', '.', '8', '0', '0', '0', '0', '0',
416         '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3',
417         0
418       };
419     static const uint16_t expected3[] =
420       { '0', 'x', '7', '.', '0', '0', '0', '0', '0', '0',
421         '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3',
422         0
423       };
424     static const uint16_t expected4[] =
425       { '0', 'x', 'e', '.', '0', '0', '0', '0', '0', '0',
426         '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3',
427         0
428       };
429     ASSERT (result != NULL);
430     ASSERT (u16_strcmp (result, expected1) == 0
431             || u16_strcmp (result, expected2) == 0
432             || u16_strcmp (result, expected3) == 0
433             || u16_strcmp (result, expected4) == 0);
434     free (result);
435   }
436
437   { /* Large precision.  */
438     uint16_t *result =
439       my_xasprintf ("%.50La %d", 1.75L, 33, 44, 55);
440     static const uint16_t expected1[] =
441       { '0', 'x', '1', '.', 'c', '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', '0', '0', '0', '0', '0', '0',
446         '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3',
447         0
448       };
449     static const uint16_t expected2[] =
450       { '0', 'x', '3', '.', '8', '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', '0', '0', '0', '0', '0', '0',
455         '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3',
456         0
457       };
458     static const uint16_t expected3[] =
459       { '0', 'x', '7', '.', '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', '0', '0', '0', '0', '0', '0',
464         '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3',
465         0
466       };
467     static const uint16_t expected4[] =
468       { '0', 'x', 'e', '.', '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', '0', '0', '0', '0', '0', '0',
473         '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3',
474         0
475       };
476     ASSERT (result != NULL);
477     ASSERT (u16_strcmp (result, expected1) == 0
478             || u16_strcmp (result, expected2) == 0
479             || u16_strcmp (result, expected3) == 0
480             || u16_strcmp (result, expected4) == 0);
481     free (result);
482   }
483
484   /* Test the support of the %f format directive.  */
485
486   { /* A positive number.  */
487     uint16_t *result =
488       my_xasprintf ("%f %d", 12.75, 33, 44, 55);
489     static const uint16_t expected[] =
490       { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 };
491     ASSERT (result != NULL);
492     ASSERT (u16_strcmp (result, expected) == 0);
493     free (result);
494   }
495
496   { /* Width.  */
497     uint16_t *result =
498       my_xasprintf ("%10f %d", 1.75, 33, 44, 55);
499     static const uint16_t expected[] =
500       { ' ', ' ', '1', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 };
501     ASSERT (result != NULL);
502     ASSERT (u16_strcmp (result, expected) == 0);
503     free (result);
504   }
505
506   { /* Precision.  */
507     uint16_t *result =
508       my_xasprintf ("%.f %d", 1234.0, 33, 44, 55);
509     static const uint16_t expected[] =
510       { '1', '2', '3', '4', ' ', '3', '3', 0 };
511     ASSERT (result != NULL);
512     ASSERT (u16_strcmp (result, expected) == 0);
513     free (result);
514   }
515
516   { /* A positive number.  */
517     uint16_t *result =
518       my_xasprintf ("%Lf %d", 12.75L, 33, 44, 55);
519     static const uint16_t expected[] =
520       { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 };
521     ASSERT (result != NULL);
522     ASSERT (u16_strcmp (result, expected) == 0);
523     free (result);
524   }
525
526   { /* Width.  */
527     uint16_t *result =
528       my_xasprintf ("%10Lf %d", 1.75L, 33, 44, 55);
529     static const uint16_t expected[] =
530       { ' ', ' ', '1', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 };
531     ASSERT (result != NULL);
532     ASSERT (u16_strcmp (result, expected) == 0);
533     free (result);
534   }
535
536   { /* Precision.  */
537     uint16_t *result =
538       my_xasprintf ("%.Lf %d", 1234.0L, 33, 44, 55);
539     static const uint16_t expected[] =
540       { '1', '2', '3', '4', ' ', '3', '3', 0 };
541     ASSERT (result != NULL);
542     ASSERT (u16_strcmp (result, expected) == 0);
543     free (result);
544   }
545
546   /* Test the support of the %F format directive.  */
547
548   { /* A positive number.  */
549     uint16_t *result =
550       my_xasprintf ("%F %d", 12.75, 33, 44, 55);
551     static const uint16_t expected[] =
552       { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 };
553     ASSERT (result != NULL);
554     ASSERT (u16_strcmp (result, expected) == 0);
555     free (result);
556   }
557
558   { /* Precision.  */
559     uint16_t *result =
560       my_xasprintf ("%.F %d", 1234.0, 33, 44, 55);
561     static const uint16_t expected[] =
562       { '1', '2', '3', '4', ' ', '3', '3', 0 };
563     ASSERT (result != NULL);
564     ASSERT (u16_strcmp (result, expected) == 0);
565     free (result);
566   }
567
568   { /* A positive number.  */
569     uint16_t *result =
570       my_xasprintf ("%LF %d", 12.75L, 33, 44, 55);
571     static const uint16_t expected[] =
572       { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 };
573     ASSERT (result != NULL);
574     ASSERT (u16_strcmp (result, expected) == 0);
575     free (result);
576   }
577
578   { /* Precision.  */
579     uint16_t *result =
580       my_xasprintf ("%.LF %d", 1234.0L, 33, 44, 55);
581     static const uint16_t expected[] =
582       { '1', '2', '3', '4', ' ', '3', '3', 0 };
583     ASSERT (result != NULL);
584     ASSERT (u16_strcmp (result, expected) == 0);
585     free (result);
586   }
587
588   /* Test the support of the %e format directive.  */
589
590   { /* A positive number.  */
591     uint16_t *result =
592       my_xasprintf ("%e %d", 12.75, 33, 44, 55);
593     static const uint16_t expected1[] =
594       { '1', '.', '2', '7', '5', '0', '0', '0', 'e', '+',
595         '0', '1', ' ', '3', '3', 0
596       };
597     static const uint16_t expected2[] =
598       { '1', '.', '2', '7', '5', '0', '0', '0', 'e', '+',
599         '0', '0', '1', ' ', '3', '3', 0
600       };
601     ASSERT (result != NULL);
602     ASSERT (u16_strcmp (result, expected1) == 0
603             || u16_strcmp (result, expected2) == 0);
604     free (result);
605   }
606
607   { /* Width.  */
608     uint16_t *result =
609       my_xasprintf ("%15e %d", 1.75, 33, 44, 55);
610     static const uint16_t expected1[] =
611       { ' ', ' ', ' ', '1', '.', '7', '5', '0', '0', '0',
612         '0', 'e', '+', '0', '0', ' ', '3', '3', 0
613       };
614     static const uint16_t expected2[] =
615       { ' ', ' ', '1', '.', '7', '5', '0', '0', '0', '0',
616         'e', '+', '0', '0', '0', ' ', '3', '3', 0
617       };
618     ASSERT (result != NULL);
619     ASSERT (u16_strcmp (result, expected1) == 0
620             || u16_strcmp (result, expected2) == 0);
621     free (result);
622   }
623
624   { /* Precision.  */
625     uint16_t *result =
626       my_xasprintf ("%.e %d", 1234.0, 33, 44, 55);
627     static const uint16_t expected1[] =
628       { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 };
629     static const uint16_t expected2[] =
630       { '1', 'e', '+', '0', '0', '3', ' ', '3', '3', 0 };
631     ASSERT (result != NULL);
632     ASSERT (u16_strcmp (result, expected1) == 0
633             || u16_strcmp (result, expected2) == 0);
634     free (result);
635   }
636
637   { /* A positive number.  */
638     uint16_t *result =
639       my_xasprintf ("%Le %d", 12.75L, 33, 44, 55);
640     static const uint16_t expected[] =
641       { '1', '.', '2', '7', '5', '0', '0', '0', 'e', '+',
642         '0', '1', ' ', '3', '3', 0
643       };
644     ASSERT (result != NULL);
645     ASSERT (u16_strcmp (result, expected) == 0);
646     free (result);
647   }
648
649   { /* Width.  */
650     uint16_t *result =
651       my_xasprintf ("%15Le %d", 1.75L, 33, 44, 55);
652     static const uint16_t expected[] =
653       { ' ', ' ', ' ', '1', '.', '7', '5', '0', '0', '0',
654         '0', 'e', '+', '0', '0', ' ', '3', '3', 0
655       };
656     ASSERT (result != NULL);
657     ASSERT (u16_strcmp (result, expected) == 0);
658     free (result);
659   }
660
661   { /* Precision.  */
662     uint16_t *result =
663       my_xasprintf ("%.Le %d", 1234.0L, 33, 44, 55);
664     static const uint16_t expected[] =
665       { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 };
666     ASSERT (result != NULL);
667     ASSERT (u16_strcmp (result, expected) == 0);
668     free (result);
669   }
670
671   /* Test the support of the %g format directive.  */
672
673   { /* A positive number.  */
674     uint16_t *result =
675       my_xasprintf ("%g %d", 12.75, 33, 44, 55);
676     static const uint16_t expected[] =
677       { '1', '2', '.', '7', '5', ' ', '3', '3', 0 };
678     ASSERT (result != NULL);
679     ASSERT (u16_strcmp (result, expected) == 0);
680     free (result);
681   }
682
683   { /* Width.  */
684     uint16_t *result =
685       my_xasprintf ("%10g %d", 1.75, 33, 44, 55);
686     static const uint16_t expected[] =
687       { ' ', ' ', ' ', ' ', ' ', ' ', '1', '.', '7', '5', ' ', '3', '3', 0 };
688     ASSERT (result != NULL);
689     ASSERT (u16_strcmp (result, expected) == 0);
690     free (result);
691   }
692
693   { /* Precision.  */
694     uint16_t *result =
695       my_xasprintf ("%.g %d", 1234.0, 33, 44, 55);
696     static const uint16_t expected1[] =
697       { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 };
698     static const uint16_t expected2[] =
699       { '1', 'e', '+', '0', '0', '3', ' ', '3', '3', 0 };
700     ASSERT (result != NULL);
701     ASSERT (u16_strcmp (result, expected1) == 0
702             || u16_strcmp (result, expected2) == 0);
703     free (result);
704   }
705
706   { /* A positive number.  */
707     uint16_t *result =
708       my_xasprintf ("%Lg %d", 12.75L, 33, 44, 55);
709     static const uint16_t expected[] =
710       { '1', '2', '.', '7', '5', ' ', '3', '3', 0 };
711     ASSERT (result != NULL);
712     ASSERT (u16_strcmp (result, expected) == 0);
713     free (result);
714   }
715
716   { /* Width.  */
717     uint16_t *result =
718       my_xasprintf ("%10Lg %d", 1.75L, 33, 44, 55);
719     static const uint16_t expected[] =
720       { ' ', ' ', ' ', ' ', ' ', ' ', '1', '.', '7', '5', ' ', '3', '3', 0 };
721     ASSERT (result != NULL);
722     ASSERT (u16_strcmp (result, expected) == 0);
723     free (result);
724   }
725
726   { /* Precision.  */
727     uint16_t *result =
728       my_xasprintf ("%.Lg %d", 1234.0L, 33, 44, 55);
729     static const uint16_t expected[] =
730       { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 };
731     ASSERT (result != NULL);
732     ASSERT (u16_strcmp (result, expected) == 0);
733     free (result);
734   }
735
736   /* Test the support of the %n format directive.  */
737
738   {
739     int count = -1;
740     uint16_t *result =
741       my_xasprintf ("%d %n", 123, &count, 33, 44, 55);
742     static const uint16_t expected[] =
743       { '1', '2', '3', ' ', 0 };
744     ASSERT (result != NULL);
745     ASSERT (u16_strcmp (result, expected) == 0);
746     ASSERT (count == 4);
747     free (result);
748   }
749
750   /* Test the support of the POSIX/XSI format strings with positions.  */
751
752   {
753     uint16_t *result =
754       my_xasprintf ("%2$d %1$d", 33, 55);
755     static const uint16_t expected[] =
756       { '5', '5', ' ', '3', '3', 0 };
757     ASSERT (result != NULL);
758     ASSERT (u16_strcmp (result, expected) == 0);
759     free (result);
760   }
761
762   /* Test the support of the grouping flag.  */
763
764   {
765     uint16_t *result =
766       my_xasprintf ("%'d %d", 1234567, 99);
767     ASSERT (result != NULL);
768     ASSERT (result[u16_strlen (result) - 1] == '9');
769     free (result);
770   }
771
772   /* Test the support of the 'U' conversion specifier for Unicode strings.  */
773
774   {
775     static const uint8_t unicode_string[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
776     {
777       uint16_t *result =
778         my_xasprintf ("%U %d", unicode_string, 33, 44, 55);
779       static const uint16_t expected[] =
780         { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z',
781           'k', 'o', 'w', 's', 'k', 'i', ' ', '3', '3', 0
782         };
783       ASSERT (result != NULL);
784       ASSERT (u16_strcmp (result, expected) == 0);
785       free (result);
786     }
787     { /* Width.  */
788       uint16_t *result =
789         my_xasprintf ("%20U %d", unicode_string, 33, 44, 55);
790       static const uint16_t expected[] =
791         { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ',
792           'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i',
793           ' ', '3', '3', 0
794         };
795       ASSERT (result != NULL);
796       ASSERT (u16_strcmp (result, expected) == 0);
797       free (result);
798     }
799     { /* FLAG_LEFT.  */
800       uint16_t *result =
801         my_xasprintf ("%-20U %d", unicode_string, 33, 44, 55);
802       static const uint16_t expected[] =
803         { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z',
804           'k', 'o', 'w', 's', 'k', 'i', ' ', ' ', ' ', ' ',
805           ' ', '3', '3', 0
806         };
807       ASSERT (result != NULL);
808       ASSERT (u16_strcmp (result, expected) == 0);
809       free (result);
810     }
811     { /* FLAG_ZERO: no effect.  */
812       uint16_t *result =
813         my_xasprintf ("%020U %d", unicode_string, 33, 44, 55);
814       static const uint16_t expected[] =
815         { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ',
816           'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i',
817           ' ', '3', '3', 0
818         };
819       ASSERT (result != NULL);
820       ASSERT (u16_strcmp (result, expected) == 0);
821       free (result);
822     }
823   }
824
825   {
826     static const uint16_t unicode_string[] = /* Rafał Maszkowski */
827       {
828         'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
829         's', 'k', 'i', 0
830       };
831     {
832       uint16_t *result =
833         my_xasprintf ("%lU %d", unicode_string, 33, 44, 55);
834       static const uint16_t expected[] =
835         { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z',
836           'k', 'o', 'w', 's', 'k', 'i', ' ', '3', '3', 0
837         };
838       ASSERT (result != NULL);
839       ASSERT (u16_strcmp (result, expected) == 0);
840       free (result);
841     }
842     { /* Width.  */
843       uint16_t *result =
844         my_xasprintf ("%20lU %d", unicode_string, 33, 44, 55);
845       static const uint16_t expected[] =
846         { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ',
847           'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i',
848           ' ', '3', '3', 0
849         };
850       ASSERT (result != NULL);
851       ASSERT (u16_strcmp (result, expected) == 0);
852       free (result);
853     }
854     { /* FLAG_LEFT.  */
855       uint16_t *result =
856         my_xasprintf ("%-20lU %d", unicode_string, 33, 44, 55);
857       static const uint16_t expected[] =
858         { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z',
859           'k', 'o', 'w', 's', 'k', 'i', ' ', ' ', ' ', ' ',
860           ' ', '3', '3', 0
861         };
862       ASSERT (result != NULL);
863       ASSERT (u16_strcmp (result, expected) == 0);
864       free (result);
865     }
866     { /* FLAG_ZERO: no effect.  */
867       uint16_t *result =
868         my_xasprintf ("%020lU %d", unicode_string, 33, 44, 55);
869       static const uint16_t expected[] =
870         { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ',
871           'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i',
872           ' ', '3', '3', 0
873         };
874       ASSERT (result != NULL);
875       ASSERT (u16_strcmp (result, expected) == 0);
876       free (result);
877     }
878   }
879
880   {
881     static const uint32_t unicode_string[] = /* Rafał Maszkowski */
882       {
883         'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
884         's', 'k', 'i', 0
885       };
886     {
887       uint16_t *result =
888         my_xasprintf ("%llU %d", unicode_string, 33, 44, 55);
889       static const uint16_t expected[] =
890         { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z',
891           'k', 'o', 'w', 's', 'k', 'i', ' ', '3', '3', 0
892         };
893       ASSERT (result != NULL);
894       ASSERT (u16_strcmp (result, expected) == 0);
895       free (result);
896     }
897     { /* Width.  */
898       uint16_t *result =
899         my_xasprintf ("%20llU %d", unicode_string, 33, 44, 55);
900       static const uint16_t expected[] =
901         { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ',
902           'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i',
903           ' ', '3', '3', 0
904         };
905       ASSERT (result != NULL);
906       ASSERT (u16_strcmp (result, expected) == 0);
907       free (result);
908     }
909     { /* FLAG_LEFT.  */
910       uint16_t *result =
911         my_xasprintf ("%-20llU %d", unicode_string, 33, 44, 55);
912       static const uint16_t expected[] =
913         { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z',
914           'k', 'o', 'w', 's', 'k', 'i', ' ', ' ', ' ', ' ',
915           ' ', '3', '3', 0
916         };
917       ASSERT (result != NULL);
918       ASSERT (u16_strcmp (result, expected) == 0);
919       free (result);
920     }
921     { /* FLAG_ZERO: no effect.  */
922       uint16_t *result =
923         my_xasprintf ("%020llU %d", unicode_string, 33, 44, 55);
924       static const uint16_t expected[] =
925         { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ',
926           'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i',
927           ' ', '3', '3', 0
928         };
929       ASSERT (result != NULL);
930       ASSERT (u16_strcmp (result, expected) == 0);
931       free (result);
932     }
933   }
934
935   /* Test non-ASCII characters in the format string.  */
936
937   {
938     uint16_t *result =
939       my_xasprintf ("\304rger", 33, 44, 55);
940     ASSERT (result == NULL && errno == EINVAL);
941   }
942 }