xref: /netbsd-src/tests/lib/libc/locale/t_btowc.c (revision adbde1f90007dcbbba222bd12b3ad39e535dc73d)
1 /* $NetBSD: t_btowc.c,v 1.2 2017/07/12 17:32:51 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.2 2017/07/12 17:32:51 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 		"C",
56 		"\377",
57 		"ABC123@\t",
58 		{ 'A', 'B', 'C', '1', '2', '3', '@', '\t' },
59 		{ 0x0430, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
60 	},
61 	{
62 		"en_US.UTF-8",
63 		"\200",
64 		"ABC123@\t",
65 		{ 'A', 'B', 'C', '1', '2', '3', '@', '\t' },
66 		{ 0xfdd0, 0x10fffe, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
67 	},
68 	{
69                 "ru_RU.KOI8-R",
70 		"", /* No illegal characters in KOI8-R */
71                 "A\xc2\xd7\xc7\xc4\xc5\xa3",
72 		{ 'A', 0x0431, 0x432, 0x0433, 0x0434, 0x0435, 0x0451, 0x0 },
73 		{ 0x00c5, 0x00e6, 0x00fe, 0x0630, 0x06fc, 0x56cd, 0x0, 0x0 }
74 	},
75 	{
76 		NULL,
77                 NULL,
78                 NULL,
79 		{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },
80 		{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }
81 	},
82 };
83 
84 #ifdef __STDC_ISO_10646__
85 static void
86 h_iso10646(struct test *t)
87 {
88 	const char *cp;
89 	int c, wc;
90 	char *str;
91 	const wchar_t *wcp;
92 
93 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
94 	printf("Trying locale: %s\n", t->locale);
95 	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
96 	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
97 	(void)printf("Using locale: %s\n", str);
98 
99 	/* These should have valid wchar representations */
100 	for (cp = t->legal, wcp = t->wlegal; *cp != '\0'; ++cp, ++wcp) {
101 		c = (int)(unsigned char)*cp;
102 		printf("Checking legal character 0x%x\n", c);
103 		wc = btowc(c);
104 
105 		if (errno != 0)
106 			printf(" btowc() failed with errno=%d\n", errno);
107 
108 		/* It should map to the known Unicode equivalent */
109 		printf("btowc(0x%2.2x) = 0x%x, expecting 0x%x\n",
110 		       c, wc, *wcp);
111 		ATF_REQUIRE(btowc(c) == *wcp);
112 	}
113 
114 	/* These are invalid characters in the target set */
115 	for (wcp = t->willegal; *wcp != '\0'; ++wcp) {
116 		printf("Checking illegal wide character 0x%lx\n",
117 			(unsigned long)*wcp);
118 		ATF_REQUIRE_EQ(wctob(*wcp), EOF);
119 	}
120 }
121 #endif
122 
123 static void
124 h_btowc(struct test *t)
125 {
126 	const char *cp;
127 	unsigned char c;
128 	char *str;
129 	const wchar_t *wcp;
130 
131 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
132 	printf("Trying locale: %s\n", t->locale);
133 	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
134 	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
135 	(void)printf("Using locale: %s\n", str);
136 
137 	/* btowc(EOF) -> WEOF */
138 	ATF_REQUIRE_EQ(btowc(EOF), WEOF);
139 
140 	/* wctob(WEOF) -> EOF */
141 	ATF_REQUIRE_EQ(wctob(WEOF), EOF);
142 
143 	/* Invalid in initial shift state -> WEOF */
144 	for (cp = t->illegal; *cp != '\0'; ++cp) {
145 		printf("Checking illegal character 0x%x\n",
146 			(unsigned char)*cp);
147 		ATF_REQUIRE_EQ(btowc(*cp), WEOF);
148 	}
149 
150 	/* These should have valid wchar representations */
151 	for (cp = t->legal; *cp != '\0'; ++cp) {
152 		c = (unsigned char)*cp;
153 		printf("Checking legal character 0x%x\n", c);
154 
155 		/* A legal character never maps to EOF */
156 		ATF_REQUIRE(btowc(c) != WEOF);
157 
158 		/* And the mapping should be reversible */
159 		printf("0x%x -> wide 0x%x -> 0x%x\n",
160 			c, btowc(c), (unsigned char)wctob(btowc(c)));
161 		ATF_REQUIRE_EQ(wctob(btowc(c)), c);
162 	}
163 }
164 
165 ATF_TC(btowc);
166 ATF_TC_HEAD(btowc, tc)
167 {
168 	atf_tc_set_md_var(tc, "descr", "Checks btowc(3) and wctob(3)");
169 }
170 ATF_TC_BODY(btowc, tc)
171 {
172 	struct test *t;
173 
174 	for (t = tests; t->locale != NULL; ++t)
175 		h_btowc(t);
176 }
177 
178 ATF_TC(stdc_iso_10646);
179 ATF_TC_HEAD(stdc_iso_10646, tc)
180 {
181 	atf_tc_set_md_var(tc, "descr",
182 		"Checks btowc(3) conversion to ISO10646");
183 }
184 ATF_TC_BODY(stdc_iso_10646, tc)
185 {
186 	struct test *t;
187 
188 #ifdef __STDC_ISO_10646__
189 	for (t = tests; t->locale != NULL; ++t)
190 		h_iso10646(t);
191 #else /* ! __STDC_ISO_10646__ */
192 	atf_tc_skip("__STDC_ISO_10646__ not defined");
193 #endif /* ! __STDC_ISO_10646__ */
194 }
195 
196 ATF_TP_ADD_TCS(tp)
197 {
198 	ATF_TP_ADD_TC(tp, btowc);
199 	ATF_TP_ADD_TC(tp, stdc_iso_10646);
200 
201 	return atf_no_error();
202 }
203