]>
git.saurik.com Git - apple/libc.git/blob - tests/collate.c
1 #include <TargetConditionals.h>
8 #include <darwintest.h>
10 void __collate_lookup_l(const __darwin_wchar_t
*, int *, int *, int *,
12 void __collate_lookup(const unsigned char *, int *, int *, int *);
14 #define CHARS_WITHOUT_ENTRIES "\xdf"
17 * in C or POSIX locales
18 * __collate_lookup("", ... ) -> len: 0 prim: 0 sec: 0
19 * __collate_lookup("a", ... ) -> len: 1 prim: (int)'a' sec: 0
20 * __collate_lookup("ab", ... ) -> len: 1 prim: (int)'a' sec: 0
22 * in a Latin-1 locale (de_DE.ISO8859-1)
23 * __collate_lookup("", ... ) -> len: 0 prim: 0 sec: 0
24 * __collate_lookup("a", ... ) -> len: 1 prim: > 0 sec: > 0
25 * __collate_lookup("ab", ... ) -> len: 1 prim: > 0 sec: > 0
26 * # a character not in the table - lookup failure
27 * __collate_lookup("\xdf", ... ) -> len: 0 prim: -1 sec: -1
29 * in a UTF-8 locale (de_DE.UTF-8)
30 * __collate_lookup("", ... ) -> len: 0 prim: 0 sec: 0
31 * __collate_lookup("a", ... ) -> len: 1 prim: > 0 sec: > 0
32 * __collate_lookup("ab", ... ) -> len: 1 prim: > 0 sec: > 0
33 * # An invalid multi-byte sequence
34 * __collate_lookup("\xe4", ... ) -> len: 1 prim: (int)'\xe4' sec: 0
35 * # valid multi-byte sequence
36 * __collate_lookup("\xc3\xa4", ... ) -> len: 2 prim: > 0 sec: > 0
38 T_DECL(collate_lookup
, "Test __collate_lookup() behavior") {
40 unsigned char str
[16];
41 int len
, prim
, sec
, prim2
, sec2
;
44 /* ------------------------- C Locale ------------------------- */
45 /* In the C locale primary weights should equal the int value of the
47 result
= setlocale(LC_ALL
, "C");
48 T_ASSERT_NOTNULL(result
, "changed to C locale");
50 __collate_lookup("", &len
, &prim
, &sec
);
51 T_ASSERT_EQ_INT(len
, 0, "No characters read");
52 T_EXPECT_EQ_INT(prim
, 0, "No primary weight");
53 T_EXPECT_EQ_INT(sec
, 0, "No secondary weight");
57 for (c
= 1; c
< UCHAR_MAX
; c
++) {
60 __collate_lookup(str
, &len
, &prim
, &sec
);
61 T_ASSERT_EQ_INT(len
, 1, "Only read one character");
62 T_EXPECT_EQ_INT(prim
, (int)c
, "Primary weight returned is the value of c");
63 T_EXPECT_EQ_INT(sec
, 0, "Secondary weight returned is 0");
67 /* ------------------------- German Latin-1 Locale ----------------------- */
68 result
= setlocale(LC_ALL
, "de_DE.ISO8859-1");
69 T_ASSERT_NOTNULL(result
, "changed to german Latin-1 locale");
71 __collate_lookup("", &len
, &prim
, &sec
);
72 T_ASSERT_EQ_INT(len
, 0, "No characters read");
73 T_EXPECT_EQ_INT(prim
, 0, "No primary weight");
74 T_EXPECT_EQ_INT(sec
, 0, "No secondary weight");
78 for (c
= 1; c
< UCHAR_MAX
; c
++) {
81 __collate_lookup(str
, &len
, &prim
, &sec
);
82 T_ASSERT_EQ_INT(len
, (c
== '\0' ? 0 : 1), "Only read one character");
84 if (strstr(CHARS_WITHOUT_ENTRIES
, str
)) {
85 T_EXPECT_EQ(prim
, -1, "0x%x is not present in the table", c
);
86 T_EXPECT_EQ(sec
, -1, "0x%x is not present in the table", c
);
88 T_EXPECT_GT(prim
, 0, "0x%x Has primary weight", c
);
89 T_EXPECT_GT(sec
, 0, "0x%x Has secondary weight", c
);
94 __collate_lookup(str
, &len
, &prim
, &sec
);
95 T_ASSERT_EQ_INT(len
, 1, "Only read one character");
97 /* a with dieresis in Latin-1 locales */
98 str
[0] = (unsigned char)'\xe4';
99 __collate_lookup(str
, &len
, &prim2
, &sec2
);
100 T_ASSERT_EQ_INT(len
, 1, "Only read one character");
101 T_EXPECT_EQ(prim
, prim2
, "Same primary weight");
102 T_EXPECT_LT(sec
, sec2
, "Different secondary weight");
104 /* ------------------------- German UTF-8 Locale ------------------------- */
105 result
= setlocale(LC_ALL
, "de_DE.UTF-8");
106 T_ASSERT_NOTNULL(result
, "changed to german UTF-8 locale");
108 __collate_lookup("", &len
, &prim
, &sec
);
109 T_ASSERT_EQ_INT(len
, 0, "No characters read");
110 T_EXPECT_EQ_INT(prim
, 0, "No primary weight");
111 T_EXPECT_EQ_INT(sec
, 0, "No secondary weight");
115 for (c
= 1; c
< UCHAR_MAX
; c
++) {
116 len
= 2; /* Tell it that this string is longer */
118 __collate_lookup(str
, &len
, &prim
, &sec
);
119 T_ASSERT_EQ_INT(len
, 1, "Only read one character");
120 if (strstr(CHARS_WITHOUT_ENTRIES
, (const char *)str
)) {
121 T_EXPECT_EQ(prim
, -1, "0x%x is not present in the table", c
);
122 T_EXPECT_GT(sec
, -1, "0x%x is not present in the table", c
);
124 T_EXPECT_GT(prim
, 0, "0x%x Has primary weight", c
);
125 /* weight will be 0 for sequences that result in mb failure */
127 /* So only test secondary weights for the ASCII characters */
128 T_EXPECT_GT(sec
, 0, "0x%x Has secondary weight", c
);
134 __collate_lookup(str
, &len
, &prim
, &sec
);
135 T_ASSERT_EQ_INT(len
, 1, "Only read one character");
137 /* a with dieresis in Latin-1 locales */
138 /* this character is invalid in a UTF-8 locale */
139 str
[0] = (unsigned char)'\xe4';
141 __collate_lookup(str
, &len
, &prim2
, &sec2
);
142 T_EXPECT_EQ_INT(errno
, EILSEQ
, "errno indicates invalid character");
143 T_ASSERT_EQ_INT(len
, 1, "Only read one character");
144 T_EXPECT_EQ(prim2
, (unsigned int)L
'\xe4',
145 "Invalid character - Primary weight equal to value (228)");
146 T_EXPECT_EQ(sec2
, 0, "Invalid character - No secondary weight");
148 T_EXPECT_NE(prim
, prim2
, "Different primary weight");
149 T_EXPECT_NE(sec
, sec2
, "Different secondary weight");
151 /* Test Multibyte lookup */
152 str
[0] = (unsigned char)'\xc3';
153 str
[1] = (unsigned char)'\xa4';
154 str
[2] = (unsigned char)'X';
155 str
[3] = (unsigned char)'\0';
157 __collate_lookup(str
, &len
, &prim2
, &sec2
);
158 T_EXPECTFAIL_WITH_REASON(
159 "__collate_lookup doesn't actually tell you how many bytes were used");
160 T_ASSERT_EQ_INT(len
, 2, "Only read 2 characters");
161 T_EXPECT_EQ(prim
, prim2
, "Same primary weight");
162 T_EXPECT_LT(sec
, sec2
, "Different secondary weight");
167 * Tests for the __collate_lookup_l() which is used to lookup weights of wide
170 T_DECL(collate_lookup_l
, "Test __collate_lookup_l() behavior") {
174 int len
, prim
, sec
, prim2
, sec2
;
177 /* ------------------------- C Locale ------------------------- */
178 /* In the C locale primary weights should equal the int value of the
180 result
= setlocale(LC_ALL
, "C");
181 T_ASSERT_NOTNULL(result
, "changed to C locale");
183 __collate_lookup_l(L
"", &len
, &prim
, &sec
, LC_GLOBAL_LOCALE
);
184 T_ASSERT_EQ_INT(len
, 0, "No characters read");
185 T_EXPECT_EQ_INT(prim
, 0, "No primary weight");
186 T_EXPECT_EQ_INT(sec
, 0, "No secondary weight");
190 for (wc
= 1; wc
< UCHAR_MAX
; wc
++) {
194 __collate_lookup_l(wcs
, &len
, &prim
, &sec
, LC_GLOBAL_LOCALE
);
195 T_ASSERT_EQ_INT(errno
, 0, "No error occurred");
196 T_ASSERT_EQ_INT(len
, 1, "Only read one character");
197 T_EXPECT_EQ_INT(prim
, (int)wc
,
198 "Primary weight returned is the value of wc");
199 T_EXPECT_EQ_INT(sec
, 0, "Secondary weight returned is 0");
203 /* ------------------------- German Latin-1 Locale -------------------------
205 result
= setlocale(LC_ALL
, "de_DE.ISO8859-1");
206 T_ASSERT_NOTNULL(result
, "changed to german Latin-1 locale");
210 for (wc
= 1; wc
< UCHAR_MAX
; wc
++) {
214 __collate_lookup_l(wcs
, &len
, &prim
, &sec
, LC_GLOBAL_LOCALE
);
215 T_ASSERT_EQ_INT(len
, 1, "Only read one character");
216 if (strstr(CHARS_WITHOUT_ENTRIES
, str
)) {
217 T_EXPECT_EQ(prim
, -1, "0x%x is not present in the table", wc
);
218 T_EXPECT_EQ(sec
, -1, "0x%x is not present in the table", wc
);
220 T_EXPECT_GT(prim
, 0, "Wide char 0x%x Has primary weight", wc
);
221 T_EXPECT_GT(sec
, 0, "Wide char 0x%x Has secondary weight", wc
);
226 __collate_lookup_l(wcs
, &len
, &prim
, &sec
, LC_GLOBAL_LOCALE
);
227 T_ASSERT_EQ_INT(len
, 1, "Only read one character");
229 /* a with dieresis in Latin-1 locales */
231 __collate_lookup_l(wcs
, &len
, &prim2
, &sec2
, LC_GLOBAL_LOCALE
);
232 T_ASSERT_EQ_INT(len
, 1, "Only read one character");
233 T_EXPECT_EQ(prim
, prim2
, "Same primary weight");
234 T_EXPECT_LT(sec
, sec2
, "Different secondary weight");
236 /* ------------------------- German UTF-8 Locale ------------------------- */
237 result
= setlocale(LC_ALL
, "de_DE.UTF-8");
238 T_ASSERT_NOTNULL(result
, "changed to german UTF-8 locale");
240 __collate_lookup_l(L
"", &len
, &prim
, &sec
, LC_GLOBAL_LOCALE
);
241 T_ASSERT_EQ_INT(len
, 0, "No characters read");
242 T_EXPECT_EQ_INT(prim
, 0, "No primary weight");
243 T_EXPECT_EQ_INT(sec
, 0, "No secondary weight");
245 for (wc
= 1; wc
< UCHAR_MAX
; wc
++) {
249 __collate_lookup_l(wcs
, &len
, &prim
, &sec
, LC_GLOBAL_LOCALE
);
250 T_ASSERT_EQ_INT(len
, 1, "Only read one character");
251 if (strstr(CHARS_WITHOUT_ENTRIES
, str
)) {
252 T_EXPECT_EQ(prim
, -1, "0x%x is not present in the table", wc
);
253 T_EXPECT_EQ(sec
, -1, "0x%x is not present in the table", wc
);
255 T_EXPECT_GT(prim
, 0, "Wide char 0x%x Has primary weight", wc
);
256 T_EXPECT_GT(sec
, 0, "Wide char 0x%x Has secondary weight", wc
);
260 /* Test that a lookup of 'a' and '\xe4' returns the same primary weight */
263 __collate_lookup_l(wcs
, &len
, &prim
, &sec
, LC_GLOBAL_LOCALE
);
264 T_ASSERT_EQ_INT(len
, 1, "Only read one character");
265 T_EXPECT_GT(prim
, 0, "Wide char 0x%x Has primary weight", wc
);
266 T_EXPECT_GT(sec
, 0, "Wide char 0x%x Has secondary weight", wc
);
271 __collate_lookup_l(wcs
, &len
, &prim2
, &sec2
, LC_GLOBAL_LOCALE
);
272 T_EXPECT_EQ_INT(errno
, 0, "errno was not set");
273 T_ASSERT_EQ_INT(len
, 1, "Only read one character");
274 T_EXPECT_GT(prim2
, 0, "Wide char 0x%x Has primary weight", wc
);
275 T_EXPECT_GT(sec2
, 0, "Wide char 0x%x Has secondary weight", wc
);
277 T_EXPECT_EQ(prim
, prim2
, "Primary weight equal");
278 T_EXPECT_NE(sec
, sec2
, "Different secondary weight");