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