maint: update copyright
[gnulib.git] / tests / unistdio / test-ulc-vasnprintf2.c
1 /* Test of ulc_vasnprintf() function in an ISO-8859-1 locale.
2    Copyright (C) 2007-2014 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 <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "macros.h"
30
31 static void
32 test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
33 {
34   /* Test the support of the 'U' conversion specifier for Unicode strings.  */
35
36   {
37     static const uint8_t unicode_string[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
38     {
39       size_t length;
40       char *result =
41         my_asnprintf (NULL, &length, "%U %d", unicode_string, 33, 44, 55);
42       ASSERT (result != NULL);
43       ASSERT (strcmp (result, "Rafa? Maszkowski 33") == 0
44               || strcmp (result, "Rafal Maszkowski 33") == 0);
45       ASSERT (length == strlen (result));
46       free (result);
47     }
48     { /* Width.  */
49       size_t length;
50       char *result =
51         my_asnprintf (NULL, &length, "%20U %d", unicode_string, 33, 44, 55);
52       ASSERT (result != NULL);
53       ASSERT (strcmp (result, "    Rafa? Maszkowski 33") == 0
54               || strcmp (result, "    Rafal Maszkowski 33") == 0);
55       ASSERT (length == strlen (result));
56       free (result);
57     }
58     { /* FLAG_LEFT.  */
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? Maszkowski     33") == 0
64               || strcmp (result, "Rafal Maszkowski     33") == 0);
65       ASSERT (length == strlen (result));
66       free (result);
67     }
68     { /* FLAG_ZERO: no effect.  */
69       size_t length;
70       char *result =
71         my_asnprintf (NULL, &length, "%020U %d", unicode_string, 33, 44, 55);
72       ASSERT (result != NULL);
73       ASSERT (strcmp (result, "    Rafa? Maszkowski 33") == 0
74               || strcmp (result, "    Rafal Maszkowski 33") == 0);
75       ASSERT (length == strlen (result));
76       free (result);
77     }
78   }
79
80   {
81     static const uint16_t unicode_string[] = /* Rafał Maszkowski */
82       {
83         'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
84         's', 'k', 'i', 0
85       };
86     {
87       size_t length;
88       char *result =
89         my_asnprintf (NULL, &length, "%lU %d", unicode_string, 33, 44, 55);
90       ASSERT (result != NULL);
91       ASSERT (strcmp (result, "Rafa? Maszkowski 33") == 0
92               || strcmp (result, "Rafal Maszkowski 33") == 0);
93       ASSERT (length == strlen (result));
94       free (result);
95     }
96     { /* Width.  */
97       size_t length;
98       char *result =
99         my_asnprintf (NULL, &length, "%20lU %d", unicode_string, 33, 44, 55);
100       ASSERT (result != NULL);
101       ASSERT (strcmp (result, "    Rafa? Maszkowski 33") == 0
102               || strcmp (result, "    Rafal Maszkowski 33") == 0);
103       ASSERT (length == strlen (result));
104       free (result);
105     }
106     { /* FLAG_LEFT.  */
107       size_t length;
108       char *result =
109         my_asnprintf (NULL, &length, "%-20lU %d", unicode_string, 33, 44, 55);
110       ASSERT (result != NULL);
111       ASSERT (strcmp (result, "Rafa? Maszkowski     33") == 0
112               || strcmp (result, "Rafal Maszkowski     33") == 0);
113       ASSERT (length == strlen (result));
114       free (result);
115     }
116     { /* FLAG_ZERO: no effect.  */
117       size_t length;
118       char *result =
119         my_asnprintf (NULL, &length, "%020lU %d", unicode_string, 33, 44, 55);
120       ASSERT (result != NULL);
121       ASSERT (strcmp (result, "    Rafa? Maszkowski 33") == 0
122               || strcmp (result, "    Rafal Maszkowski 33") == 0);
123       ASSERT (length == strlen (result));
124       free (result);
125     }
126   }
127
128   {
129     static const uint32_t unicode_string[] = /* Rafał Maszkowski */
130       {
131         'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
132         's', 'k', 'i', 0
133       };
134     {
135       size_t length;
136       char *result =
137         my_asnprintf (NULL, &length, "%llU %d", unicode_string, 33, 44, 55);
138       ASSERT (result != NULL);
139       ASSERT (strcmp (result, "Rafa? Maszkowski 33") == 0
140               || strcmp (result, "Rafal Maszkowski 33") == 0);
141       ASSERT (length == strlen (result));
142       free (result);
143     }
144     { /* Width.  */
145       size_t length;
146       char *result =
147         my_asnprintf (NULL, &length, "%20llU %d", unicode_string, 33, 44, 55);
148       ASSERT (result != NULL);
149       ASSERT (strcmp (result, "    Rafa? Maszkowski 33") == 0
150               || strcmp (result, "    Rafal Maszkowski 33") == 0);
151       ASSERT (length == strlen (result));
152       free (result);
153     }
154     { /* FLAG_LEFT.  */
155       size_t length;
156       char *result =
157         my_asnprintf (NULL, &length, "%-20llU %d", unicode_string, 33, 44, 55);
158       ASSERT (result != NULL);
159       ASSERT (strcmp (result, "Rafa? Maszkowski     33") == 0
160               || strcmp (result, "Rafal 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? Maszkowski 33") == 0
170               || strcmp (result, "    Rafal 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 = "\304rger"; /* Ä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, "\304rger 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, "     \304rger 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, "\304rger      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, "     \304rger 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 }