1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
/* $NetBSD: t_wctomb.c,v 1.3 2013/03/25 15:31:03 gson Exp $ */
/*-
* Copyright (c) 2011 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*-
* Copyright (c)2004 Citrus Project,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__COPYRIGHT("@(#) Copyright (c) 2011\
The NetBSD Foundation, inc. All rights reserved.");
__RCSID("$NetBSD: t_wctomb.c,v 1.3 2013/03/25 15:31:03 gson Exp $");
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <vis.h>
#include <wchar.h>
#include <string.h>
#include <limits.h>
#include <atf-c.h>
#define TC_WCTOMB 0
#define TC_WCRTOMB 1
#define TC_WCRTOMB_ST 2
static struct test {
const char *locale;
const char *data;
size_t wclen;
size_t mblen[16];
} tests[] = {
{
"ja_JP.ISO2022-JP",
"\x1b$B" /* JIS X 0208-1983 */
"\x46\x7c\x4b\x5c\x38\x6c" /* "nihongo" */
"\x1b(B" /* ISO 646 */
"ABC"
"\x1b(I" /* JIS X 0201 katakana */
"\xb1\xb2\xb3" /* "aiu" */
"\x1b(B", /* ISO 646 */
3 + 3 + 3,
{ 3+2, 2, 2, 3+1, 1, 1, 3+1, 1, 1, 3+1 }
}, {
"C",
"ABC",
3,
{ 1, 1, 1, 1 }
}, { NULL, NULL, 0, { } }
};
static void
h_wctomb(const struct test *t, char tc)
{
wchar_t wcs[16 + 2];
char buf[128];
char cs[MB_LEN_MAX];
const char *pcs;
char *str;
mbstate_t st;
mbstate_t *stp = NULL;
size_t sz, ret, i;
ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
(void)strvis(buf, t->data, VIS_WHITE | VIS_OCTAL);
(void)printf("Checking sequence: \"%s\"\n", buf);
ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
(void)printf("Using locale: %s\n", str);
if (tc == TC_WCRTOMB_ST) {
(void)memset(&st, 0, sizeof(st));
stp = &st;
}
wcs[t->wclen] = L'X'; /* poison */
pcs = t->data;
sz = mbsrtowcs(wcs, &pcs, t->wclen + 2, NULL);
ATF_REQUIRE_EQ_MSG(sz, t->wclen, "mbsrtowcs() returned: "
"%zu, expected: %zu", sz, t->wclen);
ATF_REQUIRE_EQ(wcs[t->wclen], 0);
for (i = 0; i < t->wclen + 1; i++) {
if (tc == TC_WCTOMB)
ret = wctomb(cs, wcs[i]);
else
ret = wcrtomb(cs, wcs[i], stp);
if (ret == t->mblen[i])
continue;
(void)printf("At position %zd:\n", i);
(void)printf(" expected: %zd\n", t->mblen[i]);
(void)printf(" got : %zd\n", ret);
atf_tc_fail("Test failed");
/* NOTREACHED */
}
(void)printf("Ok.\n");
}
ATF_TC(wctomb);
ATF_TC_HEAD(wctomb, tc)
{
atf_tc_set_md_var(tc, "descr", "Checks wctomb(3)");
}
ATF_TC_BODY(wctomb, tc)
{
struct test *t;
(void)printf("Checking wctomb()\n");
for (t = &tests[0]; t->data != NULL; ++t)
h_wctomb(t, TC_WCTOMB);
}
ATF_TC(wcrtomb_state);
ATF_TC_HEAD(wcrtomb_state, tc)
{
atf_tc_set_md_var(tc, "descr",
"Checks wcrtomb(3) (using state object)");
}
ATF_TC_BODY(wcrtomb_state, tc)
{
struct test *t;
(void)printf("Checking wcrtomb() (with state object)\n");
for (t = &tests[0]; t->data != NULL; ++t)
h_wctomb(t, TC_WCRTOMB_ST);
}
ATF_TC(wcrtomb);
ATF_TC_HEAD(wcrtomb, tc)
{
atf_tc_set_md_var(tc, "descr",
"Checks wcrtomb(3) (using internal state)");
}
ATF_TC_BODY(wcrtomb, tc)
{
struct test *t;
(void)printf("Checking wcrtomb() (using internal state)\n");
for (t = &tests[0]; t->data != NULL; ++t)
h_wctomb(t, TC_WCRTOMB);
}
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, wctomb);
ATF_TP_ADD_TC(tp, wcrtomb);
ATF_TP_ADD_TC(tp, wcrtomb_state);
return atf_no_error();
}
|