t_c16rtomb.c revision 1.3 1 /* $NetBSD: t_c16rtomb.c,v 1.3 2024/08/18 02:19:35 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2002 Tim J. Robbins
5 * All rights reserved.
6 *
7 * Copyright (c) 2013 Ed Schouten <ed (at) FreeBSD.org>
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31 /*
32 * Test program for c16rtomb() as specified by ISO/IEC 9899:2011.
33 */
34
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: t_c16rtomb.c,v 1.3 2024/08/18 02:19:35 riastradh Exp $");
37
38 #include <errno.h>
39 #include <limits.h>
40 #include <locale.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <uchar.h>
44
45 #include <atf-c.h>
46
47 static void
48 require_lc_ctype(const char *locale_name)
49 {
50 char *lc_ctype_set;
51
52 lc_ctype_set = setlocale(LC_CTYPE, locale_name);
53 if (lc_ctype_set == NULL)
54 atf_tc_fail("setlocale(LC_CTYPE, \"%s\") failed; errno=%d",
55 locale_name, errno);
56
57 ATF_REQUIRE_EQ_MSG(strcmp(lc_ctype_set, locale_name), 0,
58 "lc_ctype_set=%s locale_name=%s", lc_ctype_set, locale_name);
59 }
60
61 static mbstate_t s;
62 static char buf[MB_LEN_MAX + 1];
63
64 ATF_TC_WITHOUT_HEAD(c16rtomb_c_locale_test);
65 ATF_TC_BODY(c16rtomb_c_locale_test, tc)
66 {
67 size_t n;
68
69 require_lc_ctype("C");
70
71 /*
72 * If the buffer argument is NULL, c16 is implicitly 0,
73 * c16rtomb() resets its internal state.
74 */
75 ATF_CHECK_EQ_MSG((n = c16rtomb(NULL, L'\0', NULL)), 1, "n=%zu", n);
76 ATF_CHECK_EQ_MSG((n = c16rtomb(NULL, 0xdc00, NULL)), 1, "n=%zu", n);
77
78 /* Null wide character. */
79 memset(&s, 0, sizeof(s));
80 memset(buf, 0xcc, sizeof(buf));
81 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0, &s)), 1, "n=%zu", n);
82 ATF_CHECK_MSG(((unsigned char)buf[0] == 0 &&
83 (unsigned char)buf[1] == 0xcc),
84 "buf=[%02x %02x]", buf[0], buf[1]);
85
86 /* Latin letter A, internal state. */
87 ATF_CHECK_EQ_MSG((n = c16rtomb(NULL, L'\0', NULL)), 1, "n=%zu", n);
88 ATF_CHECK_EQ_MSG((n = c16rtomb(NULL, L'A', NULL)), 1, "n=%zu", n);
89
90 /* Latin letter A. */
91 memset(&s, 0, sizeof(s));
92 memset(buf, 0xcc, sizeof(buf));
93 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, L'A', &s)), 1, "n=%zu", n);
94 ATF_CHECK_MSG(((unsigned char)buf[0] == 'A' &&
95 (unsigned char)buf[1] == 0xcc),
96 "buf=[%02x %02x]", buf[0], buf[1]);
97
98 /* Unicode character 'Pile of poo'. */
99 memset(&s, 0, sizeof(s));
100 memset(buf, 0xcc, sizeof(buf));
101 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
102 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xdca9, &s)), (size_t)-1,
103 "n=%zu", n);
104 ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
105 ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
106
107 /* Incomplete Unicode character 'Pile of poo', interrupted by NUL. */
108 memset(&s, 0, sizeof(s));
109 memset(buf, 0xcc, sizeof(buf));
110 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
111 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, L'\0', &s)), 1, "n=%zu", n);
112 ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
113 (unsigned char)buf[1] == 0xcc),
114 "buf=[%02x %02x]", buf[0], buf[1]);
115 }
116
117 ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_1_test);
118 ATF_TC_BODY(c16rtomb_iso_8859_1_test, tc)
119 {
120 size_t n;
121
122 require_lc_ctype("en_US.ISO8859-1");
123
124 /* Unicode character 'Euro sign'. */
125 memset(&s, 0, sizeof(s));
126 memset(buf, 0xcc, sizeof(buf));
127 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0x20ac, &s)), (size_t)-1,
128 "n=%zu", n);
129 ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
130 ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
131 }
132
133 ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_15_test);
134 ATF_TC_BODY(c16rtomb_iso_8859_15_test, tc)
135 {
136 size_t n;
137
138 require_lc_ctype("en_US.ISO8859-15");
139
140 /* Unicode character 'Euro sign'. */
141 memset(&s, 0, sizeof(s));
142 memset(buf, 0xcc, sizeof(buf));
143 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0x20ac, &s)), 1, "n=%zu", n);
144 ATF_CHECK_MSG(((unsigned char)buf[0] == 0xa4 &&
145 (unsigned char)buf[1] == 0xcc),
146 "buf=[%02x %02x]", buf[0], buf[1]);
147 }
148
149 ATF_TC_WITHOUT_HEAD(c16rtomb_utf_8_test);
150 ATF_TC_BODY(c16rtomb_utf_8_test, tc)
151 {
152 size_t n;
153
154 require_lc_ctype("en_US.UTF-8");
155
156 /* Unicode character 'Pile of poo'. */
157 memset(&s, 0, sizeof(s));
158 memset(buf, 0xcc, sizeof(buf));
159 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
160 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xdca9, &s)), 4, "n=%zu", n);
161 ATF_CHECK_MSG(((unsigned char)buf[0] == 0xf0 &&
162 (unsigned char)buf[1] == 0x9f &&
163 (unsigned char)buf[2] == 0x92 &&
164 (unsigned char)buf[3] == 0xa9 &&
165 (unsigned char)buf[4] == 0xcc),
166 "buf=[%02x %02x %02x %02x %02x]",
167 buf[0], buf[1], buf[2], buf[3], buf[4]);
168
169 /* Invalid code; 'Pile of poo' without the trail surrogate. */
170 memset(&s, 0, sizeof(s));
171 memset(buf, 0xcc, sizeof(buf));
172 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
173 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, L'A', &s)), (size_t)-1,
174 "n=%zu", n);
175 ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
176 ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
177
178 /* Invalid code; 'Pile of poo' without the lead surrogate. */
179 memset(&s, 0, sizeof(s));
180 memset(buf, 0xcc, sizeof(buf));
181 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xdca9, &s)), (size_t)-1,
182 "n=%zu", n);
183 ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
184 ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
185
186 /* Incomplete Unicode character 'Pile of poo', interrupted by NUL. */
187 memset(&s, 0, sizeof(s));
188 memset(buf, 0xcc, sizeof(buf));
189 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
190 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, L'\0', &s)), 1,
191 "n=%zu", n);
192 ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
193 (unsigned char)buf[1] == 0xcc),
194 "buf=[%02x %02x]", buf[0], buf[1]);
195 }
196
197 ATF_TP_ADD_TCS(tp)
198 {
199
200 ATF_TP_ADD_TC(tp, c16rtomb_c_locale_test);
201 ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_1_test);
202 ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_15_test);
203 ATF_TP_ADD_TC(tp, c16rtomb_utf_8_test);
204
205 return (atf_no_error());
206 }
207