Formatted output functions for Unicode strings.
[gnulib.git] / tests / unistdio / test-ulc-vasnprintf3.c
1 /* Test of ulc_vasnprintf() function in an UTF-8 locale.
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 #include <config.h>
21
22 #include "unistdio.h"
23
24 #include <locale.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
32 #define ASSERT(expr) \
33   do                                                                         \
34     {                                                                        \
35       if (!(expr))                                                           \
36         {                                                                    \
37           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
38           abort ();                                                          \
39         }                                                                    \
40     }                                                                        \
41   while (0)
42
43 static void
44 test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
45 {
46   /* Test the support of the 'U' conversion specifier for Unicode strings.  */
47
48   {
49     static const uint8_t unicode_string[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
50     {
51       size_t length;
52       char *result =
53         my_asnprintf (NULL, &length, "%U %d", unicode_string, 33, 44, 55);
54       ASSERT (result != NULL);
55       ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
56       ASSERT (length == strlen (result));
57       free (result);
58     }
59     { /* Width.  */
60       size_t length;
61       char *result =
62         my_asnprintf (NULL, &length, "%20U %d", unicode_string, 33, 44, 55);
63       ASSERT (result != NULL);
64       ASSERT (strcmp (result, "    Rafa\305\202 Maszkowski 33") == 0);
65       ASSERT (length == strlen (result));
66       free (result);
67     }
68     { /* FLAG_LEFT.  */
69       size_t length;
70       char *result =
71         my_asnprintf (NULL, &length, "%-20U %d", unicode_string, 33, 44, 55);
72       ASSERT (result != NULL);
73       ASSERT (strcmp (result, "Rafa\305\202 Maszkowski     33") == 0);
74       ASSERT (length == strlen (result));
75       free (result);
76     }
77     { /* FLAG_ZERO: no effect.  */
78       size_t length;
79       char *result =
80         my_asnprintf (NULL, &length, "%020U %d", unicode_string, 33, 44, 55);
81       ASSERT (result != NULL);
82       ASSERT (strcmp (result, "    Rafa\305\202 Maszkowski 33") == 0);
83       ASSERT (length == strlen (result));
84       free (result);
85     }
86   }
87
88   {
89     static const uint16_t unicode_string[] = /* Rafał Maszkowski */
90       {
91         'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
92         's', 'k', 'i', 0
93       };
94     {
95       size_t length;
96       char *result =
97         my_asnprintf (NULL, &length, "%lU %d", unicode_string, 33, 44, 55);
98       ASSERT (result != NULL);
99       ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
100       ASSERT (length == strlen (result));
101       free (result);
102     }
103     { /* Width.  */
104       size_t length;
105       char *result =
106         my_asnprintf (NULL, &length, "%20lU %d", unicode_string, 33, 44, 55);
107       ASSERT (result != NULL);
108       ASSERT (strcmp (result, "    Rafa\305\202 Maszkowski 33") == 0);
109       ASSERT (length == strlen (result));
110       free (result);
111     }
112     { /* FLAG_LEFT.  */
113       size_t length;
114       char *result =
115         my_asnprintf (NULL, &length, "%-20lU %d", unicode_string, 33, 44, 55);
116       ASSERT (result != NULL);
117       ASSERT (strcmp (result, "Rafa\305\202 Maszkowski     33") == 0);
118       ASSERT (length == strlen (result));
119       free (result);
120     }
121     { /* FLAG_ZERO: no effect.  */
122       size_t length;
123       char *result =
124         my_asnprintf (NULL, &length, "%020lU %d", unicode_string, 33, 44, 55);
125       ASSERT (result != NULL);
126       ASSERT (strcmp (result, "    Rafa\305\202 Maszkowski 33") == 0);
127       ASSERT (length == strlen (result));
128       free (result);
129     }
130   }
131
132   {
133     static const uint32_t unicode_string[] = /* Rafał Maszkowski */
134       {
135         'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
136         's', 'k', 'i', 0
137       };
138     {
139       size_t length;
140       char *result =
141         my_asnprintf (NULL, &length, "%llU %d", unicode_string, 33, 44, 55);
142       ASSERT (result != NULL);
143       ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
144       ASSERT (length == strlen (result));
145       free (result);
146     }
147     { /* Width.  */
148       size_t length;
149       char *result =
150         my_asnprintf (NULL, &length, "%20llU %d", unicode_string, 33, 44, 55);
151       ASSERT (result != NULL);
152       ASSERT (strcmp (result, "    Rafa\305\202 Maszkowski 33") == 0);
153       ASSERT (length == strlen (result));
154       free (result);
155     }
156     { /* FLAG_LEFT.  */
157       size_t length;
158       char *result =
159         my_asnprintf (NULL, &length, "%-20llU %d", unicode_string, 33, 44, 55);
160       ASSERT (result != NULL);
161       ASSERT (strcmp (result, "Rafa\305\202 Maszkowski     33") == 0);
162       ASSERT (length == strlen (result));
163       free (result);
164     }
165     { /* FLAG_ZERO: no effect.  */
166       size_t length;
167       char *result =
168         my_asnprintf (NULL, &length, "%020llU %d", unicode_string, 33, 44, 55);
169       ASSERT (result != NULL);
170       ASSERT (strcmp (result, "    Rafa\305\202 Maszkowski 33") == 0);
171       ASSERT (length == strlen (result));
172       free (result);
173     }
174   }
175
176   /* Test the support of the 's' conversion specifier for strings.  */
177
178   {
179     const char *locale_string = "\303\204rger"; /* Ärger */
180     {
181       size_t length;
182       char *result =
183         my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55);
184       ASSERT (result != NULL);
185       ASSERT (strcmp (result, "\303\204rger 33") == 0);
186       ASSERT (length == strlen (result));
187       free (result);
188     }
189     { /* Width.  */
190       size_t length;
191       char *result =
192         my_asnprintf (NULL, &length, "%10s %d", locale_string, 33, 44, 55);
193       ASSERT (result != NULL);
194       ASSERT (strcmp (result, "     \303\204rger 33") == 0);
195       ASSERT (length == strlen (result));
196       free (result);
197     }
198     { /* FLAG_LEFT.  */
199       size_t length;
200       char *result =
201         my_asnprintf (NULL, &length, "%-10s %d", locale_string, 33, 44, 55);
202       ASSERT (result != NULL);
203       ASSERT (strcmp (result, "\303\204rger      33") == 0);
204       ASSERT (length == strlen (result));
205       free (result);
206     }
207     { /* FLAG_ZERO: no effect.  */
208       size_t length;
209       char *result =
210         my_asnprintf (NULL, &length, "%010s %d", locale_string, 33, 44, 55);
211       ASSERT (result != NULL);
212       ASSERT (strcmp (result, "     \303\204rger 33") == 0);
213       ASSERT (length == strlen (result));
214       free (result);
215     }
216   }
217 }
218
219 static char *
220 my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
221 {
222   va_list args;
223   char *ret;
224
225   va_start (args, format);
226   ret = ulc_vasnprintf (resultbuf, lengthp, format, args);
227   va_end (args);
228   return ret;
229 }
230
231 static void
232 test_vasnprintf ()
233 {
234   test_function (my_asnprintf);
235 }
236
237 int
238 main (int argc, char *argv[])
239 {
240   /* configure should already have checked that the locale is supported.  */
241   if (setlocale (LC_ALL, "") == NULL)
242     return 1;
243
244   test_vasnprintf ();
245   return 0;
246 }