1 /* Test of ulc_vasnprintf() function in an UTF-8 locale.
2 Copyright (C) 2007-2008 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__); \
44 test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
46 /* Test the support of the 'U' conversion specifier for Unicode strings. */
49 static const uint8_t unicode_string[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
53 my_asnprintf (NULL, &length, "%U %d", unicode_string, 33, 44, 55);
54 ASSERT (result != NULL);
55 ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
56 ASSERT (length == strlen (result));
62 my_asnprintf (NULL, &length, "%20U %d", unicode_string, 33, 44, 55);
63 ASSERT (result != NULL);
64 ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0);
65 ASSERT (length == strlen (result));
71 my_asnprintf (NULL, &length, "%-20U %d", unicode_string, 33, 44, 55);
72 ASSERT (result != NULL);
73 ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
74 ASSERT (length == strlen (result));
77 { /* FLAG_ZERO: no effect. */
80 my_asnprintf (NULL, &length, "%020U %d", unicode_string, 33, 44, 55);
81 ASSERT (result != NULL);
82 ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0);
83 ASSERT (length == strlen (result));
89 static const uint16_t unicode_string[] = /* Rafał Maszkowski */
91 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
97 my_asnprintf (NULL, &length, "%lU %d", unicode_string, 33, 44, 55);
98 ASSERT (result != NULL);
99 ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
100 ASSERT (length == strlen (result));
106 my_asnprintf (NULL, &length, "%20lU %d", unicode_string, 33, 44, 55);
107 ASSERT (result != NULL);
108 ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0);
109 ASSERT (length == strlen (result));
115 my_asnprintf (NULL, &length, "%-20lU %d", unicode_string, 33, 44, 55);
116 ASSERT (result != NULL);
117 ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
118 ASSERT (length == strlen (result));
121 { /* FLAG_ZERO: no effect. */
124 my_asnprintf (NULL, &length, "%020lU %d", unicode_string, 33, 44, 55);
125 ASSERT (result != NULL);
126 ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0);
127 ASSERT (length == strlen (result));
133 static const uint32_t unicode_string[] = /* Rafał Maszkowski */
135 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
141 my_asnprintf (NULL, &length, "%llU %d", unicode_string, 33, 44, 55);
142 ASSERT (result != NULL);
143 ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
144 ASSERT (length == strlen (result));
150 my_asnprintf (NULL, &length, "%20llU %d", unicode_string, 33, 44, 55);
151 ASSERT (result != NULL);
152 ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0);
153 ASSERT (length == strlen (result));
159 my_asnprintf (NULL, &length, "%-20llU %d", unicode_string, 33, 44, 55);
160 ASSERT (result != NULL);
161 ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
162 ASSERT (length == strlen (result));
165 { /* FLAG_ZERO: no effect. */
168 my_asnprintf (NULL, &length, "%020llU %d", unicode_string, 33, 44, 55);
169 ASSERT (result != NULL);
170 ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0);
171 ASSERT (length == strlen (result));
176 /* Test the support of the 's' conversion specifier for strings. */
179 const char *locale_string = "\303\204rger"; /* Ärger */
183 my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55);
184 ASSERT (result != NULL);
185 ASSERT (strcmp (result, "\303\204rger 33") == 0);
186 ASSERT (length == strlen (result));
192 my_asnprintf (NULL, &length, "%10s %d", locale_string, 33, 44, 55);
193 ASSERT (result != NULL);
194 ASSERT (strcmp (result, " \303\204rger 33") == 0);
195 ASSERT (length == strlen (result));
201 my_asnprintf (NULL, &length, "%-10s %d", locale_string, 33, 44, 55);
202 ASSERT (result != NULL);
203 ASSERT (strcmp (result, "\303\204rger 33") == 0);
204 ASSERT (length == strlen (result));
207 { /* FLAG_ZERO: no effect. */
210 my_asnprintf (NULL, &length, "%010s %d", locale_string, 33, 44, 55);
211 ASSERT (result != NULL);
212 ASSERT (strcmp (result, " \303\204rger 33") == 0);
213 ASSERT (length == strlen (result));
220 my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
225 va_start (args, format);
226 ret = ulc_vasnprintf (resultbuf, lengthp, format, args);
234 test_function (my_asnprintf);
238 main (int argc, char *argv[])
240 /* configure should already have checked that the locale is supported. */
241 if (setlocale (LC_ALL, "") == NULL)