summaryrefslogtreecommitdiffstats
path: root/contrib/netbsd-tests/lib/libc/locale/t_mbstowcs.c
blob: 0c443169362b393066e24b37b9637c6bd22fe769 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* $NetBSD: t_mbstowcs.c,v 1.1 2011/07/15 07:35:21 jruoho Exp $ */

/*-
 * Copyright (c) 2011 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*-
 * Copyright (c)2003 Citrus Project,
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__COPYRIGHT("@(#) Copyright (c) 2011\
 The NetBSD Foundation, inc. All rights reserved.");
__RCSID("$NetBSD: t_mbstowcs.c,v 1.1 2011/07/15 07:35:21 jruoho Exp $");

#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vis.h>
#include <wchar.h>

#include <atf-c.h>

#define REQUIRE_ERRNO(x, v) \
	ATF_REQUIRE_MSG((x) != (v), "%s: %s", #x, strerror(errno))

#define SIZE 256

static struct test {
	const char *locale;
	const char *data;
	wchar_t wchars[64];
	int widths[64];
	int width;
} tests[] = {
{
	"en_US.UTF-8",
	"[\001\177][\302\200\337\277][\340\240\200\357\277\277][\360\220\200"
	"\200\364\217\277\277]",
	{
		0x5B, 0x01, 0x7F, 0x5D, 0x5B, 0x80, 0x07FF, 0x5D, 0x5B, 0x0800,
		0xFFFF, 0x5D, 0x5B, 0x10000, 0x10FFFF, 0x5D, 0x0A
	},
#ifdef __FreeBSD__
	{	 1, -1, -1,  1,  1, -1, 1,  1,  1, 1, -1,  1,  1, -1, -1,
#else
	{	 1, -1, -1,  1,  1, -1, -1,  1,  1, -1, -1,  1,  1, -1, -1,
#endif
		 1,  1, -1, -1,  1,  1, -1, -1,  1, -1
	}, 
	-1
}, {
	"ja_JP.ISO2022-JP",
	"\033$B#J#I#S$G$9!#\033(Baaaa\033$B$\"$$$&$($*\033(B",
	{
		0x4200234A, 0x42002349, 0x42002353, 0x42002447, 0x42002439,
		0x42002123, 0x61, 0x61, 0x61, 0x61, 0x42002422, 0x42002424,
		0x42002426, 0x42002428, 0x4200242A, 0x0A
	},
	{ 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1 },
	26
}, {
	"ja_JP.SJIS",
	"\202r\202i\202h\202r\202\305\202\267\201Baaaa\202\240\202\242"
	"\202\244\202\246\202\250",
	{
		0x8272, 0x8269, 0x8268, 0x8272, 0x82C5, 0x82B7, 0x8142, 0x61,
		0x61, 0x61, 0x61, 0x82A0, 0x82A2, 0x82A4, 0x82A6, 0x82A8, 0x0A
	},
	{ 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1 },
	28
}, {
	"ja_JP.eucJP",
	"\243\305\243\325\243\303\244\307\244\271\241\243aaaa\244\242\244"
	"\244\244\246\244\250\244\252",
	{
		0xA3C5, 0xA3D5, 0xA3C3, 0xA4C7, 0xA4B9, 0xA1A3, 0x61, 0x61, 0x61,
		0x61, 0xA4A2, 0xA4A4, 0xA4A6, 0xA4A8, 0xA4AA, 0x0A
	},
	{ 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1 },
	26
}, {
	NULL,
	NULL,
	{},
	{},
	0
}
};

ATF_TC(mbstowcs_basic);
ATF_TC_HEAD(mbstowcs_basic, tc)
{
	atf_tc_set_md_var(tc, "descr",
		"Checks wide character functions with different locales");
}
ATF_TC_BODY(mbstowcs_basic, tc)
{
	struct test *t;

	for (t = &tests[0]; t->data != NULL; ++t) {
		wchar_t	wbuf[SIZE];
		char buf[SIZE];
		char visbuf[SIZE];
		char *str;
		int i;

		ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
#ifdef __NetBSD__
		ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
#else
		if (setlocale(LC_CTYPE, t->locale) == NULL) {
			fprintf(stderr, "Locale %s not found.\n", t->locale);
			continue;
		}
#endif

		(void)strvis(visbuf, t->data, VIS_WHITE | VIS_OCTAL);
		(void)printf("Checking string: \"%s\"\n", visbuf);

		ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
		(void)printf("Using locale: %s\n", str);

		REQUIRE_ERRNO((ssize_t)mbstowcs(wbuf, t->data, SIZE-1), -1);
		REQUIRE_ERRNO((ssize_t)wcstombs(buf, wbuf, SIZE-1), -1);

		if (strcmp(buf, t->data) != 0) {
			(void)strvis(visbuf, buf, VIS_WHITE | VIS_OCTAL);
			(void)printf("Conversion to wcs and back failed: "
				"\"%s\"\n", visbuf);
			atf_tc_fail("Test failed");
		}

		/* The output here is implementation-dependent. */

		for (i = 0; wbuf[i] != 0; ++i) {
			if (wbuf[i] == t->wchars[i] &&
			    wcwidth(wbuf[i]) == t->widths[i])
				continue;

			(void)printf("At position %d:\n", i);
			(void)printf("  expected: 0x%04X (%d)\n",
				t->wchars[i], t->widths[i]);
			(void)printf("  got     : 0x%04X (%d)\n", wbuf[i],
				wcwidth(wbuf[i]));
			atf_tc_fail("Test failed");
		}

		if (wcswidth(wbuf, SIZE-1) != t->width) {
			(void)printf("Incorrect wcswidth:\n");
			(void)printf("  expected: %d\n", t->width);
			(void)printf("  got     : %d\n", wcswidth(wbuf, SIZE-1));
			atf_tc_fail("Test failed");
		}

		(void)printf("Ok.\n");
	}
}

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, mbstowcs_basic);

	return atf_no_error();
}
OpenPOWER on IntegriCloud