xref: /netbsd-src/tests/lib/libc/locale/t_io.c (revision adbde1f90007dcbbba222bd12b3ad39e535dc73d)
1*adbde1f9Sperseant /* $NetBSD: t_io.c,v 1.5 2017/07/12 17:32:51 perseant Exp $ */
2f5295934Schristos 
3f5295934Schristos /*-
4f5295934Schristos  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5f5295934Schristos  * All rights reserved.
6f5295934Schristos  *
7f5295934Schristos  * This code is derived from software contributed to The NetBSD Foundation
8f5295934Schristos  * by Christos Zoulas.
9f5295934Schristos  *
10f5295934Schristos  * Redistribution and use in source and binary forms, with or without
11f5295934Schristos  * modification, are permitted provided that the following conditions
12f5295934Schristos  * are met:
13f5295934Schristos  * 1. Redistributions of source code must retain the above copyright
14f5295934Schristos  *    notice, this list of conditions and the following disclaimer.
15f5295934Schristos  * 2. Redistributions in binary form must reproduce the above copyright
16f5295934Schristos  *    notice, this list of conditions and the following disclaimer in the
17f5295934Schristos  *    documentation and/or other materials provided with the distribution.
18f5295934Schristos  *
19f5295934Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f5295934Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f5295934Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f5295934Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f5295934Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f5295934Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f5295934Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f5295934Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f5295934Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f5295934Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f5295934Schristos  * POSSIBILITY OF SUCH DAMAGE.
30f5295934Schristos  */
31f5295934Schristos 
32f5295934Schristos #include <sys/cdefs.h>
33f5295934Schristos __COPYRIGHT("@(#) Copyright (c) 2011\
34f5295934Schristos  The NetBSD Foundation, inc. All rights reserved.");
35*adbde1f9Sperseant __RCSID("$NetBSD: t_io.c,v 1.5 2017/07/12 17:32:51 perseant Exp $");
36f5295934Schristos 
37f5295934Schristos #include <sys/param.h>
38f5295934Schristos #include <errno.h>
39f5295934Schristos #include <locale.h>
40f5295934Schristos #include <stdio.h>
41f5295934Schristos #include <stdlib.h>
42f5295934Schristos #include <string.h>
43f5295934Schristos #include <wchar.h>
44f5295934Schristos 
45f5295934Schristos #include <atf-c.h>
46f5295934Schristos 
47f5295934Schristos 
48f5295934Schristos ATF_TC(bad_big5_wprintf);
ATF_TC_HEAD(bad_big5_wprintf,tc)49f5295934Schristos ATF_TC_HEAD(bad_big5_wprintf, tc)
50f5295934Schristos {
51f5295934Schristos 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar wprintf");
52f5295934Schristos }
53f5295934Schristos 
ATF_TC_BODY(bad_big5_wprintf,tc)54f5295934Schristos ATF_TC_BODY(bad_big5_wprintf, tc)
55f5295934Schristos {
56*adbde1f9Sperseant 	wchar_t ibuf[] = {
57*adbde1f9Sperseant #ifdef __STDC_ISO_10646__
58*adbde1f9Sperseant 		0x0978, 0  /* An arbitrarily chosen Devangari symbol */
59*adbde1f9Sperseant #else
60eb988d97Syamt 		/* XXX implementation detail knowledge (wchar_t encoding) */
61*adbde1f9Sperseant 		0xcf10, 0
62*adbde1f9Sperseant #endif
63*adbde1f9Sperseant 	};
64f5295934Schristos 	setlocale(LC_CTYPE, "zh_TW.Big5");
65d1e900e6Syamt 	ATF_REQUIRE_ERRNO(EILSEQ, wprintf(L"%ls\n", ibuf) < 0);
66d1e900e6Syamt 	ATF_REQUIRE(ferror(stdout));
67f5295934Schristos }
68f5295934Schristos 
69f5295934Schristos ATF_TC(bad_big5_swprintf);
ATF_TC_HEAD(bad_big5_swprintf,tc)70f5295934Schristos ATF_TC_HEAD(bad_big5_swprintf, tc)
71f5295934Schristos {
72f5295934Schristos 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar swprintf");
73f5295934Schristos }
74f5295934Schristos 
ATF_TC_BODY(bad_big5_swprintf,tc)75f5295934Schristos ATF_TC_BODY(bad_big5_swprintf, tc)
76f5295934Schristos {
77*adbde1f9Sperseant 	wchar_t ibuf[] = {
78*adbde1f9Sperseant #ifdef __STDC_ISO_10646__
79*adbde1f9Sperseant 		0x0978, 0  /* An arbitrarily chosen Devangari symbol */
80*adbde1f9Sperseant #else
81eb988d97Syamt 		/* XXX implementation detail knowledge (wchar_t encoding) */
82*adbde1f9Sperseant 		0xcf10, 0
83*adbde1f9Sperseant #endif
84*adbde1f9Sperseant 	};
85f5295934Schristos 	wchar_t obuf[20];
86f5295934Schristos 	setlocale(LC_CTYPE, "zh_TW.Big5");
87d1e900e6Syamt 	ATF_REQUIRE_ERRNO(EILSEQ,
88d1e900e6Syamt 			  swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf) < 0);
89f5295934Schristos }
90f5295934Schristos 
91f5295934Schristos ATF_TC(good_big5_wprintf);
ATF_TC_HEAD(good_big5_wprintf,tc)92f5295934Schristos ATF_TC_HEAD(good_big5_wprintf, tc)
93f5295934Schristos {
94f5295934Schristos 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar wprintf");
95f5295934Schristos }
96f5295934Schristos 
ATF_TC_BODY(good_big5_wprintf,tc)97f5295934Schristos ATF_TC_BODY(good_big5_wprintf, tc)
98f5295934Schristos {
99*adbde1f9Sperseant 	wchar_t ibuf[] = {
100*adbde1f9Sperseant #ifdef __STDC_ISO_10646__
101*adbde1f9Sperseant 		0x67DC, 0
102*adbde1f9Sperseant #else
103eb988d97Syamt 		/* XXX implementation detail knowledge (wchar_t encoding) */
104*adbde1f9Sperseant 		0xcf40, 0
105*adbde1f9Sperseant #endif
106*adbde1f9Sperseant 	};
107f5295934Schristos 	setlocale(LC_CTYPE, "zh_TW.Big5");
108f5295934Schristos 	ATF_REQUIRE_EQ(wprintf(L"%ls\n", ibuf), 2);
109f5295934Schristos }
110f5295934Schristos 
111f5295934Schristos ATF_TC(good_big5_swprintf);
ATF_TC_HEAD(good_big5_swprintf,tc)112f5295934Schristos ATF_TC_HEAD(good_big5_swprintf, tc)
113f5295934Schristos {
114f5295934Schristos 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar swprintf");
115f5295934Schristos }
116f5295934Schristos 
ATF_TC_BODY(good_big5_swprintf,tc)117f5295934Schristos ATF_TC_BODY(good_big5_swprintf, tc)
118f5295934Schristos {
119*adbde1f9Sperseant 	wchar_t ibuf[] = {
120*adbde1f9Sperseant #ifdef __STDC_ISO_10646__
121*adbde1f9Sperseant 		0x67DC, 0
122*adbde1f9Sperseant #else
123eb988d97Syamt 		/* XXX implementation detail knowledge (wchar_t encoding) */
124*adbde1f9Sperseant 		0xcf40, 0
125*adbde1f9Sperseant #endif
126*adbde1f9Sperseant 	};
127f5295934Schristos 	wchar_t obuf[20];
128f5295934Schristos 	setlocale(LC_CTYPE, "zh_TW.Big5");
129f5295934Schristos 	ATF_REQUIRE_EQ(swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf), 2);
130f5295934Schristos }
131f5295934Schristos 
132d1e900e6Syamt struct ibuf {
133d1e900e6Syamt 	off_t off;
134d1e900e6Syamt 	size_t buflen;
135d1e900e6Syamt 	const char *buf;
136d1e900e6Syamt };
137d1e900e6Syamt 
138d1e900e6Syamt static int
readfn(void * vp,char * buf,int len)139d1e900e6Syamt readfn(void *vp, char *buf, int len)
140d1e900e6Syamt {
141d1e900e6Syamt 	struct ibuf *ib = vp;
142d1e900e6Syamt 	size_t todo = MIN((size_t)len, ib->buflen - ib->off);
143d1e900e6Syamt 
144d1e900e6Syamt 	memcpy(buf, ib->buf + ib->off, todo);
145d1e900e6Syamt 	ib->off += todo;
146d1e900e6Syamt 	return todo;
147f5295934Schristos }
148f5295934Schristos 
149f5295934Schristos ATF_TC(good_big5_getwc);
ATF_TC_HEAD(good_big5_getwc,tc)150f5295934Schristos ATF_TC_HEAD(good_big5_getwc, tc)
151f5295934Schristos {
152f5295934Schristos 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar getwc");
153f5295934Schristos }
154f5295934Schristos 
ATF_TC_BODY(good_big5_getwc,tc)155f5295934Schristos ATF_TC_BODY(good_big5_getwc, tc)
156f5295934Schristos {
157d1e900e6Syamt 	const char buf[] = { 0xcf, 0x40 };
158d1e900e6Syamt 	struct ibuf ib = {
159d1e900e6Syamt 		.buf = buf,
160d1e900e6Syamt 		.buflen = sizeof(buf),
161d1e900e6Syamt 	};
162d1e900e6Syamt 	FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL);
163f5295934Schristos 
164f5295934Schristos 	ATF_REQUIRE(fp != NULL);
165f5295934Schristos 	setlocale(LC_CTYPE, "zh_TW.Big5");
166*adbde1f9Sperseant 	ATF_REQUIRE_EQ(getwc(fp),
167*adbde1f9Sperseant #ifdef __STDC_ISO_10646__
168*adbde1f9Sperseant 		       0x67DC
169*adbde1f9Sperseant #else
170eb988d97Syamt 		       /* XXX implementation detail knowledge (wchar_t encoding) */
171*adbde1f9Sperseant 		       0xcf40
172*adbde1f9Sperseant #endif
173*adbde1f9Sperseant 		);
174f5295934Schristos 	fclose(fp);
175f5295934Schristos }
176f5295934Schristos 
177f5295934Schristos ATF_TC(bad_big5_getwc);
ATF_TC_HEAD(bad_big5_getwc,tc)178f5295934Schristos ATF_TC_HEAD(bad_big5_getwc, tc)
179f5295934Schristos {
180f5295934Schristos 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar getwc");
181f5295934Schristos }
182f5295934Schristos 
ATF_TC_BODY(bad_big5_getwc,tc)183f5295934Schristos ATF_TC_BODY(bad_big5_getwc, tc)
184f5295934Schristos {
185d1e900e6Syamt 	const char buf[] = { 0xcf, 0x20 };
186d1e900e6Syamt 	struct ibuf ib = {
187d1e900e6Syamt 		.buf = buf,
188d1e900e6Syamt 		.buflen = sizeof(buf),
189d1e900e6Syamt 	};
190d1e900e6Syamt 	FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL);
191f5295934Schristos 
192f5295934Schristos 	ATF_REQUIRE(fp != NULL);
193f5295934Schristos 	setlocale(LC_CTYPE, "zh_TW.Big5");
194f5295934Schristos 	ATF_REQUIRE_EQ(getwc(fp), WEOF);
195f5295934Schristos 	fclose(fp);
196f5295934Schristos }
197f5295934Schristos 
ATF_TP_ADD_TCS(tp)198f5295934Schristos ATF_TP_ADD_TCS(tp)
199f5295934Schristos {
200f5295934Schristos 	ATF_TP_ADD_TC(tp, bad_big5_wprintf);
201f5295934Schristos 	ATF_TP_ADD_TC(tp, bad_big5_swprintf);
202f5295934Schristos 	ATF_TP_ADD_TC(tp, good_big5_wprintf);
203f5295934Schristos 	ATF_TP_ADD_TC(tp, good_big5_swprintf);
204f5295934Schristos 	ATF_TP_ADD_TC(tp, good_big5_getwc);
205f5295934Schristos 	ATF_TP_ADD_TC(tp, bad_big5_getwc);
206f5295934Schristos 
207f5295934Schristos 	return atf_no_error();
208f5295934Schristos }
209