]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/usettest.c
2 **********************************************************************
3 * Copyright (c) 2002-2009, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
7 #include "unicode/uset.h"
8 #include "unicode/ustring.h"
13 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
15 #define TEST(x) addTest(root, &x, "uset/" # x)
17 static void TestAPI(void);
18 static void Testj2269(void);
19 static void TestSerialized(void);
20 static void TestNonInvariantPattern(void);
21 static void TestBadPattern(void);
22 static void TestFreezable(void);
23 static void TestSpan(void);
25 void addUSetTest(TestNode
** root
);
27 static void expect(const USet
* set
,
31 static void expectContainment(const USet
* set
,
34 static char oneUCharToChar(UChar32 c
);
35 static void expectItems(const USet
* set
,
39 addUSetTest(TestNode
** root
) {
43 TEST(TestNonInvariantPattern
);
49 /*------------------------------------------------------------------
51 *------------------------------------------------------------------*/
53 static void Testj2269() {
54 UErrorCode status
= U_ZERO_ERROR
;
55 UChar a
[4] = { 0x61, 0x62, 0x63, 0 };
56 USet
*s
= uset_open(1, 0);
57 uset_addString(s
, a
, 3);
58 a
[0] = 0x63; a
[1] = 0x63;
59 expect(s
, "{abc}", "{ccc}", &status
);
63 static const UChar PAT
[] = {91,97,45,99,123,97,98,125,93,0}; /* "[a-c{ab}]" */
64 static const int32_t PAT_LEN
= (sizeof(PAT
) / sizeof(PAT
[0])) - 1;
66 static const UChar PAT_lb
[] = {0x6C, 0x62, 0}; /* "lb" */
67 static const int32_t PAT_lb_LEN
= (sizeof(PAT_lb
) / sizeof(PAT_lb
[0])) - 1;
69 static const UChar VAL_SP
[] = {0x53, 0x50, 0}; /* "SP" */
70 static const int32_t VAL_SP_LEN
= (sizeof(VAL_SP
) / sizeof(VAL_SP
[0])) - 1;
72 static const UChar STR_bc
[] = {98,99,0}; /* "bc" */
73 static const int32_t STR_bc_LEN
= (sizeof(STR_bc
) / sizeof(STR_bc
[0])) - 1;
75 static const UChar STR_ab
[] = {97,98,0}; /* "ab" */
76 static const int32_t STR_ab_LEN
= (sizeof(STR_ab
) / sizeof(STR_ab
[0])) - 1;
79 * Basic API test for uset.x
81 static void TestAPI() {
87 set
= uset_openEmpty();
88 expect(set
, "", "abc{ab}", NULL
);
91 set
= uset_open(1, 0);
92 expect(set
, "", "abc{ab}", NULL
);
95 set
= uset_open(1, 1);
97 expect(set
, "", "abc{ab}", NULL
);
101 set
= uset_open(0x0041, 0x0043);
102 expect(set
, "ABC", "DEF{ab}", NULL
);
107 set
= uset_openPattern(PAT
, PAT_LEN
, &ec
);
109 log_err("uset_openPattern([a-c{ab}]) failed - %s\n", u_errorName(ec
));
112 if(!uset_resemblesPattern(PAT
, PAT_LEN
, 0)) {
113 log_err("uset_resemblesPattern of PAT failed\n");
115 expect(set
, "abc{ab}", "def{bc}", &ec
);
119 expect(set
, "abcd{ab}", "ef{bc}", NULL
);
122 uset_remove(set
, 0x62);
123 uset_addString(set
, STR_bc
, STR_bc_LEN
);
124 expect(set
, "acd{ab}{bc}", "bef{cd}", NULL
);
127 uset_removeString(set
, STR_ab
, STR_ab_LEN
);
128 expect(set
, "acd{bc}", "bfg{ab}", NULL
);
131 uset_complement(set
);
132 expect(set
, "bef{bc}", "acd{ac}", NULL
);
135 uset_complement(set
);
136 uset_addRange(set
, 0x0062, 0x0065);
137 expect(set
, "abcde{bc}", "fg{ab}", NULL
);
140 uset_removeRange(set
, 0x0050, 0x0063);
141 expect(set
, "de{bc}", "bcfg{ab}", NULL
);
144 uset_set(set
, 0x0067, 0x006C);
145 expect(set
, "ghijkl", "de{bc}", NULL
);
147 if (uset_indexOf(set
, 0x0067) != 0) {
148 log_err("uset_indexOf failed finding correct index of 'g'\n");
151 if (uset_charAt(set
, 0) != 0x0067) {
152 log_err("uset_charAt failed finding correct char 'g' at index 0\n");
155 /* How to test this one...? */
159 uset_retain(set
, 0x0067, 0x0069);
160 expect(set
, "ghi", "dejkl{bc}", NULL
);
162 /* UCHAR_ASCII_HEX_DIGIT */
163 uset_applyIntPropertyValue(set
, UCHAR_ASCII_HEX_DIGIT
, 1, &ec
);
165 log_err("uset_applyIntPropertyValue([UCHAR_ASCII_HEX_DIGIT]) failed - %s\n", u_errorName(ec
));
168 expect(set
, "0123456789ABCDEFabcdef", "GHIjkl{bc}", NULL
);
172 uset_addAllCodePoints(set
, STR_ab
, STR_ab_LEN
);
173 expect(set
, "ab", "def{ab}", NULL
);
174 if (uset_containsAllCodePoints(set
, STR_bc
, STR_bc_LEN
)){
175 log_err("set should not conatin all characters of \"bc\" \n");
179 set2
= uset_open(1, 1);
183 uset_applyPropertyAlias(set2
, PAT_lb
, PAT_lb_LEN
, VAL_SP
, VAL_SP_LEN
, &ec
);
184 expect(set2
, " ", "abcdefghi{bc}", NULL
);
187 uset_set(set2
, 0x0061, 0x0063);
189 uset_set(set
, 0x0067, 0x0069);
192 if (uset_containsSome(set
, set2
)) {
193 log_err("set should not contain some of set2 yet\n");
195 uset_complementAll(set
, set2
);
196 if (!uset_containsSome(set
, set2
)) {
197 log_err("set should contain some of set2\n");
199 expect(set
, "abcghi", "def{bc}", NULL
);
202 uset_removeAll(set
, set2
);
203 expect(set
, "ghi", "abcdef{bc}", NULL
);
206 uset_addAll(set2
, set
);
207 expect(set2
, "abcghi", "def{bc}", NULL
);
210 uset_retainAll(set2
, set
);
211 expect(set2
, "ghi", "abcdef{bc}", NULL
);
217 /*------------------------------------------------------------------
219 *------------------------------------------------------------------*/
222 * Verifies that the given set contains the characters and strings in
223 * inList, and does not contain those in outList. Also verifies that
224 * 'set' is not NULL and that 'ec' succeeds.
225 * @param set the set to test, or NULL (on error)
226 * @param inList list of set contents, in iteration order. Format is
227 * list of individual strings, in iteration order, followed by sorted
228 * list of strings, delimited by {}. This means we do not test
229 * characters '{' or '}' and we do not test strings containing those
231 * @param outList list of things not in the set. Same format as
233 * @param ec an error code, checked for success. May be NULL in which
234 * case it is ignored.
236 static void expect(const USet
* set
,
240 if (ec
!=NULL
&& U_FAILURE(*ec
)) {
241 log_err("FAIL: %s\n", u_errorName(*ec
));
245 log_err("FAIL: USet is NULL\n");
248 expectContainment(set
, inList
, TRUE
);
249 expectContainment(set
, outList
, FALSE
);
250 expectItems(set
, inList
);
253 static void expectContainment(const USet
* set
,
256 const char* p
= list
;
260 int32_t rangeStart
= -1, rangeEnd
= -1, length
;
263 length
= uset_toPattern(set
, ustr
, sizeof(ustr
), TRUE
, &ec
);
265 log_err("FAIL: uset_toPattern() fails in expectContainment() - %s\n", u_errorName(ec
));
268 pat
=aescstrdup(ustr
, length
);
272 const char* stringStart
= ++p
;
273 int32_t stringLength
= 0;
276 while (*p
++ != '}') {
278 stringLength
= (int32_t)(p
- stringStart
- 1);
279 strncpy(strCopy
, stringStart
, stringLength
);
280 strCopy
[stringLength
] = 0;
282 u_charsToUChars(stringStart
, ustr
, stringLength
);
284 if (uset_containsString(set
, ustr
, stringLength
) == isIn
) {
285 log_verbose("Ok: %s %s \"%s\"\n", pat
,
286 (isIn
? "contains" : "does not contain"),
289 log_data_err("FAIL: %s %s \"%s\" (Are you missing data?)\n", pat
,
290 (isIn
? "does not contain" : "contains"),
298 u_charsToUChars(p
, ustr
, 1);
301 if (uset_contains(set
, c
) == isIn
) {
302 log_verbose("Ok: %s %s '%c'\n", pat
,
303 (isIn
? "contains" : "does not contain"),
306 log_data_err("FAIL: %s %s '%c' (Are you missing data?)\n", pat
,
307 (isIn
? "does not contain" : "contains"),
311 /* Test the range API too by looking for ranges */
312 if (c
== rangeEnd
+1) {
315 if (rangeStart
>= 0) {
316 if (uset_containsRange(set
, rangeStart
, rangeEnd
) == isIn
) {
317 log_verbose("Ok: %s %s U+%04X-U+%04X\n", pat
,
318 (isIn
? "contains" : "does not contain"),
319 rangeStart
, rangeEnd
);
321 log_data_err("FAIL: %s %s U+%04X-U+%04X (Are you missing data?)\n", pat
,
322 (isIn
? "does not contain" : "contains"),
323 rangeStart
, rangeEnd
);
326 rangeStart
= rangeEnd
= c
;
333 if (rangeStart
>= 0) {
334 if (uset_containsRange(set
, rangeStart
, rangeEnd
) == isIn
) {
335 log_verbose("Ok: %s %s U+%04X-U+%04X\n", pat
,
336 (isIn
? "contains" : "does not contain"),
337 rangeStart
, rangeEnd
);
339 log_data_err("FAIL: %s %s U+%04X-U+%04X (Are you missing data?)\n", pat
,
340 (isIn
? "does not contain" : "contains"),
341 rangeStart
, rangeEnd
);
346 /* This only works for invariant BMP chars */
347 static char oneUCharToChar(UChar32 c
) {
351 u_UCharsToChars(ubuf
, buf
, 1);
355 static void expectItems(const USet
* set
,
357 const char* p
= items
;
358 UChar ustr
[4096], itemStr
[4096];
362 int32_t expectedSize
= 0;
363 int32_t itemCount
= uset_getItemCount(set
);
364 int32_t itemIndex
= 0;
365 UChar32 start
= 1, end
= 0;
366 int32_t itemLen
= 0, length
;
369 length
= uset_toPattern(set
, ustr
, sizeof(ustr
), TRUE
, &ec
);
371 log_err("FAIL: uset_toPattern => %s\n", u_errorName(ec
));
374 pat
=aescstrdup(ustr
, length
);
376 if (uset_isEmpty(set
) != (strlen(items
)==0)) {
377 log_data_err("FAIL: %s should return %s from isEmpty (Are you missing data?)\n",
379 strlen(items
)==0 ? "TRUE" : "FALSE");
382 /* Don't test patterns starting with "[^" */
383 if (u_strlen(ustr
) > 2 && ustr
[1] == 0x5e /*'^'*/) {
391 if (start
> end
|| start
== -1) {
392 /* Fetch our next item */
393 if (itemIndex
>= itemCount
) {
394 log_data_err("FAIL: ran out of items iterating %s (Are you missing data?)\n", pat
);
398 itemLen
= uset_getItem(set
, itemIndex
, &start
, &end
,
399 itemStr
, sizeof(itemStr
), &ec
);
400 if (U_FAILURE(ec
) || itemLen
< 0) {
401 log_err("FAIL: uset_getItem => %s\n", u_errorName(ec
));
406 log_verbose("Ok: %s item %d is %c-%c\n", pat
,
407 itemIndex
, oneUCharToChar(start
),
408 oneUCharToChar(end
));
410 itemStr
[itemLen
] = 0;
411 u_UCharsToChars(itemStr
, buf
, itemLen
+1);
412 log_verbose("Ok: %s item %d is \"%s\"\n", pat
, itemIndex
, buf
);
419 const char* stringStart
= ++p
;
420 int32_t stringLength
= 0;
423 while (*p
++ != '}') {
425 stringLength
= (int32_t)(p
- stringStart
- 1);
426 strncpy(strCopy
, stringStart
, stringLength
);
427 strCopy
[stringLength
] = 0;
429 u_charsToUChars(stringStart
, ustr
, stringLength
);
430 ustr
[stringLength
] = 0;
433 log_err("FAIL: for %s expect \"%s\" next, but got a char\n",
438 if (u_strcmp(ustr
, itemStr
) != 0) {
439 log_err("FAIL: for %s expect \"%s\" next\n",
448 u_charsToUChars(p
, ustr
, 1);
452 log_err("FAIL: for %s expect '%c' next, but got a string\n",
458 log_err("FAIL: for %s expect '%c' next\n",
467 if (uset_size(set
) == expectedSize
) {
468 log_verbose("Ok: %s size is %d\n", pat
, expectedSize
);
470 log_err("FAIL: %s size is %d, expected %d\n",
471 pat
, uset_size(set
), expectedSize
);
477 uint16_t buffer
[1000];
480 UErrorCode errorCode
;
484 /* use a pattern that generates both BMP and supplementary code points */
485 U_STRING_DECL(pattern
, "[:Cf:]", 6);
486 U_STRING_INIT(pattern
, "[:Cf:]", 6);
488 errorCode
=U_ZERO_ERROR
;
489 set
=uset_openPattern(pattern
, -1, &errorCode
);
490 if(U_FAILURE(errorCode
)) {
491 log_data_err("uset_openPattern([:Cf:]) failed - %s (Are you missing data?)\n", u_errorName(errorCode
));
495 length
=uset_serialize(set
, buffer
, LENGTHOF(buffer
), &errorCode
);
496 if(U_FAILURE(errorCode
)) {
497 log_err("unable to uset_serialize([:Cf:]) - %s\n", u_errorName(errorCode
));
502 uset_getSerializedSet(&sset
, buffer
, length
);
503 for(c
=0; c
<=0x10ffff; ++c
) {
504 if(uset_contains(set
, c
)!=uset_serializedContains(&sset
, c
)) {
505 log_err("uset_contains(U+%04x)!=uset_serializedContains(U+%04x)\n", c
);
514 * Make sure that when non-invariant chars are passed to uset_openPattern
515 * they do not cause an ugly failure mode (e.g. assertion failure).
519 TestNonInvariantPattern() {
520 UErrorCode ec
= U_ZERO_ERROR
;
521 /* The critical part of this test is that the following pattern
522 must contain a non-invariant character. */
523 static const char *pattern
= "[:ccc!=0:]";
525 int32_t len
= u_unescape(pattern
, buf
, 256);
526 /* This test 'fails' by having an assertion failure within the
527 following call. It passes by running to completion with no
528 assertion failure. */
529 USet
*set
= uset_openPattern(buf
, len
, &ec
);
533 static void TestBadPattern(void) {
534 UErrorCode status
= U_ZERO_ERROR
;
536 U_STRING_DECL(pattern
, "[", 1);
537 U_STRING_INIT(pattern
, "[", 1);
538 pat
= uset_openPatternOptions(pattern
, u_strlen(pattern
), 0, &status
);
539 if (pat
!= NULL
|| U_SUCCESS(status
)) {
540 log_err("uset_openPatternOptions did not fail as expected %s\n", u_errorName(status
));
544 static USet
*openIDSet() {
545 UErrorCode errorCode
= U_ZERO_ERROR
;
546 U_STRING_DECL(pattern
, "[:ID_Continue:]", 15);
547 U_STRING_INIT(pattern
, "[:ID_Continue:]", 15);
548 return uset_openPattern(pattern
, 15, &errorCode
);
551 static void TestFreezable() {
559 log_data_err("openIDSet() returned NULL. (Are you missing data?)\n");
564 frozen
=uset_clone(idSet
);
566 if (frozen
== NULL
) {
567 log_err("uset_Clone() returned NULL\n");
571 if(!uset_equals(frozen
, idSet
)) {
572 log_err("uset_clone() did not make an equal copy\n");
576 uset_addRange(frozen
, 0xd802, 0xd805);
578 if(uset_isFrozen(idSet
) || !uset_isFrozen(frozen
) || !uset_equals(frozen
, idSet
)) {
579 log_err("uset_freeze() or uset_isFrozen() does not work\n");
582 thawed
=uset_cloneAsThawed(frozen
);
584 if (thawed
== NULL
) {
585 log_err("uset_cloneAsThawed(frozen) returned NULL");
591 uset_addRange(thawed
, 0xd802, 0xd805);
593 if(uset_isFrozen(thawed
) || uset_equals(thawed
, idSet
) || !uset_containsRange(thawed
, 0xd802, 0xd805)) {
594 log_err("uset_cloneAsThawed() does not work\n");
602 static void TestSpan() {
603 static const UChar s16
[2]={ 0xe01, 0x3000 };
604 static const char* s8
="\xE0\xB8\x81\xE3\x80\x80";
606 USet
*idSet
=openIDSet();
609 log_data_err("openIDSet() returned NULL (Are you missing data?)\n");
614 1!=uset_span(idSet
, s16
, 2, USET_SPAN_CONTAINED
) ||
615 0!=uset_span(idSet
, s16
, 2, USET_SPAN_NOT_CONTAINED
) ||
616 2!=uset_spanBack(idSet
, s16
, 2, USET_SPAN_CONTAINED
) ||
617 1!=uset_spanBack(idSet
, s16
, 2, USET_SPAN_NOT_CONTAINED
)
619 log_err("uset_span() or uset_spanBack() does not work\n");
623 3!=uset_spanUTF8(idSet
, s8
, 6, USET_SPAN_CONTAINED
) ||
624 0!=uset_spanUTF8(idSet
, s8
, 6, USET_SPAN_NOT_CONTAINED
) ||
625 6!=uset_spanBackUTF8(idSet
, s8
, 6, USET_SPAN_CONTAINED
) ||
626 3!=uset_spanBackUTF8(idSet
, s8
, 6, USET_SPAN_NOT_CONTAINED
)
628 log_err("uset_spanUTF8() or uset_spanBackUTF8() does not work\n");
634 1!=uset_span(idSet
, s16
, 2, USET_SPAN_CONTAINED
) ||
635 0!=uset_span(idSet
, s16
, 2, USET_SPAN_NOT_CONTAINED
) ||
636 2!=uset_spanBack(idSet
, s16
, 2, USET_SPAN_CONTAINED
) ||
637 1!=uset_spanBack(idSet
, s16
, 2, USET_SPAN_NOT_CONTAINED
)
639 log_err("uset_span(frozen) or uset_spanBack(frozen) does not work\n");
643 3!=uset_spanUTF8(idSet
, s8
, 6, USET_SPAN_CONTAINED
) ||
644 0!=uset_spanUTF8(idSet
, s8
, 6, USET_SPAN_NOT_CONTAINED
) ||
645 6!=uset_spanBackUTF8(idSet
, s8
, 6, USET_SPAN_CONTAINED
) ||
646 3!=uset_spanBackUTF8(idSet
, s8
, 6, USET_SPAN_NOT_CONTAINED
)
648 log_err("uset_spanUTF8(frozen) or uset_spanBackUTF8(frozen) does not work\n");