t_btowc.c revision 1.1 1 /* $NetBSD: t_btowc.c,v 1.1 2017/06/01 15:45:02 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.1 2017/06/01 15:45:02 perseant Exp $");
36
37 #include <locale.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <wchar.h>
42
43 #include <atf-c.h>
44
45 struct test {
46 const char *locale;
47 const char *illegal; /* Illegal single-byte characters, if any */
48 const char *legal; /* Legal single-byte characters */
49 /* The next two are only used if __STDC_ISO_10646__ is defined */
50 const wchar_t wlegal[8]; /* The same characters, but in ISO-10646 */
51 const wchar_t willegal[8]; /* ISO-10646 that do not map into charset */
52 } tests[] = {
53 {
54 "C",
55 "\377",
56 "ABC123@\t",
57 { 'A', 'B', 'C', '1', '2', '3', '@', '\t' },
58 { 0x0430, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
59 },
60 {
61 "en_US.UTF-8",
62 "\200",
63 "ABC123@\t",
64 { 'A', 'B', 'C', '1', '2', '3', '@', '\t' },
65 { 0xfdd0, 0x10fffe, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
66 },
67 {
68 "ru_RU.KOI8-R",
69 "", /* No illegal characters in KOI8-R */
70 "A\xc2\xd7\xc7\xc4\xc5\xa3",
71 { 'A', 0x0431, 0x432, 0x0433, 0x0434, 0x0435, 0x0451, 0x0 },
72 { 0x00c5, 0x00e6, 0x00fe, 0x0630, 0x06fc, 0x56cd, 0x0, 0x0 }
73 },
74 {
75 NULL,
76 NULL,
77 NULL,
78 { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },
79 { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }
80 },
81 };
82
83 #ifdef __STDC_ISO_10646__
84 static void
85 h_iso10646(struct test *t)
86 {
87 const char *cp;
88 unsigned char c;
89 char *str;
90 const wchar_t *wcp;
91
92 /* These should have valid wchar representations */
93 for (cp = t->legal, wcp = t->wlegal; *cp != '\0'; ++cp, ++wcp) {
94 c = (unsigned char)*cp;
95 printf("Checking legal character 0x%x\n", c);
96
97 /* It should map to the known Unicode equivalent */
98 printf("btowc(0x%2.2x) = 0x%x, expecting 0x%x\n",
99 c, btowc(c), *wcp);
100 ATF_REQUIRE(btowc(c) == *wcp);
101 }
102
103 /* These are invalid characters in the target set */
104 for (wcp = t->willegal; *wcp != '\0'; ++wcp) {
105 printf("Checking illegal wide character 0x%lx\n",
106 (unsigned long)*wcp);
107 ATF_REQUIRE_EQ(wctob(*wcp), EOF);
108 }
109 }
110 #endif
111
112 static void
113 h_btowc(struct test *t)
114 {
115 const char *cp;
116 unsigned char c;
117 char *str;
118 const wchar_t *wcp;
119
120 ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
121 printf("Trying locale: %s\n", t->locale);
122 ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
123
124 /* btowc(EOF) -> WEOF */
125 ATF_REQUIRE_EQ(btowc(EOF), WEOF);
126
127 /* wctob(WEOF) -> EOF */
128 ATF_REQUIRE_EQ(wctob(WEOF), EOF);
129
130 /* Invalid in initial shift state -> WEOF */
131 for (cp = t->illegal; *cp != '\0'; ++cp) {
132 printf("Checking illegal character 0x%x\n",
133 (unsigned char)*cp);
134 ATF_REQUIRE_EQ(btowc(*cp), WEOF);
135 }
136
137 /* These should have valid wchar representations */
138 for (cp = t->legal; *cp != '\0'; ++cp) {
139 c = (unsigned char)*cp;
140 printf("Checking legal character 0x%x\n", c);
141
142 /* A legal character never maps to EOF */
143 ATF_REQUIRE(btowc(c) != WEOF);
144
145 /* And the mapping should be reversible */
146 printf("0x%x -> wide 0x%x -> 0x%x\n",
147 c, btowc(c), (unsigned char)wctob(btowc(c)));
148 ATF_REQUIRE_EQ(wctob(btowc(c)), c);
149 }
150 }
151
152 ATF_TC(btowc);
153 ATF_TC_HEAD(btowc, tc)
154 {
155 atf_tc_set_md_var(tc, "descr", "Checks btowc(3) and wctob(3)");
156 }
157 ATF_TC_BODY(btowc, tc)
158 {
159 struct test *t;
160
161 for (t = tests; t->locale != NULL; ++t)
162 h_btowc(t);
163 }
164
165 ATF_TC(stdc_iso_10646);
166 ATF_TC_HEAD(stdc_iso_10646, tc)
167 {
168 atf_tc_set_md_var(tc, "descr",
169 "Checks btowc(3) conversion to ISO10646");
170 }
171 ATF_TC_BODY(stdc_iso_10646, tc)
172 {
173 struct test *t;
174
175 #ifdef __STDC_ISO_10646__
176 for (t = tests; t->locale != NULL; ++t)
177 h_iso10646(t);
178 #else /* ! __STDC_ISO_10646__ */
179 atf_tc_skip("__STDC_ISO_10646__ not defined");
180 #endif /* ! __STDC_ISO_10646__ */
181 }
182
183 ATF_TP_ADD_TCS(tp)
184 {
185 ATF_TP_ADD_TC(tp, btowc);
186 ATF_TP_ADD_TC(tp, stdc_iso_10646);
187
188 return atf_no_error();
189 }
190