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