]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/usettest.c
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (c) 2002-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
9 #include "unicode/uset.h"
10 #include "unicode/ustring.h"
16 #define TEST(x) addTest(root, &x, "uset/" # x)
18 static void TestAPI(void);
19 static void Testj2269(void);
20 static void TestSerialized(void);
21 static void TestNonInvariantPattern(void);
22 static void TestBadPattern(void);
23 static void TestFreezable(void);
24 static void TestSpan(void);
26 void addUSetTest(TestNode
** root
);
28 static void expect(const USet
* set
,
32 static void expectContainment(const USet
* set
,
35 static char oneUCharToChar(UChar32 c
);
36 static void expectItems(const USet
* set
,
40 addUSetTest(TestNode
** root
) {
44 TEST(TestNonInvariantPattern
);
50 /*------------------------------------------------------------------
52 *------------------------------------------------------------------*/
54 static void Testj2269() {
55 UErrorCode status
= U_ZERO_ERROR
;
56 UChar a
[4] = { 0x61, 0x62, 0x63, 0 };
57 USet
*s
= uset_open(1, 0);
58 uset_addString(s
, a
, 3);
59 a
[0] = 0x63; a
[1] = 0x63;
60 expect(s
, "{abc}", "{ccc}", &status
);
64 static const UChar PAT
[] = {91,97,45,99,123,97,98,125,93,0}; /* "[a-c{ab}]" */
65 static const int32_t PAT_LEN
= UPRV_LENGTHOF(PAT
) - 1;
67 static const UChar PAT_lb
[] = {0x6C, 0x62, 0}; /* "lb" */
68 static const int32_t PAT_lb_LEN
= UPRV_LENGTHOF(PAT_lb
) - 1;
70 static const UChar VAL_SP
[] = {0x53, 0x50, 0}; /* "SP" */
71 static const int32_t VAL_SP_LEN
= UPRV_LENGTHOF(VAL_SP
) - 1;
73 static const UChar STR_bc
[] = {98,99,0}; /* "bc" */
74 static const int32_t STR_bc_LEN
= UPRV_LENGTHOF(STR_bc
) - 1;
76 static const UChar STR_ab
[] = {97,98,0}; /* "ab" */
77 static const int32_t STR_ab_LEN
= UPRV_LENGTHOF(STR_ab
) - 1;
80 * Basic API test for uset.x
82 static void TestAPI() {
88 set
= uset_openEmpty();
89 expect(set
, "", "abc{ab}", NULL
);
92 set
= uset_open(1, 0);
93 expect(set
, "", "abc{ab}", NULL
);
96 set
= uset_open(1, 1);
98 expect(set
, "", "abc{ab}", NULL
);
102 set
= uset_open(0x0041, 0x0043);
103 expect(set
, "ABC", "DEF{ab}", NULL
);
108 set
= uset_openPattern(PAT
, PAT_LEN
, &ec
);
110 log_err("uset_openPattern([a-c{ab}]) failed - %s\n", u_errorName(ec
));
113 if(!uset_resemblesPattern(PAT
, PAT_LEN
, 0)) {
114 log_err("uset_resemblesPattern of PAT failed\n");
116 expect(set
, "abc{ab}", "def{bc}", &ec
);
120 expect(set
, "abcd{ab}", "ef{bc}", NULL
);
123 uset_remove(set
, 0x62);
124 uset_addString(set
, STR_bc
, STR_bc_LEN
);
125 expect(set
, "acd{ab}{bc}", "bef{cd}", NULL
);
128 uset_removeString(set
, STR_ab
, STR_ab_LEN
);
129 expect(set
, "acd{bc}", "bfg{ab}", NULL
);
132 uset_complement(set
);
133 expect(set
, "bef{bc}", "acd{ac}", NULL
);
136 uset_complement(set
);
137 uset_addRange(set
, 0x0062, 0x0065);
138 expect(set
, "abcde{bc}", "fg{ab}", NULL
);
141 uset_removeRange(set
, 0x0050, 0x0063);
142 expect(set
, "de{bc}", "bcfg{ab}", NULL
);
145 uset_set(set
, 0x0067, 0x006C);
146 expect(set
, "ghijkl", "de{bc}", NULL
);
148 if (uset_indexOf(set
, 0x0067) != 0) {
149 log_err("uset_indexOf failed finding correct index of 'g'\n");
152 if (uset_charAt(set
, 0) != 0x0067) {
153 log_err("uset_charAt failed finding correct char 'g' at index 0\n");
156 /* How to test this one...? */
160 uset_retain(set
, 0x0067, 0x0069);
161 expect(set
, "ghi", "dejkl{bc}", NULL
);
163 /* UCHAR_ASCII_HEX_DIGIT */
164 uset_applyIntPropertyValue(set
, UCHAR_ASCII_HEX_DIGIT
, 1, &ec
);
166 log_err("uset_applyIntPropertyValue([UCHAR_ASCII_HEX_DIGIT]) failed - %s\n", u_errorName(ec
));
169 expect(set
, "0123456789ABCDEFabcdef", "GHIjkl{bc}", NULL
);
173 uset_addAllCodePoints(set
, STR_ab
, STR_ab_LEN
);
174 expect(set
, "ab", "def{ab}", NULL
);
175 if (uset_containsAllCodePoints(set
, STR_bc
, STR_bc_LEN
)){
176 log_err("set should not conatin all characters of \"bc\" \n");
180 set2
= uset_open(1, 1);
184 uset_applyPropertyAlias(set2
, PAT_lb
, PAT_lb_LEN
, VAL_SP
, VAL_SP_LEN
, &ec
);
185 expect(set2
, " ", "abcdefghi{bc}", NULL
);
188 uset_set(set2
, 0x0061, 0x0063);
190 uset_set(set
, 0x0067, 0x0069);
193 if (uset_containsSome(set
, set2
)) {
194 log_err("set should not contain some of set2 yet\n");
196 uset_complementAll(set
, set2
);
197 if (!uset_containsSome(set
, set2
)) {
198 log_err("set should contain some of set2\n");
200 expect(set
, "abcghi", "def{bc}", NULL
);
203 uset_removeAll(set
, set2
);
204 expect(set
, "ghi", "abcdef{bc}", NULL
);
207 uset_addAll(set2
, set
);
208 expect(set2
, "abcghi", "def{bc}", NULL
);
211 uset_retainAll(set2
, set
);
212 expect(set2
, "ghi", "abcdef{bc}", NULL
);
218 /*------------------------------------------------------------------
220 *------------------------------------------------------------------*/
223 * Verifies that the given set contains the characters and strings in
224 * inList, and does not contain those in outList. Also verifies that
225 * 'set' is not NULL and that 'ec' succeeds.
226 * @param set the set to test, or NULL (on error)
227 * @param inList list of set contents, in iteration order. Format is
228 * list of individual strings, in iteration order, followed by sorted
229 * list of strings, delimited by {}. This means we do not test
230 * characters '{' or '}' and we do not test strings containing those
232 * @param outList list of things not in the set. Same format as
234 * @param ec an error code, checked for success. May be NULL in which
235 * case it is ignored.
237 static void expect(const USet
* set
,
241 if (ec
!=NULL
&& U_FAILURE(*ec
)) {
242 log_err("FAIL: %s\n", u_errorName(*ec
));
246 log_err("FAIL: USet is NULL\n");
249 expectContainment(set
, inList
, TRUE
);
250 expectContainment(set
, outList
, FALSE
);
251 expectItems(set
, inList
);
254 static void expectContainment(const USet
* set
,
257 const char* p
= list
;
261 int32_t rangeStart
= -1, rangeEnd
= -1, length
;
264 length
= uset_toPattern(set
, ustr
, sizeof(ustr
), TRUE
, &ec
);
266 log_err("FAIL: uset_toPattern() fails in expectContainment() - %s\n", u_errorName(ec
));
269 pat
=aescstrdup(ustr
, length
);
273 const char* stringStart
= ++p
;
274 int32_t stringLength
= 0;
277 while (*p
++ != '}') {
279 stringLength
= (int32_t)(p
- stringStart
- 1);
280 strncpy(strCopy
, stringStart
, stringLength
);
281 strCopy
[stringLength
] = 0;
283 u_charsToUChars(stringStart
, ustr
, stringLength
);
285 if (uset_containsString(set
, ustr
, stringLength
) == isIn
) {
286 log_verbose("Ok: %s %s \"%s\"\n", pat
,
287 (isIn
? "contains" : "does not contain"),
290 log_data_err("FAIL: %s %s \"%s\" (Are you missing data?)\n", pat
,
291 (isIn
? "does not contain" : "contains"),
299 u_charsToUChars(p
, ustr
, 1);
302 if (uset_contains(set
, c
) == isIn
) {
303 log_verbose("Ok: %s %s '%c'\n", pat
,
304 (isIn
? "contains" : "does not contain"),
307 log_data_err("FAIL: %s %s '%c' (Are you missing data?)\n", pat
,
308 (isIn
? "does not contain" : "contains"),
312 /* Test the range API too by looking for ranges */
313 if (c
== rangeEnd
+1) {
316 if (rangeStart
>= 0) {
317 if (uset_containsRange(set
, rangeStart
, rangeEnd
) == isIn
) {
318 log_verbose("Ok: %s %s U+%04X-U+%04X\n", pat
,
319 (isIn
? "contains" : "does not contain"),
320 rangeStart
, rangeEnd
);
322 log_data_err("FAIL: %s %s U+%04X-U+%04X (Are you missing data?)\n", pat
,
323 (isIn
? "does not contain" : "contains"),
324 rangeStart
, rangeEnd
);
327 rangeStart
= rangeEnd
= c
;
334 if (rangeStart
>= 0) {
335 if (uset_containsRange(set
, rangeStart
, rangeEnd
) == isIn
) {
336 log_verbose("Ok: %s %s U+%04X-U+%04X\n", pat
,
337 (isIn
? "contains" : "does not contain"),
338 rangeStart
, rangeEnd
);
340 log_data_err("FAIL: %s %s U+%04X-U+%04X (Are you missing data?)\n", pat
,
341 (isIn
? "does not contain" : "contains"),
342 rangeStart
, rangeEnd
);
347 /* This only works for invariant BMP chars */
348 static char oneUCharToChar(UChar32 c
) {
352 u_UCharsToChars(ubuf
, buf
, 1);
356 static void expectItems(const USet
* set
,
358 const char* p
= items
;
359 UChar ustr
[4096], itemStr
[4096];
363 int32_t expectedSize
= 0;
364 int32_t itemCount
= uset_getItemCount(set
);
365 int32_t itemIndex
= 0;
366 UChar32 start
= 1, end
= 0;
367 int32_t itemLen
= 0, length
;
370 length
= uset_toPattern(set
, ustr
, sizeof(ustr
), TRUE
, &ec
);
372 log_err("FAIL: uset_toPattern => %s\n", u_errorName(ec
));
375 pat
=aescstrdup(ustr
, length
);
377 if (uset_isEmpty(set
) != (strlen(items
)==0)) {
378 log_data_err("FAIL: %s should return %s from isEmpty (Are you missing data?)\n",
380 strlen(items
)==0 ? "TRUE" : "FALSE");
383 /* Don't test patterns starting with "[^" */
384 if (u_strlen(ustr
) > 2 && ustr
[1] == 0x5e /*'^'*/) {
392 if (start
> end
|| start
== -1) {
393 /* Fetch our next item */
394 if (itemIndex
>= itemCount
) {
395 log_data_err("FAIL: ran out of items iterating %s (Are you missing data?)\n", pat
);
399 itemLen
= uset_getItem(set
, itemIndex
, &start
, &end
,
400 itemStr
, sizeof(itemStr
), &ec
);
401 if (U_FAILURE(ec
) || itemLen
< 0) {
402 log_err("FAIL: uset_getItem => %s\n", u_errorName(ec
));
407 log_verbose("Ok: %s item %d is %c-%c\n", pat
,
408 itemIndex
, oneUCharToChar(start
),
409 oneUCharToChar(end
));
411 itemStr
[itemLen
] = 0;
412 u_UCharsToChars(itemStr
, buf
, itemLen
+1);
413 log_verbose("Ok: %s item %d is \"%s\"\n", pat
, itemIndex
, buf
);
420 const char* stringStart
= ++p
;
421 int32_t stringLength
= 0;
424 while (*p
++ != '}') {
426 stringLength
= (int32_t)(p
- stringStart
- 1);
427 strncpy(strCopy
, stringStart
, stringLength
);
428 strCopy
[stringLength
] = 0;
430 u_charsToUChars(stringStart
, ustr
, stringLength
);
431 ustr
[stringLength
] = 0;
434 log_err("FAIL: for %s expect \"%s\" next, but got a char\n",
439 if (u_strcmp(ustr
, itemStr
) != 0) {
440 log_err("FAIL: for %s expect \"%s\" next\n",
449 u_charsToUChars(p
, ustr
, 1);
453 log_err("FAIL: for %s expect '%c' next, but got a string\n",
459 log_err("FAIL: for %s expect '%c' next\n",
468 if (uset_size(set
) == expectedSize
) {
469 log_verbose("Ok: %s size is %d\n", pat
, expectedSize
);
471 log_err("FAIL: %s size is %d, expected %d\n",
472 pat
, uset_size(set
), expectedSize
);
478 uint16_t buffer
[1000];
481 UErrorCode errorCode
;
485 /* use a pattern that generates both BMP and supplementary code points */
486 U_STRING_DECL(pattern
, "[:Cf:]", 6);
487 U_STRING_INIT(pattern
, "[:Cf:]", 6);
489 errorCode
=U_ZERO_ERROR
;
490 set
=uset_openPattern(pattern
, -1, &errorCode
);
491 if(U_FAILURE(errorCode
)) {
492 log_data_err("uset_openPattern([:Cf:]) failed - %s (Are you missing data?)\n", u_errorName(errorCode
));
496 length
=uset_serialize(set
, buffer
, UPRV_LENGTHOF(buffer
), &errorCode
);
497 if(U_FAILURE(errorCode
)) {
498 log_err("unable to uset_serialize([:Cf:]) - %s\n", u_errorName(errorCode
));
503 uset_getSerializedSet(&sset
, buffer
, length
);
504 for(c
=0; c
<=0x10ffff; ++c
) {
505 if(uset_contains(set
, c
)!=uset_serializedContains(&sset
, c
)) {
506 log_err("uset_contains(U+%04x)!=uset_serializedContains(U+%04x)\n", c
);
515 * Make sure that when non-invariant chars are passed to uset_openPattern
516 * they do not cause an ugly failure mode (e.g. assertion failure).
520 TestNonInvariantPattern() {
521 UErrorCode ec
= U_ZERO_ERROR
;
522 /* The critical part of this test is that the following pattern
523 must contain a non-invariant character. */
524 static const char *pattern
= "[:ccc!=0:]";
526 int32_t len
= u_unescape(pattern
, buf
, 256);
527 /* This test 'fails' by having an assertion failure within the
528 following call. It passes by running to completion with no
529 assertion failure. */
530 USet
*set
= uset_openPattern(buf
, len
, &ec
);
534 static void TestBadPattern(void) {
535 UErrorCode status
= U_ZERO_ERROR
;
537 U_STRING_DECL(pattern
, "[", 1);
538 U_STRING_INIT(pattern
, "[", 1);
539 pat
= uset_openPatternOptions(pattern
, u_strlen(pattern
), 0, &status
);
540 if (pat
!= NULL
|| U_SUCCESS(status
)) {
541 log_err("uset_openPatternOptions did not fail as expected %s\n", u_errorName(status
));
545 static USet
*openIDSet() {
546 UErrorCode errorCode
= U_ZERO_ERROR
;
547 U_STRING_DECL(pattern
, "[:ID_Continue:]", 15);
548 U_STRING_INIT(pattern
, "[:ID_Continue:]", 15);
549 return uset_openPattern(pattern
, 15, &errorCode
);
552 static void TestFreezable() {
560 log_data_err("openIDSet() returned NULL. (Are you missing data?)\n");
565 frozen
=uset_clone(idSet
);
567 if (frozen
== NULL
) {
568 log_err("uset_Clone() returned NULL\n");
572 if(!uset_equals(frozen
, idSet
)) {
573 log_err("uset_clone() did not make an equal copy\n");
577 uset_addRange(frozen
, 0xd802, 0xd805);
579 if(uset_isFrozen(idSet
) || !uset_isFrozen(frozen
) || !uset_equals(frozen
, idSet
)) {
580 log_err("uset_freeze() or uset_isFrozen() does not work\n");
583 thawed
=uset_cloneAsThawed(frozen
);
585 if (thawed
== NULL
) {
586 log_err("uset_cloneAsThawed(frozen) returned NULL");
592 uset_addRange(thawed
, 0xd802, 0xd805);
594 if(uset_isFrozen(thawed
) || uset_equals(thawed
, idSet
) || !uset_containsRange(thawed
, 0xd802, 0xd805)) {
595 log_err("uset_cloneAsThawed() does not work\n");
603 static void TestSpan() {
604 static const UChar s16
[2]={ 0xe01, 0x3000 };
605 static const char* s8
="\xE0\xB8\x81\xE3\x80\x80";
607 USet
*idSet
=openIDSet();
610 log_data_err("openIDSet() returned NULL (Are you missing data?)\n");
615 1!=uset_span(idSet
, s16
, 2, USET_SPAN_CONTAINED
) ||
616 0!=uset_span(idSet
, s16
, 2, USET_SPAN_NOT_CONTAINED
) ||
617 2!=uset_spanBack(idSet
, s16
, 2, USET_SPAN_CONTAINED
) ||
618 1!=uset_spanBack(idSet
, s16
, 2, USET_SPAN_NOT_CONTAINED
)
620 log_err("uset_span() or uset_spanBack() does not work\n");
624 3!=uset_spanUTF8(idSet
, s8
, 6, USET_SPAN_CONTAINED
) ||
625 0!=uset_spanUTF8(idSet
, s8
, 6, USET_SPAN_NOT_CONTAINED
) ||
626 6!=uset_spanBackUTF8(idSet
, s8
, 6, USET_SPAN_CONTAINED
) ||
627 3!=uset_spanBackUTF8(idSet
, s8
, 6, USET_SPAN_NOT_CONTAINED
)
629 log_err("uset_spanUTF8() or uset_spanBackUTF8() does not work\n");
635 1!=uset_span(idSet
, s16
, 2, USET_SPAN_CONTAINED
) ||
636 0!=uset_span(idSet
, s16
, 2, USET_SPAN_NOT_CONTAINED
) ||
637 2!=uset_spanBack(idSet
, s16
, 2, USET_SPAN_CONTAINED
) ||
638 1!=uset_spanBack(idSet
, s16
, 2, USET_SPAN_NOT_CONTAINED
)
640 log_err("uset_span(frozen) or uset_spanBack(frozen) does not work\n");
644 3!=uset_spanUTF8(idSet
, s8
, 6, USET_SPAN_CONTAINED
) ||
645 0!=uset_spanUTF8(idSet
, s8
, 6, USET_SPAN_NOT_CONTAINED
) ||
646 6!=uset_spanBackUTF8(idSet
, s8
, 6, USET_SPAN_CONTAINED
) ||
647 3!=uset_spanBackUTF8(idSet
, s8
, 6, USET_SPAN_NOT_CONTAINED
)
649 log_err("uset_spanUTF8(frozen) or uset_spanBackUTF8(frozen) does not work\n");