1*2ba250a1Sjruoho /* $NetBSD: t_mbtowc.c,v 1.3 2020/06/30 16:09:40 jruoho 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)2007 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
56*2ba250a1Sjruoho /*-
57*2ba250a1Sjruoho * Copyright (c) 2005 Miloslav Trmac
58*2ba250a1Sjruoho * All rights reserved.
59*2ba250a1Sjruoho *
60*2ba250a1Sjruoho * Redistribution and use in source and binary forms, with or without
61*2ba250a1Sjruoho * modification, are permitted provided that the following conditions
62*2ba250a1Sjruoho * are met:
63*2ba250a1Sjruoho
64*2ba250a1Sjruoho * 1. Redistributions of source code must retain the above copyright
65*2ba250a1Sjruoho * notice, this list of conditions and the following disclaimer.
66*2ba250a1Sjruoho * 2. Redistributions in binary form must reproduce the above copyright
67*2ba250a1Sjruoho * notice, this list of conditions and the following disclaimer in the
68*2ba250a1Sjruoho * documentation and/or other materials provided with the distribution.
69*2ba250a1Sjruoho *
70*2ba250a1Sjruoho * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
71*2ba250a1Sjruoho * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
72*2ba250a1Sjruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
73*2ba250a1Sjruoho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
74*2ba250a1Sjruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
75*2ba250a1Sjruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
76*2ba250a1Sjruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77*2ba250a1Sjruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
78*2ba250a1Sjruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
79*2ba250a1Sjruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
80*2ba250a1Sjruoho * POSSIBILITY OF SUCH DAMAGE.
81*2ba250a1Sjruoho */
82*2ba250a1Sjruoho
83324074b9Spgoyette #include <sys/cdefs.h>
84324074b9Spgoyette __COPYRIGHT("@(#) Copyright (c) 2011\
85324074b9Spgoyette The NetBSD Foundation, inc. All rights reserved.");
86*2ba250a1Sjruoho __RCSID("$NetBSD: t_mbtowc.c,v 1.3 2020/06/30 16:09:40 jruoho Exp $");
87324074b9Spgoyette
88324074b9Spgoyette #include <errno.h>
89*2ba250a1Sjruoho #include <langinfo.h>
90324074b9Spgoyette #include <locale.h>
91*2ba250a1Sjruoho #include <limits.h>
92324074b9Spgoyette #include <stdio.h>
93324074b9Spgoyette #include <stdlib.h>
94324074b9Spgoyette #include <string.h>
95324074b9Spgoyette #include <vis.h>
96324074b9Spgoyette #include <wchar.h>
97324074b9Spgoyette
98324074b9Spgoyette #include <atf-c.h>
99324074b9Spgoyette
100324074b9Spgoyette static void
h_mbtowc(const char * locale,const char * illegal,const char * legal,size_t stateful)101*2ba250a1Sjruoho h_mbtowc(const char *locale, const char *illegal,
102*2ba250a1Sjruoho const char *legal, size_t stateful)
103324074b9Spgoyette {
104324074b9Spgoyette char buf[64];
10580f1b646Sperseant size_t ret;
106324074b9Spgoyette char *str;
107324074b9Spgoyette
108324074b9Spgoyette ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
10980f1b646Sperseant (void)printf("Trying locale: %s\n", locale);
110324074b9Spgoyette ATF_REQUIRE(setlocale(LC_CTYPE, locale) != NULL);
111324074b9Spgoyette
112324074b9Spgoyette ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
113324074b9Spgoyette (void)printf("Using locale: %s\n", str);
114324074b9Spgoyette (void)printf("Locale is state-%sdependent\n",
11580f1b646Sperseant !stateful ? "in" : "");
116324074b9Spgoyette
117324074b9Spgoyette /* initialize internal state */
118324074b9Spgoyette ret = mbtowc(NULL, NULL, 0);
119324074b9Spgoyette ATF_REQUIRE(stateful ? ret : !ret);
120324074b9Spgoyette
121324074b9Spgoyette (void)strvis(buf, illegal, VIS_WHITE | VIS_OCTAL);
122324074b9Spgoyette (void)printf("Checking illegal sequence: \"%s\"\n", buf);
123324074b9Spgoyette
124324074b9Spgoyette ret = mbtowc(NULL, illegal, strlen(illegal));
125324074b9Spgoyette (void)printf("mbtowc() returned: %zd\n", ret);
126324074b9Spgoyette ATF_REQUIRE_EQ(ret, (size_t)-1);
127324074b9Spgoyette (void)printf("errno: %s\n", strerror(errno));
128324074b9Spgoyette ATF_REQUIRE_EQ(errno, EILSEQ);
129324074b9Spgoyette
130*2ba250a1Sjruoho /*
131*2ba250a1Sjruoho * If this is stateless encoding, this
132*2ba250a1Sjruoho * re-initialization is not required.
133*2ba250a1Sjruoho */
134324074b9Spgoyette if (stateful) {
135324074b9Spgoyette /* re-initialize internal state */
13680f1b646Sperseant mbtowc(NULL, NULL, 0);
137324074b9Spgoyette }
138324074b9Spgoyette
139324074b9Spgoyette /* valid multibyte sequence case */
140324074b9Spgoyette (void)strvis(buf, legal, VIS_WHITE | VIS_OCTAL);
141324074b9Spgoyette (void)printf("Checking legal sequence: \"%s\"\n", buf);
142324074b9Spgoyette
143324074b9Spgoyette errno = 0;
144324074b9Spgoyette ret = mbtowc(NULL, legal, strlen(legal));
145324074b9Spgoyette (void)printf("mbtowc() returned: %zd\n", ret);
146324074b9Spgoyette ATF_REQUIRE(ret != (size_t)-1);
147324074b9Spgoyette (void)printf("errno: %s\n", strerror(errno));
148324074b9Spgoyette ATF_REQUIRE_EQ(errno, 0);
149324074b9Spgoyette
150324074b9Spgoyette (void)printf("Ok.\n");
151324074b9Spgoyette }
152324074b9Spgoyette
153*2ba250a1Sjruoho ATF_TC(mbtowc_basic);
ATF_TC_HEAD(mbtowc_basic,tc)154*2ba250a1Sjruoho ATF_TC_HEAD(mbtowc_basic, tc)
155324074b9Spgoyette {
156*2ba250a1Sjruoho atf_tc_set_md_var(tc, "descr", "A basic test of mbtowc(3)");
157324074b9Spgoyette }
158*2ba250a1Sjruoho
ATF_TC_BODY(mbtowc_basic,tc)159*2ba250a1Sjruoho ATF_TC_BODY(mbtowc_basic, tc)
160324074b9Spgoyette {
16180f1b646Sperseant h_mbtowc("en_US.UTF-8", "\240", "\302\240", 0);
16280f1b646Sperseant h_mbtowc("ja_JP.ISO2022-JP", "\033$B", "\033$B$\"\033(B", 1);
16380f1b646Sperseant h_mbtowc("ja_JP.SJIS", "\202", "\202\240", 0);
16480f1b646Sperseant h_mbtowc("ja_JP.eucJP", "\244", "\244\242", 0);
16580f1b646Sperseant h_mbtowc("zh_CN.GB18030", "\241", "\241\241", 0);
16680f1b646Sperseant h_mbtowc("zh_TW.Big5", "\241", "\241@", 0);
16780f1b646Sperseant h_mbtowc("zh_TW.eucTW", "\241", "\241\241", 0);
168324074b9Spgoyette }
169324074b9Spgoyette
170*2ba250a1Sjruoho ATF_TC(mbtowc_sign);
ATF_TC_HEAD(mbtowc_sign,tc)171*2ba250a1Sjruoho ATF_TC_HEAD(mbtowc_sign, tc)
172*2ba250a1Sjruoho {
173*2ba250a1Sjruoho atf_tc_set_md_var(tc, "descr", "Test mbtowc(3) sign conversion");
174*2ba250a1Sjruoho }
175*2ba250a1Sjruoho
ATF_TC_BODY(mbtowc_sign,tc)176*2ba250a1Sjruoho ATF_TC_BODY(mbtowc_sign, tc)
177*2ba250a1Sjruoho {
178*2ba250a1Sjruoho char back[MB_LEN_MAX];
179*2ba250a1Sjruoho wchar_t wc;
180*2ba250a1Sjruoho size_t i;
181*2ba250a1Sjruoho int ret;
182*2ba250a1Sjruoho
183*2ba250a1Sjruoho (void)setlocale(LC_ALL, "");
184*2ba250a1Sjruoho (void)printf("Charset: %s\n", nl_langinfo(CODESET));
185*2ba250a1Sjruoho ret = mbtowc(&wc, "\xe4", 1);
186*2ba250a1Sjruoho (void)printf("mbtowc(): %d\n", ret);
187*2ba250a1Sjruoho
188*2ba250a1Sjruoho if (ret > 0) {
189*2ba250a1Sjruoho (void)printf("Result: 0x%08lX\n",(unsigned long)wc);
190*2ba250a1Sjruoho ret = wctomb(back, wc);
191*2ba250a1Sjruoho (void)printf("wctomb(): %d\n", ret);
192*2ba250a1Sjruoho for(i = 0; ret > 0 && i < (size_t)ret; i++)
193*2ba250a1Sjruoho printf("%02X ",(unsigned char)back[i]);
194*2ba250a1Sjruoho putchar('\n');
195*2ba250a1Sjruoho }
196*2ba250a1Sjruoho
197*2ba250a1Sjruoho ATF_REQUIRE(ret > 0);
198*2ba250a1Sjruoho }
199*2ba250a1Sjruoho
ATF_TP_ADD_TCS(tp)200324074b9Spgoyette ATF_TP_ADD_TCS(tp)
201324074b9Spgoyette {
202*2ba250a1Sjruoho
203*2ba250a1Sjruoho ATF_TP_ADD_TC(tp, mbtowc_basic);
204*2ba250a1Sjruoho ATF_TP_ADD_TC(tp, mbtowc_sign);
205324074b9Spgoyette
206324074b9Spgoyette return atf_no_error();
207324074b9Spgoyette }
208