t_mbstowcs.c revision 1.3 1 1.3 wiz /* $NetBSD: t_mbstowcs.c,v 1.3 2022/12/21 09:33:34 wiz Exp $ */
2 1.1 jruoho
3 1.1 jruoho /*-
4 1.1 jruoho * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1 jruoho * All rights reserved.
6 1.1 jruoho *
7 1.1 jruoho * Redistribution and use in source and binary forms, with or without
8 1.1 jruoho * modification, are permitted provided that the following conditions
9 1.1 jruoho * are met:
10 1.1 jruoho * 1. Redistributions of source code must retain the above copyright
11 1.1 jruoho * notice, this list of conditions and the following disclaimer.
12 1.1 jruoho * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jruoho * notice, this list of conditions and the following disclaimer in the
14 1.1 jruoho * documentation and/or other materials provided with the distribution.
15 1.1 jruoho *
16 1.1 jruoho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jruoho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jruoho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jruoho * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jruoho */
28 1.1 jruoho
29 1.1 jruoho /*-
30 1.1 jruoho * Copyright (c)2003 Citrus Project,
31 1.1 jruoho * All rights reserved.
32 1.1 jruoho *
33 1.1 jruoho * Redistribution and use in source and binary forms, with or without
34 1.1 jruoho * modification, are permitted provided that the following conditions
35 1.1 jruoho * are met:
36 1.1 jruoho * 1. Redistributions of source code must retain the above copyright
37 1.1 jruoho * notice, this list of conditions and the following disclaimer.
38 1.1 jruoho * 2. Redistributions in binary form must reproduce the above copyright
39 1.1 jruoho * notice, this list of conditions and the following disclaimer in the
40 1.1 jruoho * documentation and/or other materials provided with the distribution.
41 1.1 jruoho *
42 1.1 jruoho * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43 1.1 jruoho * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 1.1 jruoho * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 1.1 jruoho * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46 1.1 jruoho * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 1.1 jruoho * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 1.1 jruoho * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 1.1 jruoho * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 1.1 jruoho * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 1.1 jruoho * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 1.1 jruoho * SUCH DAMAGE.
53 1.1 jruoho */
54 1.1 jruoho
55 1.1 jruoho #include <sys/cdefs.h>
56 1.1 jruoho __COPYRIGHT("@(#) Copyright (c) 2011\
57 1.1 jruoho The NetBSD Foundation, inc. All rights reserved.");
58 1.3 wiz __RCSID("$NetBSD: t_mbstowcs.c,v 1.3 2022/12/21 09:33:34 wiz Exp $");
59 1.1 jruoho
60 1.1 jruoho #include <errno.h>
61 1.1 jruoho #include <locale.h>
62 1.1 jruoho #include <stdio.h>
63 1.1 jruoho #include <stdlib.h>
64 1.1 jruoho #include <string.h>
65 1.1 jruoho #include <vis.h>
66 1.1 jruoho #include <wchar.h>
67 1.1 jruoho
68 1.1 jruoho #include <atf-c.h>
69 1.1 jruoho
70 1.1 jruoho #define REQUIRE_ERRNO(x, v) \
71 1.1 jruoho ATF_REQUIRE_MSG((x) != (v), "%s: %s", #x, strerror(errno))
72 1.1 jruoho
73 1.1 jruoho #define SIZE 256
74 1.1 jruoho
75 1.1 jruoho static struct test {
76 1.1 jruoho const char *locale;
77 1.1 jruoho const char *data;
78 1.1 jruoho wchar_t wchars[64];
79 1.1 jruoho int widths[64];
80 1.1 jruoho int width;
81 1.1 jruoho } tests[] = {
82 1.1 jruoho {
83 1.1 jruoho "en_US.UTF-8",
84 1.1 jruoho "[\001\177][\302\200\337\277][\340\240\200\357\277\277][\360\220\200"
85 1.1 jruoho "\200\367\277\277\277][\370\210\200\200\200\373\277\277\277\277][\374"
86 1.1 jruoho "\204\200\200\200\200\375\277\277\277\277\277]",
87 1.1 jruoho {
88 1.1 jruoho 0x5B, 0x01, 0x7F, 0x5D, 0x5B, 0x80, 0x07FF, 0x5D, 0x5B, 0x0800,
89 1.1 jruoho 0xFFFF, 0x5D, 0x5B, 0x10000, 0x1FFFFF, 0x5D, 0x5B, 0x200000,
90 1.1 jruoho 0x3FFFFFF, 0x5D, 0x5B, 0x4000000, 0x7FFFFFFF, 0x5D, 0x0A
91 1.1 jruoho },
92 1.3 wiz { 1, -1, -1, 1, 1, -1, 1, 1, 1, 1,
93 1.3 wiz -1, 1, 1, 1, -1, 1, 1, -1,
94 1.3 wiz -1, 1, 1, -1, -1, 1, -1
95 1.3 wiz },
96 1.1 jruoho -1
97 1.1 jruoho }, {
98 1.1 jruoho "ja_JP.ISO2022-JP",
99 1.1 jruoho "\033$B#J#I#S$G$9!#\033(Baaaa\033$B$\"$$$&$($*\033(B",
100 1.1 jruoho {
101 1.2 perseant #ifdef __STDC_ISO_10646__
102 1.2 perseant 0xFF2A, 0xFF29, 0xFF33, 0x3067, 0x3059,
103 1.2 perseant 0x3002, 0x61, 0x61, 0x61, 0x61, 0x3042, 0x3044,
104 1.2 perseant 0x3046, 0x3048, 0x304A, 0x0A
105 1.2 perseant #else
106 1.1 jruoho 0x4200234A, 0x42002349, 0x42002353, 0x42002447, 0x42002439,
107 1.1 jruoho 0x42002123, 0x61, 0x61, 0x61, 0x61, 0x42002422, 0x42002424,
108 1.1 jruoho 0x42002426, 0x42002428, 0x4200242A, 0x0A
109 1.2 perseant #endif
110 1.1 jruoho },
111 1.1 jruoho { 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1 },
112 1.1 jruoho 26
113 1.1 jruoho }, {
114 1.1 jruoho "ja_JP.SJIS",
115 1.1 jruoho "\202r\202i\202h\202r\202\305\202\267\201Baaaa\202\240\202\242"
116 1.1 jruoho "\202\244\202\246\202\250",
117 1.1 jruoho {
118 1.2 perseant #ifdef __STDC_ISO_10646__
119 1.2 perseant 0xFF33, 0xFF2A, 0xFF29, 0xFF33, 0x3067, 0x3059, 0x3002, 0x61,
120 1.2 perseant 0x61, 0x61, 0x61, 0x3042, 0x3044, 0x3046, 0x3048, 0x304A, 0x0A
121 1.2 perseant #else
122 1.1 jruoho 0x8272, 0x8269, 0x8268, 0x8272, 0x82C5, 0x82B7, 0x8142, 0x61,
123 1.1 jruoho 0x61, 0x61, 0x61, 0x82A0, 0x82A2, 0x82A4, 0x82A6, 0x82A8, 0x0A
124 1.2 perseant #endif
125 1.1 jruoho },
126 1.1 jruoho { 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1 },
127 1.1 jruoho 28
128 1.1 jruoho }, {
129 1.1 jruoho "ja_JP.eucJP",
130 1.1 jruoho "\243\305\243\325\243\303\244\307\244\271\241\243aaaa\244\242\244"
131 1.1 jruoho "\244\244\246\244\250\244\252",
132 1.1 jruoho {
133 1.2 perseant #ifdef __STDC_ISO_10646__
134 1.2 perseant 0xFF25, 0xFF35, 0xFF23, 0x3067, 0x3059, 0x3002, 0x61, 0x61, 0x61,
135 1.2 perseant 0x61, 0x3042, 0x3044, 0x3046, 0x3048, 0x304A, 0x0A
136 1.2 perseant #else
137 1.1 jruoho 0xA3C5, 0xA3D5, 0xA3C3, 0xA4C7, 0xA4B9, 0xA1A3, 0x61, 0x61, 0x61,
138 1.1 jruoho 0x61, 0xA4A2, 0xA4A4, 0xA4A6, 0xA4A8, 0xA4AA, 0x0A
139 1.2 perseant #endif
140 1.1 jruoho },
141 1.1 jruoho { 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1 },
142 1.1 jruoho 26
143 1.1 jruoho }, {
144 1.1 jruoho NULL,
145 1.1 jruoho NULL,
146 1.1 jruoho {},
147 1.1 jruoho {},
148 1.1 jruoho 0
149 1.1 jruoho }
150 1.1 jruoho };
151 1.1 jruoho
152 1.1 jruoho ATF_TC(mbstowcs_basic);
153 1.1 jruoho ATF_TC_HEAD(mbstowcs_basic, tc)
154 1.1 jruoho {
155 1.1 jruoho atf_tc_set_md_var(tc, "descr",
156 1.1 jruoho "Checks wide character functions with different locales");
157 1.1 jruoho }
158 1.1 jruoho ATF_TC_BODY(mbstowcs_basic, tc)
159 1.1 jruoho {
160 1.1 jruoho struct test *t;
161 1.1 jruoho
162 1.1 jruoho for (t = &tests[0]; t->data != NULL; ++t) {
163 1.1 jruoho wchar_t wbuf[SIZE];
164 1.1 jruoho char buf[SIZE];
165 1.1 jruoho char visbuf[SIZE];
166 1.1 jruoho char *str;
167 1.1 jruoho int i;
168 1.1 jruoho
169 1.1 jruoho ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
170 1.1 jruoho ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
171 1.1 jruoho
172 1.1 jruoho (void)strvis(visbuf, t->data, VIS_WHITE | VIS_OCTAL);
173 1.1 jruoho (void)printf("Checking string: \"%s\"\n", visbuf);
174 1.1 jruoho
175 1.1 jruoho ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
176 1.1 jruoho (void)printf("Using locale: %s\n", str);
177 1.1 jruoho
178 1.1 jruoho REQUIRE_ERRNO((ssize_t)mbstowcs(wbuf, t->data, SIZE-1), -1);
179 1.1 jruoho REQUIRE_ERRNO((ssize_t)wcstombs(buf, wbuf, SIZE-1), -1);
180 1.1 jruoho
181 1.1 jruoho if (strcmp(buf, t->data) != 0) {
182 1.1 jruoho (void)strvis(visbuf, buf, VIS_WHITE | VIS_OCTAL);
183 1.1 jruoho (void)printf("Conversion to wcs and back failed: "
184 1.1 jruoho "\"%s\"\n", visbuf);
185 1.1 jruoho atf_tc_fail("Test failed");
186 1.1 jruoho }
187 1.1 jruoho
188 1.1 jruoho /* The output here is implementation-dependent. */
189 1.1 jruoho
190 1.1 jruoho for (i = 0; wbuf[i] != 0; ++i) {
191 1.1 jruoho if (wbuf[i] == t->wchars[i] &&
192 1.1 jruoho wcwidth(wbuf[i]) == t->widths[i])
193 1.1 jruoho continue;
194 1.1 jruoho
195 1.1 jruoho (void)printf("At position %d:\n", i);
196 1.1 jruoho (void)printf(" expected: 0x%04X (%d)\n",
197 1.1 jruoho t->wchars[i], t->widths[i]);
198 1.1 jruoho (void)printf(" got : 0x%04X (%d)\n", wbuf[i],
199 1.1 jruoho wcwidth(wbuf[i]));
200 1.1 jruoho atf_tc_fail("Test failed");
201 1.1 jruoho }
202 1.1 jruoho
203 1.1 jruoho if (wcswidth(wbuf, SIZE-1) != t->width) {
204 1.1 jruoho (void)printf("Incorrect wcswidth:\n");
205 1.1 jruoho (void)printf(" expected: %d\n", t->width);
206 1.1 jruoho (void)printf(" got : %d\n", wcswidth(wbuf, SIZE-1));
207 1.1 jruoho atf_tc_fail("Test failed");
208 1.1 jruoho }
209 1.1 jruoho
210 1.1 jruoho (void)printf("Ok.\n");
211 1.1 jruoho }
212 1.1 jruoho }
213 1.1 jruoho
214 1.1 jruoho ATF_TP_ADD_TCS(tp)
215 1.1 jruoho {
216 1.1 jruoho ATF_TP_ADD_TC(tp, mbstowcs_basic);
217 1.1 jruoho
218 1.1 jruoho return atf_no_error();
219 1.1 jruoho }
220