1 /* Test of ulc_vasnprintf() function in an UTF-8 locale.
2 Copyright (C) 2007-2012 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. */
32 test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
34 /* Test the support of the 'U' conversion specifier for Unicode strings. */
37 static const uint8_t unicode_string[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
41 my_asnprintf (NULL, &length, "%U %d", unicode_string, 33, 44, 55);
42 ASSERT (result != NULL);
43 ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
44 ASSERT (length == strlen (result));
50 my_asnprintf (NULL, &length, "%20U %d", unicode_string, 33, 44, 55);
51 ASSERT (result != NULL);
52 ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0);
53 ASSERT (length == strlen (result));
59 my_asnprintf (NULL, &length, "%-20U %d", unicode_string, 33, 44, 55);
60 ASSERT (result != NULL);
61 ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
62 ASSERT (length == strlen (result));
65 { /* FLAG_ZERO: no effect. */
68 my_asnprintf (NULL, &length, "%020U %d", unicode_string, 33, 44, 55);
69 ASSERT (result != NULL);
70 ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0);
71 ASSERT (length == strlen (result));
77 static const uint16_t unicode_string[] = /* Rafał Maszkowski */
79 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
85 my_asnprintf (NULL, &length, "%lU %d", unicode_string, 33, 44, 55);
86 ASSERT (result != NULL);
87 ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
88 ASSERT (length == strlen (result));
94 my_asnprintf (NULL, &length, "%20lU %d", unicode_string, 33, 44, 55);
95 ASSERT (result != NULL);
96 ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0);
97 ASSERT (length == strlen (result));
103 my_asnprintf (NULL, &length, "%-20lU %d", unicode_string, 33, 44, 55);
104 ASSERT (result != NULL);
105 ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
106 ASSERT (length == strlen (result));
109 { /* FLAG_ZERO: no effect. */
112 my_asnprintf (NULL, &length, "%020lU %d", unicode_string, 33, 44, 55);
113 ASSERT (result != NULL);
114 ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0);
115 ASSERT (length == strlen (result));
121 static const uint32_t unicode_string[] = /* Rafał Maszkowski */
123 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
129 my_asnprintf (NULL, &length, "%llU %d", unicode_string, 33, 44, 55);
130 ASSERT (result != NULL);
131 ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
132 ASSERT (length == strlen (result));
138 my_asnprintf (NULL, &length, "%20llU %d", unicode_string, 33, 44, 55);
139 ASSERT (result != NULL);
140 ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0);
141 ASSERT (length == strlen (result));
147 my_asnprintf (NULL, &length, "%-20llU %d", unicode_string, 33, 44, 55);
148 ASSERT (result != NULL);
149 ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0);
150 ASSERT (length == strlen (result));
153 { /* FLAG_ZERO: no effect. */
156 my_asnprintf (NULL, &length, "%020llU %d", unicode_string, 33, 44, 55);
157 ASSERT (result != NULL);
158 ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0);
159 ASSERT (length == strlen (result));
164 /* Test the support of the 's' conversion specifier for strings. */
167 const char *locale_string = "\303\204rger"; /* Ärger */
171 my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55);
172 ASSERT (result != NULL);
173 ASSERT (strcmp (result, "\303\204rger 33") == 0);
174 ASSERT (length == strlen (result));
180 my_asnprintf (NULL, &length, "%10s %d", locale_string, 33, 44, 55);
181 ASSERT (result != NULL);
182 ASSERT (strcmp (result, " \303\204rger 33") == 0);
183 ASSERT (length == strlen (result));
189 my_asnprintf (NULL, &length, "%-10s %d", locale_string, 33, 44, 55);
190 ASSERT (result != NULL);
191 ASSERT (strcmp (result, "\303\204rger 33") == 0);
192 ASSERT (length == strlen (result));
195 { /* FLAG_ZERO: no effect. */
198 my_asnprintf (NULL, &length, "%010s %d", locale_string, 33, 44, 55);
199 ASSERT (result != NULL);
200 ASSERT (strcmp (result, " \303\204rger 33") == 0);
201 ASSERT (length == strlen (result));
208 my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
213 va_start (args, format);
214 ret = ulc_vasnprintf (resultbuf, lengthp, format, args);
222 test_function (my_asnprintf);
226 main (int argc, char *argv[])
228 /* configure should already have checked that the locale is supported. */
229 if (setlocale (LC_ALL, "") == NULL)