xref: /freebsd/contrib/netbsd-tests/lib/libc/locale/t_io.c (revision 43d55325406e96e37be661de52289c2592c82ec9)
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 
60 #if defined(__FreeBSD__)
61 	atf_tc_expect_fail("does not fail as expected (may be implementation "
62 	    "specific issue with the test)");
63 #endif
64 	ATF_REQUIRE_ERRNO(EILSEQ, wprintf(L"%ls\n", ibuf) < 0);
65 	ATF_REQUIRE(ferror(stdout));
66 }
67 
68 ATF_TC(bad_big5_swprintf);
69 ATF_TC_HEAD(bad_big5_swprintf, tc)
70 {
71 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar swprintf");
72 }
73 
74 ATF_TC_BODY(bad_big5_swprintf, tc)
75 {
76 	/* XXX implementation detail knowledge (wchar_t encoding) */
77 	wchar_t ibuf[] = { 0xcf10, 0 };
78 	wchar_t obuf[20];
79 	setlocale(LC_CTYPE, "zh_TW.Big5");
80 
81 #if defined(__FreeBSD__)
82 	atf_tc_expect_fail("does not fail as expected (may be implementation "
83 	    "specific issue with the test)");
84 #endif
85 	ATF_REQUIRE_ERRNO(EILSEQ,
86 			  swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf) < 0);
87 }
88 
89 ATF_TC(good_big5_wprintf);
90 ATF_TC_HEAD(good_big5_wprintf, tc)
91 {
92 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar wprintf");
93 }
94 
95 ATF_TC_BODY(good_big5_wprintf, tc)
96 {
97 	/* XXX implementation detail knowledge (wchar_t encoding) */
98 	wchar_t ibuf[] = { 0xcf40, 0 };
99 	setlocale(LC_CTYPE, "zh_TW.Big5");
100 	ATF_REQUIRE_EQ(wprintf(L"%ls\n", ibuf), 2);
101 }
102 
103 ATF_TC(good_big5_swprintf);
104 ATF_TC_HEAD(good_big5_swprintf, tc)
105 {
106 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar swprintf");
107 }
108 
109 ATF_TC_BODY(good_big5_swprintf, tc)
110 {
111 	/* XXX implementation detail knowledge (wchar_t encoding) */
112 	wchar_t ibuf[] = { 0xcf40, 0 };
113 	wchar_t obuf[20];
114 	setlocale(LC_CTYPE, "zh_TW.Big5");
115 	ATF_REQUIRE_EQ(swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf), 2);
116 }
117 
118 struct ibuf {
119 	off_t off;
120 	size_t buflen;
121 	const char *buf;
122 };
123 
124 static int
125 readfn(void *vp, char *buf, int len)
126 {
127 	struct ibuf *ib = vp;
128 	size_t todo = MIN((size_t)len, ib->buflen - ib->off);
129 
130 	memcpy(buf, ib->buf + ib->off, todo);
131 	ib->off += todo;
132 	return todo;
133 }
134 
135 ATF_TC(good_big5_getwc);
136 ATF_TC_HEAD(good_big5_getwc, tc)
137 {
138 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar getwc");
139 }
140 
141 ATF_TC_BODY(good_big5_getwc, tc)
142 {
143 	const char buf[] = { 0xcf, 0x40 };
144 	struct ibuf ib = {
145 		.buf = buf,
146 		.buflen = sizeof(buf),
147 	};
148 	FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL);
149 
150 	ATF_REQUIRE(fp != NULL);
151 	setlocale(LC_CTYPE, "zh_TW.Big5");
152 	/* XXX implementation detail knowledge (wchar_t encoding) */
153 	ATF_REQUIRE_EQ(getwc(fp), 0xcf40);
154 	fclose(fp);
155 }
156 
157 ATF_TC(bad_big5_getwc);
158 ATF_TC_HEAD(bad_big5_getwc, tc)
159 {
160 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar getwc");
161 }
162 
163 ATF_TC_BODY(bad_big5_getwc, tc)
164 {
165 	const char buf[] = { 0xcf, 0x20 };
166 	struct ibuf ib = {
167 		.buf = buf,
168 		.buflen = sizeof(buf),
169 	};
170 	FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL);
171 
172 	ATF_REQUIRE(fp != NULL);
173 	setlocale(LC_CTYPE, "zh_TW.Big5");
174 #if defined(__FreeBSD__)
175 	atf_tc_expect_fail("does not return WEOF as expected");
176 #endif
177 	ATF_REQUIRE_EQ(getwc(fp), WEOF);
178 	fclose(fp);
179 }
180 
181 ATF_TP_ADD_TCS(tp)
182 {
183 	ATF_TP_ADD_TC(tp, bad_big5_wprintf);
184 	ATF_TP_ADD_TC(tp, bad_big5_swprintf);
185 	ATF_TP_ADD_TC(tp, good_big5_wprintf);
186 	ATF_TP_ADD_TC(tp, good_big5_swprintf);
187 	ATF_TP_ADD_TC(tp, good_big5_getwc);
188 	ATF_TP_ADD_TC(tp, bad_big5_getwc);
189 
190 	return atf_no_error();
191 }
192