xref: /netbsd-src/tests/lib/libc/locale/t_mbtowc.c (revision 2ba250a115139f47b3ba51731cb0450c8a7dccf6)
1 /* $NetBSD: t_mbtowc.c,v 1.3 2020/06/30 16:09:40 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*-
30  * Copyright (c)2007 Citrus Project,
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  *
54  */
55 
56 /*-
57  * Copyright (c) 2005 Miloslav Trmac
58  * All rights reserved.
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63 
64  * 1. Redistributions of source code must retain the above copyright
65  *    notice, this list of conditions and the following disclaimer.
66  * 2. Redistributions in binary form must reproduce the above copyright
67  *    notice, this list of conditions and the following disclaimer in the
68  *    documentation and/or other materials provided with the distribution.
69  *
70  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
71  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
72  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
73  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
74  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
75  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
76  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
78  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
79  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
80  * POSSIBILITY OF SUCH DAMAGE.
81  */
82 
83 #include <sys/cdefs.h>
84 __COPYRIGHT("@(#) Copyright (c) 2011\
85  The NetBSD Foundation, inc. All rights reserved.");
86 __RCSID("$NetBSD: t_mbtowc.c,v 1.3 2020/06/30 16:09:40 jruoho Exp $");
87 
88 #include <errno.h>
89 #include <langinfo.h>
90 #include <locale.h>
91 #include <limits.h>
92 #include <stdio.h>
93 #include <stdlib.h>
94 #include <string.h>
95 #include <vis.h>
96 #include <wchar.h>
97 
98 #include <atf-c.h>
99 
100 static void
h_mbtowc(const char * locale,const char * illegal,const char * legal,size_t stateful)101 h_mbtowc(const char *locale, const char *illegal,
102     const char *legal, size_t stateful)
103 {
104 	char buf[64];
105 	size_t ret;
106 	char *str;
107 
108 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
109 	(void)printf("Trying locale: %s\n", locale);
110 	ATF_REQUIRE(setlocale(LC_CTYPE, locale) != NULL);
111 
112 	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
113 	(void)printf("Using locale: %s\n", str);
114 	(void)printf("Locale is state-%sdependent\n",
115 		!stateful ? "in" : "");
116 
117 	/* initialize internal state */
118 	ret = mbtowc(NULL, NULL, 0);
119 	ATF_REQUIRE(stateful ? ret : !ret);
120 
121 	(void)strvis(buf, illegal, VIS_WHITE | VIS_OCTAL);
122 	(void)printf("Checking illegal sequence: \"%s\"\n", buf);
123 
124 	ret = mbtowc(NULL, illegal, strlen(illegal));
125 	(void)printf("mbtowc() returned: %zd\n", ret);
126 	ATF_REQUIRE_EQ(ret, (size_t)-1);
127 	(void)printf("errno: %s\n", strerror(errno));
128 	ATF_REQUIRE_EQ(errno, EILSEQ);
129 
130 	/*
131 	 * If this is stateless encoding, this
132 	 * re-initialization is not required.
133 	 */
134 	if (stateful) {
135 		/* re-initialize internal state */
136 		mbtowc(NULL, NULL, 0);
137 	}
138 
139 	/* valid multibyte sequence case */
140 	(void)strvis(buf, legal, VIS_WHITE | VIS_OCTAL);
141 	(void)printf("Checking legal sequence: \"%s\"\n", buf);
142 
143 	errno = 0;
144 	ret = mbtowc(NULL, legal, strlen(legal));
145 	(void)printf("mbtowc() returned: %zd\n", ret);
146 	ATF_REQUIRE(ret != (size_t)-1);
147 	(void)printf("errno: %s\n", strerror(errno));
148 	ATF_REQUIRE_EQ(errno, 0);
149 
150 	(void)printf("Ok.\n");
151 }
152 
153 ATF_TC(mbtowc_basic);
ATF_TC_HEAD(mbtowc_basic,tc)154 ATF_TC_HEAD(mbtowc_basic, tc)
155 {
156 	atf_tc_set_md_var(tc, "descr", "A basic test of mbtowc(3)");
157 }
158 
ATF_TC_BODY(mbtowc_basic,tc)159 ATF_TC_BODY(mbtowc_basic, tc)
160 {
161 	h_mbtowc("en_US.UTF-8", "\240", "\302\240", 0);
162 	h_mbtowc("ja_JP.ISO2022-JP", "\033$B", "\033$B$\"\033(B", 1);
163 	h_mbtowc("ja_JP.SJIS", "\202", "\202\240", 0);
164 	h_mbtowc("ja_JP.eucJP", "\244", "\244\242", 0);
165 	h_mbtowc("zh_CN.GB18030", "\241", "\241\241", 0);
166 	h_mbtowc("zh_TW.Big5", "\241", "\241@", 0);
167 	h_mbtowc("zh_TW.eucTW", "\241", "\241\241", 0);
168 }
169 
170 ATF_TC(mbtowc_sign);
ATF_TC_HEAD(mbtowc_sign,tc)171 ATF_TC_HEAD(mbtowc_sign, tc)
172 {
173 	atf_tc_set_md_var(tc, "descr", "Test mbtowc(3) sign conversion");
174 }
175 
ATF_TC_BODY(mbtowc_sign,tc)176 ATF_TC_BODY(mbtowc_sign, tc)
177 {
178 	char back[MB_LEN_MAX];
179 	wchar_t wc;
180 	size_t i;
181 	int ret;
182 
183 	(void)setlocale(LC_ALL, "");
184 	(void)printf("Charset: %s\n", nl_langinfo(CODESET));
185 	ret = mbtowc(&wc, "\xe4", 1);
186 	(void)printf("mbtowc(): %d\n", ret);
187 
188 	if (ret > 0) {
189 		(void)printf("Result: 0x%08lX\n",(unsigned long)wc);
190 		ret = wctomb(back, wc);
191 		(void)printf("wctomb(): %d\n", ret);
192 		for(i = 0; ret > 0 && i < (size_t)ret; i++)
193 			printf("%02X ",(unsigned char)back[i]);
194 		putchar('\n');
195 	}
196 
197 	ATF_REQUIRE(ret > 0);
198 }
199 
ATF_TP_ADD_TCS(tp)200 ATF_TP_ADD_TCS(tp)
201 {
202 
203 	ATF_TP_ADD_TC(tp, mbtowc_basic);
204 	ATF_TP_ADD_TC(tp, mbtowc_sign);
205 
206 	return atf_no_error();
207 }
208