Formatted output functions for Unicode strings.
[gnulib.git] / tests / unistdio / test-ulc-vasnprintf2.c
1 /* Test of ulc_vasnprintf() function in an ISO-8859-1 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? Maszkowski 33") == 0
56               || strcmp (result, "Rafal Maszkowski 33") == 0);
57       ASSERT (length == strlen (result));
58       free (result);
59     }
60     { /* Width.  */
61       size_t length;
62       char *result =
63         my_asnprintf (NULL, &length, "%20U %d", unicode_string, 33, 44, 55);
64       ASSERT (result != NULL);
65       ASSERT (strcmp (result, "    Rafa? Maszkowski 33") == 0
66               || strcmp (result, "    Rafal Maszkowski 33") == 0);
67       ASSERT (length == strlen (result));
68       free (result);
69     }
70     { /* FLAG_LEFT.  */
71       size_t length;
72       char *result =
73         my_asnprintf (NULL, &length, "%-20U %d", unicode_string, 33, 44, 55);
74       ASSERT (result != NULL);
75       ASSERT (strcmp (result, "Rafa? Maszkowski     33") == 0
76               || strcmp (result, "Rafal Maszkowski     33") == 0);
77       ASSERT (length == strlen (result));
78       free (result);
79     }
80     { /* FLAG_ZERO: no effect.  */
81       size_t length;
82       char *result =
83         my_asnprintf (NULL, &length, "%020U %d", unicode_string, 33, 44, 55);
84       ASSERT (result != NULL);
85       ASSERT (strcmp (result, "    Rafa? Maszkowski 33") == 0
86               || strcmp (result, "    Rafal Maszkowski 33") == 0);
87       ASSERT (length == strlen (result));
88       free (result);
89     }
90   }
91
92   {
93     static const uint16_t unicode_string[] = /* Rafał Maszkowski */
94       {
95         'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
96         's', 'k', 'i', 0
97       };
98     {
99       size_t length;
100       char *result =
101         my_asnprintf (NULL, &length, "%lU %d", unicode_string, 33, 44, 55);
102       ASSERT (result != NULL);
103       ASSERT (strcmp (result, "Rafa? Maszkowski 33") == 0
104               || strcmp (result, "Rafal Maszkowski 33") == 0);
105       ASSERT (length == strlen (result));
106       free (result);
107     }
108     { /* Width.  */
109       size_t length;
110       char *result =
111         my_asnprintf (NULL, &length, "%20lU %d", unicode_string, 33, 44, 55);
112       ASSERT (result != NULL);
113       ASSERT (strcmp (result, "    Rafa? Maszkowski 33") == 0
114               || strcmp (result, "    Rafal Maszkowski 33") == 0);
115       ASSERT (length == strlen (result));
116       free (result);
117     }
118     { /* FLAG_LEFT.  */
119       size_t length;
120       char *result =
121         my_asnprintf (NULL, &length, "%-20lU %d", unicode_string, 33, 44, 55);
122       ASSERT (result != NULL);
123       ASSERT (strcmp (result, "Rafa? Maszkowski     33") == 0
124               || strcmp (result, "Rafal Maszkowski     33") == 0);
125       ASSERT (length == strlen (result));
126       free (result);
127     }
128     { /* FLAG_ZERO: no effect.  */
129       size_t length;
130       char *result =
131         my_asnprintf (NULL, &length, "%020lU %d", unicode_string, 33, 44, 55);
132       ASSERT (result != NULL);
133       ASSERT (strcmp (result, "    Rafa? Maszkowski 33") == 0
134               || strcmp (result, "    Rafal Maszkowski 33") == 0);
135       ASSERT (length == strlen (result));
136       free (result);
137     }
138   }
139
140   {
141     static const uint32_t unicode_string[] = /* Rafał Maszkowski */
142       {
143         'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
144         's', 'k', 'i', 0
145       };
146     {
147       size_t length;
148       char *result =
149         my_asnprintf (NULL, &length, "%llU %d", unicode_string, 33, 44, 55);
150       ASSERT (result != NULL);
151       ASSERT (strcmp (result, "Rafa? Maszkowski 33") == 0
152               || strcmp (result, "Rafal Maszkowski 33") == 0);
153       ASSERT (length == strlen (result));
154       free (result);
155     }
156     { /* Width.  */
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? Maszkowski 33") == 0
162               || strcmp (result, "    Rafal Maszkowski 33") == 0);
163       ASSERT (length == strlen (result));
164       free (result);
165     }
166     { /* FLAG_LEFT.  */
167       size_t length;
168       char *result =
169         my_asnprintf (NULL, &length, "%-20llU %d", unicode_string, 33, 44, 55);
170       ASSERT (result != NULL);
171       ASSERT (strcmp (result, "Rafa? Maszkowski     33") == 0
172               || strcmp (result, "Rafal Maszkowski     33") == 0);
173       ASSERT (length == strlen (result));
174       free (result);
175     }
176     { /* FLAG_ZERO: no effect.  */
177       size_t length;
178       char *result =
179         my_asnprintf (NULL, &length, "%020llU %d", unicode_string, 33, 44, 55);
180       ASSERT (result != NULL);
181       ASSERT (strcmp (result, "    Rafa? Maszkowski 33") == 0
182               || strcmp (result, "    Rafal Maszkowski 33") == 0);
183       ASSERT (length == strlen (result));
184       free (result);
185     }
186   }
187
188   /* Test the support of the 's' conversion specifier for strings.  */
189
190   {
191     const char *locale_string = "\304rger"; /* Ärger */
192     {
193       size_t length;
194       char *result =
195         my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55);
196       ASSERT (result != NULL);
197       ASSERT (strcmp (result, "\304rger 33") == 0);
198       ASSERT (length == strlen (result));
199       free (result);
200     }
201     { /* Width.  */
202       size_t length;
203       char *result =
204         my_asnprintf (NULL, &length, "%10s %d", locale_string, 33, 44, 55);
205       ASSERT (result != NULL);
206       ASSERT (strcmp (result, "     \304rger 33") == 0);
207       ASSERT (length == strlen (result));
208       free (result);
209     }
210     { /* FLAG_LEFT.  */
211       size_t length;
212       char *result =
213         my_asnprintf (NULL, &length, "%-10s %d", locale_string, 33, 44, 55);
214       ASSERT (result != NULL);
215       ASSERT (strcmp (result, "\304rger      33") == 0);
216       ASSERT (length == strlen (result));
217       free (result);
218     }
219     { /* FLAG_ZERO: no effect.  */
220       size_t length;
221       char *result =
222         my_asnprintf (NULL, &length, "%010s %d", locale_string, 33, 44, 55);
223       ASSERT (result != NULL);
224       ASSERT (strcmp (result, "     \304rger 33") == 0);
225       ASSERT (length == strlen (result));
226       free (result);
227     }
228   }
229 }
230
231 static char *
232 my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
233 {
234   va_list args;
235   char *ret;
236
237   va_start (args, format);
238   ret = ulc_vasnprintf (resultbuf, lengthp, format, args);
239   va_end (args);
240   return ret;
241 }
242
243 static void
244 test_vasnprintf ()
245 {
246   test_function (my_asnprintf);
247 }
248
249 int
250 main (int argc, char *argv[])
251 {
252   /* configure should already have checked that the locale is supported.  */
253   if (setlocale (LC_ALL, "") == NULL)
254     return 1;
255
256   test_vasnprintf ();
257   return 0;
258 }