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