xref: /netbsd-src/tests/lib/libc/locale/t_btowc.c (revision bb20dbc8c83e0de7ee857f1bfde94a0ed0ff57cb)
1 /* $NetBSD: t_btowc.c,v 1.3 2017/08/10 19:08:43 perseant Exp $ */
2 
3 /*-
4  * Copyright (c) 2017 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Konrad Schroder.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31 
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2017\
34  The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_btowc.c,v 1.3 2017/08/10 19:08:43 perseant Exp $");
36 
37 #include <locale.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <wchar.h>
43 
44 #include <atf-c.h>
45 
46 struct test {
47 	const char *locale;
48 	const char *illegal; /* Illegal single-byte characters, if any */
49 	const char *legal;   /* Legal single-byte characters */
50 	/* The next two are only used if __STDC_ISO_10646__ is defined */
51 	const wchar_t wlegal[8]; /* The same characters, but in ISO-10646 */
52 	const wchar_t willegal[8]; /* ISO-10646 that do not map into charset */
53 } tests[] = {
54 	{
55 		"en_US.UTF-8",
56 		"\200",
57 		"ABC123@\t",
58 		{ 'A', 'B', 'C', '1', '2', '3', '@', '\t' },
59 		{ 0xfdd0, 0x10fffe, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
60 	},
61 	{
62                 "ru_RU.KOI8-R",
63 		"", /* No illegal characters in KOI8-R */
64                 "A\xc2\xd7\xc7\xc4\xc5\xa3",
65 		{ 'A', 0x0431, 0x432, 0x0433, 0x0434, 0x0435, 0x0451, 0x0 },
66 		{ 0x00c5, 0x00e6, 0x00fe, 0x0630, 0x06fc, 0x56cd, 0x0, 0x0 }
67 	},
68 	{
69 		NULL,
70                 NULL,
71                 NULL,
72 		{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },
73 		{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }
74 	},
75 };
76 
77 #ifdef __STDC_ISO_10646__
78 static void
h_iso10646(struct test * t)79 h_iso10646(struct test *t)
80 {
81 	const char *cp;
82 	int c, wc;
83 	char *str;
84 	const wchar_t *wcp;
85 
86 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
87 	printf("Trying locale: %s\n", t->locale);
88 	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
89 	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
90 	(void)printf("Using locale: %s\n", str);
91 
92 	/* These should have valid wchar representations */
93 	for (cp = t->legal, wcp = t->wlegal; *cp != '\0'; ++cp, ++wcp) {
94 		c = (int)(unsigned char)*cp;
95 		printf("Checking legal character 0x%x\n", c);
96 		wc = btowc(c);
97 
98 		if (errno != 0)
99 			printf(" btowc() failed with errno=%d\n", errno);
100 
101 		/* It should map to the known Unicode equivalent */
102 		printf("btowc(0x%2.2x) = 0x%x, expecting 0x%x\n",
103 		       c, wc, *wcp);
104 		ATF_REQUIRE(btowc(c) == *wcp);
105 	}
106 
107 	/* These are invalid characters in the target set */
108 	for (wcp = t->willegal; *wcp != '\0'; ++wcp) {
109 		printf("Checking illegal wide character 0x%lx\n",
110 			(unsigned long)*wcp);
111 		ATF_REQUIRE_EQ(wctob(*wcp), EOF);
112 	}
113 }
114 #endif
115 
116 static void
h_btowc(struct test * t)117 h_btowc(struct test *t)
118 {
119 	const char *cp;
120 	unsigned char c;
121 	char *str;
122 	const wchar_t *wcp;
123 
124 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
125 	printf("Trying locale: %s\n", t->locale);
126 	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
127 	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
128 	(void)printf("Using locale: %s\n", str);
129 
130 	/* btowc(EOF) -> WEOF */
131 	ATF_REQUIRE_EQ(btowc(EOF), WEOF);
132 
133 	/* wctob(WEOF) -> EOF */
134 	ATF_REQUIRE_EQ(wctob(WEOF), EOF);
135 
136 	/* Invalid in initial shift state -> WEOF */
137 	for (cp = t->illegal; *cp != '\0'; ++cp) {
138 		printf("Checking illegal character 0x%x\n",
139 			(unsigned char)*cp);
140 		ATF_REQUIRE_EQ(btowc(*cp), WEOF);
141 	}
142 
143 	/* These should have valid wchar representations */
144 	for (cp = t->legal; *cp != '\0'; ++cp) {
145 		c = (unsigned char)*cp;
146 		printf("Checking legal character 0x%x\n", c);
147 
148 		/* A legal character never maps to EOF */
149 		ATF_REQUIRE(btowc(c) != WEOF);
150 
151 		/* And the mapping should be reversible */
152 		printf("0x%x -> wide 0x%x -> 0x%x\n",
153 			c, btowc(c), (unsigned char)wctob(btowc(c)));
154 		ATF_REQUIRE_EQ(wctob(btowc(c)), c);
155 	}
156 }
157 
158 ATF_TC(btowc);
ATF_TC_HEAD(btowc,tc)159 ATF_TC_HEAD(btowc, tc)
160 {
161 	atf_tc_set_md_var(tc, "descr", "Checks btowc(3) and wctob(3)");
162 }
ATF_TC_BODY(btowc,tc)163 ATF_TC_BODY(btowc, tc)
164 {
165 	struct test *t;
166 
167 	for (t = tests; t->locale != NULL; ++t)
168 		h_btowc(t);
169 }
170 
171 ATF_TC(stdc_iso_10646);
ATF_TC_HEAD(stdc_iso_10646,tc)172 ATF_TC_HEAD(stdc_iso_10646, tc)
173 {
174 	atf_tc_set_md_var(tc, "descr",
175 		"Checks btowc(3) conversion to ISO10646");
176 }
ATF_TC_BODY(stdc_iso_10646,tc)177 ATF_TC_BODY(stdc_iso_10646, tc)
178 {
179 	struct test *t;
180 
181 #ifdef __STDC_ISO_10646__
182 	for (t = tests; t->locale != NULL; ++t)
183 		h_iso10646(t);
184 #else /* ! __STDC_ISO_10646__ */
185 	atf_tc_skip("__STDC_ISO_10646__ not defined");
186 #endif /* ! __STDC_ISO_10646__ */
187 }
188 
189 ATF_TC(btowc_posix);
ATF_TC_HEAD(btowc_posix,tc)190 ATF_TC_HEAD(btowc_posix, tc)
191 {
192 	atf_tc_set_md_var(tc, "descr", "Checks btowc(3) and wctob(3) for POSIX locale");
193 }
ATF_TC_BODY(btowc_posix,tc)194 ATF_TC_BODY(btowc_posix, tc)
195 {
196 	const char *cp;
197 	unsigned char c;
198 	char *str;
199 	const wchar_t *wcp;
200 	int i;
201 
202 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "POSIX"), "POSIX");
203 
204 	/* btowc(EOF) -> WEOF */
205 	ATF_REQUIRE_EQ(btowc(EOF), WEOF);
206 
207 	/* wctob(WEOF) -> EOF */
208 	ATF_REQUIRE_EQ(wctob(WEOF), EOF);
209 
210 	/* All characters from 0 to 255, inclusive, map
211 	   onto their unsigned char equivalent */
212 	for (i = 0; i <= 255; i++) {
213 		ATF_REQUIRE_EQ(btowc(i), (wchar_t)(unsigned char)(i));
214 		ATF_REQUIRE_EQ((unsigned char)wctob(i), (wchar_t)i);
215 	}
216 }
217 
ATF_TP_ADD_TCS(tp)218 ATF_TP_ADD_TCS(tp)
219 {
220 	ATF_TP_ADD_TC(tp, btowc);
221 	ATF_TP_ADD_TC(tp, btowc_posix);
222 	ATF_TP_ADD_TC(tp, stdc_iso_10646);
223 
224 	return atf_no_error();
225 }
226