xref: /freebsd-src/contrib/netbsd-tests/lib/libc/locale/t_mbstowcs.c (revision 3d5db455951d382a9cb355cb4bc56cac5c277603)
157718be8SEnji Cooper /* $NetBSD: t_mbstowcs.c,v 1.1 2011/07/15 07:35:21 jruoho Exp $ */
257718be8SEnji Cooper 
357718be8SEnji Cooper /*-
457718be8SEnji Cooper  * Copyright (c) 2011 The NetBSD Foundation, Inc.
557718be8SEnji Cooper  * All rights reserved.
657718be8SEnji Cooper  *
757718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
857718be8SEnji Cooper  * modification, are permitted provided that the following conditions
957718be8SEnji Cooper  * are met:
1057718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
1157718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
1257718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1357718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
1457718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
1557718be8SEnji Cooper  *
1657718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1757718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1857718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1957718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2057718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2157718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2257718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2357718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2457718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2557718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2657718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
2757718be8SEnji Cooper  */
2857718be8SEnji Cooper 
2957718be8SEnji Cooper /*-
3057718be8SEnji Cooper  * Copyright (c)2003 Citrus Project,
3157718be8SEnji Cooper  * All rights reserved.
3257718be8SEnji Cooper  *
3357718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
3457718be8SEnji Cooper  * modification, are permitted provided that the following conditions
3557718be8SEnji Cooper  * are met:
3657718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
3757718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
3857718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
3957718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
4057718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
4157718be8SEnji Cooper  *
4257718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
4357718be8SEnji Cooper  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4457718be8SEnji Cooper  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4557718be8SEnji Cooper  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4657718be8SEnji Cooper  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4757718be8SEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4857718be8SEnji Cooper  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4957718be8SEnji Cooper  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5057718be8SEnji Cooper  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5157718be8SEnji Cooper  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5257718be8SEnji Cooper  * SUCH DAMAGE.
5357718be8SEnji Cooper  */
5457718be8SEnji Cooper 
5557718be8SEnji Cooper #include <sys/cdefs.h>
5657718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2011\
5757718be8SEnji Cooper  The NetBSD Foundation, inc. All rights reserved.");
5857718be8SEnji Cooper __RCSID("$NetBSD: t_mbstowcs.c,v 1.1 2011/07/15 07:35:21 jruoho Exp $");
5957718be8SEnji Cooper 
6057718be8SEnji Cooper #include <errno.h>
6157718be8SEnji Cooper #include <locale.h>
6257718be8SEnji Cooper #include <stdio.h>
6357718be8SEnji Cooper #include <stdlib.h>
6457718be8SEnji Cooper #include <string.h>
6557718be8SEnji Cooper #include <vis.h>
6657718be8SEnji Cooper #include <wchar.h>
6757718be8SEnji Cooper 
6857718be8SEnji Cooper #include <atf-c.h>
6957718be8SEnji Cooper 
7057718be8SEnji Cooper #define REQUIRE_ERRNO(x, v) \
7157718be8SEnji Cooper 	ATF_REQUIRE_MSG((x) != (v), "%s: %s", #x, strerror(errno))
7257718be8SEnji Cooper 
7357718be8SEnji Cooper #define SIZE 256
7457718be8SEnji Cooper 
7557718be8SEnji Cooper static struct test {
7657718be8SEnji Cooper 	const char *locale;
7757718be8SEnji Cooper 	const char *data;
7857718be8SEnji Cooper 	wchar_t wchars[64];
7957718be8SEnji Cooper 	int widths[64];
8057718be8SEnji Cooper 	int width;
8157718be8SEnji Cooper } tests[] = {
8257718be8SEnji Cooper {
8357718be8SEnji Cooper 	"en_US.UTF-8",
8457718be8SEnji Cooper 	"[\001\177][\302\200\337\277][\340\240\200\357\277\277][\360\220\200"
855e0069c6SJilles Tjoelker 	"\200\364\217\277\277]",
8657718be8SEnji Cooper 	{
8757718be8SEnji Cooper 		0x5B, 0x01, 0x7F, 0x5D, 0x5B, 0x80, 0x07FF, 0x5D, 0x5B, 0x0800,
885e0069c6SJilles Tjoelker 		0xFFFF, 0x5D, 0x5B, 0x10000, 0x10FFFF, 0x5D, 0x0A
8957718be8SEnji Cooper 	},
90eb5cdd01SBaptiste Daroussin #ifdef __FreeBSD__
91*adeebb83SYuri Pankov 	{	 1, -1, -1,  1,  1, -1,  1,  1,  1,  1, -1,  1,  1,  1, -1,
92eb5cdd01SBaptiste Daroussin #else
93eb5cdd01SBaptiste Daroussin 	{	 1, -1, -1,  1,  1, -1, -1,  1,  1, -1, -1,  1,  1, -1, -1,
94eb5cdd01SBaptiste Daroussin #endif
9557718be8SEnji Cooper 		 1,  1, -1, -1,  1,  1, -1, -1,  1, -1
9657718be8SEnji Cooper 	},
9757718be8SEnji Cooper 	-1
9857718be8SEnji Cooper }, {
9957718be8SEnji Cooper 	"ja_JP.ISO2022-JP",
10057718be8SEnji Cooper 	"\033$B#J#I#S$G$9!#\033(Baaaa\033$B$\"$$$&$($*\033(B",
10157718be8SEnji Cooper 	{
10257718be8SEnji Cooper 		0x4200234A, 0x42002349, 0x42002353, 0x42002447, 0x42002439,
10357718be8SEnji Cooper 		0x42002123, 0x61, 0x61, 0x61, 0x61, 0x42002422, 0x42002424,
10457718be8SEnji Cooper 		0x42002426, 0x42002428, 0x4200242A, 0x0A
10557718be8SEnji Cooper 	},
10657718be8SEnji Cooper 	{ 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1 },
10757718be8SEnji Cooper 	26
10857718be8SEnji Cooper }, {
10957718be8SEnji Cooper 	"ja_JP.SJIS",
11057718be8SEnji Cooper 	"\202r\202i\202h\202r\202\305\202\267\201Baaaa\202\240\202\242"
11157718be8SEnji Cooper 	"\202\244\202\246\202\250",
11257718be8SEnji Cooper 	{
11357718be8SEnji Cooper 		0x8272, 0x8269, 0x8268, 0x8272, 0x82C5, 0x82B7, 0x8142, 0x61,
11457718be8SEnji Cooper 		0x61, 0x61, 0x61, 0x82A0, 0x82A2, 0x82A4, 0x82A6, 0x82A8, 0x0A
11557718be8SEnji Cooper 	},
11657718be8SEnji Cooper 	{ 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1 },
11757718be8SEnji Cooper 	28
11857718be8SEnji Cooper }, {
11957718be8SEnji Cooper 	"ja_JP.eucJP",
12057718be8SEnji Cooper 	"\243\305\243\325\243\303\244\307\244\271\241\243aaaa\244\242\244"
12157718be8SEnji Cooper 	"\244\244\246\244\250\244\252",
12257718be8SEnji Cooper 	{
12357718be8SEnji Cooper 		0xA3C5, 0xA3D5, 0xA3C3, 0xA4C7, 0xA4B9, 0xA1A3, 0x61, 0x61, 0x61,
12457718be8SEnji Cooper 		0x61, 0xA4A2, 0xA4A4, 0xA4A6, 0xA4A8, 0xA4AA, 0x0A
12557718be8SEnji Cooper 	},
12657718be8SEnji Cooper 	{ 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1 },
12757718be8SEnji Cooper 	26
12857718be8SEnji Cooper }, {
12957718be8SEnji Cooper 	NULL,
13057718be8SEnji Cooper 	NULL,
13157718be8SEnji Cooper 	{},
13257718be8SEnji Cooper 	{},
13357718be8SEnji Cooper 	0
13457718be8SEnji Cooper }
13557718be8SEnji Cooper };
13657718be8SEnji Cooper 
13757718be8SEnji Cooper ATF_TC(mbstowcs_basic);
ATF_TC_HEAD(mbstowcs_basic,tc)13857718be8SEnji Cooper ATF_TC_HEAD(mbstowcs_basic, tc)
13957718be8SEnji Cooper {
14057718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
14157718be8SEnji Cooper 		"Checks wide character functions with different locales");
14257718be8SEnji Cooper }
ATF_TC_BODY(mbstowcs_basic,tc)14357718be8SEnji Cooper ATF_TC_BODY(mbstowcs_basic, tc)
14457718be8SEnji Cooper {
14557718be8SEnji Cooper 	struct test *t;
14657718be8SEnji Cooper 
14757718be8SEnji Cooper 	for (t = &tests[0]; t->data != NULL; ++t) {
14857718be8SEnji Cooper 		wchar_t	wbuf[SIZE];
14957718be8SEnji Cooper 		char buf[SIZE];
15057718be8SEnji Cooper 		char visbuf[SIZE];
15157718be8SEnji Cooper 		char *str;
15257718be8SEnji Cooper 		int i;
15357718be8SEnji Cooper 
15457718be8SEnji Cooper 		ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
155ff0ba872SEnji Cooper #ifdef __NetBSD__
15657718be8SEnji Cooper 		ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
157effc3698SEnji Cooper #else
158effc3698SEnji Cooper 		if (setlocale(LC_CTYPE, t->locale) == NULL) {
159effc3698SEnji Cooper 			fprintf(stderr, "Locale %s not found.\n", t->locale);
160effc3698SEnji Cooper 			continue;
161effc3698SEnji Cooper 		}
162effc3698SEnji Cooper #endif
16357718be8SEnji Cooper 
16457718be8SEnji Cooper 		(void)strvis(visbuf, t->data, VIS_WHITE | VIS_OCTAL);
16557718be8SEnji Cooper 		(void)printf("Checking string: \"%s\"\n", visbuf);
16657718be8SEnji Cooper 
16757718be8SEnji Cooper 		ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
16857718be8SEnji Cooper 		(void)printf("Using locale: %s\n", str);
16957718be8SEnji Cooper 
17057718be8SEnji Cooper 		REQUIRE_ERRNO((ssize_t)mbstowcs(wbuf, t->data, SIZE-1), -1);
17157718be8SEnji Cooper 		REQUIRE_ERRNO((ssize_t)wcstombs(buf, wbuf, SIZE-1), -1);
17257718be8SEnji Cooper 
17357718be8SEnji Cooper 		if (strcmp(buf, t->data) != 0) {
17457718be8SEnji Cooper 			(void)strvis(visbuf, buf, VIS_WHITE | VIS_OCTAL);
17557718be8SEnji Cooper 			(void)printf("Conversion to wcs and back failed: "
17657718be8SEnji Cooper 				"\"%s\"\n", visbuf);
17757718be8SEnji Cooper 			atf_tc_fail("Test failed");
17857718be8SEnji Cooper 		}
17957718be8SEnji Cooper 
18057718be8SEnji Cooper 		/* The output here is implementation-dependent. */
18157718be8SEnji Cooper 
18257718be8SEnji Cooper 		for (i = 0; wbuf[i] != 0; ++i) {
18357718be8SEnji Cooper 			if (wbuf[i] == t->wchars[i] &&
18457718be8SEnji Cooper 			    wcwidth(wbuf[i]) == t->widths[i])
18557718be8SEnji Cooper 				continue;
18657718be8SEnji Cooper 
18757718be8SEnji Cooper 			(void)printf("At position %d:\n", i);
18857718be8SEnji Cooper 			(void)printf("  expected: 0x%04X (%d)\n",
18957718be8SEnji Cooper 				t->wchars[i], t->widths[i]);
19057718be8SEnji Cooper 			(void)printf("  got     : 0x%04X (%d)\n", wbuf[i],
19157718be8SEnji Cooper 				wcwidth(wbuf[i]));
19257718be8SEnji Cooper 			atf_tc_fail("Test failed");
19357718be8SEnji Cooper 		}
19457718be8SEnji Cooper 
19557718be8SEnji Cooper 		if (wcswidth(wbuf, SIZE-1) != t->width) {
19657718be8SEnji Cooper 			(void)printf("Incorrect wcswidth:\n");
19757718be8SEnji Cooper 			(void)printf("  expected: %d\n", t->width);
19857718be8SEnji Cooper 			(void)printf("  got     : %d\n", wcswidth(wbuf, SIZE-1));
19957718be8SEnji Cooper 			atf_tc_fail("Test failed");
20057718be8SEnji Cooper 		}
20157718be8SEnji Cooper 
20257718be8SEnji Cooper 		(void)printf("Ok.\n");
20357718be8SEnji Cooper 	}
20457718be8SEnji Cooper }
20557718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)20657718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
20757718be8SEnji Cooper {
20857718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, mbstowcs_basic);
20957718be8SEnji Cooper 
21057718be8SEnji Cooper 	return atf_no_error();
21157718be8SEnji Cooper }
212