t_c8rtomb.c revision 1.1 1 /* $NetBSD: t_c8rtomb.c,v 1.1 2024/08/15 21:19:45 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 c8rtomb() as specified by C23.
33 */
34
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: t_c8rtomb.c,v 1.1 2024/08/15 21:19:45 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(c8rtomb_c_locale_test);
65 ATF_TC_BODY(c8rtomb_c_locale_test, tc)
66 {
67 size_t n;
68
69 require_lc_ctype("C");
70
71 /*
72 * If the buffer argument is NULL, c8 is implicitly 0,
73 * c8rtomb() resets its internal state.
74 */
75 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, '\0', NULL)), 1, "n=%zu", n);
76 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0x80, NULL)), 1, "n=%zu", n);
77 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xc0, NULL)), 1, "n=%zu", n);
78 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xe0, NULL)), 1, "n=%zu", n);
79 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xf0, NULL)), 1, "n=%zu", n);
80 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xf8, NULL)), 1, "n=%zu", n);
81 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xfc, NULL)), 1, "n=%zu", n);
82 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xfe, NULL)), 1, "n=%zu", n);
83 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xff, NULL)), 1, "n=%zu", n);
84
85
86 /* Null wide character. */
87 memset(&s, 0, sizeof(s));
88 memset(buf, 0xcc, sizeof(buf));
89 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0, &s)), 1, "n=%zu", n);
90 ATF_CHECK_MSG(((unsigned char)buf[0] == 0 &&
91 (unsigned char)buf[1] == 0xcc),
92 "buf=[%02x %02x]", buf[0], buf[1]);
93
94 /* Latin letter A, internal state. */
95 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, '\0', NULL)), 1, "n=%zu", n);
96 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 'A', NULL)), 1, "n=%zu", n);
97
98 /* Latin letter A. */
99 memset(&s, 0, sizeof(s));
100 memset(buf, 0xcc, sizeof(buf));
101 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 'A', &s)), 1, "n=%zu", n);
102 ATF_CHECK_MSG(((unsigned char)buf[0] == 'A' &&
103 (unsigned char)buf[1] == 0xcc),
104 "buf=[%02x %02x]", buf[0], buf[1]);
105
106 /* Unicode character 'Pile of poo'. */
107 memset(&s, 0, sizeof(s));
108 memset(buf, 0xcc, sizeof(buf));
109 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
110 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
111 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
112 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xa9, &s)), (size_t)-1,
113 "n=%zu", n);
114 ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
115 ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
116 }
117
118 ATF_TC_WITHOUT_HEAD(c8rtomb_iso_8859_1_test);
119 ATF_TC_BODY(c8rtomb_iso_8859_1_test, tc)
120 {
121 size_t n;
122
123 require_lc_ctype("en_US.ISO8859-1");
124
125 /* Unicode character 'Euro sign'. */
126 memset(&s, 0, sizeof(s));
127 memset(buf, 0xcc, sizeof(buf));
128 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xe2, &s)), 0, "n=%zu", n);
129 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x82, &s)), 0, "n=%zu", n);
130 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xac, &s)), (size_t)-1,
131 "n=%zu", n);
132 ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
133 ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
134 }
135
136 ATF_TC_WITHOUT_HEAD(c8rtomb_iso_8859_15_test);
137 ATF_TC_BODY(c8rtomb_iso_8859_15_test, tc)
138 {
139 size_t n;
140
141 require_lc_ctype("en_US.ISO8859-15");
142
143 /* Unicode character 'Euro sign'. */
144 memset(&s, 0, sizeof(s));
145 memset(buf, 0xcc, sizeof(buf));
146 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xe2, &s)), 0, "n=%zu", n);
147 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x82, &s)), 0, "n=%zu", n);
148 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xac, &s)), 1, "n=%zu", n);
149 ATF_CHECK_MSG(((unsigned char)buf[0] == 0xa4 &&
150 (unsigned char)buf[1] == 0xcc),
151 "buf=[%02x %02x]", buf[0], buf[1]);
152 }
153
154 ATF_TC_WITHOUT_HEAD(c8rtomb_utf_8_test);
155 ATF_TC_BODY(c8rtomb_utf_8_test, tc)
156 {
157 size_t n;
158
159 require_lc_ctype("en_US.UTF-8");
160
161 /* Unicode character 'Pile of poo'. */
162 memset(&s, 0, sizeof(s));
163 memset(buf, 0xcc, sizeof(buf));
164 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
165 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
166 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
167 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xa9, &s)), 4, "n=%zu", n);
168 ATF_CHECK_MSG(((unsigned char)buf[0] == 0xf0 &&
169 (unsigned char)buf[1] == 0x9f &&
170 (unsigned char)buf[2] == 0x92 &&
171 (unsigned char)buf[3] == 0xa9 &&
172 (unsigned char)buf[4] == 0xcc),
173 "buf=[%02x %02x %02x %02x %02x]",
174 buf[0], buf[1], buf[2], buf[3], buf[4]);
175
176 /* Invalid code; 'Pile of poo' without the last byte. */
177 memset(&s, 0, sizeof(s));
178 memset(buf, 0xcc, sizeof(buf));
179 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
180 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
181 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
182 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 'A', &s)), (size_t)-1,
183 "n=%zu", n);
184 ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
185 ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
186
187 /* Invalid code; 'Pile of poo' without the first byte. */
188 memset(&s, 0, sizeof(s));
189 memset(buf, 0xcc, sizeof(buf));
190 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), (size_t)-1,
191 "n=%zu", n);
192 ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
193 ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
194 }
195
196 ATF_TP_ADD_TCS(tp)
197 {
198
199 ATF_TP_ADD_TC(tp, c8rtomb_c_locale_test);
200 ATF_TP_ADD_TC(tp, c8rtomb_iso_8859_1_test);
201 ATF_TP_ADD_TC(tp, c8rtomb_iso_8859_15_test);
202 ATF_TP_ADD_TC(tp, c8rtomb_utf_8_test);
203
204 return (atf_no_error());
205 }
206