t_mbrtowc.c revision 1.2 1 1.2 perseant /* $NetBSD: t_mbrtowc.c,v 1.2 2017/07/12 17:32:51 perseant Exp $ */
2 1.1 jruoho
3 1.1 jruoho /*-
4 1.1 jruoho * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1 jruoho * All rights reserved.
6 1.1 jruoho *
7 1.1 jruoho * This code is derived from software contributed to The NetBSD Foundation
8 1.1 jruoho * by YAMAMOTO Takashi
9 1.1 jruoho *
10 1.1 jruoho * Redistribution and use in source and binary forms, with or without
11 1.1 jruoho * modification, are permitted provided that the following conditions
12 1.1 jruoho * are met:
13 1.1 jruoho * 1. Redistributions of source code must retain the above copyright
14 1.1 jruoho * notice, this list of conditions and the following disclaimer.
15 1.1 jruoho * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jruoho * notice, this list of conditions and the following disclaimer in the
17 1.1 jruoho * documentation and/or other materials provided with the distribution.
18 1.1 jruoho *
19 1.1 jruoho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 jruoho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 jruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 jruoho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 jruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 jruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 jruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 jruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 jruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 jruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 jruoho * POSSIBILITY OF SUCH DAMAGE.
30 1.1 jruoho */
31 1.1 jruoho
32 1.1 jruoho /*-
33 1.1 jruoho * Copyright (c)2003 Citrus Project,
34 1.1 jruoho * All rights reserved.
35 1.1 jruoho *
36 1.1 jruoho * Redistribution and use in source and binary forms, with or without
37 1.1 jruoho * modification, are permitted provided that the following conditions
38 1.1 jruoho * are met:
39 1.1 jruoho * 1. Redistributions of source code must retain the above copyright
40 1.1 jruoho * notice, this list of conditions and the following disclaimer.
41 1.1 jruoho * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 jruoho * notice, this list of conditions and the following disclaimer in the
43 1.1 jruoho * documentation and/or other materials provided with the distribution.
44 1.1 jruoho *
45 1.1 jruoho * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 1.1 jruoho * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 1.1 jruoho * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 1.1 jruoho * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 1.1 jruoho * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 1.1 jruoho * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 1.1 jruoho * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 1.1 jruoho * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 1.1 jruoho * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 1.1 jruoho * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 1.1 jruoho * SUCH DAMAGE.
56 1.1 jruoho */
57 1.1 jruoho
58 1.1 jruoho #include <sys/cdefs.h>
59 1.1 jruoho __COPYRIGHT("@(#) Copyright (c) 2011\
60 1.1 jruoho The NetBSD Foundation, inc. All rights reserved.");
61 1.2 perseant __RCSID("$NetBSD: t_mbrtowc.c,v 1.2 2017/07/12 17:32:51 perseant Exp $");
62 1.1 jruoho
63 1.1 jruoho #include <errno.h>
64 1.1 jruoho #include <locale.h>
65 1.1 jruoho #include <stdio.h>
66 1.1 jruoho #include <stdlib.h>
67 1.1 jruoho #include <string.h>
68 1.1 jruoho #include <vis.h>
69 1.1 jruoho #include <wchar.h>
70 1.1 jruoho
71 1.1 jruoho #include <atf-c.h>
72 1.1 jruoho
73 1.1 jruoho #define SIZE 256
74 1.1 jruoho
75 1.1 jruoho static struct test {
76 1.1 jruoho const char *locale;
77 1.1 jruoho const char *data;
78 1.1 jruoho const wchar_t wchars[64];
79 1.1 jruoho const wchar_t widths[64];
80 1.1 jruoho size_t length;
81 1.1 jruoho } tests[] = {
82 1.1 jruoho {
83 1.1 jruoho "C",
84 1.1 jruoho "ABCD01234_\\",
85 1.1 jruoho { 0x41, 0x42, 0x43, 0x44, 0x30, 0x31, 0x32, 0x33, 0x34, 0x5F, 0x5C },
86 1.1 jruoho { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
87 1.1 jruoho 11
88 1.1 jruoho }, {
89 1.1 jruoho "en_US.UTF-8",
90 1.1 jruoho "[\001\177][\302\200\337\277][\340\240\200\357\277\277][\360\220\200"
91 1.1 jruoho "\200\367\277\277\277][\370\210\200\200\200\373\277\277\277\277][\374"
92 1.1 jruoho "\204\200\200\200\200\375\277\277\277\277\277]",
93 1.1 jruoho { 0x5b, 0x01, 0x7f, 0x5d, 0x5b, 0x80, 0x7ff, 0x5d, 0x5b, 0x800, 0xffff,
94 1.1 jruoho 0x5d, 0x5b, 0x10000, 0x1fffff, 0x5d, 0x5b, 0x200000, 0x3ffffff, 0x5d,
95 1.1 jruoho 0x5b, 0x4000000, 0x7fffffff, 0x5d },
96 1.1 jruoho { 1, 1, 1, 1, 1, 2, 2, 1, 1, 3, 3, 1, 1, 4, 4, 1, 1, 5, 5, 1, 1, 6, 6, 1 },
97 1.1 jruoho 24
98 1.1 jruoho }, {
99 1.1 jruoho "ja_JP.ISO2022-JP2",
100 1.1 jruoho "\033$BF|K\1348l\033(BA\033$B$\"\033(BB\033$B$$\033(B",
101 1.2 perseant #ifdef __STDC_ISO_10646__
102 1.2 perseant { 0x65E5, 0x672C, 0x8A9E, 0x41, 0x3042, 0x42, 0x3044 },
103 1.2 perseant #else
104 1.1 jruoho { 0x4200467c, 0x42004b5c, 0x4200386c, 0x41, 0x42002422, 0x42, 0x42002424 },
105 1.2 perseant #endif
106 1.1 jruoho { 5, 2, 2, 4, 5, 4, 5 },
107 1.1 jruoho 7
108 1.1 jruoho }, {
109 1.1 jruoho "ja_JP.SJIS",
110 1.1 jruoho "\223\372\226{\214\352A\202\240B\202\242",
111 1.2 perseant #ifdef __STDC_ISO_10646__
112 1.2 perseant { 0x65E5, 0x672C, 0x8A9E, 0x41, 0x3042, 0x42, 0x3044 },
113 1.2 perseant #else
114 1.2 perseant { 0x93FA, 0x967B, 0x8CEA, 0x41, 0x82A0, 0x42, 0x82A2 },
115 1.2 perseant #endif
116 1.1 jruoho { 2, 2, 2, 1, 2, 1, 2 },
117 1.1 jruoho 7
118 1.1 jruoho }, {
119 1.1 jruoho "ja_JP.eucJP",
120 1.1 jruoho "\306\374\313\334\270\354A\244\242B\244\244",
121 1.2 perseant #ifdef __STDC_ISO_10646__
122 1.2 perseant { 0x65E5, 0x672C, 0x8A9E, 0x41, 0x3042, 0x42, 0x3044 },
123 1.2 perseant #else
124 1.2 perseant { 0xC6FC, 0xCBDC, 0xB8EC, 0x41, 0xA4A2, 0x42, 0xA4A4 },
125 1.2 perseant #endif
126 1.1 jruoho { 2, 2, 2, 1, 2, 1, 2 },
127 1.1 jruoho 7
128 1.1 jruoho }, {
129 1.1 jruoho NULL,
130 1.1 jruoho NULL,
131 1.1 jruoho { },
132 1.1 jruoho { },
133 1.1 jruoho 0
134 1.1 jruoho }
135 1.1 jruoho };
136 1.1 jruoho
137 1.1 jruoho static void
138 1.1 jruoho h_ctype2(const struct test *t, bool use_mbstate)
139 1.1 jruoho {
140 1.1 jruoho mbstate_t *stp;
141 1.1 jruoho mbstate_t st;
142 1.1 jruoho char buf[SIZE];
143 1.1 jruoho char *str;
144 1.1 jruoho size_t n;
145 1.1 jruoho
146 1.1 jruoho ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
147 1.1 jruoho ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
148 1.1 jruoho
149 1.1 jruoho (void)strvis(buf, t->data, VIS_WHITE | VIS_OCTAL);
150 1.1 jruoho (void)printf("Checking string: \"%s\"\n", buf);
151 1.1 jruoho
152 1.1 jruoho ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
153 1.1 jruoho (void)printf("Using locale: %s\n", str);
154 1.1 jruoho
155 1.1 jruoho (void)printf("Using mbstate: %s\n", use_mbstate ? "yes" : "no");
156 1.1 jruoho
157 1.1 jruoho (void)memset(&st, 0, sizeof(st));
158 1.1 jruoho // mbrtowc(0, 0, 0, &st); /* XXX for ISO2022-JP */
159 1.1 jruoho stp = use_mbstate ? &st : 0;
160 1.1 jruoho
161 1.2 perseant printf("First using repeated mbrtowc\n");
162 1.2 perseant
163 1.1 jruoho for (n = 9; n > 0; n--) {
164 1.1 jruoho const char *src = t->data;
165 1.1 jruoho wchar_t dst;
166 1.1 jruoho size_t nchar = 0;
167 1.1 jruoho int width = 0;
168 1.1 jruoho
169 1.1 jruoho ATF_REQUIRE(mbsinit(stp) != 0);
170 1.1 jruoho
171 1.1 jruoho for (;;) {
172 1.1 jruoho size_t rv = mbrtowc(&dst, src, n, stp);
173 1.1 jruoho
174 1.1 jruoho if (rv == 0)
175 1.1 jruoho break;
176 1.1 jruoho
177 1.1 jruoho if (rv == (size_t)-2) {
178 1.1 jruoho src += n;
179 1.1 jruoho width += n;
180 1.1 jruoho
181 1.1 jruoho continue;
182 1.1 jruoho }
183 1.1 jruoho if (rv == (size_t)-1) {
184 1.1 jruoho ATF_REQUIRE_EQ(errno, EILSEQ);
185 1.1 jruoho atf_tc_fail("Invalid sequence");
186 1.1 jruoho /* NOTREACHED */
187 1.1 jruoho }
188 1.1 jruoho
189 1.1 jruoho width += rv;
190 1.1 jruoho src += rv;
191 1.1 jruoho
192 1.1 jruoho if (dst != t->wchars[nchar] ||
193 1.1 jruoho width != t->widths[nchar]) {
194 1.1 jruoho (void)printf("At position %zd:\n", nchar);
195 1.1 jruoho (void)printf(" expected: 0x%04X (%u)\n",
196 1.1 jruoho t->wchars[nchar], t->widths[nchar]);
197 1.1 jruoho (void)printf(" got : 0x%04X (%u)\n",
198 1.1 jruoho dst, width);
199 1.1 jruoho atf_tc_fail("Test failed");
200 1.1 jruoho }
201 1.1 jruoho
202 1.1 jruoho nchar++;
203 1.1 jruoho width = 0;
204 1.1 jruoho }
205 1.1 jruoho
206 1.1 jruoho ATF_REQUIRE_EQ_MSG(dst, 0, "Incorrect terminating character: "
207 1.1 jruoho "0x%04X (expected: 0x00)", dst);
208 1.1 jruoho
209 1.1 jruoho ATF_REQUIRE_EQ_MSG(nchar, t->length, "Incorrect length: "
210 1.1 jruoho "%zd (expected: %zd)", nchar, t->length);
211 1.1 jruoho }
212 1.1 jruoho
213 1.2 perseant printf("Now using mbsrtowcs\n");
214 1.2 perseant
215 1.1 jruoho {
216 1.1 jruoho wchar_t wbuf[SIZE];
217 1.1 jruoho size_t rv;
218 1.1 jruoho char const *src = t->data;
219 1.1 jruoho int i;
220 1.1 jruoho
221 1.1 jruoho (void)memset(wbuf, 0xFF, sizeof(wbuf));
222 1.1 jruoho
223 1.1 jruoho rv = mbsrtowcs(wbuf, &src, SIZE, stp);
224 1.1 jruoho
225 1.1 jruoho ATF_REQUIRE_EQ_MSG(rv, t->length, "Incorrect length: %zd "
226 1.1 jruoho "(expected: %zd)", rv, t->length);
227 1.1 jruoho ATF_REQUIRE_EQ(src, NULL);
228 1.1 jruoho
229 1.1 jruoho for (i = 0; wbuf[i] != 0; ++i) {
230 1.1 jruoho if (wbuf[i] == t->wchars[i])
231 1.1 jruoho continue;
232 1.1 jruoho
233 1.1 jruoho (void)printf("At position %d:\n", i);
234 1.1 jruoho (void)printf(" expected: 0x%04X\n", t->wchars[i]);
235 1.1 jruoho (void)printf(" got : 0x%04X\n", wbuf[i]);
236 1.1 jruoho atf_tc_fail("Test failed");
237 1.1 jruoho }
238 1.1 jruoho
239 1.1 jruoho ATF_REQUIRE_EQ_MSG((size_t)i, t->length, "Incorrect length: "
240 1.1 jruoho "%d (expected: %zd)", i, t->length);
241 1.1 jruoho }
242 1.1 jruoho
243 1.1 jruoho (void)printf("Ok.\n");
244 1.1 jruoho }
245 1.1 jruoho
246 1.1 jruoho ATF_TC(mbrtowc_internal);
247 1.1 jruoho ATF_TC_HEAD(mbrtowc_internal, tc)
248 1.1 jruoho {
249 1.1 jruoho atf_tc_set_md_var(tc, "descr",
250 1.1 jruoho "Checks mbrtowc(3) and mbsrtowcs(3) (using internal "
251 1.1 jruoho "state) with different locales");
252 1.1 jruoho }
253 1.1 jruoho ATF_TC_BODY(mbrtowc_internal, tc)
254 1.1 jruoho {
255 1.1 jruoho struct test *t;
256 1.1 jruoho
257 1.1 jruoho for (t = &tests[0]; t->data != NULL; ++t)
258 1.1 jruoho h_ctype2(t, false);
259 1.1 jruoho }
260 1.1 jruoho
261 1.1 jruoho ATF_TC(mbrtowc_object);
262 1.1 jruoho ATF_TC_HEAD(mbrtowc_object, tc)
263 1.1 jruoho {
264 1.1 jruoho atf_tc_set_md_var(tc, "descr",
265 1.1 jruoho "Checks mbrtowc(3) and mbsrtowcs(3) (using state "
266 1.1 jruoho "object) with different locales");
267 1.1 jruoho }
268 1.1 jruoho ATF_TC_BODY(mbrtowc_object, tc)
269 1.1 jruoho {
270 1.1 jruoho struct test *t;
271 1.1 jruoho
272 1.1 jruoho for (t = &tests[0]; t->data != NULL; ++t)
273 1.1 jruoho h_ctype2(t, true);
274 1.1 jruoho }
275 1.1 jruoho
276 1.1 jruoho ATF_TP_ADD_TCS(tp)
277 1.1 jruoho {
278 1.1 jruoho
279 1.1 jruoho ATF_TP_ADD_TC(tp, mbrtowc_internal);
280 1.1 jruoho ATF_TP_ADD_TC(tp, mbrtowc_object);
281 1.1 jruoho
282 1.1 jruoho return atf_no_error();
283 1.1 jruoho }
284