xref: /minix/tests/lib/libc/locale/t_wctomb.c (revision 11be35a1)
1*11be35a1SLionel Sambuc /* $NetBSD: t_wctomb.c,v 1.3 2013/03/25 15:31:03 gson Exp $ */
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc  * are met:
10*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc  *
16*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*11be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*11be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*11be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*11be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*11be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*11be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc  */
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc /*-
30*11be35a1SLionel Sambuc  * Copyright (c)2004 Citrus Project,
31*11be35a1SLionel Sambuc  * All rights reserved.
32*11be35a1SLionel Sambuc  *
33*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
34*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
35*11be35a1SLionel Sambuc  * are met:
36*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
37*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
38*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
39*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
40*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
41*11be35a1SLionel Sambuc  *
42*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43*11be35a1SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44*11be35a1SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45*11be35a1SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46*11be35a1SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47*11be35a1SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48*11be35a1SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49*11be35a1SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50*11be35a1SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51*11be35a1SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52*11be35a1SLionel Sambuc  * SUCH DAMAGE.
53*11be35a1SLionel Sambuc  */
54*11be35a1SLionel Sambuc 
55*11be35a1SLionel Sambuc #include <sys/cdefs.h>
56*11be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2011\
57*11be35a1SLionel Sambuc  The NetBSD Foundation, inc. All rights reserved.");
58*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_wctomb.c,v 1.3 2013/03/25 15:31:03 gson Exp $");
59*11be35a1SLionel Sambuc 
60*11be35a1SLionel Sambuc #include <stdio.h>
61*11be35a1SLionel Sambuc #include <stdlib.h>
62*11be35a1SLionel Sambuc #include <locale.h>
63*11be35a1SLionel Sambuc #include <vis.h>
64*11be35a1SLionel Sambuc #include <wchar.h>
65*11be35a1SLionel Sambuc #include <string.h>
66*11be35a1SLionel Sambuc #include <limits.h>
67*11be35a1SLionel Sambuc 
68*11be35a1SLionel Sambuc #include <atf-c.h>
69*11be35a1SLionel Sambuc 
70*11be35a1SLionel Sambuc #define TC_WCTOMB	0
71*11be35a1SLionel Sambuc #define TC_WCRTOMB	1
72*11be35a1SLionel Sambuc #define TC_WCRTOMB_ST	2
73*11be35a1SLionel Sambuc 
74*11be35a1SLionel Sambuc static struct test {
75*11be35a1SLionel Sambuc 	const char *locale;
76*11be35a1SLionel Sambuc 	const char *data;
77*11be35a1SLionel Sambuc 	size_t wclen;
78*11be35a1SLionel Sambuc 	size_t mblen[16];
79*11be35a1SLionel Sambuc } tests[] = {
80*11be35a1SLionel Sambuc {
81*11be35a1SLionel Sambuc 	"ja_JP.ISO2022-JP",
82*11be35a1SLionel Sambuc 	"\x1b$B"	/* JIS X 0208-1983 */
83*11be35a1SLionel Sambuc 	"\x46\x7c\x4b\x5c\x38\x6c" /* "nihongo" */
84*11be35a1SLionel Sambuc 	"\x1b(B"	/* ISO 646 */
85*11be35a1SLionel Sambuc 	"ABC"
86*11be35a1SLionel Sambuc 	"\x1b(I"	/* JIS X 0201 katakana */
87*11be35a1SLionel Sambuc 	"\xb1\xb2\xb3"	/* "aiu" */
88*11be35a1SLionel Sambuc 	"\x1b(B",	/* ISO 646 */
89*11be35a1SLionel Sambuc 	3 + 3 + 3,
90*11be35a1SLionel Sambuc 	{ 3+2, 2, 2, 3+1, 1, 1, 3+1, 1, 1, 3+1 }
91*11be35a1SLionel Sambuc }, {
92*11be35a1SLionel Sambuc 	"C",
93*11be35a1SLionel Sambuc 	"ABC",
94*11be35a1SLionel Sambuc 	3,
95*11be35a1SLionel Sambuc 	{ 1, 1, 1, 1 }
96*11be35a1SLionel Sambuc }, { NULL, NULL, 0, { } }
97*11be35a1SLionel Sambuc };
98*11be35a1SLionel Sambuc 
99*11be35a1SLionel Sambuc static void
h_wctomb(const struct test * t,char tc)100*11be35a1SLionel Sambuc h_wctomb(const struct test *t, char tc)
101*11be35a1SLionel Sambuc {
102*11be35a1SLionel Sambuc 	wchar_t wcs[16 + 2];
103*11be35a1SLionel Sambuc 	char buf[128];
104*11be35a1SLionel Sambuc 	char cs[MB_LEN_MAX];
105*11be35a1SLionel Sambuc 	const char *pcs;
106*11be35a1SLionel Sambuc 	char *str;
107*11be35a1SLionel Sambuc 	mbstate_t st;
108*11be35a1SLionel Sambuc 	mbstate_t *stp = NULL;
109*11be35a1SLionel Sambuc 	size_t sz, ret, i;
110*11be35a1SLionel Sambuc 
111*11be35a1SLionel Sambuc 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
112*11be35a1SLionel Sambuc 	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
113*11be35a1SLionel Sambuc 
114*11be35a1SLionel Sambuc 	(void)strvis(buf, t->data, VIS_WHITE | VIS_OCTAL);
115*11be35a1SLionel Sambuc 	(void)printf("Checking sequence: \"%s\"\n", buf);
116*11be35a1SLionel Sambuc 
117*11be35a1SLionel Sambuc 	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
118*11be35a1SLionel Sambuc 	(void)printf("Using locale: %s\n", str);
119*11be35a1SLionel Sambuc 
120*11be35a1SLionel Sambuc 	if (tc == TC_WCRTOMB_ST) {
121*11be35a1SLionel Sambuc 		(void)memset(&st, 0, sizeof(st));
122*11be35a1SLionel Sambuc 		stp = &st;
123*11be35a1SLionel Sambuc 	}
124*11be35a1SLionel Sambuc 
125*11be35a1SLionel Sambuc 	wcs[t->wclen] = L'X'; /* poison */
126*11be35a1SLionel Sambuc 	pcs = t->data;
127*11be35a1SLionel Sambuc 	sz = mbsrtowcs(wcs, &pcs, t->wclen + 2, NULL);
128*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ_MSG(sz, t->wclen, "mbsrtowcs() returned: "
129*11be35a1SLionel Sambuc 		"%zu, expected: %zu", sz, t->wclen);
130*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(wcs[t->wclen], 0);
131*11be35a1SLionel Sambuc 
132*11be35a1SLionel Sambuc 	for (i = 0; i < t->wclen + 1; i++) {
133*11be35a1SLionel Sambuc 		if (tc == TC_WCTOMB)
134*11be35a1SLionel Sambuc 			ret = wctomb(cs, wcs[i]);
135*11be35a1SLionel Sambuc 		else
136*11be35a1SLionel Sambuc 			ret = wcrtomb(cs, wcs[i], stp);
137*11be35a1SLionel Sambuc 
138*11be35a1SLionel Sambuc 		if (ret == t->mblen[i])
139*11be35a1SLionel Sambuc 			continue;
140*11be35a1SLionel Sambuc 
141*11be35a1SLionel Sambuc 		(void)printf("At position %zd:\n", i);
142*11be35a1SLionel Sambuc 		(void)printf("  expected: %zd\n", t->mblen[i]);
143*11be35a1SLionel Sambuc 		(void)printf("  got     : %zd\n", ret);
144*11be35a1SLionel Sambuc 		atf_tc_fail("Test failed");
145*11be35a1SLionel Sambuc 		/* NOTREACHED */
146*11be35a1SLionel Sambuc 	}
147*11be35a1SLionel Sambuc 
148*11be35a1SLionel Sambuc 	(void)printf("Ok.\n");
149*11be35a1SLionel Sambuc }
150*11be35a1SLionel Sambuc 
151*11be35a1SLionel Sambuc ATF_TC(wctomb);
ATF_TC_HEAD(wctomb,tc)152*11be35a1SLionel Sambuc ATF_TC_HEAD(wctomb, tc)
153*11be35a1SLionel Sambuc {
154*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks wctomb(3)");
155*11be35a1SLionel Sambuc }
ATF_TC_BODY(wctomb,tc)156*11be35a1SLionel Sambuc ATF_TC_BODY(wctomb, tc)
157*11be35a1SLionel Sambuc {
158*11be35a1SLionel Sambuc 	struct test *t;
159*11be35a1SLionel Sambuc 
160*11be35a1SLionel Sambuc 	(void)printf("Checking wctomb()\n");
161*11be35a1SLionel Sambuc 
162*11be35a1SLionel Sambuc 	for (t = &tests[0]; t->data != NULL; ++t)
163*11be35a1SLionel Sambuc 		h_wctomb(t, TC_WCTOMB);
164*11be35a1SLionel Sambuc }
165*11be35a1SLionel Sambuc 
166*11be35a1SLionel Sambuc ATF_TC(wcrtomb_state);
ATF_TC_HEAD(wcrtomb_state,tc)167*11be35a1SLionel Sambuc ATF_TC_HEAD(wcrtomb_state, tc)
168*11be35a1SLionel Sambuc {
169*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
170*11be35a1SLionel Sambuc 		"Checks wcrtomb(3) (using state object)");
171*11be35a1SLionel Sambuc }
ATF_TC_BODY(wcrtomb_state,tc)172*11be35a1SLionel Sambuc ATF_TC_BODY(wcrtomb_state, tc)
173*11be35a1SLionel Sambuc {
174*11be35a1SLionel Sambuc 	struct test *t;
175*11be35a1SLionel Sambuc 
176*11be35a1SLionel Sambuc 	(void)printf("Checking wcrtomb() (with state object)\n");
177*11be35a1SLionel Sambuc 
178*11be35a1SLionel Sambuc 	for (t = &tests[0]; t->data != NULL; ++t)
179*11be35a1SLionel Sambuc 		h_wctomb(t, TC_WCRTOMB_ST);
180*11be35a1SLionel Sambuc }
181*11be35a1SLionel Sambuc 
182*11be35a1SLionel Sambuc ATF_TC(wcrtomb);
ATF_TC_HEAD(wcrtomb,tc)183*11be35a1SLionel Sambuc ATF_TC_HEAD(wcrtomb, tc)
184*11be35a1SLionel Sambuc {
185*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
186*11be35a1SLionel Sambuc 		"Checks wcrtomb(3) (using internal state)");
187*11be35a1SLionel Sambuc }
ATF_TC_BODY(wcrtomb,tc)188*11be35a1SLionel Sambuc ATF_TC_BODY(wcrtomb, tc)
189*11be35a1SLionel Sambuc {
190*11be35a1SLionel Sambuc 	struct test *t;
191*11be35a1SLionel Sambuc 
192*11be35a1SLionel Sambuc 	(void)printf("Checking wcrtomb() (using internal state)\n");
193*11be35a1SLionel Sambuc 
194*11be35a1SLionel Sambuc 	for (t = &tests[0]; t->data != NULL; ++t)
195*11be35a1SLionel Sambuc 		h_wctomb(t, TC_WCRTOMB);
196*11be35a1SLionel Sambuc }
197*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)198*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
199*11be35a1SLionel Sambuc {
200*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, wctomb);
201*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, wcrtomb);
202*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, wcrtomb_state);
203*11be35a1SLionel Sambuc 
204*11be35a1SLionel Sambuc 	return atf_no_error();
205*11be35a1SLionel Sambuc }
206