xref: /minix3/tests/lib/libc/locale/t_io.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_io.c,v 1.4 2014/01/21 00:32:16 yamt Exp $ */
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2013 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc  * by Christos Zoulas.
911be35a1SLionel Sambuc  *
1011be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc  * are met:
1311be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc  *
1911be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc  */
3111be35a1SLionel Sambuc 
3211be35a1SLionel Sambuc #include <sys/cdefs.h>
3311be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2011\
3411be35a1SLionel Sambuc  The NetBSD Foundation, inc. All rights reserved.");
35*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_io.c,v 1.4 2014/01/21 00:32:16 yamt Exp $");
3611be35a1SLionel Sambuc 
3711be35a1SLionel Sambuc #include <sys/param.h>
3811be35a1SLionel Sambuc #include <errno.h>
3911be35a1SLionel Sambuc #include <locale.h>
4011be35a1SLionel Sambuc #include <stdio.h>
4111be35a1SLionel Sambuc #include <stdlib.h>
4211be35a1SLionel Sambuc #include <string.h>
4311be35a1SLionel Sambuc #include <wchar.h>
4411be35a1SLionel Sambuc 
4511be35a1SLionel Sambuc #include <atf-c.h>
4611be35a1SLionel Sambuc 
4711be35a1SLionel Sambuc 
4811be35a1SLionel Sambuc ATF_TC(bad_big5_wprintf);
ATF_TC_HEAD(bad_big5_wprintf,tc)4911be35a1SLionel Sambuc ATF_TC_HEAD(bad_big5_wprintf, tc)
5011be35a1SLionel Sambuc {
5111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar wprintf");
5211be35a1SLionel Sambuc }
5311be35a1SLionel Sambuc 
ATF_TC_BODY(bad_big5_wprintf,tc)5411be35a1SLionel Sambuc ATF_TC_BODY(bad_big5_wprintf, tc)
5511be35a1SLionel Sambuc {
56*0a6a1f1dSLionel Sambuc 	/* XXX implementation detail knowledge (wchar_t encoding) */
5711be35a1SLionel Sambuc 	wchar_t ibuf[] = { 0xcf10, 0 };
5811be35a1SLionel Sambuc 	setlocale(LC_CTYPE, "zh_TW.Big5");
59*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE_ERRNO(EILSEQ, wprintf(L"%ls\n", ibuf) < 0);
60*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(ferror(stdout));
6111be35a1SLionel Sambuc }
6211be35a1SLionel Sambuc 
6311be35a1SLionel Sambuc ATF_TC(bad_big5_swprintf);
ATF_TC_HEAD(bad_big5_swprintf,tc)6411be35a1SLionel Sambuc ATF_TC_HEAD(bad_big5_swprintf, tc)
6511be35a1SLionel Sambuc {
6611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar swprintf");
6711be35a1SLionel Sambuc }
6811be35a1SLionel Sambuc 
ATF_TC_BODY(bad_big5_swprintf,tc)6911be35a1SLionel Sambuc ATF_TC_BODY(bad_big5_swprintf, tc)
7011be35a1SLionel Sambuc {
71*0a6a1f1dSLionel Sambuc 	/* XXX implementation detail knowledge (wchar_t encoding) */
7211be35a1SLionel Sambuc 	wchar_t ibuf[] = { 0xcf10, 0 };
7311be35a1SLionel Sambuc 	wchar_t obuf[20];
7411be35a1SLionel Sambuc 	setlocale(LC_CTYPE, "zh_TW.Big5");
75*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE_ERRNO(EILSEQ,
76*0a6a1f1dSLionel Sambuc 			  swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf) < 0);
7711be35a1SLionel Sambuc }
7811be35a1SLionel Sambuc 
7911be35a1SLionel Sambuc ATF_TC(good_big5_wprintf);
ATF_TC_HEAD(good_big5_wprintf,tc)8011be35a1SLionel Sambuc ATF_TC_HEAD(good_big5_wprintf, tc)
8111be35a1SLionel Sambuc {
8211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar wprintf");
8311be35a1SLionel Sambuc }
8411be35a1SLionel Sambuc 
ATF_TC_BODY(good_big5_wprintf,tc)8511be35a1SLionel Sambuc ATF_TC_BODY(good_big5_wprintf, tc)
8611be35a1SLionel Sambuc {
87*0a6a1f1dSLionel Sambuc 	/* XXX implementation detail knowledge (wchar_t encoding) */
8811be35a1SLionel Sambuc 	wchar_t ibuf[] = { 0xcf40, 0 };
8911be35a1SLionel Sambuc 	setlocale(LC_CTYPE, "zh_TW.Big5");
9011be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(wprintf(L"%ls\n", ibuf), 2);
9111be35a1SLionel Sambuc }
9211be35a1SLionel Sambuc 
9311be35a1SLionel Sambuc ATF_TC(good_big5_swprintf);
ATF_TC_HEAD(good_big5_swprintf,tc)9411be35a1SLionel Sambuc ATF_TC_HEAD(good_big5_swprintf, tc)
9511be35a1SLionel Sambuc {
9611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar swprintf");
9711be35a1SLionel Sambuc }
9811be35a1SLionel Sambuc 
ATF_TC_BODY(good_big5_swprintf,tc)9911be35a1SLionel Sambuc ATF_TC_BODY(good_big5_swprintf, tc)
10011be35a1SLionel Sambuc {
101*0a6a1f1dSLionel Sambuc 	/* XXX implementation detail knowledge (wchar_t encoding) */
10211be35a1SLionel Sambuc 	wchar_t ibuf[] = { 0xcf40, 0 };
10311be35a1SLionel Sambuc 	wchar_t obuf[20];
10411be35a1SLionel Sambuc 	setlocale(LC_CTYPE, "zh_TW.Big5");
10511be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf), 2);
10611be35a1SLionel Sambuc }
10711be35a1SLionel Sambuc 
108*0a6a1f1dSLionel Sambuc struct ibuf {
109*0a6a1f1dSLionel Sambuc 	off_t off;
110*0a6a1f1dSLionel Sambuc 	size_t buflen;
111*0a6a1f1dSLionel Sambuc 	const char *buf;
112*0a6a1f1dSLionel Sambuc };
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc static int
readfn(void * vp,char * buf,int len)115*0a6a1f1dSLionel Sambuc readfn(void *vp, char *buf, int len)
116*0a6a1f1dSLionel Sambuc {
117*0a6a1f1dSLionel Sambuc 	struct ibuf *ib = vp;
118*0a6a1f1dSLionel Sambuc 	size_t todo = MIN((size_t)len, ib->buflen - ib->off);
119*0a6a1f1dSLionel Sambuc 
120*0a6a1f1dSLionel Sambuc 	memcpy(buf, ib->buf + ib->off, todo);
121*0a6a1f1dSLionel Sambuc 	ib->off += todo;
122*0a6a1f1dSLionel Sambuc 	return todo;
12311be35a1SLionel Sambuc }
12411be35a1SLionel Sambuc 
12511be35a1SLionel Sambuc ATF_TC(good_big5_getwc);
ATF_TC_HEAD(good_big5_getwc,tc)12611be35a1SLionel Sambuc ATF_TC_HEAD(good_big5_getwc, tc)
12711be35a1SLionel Sambuc {
12811be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar getwc");
12911be35a1SLionel Sambuc }
13011be35a1SLionel Sambuc 
ATF_TC_BODY(good_big5_getwc,tc)13111be35a1SLionel Sambuc ATF_TC_BODY(good_big5_getwc, tc)
13211be35a1SLionel Sambuc {
133*0a6a1f1dSLionel Sambuc 	const char buf[] = { 0xcf, 0x40 };
134*0a6a1f1dSLionel Sambuc 	struct ibuf ib = {
135*0a6a1f1dSLionel Sambuc 		.buf = buf,
136*0a6a1f1dSLionel Sambuc 		.buflen = sizeof(buf),
137*0a6a1f1dSLionel Sambuc 	};
138*0a6a1f1dSLionel Sambuc 	FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL);
13911be35a1SLionel Sambuc 
14011be35a1SLionel Sambuc 	ATF_REQUIRE(fp != NULL);
14111be35a1SLionel Sambuc 	setlocale(LC_CTYPE, "zh_TW.Big5");
142*0a6a1f1dSLionel Sambuc 	/* XXX implementation detail knowledge (wchar_t encoding) */
14311be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(getwc(fp), 0xcf40);
14411be35a1SLionel Sambuc 	fclose(fp);
14511be35a1SLionel Sambuc }
14611be35a1SLionel Sambuc 
14711be35a1SLionel Sambuc ATF_TC(bad_big5_getwc);
ATF_TC_HEAD(bad_big5_getwc,tc)14811be35a1SLionel Sambuc ATF_TC_HEAD(bad_big5_getwc, tc)
14911be35a1SLionel Sambuc {
15011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar getwc");
15111be35a1SLionel Sambuc }
15211be35a1SLionel Sambuc 
ATF_TC_BODY(bad_big5_getwc,tc)15311be35a1SLionel Sambuc ATF_TC_BODY(bad_big5_getwc, tc)
15411be35a1SLionel Sambuc {
155*0a6a1f1dSLionel Sambuc 	const char buf[] = { 0xcf, 0x20 };
156*0a6a1f1dSLionel Sambuc 	struct ibuf ib = {
157*0a6a1f1dSLionel Sambuc 		.buf = buf,
158*0a6a1f1dSLionel Sambuc 		.buflen = sizeof(buf),
159*0a6a1f1dSLionel Sambuc 	};
160*0a6a1f1dSLionel Sambuc 	FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL);
16111be35a1SLionel Sambuc 
16211be35a1SLionel Sambuc 	ATF_REQUIRE(fp != NULL);
16311be35a1SLionel Sambuc 	setlocale(LC_CTYPE, "zh_TW.Big5");
16411be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(getwc(fp), WEOF);
16511be35a1SLionel Sambuc 	fclose(fp);
16611be35a1SLionel Sambuc }
16711be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)16811be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
16911be35a1SLionel Sambuc {
17011be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, bad_big5_wprintf);
17111be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, bad_big5_swprintf);
17211be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, good_big5_wprintf);
17311be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, good_big5_swprintf);
17411be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, good_big5_getwc);
17511be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, bad_big5_getwc);
17611be35a1SLionel Sambuc 
17711be35a1SLionel Sambuc 	return atf_no_error();
17811be35a1SLionel Sambuc }
179