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