1*bb20dbc8Sperseant /* $NetBSD: t_btowc.c,v 1.3 2017/08/10 19:08:43 perseant Exp $ */
2d4f6523bSperseant
3d4f6523bSperseant /*-
4d4f6523bSperseant * Copyright (c) 2017 The NetBSD Foundation, Inc.
5d4f6523bSperseant * All rights reserved.
6d4f6523bSperseant *
7d4f6523bSperseant * This code is derived from software contributed to The NetBSD Foundation
8d4f6523bSperseant * by Konrad Schroder.
9d4f6523bSperseant *
10d4f6523bSperseant * Redistribution and use in source and binary forms, with or without
11d4f6523bSperseant * modification, are permitted provided that the following conditions
12d4f6523bSperseant * are met:
13d4f6523bSperseant * 1. Redistributions of source code must retain the above copyright
14d4f6523bSperseant * notice, this list of conditions and the following disclaimer.
15d4f6523bSperseant * 2. Redistributions in binary form must reproduce the above copyright
16d4f6523bSperseant * notice, this list of conditions and the following disclaimer in the
17d4f6523bSperseant * documentation and/or other materials provided with the distribution.
18d4f6523bSperseant *
19d4f6523bSperseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d4f6523bSperseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d4f6523bSperseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d4f6523bSperseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d4f6523bSperseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d4f6523bSperseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d4f6523bSperseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d4f6523bSperseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d4f6523bSperseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d4f6523bSperseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d4f6523bSperseant * POSSIBILITY OF SUCH DAMAGE.
30d4f6523bSperseant */
31d4f6523bSperseant
32d4f6523bSperseant #include <sys/cdefs.h>
33d4f6523bSperseant __COPYRIGHT("@(#) Copyright (c) 2017\
34d4f6523bSperseant The NetBSD Foundation, inc. All rights reserved.");
35*bb20dbc8Sperseant __RCSID("$NetBSD: t_btowc.c,v 1.3 2017/08/10 19:08:43 perseant Exp $");
36d4f6523bSperseant
37d4f6523bSperseant #include <locale.h>
38d4f6523bSperseant #include <stdio.h>
39d4f6523bSperseant #include <stdlib.h>
40adbde1f9Sperseant #include <errno.h>
41d4f6523bSperseant #include <string.h>
42d4f6523bSperseant #include <wchar.h>
43d4f6523bSperseant
44d4f6523bSperseant #include <atf-c.h>
45d4f6523bSperseant
46d4f6523bSperseant struct test {
47d4f6523bSperseant const char *locale;
48d4f6523bSperseant const char *illegal; /* Illegal single-byte characters, if any */
49d4f6523bSperseant const char *legal; /* Legal single-byte characters */
50d4f6523bSperseant /* The next two are only used if __STDC_ISO_10646__ is defined */
51d4f6523bSperseant const wchar_t wlegal[8]; /* The same characters, but in ISO-10646 */
52d4f6523bSperseant const wchar_t willegal[8]; /* ISO-10646 that do not map into charset */
53d4f6523bSperseant } tests[] = {
54d4f6523bSperseant {
55d4f6523bSperseant "en_US.UTF-8",
56d4f6523bSperseant "\200",
57d4f6523bSperseant "ABC123@\t",
58d4f6523bSperseant { 'A', 'B', 'C', '1', '2', '3', '@', '\t' },
59d4f6523bSperseant { 0xfdd0, 0x10fffe, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
60d4f6523bSperseant },
61d4f6523bSperseant {
62d4f6523bSperseant "ru_RU.KOI8-R",
63d4f6523bSperseant "", /* No illegal characters in KOI8-R */
64d4f6523bSperseant "A\xc2\xd7\xc7\xc4\xc5\xa3",
65d4f6523bSperseant { 'A', 0x0431, 0x432, 0x0433, 0x0434, 0x0435, 0x0451, 0x0 },
66d4f6523bSperseant { 0x00c5, 0x00e6, 0x00fe, 0x0630, 0x06fc, 0x56cd, 0x0, 0x0 }
67d4f6523bSperseant },
68d4f6523bSperseant {
69d4f6523bSperseant NULL,
70d4f6523bSperseant NULL,
71d4f6523bSperseant NULL,
72d4f6523bSperseant { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },
73d4f6523bSperseant { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }
74d4f6523bSperseant },
75d4f6523bSperseant };
76d4f6523bSperseant
77d4f6523bSperseant #ifdef __STDC_ISO_10646__
78d4f6523bSperseant static void
h_iso10646(struct test * t)79d4f6523bSperseant h_iso10646(struct test *t)
80d4f6523bSperseant {
81d4f6523bSperseant const char *cp;
82adbde1f9Sperseant int c, wc;
83d4f6523bSperseant char *str;
84d4f6523bSperseant const wchar_t *wcp;
85d4f6523bSperseant
86adbde1f9Sperseant ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
87adbde1f9Sperseant printf("Trying locale: %s\n", t->locale);
88adbde1f9Sperseant ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
89adbde1f9Sperseant ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
90adbde1f9Sperseant (void)printf("Using locale: %s\n", str);
91adbde1f9Sperseant
92d4f6523bSperseant /* These should have valid wchar representations */
93d4f6523bSperseant for (cp = t->legal, wcp = t->wlegal; *cp != '\0'; ++cp, ++wcp) {
94adbde1f9Sperseant c = (int)(unsigned char)*cp;
95d4f6523bSperseant printf("Checking legal character 0x%x\n", c);
96adbde1f9Sperseant wc = btowc(c);
97adbde1f9Sperseant
98adbde1f9Sperseant if (errno != 0)
99adbde1f9Sperseant printf(" btowc() failed with errno=%d\n", errno);
100d4f6523bSperseant
101d4f6523bSperseant /* It should map to the known Unicode equivalent */
102d4f6523bSperseant printf("btowc(0x%2.2x) = 0x%x, expecting 0x%x\n",
103adbde1f9Sperseant c, wc, *wcp);
104d4f6523bSperseant ATF_REQUIRE(btowc(c) == *wcp);
105d4f6523bSperseant }
106d4f6523bSperseant
107d4f6523bSperseant /* These are invalid characters in the target set */
108d4f6523bSperseant for (wcp = t->willegal; *wcp != '\0'; ++wcp) {
109d4f6523bSperseant printf("Checking illegal wide character 0x%lx\n",
110d4f6523bSperseant (unsigned long)*wcp);
111d4f6523bSperseant ATF_REQUIRE_EQ(wctob(*wcp), EOF);
112d4f6523bSperseant }
113d4f6523bSperseant }
114d4f6523bSperseant #endif
115d4f6523bSperseant
116d4f6523bSperseant static void
h_btowc(struct test * t)117d4f6523bSperseant h_btowc(struct test *t)
118d4f6523bSperseant {
119d4f6523bSperseant const char *cp;
120d4f6523bSperseant unsigned char c;
121d4f6523bSperseant char *str;
122d4f6523bSperseant const wchar_t *wcp;
123d4f6523bSperseant
124d4f6523bSperseant ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
125d4f6523bSperseant printf("Trying locale: %s\n", t->locale);
126d4f6523bSperseant ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
127adbde1f9Sperseant ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
128adbde1f9Sperseant (void)printf("Using locale: %s\n", str);
129d4f6523bSperseant
130d4f6523bSperseant /* btowc(EOF) -> WEOF */
131d4f6523bSperseant ATF_REQUIRE_EQ(btowc(EOF), WEOF);
132d4f6523bSperseant
133d4f6523bSperseant /* wctob(WEOF) -> EOF */
134d4f6523bSperseant ATF_REQUIRE_EQ(wctob(WEOF), EOF);
135d4f6523bSperseant
136d4f6523bSperseant /* Invalid in initial shift state -> WEOF */
137d4f6523bSperseant for (cp = t->illegal; *cp != '\0'; ++cp) {
138d4f6523bSperseant printf("Checking illegal character 0x%x\n",
139d4f6523bSperseant (unsigned char)*cp);
140d4f6523bSperseant ATF_REQUIRE_EQ(btowc(*cp), WEOF);
141d4f6523bSperseant }
142d4f6523bSperseant
143d4f6523bSperseant /* These should have valid wchar representations */
144d4f6523bSperseant for (cp = t->legal; *cp != '\0'; ++cp) {
145d4f6523bSperseant c = (unsigned char)*cp;
146d4f6523bSperseant printf("Checking legal character 0x%x\n", c);
147d4f6523bSperseant
148d4f6523bSperseant /* A legal character never maps to EOF */
149d4f6523bSperseant ATF_REQUIRE(btowc(c) != WEOF);
150d4f6523bSperseant
151d4f6523bSperseant /* And the mapping should be reversible */
152d4f6523bSperseant printf("0x%x -> wide 0x%x -> 0x%x\n",
153d4f6523bSperseant c, btowc(c), (unsigned char)wctob(btowc(c)));
154d4f6523bSperseant ATF_REQUIRE_EQ(wctob(btowc(c)), c);
155d4f6523bSperseant }
156d4f6523bSperseant }
157d4f6523bSperseant
158d4f6523bSperseant ATF_TC(btowc);
ATF_TC_HEAD(btowc,tc)159d4f6523bSperseant ATF_TC_HEAD(btowc, tc)
160d4f6523bSperseant {
161d4f6523bSperseant atf_tc_set_md_var(tc, "descr", "Checks btowc(3) and wctob(3)");
162d4f6523bSperseant }
ATF_TC_BODY(btowc,tc)163d4f6523bSperseant ATF_TC_BODY(btowc, tc)
164d4f6523bSperseant {
165d4f6523bSperseant struct test *t;
166d4f6523bSperseant
167d4f6523bSperseant for (t = tests; t->locale != NULL; ++t)
168d4f6523bSperseant h_btowc(t);
169d4f6523bSperseant }
170d4f6523bSperseant
171d4f6523bSperseant ATF_TC(stdc_iso_10646);
ATF_TC_HEAD(stdc_iso_10646,tc)172d4f6523bSperseant ATF_TC_HEAD(stdc_iso_10646, tc)
173d4f6523bSperseant {
174d4f6523bSperseant atf_tc_set_md_var(tc, "descr",
175d4f6523bSperseant "Checks btowc(3) conversion to ISO10646");
176d4f6523bSperseant }
ATF_TC_BODY(stdc_iso_10646,tc)177d4f6523bSperseant ATF_TC_BODY(stdc_iso_10646, tc)
178d4f6523bSperseant {
179d4f6523bSperseant struct test *t;
180d4f6523bSperseant
181d4f6523bSperseant #ifdef __STDC_ISO_10646__
182d4f6523bSperseant for (t = tests; t->locale != NULL; ++t)
183d4f6523bSperseant h_iso10646(t);
184d4f6523bSperseant #else /* ! __STDC_ISO_10646__ */
185d4f6523bSperseant atf_tc_skip("__STDC_ISO_10646__ not defined");
186d4f6523bSperseant #endif /* ! __STDC_ISO_10646__ */
187d4f6523bSperseant }
188d4f6523bSperseant
189*bb20dbc8Sperseant ATF_TC(btowc_posix);
ATF_TC_HEAD(btowc_posix,tc)190*bb20dbc8Sperseant ATF_TC_HEAD(btowc_posix, tc)
191*bb20dbc8Sperseant {
192*bb20dbc8Sperseant atf_tc_set_md_var(tc, "descr", "Checks btowc(3) and wctob(3) for POSIX locale");
193*bb20dbc8Sperseant }
ATF_TC_BODY(btowc_posix,tc)194*bb20dbc8Sperseant ATF_TC_BODY(btowc_posix, tc)
195*bb20dbc8Sperseant {
196*bb20dbc8Sperseant const char *cp;
197*bb20dbc8Sperseant unsigned char c;
198*bb20dbc8Sperseant char *str;
199*bb20dbc8Sperseant const wchar_t *wcp;
200*bb20dbc8Sperseant int i;
201*bb20dbc8Sperseant
202*bb20dbc8Sperseant ATF_REQUIRE_STREQ(setlocale(LC_ALL, "POSIX"), "POSIX");
203*bb20dbc8Sperseant
204*bb20dbc8Sperseant /* btowc(EOF) -> WEOF */
205*bb20dbc8Sperseant ATF_REQUIRE_EQ(btowc(EOF), WEOF);
206*bb20dbc8Sperseant
207*bb20dbc8Sperseant /* wctob(WEOF) -> EOF */
208*bb20dbc8Sperseant ATF_REQUIRE_EQ(wctob(WEOF), EOF);
209*bb20dbc8Sperseant
210*bb20dbc8Sperseant /* All characters from 0 to 255, inclusive, map
211*bb20dbc8Sperseant onto their unsigned char equivalent */
212*bb20dbc8Sperseant for (i = 0; i <= 255; i++) {
213*bb20dbc8Sperseant ATF_REQUIRE_EQ(btowc(i), (wchar_t)(unsigned char)(i));
214*bb20dbc8Sperseant ATF_REQUIRE_EQ((unsigned char)wctob(i), (wchar_t)i);
215*bb20dbc8Sperseant }
216*bb20dbc8Sperseant }
217*bb20dbc8Sperseant
ATF_TP_ADD_TCS(tp)218d4f6523bSperseant ATF_TP_ADD_TCS(tp)
219d4f6523bSperseant {
220d4f6523bSperseant ATF_TP_ADD_TC(tp, btowc);
221*bb20dbc8Sperseant ATF_TP_ADD_TC(tp, btowc_posix);
222d4f6523bSperseant ATF_TP_ADD_TC(tp, stdc_iso_10646);
223d4f6523bSperseant
224d4f6523bSperseant return atf_no_error();
225d4f6523bSperseant }
226