1272343Sngie/* $NetBSD: t_wctomb.c,v 1.3 2013/03/25 15:31:03 gson Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie
29272343Sngie/*-
30272343Sngie * Copyright (c)2004 Citrus Project,
31272343Sngie * All rights reserved.
32272343Sngie *
33272343Sngie * Redistribution and use in source and binary forms, with or without
34272343Sngie * modification, are permitted provided that the following conditions
35272343Sngie * are met:
36272343Sngie * 1. Redistributions of source code must retain the above copyright
37272343Sngie *    notice, this list of conditions and the following disclaimer.
38272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
39272343Sngie *    notice, this list of conditions and the following disclaimer in the
40272343Sngie *    documentation and/or other materials provided with the distribution.
41272343Sngie *
42272343Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43272343Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44272343Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45272343Sngie * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46272343Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48272343Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49272343Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50272343Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51272343Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52272343Sngie * SUCH DAMAGE.
53272343Sngie */
54272343Sngie
55272343Sngie#include <sys/cdefs.h>
56272343Sngie__COPYRIGHT("@(#) Copyright (c) 2011\
57272343Sngie The NetBSD Foundation, inc. All rights reserved.");
58272343Sngie__RCSID("$NetBSD: t_wctomb.c,v 1.3 2013/03/25 15:31:03 gson Exp $");
59272343Sngie
60272343Sngie#include <stdio.h>
61272343Sngie#include <stdlib.h>
62272343Sngie#include <locale.h>
63272343Sngie#include <vis.h>
64272343Sngie#include <wchar.h>
65272343Sngie#include <string.h>
66272343Sngie#include <limits.h>
67272343Sngie
68272343Sngie#include <atf-c.h>
69272343Sngie
70272343Sngie#define TC_WCTOMB	0
71272343Sngie#define TC_WCRTOMB	1
72272343Sngie#define TC_WCRTOMB_ST	2
73272343Sngie
74272343Sngiestatic struct test {
75272343Sngie	const char *locale;
76272343Sngie	const char *data;
77272343Sngie	size_t wclen;
78272343Sngie	size_t mblen[16];
79272343Sngie} tests[] = {
80272343Sngie{
81272343Sngie	"ja_JP.ISO2022-JP",
82272343Sngie	"\x1b$B"	/* JIS X 0208-1983 */
83272343Sngie	"\x46\x7c\x4b\x5c\x38\x6c" /* "nihongo" */
84272343Sngie	"\x1b(B"	/* ISO 646 */
85272343Sngie	"ABC"
86272343Sngie	"\x1b(I"	/* JIS X 0201 katakana */
87272343Sngie	"\xb1\xb2\xb3"	/* "aiu" */
88272343Sngie	"\x1b(B",	/* ISO 646 */
89272343Sngie	3 + 3 + 3,
90272343Sngie	{ 3+2, 2, 2, 3+1, 1, 1, 3+1, 1, 1, 3+1 }
91272343Sngie}, {
92272343Sngie	"C",
93272343Sngie	"ABC",
94272343Sngie	3,
95272343Sngie	{ 1, 1, 1, 1 }
96272343Sngie}, { NULL, NULL, 0, { } }
97272343Sngie};
98272343Sngie
99272343Sngiestatic void
100272343Sngieh_wctomb(const struct test *t, char tc)
101272343Sngie{
102272343Sngie	wchar_t wcs[16 + 2];
103272343Sngie	char buf[128];
104272343Sngie	char cs[MB_LEN_MAX];
105272343Sngie	const char *pcs;
106272343Sngie	char *str;
107272343Sngie	mbstate_t st;
108272343Sngie	mbstate_t *stp = NULL;
109272343Sngie	size_t sz, ret, i;
110272343Sngie
111272343Sngie	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
112276478Sngie#ifdef __NetBSD__
113272343Sngie	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
114276478Sngie#else
115276478Sngie	if (setlocale(LC_CTYPE, t->locale) == NULL) {
116276478Sngie		fprintf(stderr, "Locale %s not found.\n", t->locale);
117276478Sngie		return;
118276478Sngie	}
119276478Sngie#endif
120272343Sngie
121272343Sngie	(void)strvis(buf, t->data, VIS_WHITE | VIS_OCTAL);
122272343Sngie	(void)printf("Checking sequence: \"%s\"\n", buf);
123272343Sngie
124272343Sngie	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
125272343Sngie	(void)printf("Using locale: %s\n", str);
126272343Sngie
127272343Sngie	if (tc == TC_WCRTOMB_ST) {
128272343Sngie		(void)memset(&st, 0, sizeof(st));
129272343Sngie		stp = &st;
130272343Sngie	}
131272343Sngie
132272343Sngie	wcs[t->wclen] = L'X'; /* poison */
133272343Sngie	pcs = t->data;
134272343Sngie	sz = mbsrtowcs(wcs, &pcs, t->wclen + 2, NULL);
135272343Sngie	ATF_REQUIRE_EQ_MSG(sz, t->wclen, "mbsrtowcs() returned: "
136272343Sngie		"%zu, expected: %zu", sz, t->wclen);
137272343Sngie	ATF_REQUIRE_EQ(wcs[t->wclen], 0);
138272343Sngie
139272343Sngie	for (i = 0; i < t->wclen + 1; i++) {
140272343Sngie		if (tc == TC_WCTOMB)
141272343Sngie			ret = wctomb(cs, wcs[i]);
142272343Sngie		else
143272343Sngie			ret = wcrtomb(cs, wcs[i], stp);
144272343Sngie
145272343Sngie		if (ret == t->mblen[i])
146272343Sngie			continue;
147272343Sngie
148272343Sngie		(void)printf("At position %zd:\n", i);
149272343Sngie		(void)printf("  expected: %zd\n", t->mblen[i]);
150272343Sngie		(void)printf("  got     : %zd\n", ret);
151272343Sngie		atf_tc_fail("Test failed");
152272343Sngie		/* NOTREACHED */
153272343Sngie	}
154272343Sngie
155272343Sngie	(void)printf("Ok.\n");
156272343Sngie}
157272343Sngie
158272343SngieATF_TC(wctomb);
159272343SngieATF_TC_HEAD(wctomb, tc)
160272343Sngie{
161272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks wctomb(3)");
162272343Sngie}
163272343SngieATF_TC_BODY(wctomb, tc)
164272343Sngie{
165272343Sngie	struct test *t;
166272343Sngie
167272343Sngie	(void)printf("Checking wctomb()\n");
168272343Sngie
169272343Sngie	for (t = &tests[0]; t->data != NULL; ++t)
170272343Sngie		h_wctomb(t, TC_WCTOMB);
171272343Sngie}
172272343Sngie
173272343SngieATF_TC(wcrtomb_state);
174272343SngieATF_TC_HEAD(wcrtomb_state, tc)
175272343Sngie{
176272343Sngie	atf_tc_set_md_var(tc, "descr",
177272343Sngie		"Checks wcrtomb(3) (using state object)");
178272343Sngie}
179272343SngieATF_TC_BODY(wcrtomb_state, tc)
180272343Sngie{
181272343Sngie	struct test *t;
182272343Sngie
183272343Sngie	(void)printf("Checking wcrtomb() (with state object)\n");
184272343Sngie
185272343Sngie	for (t = &tests[0]; t->data != NULL; ++t)
186272343Sngie		h_wctomb(t, TC_WCRTOMB_ST);
187272343Sngie}
188272343Sngie
189272343SngieATF_TC(wcrtomb);
190272343SngieATF_TC_HEAD(wcrtomb, tc)
191272343Sngie{
192272343Sngie	atf_tc_set_md_var(tc, "descr",
193272343Sngie		"Checks wcrtomb(3) (using internal state)");
194272343Sngie}
195272343SngieATF_TC_BODY(wcrtomb, tc)
196272343Sngie{
197272343Sngie	struct test *t;
198272343Sngie
199272343Sngie	(void)printf("Checking wcrtomb() (using internal state)\n");
200272343Sngie
201272343Sngie	for (t = &tests[0]; t->data != NULL; ++t)
202272343Sngie		h_wctomb(t, TC_WCRTOMB);
203272343Sngie}
204272343Sngie
205272343SngieATF_TP_ADD_TCS(tp)
206272343Sngie{
207272343Sngie	ATF_TP_ADD_TC(tp, wctomb);
208272343Sngie	ATF_TP_ADD_TC(tp, wcrtomb);
209272343Sngie	ATF_TP_ADD_TC(tp, wcrtomb_state);
210272343Sngie
211272343Sngie	return atf_no_error();
212272343Sngie}
213