t_c16rtomb.c revision 1.1 1 /* $NetBSD: t_c16rtomb.c,v 1.1 2024/08/15 14:16:34 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.1 2024/08/15 14:16:34 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
108 ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_1_test);
109 ATF_TC_BODY(c16rtomb_iso_8859_1_test, tc)
110 {
111 size_t n;
112
113 require_lc_ctype("en_US.ISO8859-1");
114
115 /* Unicode character 'Euro sign'. */
116 memset(&s, 0, sizeof(s));
117 memset(buf, 0xcc, sizeof(buf));
118 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0x20ac, &s)), (size_t)-1,
119 "n=%zu", n);
120 ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
121 ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
122 }
123
124 ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_15_test);
125 ATF_TC_BODY(c16rtomb_iso_8859_15_test, tc)
126 {
127 size_t n;
128
129 require_lc_ctype("en_US.ISO8859-15");
130
131 /* Unicode character 'Euro sign'. */
132 memset(&s, 0, sizeof(s));
133 memset(buf, 0xcc, sizeof(buf));
134 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0x20ac, &s)), 1, "n=%zu", n);
135 ATF_CHECK_MSG(((unsigned char)buf[0] == 0xa4 &&
136 (unsigned char)buf[1] == 0xcc),
137 "buf=[%02x %02x]", buf[0], buf[1]);
138 }
139
140 ATF_TC_WITHOUT_HEAD(c16rtomb_utf_8_test);
141 ATF_TC_BODY(c16rtomb_utf_8_test, tc)
142 {
143 size_t n;
144
145 require_lc_ctype("en_US.UTF-8");
146
147 /* Unicode character 'Pile of poo'. */
148 memset(&s, 0, sizeof(s));
149 memset(buf, 0xcc, sizeof(buf));
150 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
151 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xdca9, &s)), 4, "n=%zu", n);
152 ATF_CHECK_MSG(((unsigned char)buf[0] == 0xf0 &&
153 (unsigned char)buf[1] == 0x9f &&
154 (unsigned char)buf[2] == 0x92 &&
155 (unsigned char)buf[3] == 0xa9 &&
156 (unsigned char)buf[4] == 0xcc),
157 "buf=[%02x %02x %02x %02x %02x]",
158 buf[0], buf[1], buf[2], buf[3], buf[4]);
159
160 /* Invalid code; 'Pile of poo' without the trail surrogate. */
161 memset(&s, 0, sizeof(s));
162 memset(buf, 0xcc, sizeof(buf));
163 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
164 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, L'A', &s)), (size_t)-1,
165 "n=%zu", n);
166 ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
167 ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
168
169 /* Invalid code; 'Pile of poo' without the lead surrogate. */
170 memset(&s, 0, sizeof(s));
171 memset(buf, 0xcc, sizeof(buf));
172 ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xdca9, &s)), (size_t)-1,
173 "n=%zu", n);
174 ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
175 ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
176 }
177
178 ATF_TP_ADD_TCS(tp)
179 {
180
181 ATF_TP_ADD_TC(tp, c16rtomb_c_locale_test);
182 ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_1_test);
183 ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_15_test);
184 ATF_TP_ADD_TC(tp, c16rtomb_utf_8_test);
185
186 return (atf_no_error());
187 }
188