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