1 /* Test of ulc_vasnprintf() function in an UTF-8 locale.
2 Copyright (C) 2007 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
30 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
31 #define ASSERT(expr) \
36 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
43 test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
45 /* Test the support of the 'U' conversion specifier for Unicode strings. */
48 static const uint8_t unicode_string[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
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));
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));
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));
76 { /* FLAG_ZERO: no effect. */
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));
88 static const uint16_t unicode_string[] = /* Rafał Maszkowski */
90 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
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));
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));
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));
120 { /* FLAG_ZERO: no effect. */
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));
132 static const uint32_t unicode_string[] = /* Rafał Maszkowski */
134 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
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));
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));
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));
164 { /* FLAG_ZERO: no effect. */
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));
175 /* Test the support of the 's' conversion specifier for strings. */
178 const char *locale_string = "\303\204rger"; /* Ärger */
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));
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));
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));
206 { /* FLAG_ZERO: no effect. */
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));
219 my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
224 va_start (args, format);
225 ret = ulc_vasnprintf (resultbuf, lengthp, format, args);
233 test_function (my_asnprintf);
237 main (int argc, char *argv[])
239 /* configure should already have checked that the locale is supported. */
240 if (setlocale (LC_ALL, "") == NULL)