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