1 /* $NetBSD: t_mbrtowc.c,v 1.1 2011/07/15 07:35:21 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c)2003 Citrus Project,
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/cdefs.h>
59 __COPYRIGHT("@(#) Copyright (c) 2011\
60 The NetBSD Foundation, inc. All rights reserved.");
61 __RCSID("$NetBSD: t_mbrtowc.c,v 1.1 2011/07/15 07:35:21 jruoho Exp $");
78 const wchar_t wchars[64];
79 const wchar_t widths[64];
85 { 0x41, 0x42, 0x43, 0x44, 0x30, 0x31, 0x32, 0x33, 0x34, 0x5F, 0x5C },
86 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
90 "[\001\177][\302\200\337\277][\340\240\200\357\277\277][\360\220\200"
91 "\200\364\217\277\277]",
92 { 0x5b, 0x01, 0x7f, 0x5d, 0x5b, 0x80, 0x7ff, 0x5d, 0x5b, 0x800, 0xffff,
93 0x5d, 0x5b, 0x10000, 0x10ffff, 0x5d },
94 { 1, 1, 1, 1, 1, 2, 2, 1, 1, 3, 3, 1, 1, 4, 4, 1 },
98 "\033$BF|K\1348l\033(BA\033$B$\"\033(BB\033$B$$\033(B",
99 { 0x4200467c, 0x42004b5c, 0x4200386c, 0x41, 0x42002422, 0x42, 0x42002424 },
100 { 5, 2, 2, 4, 5, 4, 5 },
104 "\223\372\226{\214\352A\202\240B\202\242",
105 { 0x93fa, 0x967b, 0x8cea, 0x41, 0x82a0, 0x42, 0x82a2 },
106 { 2, 2, 2, 1, 2, 1, 2 },
110 "\306\374\313\334\270\354A\244\242B\244\244",
111 { 0xc6fc, 0xcbdc, 0xb8ec, 0x41, 0xa4a2, 0x42, 0xa4a4 },
112 { 2, 2, 2, 1, 2, 1, 2 },
124 h_ctype2(const struct test *t, bool use_mbstate)
132 ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
134 ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
136 if (setlocale(LC_CTYPE, t->locale) == NULL) {
137 fprintf(stderr, "Locale %s not found.\n", t->locale);
142 (void)strvis(buf, t->data, VIS_WHITE | VIS_OCTAL);
143 (void)printf("Checking string: \"%s\"\n", buf);
145 ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
146 (void)printf("Using locale: %s\n", str);
148 (void)printf("Using mbstate: %s\n", use_mbstate ? "yes" : "no");
150 (void)memset(&st, 0, sizeof(st));
151 // mbrtowc(0, 0, 0, &st); /* XXX for ISO2022-JP */
152 stp = use_mbstate ? &st : 0;
154 for (n = 9; n > 0; n--) {
155 const char *src = t->data;
160 ATF_REQUIRE(mbsinit(stp) != 0);
163 size_t rv = mbrtowc(&dst, src, n, stp);
168 if (rv == (size_t)-2) {
174 if (rv == (size_t)-1) {
175 ATF_REQUIRE_EQ(errno, EILSEQ);
176 atf_tc_fail("Invalid sequence");
183 if (dst != t->wchars[nchar] ||
184 width != t->widths[nchar]) {
185 (void)printf("At position %zd:\n", nchar);
186 (void)printf(" expected: 0x%04X (%u)\n",
187 t->wchars[nchar], t->widths[nchar]);
188 (void)printf(" got : 0x%04X (%u)\n",
190 atf_tc_fail("Test failed");
197 ATF_REQUIRE_EQ_MSG(dst, 0, "Incorrect terminating character: "
198 "0x%04X (expected: 0x00)", dst);
200 ATF_REQUIRE_EQ_MSG(nchar, t->length, "Incorrect length: "
201 "%zd (expected: %zd)", nchar, t->length);
207 char const *src = t->data;
210 (void)memset(wbuf, 0xFF, sizeof(wbuf));
212 rv = mbsrtowcs(wbuf, &src, SIZE, stp);
214 ATF_REQUIRE_EQ_MSG(rv, t->length, "Incorrect length: %zd "
215 "(expected: %zd)", rv, t->length);
216 ATF_REQUIRE_EQ(src, NULL);
218 for (i = 0; wbuf[i] != 0; ++i) {
219 if (wbuf[i] == t->wchars[i])
222 (void)printf("At position %d:\n", i);
223 (void)printf(" expected: 0x%04X\n", t->wchars[i]);
224 (void)printf(" got : 0x%04X\n", wbuf[i]);
225 atf_tc_fail("Test failed");
228 ATF_REQUIRE_EQ_MSG((size_t)i, t->length, "Incorrect length: "
229 "%d (expected: %zd)", i, t->length);
232 (void)printf("Ok.\n");
235 ATF_TC(mbrtowc_internal);
236 ATF_TC_HEAD(mbrtowc_internal, tc)
238 atf_tc_set_md_var(tc, "descr",
239 "Checks mbrtowc(3) and mbsrtowcs(3) (using internal "
240 "state) with different locales");
242 ATF_TC_BODY(mbrtowc_internal, tc)
247 atf_tc_expect_fail("ja_* locale fails");
249 for (t = &tests[0]; t->data != NULL; ++t)
253 ATF_TC(mbrtowc_object);
254 ATF_TC_HEAD(mbrtowc_object, tc)
256 atf_tc_set_md_var(tc, "descr",
257 "Checks mbrtowc(3) and mbsrtowcs(3) (using state "
258 "object) with different locales");
260 ATF_TC_BODY(mbrtowc_object, tc)
264 for (t = &tests[0]; t->data != NULL; ++t)
271 ATF_TP_ADD_TC(tp, mbrtowc_internal);
272 ATF_TP_ADD_TC(tp, mbrtowc_object);
274 return atf_no_error();