xref: /netbsd-src/tests/lib/libc/locale/t_c8rtomb.c (revision a35ceff4b39ccce6de8bd3b28adf00e694090abc)
1*a35ceff4Sriastradh /*	$NetBSD: t_c8rtomb.c,v 1.7 2024/08/19 16:22:10 riastradh Exp $	*/
2c4e44ee2Sriastradh 
3c4e44ee2Sriastradh /*-
4c4e44ee2Sriastradh  * Copyright (c) 2002 Tim J. Robbins
5c4e44ee2Sriastradh  * All rights reserved.
6c4e44ee2Sriastradh  *
7c4e44ee2Sriastradh  * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org>
8c4e44ee2Sriastradh  * All rights reserved.
9c4e44ee2Sriastradh  *
10c4e44ee2Sriastradh  * Redistribution and use in source and binary forms, with or without
11c4e44ee2Sriastradh  * modification, are permitted provided that the following conditions
12c4e44ee2Sriastradh  * are met:
13c4e44ee2Sriastradh  * 1. Redistributions of source code must retain the above copyright
14c4e44ee2Sriastradh  *    notice, this list of conditions and the following disclaimer.
15c4e44ee2Sriastradh  * 2. Redistributions in binary form must reproduce the above copyright
16c4e44ee2Sriastradh  *    notice, this list of conditions and the following disclaimer in the
17c4e44ee2Sriastradh  *    documentation and/or other materials provided with the distribution.
18c4e44ee2Sriastradh  *
19c4e44ee2Sriastradh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20c4e44ee2Sriastradh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21c4e44ee2Sriastradh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22c4e44ee2Sriastradh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23c4e44ee2Sriastradh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24c4e44ee2Sriastradh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25c4e44ee2Sriastradh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26c4e44ee2Sriastradh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27c4e44ee2Sriastradh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28c4e44ee2Sriastradh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29c4e44ee2Sriastradh  * SUCH DAMAGE.
30c4e44ee2Sriastradh  */
31c4e44ee2Sriastradh /*
32c4e44ee2Sriastradh  * Test program for c8rtomb() as specified by C23.
33c4e44ee2Sriastradh  */
34c4e44ee2Sriastradh 
35c4e44ee2Sriastradh #include <sys/cdefs.h>
36*a35ceff4Sriastradh __RCSID("$NetBSD: t_c8rtomb.c,v 1.7 2024/08/19 16:22:10 riastradh Exp $");
37c4e44ee2Sriastradh 
38c4e44ee2Sriastradh #include <errno.h>
39c4e44ee2Sriastradh #include <limits.h>
40c4e44ee2Sriastradh #include <locale.h>
41c4e44ee2Sriastradh #include <stdio.h>
42c4e44ee2Sriastradh #include <string.h>
43c4e44ee2Sriastradh #include <uchar.h>
44c4e44ee2Sriastradh 
45c4e44ee2Sriastradh #include <atf-c.h>
46c4e44ee2Sriastradh 
47c4e44ee2Sriastradh static void
48c4e44ee2Sriastradh require_lc_ctype(const char *locale_name)
49c4e44ee2Sriastradh {
50c4e44ee2Sriastradh 	char *lc_ctype_set;
51c4e44ee2Sriastradh 
52c4e44ee2Sriastradh 	lc_ctype_set = setlocale(LC_CTYPE, locale_name);
53c4e44ee2Sriastradh 	if (lc_ctype_set == NULL)
54c4e44ee2Sriastradh 		atf_tc_fail("setlocale(LC_CTYPE, \"%s\") failed; errno=%d",
55c4e44ee2Sriastradh 		    locale_name, errno);
56c4e44ee2Sriastradh 
57c4e44ee2Sriastradh 	ATF_REQUIRE_EQ_MSG(strcmp(lc_ctype_set, locale_name), 0,
58c4e44ee2Sriastradh 	    "lc_ctype_set=%s locale_name=%s", lc_ctype_set, locale_name);
59c4e44ee2Sriastradh }
60c4e44ee2Sriastradh 
61c4e44ee2Sriastradh static mbstate_t s;
62e54bcaa3Sriastradh static char buf[7*MB_LEN_MAX + 1];
63c4e44ee2Sriastradh 
64c4e44ee2Sriastradh ATF_TC_WITHOUT_HEAD(c8rtomb_c_locale_test);
65c4e44ee2Sriastradh ATF_TC_BODY(c8rtomb_c_locale_test, tc)
66c4e44ee2Sriastradh {
67c4e44ee2Sriastradh 	size_t n;
68c4e44ee2Sriastradh 
69c4e44ee2Sriastradh 	require_lc_ctype("C");
70c4e44ee2Sriastradh 
71c4e44ee2Sriastradh 	/*
72c4e44ee2Sriastradh 	 * If the buffer argument is NULL, c8 is implicitly 0,
73c4e44ee2Sriastradh 	 * c8rtomb() resets its internal state.
74c4e44ee2Sriastradh 	 */
75c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, '\0', NULL)), 1, "n=%zu", n);
76c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0x80, NULL)), 1, "n=%zu", n);
77c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xc0, NULL)), 1, "n=%zu", n);
78c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xe0, NULL)), 1, "n=%zu", n);
79c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xf0, NULL)), 1, "n=%zu", n);
80c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xf8, NULL)), 1, "n=%zu", n);
81c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xfc, NULL)), 1, "n=%zu", n);
82c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xfe, NULL)), 1, "n=%zu", n);
83c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xff, NULL)), 1, "n=%zu", n);
84c4e44ee2Sriastradh 
85c4e44ee2Sriastradh 	/* Null wide character. */
86c4e44ee2Sriastradh 	memset(&s, 0, sizeof(s));
87c4e44ee2Sriastradh 	memset(buf, 0xcc, sizeof(buf));
88c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0, &s)), 1, "n=%zu", n);
89c4e44ee2Sriastradh 	ATF_CHECK_MSG(((unsigned char)buf[0] == 0 &&
90c4e44ee2Sriastradh 		(unsigned char)buf[1] == 0xcc),
91c4e44ee2Sriastradh 	    "buf=[%02x %02x]", buf[0], buf[1]);
92c4e44ee2Sriastradh 
93c4e44ee2Sriastradh 	/* Latin letter A, internal state. */
94c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, '\0', NULL)), 1, "n=%zu", n);
95c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 'A', NULL)), 1, "n=%zu", n);
96c4e44ee2Sriastradh 
97c4e44ee2Sriastradh 	/* Latin letter A. */
98c4e44ee2Sriastradh 	memset(&s, 0, sizeof(s));
99c4e44ee2Sriastradh 	memset(buf, 0xcc, sizeof(buf));
100c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 'A', &s)), 1, "n=%zu", n);
101c4e44ee2Sriastradh 	ATF_CHECK_MSG(((unsigned char)buf[0] == 'A' &&
102c4e44ee2Sriastradh 		(unsigned char)buf[1] == 0xcc),
103c4e44ee2Sriastradh 	    "buf=[%02x %02x]", buf[0], buf[1]);
104c4e44ee2Sriastradh 
105c4e44ee2Sriastradh 	/* Unicode character 'Pile of poo'. */
106c4e44ee2Sriastradh 	memset(&s, 0, sizeof(s));
107c4e44ee2Sriastradh 	memset(buf, 0xcc, sizeof(buf));
108c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
109c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
110c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
111c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xa9, &s)), (size_t)-1,
112c4e44ee2Sriastradh 	    "n=%zu", n);
113c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
114c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
115ae2efd9aSriastradh 
116ae2efd9aSriastradh 	/* Incomplete Unicode character 'Pile of poo', interrupted by NUL. */
117ae2efd9aSriastradh 	memset(&s, 0, sizeof(s));
118ae2efd9aSriastradh 	memset(buf, 0xcc, sizeof(buf));
119ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
120ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
121ae2efd9aSriastradh 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
122ae2efd9aSriastradh 		(unsigned char)buf[1] == 0xcc),
123ae2efd9aSriastradh 	    "buf=[%02x %02x]", buf[0], buf[1]);
124ae2efd9aSriastradh 
125ae2efd9aSriastradh 	memset(&s, 0, sizeof(s));
126ae2efd9aSriastradh 	memset(buf, 0xcc, sizeof(buf));
127ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
128ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
129ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
130ae2efd9aSriastradh 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
131ae2efd9aSriastradh 		(unsigned char)buf[1] == 0xcc),
132ae2efd9aSriastradh 	    "buf=[%02x %02x]", buf[0], buf[1]);
133ae2efd9aSriastradh 
134ae2efd9aSriastradh 	memset(&s, 0, sizeof(s));
135ae2efd9aSriastradh 	memset(buf, 0xcc, sizeof(buf));
136ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
137ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
138ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
139ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
140ae2efd9aSriastradh 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
141ae2efd9aSriastradh 		(unsigned char)buf[1] == 0xcc),
142ae2efd9aSriastradh 	    "buf=[%02x %02x]", buf[0], buf[1]);
143c4e44ee2Sriastradh }
144c4e44ee2Sriastradh 
145e54bcaa3Sriastradh ATF_TC_WITHOUT_HEAD(c8rtomb_iso2022jp_locale_test);
146e54bcaa3Sriastradh ATF_TC_BODY(c8rtomb_iso2022jp_locale_test, tc)
147e54bcaa3Sriastradh {
148e54bcaa3Sriastradh 	char *p;
149e54bcaa3Sriastradh 	size_t n;
150e54bcaa3Sriastradh 
151e54bcaa3Sriastradh 	require_lc_ctype("ja_JP.ISO-2022-JP");
152e54bcaa3Sriastradh 
153e54bcaa3Sriastradh 	/*
154e54bcaa3Sriastradh 	 * If the buffer argument is NULL, c8 is implicitly 0,
155e54bcaa3Sriastradh 	 * c8rtomb() resets its internal state.
156e54bcaa3Sriastradh 	 */
157e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, '\0', NULL)), 1, "n=%zu", n);
158e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0x80, NULL)), 1, "n=%zu", n);
159e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xc0, NULL)), 1, "n=%zu", n);
160e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xe0, NULL)), 1, "n=%zu", n);
161e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xf0, NULL)), 1, "n=%zu", n);
162e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xf8, NULL)), 1, "n=%zu", n);
163e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xfc, NULL)), 1, "n=%zu", n);
164e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xfe, NULL)), 1, "n=%zu", n);
165e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xff, NULL)), 1, "n=%zu", n);
166e54bcaa3Sriastradh 
167e54bcaa3Sriastradh 	/* Null wide character. */
168e54bcaa3Sriastradh 	memset(&s, 0, sizeof(s));
169e54bcaa3Sriastradh 	memset(buf, 0xcc, sizeof(buf));
170e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0, &s)), 1, "n=%zu", n);
171e54bcaa3Sriastradh 	ATF_CHECK_MSG(((unsigned char)buf[0] == 0 &&
172e54bcaa3Sriastradh 		(unsigned char)buf[1] == 0xcc),
173e54bcaa3Sriastradh 	    "buf=[%02x %02x]", buf[0], buf[1]);
174e54bcaa3Sriastradh 
175e54bcaa3Sriastradh 	/* Latin letter A, internal state. */
176e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, '\0', NULL)), 1, "n=%zu", n);
177e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 'A', NULL)), 1, "n=%zu", n);
178e54bcaa3Sriastradh 
179e54bcaa3Sriastradh 	/*
180e54bcaa3Sriastradh 	 * 1. U+0042 LATIN CAPITAL LETTER A
181e54bcaa3Sriastradh 	 * 2. U+00A5 YEN SIGN
182e54bcaa3Sriastradh 	 * 3. U+00A5 YEN SIGN (again, no shift needed)
183e54bcaa3Sriastradh 	 * 4. U+30A2 KATAKANA LETTER A
184e54bcaa3Sriastradh 	 * 5. U+30A2 KATAKANA LETTER A (again, no shift needed)
185e54bcaa3Sriastradh 	 * 6. incomplete UTF-8 multibyte sequence -- no output
186e54bcaa3Sriastradh 	 * 7. U+0000 NUL (plus shift sequence to initial state)
187e54bcaa3Sriastradh 	 */
188e54bcaa3Sriastradh 	memset(&s, 0, sizeof(s));
189e54bcaa3Sriastradh 	memset(buf, 0xcc, sizeof(buf));
190e54bcaa3Sriastradh 	p = buf;
191e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 'A', &s)), 1, "n=%zu", n); /* 1 */
192e54bcaa3Sriastradh 	p += 1;
193e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xc2, &s)), 0, "n=%zu", n); /* 2 */
194e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xa5, &s)), 4, "n=%zu", n);
195e54bcaa3Sriastradh 	p += 4;
196e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xc2, &s)), 0, "n=%zu", n); /* 3 */
197e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xa5, &s)), 1, "n=%zu", n);
198e54bcaa3Sriastradh 	p += 1;
199e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xe3, &s)), 0, "n=%zu", n); /* 4 */
200e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0x82, &s)), 0, "n=%zu", n);
2015078d352Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xa2, &s)), 5, "n=%zu", n);
202e54bcaa3Sriastradh 	p += 5;
203e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xe3, &s)), 0, "n=%zu", n); /* 5 */
204e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0x82, &s)), 0, "n=%zu", n);
205e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xa2, &s)), 2, "n=%zu", n);
206e54bcaa3Sriastradh 	p += 2;
207e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xe3, &s)), 0, "n=%zu", n); /* 6 */
208e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0x82, &s)), 0, "n=%zu", n);
209e54bcaa3Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, '\0', &s)), 4, "n=%zu", n); /* 7 */
210e54bcaa3Sriastradh 	p += 4;
211e54bcaa3Sriastradh 	ATF_CHECK_MSG(((unsigned char)buf[0] == 'A' &&
212e54bcaa3Sriastradh 		(unsigned char)buf[1] == 0x1b && /* shift ISO/IEC 646:JP */
213e54bcaa3Sriastradh 		(unsigned char)buf[2] == '(' &&
214e54bcaa3Sriastradh 		(unsigned char)buf[3] == 'J' &&
215e54bcaa3Sriastradh 		(unsigned char)buf[4] == 0x5c && /* YEN SIGN */
216e54bcaa3Sriastradh 		(unsigned char)buf[5] == 0x5c && /* YEN SIGN */
2176b5314b8Sriastradh 		(unsigned char)buf[6] == 0x1b && /* shift JIS X 0208 */
218e54bcaa3Sriastradh 		(unsigned char)buf[7] == '$' &&
219e54bcaa3Sriastradh 		(unsigned char)buf[8] == 'B' &&
220e54bcaa3Sriastradh 		(unsigned char)buf[9] == 0x25 && /* KATAKANA LETTER A */
221e54bcaa3Sriastradh 		(unsigned char)buf[10] == 0x22 &&
222e54bcaa3Sriastradh 		(unsigned char)buf[11] == 0x25 && /* KATAKANA LETTER A */
223e54bcaa3Sriastradh 		(unsigned char)buf[12] == 0x22 &&
224e54bcaa3Sriastradh 		(unsigned char)buf[13] == 0x1b && /* shift US-ASCII */
225e54bcaa3Sriastradh 		(unsigned char)buf[14] == '(' &&
226e54bcaa3Sriastradh 		(unsigned char)buf[15] == 'B' &&
227e54bcaa3Sriastradh 		(unsigned char)buf[16] == '\0' &&
228e54bcaa3Sriastradh 		(unsigned char)buf[17] == 0xcc),
229e54bcaa3Sriastradh 	    "buf=[%02x %02x %02x %02x  %02x %02x %02x %02x "
230e54bcaa3Sriastradh 	    " %02x %02x %02x %02x  %02x %02x %02x %02x "
231e54bcaa3Sriastradh 	    " %02x %02x]",
232e54bcaa3Sriastradh 	    buf[0], buf[1], buf[2], buf[3],
233e54bcaa3Sriastradh 	    buf[4], buf[5], buf[6], buf[7],
234e54bcaa3Sriastradh 	    buf[8], buf[9], buf[10], buf[11],
235e54bcaa3Sriastradh 	    buf[12], buf[13], buf[14], buf[15],
236e54bcaa3Sriastradh 	    buf[16], buf[17]);
237e54bcaa3Sriastradh }
238e54bcaa3Sriastradh 
239c4e44ee2Sriastradh ATF_TC_WITHOUT_HEAD(c8rtomb_iso_8859_1_test);
240c4e44ee2Sriastradh ATF_TC_BODY(c8rtomb_iso_8859_1_test, tc)
241c4e44ee2Sriastradh {
242c4e44ee2Sriastradh 	size_t n;
243c4e44ee2Sriastradh 
244c4e44ee2Sriastradh 	require_lc_ctype("en_US.ISO8859-1");
245c4e44ee2Sriastradh 
246c4e44ee2Sriastradh 	/* Unicode character 'Euro sign'. */
247c4e44ee2Sriastradh 	memset(&s, 0, sizeof(s));
248c4e44ee2Sriastradh 	memset(buf, 0xcc, sizeof(buf));
249c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xe2, &s)), 0, "n=%zu", n);
250c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x82, &s)), 0, "n=%zu", n);
251c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xac, &s)), (size_t)-1,
252c4e44ee2Sriastradh 	    "n=%zu", n);
253c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
254c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
255c4e44ee2Sriastradh }
256c4e44ee2Sriastradh 
257c4e44ee2Sriastradh ATF_TC_WITHOUT_HEAD(c8rtomb_iso_8859_15_test);
258c4e44ee2Sriastradh ATF_TC_BODY(c8rtomb_iso_8859_15_test, tc)
259c4e44ee2Sriastradh {
260c4e44ee2Sriastradh 	size_t n;
261c4e44ee2Sriastradh 
262c4e44ee2Sriastradh 	require_lc_ctype("en_US.ISO8859-15");
263c4e44ee2Sriastradh 
264c4e44ee2Sriastradh 	/* Unicode character 'Euro sign'. */
265c4e44ee2Sriastradh 	memset(&s, 0, sizeof(s));
266c4e44ee2Sriastradh 	memset(buf, 0xcc, sizeof(buf));
267c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xe2, &s)), 0, "n=%zu", n);
268c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x82, &s)), 0, "n=%zu", n);
269c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xac, &s)), 1, "n=%zu", n);
270c4e44ee2Sriastradh 	ATF_CHECK_MSG(((unsigned char)buf[0] == 0xa4 &&
271c4e44ee2Sriastradh 		(unsigned char)buf[1] == 0xcc),
272c4e44ee2Sriastradh 	    "buf=[%02x %02x]", buf[0], buf[1]);
273c4e44ee2Sriastradh }
274c4e44ee2Sriastradh 
275c4e44ee2Sriastradh ATF_TC_WITHOUT_HEAD(c8rtomb_utf_8_test);
276c4e44ee2Sriastradh ATF_TC_BODY(c8rtomb_utf_8_test, tc)
277c4e44ee2Sriastradh {
278c4e44ee2Sriastradh 	size_t n;
279c4e44ee2Sriastradh 
280c4e44ee2Sriastradh 	require_lc_ctype("en_US.UTF-8");
281c4e44ee2Sriastradh 
282c4e44ee2Sriastradh 	/* Unicode character 'Pile of poo'. */
283c4e44ee2Sriastradh 	memset(&s, 0, sizeof(s));
284c4e44ee2Sriastradh 	memset(buf, 0xcc, sizeof(buf));
285c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
286c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
287c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
288c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xa9, &s)), 4, "n=%zu", n);
289c4e44ee2Sriastradh 	ATF_CHECK_MSG(((unsigned char)buf[0] == 0xf0 &&
290c4e44ee2Sriastradh 		(unsigned char)buf[1] == 0x9f &&
291c4e44ee2Sriastradh 		(unsigned char)buf[2] == 0x92 &&
292c4e44ee2Sriastradh 		(unsigned char)buf[3] == 0xa9 &&
293c4e44ee2Sriastradh 		(unsigned char)buf[4] == 0xcc),
294c4e44ee2Sriastradh 	    "buf=[%02x %02x %02x %02x %02x]",
295c4e44ee2Sriastradh 	    buf[0], buf[1], buf[2], buf[3], buf[4]);
296c4e44ee2Sriastradh 
297c4e44ee2Sriastradh 	/* Invalid code; 'Pile of poo' without the last byte. */
298c4e44ee2Sriastradh 	memset(&s, 0, sizeof(s));
299c4e44ee2Sriastradh 	memset(buf, 0xcc, sizeof(buf));
300c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
301c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
302c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
303c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 'A', &s)), (size_t)-1,
304c4e44ee2Sriastradh 	    "n=%zu", n);
305c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
306c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
307c4e44ee2Sriastradh 
308c4e44ee2Sriastradh 	/* Invalid code; 'Pile of poo' without the first byte. */
309c4e44ee2Sriastradh 	memset(&s, 0, sizeof(s));
310c4e44ee2Sriastradh 	memset(buf, 0xcc, sizeof(buf));
311c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), (size_t)-1,
312c4e44ee2Sriastradh 	    "n=%zu", n);
313c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
314c4e44ee2Sriastradh 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
315ae2efd9aSriastradh 
316ae2efd9aSriastradh 	/* Incomplete Unicode character 'Pile of poo', interrupted by NUL. */
317ae2efd9aSriastradh 	memset(&s, 0, sizeof(s));
318ae2efd9aSriastradh 	memset(buf, 0xcc, sizeof(buf));
319ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
320ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
321ae2efd9aSriastradh 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
322ae2efd9aSriastradh 		(unsigned char)buf[1] == 0xcc),
323ae2efd9aSriastradh 	    "buf=[%02x %02x]", buf[0], buf[1]);
324ae2efd9aSriastradh 
325ae2efd9aSriastradh 	memset(&s, 0, sizeof(s));
326ae2efd9aSriastradh 	memset(buf, 0xcc, sizeof(buf));
327ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
328ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
329ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
330ae2efd9aSriastradh 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
331ae2efd9aSriastradh 		(unsigned char)buf[1] == 0xcc),
332ae2efd9aSriastradh 	    "buf=[%02x %02x]", buf[0], buf[1]);
333ae2efd9aSriastradh 
334ae2efd9aSriastradh 	memset(&s, 0, sizeof(s));
335ae2efd9aSriastradh 	memset(buf, 0xcc, sizeof(buf));
336ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
337ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
338ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
339ae2efd9aSriastradh 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
340ae2efd9aSriastradh 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
341ae2efd9aSriastradh 		(unsigned char)buf[1] == 0xcc),
342ae2efd9aSriastradh 	    "buf=[%02x %02x]", buf[0], buf[1]);
343c4e44ee2Sriastradh }
344c4e44ee2Sriastradh 
345c4e44ee2Sriastradh ATF_TP_ADD_TCS(tp)
346c4e44ee2Sriastradh {
347c4e44ee2Sriastradh 
348c4e44ee2Sriastradh 	ATF_TP_ADD_TC(tp, c8rtomb_c_locale_test);
349e54bcaa3Sriastradh 	ATF_TP_ADD_TC(tp, c8rtomb_iso2022jp_locale_test);
350c4e44ee2Sriastradh 	ATF_TP_ADD_TC(tp, c8rtomb_iso_8859_1_test);
351c4e44ee2Sriastradh 	ATF_TP_ADD_TC(tp, c8rtomb_iso_8859_15_test);
352c4e44ee2Sriastradh 	ATF_TP_ADD_TC(tp, c8rtomb_utf_8_test);
353c4e44ee2Sriastradh 
354c4e44ee2Sriastradh 	return (atf_no_error());
355c4e44ee2Sriastradh }
356