1 /* Test of ulc_vasnprintf() function in an UTF-8 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
test_function(char * (* my_asnprintf)(char *,size_t *,const char *,...))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\305\202 Maszkowski 33") == 0);
44 ASSERT (length == strlen (result));
45 free (result);
46 }
47 { /* Width. */
48 size_t length;
49 char *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));
54 free (result);
55 }
56 { /* FLAG_LEFT. */
57 size_t length;
58 char *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));
63 free (result);
64 }
65 { /* FLAG_ZERO: no effect. */
66 size_t length;
67 char *result =
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));
72 free (result);
73 }
74 }
75
76 {
77 static const uint16_t unicode_string[] = /* Rafał Maszkowski */
78 {
79 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
80 's', 'k', 'i', 0
81 };
82 {
83 size_t length;
84 char *result =
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));
89 free (result);
90 }
91 { /* Width. */
92 size_t length;
93 char *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));
98 free (result);
99 }
100 { /* FLAG_LEFT. */
101 size_t length;
102 char *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));
107 free (result);
108 }
109 { /* FLAG_ZERO: no effect. */
110 size_t length;
111 char *result =
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));
116 free (result);
117 }
118 }
119
120 {
121 static const uint32_t unicode_string[] = /* Rafał Maszkowski */
122 {
123 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
124 's', 'k', 'i', 0
125 };
126 {
127 size_t length;
128 char *result =
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));
133 free (result);
134 }
135 { /* Width. */
136 size_t length;
137 char *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));
142 free (result);
143 }
144 { /* FLAG_LEFT. */
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\305\202 Maszkowski 33") == 0);
150 ASSERT (length == strlen (result));
151 free (result);
152 }
153 { /* FLAG_ZERO: no effect. */
154 size_t length;
155 char *result =
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));
160 free (result);
161 }
162 }
163
164 /* Test the support of the 's' conversion specifier for strings. */
165
166 {
167 const char *locale_string = "\303\204rger"; /* Ärger */
168 {
169 size_t length;
170 char *result =
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));
175 free (result);
176 }
177 { /* Width. */
178 size_t length;
179 char *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));
184 free (result);
185 }
186 { /* FLAG_LEFT. */
187 size_t length;
188 char *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));
193 free (result);
194 }
195 { /* FLAG_ZERO: no effect. */
196 size_t length;
197 char *result =
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));
202 free (result);
203 }
204 }
205 }
206
207 static char *
my_asnprintf(char * resultbuf,size_t * lengthp,const char * format,...)208 my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
209 {
210 va_list args;
211 char *ret;
212
213 va_start (args, format);
214 ret = ulc_vasnprintf (resultbuf, lengthp, format, args);
215 va_end (args);
216 return ret;
217 }
218
219 static void
test_vasnprintf()220 test_vasnprintf ()
221 {
222 test_function (my_asnprintf);
223 }
224
225 int
main(int argc,char * argv[])226 main (int argc, char *argv[])
227 {
228 /* configure should already have checked that the locale is supported. */
229 if (setlocale (LC_ALL, "") == NULL)
230 return 1;
231
232 test_vasnprintf ();
233 return 0;
234 }
235