1*adbde1f9Sperseant /* $NetBSD: t_wctomb.c,v 1.5 2017/07/12 17:32:51 perseant Exp $ */
2324074b9Spgoyette
3324074b9Spgoyette /*-
4324074b9Spgoyette * Copyright (c) 2011 The NetBSD Foundation, Inc.
5324074b9Spgoyette * All rights reserved.
6324074b9Spgoyette *
7324074b9Spgoyette * Redistribution and use in source and binary forms, with or without
8324074b9Spgoyette * modification, are permitted provided that the following conditions
9324074b9Spgoyette * are met:
10324074b9Spgoyette * 1. Redistributions of source code must retain the above copyright
11324074b9Spgoyette * notice, this list of conditions and the following disclaimer.
12324074b9Spgoyette * 2. Redistributions in binary form must reproduce the above copyright
13324074b9Spgoyette * notice, this list of conditions and the following disclaimer in the
14324074b9Spgoyette * documentation and/or other materials provided with the distribution.
15324074b9Spgoyette *
16324074b9Spgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17324074b9Spgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18324074b9Spgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19324074b9Spgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20324074b9Spgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21324074b9Spgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22324074b9Spgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23324074b9Spgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24324074b9Spgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25324074b9Spgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26324074b9Spgoyette * POSSIBILITY OF SUCH DAMAGE.
27324074b9Spgoyette */
28324074b9Spgoyette
29324074b9Spgoyette /*-
30324074b9Spgoyette * Copyright (c)2004 Citrus Project,
31324074b9Spgoyette * All rights reserved.
32324074b9Spgoyette *
33324074b9Spgoyette * Redistribution and use in source and binary forms, with or without
34324074b9Spgoyette * modification, are permitted provided that the following conditions
35324074b9Spgoyette * are met:
36324074b9Spgoyette * 1. Redistributions of source code must retain the above copyright
37324074b9Spgoyette * notice, this list of conditions and the following disclaimer.
38324074b9Spgoyette * 2. Redistributions in binary form must reproduce the above copyright
39324074b9Spgoyette * notice, this list of conditions and the following disclaimer in the
40324074b9Spgoyette * documentation and/or other materials provided with the distribution.
41324074b9Spgoyette *
42324074b9Spgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43324074b9Spgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44324074b9Spgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45324074b9Spgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46324074b9Spgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47324074b9Spgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48324074b9Spgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49324074b9Spgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50324074b9Spgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51324074b9Spgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52324074b9Spgoyette * SUCH DAMAGE.
53324074b9Spgoyette */
54324074b9Spgoyette
55324074b9Spgoyette #include <sys/cdefs.h>
56324074b9Spgoyette __COPYRIGHT("@(#) Copyright (c) 2011\
57324074b9Spgoyette The NetBSD Foundation, inc. All rights reserved.");
58*adbde1f9Sperseant __RCSID("$NetBSD: t_wctomb.c,v 1.5 2017/07/12 17:32:51 perseant Exp $");
59324074b9Spgoyette
60324074b9Spgoyette #include <stdio.h>
61324074b9Spgoyette #include <stdlib.h>
62324074b9Spgoyette #include <locale.h>
63324074b9Spgoyette #include <vis.h>
64324074b9Spgoyette #include <wchar.h>
65324074b9Spgoyette #include <string.h>
661189f7bcSgson #include <limits.h>
67324074b9Spgoyette
68324074b9Spgoyette #include <atf-c.h>
69324074b9Spgoyette
70324074b9Spgoyette #define TC_WCTOMB 0
71324074b9Spgoyette #define TC_WCRTOMB 1
72324074b9Spgoyette #define TC_WCRTOMB_ST 2
73324074b9Spgoyette
74324074b9Spgoyette static struct test {
75324074b9Spgoyette const char *locale;
76324074b9Spgoyette const char *data;
77324074b9Spgoyette size_t wclen;
78324074b9Spgoyette size_t mblen[16];
7980f1b646Sperseant size_t stateful;
80324074b9Spgoyette } tests[] = {
81324074b9Spgoyette {
82324074b9Spgoyette "ja_JP.ISO2022-JP",
83324074b9Spgoyette "\x1b$B" /* JIS X 0208-1983 */
84324074b9Spgoyette "\x46\x7c\x4b\x5c\x38\x6c" /* "nihongo" */
85324074b9Spgoyette "\x1b(B" /* ISO 646 */
86324074b9Spgoyette "ABC"
87324074b9Spgoyette "\x1b(I" /* JIS X 0201 katakana */
88324074b9Spgoyette "\xb1\xb2\xb3" /* "aiu" */
89324074b9Spgoyette "\x1b(B", /* ISO 646 */
90324074b9Spgoyette 3 + 3 + 3,
9180f1b646Sperseant { 3+2, 2, 2, 3+1, 1, 1, 3+1, 1, 1, 3+1 },
9280f1b646Sperseant 1,
93324074b9Spgoyette }, {
94324074b9Spgoyette "C",
95324074b9Spgoyette "ABC",
96324074b9Spgoyette 3,
9780f1b646Sperseant { 1, 1, 1, 1 },
9880f1b646Sperseant 0,
9980f1b646Sperseant }, { NULL, NULL, 0, { }, 0 }
100324074b9Spgoyette };
101324074b9Spgoyette
102324074b9Spgoyette static void
h_wctomb(const struct test * t,char tc)103324074b9Spgoyette h_wctomb(const struct test *t, char tc)
104324074b9Spgoyette {
105324074b9Spgoyette wchar_t wcs[16 + 2];
106324074b9Spgoyette char buf[128];
1071189f7bcSgson char cs[MB_LEN_MAX];
108324074b9Spgoyette const char *pcs;
109324074b9Spgoyette char *str;
110324074b9Spgoyette mbstate_t st;
111324074b9Spgoyette mbstate_t *stp = NULL;
112324074b9Spgoyette size_t sz, ret, i;
113324074b9Spgoyette
114324074b9Spgoyette ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
11580f1b646Sperseant (void)printf("Trying locale: %s\n", t->locale);
116324074b9Spgoyette ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
117324074b9Spgoyette
11880f1b646Sperseant if (tc == TC_WCRTOMB_ST) {
11980f1b646Sperseant (void)memset(&st, 0, sizeof(st));
12080f1b646Sperseant stp = &st;
12180f1b646Sperseant } else {
12280f1b646Sperseant (void)printf("Checking correct reporting of statefulness\n");
12380f1b646Sperseant ret = wctomb(NULL, 0);
12480f1b646Sperseant ATF_REQUIRE_EQ(t->stateful, !!ret);
12580f1b646Sperseant }
12680f1b646Sperseant
127324074b9Spgoyette (void)strvis(buf, t->data, VIS_WHITE | VIS_OCTAL);
128324074b9Spgoyette (void)printf("Checking sequence: \"%s\"\n", buf);
129324074b9Spgoyette
130324074b9Spgoyette ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
131324074b9Spgoyette (void)printf("Using locale: %s\n", str);
132324074b9Spgoyette
133324074b9Spgoyette wcs[t->wclen] = L'X'; /* poison */
134324074b9Spgoyette pcs = t->data;
135324074b9Spgoyette sz = mbsrtowcs(wcs, &pcs, t->wclen + 2, NULL);
136324074b9Spgoyette ATF_REQUIRE_EQ_MSG(sz, t->wclen, "mbsrtowcs() returned: "
1372a18cea9Schristos "%zu, expected: %zu", sz, t->wclen);
138324074b9Spgoyette ATF_REQUIRE_EQ(wcs[t->wclen], 0);
139324074b9Spgoyette
140324074b9Spgoyette for (i = 0; i < t->wclen + 1; i++) {
141324074b9Spgoyette if (tc == TC_WCTOMB)
142324074b9Spgoyette ret = wctomb(cs, wcs[i]);
143324074b9Spgoyette else
144324074b9Spgoyette ret = wcrtomb(cs, wcs[i], stp);
145324074b9Spgoyette
146324074b9Spgoyette if (ret == t->mblen[i])
147324074b9Spgoyette continue;
148324074b9Spgoyette
149324074b9Spgoyette (void)printf("At position %zd:\n", i);
150324074b9Spgoyette (void)printf(" expected: %zd\n", t->mblen[i]);
151324074b9Spgoyette (void)printf(" got : %zd\n", ret);
152*adbde1f9Sperseant (void)strvis(buf, cs, VIS_WHITE | VIS_OCTAL);
153*adbde1f9Sperseant (void)printf(" sequence: \"%s\"\n", buf);
154324074b9Spgoyette atf_tc_fail("Test failed");
155324074b9Spgoyette /* NOTREACHED */
156324074b9Spgoyette }
157324074b9Spgoyette
158324074b9Spgoyette (void)printf("Ok.\n");
159324074b9Spgoyette }
160324074b9Spgoyette
161324074b9Spgoyette ATF_TC(wctomb);
ATF_TC_HEAD(wctomb,tc)162324074b9Spgoyette ATF_TC_HEAD(wctomb, tc)
163324074b9Spgoyette {
164324074b9Spgoyette atf_tc_set_md_var(tc, "descr", "Checks wctomb(3)");
165324074b9Spgoyette }
ATF_TC_BODY(wctomb,tc)166324074b9Spgoyette ATF_TC_BODY(wctomb, tc)
167324074b9Spgoyette {
168324074b9Spgoyette struct test *t;
169324074b9Spgoyette
170324074b9Spgoyette (void)printf("Checking wctomb()\n");
171324074b9Spgoyette
172324074b9Spgoyette for (t = &tests[0]; t->data != NULL; ++t)
173324074b9Spgoyette h_wctomb(t, TC_WCTOMB);
174324074b9Spgoyette }
175324074b9Spgoyette
176324074b9Spgoyette ATF_TC(wcrtomb_state);
ATF_TC_HEAD(wcrtomb_state,tc)177324074b9Spgoyette ATF_TC_HEAD(wcrtomb_state, tc)
178324074b9Spgoyette {
179324074b9Spgoyette atf_tc_set_md_var(tc, "descr",
180324074b9Spgoyette "Checks wcrtomb(3) (using state object)");
181324074b9Spgoyette }
ATF_TC_BODY(wcrtomb_state,tc)182324074b9Spgoyette ATF_TC_BODY(wcrtomb_state, tc)
183324074b9Spgoyette {
184324074b9Spgoyette struct test *t;
185324074b9Spgoyette
186324074b9Spgoyette (void)printf("Checking wcrtomb() (with state object)\n");
187324074b9Spgoyette
188324074b9Spgoyette for (t = &tests[0]; t->data != NULL; ++t)
189324074b9Spgoyette h_wctomb(t, TC_WCRTOMB_ST);
190324074b9Spgoyette }
191324074b9Spgoyette
192324074b9Spgoyette ATF_TC(wcrtomb);
ATF_TC_HEAD(wcrtomb,tc)193324074b9Spgoyette ATF_TC_HEAD(wcrtomb, tc)
194324074b9Spgoyette {
195324074b9Spgoyette atf_tc_set_md_var(tc, "descr",
196324074b9Spgoyette "Checks wcrtomb(3) (using internal state)");
197324074b9Spgoyette }
ATF_TC_BODY(wcrtomb,tc)198324074b9Spgoyette ATF_TC_BODY(wcrtomb, tc)
199324074b9Spgoyette {
200324074b9Spgoyette struct test *t;
201324074b9Spgoyette
202324074b9Spgoyette (void)printf("Checking wcrtomb() (using internal state)\n");
203324074b9Spgoyette
204324074b9Spgoyette for (t = &tests[0]; t->data != NULL; ++t)
205324074b9Spgoyette h_wctomb(t, TC_WCRTOMB);
206324074b9Spgoyette }
207324074b9Spgoyette
ATF_TP_ADD_TCS(tp)208324074b9Spgoyette ATF_TP_ADD_TCS(tp)
209324074b9Spgoyette {
210324074b9Spgoyette ATF_TP_ADD_TC(tp, wctomb);
211324074b9Spgoyette ATF_TP_ADD_TC(tp, wcrtomb);
212324074b9Spgoyette ATF_TP_ADD_TC(tp, wcrtomb_state);
213324074b9Spgoyette
214324074b9Spgoyette return atf_no_error();
215324074b9Spgoyette }
216