2 ********************************************************************************
3 * Copyright (C) 1999-2014 International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************************
6 * Date Name Description
7 * 10/20/99 alan Creation.
8 * 03/22/2000 Madhu Added additional tests
9 ********************************************************************************
15 #include "unicode/utypes.h"
17 #include "unicode/ucnv.h"
18 #include "unicode/uniset.h"
19 #include "unicode/uchar.h"
20 #include "unicode/usetiter.h"
21 #include "unicode/ustring.h"
22 #include "unicode/parsepos.h"
23 #include "unicode/symtable.h"
24 #include "unicode/uversion.h"
27 #define TEST_ASSERT_SUCCESS(status) {if (U_FAILURE(status)) { \
28 dataerrln("fail in file \"%s\", line %d: \"%s\"", __FILE__, __LINE__, \
29 u_errorName(status));}}
31 #define TEST_ASSERT(expr) {if (!(expr)) { \
32 dataerrln("fail in file \"%s\", line %d", __FILE__, __LINE__); }}
34 UnicodeString
operator+(const UnicodeString
& left
, const UnicodeSet
& set
) {
37 return left
+ UnicodeSetTest::escape(pat
);
40 #define CASE(id,test) case id: \
49 UnicodeSetTest::UnicodeSetTest() : utf8Cnv(NULL
) {
52 UConverter
*UnicodeSetTest::openUTF8Converter() {
54 UErrorCode errorCode
=U_ZERO_ERROR
;
55 utf8Cnv
=ucnv_open("UTF-8", &errorCode
);
60 UnicodeSetTest::~UnicodeSetTest() {
65 UnicodeSetTest::runIndexedTest(int32_t index
, UBool exec
,
66 const char* &name
, char* /*par*/) {
67 // if (exec) logln((UnicodeString)"TestSuite UnicodeSetTest");
70 CASE(1,TestAddRemove
);
71 CASE(2,TestCategories
);
72 CASE(3,TestCloneEqualHash
);
73 CASE(4,TestMinimalRep
);
75 CASE(6,TestScriptSet
);
76 CASE(7,TestPropertySet
);
78 CASE(9,TestExhaustive
);
79 CASE(10,TestToPattern
);
83 CASE(14,TestCloseOver
);
84 CASE(15,TestEscapePattern
);
85 CASE(16,TestInvalidCodePoint
);
86 CASE(17,TestSymbolTable
);
87 CASE(18,TestSurrogate
);
88 CASE(19,TestPosixClasses
);
89 CASE(20,TestIteration
);
90 CASE(21,TestFreezable
);
92 CASE(23,TestStringSpan
);
93 default: name
= ""; break;
97 static const char NOT
[] = "%%%%";
100 * UVector was improperly copying contents
101 * This code will crash this is still true
103 void UnicodeSetTest::Testj2268() {
105 t
.add(UnicodeString("abc"));
107 UnicodeString ustrPat
;
108 test
.toPattern(ustrPat
, TRUE
);
114 void UnicodeSetTest::TestToPattern() {
115 UErrorCode ec
= U_ZERO_ERROR
;
117 // Test that toPattern() round trips with syntax characters and
120 static const char* OTHER_TOPATTERN_TESTS
[] = {
121 "[[:latin:]&[:greek:]]",
122 "[[:latin:]-[:greek:]]",
123 "[:nonspacing mark:]",
127 for (int32_t j
=0; OTHER_TOPATTERN_TESTS
[j
]!=NULL
; ++j
) {
129 UnicodeSet
s(OTHER_TOPATTERN_TESTS
[j
], ec
);
131 dataerrln((UnicodeString
)"FAIL: bad pattern " + OTHER_TOPATTERN_TESTS
[j
] + " - " + UnicodeString(u_errorName(ec
)));
134 checkPat(OTHER_TOPATTERN_TESTS
[j
], s
);
137 for (UChar32 i
= 0; i
<= 0x10FFFF; ++i
) {
138 if ((i
<= 0xFF && !u_isalpha(i
)) || u_isspace(i
)) {
140 // check various combinations to make sure they all work.
141 if (i
!= 0 && !toPatternAux(i
, i
)){
144 if (!toPatternAux(0, i
)){
147 if (!toPatternAux(i
, 0xFFFF)){
154 // Test pattern behavior of multicharacter strings.
157 UnicodeSet
* s
= new UnicodeSet("[a-z {aa} {ab}]", ec
);
159 // This loop isn't a loop. It's here to make the compiler happy.
160 // If you're curious, try removing it and changing the 'break'
161 // statements (except for the last) to goto's.
163 if (U_FAILURE(ec
)) break;
164 const char* exp1
[] = {"aa", "ab", NOT
, "ac", NULL
};
165 expectToPattern(*s
, "[a-z{aa}{ab}]", exp1
);
168 const char* exp2
[] = {"aa", "ab", "ac", NOT
, "xy", NULL
};
169 expectToPattern(*s
, "[a-z{aa}{ab}{ac}]", exp2
);
171 s
->applyPattern(UNICODE_STRING_SIMPLE("[a-z {\\{l} {r\\}}]"), ec
);
172 if (U_FAILURE(ec
)) break;
173 const char* exp3
[] = {"{l", "r}", NOT
, "xy", NULL
};
174 expectToPattern(*s
, UNICODE_STRING_SIMPLE("[a-z{r\\}}{\\{l}]"), exp3
);
177 const char* exp4
[] = {"{l", "r}", "[]", NOT
, "xy", NULL
};
178 expectToPattern(*s
, UNICODE_STRING_SIMPLE("[a-z{\\[\\]}{r\\}}{\\{l}]"), exp4
);
180 s
->applyPattern(UNICODE_STRING_SIMPLE("[a-z {\\u4E01\\u4E02}{\\n\\r}]"), ec
);
181 if (U_FAILURE(ec
)) break;
182 const char* exp5
[] = {"\\u4E01\\u4E02", "\n\r", NULL
};
183 expectToPattern(*s
, UNICODE_STRING_SIMPLE("[a-z{\\u000A\\u000D}{\\u4E01\\u4E02}]"), exp5
);
187 s
->add(UnicodeString("abc", ""));
188 s
->add(UnicodeString("abc", ""));
189 const char* exp6
[] = {"abc", NOT
, "ab", NULL
};
190 expectToPattern(*s
, "[{abc}]", exp6
);
195 if (U_FAILURE(ec
)) errln("FAIL: pattern parse error");
199 // JB#3400: For 2 character ranges prefer [ab] to [a-b]
201 s
.add((UChar
)97, (UChar
)98); // 'a', 'b'
202 expectToPattern(s
, "[ab]", NULL
);
205 UBool
UnicodeSetTest::toPatternAux(UChar32 start
, UChar32 end
) {
207 // use Integer.toString because Utility.hex doesn't handle ints
208 UnicodeString pat
= "";
209 // TODO do these in hex
210 //String source = "0x" + Integer.toString(start,16).toUpperCase();
211 //if (start != end) source += "..0x" + Integer.toString(end,16).toUpperCase();
212 UnicodeString source
;
213 source
= source
+ (uint32_t)start
;
215 source
= source
+ ".." + (uint32_t)end
;
217 testSet
.add(start
, end
);
218 return checkPat(source
, testSet
);
221 UBool
UnicodeSetTest::checkPat(const UnicodeString
& source
,
222 const UnicodeSet
& testSet
) {
223 // What we want to make sure of is that a pattern generated
224 // by toPattern(), with or without escaped unprintables, can
225 // be passed back into the UnicodeSet constructor.
228 testSet
.toPattern(pat0
, TRUE
);
230 if (!checkPat(source
+ " (escaped)", testSet
, pat0
)) return FALSE
;
232 //String pat1 = unescapeLeniently(pat0);
233 //if (!checkPat(source + " (in code)", testSet, pat1)) return false;
236 testSet
.toPattern(pat2
, FALSE
);
237 if (!checkPat(source
, testSet
, pat2
)) return FALSE
;
239 //String pat3 = unescapeLeniently(pat2);
240 // if (!checkPat(source + " (in code)", testSet, pat3)) return false;
242 //logln(source + " => " + pat0 + ", " + pat1 + ", " + pat2 + ", " + pat3);
243 logln((UnicodeString
)source
+ " => " + pat0
+ ", " + pat2
);
247 UBool
UnicodeSetTest::checkPat(const UnicodeString
& source
,
248 const UnicodeSet
& testSet
,
249 const UnicodeString
& pat
) {
250 UErrorCode ec
= U_ZERO_ERROR
;
251 UnicodeSet
testSet2(pat
, ec
);
252 if (testSet2
!= testSet
) {
253 errln((UnicodeString
)"Fail toPattern: " + source
+ " => " + pat
);
260 UnicodeSetTest::TestPatterns(void) {
262 expectPattern(set
, UnicodeString("[[a-m]&[d-z]&[k-y]]", ""), "km");
263 expectPattern(set
, UnicodeString("[[a-z]-[m-y]-[d-r]]", ""), "aczz");
264 expectPattern(set
, UnicodeString("[a\\-z]", ""), "--aazz");
265 expectPattern(set
, UnicodeString("[-az]", ""), "--aazz");
266 expectPattern(set
, UnicodeString("[az-]", ""), "--aazz");
267 expectPattern(set
, UnicodeString("[[[a-z]-[aeiou]i]]", ""), "bdfnptvz");
269 // Throw in a test of complement
272 exp
.append((UChar
)0x0000).append("aeeoouu").append((UChar
)(0x007a+1)).append((UChar
)0xFFFF);
273 expectPairs(set
, exp
);
277 UnicodeSetTest::TestCategories(void) {
278 UErrorCode status
= U_ZERO_ERROR
;
279 const char* pat
= " [:Lu:] "; // Whitespace ok outside [:..:]
280 UnicodeSet
set(pat
, status
);
281 if (U_FAILURE(status
)) {
282 dataerrln((UnicodeString
)"Fail: Can't construct set with " + pat
+ " - " + UnicodeString(u_errorName(status
)));
285 expectContainment(set
, pat
, "ABC", "abc");
289 int32_t failures
= 0;
290 // Make sure generation of L doesn't pollute cached Lu set
291 // First generate L, then Lu
292 set
.applyPattern("[:L:]", status
);
293 if (U_FAILURE(status
)) { errln("FAIL"); return; }
294 for (i
=0; i
<0x200; ++i
) {
295 UBool l
= u_isalpha((UChar
)i
);
296 if (l
!= set
.contains(i
)) {
297 errln((UnicodeString
)"FAIL: L contains " + (unsigned short)i
+ " = " +
299 if (++failures
== 10) break;
303 set
.applyPattern("[:Lu:]", status
);
304 if (U_FAILURE(status
)) { errln("FAIL"); return; }
305 for (i
=0; i
<0x200; ++i
) {
306 UBool lu
= (u_charType((UChar
)i
) == U_UPPERCASE_LETTER
);
307 if (lu
!= set
.contains(i
)) {
308 errln((UnicodeString
)"FAIL: Lu contains " + (unsigned short)i
+ " = " +
310 if (++failures
== 20) break;
315 UnicodeSetTest::TestCloneEqualHash(void) {
316 UErrorCode status
= U_ZERO_ERROR
;
317 // set1 and set2 used to be built with the obsolete constructor taking
318 // UCharCategory values; replaced with pattern constructors
320 UnicodeSet
*set1
=new UnicodeSet(UNICODE_STRING_SIMPLE("\\p{Lowercase Letter}"), status
); // :Ll: Letter, lowercase
321 UnicodeSet
*set1a
=new UnicodeSet(UNICODE_STRING_SIMPLE("[:Ll:]"), status
); // Letter, lowercase
322 if (U_FAILURE(status
)){
323 dataerrln((UnicodeString
)"FAIL: Can't construst set with category->Ll" + " - " + UnicodeString(u_errorName(status
)));
326 UnicodeSet
*set2
=new UnicodeSet(UNICODE_STRING_SIMPLE("\\p{Decimal Number}"), status
); //Number, Decimal digit
327 UnicodeSet
*set2a
=new UnicodeSet(UNICODE_STRING_SIMPLE("[:Nd:]"), status
); //Number, Decimal digit
328 if (U_FAILURE(status
)){
329 errln((UnicodeString
)"FAIL: Can't construct set with category->Nd");
333 if (*set1
!= *set1a
) {
334 errln("FAIL: category constructor for Ll broken");
336 if (*set2
!= *set2a
) {
337 errln("FAIL: category constructor for Nd broken");
342 logln("Testing copy construction");
343 UnicodeSet
*set1copy
=new UnicodeSet(*set1
);
344 if(*set1
!= *set1copy
|| *set1
== *set2
||
345 getPairs(*set1
) != getPairs(*set1copy
) ||
346 set1
->hashCode() != set1copy
->hashCode()){
347 errln("FAIL : Error in copy construction");
351 logln("Testing =operator");
352 UnicodeSet set1equal
=*set1
;
353 UnicodeSet set2equal
=*set2
;
354 if(set1equal
!= *set1
|| set1equal
!= *set1copy
|| set2equal
!= *set2
||
355 set2equal
== *set1
|| set2equal
== *set1copy
|| set2equal
== set1equal
){
356 errln("FAIL: Error in =operator");
359 logln("Testing clone()");
360 UnicodeSet
*set1clone
=(UnicodeSet
*)set1
->clone();
361 UnicodeSet
*set2clone
=(UnicodeSet
*)set2
->clone();
362 if(*set1clone
!= *set1
|| *set1clone
!= *set1copy
|| *set1clone
!= set1equal
||
363 *set2clone
!= *set2
|| *set2clone
== *set1copy
|| *set2clone
!= set2equal
||
364 *set2clone
== *set1
|| *set2clone
== set1equal
|| *set2clone
== *set1clone
){
365 errln("FAIL: Error in clone");
368 logln("Testing hashcode");
369 if(set1
->hashCode() != set1equal
.hashCode() || set1
->hashCode() != set1clone
->hashCode() ||
370 set2
->hashCode() != set2equal
.hashCode() || set2
->hashCode() != set2clone
->hashCode() ||
371 set1copy
->hashCode() != set1equal
.hashCode() || set1copy
->hashCode() != set1clone
->hashCode() ||
372 set1
->hashCode() == set2
->hashCode() || set1copy
->hashCode() == set2
->hashCode() ||
373 set2
->hashCode() == set1clone
->hashCode() || set2
->hashCode() == set1equal
.hashCode() ){
374 errln("FAIL: Error in hashCode()");
386 UnicodeSetTest::TestAddRemove(void) {
387 UnicodeSet set
; // Construct empty set
388 doAssert(set
.isEmpty() == TRUE
, "set should be empty");
389 doAssert(set
.size() == 0, "size should be 0");
391 doAssert(set
.size() == 0x110000, "size should be 0x110000");
393 set
.add(0x0061, 0x007a);
394 expectPairs(set
, "az");
395 doAssert(set
.isEmpty() == FALSE
, "set should not be empty");
396 doAssert(set
.size() != 0, "size should not be equal to 0");
397 doAssert(set
.size() == 26, "size should be equal to 26");
398 set
.remove(0x006d, 0x0070);
399 expectPairs(set
, "alqz");
400 doAssert(set
.size() == 22, "size should be equal to 22");
401 set
.remove(0x0065, 0x0067);
402 expectPairs(set
, "adhlqz");
403 doAssert(set
.size() == 19, "size should be equal to 19");
404 set
.remove(0x0064, 0x0069);
405 expectPairs(set
, "acjlqz");
406 doAssert(set
.size() == 16, "size should be equal to 16");
407 set
.remove(0x0063, 0x0072);
408 expectPairs(set
, "absz");
409 doAssert(set
.size() == 10, "size should be equal to 10");
410 set
.add(0x0066, 0x0071);
411 expectPairs(set
, "abfqsz");
412 doAssert(set
.size() == 22, "size should be equal to 22");
413 set
.remove(0x0061, 0x0067);
414 expectPairs(set
, "hqsz");
415 set
.remove(0x0061, 0x007a);
416 expectPairs(set
, "");
417 doAssert(set
.isEmpty() == TRUE
, "set should be empty");
418 doAssert(set
.size() == 0, "size should be 0");
420 doAssert(set
.isEmpty() == FALSE
, "set should not be empty");
421 doAssert(set
.size() == 1, "size should not be equal to 1");
424 expectPairs(set
, "ac");
425 doAssert(set
.size() == 3, "size should not be equal to 3");
428 expectPairs(set
, "acpq");
429 doAssert(set
.size() == 5, "size should not be equal to 5");
431 expectPairs(set
, "");
432 doAssert(set
.isEmpty() == TRUE
, "set should be empty");
433 doAssert(set
.size() == 0, "size should be 0");
435 // Try removing an entire set from another set
436 expectPattern(set
, "[c-x]", "cx");
438 expectPattern(set2
, "[f-ky-za-bc[vw]]", "acfkvwyz");
440 expectPairs(set
, "deluxx");
442 // Try adding an entire set to another set
443 expectPattern(set
, "[jackiemclean]", "aacceein");
444 expectPattern(set2
, "[hitoshinamekatajamesanderson]", "aadehkmort");
446 expectPairs(set
, "aacehort");
447 doAssert(set
.containsAll(set2
) == TRUE
, "set should contain all the elements in set2");
449 // Try retaining an set of elements contained in another set (intersection)
451 expectPattern(set3
, "[a-c]", "ac");
452 doAssert(set
.containsAll(set3
) == FALSE
, "set doesn't contain all the elements in set3");
454 expectPairs(set3
, "aacc");
455 doAssert(set
.containsAll(set3
) == TRUE
, "set should contain all the elements in set3");
457 expectPairs(set
, "aacc");
458 doAssert(set
.size() == set3
.size(), "set.size() should be set3.size()");
459 doAssert(set
.containsAll(set3
) == TRUE
, "set should contain all the elements in set3");
461 doAssert(set
.size() != set3
.size(), "set.size() != set3.size()");
463 // Test commutativity
464 expectPattern(set
, "[hitoshinamekatajamesanderson]", "aadehkmort");
465 expectPattern(set2
, "[jackiemclean]", "aacceein");
467 expectPairs(set
, "aacehort");
468 doAssert(set
.containsAll(set2
) == TRUE
, "set should contain all the elements in set2");
476 * Make sure minimal representation is maintained.
478 void UnicodeSetTest::TestMinimalRep() {
479 UErrorCode status
= U_ZERO_ERROR
;
480 // This is pretty thoroughly tested by checkCanonicalRep()
481 // run against the exhaustive operation results. Use the code
482 // here for debugging specific spot problems.
484 // 1 overlap against 2
485 UnicodeSet
set("[h-km-q]", status
);
486 if (U_FAILURE(status
)) { errln("FAIL"); return; }
487 UnicodeSet
set2("[i-o]", status
);
488 if (U_FAILURE(status
)) { errln("FAIL"); return; }
490 expectPairs(set
, "hq");
492 set
.applyPattern("[a-m]", status
);
493 if (U_FAILURE(status
)) { errln("FAIL"); return; }
494 set2
.applyPattern("[e-o]", status
);
495 if (U_FAILURE(status
)) { errln("FAIL"); return; }
497 expectPairs(set
, "ao");
499 set
.applyPattern("[e-o]", status
);
500 if (U_FAILURE(status
)) { errln("FAIL"); return; }
501 set2
.applyPattern("[a-m]", status
);
502 if (U_FAILURE(status
)) { errln("FAIL"); return; }
504 expectPairs(set
, "ao");
505 // 1 overlap against 3
506 set
.applyPattern("[a-eg-mo-w]", status
);
507 if (U_FAILURE(status
)) { errln("FAIL"); return; }
508 set2
.applyPattern("[d-q]", status
);
509 if (U_FAILURE(status
)) { errln("FAIL"); return; }
511 expectPairs(set
, "aw");
514 void UnicodeSetTest::TestAPI() {
515 UErrorCode status
= U_ZERO_ERROR
;
518 if (!set
.isEmpty() || set
.getRangeCount() != 0) {
519 errln((UnicodeString
)"FAIL, set should be empty but isn't: " +
523 // clear(), isEmpty()
526 errln((UnicodeString
)"FAIL, set shouldn't be empty but is: " +
530 if (!set
.isEmpty()) {
531 errln((UnicodeString
)"FAIL, set should be empty but isn't: " +
537 if (set
.size() != 0) {
538 errln((UnicodeString
)"FAIL, size should be 0, but is " + set
.size() +
542 if (set
.size() != 1) {
543 errln((UnicodeString
)"FAIL, size should be 1, but is " + set
.size() +
546 set
.add(0x0031, 0x0039);
547 if (set
.size() != 10) {
548 errln((UnicodeString
)"FAIL, size should be 10, but is " + set
.size() +
552 // contains(first, last)
554 set
.applyPattern("[A-Y 1-8 b-d l-y]", status
);
555 if (U_FAILURE(status
)) { errln("FAIL"); return; }
556 for (int32_t i
= 0; i
<set
.getRangeCount(); ++i
) {
557 UChar32 a
= set
.getRangeStart(i
);
558 UChar32 b
= set
.getRangeEnd(i
);
559 if (!set
.contains(a
, b
)) {
560 errln((UnicodeString
)"FAIL, should contain " + (unsigned short)a
+ '-' + (unsigned short)b
+
561 " but doesn't: " + set
);
563 if (set
.contains((UChar32
)(a
-1), b
)) {
564 errln((UnicodeString
)"FAIL, shouldn't contain " +
565 (unsigned short)(a
-1) + '-' + (unsigned short)b
+
566 " but does: " + set
);
568 if (set
.contains(a
, (UChar32
)(b
+1))) {
569 errln((UnicodeString
)"FAIL, shouldn't contain " +
570 (unsigned short)a
+ '-' + (unsigned short)(b
+1) +
571 " but does: " + set
);
575 // Ported InversionList test.
576 UnicodeSet
a((UChar32
)3,(UChar32
)10);
577 UnicodeSet
b((UChar32
)7,(UChar32
)15);
580 logln((UnicodeString
)"a [3-10]: " + a
);
581 logln((UnicodeString
)"b [7-15]: " + b
);
584 UnicodeSet
exp((UChar32
)3,(UChar32
)15);
586 logln((UnicodeString
)"c.set(a).add(b): " + c
);
588 errln((UnicodeString
)"FAIL: c.set(a).add(b) = " + c
+ ", expect " + exp
);
591 exp
.set((UChar32
)0, (UChar32
)2);
592 exp
.add((UChar32
)16, UnicodeSet::MAX_VALUE
);
594 logln((UnicodeString
)"c.complement(): " + c
);
596 errln((UnicodeString
)"FAIL: c.complement() = " + c
+ ", expect " + exp
);
599 exp
.set((UChar32
)3, (UChar32
)15);
601 logln((UnicodeString
)"c.complement(): " + c
);
603 errln((UnicodeString
)"FAIL: c.complement() = " + c
+ ", expect " + exp
);
607 exp
.set((UChar32
)3,(UChar32
)6);
608 exp
.add((UChar32
)11,(UChar32
) 15);
610 logln((UnicodeString
)"c.set(a).exclusiveOr(b): " + c
);
612 errln((UnicodeString
)"FAIL: c.set(a).exclusiveOr(b) = " + c
+ ", expect " + exp
);
616 bitsToSet(setToBits(c
), c
);
618 logln((UnicodeString
)"bitsToSet(setToBits(c)): " + c
);
620 errln((UnicodeString
)"FAIL: bitsToSet(setToBits(c)) = " + c
+ ", expect " + exp
);
623 // Additional tests for coverage JB#2118
624 //UnicodeSet::complement(class UnicodeString const &)
625 //UnicodeSet::complementAll(class UnicodeString const &)
626 //UnicodeSet::containsNone(class UnicodeSet const &)
627 //UnicodeSet::containsNone(long,long)
628 //UnicodeSet::containsSome(class UnicodeSet const &)
629 //UnicodeSet::containsSome(long,long)
630 //UnicodeSet::removeAll(class UnicodeString const &)
631 //UnicodeSet::retain(long)
632 //UnicodeSet::retainAll(class UnicodeString const &)
633 //UnicodeSet::serialize(unsigned short *,long,enum UErrorCode &)
634 //UnicodeSetIterator::getString(void)
636 set
.complement("ab");
637 exp
.applyPattern("[{ab}]", status
);
638 if (U_FAILURE(status
)) { errln("FAIL"); return; }
639 if (set
!= exp
) { errln("FAIL: complement(\"ab\")"); return; }
641 UnicodeSetIterator
iset(set
);
642 if (!iset
.next() || !iset
.isString()) {
643 errln("FAIL: UnicodeSetIterator::next/isString");
644 } else if (iset
.getString() != "ab") {
645 errln("FAIL: UnicodeSetIterator::getString");
648 set
.add((UChar32
)0x61, (UChar32
)0x7A);
649 set
.complementAll("alan");
650 exp
.applyPattern("[{ab}b-kmo-z]", status
);
651 if (U_FAILURE(status
)) { errln("FAIL"); return; }
652 if (set
!= exp
) { errln("FAIL: complementAll(\"alan\")"); return; }
654 exp
.applyPattern("[a-z]", status
);
655 if (U_FAILURE(status
)) { errln("FAIL"); return; }
656 if (set
.containsNone(exp
)) { errln("FAIL: containsNone(UnicodeSet)"); }
657 if (!set
.containsSome(exp
)) { errln("FAIL: containsSome(UnicodeSet)"); }
658 exp
.applyPattern("[aln]", status
);
659 if (U_FAILURE(status
)) { errln("FAIL"); return; }
660 if (!set
.containsNone(exp
)) { errln("FAIL: containsNone(UnicodeSet)"); }
661 if (set
.containsSome(exp
)) { errln("FAIL: containsSome(UnicodeSet)"); }
663 if (set
.containsNone((UChar32
)0x61, (UChar32
)0x7A)) {
664 errln("FAIL: containsNone(UChar32, UChar32)");
666 if (!set
.containsSome((UChar32
)0x61, (UChar32
)0x7A)) {
667 errln("FAIL: containsSome(UChar32, UChar32)");
669 if (!set
.containsNone((UChar32
)0x41, (UChar32
)0x5A)) {
670 errln("FAIL: containsNone(UChar32, UChar32)");
672 if (set
.containsSome((UChar32
)0x41, (UChar32
)0x5A)) {
673 errln("FAIL: containsSome(UChar32, UChar32)");
676 set
.removeAll("liu");
677 exp
.applyPattern("[{ab}b-hj-kmo-tv-z]", status
);
678 if (U_FAILURE(status
)) { errln("FAIL"); return; }
679 if (set
!= exp
) { errln("FAIL: removeAll(\"liu\")"); return; }
681 set
.retainAll("star");
682 exp
.applyPattern("[rst]", status
);
683 if (U_FAILURE(status
)) { errln("FAIL"); return; }
684 if (set
!= exp
) { errln("FAIL: retainAll(\"star\")"); return; }
686 set
.retain((UChar32
)0x73);
687 exp
.applyPattern("[s]", status
);
688 if (U_FAILURE(status
)) { errln("FAIL"); return; }
689 if (set
!= exp
) { errln("FAIL: retain('s')"); return; }
692 int32_t slen
= set
.serialize(buf
, sizeof(buf
)/sizeof(buf
[0]), status
);
693 if (U_FAILURE(status
)) { errln("FAIL: serialize"); return; }
694 if (slen
!= 3 || buf
[0] != 2 || buf
[1] != 0x73 || buf
[2] != 0x74) {
695 errln("FAIL: serialize");
699 // Conversions to and from USet
700 UnicodeSet
*uniset
= &set
;
701 USet
*uset
= uniset
->toUSet();
702 TEST_ASSERT((void *)uset
== (void *)uniset
);
703 UnicodeSet
*setx
= UnicodeSet::fromUSet(uset
);
704 TEST_ASSERT((void *)setx
== (void *)uset
);
705 const UnicodeSet
*constSet
= uniset
;
706 const USet
*constUSet
= constSet
->toUSet();
707 TEST_ASSERT((void *)constUSet
== (void *)constSet
);
708 const UnicodeSet
*constSetx
= UnicodeSet::fromUSet(constUSet
);
709 TEST_ASSERT((void *)constSetx
== (void *)constUSet
);
711 // span(UnicodeString) and spanBack(UnicodeString) convenience methods
712 UnicodeString longString
=UNICODE_STRING_SIMPLE("aaaaaaaaaabbbbbbbbbbcccccccccc");
713 UnicodeSet
ac(0x61, 0x63);
714 ac
.remove(0x62).freeze();
715 if( ac
.span(longString
, -5, USET_SPAN_CONTAINED
)!=10 ||
716 ac
.span(longString
, 0, USET_SPAN_CONTAINED
)!=10 ||
717 ac
.span(longString
, 5, USET_SPAN_CONTAINED
)!=10 ||
718 ac
.span(longString
, 10, USET_SPAN_CONTAINED
)!=10 ||
719 ac
.span(longString
, 15, USET_SPAN_CONTAINED
)!=15 ||
720 ac
.span(longString
, 20, USET_SPAN_CONTAINED
)!=30 ||
721 ac
.span(longString
, 25, USET_SPAN_CONTAINED
)!=30 ||
722 ac
.span(longString
, 30, USET_SPAN_CONTAINED
)!=30 ||
723 ac
.span(longString
, 35, USET_SPAN_CONTAINED
)!=30 ||
724 ac
.span(longString
, INT32_MAX
, USET_SPAN_CONTAINED
)!=30
726 errln("UnicodeSet.span(UnicodeString, ...) returns incorrect end indexes");
728 if( ac
.spanBack(longString
, -5, USET_SPAN_CONTAINED
)!=0 ||
729 ac
.spanBack(longString
, 0, USET_SPAN_CONTAINED
)!=0 ||
730 ac
.spanBack(longString
, 5, USET_SPAN_CONTAINED
)!=0 ||
731 ac
.spanBack(longString
, 10, USET_SPAN_CONTAINED
)!=0 ||
732 ac
.spanBack(longString
, 15, USET_SPAN_CONTAINED
)!=15 ||
733 ac
.spanBack(longString
, 20, USET_SPAN_CONTAINED
)!=20 ||
734 ac
.spanBack(longString
, 25, USET_SPAN_CONTAINED
)!=20 ||
735 ac
.spanBack(longString
, 30, USET_SPAN_CONTAINED
)!=20 ||
736 ac
.spanBack(longString
, 35, USET_SPAN_CONTAINED
)!=20 ||
737 ac
.spanBack(longString
, INT32_MAX
, USET_SPAN_CONTAINED
)!=20
739 errln("UnicodeSet.spanBack(UnicodeString, ...) returns incorrect start indexes");
743 void UnicodeSetTest::TestIteration() {
744 UErrorCode ec
= U_ZERO_ERROR
;
748 // 6 code points, 3 ranges, 2 strings, 8 total elements
749 // Iteration will access them in sorted order - a, b, c, y, z, U0001abcd, "str1", "str2"
750 UnicodeSet
set(UNICODE_STRING_SIMPLE("[zabyc\\U0001abcd{str1}{str2}]"), ec
);
751 TEST_ASSERT_SUCCESS(ec
);
752 UnicodeSetIterator
it(set
);
754 for (outerLoop
=0; outerLoop
<3; outerLoop
++) {
755 // Run the test multiple times, to check that iterator.reset() is working.
756 for (i
=0; i
<10; i
++) {
757 UBool nextv
= it
.next();
758 UBool isString
= it
.isString();
759 int32_t codePoint
= it
.getCodepoint();
760 //int32_t codePointEnd = it.getCodepointEnd();
761 UnicodeString s
= it
.getString();
764 TEST_ASSERT(nextv
== TRUE
);
765 TEST_ASSERT(isString
== FALSE
);
766 TEST_ASSERT(codePoint
==0x61);
767 TEST_ASSERT(s
== "a");
770 TEST_ASSERT(nextv
== TRUE
);
771 TEST_ASSERT(isString
== FALSE
);
772 TEST_ASSERT(codePoint
==0x62);
773 TEST_ASSERT(s
== "b");
776 TEST_ASSERT(nextv
== TRUE
);
777 TEST_ASSERT(isString
== FALSE
);
778 TEST_ASSERT(codePoint
==0x63);
779 TEST_ASSERT(s
== "c");
782 TEST_ASSERT(nextv
== TRUE
);
783 TEST_ASSERT(isString
== FALSE
);
784 TEST_ASSERT(codePoint
==0x79);
785 TEST_ASSERT(s
== "y");
788 TEST_ASSERT(nextv
== TRUE
);
789 TEST_ASSERT(isString
== FALSE
);
790 TEST_ASSERT(codePoint
==0x7a);
791 TEST_ASSERT(s
== "z");
794 TEST_ASSERT(nextv
== TRUE
);
795 TEST_ASSERT(isString
== FALSE
);
796 TEST_ASSERT(codePoint
==0x1abcd);
797 TEST_ASSERT(s
== UnicodeString((UChar32
)0x1abcd));
800 TEST_ASSERT(nextv
== TRUE
);
801 TEST_ASSERT(isString
== TRUE
);
802 TEST_ASSERT(s
== "str1");
805 TEST_ASSERT(nextv
== TRUE
);
806 TEST_ASSERT(isString
== TRUE
);
807 TEST_ASSERT(s
== "str2");
810 TEST_ASSERT(nextv
== FALSE
);
813 TEST_ASSERT(nextv
== FALSE
);
817 it
.reset(); // prepare to run the iteration again.
824 void UnicodeSetTest::TestStrings() {
825 UErrorCode ec
= U_ZERO_ERROR
;
827 UnicodeSet
* testList
[] = {
828 UnicodeSet::createFromAll("abc"),
829 new UnicodeSet("[a-c]", ec
),
831 &(UnicodeSet::createFrom("ch")->add('a','z').add("ll")),
832 new UnicodeSet("[{ll}{ch}a-z]", ec
),
834 UnicodeSet::createFrom("ab}c"),
835 new UnicodeSet("[{ab\\}c}]", ec
),
837 &((new UnicodeSet('a','z'))->add('A', 'Z').retain('M','m').complement('X')),
838 new UnicodeSet("[[a-zA-Z]&[M-m]-[X]]", ec
),
844 errln("FAIL: couldn't construct test sets");
847 for (int32_t i
= 0; testList
[i
] != NULL
; i
+=2) {
849 UnicodeString pat0
, pat1
;
850 testList
[i
]->toPattern(pat0
, TRUE
);
851 testList
[i
+1]->toPattern(pat1
, TRUE
);
852 if (*testList
[i
] == *testList
[i
+1]) {
853 logln((UnicodeString
)"Ok: " + pat0
+ " == " + pat1
);
855 logln((UnicodeString
)"FAIL: " + pat0
+ " != " + pat1
);
859 delete testList
[i
+1];
864 * Test the [:Latin:] syntax.
866 void UnicodeSetTest::TestScriptSet() {
867 expectContainment(UNICODE_STRING_SIMPLE("[:Latin:]"), "aA", CharsToUnicodeString("\\u0391\\u03B1"));
869 expectContainment(UNICODE_STRING_SIMPLE("[:Greek:]"), CharsToUnicodeString("\\u0391\\u03B1"), "aA");
872 expectContainment(UNICODE_STRING_SIMPLE("[[:Common:][:Inherited:]]"), CharsToUnicodeString("\\U00003099\\U0001D169\\u0000"), "aA");
877 * Test the [:Latin:] syntax.
879 void UnicodeSetTest::TestPropertySet() {
880 static const char* const DATA
[] = {
881 // Pattern, Chars IN, Chars NOT in
891 "\\P{ GENERAL Category = upper case letter }",
895 #if !UCONFIG_NO_NORMALIZATION
896 // Combining class: @since ICU 2.2
897 // Check both symbolic and numeric
902 "\\p{Canonical Combining Class = 11}",
906 "[:c c c = iota subscript :]",
911 // Bidi class: @since ICU 2.2
912 "\\p{bidiclass=lefttoright}",
916 // Binary properties: @since ICU 2.2
923 // weiv: )(and * were removed from math in Unicode 4.0.1
927 // JB#1767 \N{}, \p{ASCII}
932 "[\\N{ latin small letter a }[:name= latin small letter z:]]",
948 "\\u03D8\\u03D9", // 3.2
951 "\\u1800\\u3400\\U0002f800",
952 "\\u0220\\u034f\\u30ff\\u33ff\\ufe73\\U00010000\\U00050000",
954 // JB#2350: Case_Sensitive
955 "[:Case Sensitive:]",
956 "A\\u1FFC\\U00010410",
957 ";\\u00B4\\U00010500",
959 // JB#2832: C99-compatibility props
966 " \\u0003\\u0007\\u0009\\u000A\\u000D",
969 "!@#%&*()[]{}-_\\/;:,.?'\"",
976 // Regex compatibility test
977 "[-b]", // leading '-' is literal
981 "[^-b]", // leading '-' is literal
985 "[b-]", // trailing '-' is literal
989 "[^b-]", // trailing '-' is literal
993 "[a-b-]", // trailing '-' is literal
997 "[[a-q]&[p-z]-]", // trailing '-' is literal
1001 "[\\s|\\)|:|$|\\>]", // from regex tests
1005 "[\\uDC00cd]", // JB#2906: isolated trail at start
1007 "ab\\uD800\\U00010000",
1009 "[ab\\uD800]", // JB#2906: isolated trail at start
1011 "cd\\uDC00\\U00010000",
1013 "[ab\\uD800cd]", // JB#2906: isolated lead in middle
1015 "ef\\uDC00\\U00010000",
1017 "[ab\\uDC00cd]", // JB#2906: isolated trail in middle
1019 "ef\\uD800\\U00010000",
1021 #if !UCONFIG_NO_NORMALIZATION
1022 "[:^lccc=0:]", // Lead canonical class
1024 "abcd\\u00c0\\u00c5",
1026 "[:^tccc=0:]", // Trail canonical class
1027 "\\u0300\\u0301\\u00c0\\u00c5",
1030 "[[:^lccc=0:][:^tccc=0:]]", // Lead and trail canonical class
1031 "\\u0300\\u0301\\u00c0\\u00c5",
1034 "[[:^lccc=0:]-[:^tccc=0:]]", // Stuff that starts with an accent but ends with a base (none right now)
1036 "abcd\\u0300\\u0301\\u00c0\\u00c5",
1038 "[[:ccc=0:]-[:lccc=0:]-[:tccc=0:]]", // Weirdos. Complete canonical class is zero, but both lead and trail are not
1039 "\\u0F73\\u0F75\\u0F81",
1040 "abcd\\u0300\\u0301\\u00c0\\u00c5",
1041 #endif /* !UCONFIG_NO_NORMALIZATION */
1044 "A\\uE000\\uF8FF\\uFDC7\\U00010000\\U0010FFFD",
1045 "\\u0888\\uFDD3\\uFFFE\\U00050005",
1047 // Script_Extensions, new in Unicode 6.0
1049 "\\u061E\\u061F\\u0620\\u0621\\u063F\\u0640\\u0650\\u065E\\uFDF1\\uFDF2\\uFDF3",
1050 "\\u061D\\uFDEF\\uFDFE",
1052 // U+FDF2 has Script=Arabic and also Arab in its Script_Extensions,
1053 // so scx-sc is missing U+FDF2.
1054 "[[:Script_Extensions=Arabic:]-[:Arab:]]",
1055 "\\u0640\\u064B\\u0650\\u0655",
1059 static const int32_t DATA_LEN
= sizeof(DATA
)/sizeof(DATA
[0]);
1061 for (int32_t i
=0; i
<DATA_LEN
; i
+=3) {
1062 expectContainment(UnicodeString(DATA
[i
], -1, US_INV
), CharsToUnicodeString(DATA
[i
+1]),
1063 CharsToUnicodeString(DATA
[i
+2]));
1068 * Test that Posix style character classes [:digit:], etc.
1069 * have the Unicode definitions from TR 18.
1071 void UnicodeSetTest::TestPosixClasses() {
1073 UErrorCode status
= U_ZERO_ERROR
;
1074 UnicodeSet
s1("[:alpha:]", status
);
1075 UnicodeSet
s2(UNICODE_STRING_SIMPLE("\\p{Alphabetic}"), status
);
1076 TEST_ASSERT_SUCCESS(status
);
1077 TEST_ASSERT(s1
==s2
);
1080 UErrorCode status
= U_ZERO_ERROR
;
1081 UnicodeSet
s1("[:lower:]", status
);
1082 UnicodeSet
s2(UNICODE_STRING_SIMPLE("\\p{lowercase}"), status
);
1083 TEST_ASSERT_SUCCESS(status
);
1084 TEST_ASSERT(s1
==s2
);
1087 UErrorCode status
= U_ZERO_ERROR
;
1088 UnicodeSet
s1("[:upper:]", status
);
1089 UnicodeSet
s2(UNICODE_STRING_SIMPLE("\\p{Uppercase}"), status
);
1090 TEST_ASSERT_SUCCESS(status
);
1091 TEST_ASSERT(s1
==s2
);
1094 UErrorCode status
= U_ZERO_ERROR
;
1095 UnicodeSet
s1("[:punct:]", status
);
1096 UnicodeSet
s2(UNICODE_STRING_SIMPLE("\\p{gc=Punctuation}"), status
);
1097 TEST_ASSERT_SUCCESS(status
);
1098 TEST_ASSERT(s1
==s2
);
1101 UErrorCode status
= U_ZERO_ERROR
;
1102 UnicodeSet
s1("[:digit:]", status
);
1103 UnicodeSet
s2(UNICODE_STRING_SIMPLE("\\p{gc=DecimalNumber}"), status
);
1104 TEST_ASSERT_SUCCESS(status
);
1105 TEST_ASSERT(s1
==s2
);
1108 UErrorCode status
= U_ZERO_ERROR
;
1109 UnicodeSet
s1("[:xdigit:]", status
);
1110 UnicodeSet
s2(UNICODE_STRING_SIMPLE("[\\p{DecimalNumber}\\p{HexDigit}]"), status
);
1111 TEST_ASSERT_SUCCESS(status
);
1112 TEST_ASSERT(s1
==s2
);
1115 UErrorCode status
= U_ZERO_ERROR
;
1116 UnicodeSet
s1("[:alnum:]", status
);
1117 UnicodeSet
s2(UNICODE_STRING_SIMPLE("[\\p{Alphabetic}\\p{DecimalNumber}]"), status
);
1118 TEST_ASSERT_SUCCESS(status
);
1119 TEST_ASSERT(s1
==s2
);
1122 UErrorCode status
= U_ZERO_ERROR
;
1123 UnicodeSet
s1("[:space:]", status
);
1124 UnicodeSet
s2(UNICODE_STRING_SIMPLE("\\p{Whitespace}"), status
);
1125 TEST_ASSERT_SUCCESS(status
);
1126 TEST_ASSERT(s1
==s2
);
1129 UErrorCode status
= U_ZERO_ERROR
;
1130 UnicodeSet
s1("[:blank:]", status
);
1131 TEST_ASSERT_SUCCESS(status
);
1132 UnicodeSet
s2(UNICODE_STRING_SIMPLE("[\\p{Whitespace}-[\\u000a\\u000B\\u000c\\u000d\\u0085\\p{LineSeparator}\\p{ParagraphSeparator}]]"),
1134 TEST_ASSERT_SUCCESS(status
);
1135 TEST_ASSERT(s1
==s2
);
1138 UErrorCode status
= U_ZERO_ERROR
;
1139 UnicodeSet
s1("[:cntrl:]", status
);
1140 TEST_ASSERT_SUCCESS(status
);
1141 UnicodeSet
s2(UNICODE_STRING_SIMPLE("\\p{Control}"), status
);
1142 TEST_ASSERT_SUCCESS(status
);
1143 TEST_ASSERT(s1
==s2
);
1146 UErrorCode status
= U_ZERO_ERROR
;
1147 UnicodeSet
s1("[:graph:]", status
);
1148 TEST_ASSERT_SUCCESS(status
);
1149 UnicodeSet
s2(UNICODE_STRING_SIMPLE("[^\\p{Whitespace}\\p{Control}\\p{Surrogate}\\p{Unassigned}]"), status
);
1150 TEST_ASSERT_SUCCESS(status
);
1151 TEST_ASSERT(s1
==s2
);
1154 UErrorCode status
= U_ZERO_ERROR
;
1155 UnicodeSet
s1("[:print:]", status
);
1156 TEST_ASSERT_SUCCESS(status
);
1157 UnicodeSet
s2(UNICODE_STRING_SIMPLE("[[:graph:][:blank:]-[\\p{Control}]]") ,status
);
1158 TEST_ASSERT_SUCCESS(status
);
1159 TEST_ASSERT(s1
==s2
);
1163 * Test cloning of UnicodeSet. For C++, we test the copy constructor.
1165 void UnicodeSetTest::TestClone() {
1166 UErrorCode ec
= U_ZERO_ERROR
;
1167 UnicodeSet
s("[abcxyz]", ec
);
1169 expectContainment(t
, "abc", "def");
1173 * Test the indexOf() and charAt() methods.
1175 void UnicodeSetTest::TestIndexOf() {
1176 UErrorCode ec
= U_ZERO_ERROR
;
1177 UnicodeSet
set("[a-cx-y3578]", ec
);
1178 if (U_FAILURE(ec
)) {
1179 errln("FAIL: UnicodeSet constructor");
1182 for (int32_t i
=0; i
<set
.size(); ++i
) {
1183 UChar32 c
= set
.charAt(i
);
1184 if (set
.indexOf(c
) != i
) {
1185 errln("FAIL: charAt(%d) = %X => indexOf() => %d",
1186 i
, c
, set
.indexOf(c
));
1189 UChar32 c
= set
.charAt(set
.size());
1191 errln("FAIL: charAt(<out of range>) = %X", c
);
1193 int32_t j
= set
.indexOf((UChar32
)0x71/*'q'*/);
1195 errln((UnicodeString
)"FAIL: indexOf('q') = " + j
);
1202 void UnicodeSetTest::TestCloseOver() {
1203 UErrorCode ec
= U_ZERO_ERROR
;
1205 char CASE
[] = {(char)USET_CASE_INSENSITIVE
};
1206 char CASE_MAPPINGS
[] = {(char)USET_ADD_CASE_MAPPINGS
};
1207 const char* DATA
[] = {
1208 // selector, input, output
1210 "[aq\\u00DF{Bc}{bC}{Fi}]",
1211 "[aAqQ\\u00DF\\u1E9E\\uFB01{ss}{bc}{fi}]", // U+1E9E LATIN CAPITAL LETTER SHARP S is new in Unicode 5.1
1214 "[\\u01F1]", // 'DZ'
1215 "[\\u01F1\\u01F2\\u01F3]",
1219 "[\\u1FB4{\\u03AC\\u03B9}]",
1225 CASE
, // make sure binary search finds limits
1227 "[aA\\uFF3A\\uFF5A]",
1230 "[a-z]","[A-Za-z\\u017F\\u212A]",
1236 CASE
, "[i]", "[iI]",
1238 CASE
, "[\\u0130]", "[\\u0130{i\\u0307}]", // dotted I
1239 CASE
, "[{i\\u0307}]", "[\\u0130{i\\u0307}]", // i with dot
1241 CASE
, "[\\u0131]", "[\\u0131]", // dotless i
1243 CASE
, "[\\u0390]", "[\\u0390\\u1FD3{\\u03B9\\u0308\\u0301}]",
1245 CASE
, "[\\u03c2]", "[\\u03a3\\u03c2\\u03c3]", // sigmas
1247 CASE
, "[\\u03f2]", "[\\u03f2\\u03f9]", // lunate sigmas
1249 CASE
, "[\\u03f7]", "[\\u03f7\\u03f8]",
1251 CASE
, "[\\u1fe3]", "[\\u03b0\\u1fe3{\\u03c5\\u0308\\u0301}]",
1253 CASE
, "[\\ufb05]", "[\\ufb05\\ufb06{st}]",
1254 CASE
, "[{st}]", "[\\ufb05\\ufb06{st}]",
1256 CASE
, "[\\U0001044F]", "[\\U00010427\\U0001044F]",
1258 CASE
, "[{a\\u02BE}]", "[\\u1E9A{a\\u02BE}]", // first in sorted table
1260 CASE
, "[{\\u1f7c\\u03b9}]", "[\\u1ff2{\\u1f7c\\u03b9}]", // last in sorted table
1262 #if !UCONFIG_NO_FILE_IO
1264 "[aq\\u00DF{Bc}{bC}{Fi}]",
1265 "[aAqQ\\u00DF{ss}{Ss}{SS}{Bc}{BC}{bC}{bc}{FI}{Fi}{fi}]",
1269 "[\\u01F1]", // 'DZ'
1270 "[\\u01F1\\u01F2\\u01F3]",
1282 for (int32_t i
=0; DATA
[i
]!=NULL
; i
+=3) {
1283 int32_t selector
= DATA
[i
][0];
1284 UnicodeString
pat(DATA
[i
+1], -1, US_INV
);
1285 UnicodeString
exp(DATA
[i
+2], -1, US_INV
);
1286 s
.applyPattern(pat
, ec
);
1287 s
.closeOver(selector
);
1288 t
.applyPattern(exp
, ec
);
1289 if (U_FAILURE(ec
)) {
1290 errln("FAIL: applyPattern failed");
1294 logln((UnicodeString
)"Ok: " + pat
+ ".closeOver(" + selector
+ ") => " + exp
);
1296 dataerrln((UnicodeString
)"FAIL: " + pat
+ ".closeOver(" + selector
+ ") => " +
1297 s
.toPattern(buf
, TRUE
) + ", expected " + exp
);
1304 * This was used to compare the old implementation (using USET_CASE)
1305 * with the new one (using 0x100 temporarily)
1306 * while transitioning from hardcoded case closure tables in uniset.cpp
1307 * (moved to uniset_props.cpp) to building the data by gencase into ucase.icu.
1308 * and using ucase.c functions for closure.
1309 * See Jitterbug 3432 RFE: Move uniset.cpp data to a data file
1311 * Note: The old and new implementation never fully matched because
1312 * the old implementation turned out to not map U+0130 and U+0131 correctly
1313 * (dotted I and dotless i) and because the old implementation's data tables
1314 * were outdated compared to Unicode 4.0.1 at the time of the change to the
1315 * new implementation. (So sigmas and some other characters were not handled
1316 * according to the newer Unicode version.)
1318 UnicodeSet
sens("[:case_sensitive:]", ec
), sens2
, s2
;
1319 UnicodeSetIterator
si(sens
);
1320 UnicodeString str
, buf2
;
1321 const UnicodeString
*pStr
;
1324 if(!si
.isString()) {
1325 c
=si
.getCodepoint();
1334 s
.closeOver(USET_CASE
);
1337 errln("FAIL: closeOver(U+%04x) differs: ", c
);
1338 errln((UnicodeString
)"old "+s
.toPattern(buf
, TRUE
)+" new: "+t
.toPattern(buf2
, TRUE
));
1342 // remove all code points
1343 // should contain all full case folding mapping strings
1344 sens2
.remove(0, 0x10ffff);
1348 pStr
=&si
.getString();
1352 s
.closeOver(USET_CASE
);
1355 errln((UnicodeString
)"FAIL: closeOver("+s2
.toPattern(buf
, TRUE
)+") differs: ");
1356 errln((UnicodeString
)"old "+s
.toPattern(buf
, TRUE
)+" new: "+t
.toPattern(buf2
, TRUE
));
1362 // Test the pattern API
1363 s
.applyPattern("[abc]", USET_CASE_INSENSITIVE
, NULL
, ec
);
1364 if (U_FAILURE(ec
)) {
1365 errln("FAIL: applyPattern failed");
1367 expectContainment(s
, "abcABC", "defDEF");
1369 UnicodeSet
v("[^abc]", USET_CASE_INSENSITIVE
, NULL
, ec
);
1370 if (U_FAILURE(ec
)) {
1371 errln("FAIL: constructor failed");
1373 expectContainment(v
, "defDEF", "abcABC");
1375 UnicodeSet
cm("[abck]", USET_ADD_CASE_MAPPINGS
, NULL
, ec
);
1376 if (U_FAILURE(ec
)) {
1377 errln("FAIL: construct w/case mappings failed");
1379 expectContainment(cm
, "abckABCK", CharsToUnicodeString("defDEF\\u212A"));
1383 void UnicodeSetTest::TestEscapePattern() {
1384 const char pattern
[] =
1385 "[\\uFEFF \\u200A-\\u200E \\U0001D173-\\U0001D17A \\U000F0000-\\U000FFFFD ]";
1387 "[\\u200A-\\u200E\\uFEFF\\U0001D173-\\U0001D17A\\U000F0000-\\U000FFFFD]";
1388 // We test this with two passes; in the second pass we
1389 // pre-unescape the pattern. Since U+200E is Pattern_White_Space,
1390 // this fails -- which is what we expect.
1391 for (int32_t pass
=1; pass
<=2; ++pass
) {
1392 UErrorCode ec
= U_ZERO_ERROR
;
1393 UnicodeString
pat(pattern
, -1, US_INV
);
1395 pat
= pat
.unescape();
1397 // Pattern is only good for pass 1
1398 UBool isPatternValid
= (pass
==1);
1400 UnicodeSet
set(pat
, ec
);
1401 if (U_SUCCESS(ec
) != isPatternValid
){
1402 errln((UnicodeString
)"FAIL: applyPattern(" +
1403 escape(pat
) + ") => " +
1407 if (U_FAILURE(ec
)) {
1410 if (set
.contains((UChar
)0x0644)){
1411 errln((UnicodeString
)"FAIL: " + escape(pat
) + " contains(U+0664)");
1414 UnicodeString newpat
;
1415 set
.toPattern(newpat
, TRUE
);
1416 if (newpat
== UnicodeString(exp
, -1, US_INV
)) {
1417 logln(escape(pat
) + " => " + newpat
);
1419 errln((UnicodeString
)"FAIL: " + escape(pat
) + " => " + newpat
);
1422 for (int32_t i
=0; i
<set
.getRangeCount(); ++i
) {
1423 UnicodeString
str("Range ");
1424 str
.append((UChar
)(0x30 + i
))
1426 .append((UChar32
)set
.getRangeStart(i
))
1428 .append((UChar32
)set
.getRangeEnd(i
));
1429 str
= str
+ " (" + set
.getRangeStart(i
) + " - " +
1430 set
.getRangeEnd(i
) + ")";
1431 if (set
.getRangeStart(i
) < 0) {
1432 errln((UnicodeString
)"FAIL: " + escape(str
));
1440 void UnicodeSetTest::expectRange(const UnicodeString
& label
,
1441 const UnicodeSet
& set
,
1442 UChar32 start
, UChar32 end
) {
1443 UnicodeSet
exp(start
, end
);
1446 logln(label
+ " => " + set
.toPattern(pat
, TRUE
));
1449 errln((UnicodeString
)"FAIL: " + label
+ " => " +
1450 set
.toPattern(pat
, TRUE
) +
1451 ", expected " + exp
.toPattern(xpat
, TRUE
));
1455 void UnicodeSetTest::TestInvalidCodePoint() {
1457 const UChar32 DATA
[] = {
1458 // Test range Expected range
1459 0, 0x10FFFF, 0, 0x10FFFF,
1460 (UChar32
)-1, 8, 0, 8,
1461 8, 0x110000, 8, 0x10FFFF
1463 const int32_t DATA_LENGTH
= sizeof(DATA
)/sizeof(DATA
[0]);
1468 for (i
=0; i
<DATA_LENGTH
; i
+=4) {
1469 UChar32 start
= DATA
[i
];
1470 UChar32 end
= DATA
[i
+1];
1471 UChar32 xstart
= DATA
[i
+2];
1472 UChar32 xend
= DATA
[i
+3];
1474 // Try various API using the test code points
1476 UnicodeSet
set(start
, end
);
1477 expectRange((UnicodeString
)"ct(" + start
+ "," + end
+ ")",
1481 set
.set(start
, end
);
1482 expectRange((UnicodeString
)"set(" + start
+ "," + end
+ ")",
1485 UBool b
= set
.contains(start
);
1486 b
= set
.contains(start
, end
);
1487 b
= set
.containsNone(start
, end
);
1488 b
= set
.containsSome(start
, end
);
1489 (void)b
; // Suppress set but not used warning.
1491 /*int32_t index = set.indexOf(start);*/
1495 set
.add(start
, end
);
1496 expectRange((UnicodeString
)"add(" + start
+ "," + end
+ ")",
1499 set
.set(0, 0x10FFFF);
1500 set
.retain(start
, end
);
1501 expectRange((UnicodeString
)"retain(" + start
+ "," + end
+ ")",
1505 set
.set(0, 0x10FFFF);
1507 set
.remove(start
, end
);
1509 expectRange((UnicodeString
)"!remove(" + start
+ "," + end
+ ")",
1512 set
.set(0, 0x10FFFF);
1513 set
.complement(start
, end
);
1515 expectRange((UnicodeString
)"!complement(" + start
+ "," + end
+ ")",
1517 set
.complement(start
);
1520 const UChar32 DATA2
[] = {
1526 const int32_t DATA2_LENGTH
= sizeof(DATA2
)/sizeof(DATA2
[0]);
1528 for (i
=0; i
<DATA2_LENGTH
; ++i
) {
1529 UChar32 c
= DATA2
[i
], end
= 0x10FFFF;
1530 UBool valid
= (c
>= 0 && c
<= 0x10FFFF);
1532 UnicodeSet
set(0, 0x10FFFF);
1534 // For single-codepoint contains, invalid codepoints are NOT contained
1535 UBool b
= set
.contains(c
);
1537 logln((UnicodeString
)"[\\u0000-\\U0010FFFF].contains(" + c
+
1540 errln((UnicodeString
)"FAIL: [\\u0000-\\U0010FFFF].contains(" + c
+
1544 // For codepoint range contains, containsNone, and containsSome,
1545 // invalid or empty (start > end) ranges have UNDEFINED behavior.
1546 b
= set
.contains(c
, end
);
1547 logln((UnicodeString
)"* [\\u0000-\\U0010FFFF].contains(" + c
+
1548 "," + end
+ ") = " + b
);
1550 b
= set
.containsNone(c
, end
);
1551 logln((UnicodeString
)"* [\\u0000-\\U0010FFFF].containsNone(" + c
+
1552 "," + end
+ ") = " + b
);
1554 b
= set
.containsSome(c
, end
);
1555 logln((UnicodeString
)"* [\\u0000-\\U0010FFFF].containsSome(" + c
+
1556 "," + end
+ ") = " + b
);
1558 int32_t index
= set
.indexOf(c
);
1559 if ((index
>= 0) == valid
) {
1560 logln((UnicodeString
)"[\\u0000-\\U0010FFFF].indexOf(" + c
+
1563 errln((UnicodeString
)"FAIL: [\\u0000-\\U0010FFFF].indexOf(" + c
+
1569 // Used by TestSymbolTable
1570 class TokenSymbolTable
: public SymbolTable
{
1574 TokenSymbolTable(UErrorCode
& ec
) : contents(FALSE
, ec
) {
1575 contents
.setValueDeleter(uprv_deleteUObject
);
1578 ~TokenSymbolTable() {}
1581 * (Non-SymbolTable API) Add the given variable and value to
1582 * the table. Variable should NOT contain leading '$'.
1584 void add(const UnicodeString
& var
, const UnicodeString
& value
,
1586 if (U_SUCCESS(ec
)) {
1587 contents
.put(var
, new UnicodeString(value
), ec
);
1594 virtual const UnicodeString
* lookup(const UnicodeString
& s
) const {
1595 return (const UnicodeString
*) contents
.get(s
);
1601 virtual const UnicodeFunctor
* lookupMatcher(UChar32
/*ch*/) const {
1608 virtual UnicodeString
parseReference(const UnicodeString
& text
,
1609 ParsePosition
& pos
, int32_t limit
) const {
1610 int32_t start
= pos
.getIndex();
1612 UnicodeString result
;
1614 UChar c
= text
.charAt(i
);
1615 if ((i
==start
&& !u_isIDStart(c
)) || !u_isIDPart(c
)) {
1620 if (i
== start
) { // No valid name chars
1621 return result
; // Indicate failure with empty string
1624 text
.extractBetween(start
, i
, result
);
1629 void UnicodeSetTest::TestSymbolTable() {
1630 // Multiple test cases can be set up here. Each test case
1631 // is terminated by null:
1632 // var, value, var, value,..., input pat., exp. output pat., null
1633 const char* DATA
[] = {
1634 "us", "a-z", "[0-1$us]", "[0-1a-z]", NULL
,
1635 "us", "[a-z]", "[0-1$us]", "[0-1[a-z]]", NULL
,
1636 "us", "\\[a\\-z\\]", "[0-1$us]", "[-01\\[\\]az]", NULL
,
1640 for (int32_t i
=0; DATA
[i
]!=NULL
; ++i
) {
1641 UErrorCode ec
= U_ZERO_ERROR
;
1642 TokenSymbolTable
sym(ec
);
1643 if (U_FAILURE(ec
)) {
1644 errln("FAIL: couldn't construct TokenSymbolTable");
1649 while (DATA
[i
+2] != NULL
) {
1650 sym
.add(UnicodeString(DATA
[i
], -1, US_INV
), UnicodeString(DATA
[i
+1], -1, US_INV
), ec
);
1651 if (U_FAILURE(ec
)) {
1652 errln("FAIL: couldn't add to TokenSymbolTable");
1658 // Input pattern and expected output pattern
1659 UnicodeString inpat
= UnicodeString(DATA
[i
], -1, US_INV
), exppat
= UnicodeString(DATA
[i
+1], -1, US_INV
);
1662 ParsePosition
pos(0);
1663 UnicodeSet
us(inpat
, pos
, USET_IGNORE_SPACE
, &sym
, ec
);
1664 if (U_FAILURE(ec
)) {
1665 errln("FAIL: couldn't construct UnicodeSet");
1670 if (pos
.getIndex() != inpat
.length()) {
1671 errln((UnicodeString
)"Failed to read to end of string \""
1672 + inpat
+ "\": read to "
1673 + pos
.getIndex() + ", length is "
1677 UnicodeSet
us2(exppat
, ec
);
1678 if (U_FAILURE(ec
)) {
1679 errln("FAIL: couldn't construct expected UnicodeSet");
1685 errln((UnicodeString
)"Failed, got " + us
.toPattern(a
, TRUE
) +
1686 ", expected " + us2
.toPattern(b
, TRUE
));
1688 logln((UnicodeString
)"Ok, got " + us
.toPattern(a
, TRUE
));
1693 void UnicodeSetTest::TestSurrogate() {
1694 const char* DATA
[] = {
1695 // These should all behave identically
1696 "[abc\\uD800\\uDC00]",
1697 // "[abc\uD800\uDC00]", // Can't do this on C -- only Java
1701 for (int i
=0; DATA
[i
] != 0; ++i
) {
1702 UErrorCode ec
= U_ZERO_ERROR
;
1703 logln((UnicodeString
)"Test pattern " + i
+ " :" + UnicodeString(DATA
[i
], -1, US_INV
));
1704 UnicodeString str
= UnicodeString(DATA
[i
], -1, US_INV
);
1705 UnicodeSet
set(str
, ec
);
1706 if (U_FAILURE(ec
)) {
1707 errln("FAIL: UnicodeSet constructor");
1710 expectContainment(set
,
1711 CharsToUnicodeString("abc\\U00010000"),
1712 CharsToUnicodeString("\\uD800;\\uDC00")); // split apart surrogate-pair
1713 if (set
.size() != 4) {
1714 errln((UnicodeString
)"FAIL: " + UnicodeString(DATA
[i
], -1, US_INV
) + ".size() == " +
1715 set
.size() + ", expected 4");
1720 void UnicodeSetTest::TestExhaustive() {
1721 // exhaustive tests. Simulate UnicodeSets with integers.
1722 // That gives us very solid tests (except for large memory tests).
1724 int32_t limit
= 128;
1726 UnicodeSet x
, y
, z
, aa
;
1728 for (int32_t i
= 0; i
< limit
; ++i
) {
1730 logln((UnicodeString
)"Testing " + i
+ ", " + x
);
1731 _testComplement(i
, x
, y
);
1733 // AS LONG AS WE ARE HERE, check roundtrip
1734 checkRoundTrip(bitsToSet(i
, aa
));
1736 for (int32_t j
= 0; j
< limit
; ++j
) {
1737 _testAdd(i
,j
, x
,y
,z
);
1738 _testXor(i
,j
, x
,y
,z
);
1739 _testRetain(i
,j
, x
,y
,z
);
1740 _testRemove(i
,j
, x
,y
,z
);
1745 void UnicodeSetTest::_testComplement(int32_t a
, UnicodeSet
& x
, UnicodeSet
& z
) {
1749 int32_t c
= setToBits(z
);
1751 errln((UnicodeString
)"FAILED: add: ~" + x
+ " != " + z
);
1752 errln((UnicodeString
)"FAILED: add: ~" + a
+ " != " + c
);
1754 checkCanonicalRep(z
, (UnicodeString
)"complement " + a
);
1757 void UnicodeSetTest::_testAdd(int32_t a
, int32_t b
, UnicodeSet
& x
, UnicodeSet
& y
, UnicodeSet
& z
) {
1762 int32_t c
= setToBits(z
);
1764 errln((UnicodeString
)"FAILED: add: " + x
+ " | " + y
+ " != " + z
);
1765 errln((UnicodeString
)"FAILED: add: " + a
+ " | " + b
+ " != " + c
);
1767 checkCanonicalRep(z
, (UnicodeString
)"add " + a
+ "," + b
);
1770 void UnicodeSetTest::_testRetain(int32_t a
, int32_t b
, UnicodeSet
& x
, UnicodeSet
& y
, UnicodeSet
& z
) {
1775 int32_t c
= setToBits(z
);
1777 errln((UnicodeString
)"FAILED: retain: " + x
+ " & " + y
+ " != " + z
);
1778 errln((UnicodeString
)"FAILED: retain: " + a
+ " & " + b
+ " != " + c
);
1780 checkCanonicalRep(z
, (UnicodeString
)"retain " + a
+ "," + b
);
1783 void UnicodeSetTest::_testRemove(int32_t a
, int32_t b
, UnicodeSet
& x
, UnicodeSet
& y
, UnicodeSet
& z
) {
1788 int32_t c
= setToBits(z
);
1789 if (c
!= (a
&~ b
)) {
1790 errln((UnicodeString
)"FAILED: remove: " + x
+ " &~ " + y
+ " != " + z
);
1791 errln((UnicodeString
)"FAILED: remove: " + a
+ " &~ " + b
+ " != " + c
);
1793 checkCanonicalRep(z
, (UnicodeString
)"remove " + a
+ "," + b
);
1796 void UnicodeSetTest::_testXor(int32_t a
, int32_t b
, UnicodeSet
& x
, UnicodeSet
& y
, UnicodeSet
& z
) {
1801 int32_t c
= setToBits(z
);
1803 errln((UnicodeString
)"FAILED: complement: " + x
+ " ^ " + y
+ " != " + z
);
1804 errln((UnicodeString
)"FAILED: complement: " + a
+ " ^ " + b
+ " != " + c
);
1806 checkCanonicalRep(z
, (UnicodeString
)"complement " + a
+ "," + b
);
1810 * Check that ranges are monotonically increasing and non-
1813 void UnicodeSetTest::checkCanonicalRep(const UnicodeSet
& set
, const UnicodeString
& msg
) {
1814 int32_t n
= set
.getRangeCount();
1816 errln((UnicodeString
)"FAIL result of " + msg
+
1817 ": range count should be >= 0 but is " +
1818 n
/*+ " for " + set.toPattern())*/);
1822 for (int32_t i
=0; i
<n
; ++i
) {
1823 UChar32 start
= set
.getRangeStart(i
);
1824 UChar32 end
= set
.getRangeEnd(i
);
1826 errln((UnicodeString
)"FAIL result of " + msg
+
1827 ": range " + (i
+1) +
1828 " start > end: " + (int)start
+ ", " + (int)end
+
1831 if (i
> 0 && start
<= last
) {
1832 errln((UnicodeString
)"FAIL result of " + msg
+
1833 ": range " + (i
+1) +
1834 " overlaps previous range: " + (int)start
+ ", " + (int)end
+
1842 * Convert a bitmask to a UnicodeSet.
1844 UnicodeSet
& UnicodeSetTest::bitsToSet(int32_t a
, UnicodeSet
& result
) {
1846 for (UChar32 i
= 0; i
< 32; ++i
) {
1847 if ((a
& (1<<i
)) != 0) {
1855 * Convert a UnicodeSet to a bitmask. Only the characters
1856 * U+0000 to U+0020 are represented in the bitmask.
1858 int32_t UnicodeSetTest::setToBits(const UnicodeSet
& x
) {
1860 for (int32_t i
= 0; i
< 32; ++i
) {
1861 if (x
.contains((UChar32
)i
)) {
1869 * Return the representation of an inversion list based UnicodeSet
1870 * as a pairs list. Ranges are listed in ascending Unicode order.
1871 * For example, the set [a-zA-M3] is represented as "33AMaz".
1873 UnicodeString
UnicodeSetTest::getPairs(const UnicodeSet
& set
) {
1874 UnicodeString pairs
;
1875 for (int32_t i
=0; i
<set
.getRangeCount(); ++i
) {
1876 UChar32 start
= set
.getRangeStart(i
);
1877 UChar32 end
= set
.getRangeEnd(i
);
1880 i
= set
.getRangeCount(); // Should be unnecessary
1882 pairs
.append((UChar
)start
).append((UChar
)end
);
1888 * Basic consistency check for a few items.
1889 * That the iterator works, and that we can create a pattern and
1890 * get the same thing back
1892 void UnicodeSetTest::checkRoundTrip(const UnicodeSet
& s
) {
1893 UErrorCode ec
= U_ZERO_ERROR
;
1896 checkEqual(s
, t
, "copy ct");
1899 checkEqual(s
, t
, "operator=");
1901 copyWithIterator(t
, s
, FALSE
);
1902 checkEqual(s
, t
, "iterator roundtrip");
1904 copyWithIterator(t
, s
, TRUE
); // try range
1905 checkEqual(s
, t
, "iterator roundtrip");
1907 UnicodeString pat
; s
.toPattern(pat
, FALSE
);
1908 t
.applyPattern(pat
, ec
);
1909 if (U_FAILURE(ec
)) {
1910 errln("FAIL: applyPattern");
1913 checkEqual(s
, t
, "toPattern(false)");
1916 s
.toPattern(pat
, TRUE
);
1917 t
.applyPattern(pat
, ec
);
1918 if (U_FAILURE(ec
)) {
1919 errln("FAIL: applyPattern");
1922 checkEqual(s
, t
, "toPattern(true)");
1926 void UnicodeSetTest::copyWithIterator(UnicodeSet
& t
, const UnicodeSet
& s
, UBool withRange
) {
1928 UnicodeSetIterator
it(s
);
1930 while (it
.nextRange()) {
1931 if (it
.isString()) {
1932 t
.add(it
.getString());
1934 t
.add(it
.getCodepoint(), it
.getCodepointEnd());
1939 if (it
.isString()) {
1940 t
.add(it
.getString());
1942 t
.add(it
.getCodepoint());
1948 UBool
UnicodeSetTest::checkEqual(const UnicodeSet
& s
, const UnicodeSet
& t
, const char* message
) {
1949 UnicodeString source
; s
.toPattern(source
, TRUE
);
1950 UnicodeString result
; t
.toPattern(result
, TRUE
);
1952 errln((UnicodeString
)"FAIL: " + message
1953 + "; source = " + source
1954 + "; result = " + result
1958 logln((UnicodeString
)"Ok: " + message
1959 + "; source = " + source
1960 + "; result = " + result
1967 UnicodeSetTest::expectContainment(const UnicodeString
& pat
,
1968 const UnicodeString
& charsIn
,
1969 const UnicodeString
& charsOut
) {
1970 UErrorCode ec
= U_ZERO_ERROR
;
1971 UnicodeSet
set(pat
, ec
);
1972 if (U_FAILURE(ec
)) {
1973 dataerrln((UnicodeString
)"FAIL: pattern \"" +
1974 pat
+ "\" => " + u_errorName(ec
));
1977 expectContainment(set
, pat
, charsIn
, charsOut
);
1981 UnicodeSetTest::expectContainment(const UnicodeSet
& set
,
1982 const UnicodeString
& charsIn
,
1983 const UnicodeString
& charsOut
) {
1986 expectContainment(set
, pat
, charsIn
, charsOut
);
1990 UnicodeSetTest::expectContainment(const UnicodeSet
& set
,
1991 const UnicodeString
& setName
,
1992 const UnicodeString
& charsIn
,
1993 const UnicodeString
& charsOut
) {
1998 for (i
=0; i
<charsIn
.length(); i
+=U16_LENGTH(c
)) {
1999 c
= charsIn
.char32At(i
);
2000 if (!set
.contains(c
)) {
2004 if (bad
.length() > 0) {
2005 errln((UnicodeString
)"Fail: set " + setName
+ " does not contain " + prettify(bad
) +
2006 ", expected containment of " + prettify(charsIn
));
2008 logln((UnicodeString
)"Ok: set " + setName
+ " contains " + prettify(charsIn
));
2012 for (i
=0; i
<charsOut
.length(); i
+=U16_LENGTH(c
)) {
2013 c
= charsOut
.char32At(i
);
2014 if (set
.contains(c
)) {
2018 if (bad
.length() > 0) {
2019 errln((UnicodeString
)"Fail: set " + setName
+ " contains " + prettify(bad
) +
2020 ", expected non-containment of " + prettify(charsOut
));
2022 logln((UnicodeString
)"Ok: set " + setName
+ " does not contain " + prettify(charsOut
));
2027 UnicodeSetTest::expectPattern(UnicodeSet
& set
,
2028 const UnicodeString
& pattern
,
2029 const UnicodeString
& expectedPairs
){
2030 UErrorCode status
= U_ZERO_ERROR
;
2031 set
.applyPattern(pattern
, status
);
2032 if (U_FAILURE(status
)) {
2033 errln(UnicodeString("FAIL: applyPattern(\"") + pattern
+
2037 if (getPairs(set
) != expectedPairs
) {
2038 errln(UnicodeString("FAIL: applyPattern(\"") + pattern
+
2040 escape(getPairs(set
)) + "\", expected \"" +
2041 escape(expectedPairs
) + "\"");
2043 logln(UnicodeString("Ok: applyPattern(\"") + pattern
+
2045 escape(getPairs(set
)) + "\"");
2048 // the result of calling set.toPattern(), which is the string representation of
2049 // this set(set), is passed to a UnicodeSet constructor, and tested that it
2050 // will produce another set that is equal to this one.
2051 UnicodeString temppattern
;
2052 set
.toPattern(temppattern
);
2053 UnicodeSet
*tempset
=new UnicodeSet(temppattern
, status
);
2054 if (U_FAILURE(status
)) {
2055 errln(UnicodeString("FAIL: applyPattern(\""+ pattern
+ "\").toPattern() => " + temppattern
+ " => invalid pattern"));
2058 if(*tempset
!= set
|| getPairs(*tempset
) != getPairs(set
)){
2059 errln(UnicodeString("FAIL: applyPattern(\""+ pattern
+ "\").toPattern() => " + temppattern
+ " => pairs \""+ escape(getPairs(*tempset
)) + "\", expected pairs \"" +
2060 escape(getPairs(set
)) + "\""));
2062 logln(UnicodeString("Ok: applyPattern(\""+ pattern
+ "\").toPattern() => " + temppattern
+ " => pairs \"" + escape(getPairs(*tempset
)) + "\""));
2070 UnicodeSetTest::expectPairs(const UnicodeSet
& set
, const UnicodeString
& expectedPairs
) {
2071 if (getPairs(set
) != expectedPairs
) {
2072 errln(UnicodeString("FAIL: Expected pair list \"") +
2073 escape(expectedPairs
) + "\", got \"" +
2074 escape(getPairs(set
)) + "\"");
2078 void UnicodeSetTest::expectToPattern(const UnicodeSet
& set
,
2079 const UnicodeString
& expPat
,
2080 const char** expStrings
) {
2082 set
.toPattern(pat
, TRUE
);
2083 if (pat
== expPat
) {
2084 logln((UnicodeString
)"Ok: toPattern() => \"" + pat
+ "\"");
2086 errln((UnicodeString
)"FAIL: toPattern() => \"" + pat
+ "\", expected \"" + expPat
+ "\"");
2089 if (expStrings
== NULL
) {
2093 for (int32_t i
=0; expStrings
[i
] != NULL
; ++i
) {
2094 if (expStrings
[i
] == NOT
) { // sic; pointer comparison
2098 UnicodeString s
= CharsToUnicodeString(expStrings
[i
]);
2099 UBool contained
= set
.contains(s
);
2100 if (contained
== in
) {
2101 logln((UnicodeString
)"Ok: " + expPat
+
2102 (contained
? " contains {" : " does not contain {") +
2103 escape(expStrings
[i
]) + "}");
2105 errln((UnicodeString
)"FAIL: " + expPat
+
2106 (contained
? " contains {" : " does not contain {") +
2107 escape(expStrings
[i
]) + "}");
2112 static UChar
toHexString(int32_t i
) { return (UChar
)(i
+ (i
< 10 ? 0x30 : (0x41 - 10))); }
2115 UnicodeSetTest::doAssert(UBool condition
, const char *message
)
2118 errln(UnicodeString("ERROR : ") + message
);
2123 UnicodeSetTest::escape(const UnicodeString
& s
) {
2125 for (int32_t i
=0; i
<s
.length(); )
2127 UChar32 c
= s
.char32At(i
);
2128 if (0x0020 <= c
&& c
<= 0x007F) {
2132 buf
+= (UChar
)0x5c; buf
+= (UChar
)0x75;
2134 buf
+= (UChar
)0x5c; buf
+= (UChar
)0x55;
2135 buf
+= toHexString((c
& 0xF0000000) >> 28);
2136 buf
+= toHexString((c
& 0x0F000000) >> 24);
2137 buf
+= toHexString((c
& 0x00F00000) >> 20);
2138 buf
+= toHexString((c
& 0x000F0000) >> 16);
2140 buf
+= toHexString((c
& 0xF000) >> 12);
2141 buf
+= toHexString((c
& 0x0F00) >> 8);
2142 buf
+= toHexString((c
& 0x00F0) >> 4);
2143 buf
+= toHexString(c
& 0x000F);
2150 void UnicodeSetTest::TestFreezable() {
2151 UErrorCode errorCode
=U_ZERO_ERROR
;
2152 UnicodeString idPattern
=UNICODE_STRING("[:ID_Continue:]", 15);
2153 UnicodeSet
idSet(idPattern
, errorCode
);
2154 if(U_FAILURE(errorCode
)) {
2155 dataerrln("FAIL: unable to create UnicodeSet([:ID_Continue:]) - %s", u_errorName(errorCode
));
2159 UnicodeString wsPattern
=UNICODE_STRING("[:White_Space:]", 15);
2160 UnicodeSet
wsSet(wsPattern
, errorCode
);
2161 if(U_FAILURE(errorCode
)) {
2162 dataerrln("FAIL: unable to create UnicodeSet([:White_Space:]) - %s", u_errorName(errorCode
));
2166 idSet
.add(idPattern
);
2167 UnicodeSet
frozen(idSet
);
2170 if(idSet
.isFrozen() || !frozen
.isFrozen()) {
2171 errln("FAIL: isFrozen() is wrong");
2173 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2174 errln("FAIL: a copy-constructed frozen set differs from its original");
2178 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2179 errln("FAIL: a frozen set was modified by operator=");
2182 UnicodeSet
frozen2(frozen
);
2183 if(frozen2
!=frozen
|| frozen2
!=idSet
) {
2184 errln("FAIL: a copied frozen set differs from its frozen original");
2186 if(!frozen2
.isFrozen()) {
2187 errln("FAIL: copy-constructing a frozen set results in a thawed one");
2189 UnicodeSet
frozen3(5, 55); // Set to some values to really test assignment below, not copy construction.
2190 if(frozen3
.contains(0, 4) || !frozen3
.contains(5, 55) || frozen3
.contains(56, 0x10ffff)) {
2191 errln("FAIL: UnicodeSet(5, 55) failed");
2194 if(!frozen3
.isFrozen()) {
2195 errln("FAIL: copying a frozen set results in a thawed one");
2198 UnicodeSet
*cloned
=(UnicodeSet
*)frozen
.clone();
2199 if(!cloned
->isFrozen() || *cloned
!=frozen
|| cloned
->containsSome(0xd802, 0xd805)) {
2200 errln("FAIL: clone() failed");
2202 cloned
->add(0xd802, 0xd805);
2203 if(cloned
->containsSome(0xd802, 0xd805)) {
2204 errln("FAIL: unable to modify clone");
2208 UnicodeSet
*thawed
=(UnicodeSet
*)frozen
.cloneAsThawed();
2209 if(thawed
->isFrozen() || *thawed
!=frozen
|| thawed
->containsSome(0xd802, 0xd805)) {
2210 errln("FAIL: cloneAsThawed() failed");
2212 thawed
->add(0xd802, 0xd805);
2213 if(!thawed
->contains(0xd802, 0xd805)) {
2214 errln("FAIL: unable to modify thawed clone");
2219 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2220 errln("FAIL: UnicodeSet::set() modified a frozen set");
2224 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2225 errln("FAIL: UnicodeSet::clear() modified a frozen set");
2228 frozen
.closeOver(USET_CASE_INSENSITIVE
);
2229 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2230 errln("FAIL: UnicodeSet::closeOver() modified a frozen set");
2234 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2235 errln("FAIL: UnicodeSet::compact() modified a frozen set");
2240 applyPattern(wsPattern
, errorCode
).
2241 applyPattern(wsPattern
, USET_IGNORE_SPACE
, NULL
, errorCode
).
2242 applyPattern(wsPattern
, pos
, USET_IGNORE_SPACE
, NULL
, errorCode
).
2243 applyIntPropertyValue(UCHAR_CANONICAL_COMBINING_CLASS
, 230, errorCode
).
2244 applyPropertyAlias(UNICODE_STRING_SIMPLE("Assigned"), UnicodeString(), errorCode
);
2245 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2246 errln("FAIL: UnicodeSet::applyXYZ() modified a frozen set");
2251 add(0xd802, 0xd805).
2255 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2256 errln("FAIL: UnicodeSet::addXYZ() modified a frozen set");
2262 retainAll(wsPattern
).
2264 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2265 errln("FAIL: UnicodeSet::retainXYZ() modified a frozen set");
2272 removeAll(idPattern
).
2274 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2275 errln("FAIL: UnicodeSet::removeXYZ() modified a frozen set");
2281 complement(0x64, 0x69).
2282 complement(idPattern
).
2283 complementAll(idPattern
).
2284 complementAll(idSet
);
2285 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2286 errln("FAIL: UnicodeSet::complementXYZ() modified a frozen set");
2290 // Test span() etc. -------------------------------------------------------- ***
2292 // Append the UTF-8 version of the string to t and return the appended UTF-8 length.
2294 appendUTF8(const UChar
*s
, int32_t length
, char *t
, int32_t capacity
) {
2295 UErrorCode errorCode
=U_ZERO_ERROR
;
2297 u_strToUTF8(t
, capacity
, &length8
, s
, length
, &errorCode
);
2298 if(U_SUCCESS(errorCode
)) {
2301 // The string contains an unpaired surrogate.
2302 // Ignore this string.
2307 class UnicodeSetWithStringsIterator
;
2309 // Make the strings in a UnicodeSet easily accessible.
2310 class UnicodeSetWithStrings
{
2312 UnicodeSetWithStrings(const UnicodeSet
&normalSet
) :
2313 set(normalSet
), stringsLength(0), hasSurrogates(FALSE
) {
2314 int32_t size
=set
.size();
2315 if(size
>0 && set
.charAt(size
-1)<0) {
2316 // If a set's last element is not a code point, then it must contain strings.
2317 // Iterate over the set, skip all code point ranges, and cache the strings.
2318 // Convert them to UTF-8 for spanUTF8().
2319 UnicodeSetIterator
iter(set
);
2320 const UnicodeString
*s
;
2322 int32_t length8
, utf8Count
=0;
2323 while(iter
.nextRange() && stringsLength
<UPRV_LENGTHOF(strings
)) {
2324 if(iter
.isString()) {
2325 // Store the pointer to the set's string element
2326 // which we happen to know is a stable pointer.
2327 strings
[stringsLength
]=s
=&iter
.getString();
2329 utf8Lengths
[stringsLength
]=length8
=
2330 appendUTF8(s
->getBuffer(), s
->length(),
2331 s8
, (int32_t)(sizeof(utf8
)-utf8Count
));
2333 hasSurrogates
=TRUE
; // Contains unpaired surrogates.
2342 const UnicodeSet
&getSet() const {
2346 UBool
hasStrings() const {
2347 return (UBool
)(stringsLength
>0);
2350 UBool
hasStringsWithSurrogates() const {
2351 return hasSurrogates
;
2355 friend class UnicodeSetWithStringsIterator
;
2357 const UnicodeSet
&set
;
2359 const UnicodeString
*strings
[20];
2360 int32_t stringsLength
;
2361 UBool hasSurrogates
;
2364 int32_t utf8Lengths
[20];
2367 class UnicodeSetWithStringsIterator
{
2369 UnicodeSetWithStringsIterator(const UnicodeSetWithStrings
&set
) :
2370 fSet(set
), nextStringIndex(0), nextUTF8Start(0) {
2374 nextStringIndex
=nextUTF8Start
=0;
2377 const UnicodeString
*nextString() {
2378 if(nextStringIndex
<fSet
.stringsLength
) {
2379 return fSet
.strings
[nextStringIndex
++];
2385 // Do not mix with calls to nextString().
2386 const char *nextUTF8(int32_t &length
) {
2387 if(nextStringIndex
<fSet
.stringsLength
) {
2388 const char *s8
=fSet
.utf8
+nextUTF8Start
;
2389 nextUTF8Start
+=length
=fSet
.utf8Lengths
[nextStringIndex
++];
2398 const UnicodeSetWithStrings
&fSet
;
2399 int32_t nextStringIndex
;
2400 int32_t nextUTF8Start
;
2403 // Compare 16-bit Unicode strings (which may be malformed UTF-16)
2404 // at code point boundaries.
2405 // That is, each edge of a match must not be in the middle of a surrogate pair.
2407 matches16CPB(const UChar
*s
, int32_t start
, int32_t limit
, const UnicodeString
&t
) {
2410 int32_t length
=t
.length();
2411 return 0==t
.compare(s
, length
) &&
2412 !(0<start
&& U16_IS_LEAD(s
[-1]) && U16_IS_TRAIL(s
[0])) &&
2413 !(length
<limit
&& U16_IS_LEAD(s
[length
-1]) && U16_IS_TRAIL(s
[length
]));
2416 // Implement span() with contains() for comparison.
2417 static int32_t containsSpanUTF16(const UnicodeSetWithStrings
&set
, const UChar
*s
, int32_t length
,
2418 USetSpanCondition spanCondition
) {
2419 const UnicodeSet
&realSet(set
.getSet());
2420 if(!set
.hasStrings()) {
2421 if(spanCondition
!=USET_SPAN_NOT_CONTAINED
) {
2422 spanCondition
=USET_SPAN_CONTAINED
; // Pin to 0/1 values.
2426 int32_t start
=0, prev
;
2427 while((prev
=start
)<length
) {
2428 U16_NEXT(s
, start
, length
, c
);
2429 if(realSet
.contains(c
)!=spanCondition
) {
2434 } else if(spanCondition
==USET_SPAN_NOT_CONTAINED
) {
2435 UnicodeSetWithStringsIterator
iter(set
);
2437 int32_t start
, next
;
2438 for(start
=next
=0; start
<length
;) {
2439 U16_NEXT(s
, next
, length
, c
);
2440 if(realSet
.contains(c
)) {
2443 const UnicodeString
*str
;
2445 while((str
=iter
.nextString())!=NULL
) {
2446 if(str
->length()<=(length
-start
) && matches16CPB(s
, start
, length
, *str
)) {
2447 // spanNeedsStrings=TRUE;
2454 } else /* USET_SPAN_CONTAINED or USET_SPAN_SIMPLE */ {
2455 UnicodeSetWithStringsIterator
iter(set
);
2457 int32_t start
, next
, maxSpanLimit
=0;
2458 for(start
=next
=0; start
<length
;) {
2459 U16_NEXT(s
, next
, length
, c
);
2460 if(!realSet
.contains(c
)) {
2461 next
=start
; // Do not span this single, not-contained code point.
2463 const UnicodeString
*str
;
2465 while((str
=iter
.nextString())!=NULL
) {
2466 if(str
->length()<=(length
-start
) && matches16CPB(s
, start
, length
, *str
)) {
2467 // spanNeedsStrings=TRUE;
2468 int32_t matchLimit
=start
+str
->length();
2469 if(matchLimit
==length
) {
2472 if(spanCondition
==USET_SPAN_CONTAINED
) {
2473 // Iterate for the shortest match at each position.
2474 // Recurse for each but the shortest match.
2476 next
=matchLimit
; // First match from start.
2478 if(matchLimit
<next
) {
2479 // Remember shortest match from start for iteration.
2484 // Recurse for non-shortest match from start.
2485 int32_t spanLength
=containsSpanUTF16(set
, s
+matchLimit
, length
-matchLimit
,
2486 USET_SPAN_CONTAINED
);
2487 if((matchLimit
+spanLength
)>maxSpanLimit
) {
2488 maxSpanLimit
=matchLimit
+spanLength
;
2489 if(maxSpanLimit
==length
) {
2494 } else /* spanCondition==USET_SPAN_SIMPLE */ {
2495 if(matchLimit
>next
) {
2496 // Remember longest match from start.
2503 break; // No match from start.
2507 if(start
>maxSpanLimit
) {
2510 return maxSpanLimit
;
2515 static int32_t containsSpanBackUTF16(const UnicodeSetWithStrings
&set
, const UChar
*s
, int32_t length
,
2516 USetSpanCondition spanCondition
) {
2520 const UnicodeSet
&realSet(set
.getSet());
2521 if(!set
.hasStrings()) {
2522 if(spanCondition
!=USET_SPAN_NOT_CONTAINED
) {
2523 spanCondition
=USET_SPAN_CONTAINED
; // Pin to 0/1 values.
2527 int32_t prev
=length
;
2529 U16_PREV(s
, 0, length
, c
);
2530 if(realSet
.contains(c
)!=spanCondition
) {
2533 } while((prev
=length
)>0);
2535 } else if(spanCondition
==USET_SPAN_NOT_CONTAINED
) {
2536 UnicodeSetWithStringsIterator
iter(set
);
2538 int32_t prev
=length
, length0
=length
;
2540 U16_PREV(s
, 0, length
, c
);
2541 if(realSet
.contains(c
)) {
2544 const UnicodeString
*str
;
2546 while((str
=iter
.nextString())!=NULL
) {
2547 if(str
->length()<=prev
&& matches16CPB(s
, prev
-str
->length(), length0
, *str
)) {
2548 // spanNeedsStrings=TRUE;
2552 } while((prev
=length
)>0);
2554 } else /* USET_SPAN_CONTAINED or USET_SPAN_SIMPLE */ {
2555 UnicodeSetWithStringsIterator
iter(set
);
2557 int32_t prev
=length
, minSpanStart
=length
, length0
=length
;
2559 U16_PREV(s
, 0, length
, c
);
2560 if(!realSet
.contains(c
)) {
2561 length
=prev
; // Do not span this single, not-contained code point.
2563 const UnicodeString
*str
;
2565 while((str
=iter
.nextString())!=NULL
) {
2566 if(str
->length()<=prev
&& matches16CPB(s
, prev
-str
->length(), length0
, *str
)) {
2567 // spanNeedsStrings=TRUE;
2568 int32_t matchStart
=prev
-str
->length();
2572 if(spanCondition
==USET_SPAN_CONTAINED
) {
2573 // Iterate for the shortest match at each position.
2574 // Recurse for each but the shortest match.
2576 length
=matchStart
; // First match from prev.
2578 if(matchStart
>length
) {
2579 // Remember shortest match from prev for iteration.
2580 int32_t temp
=length
;
2584 // Recurse for non-shortest match from prev.
2585 int32_t spanStart
=containsSpanBackUTF16(set
, s
, matchStart
,
2586 USET_SPAN_CONTAINED
);
2587 if(spanStart
<minSpanStart
) {
2588 minSpanStart
=spanStart
;
2589 if(minSpanStart
==0) {
2594 } else /* spanCondition==USET_SPAN_SIMPLE */ {
2595 if(matchStart
<length
) {
2596 // Remember longest match from prev.
2603 break; // No match from prev.
2605 } while((prev
=length
)>0);
2606 if(prev
<minSpanStart
) {
2609 return minSpanStart
;
2614 static int32_t containsSpanUTF8(const UnicodeSetWithStrings
&set
, const char *s
, int32_t length
,
2615 USetSpanCondition spanCondition
) {
2616 const UnicodeSet
&realSet(set
.getSet());
2617 if(!set
.hasStrings()) {
2618 if(spanCondition
!=USET_SPAN_NOT_CONTAINED
) {
2619 spanCondition
=USET_SPAN_CONTAINED
; // Pin to 0/1 values.
2623 int32_t start
=0, prev
;
2624 while((prev
=start
)<length
) {
2625 U8_NEXT_OR_FFFD(s
, start
, length
, c
);
2626 if(realSet
.contains(c
)!=spanCondition
) {
2631 } else if(spanCondition
==USET_SPAN_NOT_CONTAINED
) {
2632 UnicodeSetWithStringsIterator
iter(set
);
2634 int32_t start
, next
;
2635 for(start
=next
=0; start
<length
;) {
2636 U8_NEXT_OR_FFFD(s
, next
, length
, c
);
2637 if(realSet
.contains(c
)) {
2643 while((s8
=iter
.nextUTF8(length8
))!=NULL
) {
2644 if(length8
!=0 && length8
<=(length
-start
) && 0==memcmp(s
+start
, s8
, length8
)) {
2645 // spanNeedsStrings=TRUE;
2652 } else /* USET_SPAN_CONTAINED or USET_SPAN_SIMPLE */ {
2653 UnicodeSetWithStringsIterator
iter(set
);
2655 int32_t start
, next
, maxSpanLimit
=0;
2656 for(start
=next
=0; start
<length
;) {
2657 U8_NEXT_OR_FFFD(s
, next
, length
, c
);
2658 if(!realSet
.contains(c
)) {
2659 next
=start
; // Do not span this single, not-contained code point.
2664 while((s8
=iter
.nextUTF8(length8
))!=NULL
) {
2665 if(length8
!=0 && length8
<=(length
-start
) && 0==memcmp(s
+start
, s8
, length8
)) {
2666 // spanNeedsStrings=TRUE;
2667 int32_t matchLimit
=start
+length8
;
2668 if(matchLimit
==length
) {
2671 if(spanCondition
==USET_SPAN_CONTAINED
) {
2672 // Iterate for the shortest match at each position.
2673 // Recurse for each but the shortest match.
2675 next
=matchLimit
; // First match from start.
2677 if(matchLimit
<next
) {
2678 // Remember shortest match from start for iteration.
2683 // Recurse for non-shortest match from start.
2684 int32_t spanLength
=containsSpanUTF8(set
, s
+matchLimit
, length
-matchLimit
,
2685 USET_SPAN_CONTAINED
);
2686 if((matchLimit
+spanLength
)>maxSpanLimit
) {
2687 maxSpanLimit
=matchLimit
+spanLength
;
2688 if(maxSpanLimit
==length
) {
2693 } else /* spanCondition==USET_SPAN_SIMPLE */ {
2694 if(matchLimit
>next
) {
2695 // Remember longest match from start.
2702 break; // No match from start.
2706 if(start
>maxSpanLimit
) {
2709 return maxSpanLimit
;
2714 static int32_t containsSpanBackUTF8(const UnicodeSetWithStrings
&set
, const char *s
, int32_t length
,
2715 USetSpanCondition spanCondition
) {
2719 const UnicodeSet
&realSet(set
.getSet());
2720 if(!set
.hasStrings()) {
2721 if(spanCondition
!=USET_SPAN_NOT_CONTAINED
) {
2722 spanCondition
=USET_SPAN_CONTAINED
; // Pin to 0/1 values.
2726 int32_t prev
=length
;
2728 U8_PREV_OR_FFFD(s
, 0, length
, c
);
2729 if(realSet
.contains(c
)!=spanCondition
) {
2732 } while((prev
=length
)>0);
2734 } else if(spanCondition
==USET_SPAN_NOT_CONTAINED
) {
2735 UnicodeSetWithStringsIterator
iter(set
);
2737 int32_t prev
=length
;
2739 U8_PREV_OR_FFFD(s
, 0, length
, c
);
2740 if(realSet
.contains(c
)) {
2746 while((s8
=iter
.nextUTF8(length8
))!=NULL
) {
2747 if(length8
!=0 && length8
<=prev
&& 0==memcmp(s
+prev
-length8
, s8
, length8
)) {
2748 // spanNeedsStrings=TRUE;
2752 } while((prev
=length
)>0);
2754 } else /* USET_SPAN_CONTAINED or USET_SPAN_SIMPLE */ {
2755 UnicodeSetWithStringsIterator
iter(set
);
2757 int32_t prev
=length
, minSpanStart
=length
;
2759 U8_PREV_OR_FFFD(s
, 0, length
, c
);
2760 if(!realSet
.contains(c
)) {
2761 length
=prev
; // Do not span this single, not-contained code point.
2766 while((s8
=iter
.nextUTF8(length8
))!=NULL
) {
2767 if(length8
!=0 && length8
<=prev
&& 0==memcmp(s
+prev
-length8
, s8
, length8
)) {
2768 // spanNeedsStrings=TRUE;
2769 int32_t matchStart
=prev
-length8
;
2773 if(spanCondition
==USET_SPAN_CONTAINED
) {
2774 // Iterate for the shortest match at each position.
2775 // Recurse for each but the shortest match.
2777 length
=matchStart
; // First match from prev.
2779 if(matchStart
>length
) {
2780 // Remember shortest match from prev for iteration.
2781 int32_t temp
=length
;
2785 // Recurse for non-shortest match from prev.
2786 int32_t spanStart
=containsSpanBackUTF8(set
, s
, matchStart
,
2787 USET_SPAN_CONTAINED
);
2788 if(spanStart
<minSpanStart
) {
2789 minSpanStart
=spanStart
;
2790 if(minSpanStart
==0) {
2795 } else /* spanCondition==USET_SPAN_SIMPLE */ {
2796 if(matchStart
<length
) {
2797 // Remember longest match from prev.
2804 break; // No match from prev.
2806 } while((prev
=length
)>0);
2807 if(prev
<minSpanStart
) {
2810 return minSpanStart
;
2815 // spans to be performed and compared
2829 SPAN_CONTAINED
=0x100,
2831 SPAN_CONDITION
=0x300,
2836 static inline USetSpanCondition
invertSpanCondition(USetSpanCondition spanCondition
, USetSpanCondition contained
) {
2837 return spanCondition
== USET_SPAN_NOT_CONTAINED
? contained
: USET_SPAN_NOT_CONTAINED
;
2840 static inline int32_t slen(const void *s
, UBool isUTF16
) {
2841 return isUTF16
? u_strlen((const UChar
*)s
) : strlen((const char *)s
);
2845 * Count spans on a string with the method according to type and set the span limits.
2846 * The set may be the complement of the original.
2847 * When using spanBack() and comparing with span(), use a span condition for the first spanBack()
2848 * according to the expected number of spans.
2849 * Sets typeName to an empty string if there is no such type.
2850 * Returns -1 if the span option is filtered out.
2852 static int32_t getSpans(const UnicodeSetWithStrings
&set
, UBool isComplement
,
2853 const void *s
, int32_t length
, UBool isUTF16
,
2854 uint32_t whichSpans
,
2855 int type
, const char *&typeName
,
2856 int32_t limits
[], int32_t limitsCapacity
,
2857 int32_t expectCount
) {
2858 const UnicodeSet
&realSet(set
.getSet());
2859 int32_t start
, count
;
2860 USetSpanCondition spanCondition
, firstSpanCondition
, contained
;
2863 if(type
<0 || 7<type
) {
2868 static const char *const typeNames16
[]={
2869 "contains", "contains(LM)",
2871 "containsBack", "containsBack(LM)",
2872 "spanBack", "spanBack(LM)"
2875 static const char *const typeNames8
[]={
2876 "containsUTF8", "containsUTF8(LM)",
2877 "spanUTF8", "spanUTF8(LM)",
2878 "containsBackUTF8", "containsBackUTF8(LM)", // not implemented
2879 "spanBackUTF8", "spanBackUTF8(LM)"
2882 typeName
= isUTF16
? typeNames16
[type
] : typeNames8
[type
];
2884 // filter span options
2887 if((whichSpans
&SPAN_FWD
)==0) {
2893 if((whichSpans
&SPAN_BACK
)==0) {
2899 // use USET_SPAN_CONTAINED
2900 if((whichSpans
&SPAN_CONTAINED
)==0) {
2903 contained
=USET_SPAN_CONTAINED
;
2905 // use USET_SPAN_SIMPLE
2906 if((whichSpans
&SPAN_SIMPLE
)==0) {
2909 contained
=USET_SPAN_SIMPLE
;
2912 // Default first span condition for going forward with an uncomplemented set.
2913 spanCondition
=USET_SPAN_NOT_CONTAINED
;
2915 spanCondition
=invertSpanCondition(spanCondition
, contained
);
2918 // First span condition for span(), used to terminate the spanBack() iteration.
2919 firstSpanCondition
=spanCondition
;
2921 // spanBack(): Its initial span condition is span()'s last span condition,
2922 // which is the opposite of span()'s first span condition
2923 // if we expect an even number of spans.
2924 // (The loop inverts spanCondition (expectCount-1) times
2925 // before the expectCount'th span() call.)
2926 // If we do not compare forward and backward directions, then we do not have an
2927 // expectCount and just start with firstSpanCondition.
2928 if(!isForward
&& (whichSpans
&SPAN_FWD
)!=0 && (expectCount
&1)==0) {
2929 spanCondition
=invertSpanCondition(spanCondition
, contained
);
2938 length
=slen(s
, isUTF16
);
2941 start
+= isUTF16
? containsSpanUTF16(set
, (const UChar
*)s
+start
, length
-start
, spanCondition
) :
2942 containsSpanUTF8(set
, (const char *)s
+start
, length
-start
, spanCondition
);
2943 if(count
<limitsCapacity
) {
2944 limits
[count
]=start
;
2950 spanCondition
=invertSpanCondition(spanCondition
, contained
);
2957 start
+= isUTF16
? realSet
.span((const UChar
*)s
+start
, length
>=0 ? length
-start
: length
, spanCondition
) :
2958 realSet
.spanUTF8((const char *)s
+start
, length
>=0 ? length
-start
: length
, spanCondition
);
2959 if(count
<limitsCapacity
) {
2960 limits
[count
]=start
;
2963 if(length
>=0 ? start
>=length
:
2964 isUTF16
? ((const UChar
*)s
)[start
]==0 :
2965 ((const char *)s
)[start
]==0
2969 spanCondition
=invertSpanCondition(spanCondition
, contained
);
2975 length
=slen(s
, isUTF16
);
2979 if(count
<=limitsCapacity
) {
2980 limits
[limitsCapacity
-count
]=length
;
2982 length
= isUTF16
? containsSpanBackUTF16(set
, (const UChar
*)s
, length
, spanCondition
) :
2983 containsSpanBackUTF8(set
, (const char *)s
, length
, spanCondition
);
2984 if(length
==0 && spanCondition
==firstSpanCondition
) {
2987 spanCondition
=invertSpanCondition(spanCondition
, contained
);
2989 if(count
<limitsCapacity
) {
2990 memmove(limits
, limits
+(limitsCapacity
-count
), count
*4);
2997 if(count
<=limitsCapacity
) {
2998 limits
[limitsCapacity
-count
]= length
>=0 ? length
: slen(s
, isUTF16
);
3000 // Note: Length<0 is tested only for the first spanBack().
3001 // If we wanted to keep length<0 for all spanBack()s, we would have to
3002 // temporarily modify the string by placing a NUL where the previous spanBack() stopped.
3003 length
= isUTF16
? realSet
.spanBack((const UChar
*)s
, length
, spanCondition
) :
3004 realSet
.spanBackUTF8((const char *)s
, length
, spanCondition
);
3005 if(length
==0 && spanCondition
==firstSpanCondition
) {
3008 spanCondition
=invertSpanCondition(spanCondition
, contained
);
3010 if(count
<limitsCapacity
) {
3011 memmove(limits
, limits
+(limitsCapacity
-count
), count
*4);
3022 // sets to be tested; odd index=isComplement
3031 static const char *const setNames
[SET_COUNT
]={
3039 * Verify that we get the same results whether we look at text with contains(),
3040 * span() or spanBack(), using unfrozen or frozen versions of the set,
3041 * and using the set or its complement (switching the spanConditions accordingly).
3042 * The latter verifies that
3043 * set.span(spanCondition) == set.complement().span(!spanCondition).
3045 * The expectLimits[] are either provided by the caller (with expectCount>=0)
3046 * or returned to the caller (with an input expectCount<0).
3048 void UnicodeSetTest::testSpan(const UnicodeSetWithStrings
*sets
[4],
3049 const void *s
, int32_t length
, UBool isUTF16
,
3050 uint32_t whichSpans
,
3051 int32_t expectLimits
[], int32_t &expectCount
,
3052 const char *testName
, int32_t index
) {
3053 int32_t limits
[500];
3054 int32_t limitsCount
;
3057 const char *typeName
;
3060 for(i
=0; i
<SET_COUNT
; ++i
) {
3062 // Even-numbered sets are original, uncomplemented sets.
3063 if((whichSpans
&SPAN_SET
)==0) {
3067 // Odd-numbered sets are complemented.
3068 if((whichSpans
&SPAN_COMPLEMENT
)==0) {
3072 for(type
=0;; ++type
) {
3073 limitsCount
=getSpans(*sets
[i
], (UBool
)(i
&1),
3077 limits
, UPRV_LENGTHOF(limits
), expectCount
);
3078 if(typeName
[0]==0) {
3079 break; // All types tried.
3082 continue; // Span option filtered out.
3085 expectCount
=limitsCount
;
3086 if(limitsCount
>UPRV_LENGTHOF(limits
)) {
3087 errln("FAIL: %s[0x%lx].%s.%s span count=%ld > %ld capacity - too many spans",
3088 testName
, (long)index
, setNames
[i
], typeName
, (long)limitsCount
, (long)UPRV_LENGTHOF(limits
));
3091 memcpy(expectLimits
, limits
, limitsCount
*4);
3092 } else if(limitsCount
!=expectCount
) {
3093 errln("FAIL: %s[0x%lx].%s.%s span count=%ld != %ld",
3094 testName
, (long)index
, setNames
[i
], typeName
, (long)limitsCount
, (long)expectCount
);
3096 for(j
=0; j
<limitsCount
; ++j
) {
3097 if(limits
[j
]!=expectLimits
[j
]) {
3098 errln("FAIL: %s[0x%lx].%s.%s span count=%ld limits[%d]=%ld != %ld",
3099 testName
, (long)index
, setNames
[i
], typeName
, (long)limitsCount
,
3100 j
, (long)limits
[j
], (long)expectLimits
[j
]);
3108 // Compare span() with containsAll()/containsNone(),
3109 // but only if we have expectLimits[] from the uncomplemented set.
3110 if(isUTF16
&& (whichSpans
&SPAN_SET
)!=0) {
3111 const UChar
*s16
=(const UChar
*)s
;
3112 UnicodeString string
;
3113 int32_t prev
=0, limit
, length
;
3114 for(i
=0; i
<expectCount
; ++i
) {
3115 limit
=expectLimits
[i
];
3118 string
.setTo(FALSE
, s16
+prev
, length
); // read-only alias
3120 if(!sets
[SLOW
]->getSet().containsAll(string
)) {
3121 errln("FAIL: %s[0x%lx].%s.containsAll(%ld..%ld)==FALSE contradicts span()",
3122 testName
, (long)index
, setNames
[SLOW
], (long)prev
, (long)limit
);
3125 if(!sets
[FAST
]->getSet().containsAll(string
)) {
3126 errln("FAIL: %s[0x%lx].%s.containsAll(%ld..%ld)==FALSE contradicts span()",
3127 testName
, (long)index
, setNames
[FAST
], (long)prev
, (long)limit
);
3131 if(!sets
[SLOW
]->getSet().containsNone(string
)) {
3132 errln("FAIL: %s[0x%lx].%s.containsNone(%ld..%ld)==FALSE contradicts span()",
3133 testName
, (long)index
, setNames
[SLOW
], (long)prev
, (long)limit
);
3136 if(!sets
[FAST
]->getSet().containsNone(string
)) {
3137 errln("FAIL: %s[0x%lx].%s.containsNone(%ld..%ld)==FALSE contradicts span()",
3138 testName
, (long)index
, setNames
[FAST
], (long)prev
, (long)limit
);
3148 // Specifically test either UTF-16 or UTF-8.
3149 void UnicodeSetTest::testSpan(const UnicodeSetWithStrings
*sets
[4],
3150 const void *s
, int32_t length
, UBool isUTF16
,
3151 uint32_t whichSpans
,
3152 const char *testName
, int32_t index
) {
3153 int32_t expectLimits
[500];
3154 int32_t expectCount
=-1;
3155 testSpan(sets
, s
, length
, isUTF16
, whichSpans
, expectLimits
, expectCount
, testName
, index
);
3158 UBool
stringContainsUnpairedSurrogate(const UChar
*s
, int32_t length
) {
3165 if(0xd800<=c
&& c
<0xe000) {
3166 if(c
>=0xdc00 || length
==0 || !U16_IS_TRAIL(c2
=*s
++)) {
3173 while((c
=*s
++)!=0) {
3174 if(0xd800<=c
&& c
<0xe000) {
3175 if(c
>=0xdc00 || !U16_IS_TRAIL(c2
=*s
++)) {
3184 // Test both UTF-16 and UTF-8 versions of span() etc. on the same sets and text,
3185 // unless either UTF is turned off in whichSpans.
3186 // Testing UTF-16 and UTF-8 together requires that surrogate code points
3187 // have the same contains(c) value as U+FFFD.
3188 void UnicodeSetTest::testSpanBothUTFs(const UnicodeSetWithStrings
*sets
[4],
3189 const UChar
*s16
, int32_t length16
,
3190 uint32_t whichSpans
,
3191 const char *testName
, int32_t index
) {
3192 int32_t expectLimits
[500];
3193 int32_t expectCount
;
3195 expectCount
=-1; // Get expectLimits[] from testSpan().
3197 if((whichSpans
&SPAN_UTF16
)!=0) {
3198 testSpan(sets
, s16
, length16
, TRUE
, whichSpans
, expectLimits
, expectCount
, testName
, index
);
3200 if((whichSpans
&SPAN_UTF8
)==0) {
3204 // Convert s16[] and expectLimits[] to UTF-8.
3206 int32_t offsets
[3000];
3208 const UChar
*s16Limit
=s16
+length16
;
3210 char *tLimit
=t
+sizeof(s8
);
3212 UErrorCode errorCode
=U_ZERO_ERROR
;
3214 // Convert with substitution: Turn unpaired surrogates into U+FFFD.
3215 ucnv_fromUnicode(openUTF8Converter(), &t
, tLimit
, &s16
, s16Limit
, o
, TRUE
, &errorCode
);
3216 if(U_FAILURE(errorCode
)) {
3217 errln("FAIL: %s[0x%lx] ucnv_fromUnicode(to UTF-8) fails with %s",
3218 testName
, (long)index
, u_errorName(errorCode
));
3219 ucnv_resetFromUnicode(utf8Cnv
);
3222 int32_t length8
=(int32_t)(t
-(char *)s8
);
3224 // Convert expectLimits[].
3225 int32_t i
, j
, expect
;
3226 for(i
=j
=0; i
<expectCount
; ++i
) {
3227 expect
=expectLimits
[i
];
3228 if(expect
==length16
) {
3229 expectLimits
[i
]=length8
;
3231 while(offsets
[j
]<expect
) {
3238 testSpan(sets
, s8
, length8
, FALSE
, whichSpans
, expectLimits
, expectCount
, testName
, index
);
3241 static UChar32
nextCodePoint(UChar32 c
) {
3242 // Skip some large and boring ranges.
3263 // Verify that all implementations represent the same set.
3264 void UnicodeSetTest::testSpanContents(const UnicodeSetWithStrings
*sets
[4], uint32_t whichSpans
, const char *testName
) {
3265 // contains(U+FFFD) is inconsistent with contains(some surrogates),
3266 // or the set contains strings with unpaired surrogates which don't translate to valid UTF-8:
3267 // Skip the UTF-8 part of the test - if the string contains surrogates -
3268 // because it is likely to produce a different result.
3269 UBool inconsistentSurrogates
=
3270 (!(sets
[0]->getSet().contains(0xfffd) ?
3271 sets
[0]->getSet().contains(0xd800, 0xdfff) :
3272 sets
[0]->getSet().containsNone(0xd800, 0xdfff)) ||
3273 sets
[0]->hasStringsWithSurrogates());
3277 uint32_t localWhichSpans
;
3280 for(first
=c
=0;; c
=nextCodePoint(c
)) {
3281 if(c
>0x10ffff || length
>(UPRV_LENGTHOF(s
)-U16_MAX_LENGTH
)) {
3282 localWhichSpans
=whichSpans
;
3283 if(stringContainsUnpairedSurrogate(s
, length
) && inconsistentSurrogates
) {
3284 localWhichSpans
&=~SPAN_UTF8
;
3286 testSpanBothUTFs(sets
, s
, length
, localWhichSpans
, testName
, first
);
3293 U16_APPEND_UNSAFE(s
, length
, c
);
3297 // Test with a particular, interesting string.
3298 // Specify length and try NUL-termination.
3299 void UnicodeSetTest::testSpanUTF16String(const UnicodeSetWithStrings
*sets
[4], uint32_t whichSpans
, const char *testName
) {
3300 static const UChar s
[]={
3301 0x61, 0x62, 0x20, // Latin, space
3302 0x3b1, 0x3b2, 0x3b3, // Greek
3303 0xd900, // lead surrogate
3304 0x3000, 0x30ab, 0x30ad, // wide space, Katakana
3305 0xdc05, // trail surrogate
3306 0xa0, 0xac00, 0xd7a3, // nbsp, Hangul
3307 0xd900, 0xdc05, // unassigned supplementary
3308 0xd840, 0xdfff, 0xd860, 0xdffe, // Han supplementary
3309 0xd7a4, 0xdc05, 0xd900, 0x2028, // unassigned, surrogates in wrong order, LS
3313 if((whichSpans
&SPAN_UTF16
)==0) {
3316 testSpan(sets
, s
, -1, TRUE
, (whichSpans
&~SPAN_UTF8
), testName
, 0);
3317 testSpan(sets
, s
, UPRV_LENGTHOF(s
)-1, TRUE
, (whichSpans
&~SPAN_UTF8
), testName
, 1);
3320 void UnicodeSetTest::testSpanUTF8String(const UnicodeSetWithStrings
*sets
[4], uint32_t whichSpans
, const char *testName
) {
3321 static const char s
[]={
3324 /* trail byte in lead position */
3329 /* truncated multi-byte sequences */
3341 "\xCE\xB1\xCE\xB2\xCE\xB3" // Greek
3343 /* trail byte in lead position */
3360 "\xE3\x80\x80\xE3\x82\xAB\xE3\x82\xAD" // wide space, Katakana
3362 /* trail byte in lead position */
3373 "\xC2\xA0\xEA\xB0\x80\xED\x9E\xA3" // nbsp, Hangul
3375 /* trail byte in lead position */
3381 "\xF1\x90\x80\x85" // unassigned supplementary
3383 /* trail byte in lead position */
3386 "\xfc\x80\x80\x80\x80"
3388 "\xF0\xA0\x8F\xBF\xF0\xA8\x8F\xBE" // Han supplementary
3390 /* trail byte in lead position */
3393 /* complete sequences but non-shortest forms or out of range etc. */
3399 "\xf8\x80\x80\x80\x80"
3400 "\xfc\x80\x80\x80\x80\x80"
3404 /* trail byte in lead position */
3407 "\xED\x9E\xA4\xE2\x80\xA8" // unassigned, LS, NUL-terminated
3410 if((whichSpans
&SPAN_UTF8
)==0) {
3413 testSpan(sets
, s
, -1, FALSE
, (whichSpans
&~SPAN_UTF16
), testName
, 0);
3414 testSpan(sets
, s
, UPRV_LENGTHOF(s
)-1, FALSE
, (whichSpans
&~SPAN_UTF16
), testName
, 1);
3417 // Take a set of span options and multiply them so that
3418 // each portion only has one of the options a, b and c.
3419 // If b==0, then the set of options is just modified with mask and a.
3420 // If b!=0 and c==0, then the set of options is just modified with mask, a and b.
3422 addAlternative(uint32_t whichSpans
[], int32_t whichSpansCount
,
3423 uint32_t mask
, uint32_t a
, uint32_t b
, uint32_t c
) {
3427 for(i
=0; i
<whichSpansCount
; ++i
) {
3428 s
=whichSpans
[i
]&mask
;
3431 whichSpans
[whichSpansCount
+i
]=s
|b
;
3433 whichSpans
[2*whichSpansCount
+i
]=s
|c
;
3437 return b
==0 ? whichSpansCount
: c
==0 ? 2*whichSpansCount
: 3*whichSpansCount
;
3440 #define _63_a "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3441 #define _64_a "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3442 #define _63_b "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
3443 #define _64_b "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
3445 void UnicodeSetTest::TestSpan() {
3446 // "[...]" is a UnicodeSet pattern.
3447 // "*" performs tests on all Unicode code points and on a selection of
3448 // malformed UTF-8/16 strings.
3449 // "-options" limits the scope of testing for the current set.
3450 // By default, the test verifies that equivalent boundaries are found
3451 // for UTF-16 and UTF-8, going forward and backward,
3452 // alternating USET_SPAN_NOT_CONTAINED with
3453 // either USET_SPAN_CONTAINED or USET_SPAN_SIMPLE.
3454 // Single-character options:
3455 // 8 -- UTF-16 and UTF-8 boundaries may differ.
3456 // Cause: contains(U+FFFD) is inconsistent with contains(some surrogates),
3457 // or the set contains strings with unpaired surrogates
3458 // which do not translate to valid UTF-8.
3459 // c -- set.span() and set.complement().span() boundaries may differ.
3460 // Cause: Set strings are not complemented.
3461 // b -- span() and spanBack() boundaries may differ.
3462 // Cause: Strings in the set overlap, and spanBack(USET_SPAN_CONTAINED)
3463 // and spanBack(USET_SPAN_SIMPLE) are defined to
3464 // match with non-overlapping substrings.
3465 // For example, with a set containing "ab" and "ba",
3466 // span() of "aba" yields boundaries { 0, 2, 3 }
3467 // because the initial "ab" matches from 0 to 2,
3468 // while spanBack() yields boundaries { 0, 1, 3 }
3469 // because the final "ba" matches from 1 to 3.
3470 // l -- USET_SPAN_CONTAINED and USET_SPAN_SIMPLE boundaries may differ.
3471 // Cause: Strings in the set overlap, and a longer match may
3472 // require a sequence including non-longest substrings.
3473 // For example, with a set containing "ab", "abc" and "cd",
3474 // span(contained) of "abcd" spans the entire string
3475 // but span(longest match) only spans the first 3 characters.
3476 // Each "-options" first resets all options and then applies the specified options.
3477 // A "-" without options resets the options.
3478 // The options are also reset for each new set.
3479 // Other strings will be spanned.
3480 static const char *const testdata
[]={
3487 "[\\u0000-\\U0010FFFF]",
3489 "[\\u0000\\u0080\\u0800\\U00010000]",
3491 "[\\u007F\\u07FF\\uFFFF\\U0010FFFF]",
3493 "[[[:ID_Continue:]-[\\u30ab\\u30ad]]{\\u3000\\u30ab}{\\u3000\\u30ab\\u30ad}]",
3496 "[[[:ID_Continue:]-[\\u30ab\\u30ad]]{\\u30ab\\u30ad}{\\u3000\\u30ab\\u30ad}]",
3500 // Overlapping strings cause overlapping attempts to match.
3501 "[x{xy}{xya}{axy}{ax}]",
3504 // More repetitions of "xya" would take too long with the recursive
3505 // reference implementation.
3506 // containsAll()=FALSE
3509 "xyaxyaxyaxya" // set.complement().span(longest match) will stop here.
3510 "xx" // set.complement().span(contained) will stop between the two 'x'es.
3513 "xyaxyaxyaxya" // span() ends here.
3516 // containsAll()=TRUE
3527 "byayaxya", // span() -> { 4, 7, 8 } spanBack() -> { 5, 8 }
3529 "byayaxy", // span() -> { 4, 7 } complement.span() -> { 7 }
3530 "byayax", // span() -> { 4, 6 } complement.span() -> { 6 }
3532 "byaya", // span() -> { 5 }
3533 "byay", // span() -> { 4 }
3534 "bya", // span() -> { 3 }
3536 // span(longest match) will not span the whole string.
3546 // spanBack(longest match) will not span the whole string.
3555 // Test with non-ASCII set strings - test proper handling of surrogate pairs
3556 // and UTF-8 trail bytes.
3557 // Copies of above test sets and strings, but transliterated to have
3558 // different code points with similar trail units.
3559 // Previous: a b c d
3560 // Unicode: 042B 30AB 200AB 204AB
3561 // UTF-16: 042B 30AB D840 DCAB D841 DCAB
3562 // UTF-8: D0 AB E3 82 AB F0 A0 82 AB F0 A0 92 AB
3563 "[\\u042B{\\u042B\\u30AB}{\\u042B\\u30AB\\U000200AB}{\\U000200AB\\U000204AB}]",
3565 "\\u042B\\U000200AB\\U000204AB\\u042B\\u30AB\\U000200AB\\U000204AB\\u042B\\u30AB\\U000200AB\\U000200AB\\U000204AB",
3567 "[\\U000204AB{\\U000200AB\\U000204AB}{\\u30AB\\U000200AB\\U000204AB}{\\u042B\\u30AB}]",
3569 "\\u042B\\u30AB\\u30AB\\U000200AB\\U000204AB\\u042B\\u30AB\\U000200AB\\U000204AB\\u042B\\u30AB\\U000204AB",
3571 // Stress bookkeeping and recursion.
3572 // The following strings are barely doable with the recursive
3573 // reference implementation.
3574 // The not-contained character at the end prevents an early exit from the span().
3578 "bbbbbbbbbbbbbbbbbbbbbbbb-",
3579 // On complement sets, span() and spanBack() get different results
3580 // because b is not in the complement set and there is an odd number of b's
3581 // in the test string.
3583 "bbbbbbbbbbbbbbbbbbbbbbbbb-",
3585 // Test with set strings with an initial or final code point span
3587 "[a{" _64_a _64_a _64_a _64_a
"b}"
3588 "{a" _64_b _64_b _64_b _64_b
"}]",
3590 _64_a _64_a _64_a _63_a
"b",
3591 _64_a _64_a _64_a _64_a
"b",
3592 _64_a _64_a _64_a _64_a
"aaaabbbb",
3593 "a" _64_b _64_b _64_b _63_b
,
3594 "a" _64_b _64_b _64_b _64_b
,
3595 "aaaabbbb" _64_b _64_b _64_b _64_b
,
3597 // Test with strings containing unpaired surrogates.
3598 // They are not representable in UTF-8, and a leading trail surrogate
3599 // and a trailing lead surrogate must not match in the middle of a proper surrogate pair.
3600 // U+20001 == \\uD840\\uDC01
3601 // U+20400 == \\uD841\\uDC00
3602 "[a\\U00020001\\U00020400{ab}{b\\uD840}{\\uDC00a}]",
3604 "aaab\\U00020001ba\\U00020400aba\\uD840ab\\uD840\\U00020000b\\U00020000a\\U00020000\\uDC00a\\uDC00babbb"
3606 uint32_t whichSpans
[96]={ SPAN_ALL
};
3607 int32_t whichSpansCount
=1;
3609 UnicodeSet
*sets
[SET_COUNT
]={ NULL
};
3610 const UnicodeSetWithStrings
*sets_with_str
[SET_COUNT
]={ NULL
};
3612 char testName
[1024];
3613 char *testNameLimit
=testName
;
3616 for(i
=0; i
<UPRV_LENGTHOF(testdata
); ++i
) {
3617 const char *s
=testdata
[i
];
3619 // Create new test sets from this pattern.
3620 for(j
=0; j
<SET_COUNT
; ++j
) {
3621 delete sets_with_str
[j
];
3624 UErrorCode errorCode
=U_ZERO_ERROR
;
3625 sets
[SLOW
]=new UnicodeSet(UnicodeString(s
, -1, US_INV
).unescape(), errorCode
);
3626 if(U_FAILURE(errorCode
)) {
3627 dataerrln("FAIL: Unable to create UnicodeSet(%s) - %s", s
, u_errorName(errorCode
));
3630 sets
[SLOW_NOT
]=new UnicodeSet(*sets
[SLOW
]);
3631 sets
[SLOW_NOT
]->complement();
3632 // Intermediate set: Test cloning of a frozen set.
3633 UnicodeSet
*fast
=new UnicodeSet(*sets
[SLOW
]);
3635 sets
[FAST
]=(UnicodeSet
*)fast
->clone();
3637 UnicodeSet
*fastNot
=new UnicodeSet(*sets
[SLOW_NOT
]);
3639 sets
[FAST_NOT
]=(UnicodeSet
*)fastNot
->clone();
3642 for(j
=0; j
<SET_COUNT
; ++j
) {
3643 sets_with_str
[j
]=new UnicodeSetWithStrings(*sets
[j
]);
3646 strcpy(testName
, s
);
3647 testNameLimit
=strchr(testName
, 0);
3648 *testNameLimit
++=':';
3651 whichSpans
[0]=SPAN_ALL
;
3653 } else if(s
[0]=='-') {
3654 whichSpans
[0]=SPAN_ALL
;
3660 whichSpansCount
=addAlternative(whichSpans
, whichSpansCount
,
3667 whichSpansCount
=addAlternative(whichSpans
, whichSpansCount
,
3674 // test USET_SPAN_CONTAINED FWD & BACK, and separately
3675 // USET_SPAN_SIMPLE only FWD, and separately
3676 // USET_SPAN_SIMPLE only BACK
3677 whichSpansCount
=addAlternative(whichSpans
, whichSpansCount
,
3678 ~(SPAN_DIRS
|SPAN_CONDITION
),
3679 SPAN_DIRS
|SPAN_CONTAINED
,
3680 SPAN_FWD
|SPAN_SIMPLE
,
3681 SPAN_BACK
|SPAN_SIMPLE
);
3684 whichSpansCount
=addAlternative(whichSpans
, whichSpansCount
,
3691 errln("FAIL: unrecognized span set option in \"%s\"", testdata
[i
]);
3695 } else if(0==strcmp(s
, "*")) {
3696 strcpy(testNameLimit
, "bad_string");
3697 for(j
=0; j
<whichSpansCount
; ++j
) {
3698 if(whichSpansCount
>1) {
3699 sprintf(testNameLimit
+10 /* strlen("bad_string") */,
3703 testSpanUTF16String(sets_with_str
, whichSpans
[j
], testName
);
3704 testSpanUTF8String(sets_with_str
, whichSpans
[j
], testName
);
3707 strcpy(testNameLimit
, "contents");
3708 for(j
=0; j
<whichSpansCount
; ++j
) {
3709 if(whichSpansCount
>1) {
3710 sprintf(testNameLimit
+8 /* strlen("contents") */,
3714 testSpanContents(sets_with_str
, whichSpans
[j
], testName
);
3717 UnicodeString string
=UnicodeString(s
, -1, US_INV
).unescape();
3718 strcpy(testNameLimit
, "test_string");
3719 for(j
=0; j
<whichSpansCount
; ++j
) {
3720 if(whichSpansCount
>1) {
3721 sprintf(testNameLimit
+11 /* strlen("test_string") */,
3725 testSpanBothUTFs(sets_with_str
, string
.getBuffer(), string
.length(), whichSpans
[j
], testName
, i
);
3729 for(j
=0; j
<SET_COUNT
; ++j
) {
3730 delete sets_with_str
[j
];
3735 // Test select patterns and strings, and test USET_SPAN_SIMPLE.
3736 void UnicodeSetTest::TestStringSpan() {
3737 static const char *pattern
="[x{xy}{xya}{axy}{ax}]";
3738 static const char *const string
=
3740 "xyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxya"
3742 "xyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxya"
3744 "xyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxy"
3747 UErrorCode errorCode
=U_ZERO_ERROR
;
3748 UnicodeString pattern16
=UnicodeString(pattern
, -1, US_INV
);
3749 UnicodeSet
set(pattern16
, errorCode
);
3750 if(U_FAILURE(errorCode
)) {
3751 errln("FAIL: Unable to create UnicodeSet(%s) - %s", pattern
, u_errorName(errorCode
));
3755 UnicodeString string16
=UnicodeString(string
, -1, US_INV
).unescape();
3757 if(set
.containsAll(string16
)) {
3758 errln("FAIL: UnicodeSet(%s).containsAll(%s) should be FALSE", pattern
, string
);
3761 // Remove trailing "aaaa".
3762 string16
.truncate(string16
.length()-4);
3763 if(!set
.containsAll(string16
)) {
3764 errln("FAIL: UnicodeSet(%s).containsAll(%s[:-4]) should be TRUE", pattern
, string
);
3767 string16
=UNICODE_STRING_SIMPLE("byayaxya");
3768 const UChar
*s16
=string16
.getBuffer();
3769 int32_t length16
=string16
.length();
3770 (void)length16
; // Suppress set but not used warning.
3771 if( set
.span(s16
, 8, USET_SPAN_NOT_CONTAINED
)!=4 ||
3772 set
.span(s16
, 7, USET_SPAN_NOT_CONTAINED
)!=4 ||
3773 set
.span(s16
, 6, USET_SPAN_NOT_CONTAINED
)!=4 ||
3774 set
.span(s16
, 5, USET_SPAN_NOT_CONTAINED
)!=5 ||
3775 set
.span(s16
, 4, USET_SPAN_NOT_CONTAINED
)!=4 ||
3776 set
.span(s16
, 3, USET_SPAN_NOT_CONTAINED
)!=3
3778 errln("FAIL: UnicodeSet(%s).span(while not) returns the wrong value", pattern
);
3781 pattern
="[a{ab}{abc}{cd}]";
3782 pattern16
=UnicodeString(pattern
, -1, US_INV
);
3783 set
.applyPattern(pattern16
, errorCode
);
3784 if(U_FAILURE(errorCode
)) {
3785 errln("FAIL: Unable to create UnicodeSet(%s) - %s", pattern
, u_errorName(errorCode
));
3788 string16
=UNICODE_STRING_SIMPLE("acdabcdabccd");
3789 s16
=string16
.getBuffer();
3790 length16
=string16
.length();
3791 if( set
.span(s16
, 12, USET_SPAN_CONTAINED
)!=12 ||
3792 set
.span(s16
, 12, USET_SPAN_SIMPLE
)!=6 ||
3793 set
.span(s16
+7, 5, USET_SPAN_SIMPLE
)!=5
3795 errln("FAIL: UnicodeSet(%s).span(while longest match) returns the wrong value", pattern
);
3798 pattern
="[d{cd}{bcd}{ab}]";
3799 pattern16
=UnicodeString(pattern
, -1, US_INV
);
3800 set
.applyPattern(pattern16
, errorCode
).freeze();
3801 if(U_FAILURE(errorCode
)) {
3802 errln("FAIL: Unable to create UnicodeSet(%s) - %s", pattern
, u_errorName(errorCode
));
3805 string16
=UNICODE_STRING_SIMPLE("abbcdabcdabd");
3806 s16
=string16
.getBuffer();
3807 length16
=string16
.length();
3808 if( set
.spanBack(s16
, 12, USET_SPAN_CONTAINED
)!=0 ||
3809 set
.spanBack(s16
, 12, USET_SPAN_SIMPLE
)!=6 ||
3810 set
.spanBack(s16
, 5, USET_SPAN_SIMPLE
)!=0
3812 errln("FAIL: UnicodeSet(%s).spanBack(while longest match) returns the wrong value", pattern
);