t_mbrtoc16.c revision 1.1 1 1.1 riastrad /* $NetBSD: t_mbrtoc16.c,v 1.1 2024/08/15 14:16:34 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.1 riastrad __RCSID("$NetBSD: t_mbrtoc16.c,v 1.1 2024/08/15 14:16:34 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.1 riastrad
117 1.1 riastrad }
118 1.1 riastrad
119 1.1 riastrad ATF_TC_WITHOUT_HEAD(mbrtoc16_iso_8859_1_test);
120 1.1 riastrad ATF_TC_BODY(mbrtoc16_iso_8859_1_test, tc)
121 1.1 riastrad {
122 1.1 riastrad size_t n;
123 1.1 riastrad
124 1.1 riastrad require_lc_ctype("en_US.ISO8859-1");
125 1.1 riastrad
126 1.1 riastrad /* Currency sign. */
127 1.1 riastrad memset(&s, 0, sizeof(s));
128 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xa4", 1, &s)), 1, "n=%zu", n);
129 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0xa4, "c16=U+%"PRIx16, (uint16_t)c16);
130 1.1 riastrad }
131 1.1 riastrad
132 1.1 riastrad ATF_TC_WITHOUT_HEAD(mbrtoc16_iso_8859_15_test);
133 1.1 riastrad ATF_TC_BODY(mbrtoc16_iso_8859_15_test, tc)
134 1.1 riastrad {
135 1.1 riastrad size_t n;
136 1.1 riastrad
137 1.1 riastrad require_lc_ctype("en_US.ISO8859-15");
138 1.1 riastrad
139 1.1 riastrad /* Euro sign. */
140 1.1 riastrad memset(&s, 0, sizeof(s));
141 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xa4", 1, &s)), 1, "n=%zu", n);
142 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0x20ac, "c16=U+%"PRIx16, (uint16_t)c16);
143 1.1 riastrad }
144 1.1 riastrad
145 1.1 riastrad ATF_TC_WITHOUT_HEAD(mbrtoc16_utf_8_test);
146 1.1 riastrad ATF_TC_BODY(mbrtoc16_utf_8_test, tc)
147 1.1 riastrad {
148 1.1 riastrad size_t n;
149 1.1 riastrad
150 1.1 riastrad require_lc_ctype("en_US.UTF-8");
151 1.1 riastrad
152 1.1 riastrad /* Null wide character, internal state. */
153 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(NULL, 0, 0, NULL)), 0, "n=%zu", n);
154 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 1, NULL)), 0, "n=%zu", n);
155 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%"PRIx16, (uint16_t)c16);
156 1.1 riastrad
157 1.1 riastrad /* Null wide character. */
158 1.1 riastrad memset(&s, 0, sizeof(s));
159 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 1, &s)), 0, "n=%zu", n);
160 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%"PRIx16, (uint16_t)c16);
161 1.1 riastrad
162 1.1 riastrad /* Latin letter A, internal state. */
163 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(NULL, 0, 0, NULL)), 0, "n=%zu", n);
164 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "A", 1, NULL)), 1, "n=%zu", n);
165 1.1 riastrad ATF_CHECK_EQ_MSG(c16, L'A', "c16=U+%"PRIx16" L'A'=U+%"PRIx16,
166 1.1 riastrad (uint16_t)c16, (uint16_t)L'A');
167 1.1 riastrad
168 1.1 riastrad /* Latin letter A. */
169 1.1 riastrad memset(&s, 0, sizeof(s));
170 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "A", 1, &s)), 1, "n=%zu", n);
171 1.1 riastrad ATF_CHECK_EQ_MSG(c16, L'A', "c16=U+%"PRIx16" L'A'=U+%"PRIx16,
172 1.1 riastrad (uint16_t)c16, (uint16_t)L'A');
173 1.1 riastrad
174 1.1 riastrad /* Incomplete character sequence (zero length). */
175 1.1 riastrad c16 = L'z';
176 1.1 riastrad memset(&s, 0, sizeof(s));
177 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 0, &s)), (size_t)-2,
178 1.1 riastrad "n=%zu", n);
179 1.1 riastrad ATF_CHECK_EQ_MSG(c16, L'z', "c16=U+%"PRIx16" L'z'=U+%"PRIx16,
180 1.1 riastrad (uint16_t)c16, (uint16_t)L'z');
181 1.1 riastrad
182 1.1 riastrad /* Incomplete character sequence (truncated double-byte). */
183 1.1 riastrad memset(&s, 0, sizeof(s));
184 1.1 riastrad c16 = 0;
185 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xc3", 1, &s)), (size_t)-2,
186 1.1 riastrad "n=%zu", n);
187 1.1 riastrad
188 1.1 riastrad /* Same as above, but complete. */
189 1.1 riastrad memset(&s, 0, sizeof(s));
190 1.1 riastrad c16 = 0;
191 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xc3\x84", 2, &s)), 2,
192 1.1 riastrad "n=%zu", n);
193 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0xc4, "c16=U+%"PRIx16, (uint16_t)c16);
194 1.1 riastrad
195 1.1 riastrad /* Test restarting behaviour. */
196 1.1 riastrad memset(&s, 0, sizeof(s));
197 1.1 riastrad c16 = 0;
198 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xc3", 1, &s)), (size_t)-2,
199 1.1 riastrad "n=%zu", n);
200 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0, "c16=U+%"PRIx16, (uint16_t)c16);
201 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xb7", 1, &s)), 1, "n=%zu", n);
202 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0xf7, "c16=U+%"PRIx16, (uint16_t)c16);
203 1.1 riastrad
204 1.1 riastrad /* Surrogate pair. */
205 1.1 riastrad memset(&s, 0, sizeof(s));
206 1.1 riastrad c16 = 0;
207 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xf0\x9f\x92\xa9", 4, &s)), 4,
208 1.1 riastrad "n=%zu", n);
209 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0xd83d, "c16=U+%"PRIx16, (uint16_t)c16);
210 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "", 0, &s)), (size_t)-3,
211 1.1 riastrad "n=%zu", n);
212 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0xdca9, "c16=U+%"PRIx16, (uint16_t)c16);
213 1.1 riastrad
214 1.1 riastrad /* Letter e with acute, precomposed. */
215 1.1 riastrad memset(&s, 0, sizeof(s));
216 1.1 riastrad c16 = 0;
217 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xc3\xa9", 2, &s)), 2,
218 1.1 riastrad "n=%zu", n);
219 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0xe9, "c16=U+%"PRIx16, (uint16_t)c16);
220 1.1 riastrad
221 1.1 riastrad /* Letter e with acute, combined. */
222 1.1 riastrad memset(&s, 0, sizeof(s));
223 1.1 riastrad c16 = 0;
224 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\x65\xcc\x81", 3, &s)), 1,
225 1.1 riastrad "n=%zu", n);
226 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0x65, "c16=U+%"PRIx16, (uint16_t)c16);
227 1.1 riastrad ATF_CHECK_EQ_MSG((n = mbrtoc16(&c16, "\xcc\x81", 2, &s)), 2,
228 1.1 riastrad "n=%zu", n);
229 1.1 riastrad ATF_CHECK_EQ_MSG(c16, 0x301, "c16=U+%"PRIx16, (uint16_t)c16);
230 1.1 riastrad }
231 1.1 riastrad
232 1.1 riastrad ATF_TP_ADD_TCS(tp)
233 1.1 riastrad {
234 1.1 riastrad
235 1.1 riastrad ATF_TP_ADD_TC(tp, mbrtoc16_c_locale_test);
236 1.1 riastrad ATF_TP_ADD_TC(tp, mbrtoc16_iso_8859_1_test);
237 1.1 riastrad ATF_TP_ADD_TC(tp, mbrtoc16_iso_8859_15_test);
238 1.1 riastrad ATF_TP_ADD_TC(tp, mbrtoc16_utf_8_test);
239 1.1 riastrad
240 1.1 riastrad return (atf_no_error());
241 1.1 riastrad }
242