]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/usettest.c
2 **********************************************************************
3 * Copyright (c) 2002-2005, 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);
23 void addUSetTest(TestNode
** root
);
25 static void expect(const USet
* set
,
29 static void expectContainment(const USet
* set
,
32 static char oneUCharToChar(UChar32 c
);
33 static void expectItems(const USet
* set
,
37 addUSetTest(TestNode
** root
) {
41 TEST(TestNonInvariantPattern
);
45 /*------------------------------------------------------------------
47 *------------------------------------------------------------------*/
49 static void Testj2269() {
50 UErrorCode status
= U_ZERO_ERROR
;
51 UChar a
[4] = { 0x61, 0x62, 0x63, 0 };
52 USet
*s
= uset_open(1, 0);
53 uset_addString(s
, a
, 3);
54 a
[0] = 0x63; a
[1] = 0x63;
55 expect(s
, "{abc}", "{ccc}", &status
);
59 static const UChar PAT
[] = {91,97,45,99,123,97,98,125,93,0}; /* "[a-c{ab}]" */
60 static const int32_t PAT_LEN
= (sizeof(PAT
) / sizeof(PAT
[0])) - 1;
62 static const UChar PAT_lb
[] = {0x6C, 0x62, 0}; /* "lb" */
63 static const int32_t PAT_lb_LEN
= (sizeof(PAT_lb
) / sizeof(PAT_lb
[0])) - 1;
65 static const UChar VAL_SP
[] = {0x53, 0x50, 0}; /* "SP" */
66 static const int32_t VAL_SP_LEN
= (sizeof(VAL_SP
) / sizeof(VAL_SP
[0])) - 1;
68 static const UChar STR_bc
[] = {98,99,0}; /* "bc" */
69 static const int32_t STR_bc_LEN
= (sizeof(STR_bc
) / sizeof(STR_bc
[0])) - 1;
71 static const UChar STR_ab
[] = {97,98,0}; /* "ab" */
72 static const int32_t STR_ab_LEN
= (sizeof(STR_ab
) / sizeof(STR_ab
[0])) - 1;
75 * Basic API test for uset.x
77 static void TestAPI() {
83 set
= uset_open(1, 1);
85 expect(set
, "", "abc{ab}", NULL
);
89 set
= uset_open(0x0041, 0x0043);
90 expect(set
, "ABC", "DEF{ab}", NULL
);
95 set
= uset_openPattern(PAT
, PAT_LEN
, &ec
);
97 log_err("uset_openPattern([a-c{ab}]) failed - %s\n", u_errorName(ec
));
100 if(!uset_resemblesPattern(PAT
, PAT_LEN
, 0)) {
101 log_err("uset_resemblesPattern of PAT failed\n");
103 expect(set
, "abc{ab}", "def{bc}", &ec
);
107 expect(set
, "abcd{ab}", "ef{bc}", NULL
);
110 uset_remove(set
, 0x62);
111 uset_addString(set
, STR_bc
, STR_bc_LEN
);
112 expect(set
, "acd{ab}{bc}", "bef{cd}", NULL
);
115 uset_removeString(set
, STR_ab
, STR_ab_LEN
);
116 expect(set
, "acd{bc}", "bfg{ab}", NULL
);
119 uset_complement(set
);
120 expect(set
, "bef{bc}", "acd{ac}", NULL
);
123 uset_complement(set
);
124 uset_addRange(set
, 0x0062, 0x0065);
125 expect(set
, "abcde{bc}", "fg{ab}", NULL
);
128 uset_removeRange(set
, 0x0050, 0x0063);
129 expect(set
, "de{bc}", "bcfg{ab}", NULL
);
132 uset_set(set
, 0x0067, 0x006C);
133 expect(set
, "ghijkl", "de{bc}", NULL
);
135 if (uset_indexOf(set
, 0x0067) != 0) {
136 log_err("uset_indexOf failed finding correct index of 'g'\n");
139 if (uset_charAt(set
, 0) != 0x0067) {
140 log_err("uset_charAt failed finding correct char 'g' at index 0\n");
143 /* How to test this one...? */
147 uset_retain(set
, 0x0067, 0x0069);
148 expect(set
, "ghi", "dejkl{bc}", NULL
);
150 /* UCHAR_ASCII_HEX_DIGIT */
151 uset_applyIntPropertyValue(set
, UCHAR_ASCII_HEX_DIGIT
, 1, &ec
);
153 log_err("uset_applyIntPropertyValue([UCHAR_ASCII_HEX_DIGIT]) failed - %s\n", u_errorName(ec
));
156 expect(set
, "0123456789ABCDEFabcdef", "GHIjkl{bc}", NULL
);
160 uset_addAllCodePoints(set
, STR_ab
, STR_ab_LEN
);
161 expect(set
, "ab", "def{ab}", NULL
);
162 if (uset_containsAllCodePoints(set
, STR_bc
, STR_bc_LEN
)){
163 log_err("set should not conatin all characters of \"bc\" \n");
167 set2
= uset_open(1, 1);
171 uset_applyPropertyAlias(set2
, PAT_lb
, PAT_lb_LEN
, VAL_SP
, VAL_SP_LEN
, &ec
);
172 expect(set2
, " ", "abcdefghi{bc}", NULL
);
175 uset_set(set2
, 0x0061, 0x0063);
177 uset_set(set
, 0x0067, 0x0069);
180 if (uset_containsSome(set
, set2
)) {
181 log_err("set should not contain some of set2 yet\n");
183 uset_complementAll(set
, set2
);
184 if (!uset_containsSome(set
, set2
)) {
185 log_err("set should contain some of set2\n");
187 expect(set
, "abcghi", "def{bc}", NULL
);
190 uset_removeAll(set
, set2
);
191 expect(set
, "ghi", "abcdef{bc}", NULL
);
194 uset_addAll(set2
, set
);
195 expect(set2
, "abcghi", "def{bc}", NULL
);
198 uset_retainAll(set2
, set
);
199 expect(set2
, "ghi", "abcdef{bc}", NULL
);
205 /*------------------------------------------------------------------
207 *------------------------------------------------------------------*/
210 * Verifies that the given set contains the characters and strings in
211 * inList, and does not contain those in outList. Also verifies that
212 * 'set' is not NULL and that 'ec' succeeds.
213 * @param set the set to test, or NULL (on error)
214 * @param inList list of set contents, in iteration order. Format is
215 * list of individual strings, in iteration order, followed by sorted
216 * list of strings, delimited by {}. This means we do not test
217 * characters '{' or '}' and we do not test strings containing those
219 * @param outList list of things not in the set. Same format as
221 * @param ec an error code, checked for success. May be NULL in which
222 * case it is ignored.
224 static void expect(const USet
* set
,
228 if (ec
!=NULL
&& U_FAILURE(*ec
)) {
229 log_err("FAIL: %s\n", u_errorName(*ec
));
233 log_err("FAIL: USet is NULL\n");
236 expectContainment(set
, inList
, TRUE
);
237 expectContainment(set
, outList
, FALSE
);
238 expectItems(set
, inList
);
241 static void expectContainment(const USet
* set
,
244 const char* p
= list
;
248 int32_t rangeStart
= -1, rangeEnd
= -1, length
;
251 length
= uset_toPattern(set
, ustr
, sizeof(ustr
), TRUE
, &ec
);
253 log_err("FAIL: uset_toPattern() fails in expectContainment() - %s\n", u_errorName(ec
));
256 pat
=aescstrdup(ustr
, length
);
260 const char* stringStart
= ++p
;
261 int32_t stringLength
= 0;
264 while (*p
++ != '}') {
266 stringLength
= (int32_t)(p
- stringStart
- 1);
267 strncpy(strCopy
, stringStart
, stringLength
);
268 strCopy
[stringLength
] = 0;
270 u_charsToUChars(stringStart
, ustr
, stringLength
);
272 if (uset_containsString(set
, ustr
, stringLength
) == isIn
) {
273 log_verbose("Ok: %s %s \"%s\"\n", pat
,
274 (isIn
? "contains" : "does not contain"),
277 log_err("FAIL: %s %s \"%s\"\n", pat
,
278 (isIn
? "does not contain" : "contains"),
286 u_charsToUChars(p
, ustr
, 1);
289 if (uset_contains(set
, c
) == isIn
) {
290 log_verbose("Ok: %s %s '%c'\n", pat
,
291 (isIn
? "contains" : "does not contain"),
294 log_err("FAIL: %s %s '%c'\n", pat
,
295 (isIn
? "does not contain" : "contains"),
299 /* Test the range API too by looking for ranges */
300 if (c
== rangeEnd
+1) {
303 if (rangeStart
>= 0) {
304 if (uset_containsRange(set
, rangeStart
, rangeEnd
) == isIn
) {
305 log_verbose("Ok: %s %s U+%04X-U+%04X\n", pat
,
306 (isIn
? "contains" : "does not contain"),
307 rangeStart
, rangeEnd
);
309 log_err("FAIL: %s %s U+%04X-U+%04X\n", pat
,
310 (isIn
? "does not contain" : "contains"),
311 rangeStart
, rangeEnd
);
314 rangeStart
= rangeEnd
= c
;
321 if (rangeStart
>= 0) {
322 if (uset_containsRange(set
, rangeStart
, rangeEnd
) == isIn
) {
323 log_verbose("Ok: %s %s U+%04X-U+%04X\n", pat
,
324 (isIn
? "contains" : "does not contain"),
325 rangeStart
, rangeEnd
);
327 log_err("FAIL: %s %s U+%04X-U+%04X\n", pat
,
328 (isIn
? "does not contain" : "contains"),
329 rangeStart
, rangeEnd
);
334 /* This only works for invariant BMP chars */
335 static char oneUCharToChar(UChar32 c
) {
339 u_UCharsToChars(ubuf
, buf
, 1);
343 static void expectItems(const USet
* set
,
345 const char* p
= items
;
346 UChar ustr
[4096], itemStr
[4096];
350 int32_t expectedSize
= 0;
351 int32_t itemCount
= uset_getItemCount(set
);
352 int32_t itemIndex
= 0;
353 UChar32 start
= 1, end
= 0;
354 int32_t itemLen
= 0, length
;
357 length
= uset_toPattern(set
, ustr
, sizeof(ustr
), TRUE
, &ec
);
359 log_err("FAIL: uset_toPattern => %s\n", u_errorName(ec
));
362 pat
=aescstrdup(ustr
, length
);
364 if (uset_isEmpty(set
) != (strlen(items
)==0)) {
365 log_err("FAIL: %s should return %s from isEmpty\n",
367 strlen(items
)==0 ? "TRUE" : "FALSE");
370 /* Don't test patterns starting with "[^" */
371 if (u_strlen(ustr
) > 2 && ustr
[1] == 0x5e /*'^'*/) {
379 if (start
> end
|| start
== -1) {
380 /* Fetch our next item */
381 if (itemIndex
>= itemCount
) {
382 log_err("FAIL: ran out of items iterating %s\n", pat
);
386 itemLen
= uset_getItem(set
, itemIndex
, &start
, &end
,
387 itemStr
, sizeof(itemStr
), &ec
);
388 if (U_FAILURE(ec
) || itemLen
< 0) {
389 log_err("FAIL: uset_getItem => %s\n", u_errorName(ec
));
394 log_verbose("Ok: %s item %d is %c-%c\n", pat
,
395 itemIndex
, oneUCharToChar(start
),
396 oneUCharToChar(end
));
398 itemStr
[itemLen
] = 0;
399 u_UCharsToChars(itemStr
, buf
, itemLen
+1);
400 log_verbose("Ok: %s item %d is \"%s\"\n", pat
, itemIndex
, buf
);
407 const char* stringStart
= ++p
;
408 int32_t stringLength
= 0;
411 while (*p
++ != '}') {
413 stringLength
= (int32_t)(p
- stringStart
- 1);
414 strncpy(strCopy
, stringStart
, stringLength
);
415 strCopy
[stringLength
] = 0;
417 u_charsToUChars(stringStart
, ustr
, stringLength
);
418 ustr
[stringLength
] = 0;
421 log_err("FAIL: for %s expect \"%s\" next, but got a char\n",
426 if (u_strcmp(ustr
, itemStr
) != 0) {
427 log_err("FAIL: for %s expect \"%s\" next\n",
436 u_charsToUChars(p
, ustr
, 1);
440 log_err("FAIL: for %s expect '%c' next, but got a string\n",
446 log_err("FAIL: for %s expect '%c' next\n",
455 if (uset_size(set
) == expectedSize
) {
456 log_verbose("Ok: %s size is %d\n", pat
, expectedSize
);
458 log_err("FAIL: %s size is %d, expected %d\n",
459 pat
, uset_size(set
), expectedSize
);
465 uint16_t buffer
[1000];
468 UErrorCode errorCode
;
472 /* use a pattern that generates both BMP and supplementary code points */
473 U_STRING_DECL(pattern
, "[:Cf:]", 6);
474 U_STRING_INIT(pattern
, "[:Cf:]", 6);
476 errorCode
=U_ZERO_ERROR
;
477 set
=uset_openPattern(pattern
, -1, &errorCode
);
478 if(U_FAILURE(errorCode
)) {
479 log_err("uset_openPattern([:Cf:]) failed - %s\n", u_errorName(errorCode
));
483 length
=uset_serialize(set
, buffer
, LENGTHOF(buffer
), &errorCode
);
484 if(U_FAILURE(errorCode
)) {
485 log_err("unable to uset_serialize([:Cf:]) - %s\n", u_errorName(errorCode
));
490 uset_getSerializedSet(&sset
, buffer
, length
);
491 for(c
=0; c
<=0x10ffff; ++c
) {
492 if(uset_contains(set
, c
)!=uset_serializedContains(&sset
, c
)) {
493 log_err("uset_contains(U+%04x)!=uset_serializedContains(U+%04x)\n", c
);
502 * Make sure that when non-invariant chars are passed to uset_openPattern
503 * they do not cause an ugly failure mode (e.g. assertion failure).
507 TestNonInvariantPattern() {
508 UErrorCode ec
= U_ZERO_ERROR
;
509 /* The critical part of this test is that the following pattern
510 must contain a non-invariant character. */
511 static const char *pattern
= "[:ccc!=0:]";
513 int32_t len
= u_unescape(pattern
, buf
, 256);
514 /* This test 'fails' by having an assertion failure within the
515 following call. It passes by running to completion with no
516 assertion failure. */
517 USet
*set
= uset_openPattern(buf
, len
, &ec
);
521 static void TestBadPattern(void) {
522 UErrorCode status
= U_ZERO_ERROR
;
524 U_STRING_DECL(pattern
, "[", 1);
525 U_STRING_INIT(pattern
, "[", 1);
526 pat
= uset_openPatternOptions(pattern
, u_strlen(pattern
), 0, &status
);
527 if (pat
!= NULL
|| U_SUCCESS(status
)) {
528 log_err("uset_openPatternOptions did not fail as expected %s\n", u_errorName(status
));