2 * Copyright (c) 2002 Tim J. Robbins
5 * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 * Test program for c16rtomb() as specified by ISO/IEC 9899:2011.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
46 require_lc_ctype(const char *locale_name)
50 lc_ctype_set = setlocale(LC_CTYPE, locale_name);
51 if (lc_ctype_set == NULL)
52 atf_tc_fail("setlocale(LC_CTYPE, \"%s\") failed; errno=%d",
55 ATF_REQUIRE(strcmp(lc_ctype_set, locale_name) == 0);
59 static char buf[MB_LEN_MAX + 1];
61 ATF_TC_WITHOUT_HEAD(c16rtomb_c_locale_test);
62 ATF_TC_BODY(c16rtomb_c_locale_test, tc)
65 require_lc_ctype("C");
68 * If the buffer argument is NULL, c16 is implicitly 0,
69 * c16rtomb() resets its internal state.
71 ATF_REQUIRE(c16rtomb(NULL, L'\0', NULL) == 1);
72 ATF_REQUIRE(c16rtomb(NULL, 0xdc00, NULL) == 1);
74 /* Null wide character. */
75 memset(&s, 0, sizeof(s));
76 memset(buf, 0xcc, sizeof(buf));
77 ATF_REQUIRE(c16rtomb(buf, 0, &s) == 1);
78 ATF_REQUIRE((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc);
80 /* Latin letter A, internal state. */
81 ATF_REQUIRE(c16rtomb(NULL, L'\0', NULL) == 1);
82 ATF_REQUIRE(c16rtomb(NULL, L'A', NULL) == 1);
85 memset(&s, 0, sizeof(s));
86 memset(buf, 0xcc, sizeof(buf));
87 ATF_REQUIRE(c16rtomb(buf, L'A', &s) == 1);
88 ATF_REQUIRE((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc);
90 /* Unicode character 'Pile of poo'. */
91 memset(&s, 0, sizeof(s));
92 memset(buf, 0xcc, sizeof(buf));
93 ATF_REQUIRE(c16rtomb(buf, 0xd83d, &s) == 0);
94 ATF_REQUIRE(c16rtomb(buf, 0xdca9, &s) == (size_t)-1);
95 ATF_REQUIRE(errno == EILSEQ);
96 ATF_REQUIRE((unsigned char)buf[0] == 0xcc);
99 ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_1_test);
100 ATF_TC_BODY(c16rtomb_iso_8859_1_test, tc)
103 require_lc_ctype("en_US.ISO8859-1");
105 /* Unicode character 'Euro sign'. */
106 memset(&s, 0, sizeof(s));
107 memset(buf, 0xcc, sizeof(buf));
108 ATF_REQUIRE(c16rtomb(buf, 0x20ac, &s) == (size_t)-1);
109 ATF_REQUIRE(errno == EILSEQ);
110 ATF_REQUIRE((unsigned char)buf[0] == 0xcc);
113 ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_15_test);
114 ATF_TC_BODY(c16rtomb_iso_8859_15_test, tc)
117 require_lc_ctype("en_US.ISO8859-15");
119 /* Unicode character 'Euro sign'. */
120 memset(&s, 0, sizeof(s));
121 memset(buf, 0xcc, sizeof(buf));
122 ATF_REQUIRE(c16rtomb(buf, 0x20ac, &s) == 1);
123 ATF_REQUIRE((unsigned char)buf[0] == 0xa4 && (unsigned char)buf[1] == 0xcc);
126 ATF_TC_WITHOUT_HEAD(c16rtomb_utf_8_test);
127 ATF_TC_BODY(c16rtomb_utf_8_test, tc)
130 require_lc_ctype("en_US.UTF-8");
132 /* Unicode character 'Pile of poo'. */
133 memset(&s, 0, sizeof(s));
134 memset(buf, 0xcc, sizeof(buf));
135 ATF_REQUIRE(c16rtomb(buf, 0xd83d, &s) == 0);
136 ATF_REQUIRE(c16rtomb(buf, 0xdca9, &s) == 4);
137 ATF_REQUIRE((unsigned char)buf[0] == 0xf0 && (unsigned char)buf[1] == 0x9f &&
138 (unsigned char)buf[2] == 0x92 && (unsigned char)buf[3] == 0xa9 &&
139 (unsigned char)buf[4] == 0xcc);
141 /* Invalid code; 'Pile of poo' without the trail surrogate. */
142 memset(&s, 0, sizeof(s));
143 memset(buf, 0xcc, sizeof(buf));
144 ATF_REQUIRE(c16rtomb(buf, 0xd83d, &s) == 0);
145 ATF_REQUIRE(c16rtomb(buf, L'A', &s) == (size_t)-1);
146 ATF_REQUIRE(errno == EILSEQ);
147 ATF_REQUIRE((unsigned char)buf[0] == 0xcc);
149 /* Invalid code; 'Pile of poo' without the lead surrogate. */
150 memset(&s, 0, sizeof(s));
151 memset(buf, 0xcc, sizeof(buf));
152 ATF_REQUIRE(c16rtomb(buf, 0xdca9, &s) == (size_t)-1);
153 ATF_REQUIRE(errno == EILSEQ);
154 ATF_REQUIRE((unsigned char)buf[0] == 0xcc);
160 ATF_TP_ADD_TC(tp, c16rtomb_c_locale_test);
161 ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_1_test);
162 ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_15_test);
163 ATF_TP_ADD_TC(tp, c16rtomb_utf_8_test);
165 return (atf_no_error());