t_mbrtoc16.c revision 1.2 1 1.2 riastrad /* $NetBSD: t_mbrtoc16.c,v 1.2 2024/08/19 16:24:05 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2002 Tim J. Robbins
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Copyright (c) 2013 Ed Schouten <ed (at) FreeBSD.org>
8 1.1 riastrad * All rights reserved.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 1.1 riastrad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 riastrad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 riastrad * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 1.1 riastrad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 riastrad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 riastrad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 riastrad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 riastrad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 riastrad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 riastrad * SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad /*
32 1.1 riastrad * Test program for mbrtoc16() as specified by ISO/IEC 9899:2011.
33 1.1 riastrad */
34 1.1 riastrad
35 1.1 riastrad #include <sys/cdefs.h>
36 1.2 riastrad __RCSID("$NetBSD: t_mbrtoc16.c,v 1.2 2024/08/19 16:24:05 riastradh Exp $");
37 1.1 riastrad
38 1.1 riastrad #include <errno.h>
39 1.1 riastrad #include <inttypes.h>
40 1.1 riastrad #include <limits.h>
41 1.1 riastrad #include <locale.h>
42 1.1 riastrad #include <string.h>
43 1.1 riastrad #include <uchar.h>
44 1.1 riastrad
45 1.1 riastrad #include <atf-c.h>
46 1.1 riastrad
47 1.1 riastrad static void
48 1.1 riastrad require_lc_ctype(const char *locale_name)
49 1.1 riastrad {
50 1.1 riastrad char *lc_ctype_set;
51 1.1 riastrad
52 1.1 riastrad lc_ctype_set = setlocale(LC_CTYPE, locale_name);
53 1.1 riastrad if (lc_ctype_set == NULL)
54 1.1 riastrad atf_tc_fail("setlocale(LC_CTYPE, \"%s\") failed; errno=%d",
55 1.1 riastrad locale_name, errno);
56 1.1 riastrad
57 1.1 riastrad ATF_REQUIRE_EQ_MSG(strcmp(lc_ctype_set, locale_name), 0,
58 1.1 riastrad "lc_ctype_set=%s locale_name=%s", lc_ctype_set, locale_name);
59 1.1 riastrad }
60 1.1 riastrad
61 1.1 riastrad static mbstate_t s;
62 1.1 riastrad static char16_t c16;
63 1.1 riastrad
64 1.1 riastrad ATF_TC_WITHOUT_HEAD(mbrtoc16_c_locale_test);
65 1.1 riastrad ATF_TC_BODY(mbrtoc16_c_locale_test, tc)
66 1.1 riastrad {
67 1.1 riastrad size_t n;
68 1.1 riastrad
69 1.1 riastrad require_lc_ctype("C");
70 1.1 riastrad
71 1.1 riastrad /* Null wide character, internal state. */
72 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 1, NULL)), 0, "n=%zu", n);
73 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%"PRIx16, (uint16_t)c16);
74 1.1 riastrad
75 1.1 riastrad /* Null wide character. */
76 1.1 riastrad memset(&s, 0, sizeof(s));
77 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 1, &s)), 0, "n=%zu", n);
78 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%"PRIx16, (uint16_t)c16);
79 1.1 riastrad
80 1.1 riastrad /* Latin letter A, internal state. */
81 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(NULL, 0, 0, NULL)), 0, "n=%zu", n);
82 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "A", 1, NULL)), 1, "n=%zu", n);
83 1.1 riastrad ATF_CHECK_EQ_MSG(c16, L'A', "c16=U+%"PRIx16" L'A'=U+%"PRIx16,
84 1.1 riastrad (uint16_t)c16, (uint16_t)L'A');
85 1.1 riastrad
86 1.1 riastrad /* Latin letter A. */
87 1.1 riastrad memset(&s, 0, sizeof(s));
88 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "A", 1, &s)), 1, "n=%zu", n);
89 1.1 riastrad ATF_CHECK_EQ_MSG(c16, L'A', "c16=U+%"PRIx16" L'A'=U+%"PRIx16,
90 1.1 riastrad (uint16_t)c16, (uint16_t)L'A');
91 1.1 riastrad
92 1.1 riastrad /* Incomplete character sequence. */
93 1.1 riastrad c16 = L'z';
94 1.1 riastrad memset(&s, 0, sizeof(s));
95 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 0, &s)), (size_t)-2,
96 1.1 riastrad "n=%zu", n);
97 1.1 riastrad ATF_CHECK_EQ_MSG(c16, L'z', "c16=U+%"PRIx16" L'z'=U+%"PRIx16,
98 1.1 riastrad (uint16_t)c16, (uint16_t)L'z');
99 1.1 riastrad
100 1.1 riastrad /* Check that mbrtoc16() doesn't access the buffer when n == 0. */
101 1.1 riastrad c16 = L'z';
102 1.1 riastrad memset(&s, 0, sizeof(s));
103 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 0, &s)), (size_t)-2,
104 1.1 riastrad "n=%zu", n);
105 1.1 riastrad ATF_CHECK_EQ_MSG(c16, L'z', "c16=U+%"PRIx16" L'z'=U+%"PRIx16,
106 1.1 riastrad (uint16_t)c16, (uint16_t)L'z');
107 1.1 riastrad
108 1.1 riastrad /* Check that mbrtoc16() doesn't read ahead too aggressively. */
109 1.1 riastrad memset(&s, 0, sizeof(s));
110 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "AB", 2, &s)), 1, "n=%zu", n);
111 1.1 riastrad ATF_CHECK_EQ_MSG(c16, L'A', "c16=U+%"PRIx16" L'A'=U+%"PRIx16,
112 1.1 riastrad (uint16_t)c16, (uint16_t)L'A');
113 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "C", 1, &s)), 1, "n=%zu", n);
114 1.1 riastrad ATF_CHECK_EQ_MSG(c16, L'C', "c16=U+%"PRIx16" L'C'=U+%"PRIx16,
115 1.1 riastrad (uint16_t)c16, (uint16_t)L'C');
116 1.2 riastrad }
117 1.2 riastrad
118 1.2 riastrad ATF_TC_WITHOUT_HEAD(mbrtoc16_iso2022jp_locale_test);
119 1.2 riastrad ATF_TC_BODY(mbrtoc16_iso2022jp_locale_test, tc)
120 1.2 riastrad {
121 1.2 riastrad size_t n;
122 1.2 riastrad
123 1.2 riastrad require_lc_ctype("ja_JP.ISO-2022-JP");
124 1.2 riastrad
125 1.2 riastrad /* Null wide character, internal state. */
126 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 1, NULL)), 0, "n=%zu", n);
127 1.2 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%04"PRIx16, (uint16_t)c16);
128 1.2 riastrad
129 1.2 riastrad /* Null wide character. */
130 1.2 riastrad memset(&s, 0, sizeof(s));
131 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 1, &s)), 0, "n=%zu", n);
132 1.2 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%04"PRIx16, (uint16_t)c16);
133 1.1 riastrad
134 1.2 riastrad /* Latin letter A, internal state. */
135 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(NULL, 0, 0, NULL)), 0, "n=%zu", n);
136 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "A", 1, NULL)), 1, "n=%zu", n);
137 1.2 riastrad ATF_CHECK_EQ_MSG(c16, L'A', "c16=U+%04"PRIx16" L'A'=U+%04"PRIx16,
138 1.2 riastrad (uint16_t)c16, (uint16_t)L'A');
139 1.2 riastrad
140 1.2 riastrad /* Latin letter A. */
141 1.2 riastrad memset(&s, 0, sizeof(s));
142 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "A", 1, &s)), 1, "n=%zu", n);
143 1.2 riastrad ATF_CHECK_EQ_MSG(c16, L'A', "c16=U+%04"PRIx16" L'A'=U+%04"PRIx16,
144 1.2 riastrad (uint16_t)c16, (uint16_t)L'A');
145 1.2 riastrad
146 1.2 riastrad /* Incomplete character sequence. */
147 1.2 riastrad c16 = L'z';
148 1.2 riastrad memset(&s, 0, sizeof(s));
149 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 0, &s)), (size_t)-2,
150 1.2 riastrad "n=%zu", n);
151 1.2 riastrad ATF_CHECK_EQ_MSG(c16, L'z', "c16=U+%04"PRIx16" L'z'=U+%04"PRIx16,
152 1.2 riastrad (uint16_t)c16, (uint16_t)L'z');
153 1.2 riastrad
154 1.2 riastrad /* Check that mbrtoc16() doesn't access the buffer when n == 0. */
155 1.2 riastrad c16 = L'z';
156 1.2 riastrad memset(&s, 0, sizeof(s));
157 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 0, &s)), (size_t)-2,
158 1.2 riastrad "n=%zu", n);
159 1.2 riastrad ATF_CHECK_EQ_MSG(c16, L'z', "c16=U+%04"PRIx16" L'z'=U+%04"PRIx16,
160 1.2 riastrad (uint16_t)c16, (uint16_t)L'z');
161 1.2 riastrad
162 1.2 riastrad /* Check that mbrtoc16() doesn't read ahead too aggressively. */
163 1.2 riastrad memset(&s, 0, sizeof(s));
164 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "AB", 2, &s)), 1, "n=%zu", n);
165 1.2 riastrad ATF_CHECK_EQ_MSG(c16, L'A', "c16=U+%04"PRIx16" L'A'=U+%04"PRIx16,
166 1.2 riastrad (uint16_t)c16, (uint16_t)L'A');
167 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "C", 1, &s)), 1, "n=%zu", n);
168 1.2 riastrad ATF_CHECK_EQ_MSG(c16, L'C', "c16=U+%04"PRIx16" L'C'=U+%04"PRIx16,
169 1.2 riastrad (uint16_t)c16, (uint16_t)L'C');
170 1.2 riastrad
171 1.2 riastrad /* Incomplete character sequence (shift sequence only). */
172 1.2 riastrad memset(&s, 0, sizeof(s));
173 1.2 riastrad c16 = 0;
174 1.2 riastrad atf_tc_expect_fail("PR lib/58618:"
175 1.2 riastrad " mbrtocN(3) fails to keep shift state");
176 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\x1b(J", 3, &s)), (size_t)-2,
177 1.2 riastrad "n=%zu", n);
178 1.2 riastrad atf_tc_expect_pass();
179 1.2 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%04"PRIx16, (uint16_t)c16);
180 1.2 riastrad
181 1.2 riastrad /* Same as above, but complete (U+00A5 YEN SIGN). */
182 1.2 riastrad memset(&s, 0, sizeof(s));
183 1.2 riastrad c16 = 0;
184 1.2 riastrad atf_tc_expect_fail("PR lib/58618:"
185 1.2 riastrad " mbrtocN(3) fails to keep shift state");
186 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\x1b(J\x5c", 4, &s)), 4,
187 1.2 riastrad "n=%zu", n);
188 1.2 riastrad ATF_CHECK_EQ_MSG(c16, 0xa5, "c16=U+%04"PRIx16, (uint16_t)c16);
189 1.2 riastrad atf_tc_expect_pass();
190 1.2 riastrad
191 1.2 riastrad /* Test restarting behaviour. */
192 1.2 riastrad memset(&s, 0, sizeof(s));
193 1.2 riastrad c16 = 0;
194 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\x1b(", 2, &s)), (size_t)-2,
195 1.2 riastrad "n=%zu", n);
196 1.2 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%04"PRIx16, (uint16_t)c16);
197 1.2 riastrad atf_tc_expect_fail("PR lib/58618:"
198 1.2 riastrad " mbrtocN(3) fails to keep shift state");
199 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "J\x5c", 2, &s)), 2, "n=%zu", n);
200 1.2 riastrad ATF_CHECK_EQ_MSG(c16, 0xa5, "c16=U+%04"PRIx16, (uint16_t)c16);
201 1.2 riastrad atf_tc_expect_pass();
202 1.2 riastrad
203 1.2 riastrad /*
204 1.2 riastrad * Test shift sequence state in various increments:
205 1.2 riastrad * 1. U+0042 LATIN CAPITAL LETTER A
206 1.2 riastrad * 2. (shift ISO/IEC 646:JP) U+00A5 YEN SIGN
207 1.2 riastrad * 3. U+00A5 YEN SIGN
208 1.2 riastrad * 4. (shift JIS X 0208) U+30A2 KATAKANA LETTER A
209 1.2 riastrad * 5. U+30A2 KATAKANA LETTER A
210 1.2 riastrad * 6. (shift to initial state) U+0000 NUL
211 1.2 riastrad */
212 1.2 riastrad memset(&s, 0, sizeof(s));
213 1.2 riastrad c16 = 0;
214 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "A\x1b(J", 4, &s)), 1,
215 1.2 riastrad "n=%zu", n);
216 1.2 riastrad ATF_CHECK_EQ_MSG(c16, L'A', "c16=U+%04"PRIx16, (uint16_t)c16);
217 1.2 riastrad c16 = 0;
218 1.2 riastrad atf_tc_expect_fail("PR lib/58618:"
219 1.2 riastrad " mbrtocN(3) fails to keep shift state");
220 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\x1b(J", 3, &s)), (size_t)-2,
221 1.2 riastrad "n=%zu", n);
222 1.2 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%04"PRIx16, (uint16_t)c16);
223 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\x5c\x5c", 2, &s)), 1,
224 1.2 riastrad "n=%zu", n);
225 1.2 riastrad ATF_CHECK_EQ_MSG(c16, 0x00a5, "c16=U+%04"PRIx16, (uint16_t)c16);
226 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\x5c\x1b$", 3, &s)), 1,
227 1.2 riastrad "n=%zu", n);
228 1.2 riastrad ATF_CHECK_EQ_MSG(c16, 0x00a5, "c16=U+%04"PRIx16, (uint16_t)c16);
229 1.2 riastrad c16 = 0x1234;
230 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\x1b", 1, &s)), (size_t)-2,
231 1.2 riastrad "n=%zu", n);
232 1.2 riastrad ATF_CHECK_EQ_MSG(c16, 0x1234, "c16=U+%04"PRIx16, (uint16_t)c16);
233 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "$B\x25\x22", 4, &s)), 4,
234 1.2 riastrad "n=%zu", n);
235 1.2 riastrad ATF_CHECK_EQ_MSG(c16, 0x30a2, "c16=U+%04"PRIx16, (uint16_t)c16);
236 1.2 riastrad c16 = 0;
237 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\x25", 1, &s)), (size_t)-2,
238 1.2 riastrad "n=%zu", n);
239 1.2 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%04"PRIx16, (uint16_t)c16);
240 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\x22\x1b(B\x00", 5, &s)), 1,
241 1.2 riastrad "n=%zu", n);
242 1.2 riastrad ATF_CHECK_EQ_MSG(c16, 0x30a2, "c16=U+%04"PRIx16, (uint16_t)c16);
243 1.2 riastrad atf_tc_expect_pass();
244 1.2 riastrad c16 = 0;
245 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\x1b(", 2, &s)), (size_t)-2,
246 1.2 riastrad "n=%zu", n);
247 1.2 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%04"PRIx16, (uint16_t)c16);
248 1.2 riastrad c16 = 42;
249 1.2 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "B\x00", 2, &s)), 0, "n=%zu", n);
250 1.2 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%04"PRIx16, (uint16_t)c16);
251 1.1 riastrad }
252 1.1 riastrad
253 1.1 riastrad ATF_TC_WITHOUT_HEAD(mbrtoc16_iso_8859_1_test);
254 1.1 riastrad ATF_TC_BODY(mbrtoc16_iso_8859_1_test, tc)
255 1.1 riastrad {
256 1.1 riastrad size_t n;
257 1.1 riastrad
258 1.1 riastrad require_lc_ctype("en_US.ISO8859-1");
259 1.1 riastrad
260 1.1 riastrad /* Currency sign. */
261 1.1 riastrad memset(&s, 0, sizeof(s));
262 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xa4", 1, &s)), 1, "n=%zu", n);
263 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0xa4, "c16=U+%"PRIx16, (uint16_t)c16);
264 1.1 riastrad }
265 1.1 riastrad
266 1.1 riastrad ATF_TC_WITHOUT_HEAD(mbrtoc16_iso_8859_15_test);
267 1.1 riastrad ATF_TC_BODY(mbrtoc16_iso_8859_15_test, tc)
268 1.1 riastrad {
269 1.1 riastrad size_t n;
270 1.1 riastrad
271 1.1 riastrad require_lc_ctype("en_US.ISO8859-15");
272 1.1 riastrad
273 1.1 riastrad /* Euro sign. */
274 1.1 riastrad memset(&s, 0, sizeof(s));
275 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xa4", 1, &s)), 1, "n=%zu", n);
276 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0x20ac, "c16=U+%"PRIx16, (uint16_t)c16);
277 1.1 riastrad }
278 1.1 riastrad
279 1.1 riastrad ATF_TC_WITHOUT_HEAD(mbrtoc16_utf_8_test);
280 1.1 riastrad ATF_TC_BODY(mbrtoc16_utf_8_test, tc)
281 1.1 riastrad {
282 1.1 riastrad size_t n;
283 1.1 riastrad
284 1.1 riastrad require_lc_ctype("en_US.UTF-8");
285 1.1 riastrad
286 1.1 riastrad /* Null wide character, internal state. */
287 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(NULL, 0, 0, NULL)), 0, "n=%zu", n);
288 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 1, NULL)), 0, "n=%zu", n);
289 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%"PRIx16, (uint16_t)c16);
290 1.1 riastrad
291 1.1 riastrad /* Null wide character. */
292 1.1 riastrad memset(&s, 0, sizeof(s));
293 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 1, &s)), 0, "n=%zu", n);
294 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%"PRIx16, (uint16_t)c16);
295 1.1 riastrad
296 1.1 riastrad /* Latin letter A, internal state. */
297 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(NULL, 0, 0, NULL)), 0, "n=%zu", n);
298 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "A", 1, NULL)), 1, "n=%zu", n);
299 1.1 riastrad ATF_CHECK_EQ_MSG(c16, L'A', "c16=U+%"PRIx16" L'A'=U+%"PRIx16,
300 1.1 riastrad (uint16_t)c16, (uint16_t)L'A');
301 1.1 riastrad
302 1.1 riastrad /* Latin letter A. */
303 1.1 riastrad memset(&s, 0, sizeof(s));
304 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "A", 1, &s)), 1, "n=%zu", n);
305 1.1 riastrad ATF_CHECK_EQ_MSG(c16, L'A', "c16=U+%"PRIx16" L'A'=U+%"PRIx16,
306 1.1 riastrad (uint16_t)c16, (uint16_t)L'A');
307 1.1 riastrad
308 1.1 riastrad /* Incomplete character sequence (zero length). */
309 1.1 riastrad c16 = L'z';
310 1.1 riastrad memset(&s, 0, sizeof(s));
311 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 0, &s)), (size_t)-2,
312 1.1 riastrad "n=%zu", n);
313 1.1 riastrad ATF_CHECK_EQ_MSG(c16, L'z', "c16=U+%"PRIx16" L'z'=U+%"PRIx16,
314 1.1 riastrad (uint16_t)c16, (uint16_t)L'z');
315 1.1 riastrad
316 1.1 riastrad /* Incomplete character sequence (truncated double-byte). */
317 1.1 riastrad memset(&s, 0, sizeof(s));
318 1.1 riastrad c16 = 0;
319 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xc3", 1, &s)), (size_t)-2,
320 1.1 riastrad "n=%zu", n);
321 1.1 riastrad
322 1.1 riastrad /* Same as above, but complete. */
323 1.1 riastrad memset(&s, 0, sizeof(s));
324 1.1 riastrad c16 = 0;
325 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xc3\x84", 2, &s)), 2,
326 1.1 riastrad "n=%zu", n);
327 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0xc4, "c16=U+%"PRIx16, (uint16_t)c16);
328 1.1 riastrad
329 1.1 riastrad /* Test restarting behaviour. */
330 1.1 riastrad memset(&s, 0, sizeof(s));
331 1.1 riastrad c16 = 0;
332 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xc3", 1, &s)), (size_t)-2,
333 1.1 riastrad "n=%zu", n);
334 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%"PRIx16, (uint16_t)c16);
335 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xb7", 1, &s)), 1, "n=%zu", n);
336 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0xf7, "c16=U+%"PRIx16, (uint16_t)c16);
337 1.1 riastrad
338 1.1 riastrad /* Surrogate pair. */
339 1.1 riastrad memset(&s, 0, sizeof(s));
340 1.1 riastrad c16 = 0;
341 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xf0\x9f\x92\xa9", 4, &s)), 4,
342 1.1 riastrad "n=%zu", n);
343 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0xd83d, "c16=U+%"PRIx16, (uint16_t)c16);
344 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 0, &s)), (size_t)-3,
345 1.1 riastrad "n=%zu", n);
346 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0xdca9, "c16=U+%"PRIx16, (uint16_t)c16);
347 1.1 riastrad
348 1.1 riastrad /* Letter e with acute, precomposed. */
349 1.1 riastrad memset(&s, 0, sizeof(s));
350 1.1 riastrad c16 = 0;
351 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xc3\xa9", 2, &s)), 2,
352 1.1 riastrad "n=%zu", n);
353 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0xe9, "c16=U+%"PRIx16, (uint16_t)c16);
354 1.1 riastrad
355 1.1 riastrad /* Letter e with acute, combined. */
356 1.1 riastrad memset(&s, 0, sizeof(s));
357 1.1 riastrad c16 = 0;
358 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\x65\xcc\x81", 3, &s)), 1,
359 1.1 riastrad "n=%zu", n);
360 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0x65, "c16=U+%"PRIx16, (uint16_t)c16);
361 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xcc\x81", 2, &s)), 2,
362 1.1 riastrad "n=%zu", n);
363 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0x301, "c16=U+%"PRIx16, (uint16_t)c16);
364 1.1 riastrad }
365 1.1 riastrad
366 1.1 riastrad ATF_TP_ADD_TCS(tp)
367 1.1 riastrad {
368 1.1 riastrad
369 1.1 riastrad ATF_TP_ADD_TC(tp, mbrtoc16_c_locale_test);
370 1.2 riastrad ATF_TP_ADD_TC(tp, mbrtoc16_iso2022jp_locale_test);
371 1.1 riastrad ATF_TP_ADD_TC(tp, mbrtoc16_iso_8859_1_test);
372 1.1 riastrad ATF_TP_ADD_TC(tp, mbrtoc16_iso_8859_15_test);
373 1.1 riastrad ATF_TP_ADD_TC(tp, mbrtoc16_utf_8_test);
374 1.1 riastrad
375 1.1 riastrad return (atf_no_error());
376 1.1 riastrad }
377