1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ********************************************************************************
5 * Copyright (C) 1999-2016 International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************************
8 * Date Name Description
9 * 10/20/99 alan Creation.
10 * 03/22/2000 Madhu Added additional tests
11 ********************************************************************************
17 #include "unicode/utypes.h"
19 #include "unicode/ucnv.h"
20 #include "unicode/uniset.h"
21 #include "unicode/uchar.h"
22 #include "unicode/usetiter.h"
23 #include "unicode/ustring.h"
24 #include "unicode/parsepos.h"
25 #include "unicode/symtable.h"
26 #include "unicode/uversion.h"
30 #define TEST_ASSERT_SUCCESS(status) {if (U_FAILURE(status)) { \
31 dataerrln("fail in file \"%s\", line %d: \"%s\"", __FILE__, __LINE__, \
32 u_errorName(status));}}
34 #define TEST_ASSERT(expr) {if (!(expr)) { \
35 dataerrln("fail in file \"%s\", line %d", __FILE__, __LINE__); }}
37 UnicodeString
operator+(const UnicodeString
& left
, const UnicodeSet
& set
) {
40 return left
+ UnicodeSetTest::escape(pat
);
43 #define CASE(id,test) case id: \
52 UnicodeSetTest::UnicodeSetTest() : utf8Cnv(NULL
) {
55 UConverter
*UnicodeSetTest::openUTF8Converter() {
57 UErrorCode errorCode
=U_ZERO_ERROR
;
58 utf8Cnv
=ucnv_open("UTF-8", &errorCode
);
63 UnicodeSetTest::~UnicodeSetTest() {
68 UnicodeSetTest::runIndexedTest(int32_t index
, UBool exec
,
69 const char* &name
, char* /*par*/) {
70 // if (exec) logln((UnicodeString)"TestSuite UnicodeSetTest");
73 CASE(1,TestAddRemove
);
74 CASE(2,TestCategories
);
75 CASE(3,TestCloneEqualHash
);
76 CASE(4,TestMinimalRep
);
78 CASE(6,TestScriptSet
);
79 CASE(7,TestPropertySet
);
81 CASE(9,TestExhaustive
);
82 CASE(10,TestToPattern
);
86 CASE(14,TestCloseOver
);
87 CASE(15,TestEscapePattern
);
88 CASE(16,TestInvalidCodePoint
);
89 CASE(17,TestSymbolTable
);
90 CASE(18,TestSurrogate
);
91 CASE(19,TestPosixClasses
);
92 CASE(20,TestIteration
);
93 CASE(21,TestFreezable
);
95 CASE(23,TestStringSpan
);
96 CASE(24,TestUCAUnsafeBackwards
);
97 default: name
= ""; break;
101 static const char NOT
[] = "%%%%";
104 * UVector was improperly copying contents
105 * This code will crash this is still true
107 void UnicodeSetTest::Testj2268() {
109 t
.add(UnicodeString("abc"));
111 UnicodeString ustrPat
;
112 test
.toPattern(ustrPat
, TRUE
);
118 void UnicodeSetTest::TestToPattern() {
119 UErrorCode ec
= U_ZERO_ERROR
;
121 // Test that toPattern() round trips with syntax characters and
124 static const char* OTHER_TOPATTERN_TESTS
[] = {
125 "[[:latin:]&[:greek:]]",
126 "[[:latin:]-[:greek:]]",
127 "[:nonspacing mark:]",
131 for (int32_t j
=0; OTHER_TOPATTERN_TESTS
[j
]!=NULL
; ++j
) {
133 UnicodeSet
s(OTHER_TOPATTERN_TESTS
[j
], ec
);
135 dataerrln((UnicodeString
)"FAIL: bad pattern " + OTHER_TOPATTERN_TESTS
[j
] + " - " + UnicodeString(u_errorName(ec
)));
138 checkPat(OTHER_TOPATTERN_TESTS
[j
], s
);
141 for (UChar32 i
= 0; i
<= 0x10FFFF; ++i
) {
142 if ((i
<= 0xFF && !u_isalpha(i
)) || u_isspace(i
)) {
144 // check various combinations to make sure they all work.
145 if (i
!= 0 && !toPatternAux(i
, i
)){
148 if (!toPatternAux(0, i
)){
151 if (!toPatternAux(i
, 0xFFFF)){
158 // Test pattern behavior of multicharacter strings.
161 UnicodeSet
* s
= new UnicodeSet("[a-z {aa} {ab}]", ec
);
163 // This loop isn't a loop. It's here to make the compiler happy.
164 // If you're curious, try removing it and changing the 'break'
165 // statements (except for the last) to goto's.
167 if (U_FAILURE(ec
)) break;
168 const char* exp1
[] = {"aa", "ab", NOT
, "ac", NULL
};
169 expectToPattern(*s
, "[a-z{aa}{ab}]", exp1
);
172 const char* exp2
[] = {"aa", "ab", "ac", NOT
, "xy", NULL
};
173 expectToPattern(*s
, "[a-z{aa}{ab}{ac}]", exp2
);
175 s
->applyPattern(UNICODE_STRING_SIMPLE("[a-z {\\{l} {r\\}}]"), ec
);
176 if (U_FAILURE(ec
)) break;
177 const char* exp3
[] = {"{l", "r}", NOT
, "xy", NULL
};
178 expectToPattern(*s
, UNICODE_STRING_SIMPLE("[a-z{r\\}}{\\{l}]"), exp3
);
181 const char* exp4
[] = {"{l", "r}", "[]", NOT
, "xy", NULL
};
182 expectToPattern(*s
, UNICODE_STRING_SIMPLE("[a-z{\\[\\]}{r\\}}{\\{l}]"), exp4
);
184 s
->applyPattern(UNICODE_STRING_SIMPLE("[a-z {\\u4E01\\u4E02}{\\n\\r}]"), ec
);
185 if (U_FAILURE(ec
)) break;
186 const char* exp5
[] = {"\\u4E01\\u4E02", "\n\r", NULL
};
187 expectToPattern(*s
, UNICODE_STRING_SIMPLE("[a-z{\\u000A\\u000D}{\\u4E01\\u4E02}]"), exp5
);
191 s
->add(UnicodeString("abc", ""));
192 s
->add(UnicodeString("abc", ""));
193 const char* exp6
[] = {"abc", NOT
, "ab", NULL
};
194 expectToPattern(*s
, "[{abc}]", exp6
);
199 if (U_FAILURE(ec
)) errln("FAIL: pattern parse error");
203 // JB#3400: For 2 character ranges prefer [ab] to [a-b]
205 s
.add((UChar
)97, (UChar
)98); // 'a', 'b'
206 expectToPattern(s
, "[ab]", NULL
);
209 UBool
UnicodeSetTest::toPatternAux(UChar32 start
, UChar32 end
) {
211 // use Integer.toString because Utility.hex doesn't handle ints
212 UnicodeString pat
= "";
213 // TODO do these in hex
214 //String source = "0x" + Integer.toString(start,16).toUpperCase();
215 //if (start != end) source += "..0x" + Integer.toString(end,16).toUpperCase();
216 UnicodeString source
;
217 source
= source
+ (uint32_t)start
;
219 source
= source
+ ".." + (uint32_t)end
;
221 testSet
.add(start
, end
);
222 return checkPat(source
, testSet
);
225 UBool
UnicodeSetTest::checkPat(const UnicodeString
& source
,
226 const UnicodeSet
& testSet
) {
227 // What we want to make sure of is that a pattern generated
228 // by toPattern(), with or without escaped unprintables, can
229 // be passed back into the UnicodeSet constructor.
232 testSet
.toPattern(pat0
, TRUE
);
234 if (!checkPat(source
+ " (escaped)", testSet
, pat0
)) return FALSE
;
236 //String pat1 = unescapeLeniently(pat0);
237 //if (!checkPat(source + " (in code)", testSet, pat1)) return false;
240 testSet
.toPattern(pat2
, FALSE
);
241 if (!checkPat(source
, testSet
, pat2
)) return FALSE
;
243 //String pat3 = unescapeLeniently(pat2);
244 // if (!checkPat(source + " (in code)", testSet, pat3)) return false;
246 //logln(source + " => " + pat0 + ", " + pat1 + ", " + pat2 + ", " + pat3);
247 logln((UnicodeString
)source
+ " => " + pat0
+ ", " + pat2
);
251 UBool
UnicodeSetTest::checkPat(const UnicodeString
& source
,
252 const UnicodeSet
& testSet
,
253 const UnicodeString
& pat
) {
254 UErrorCode ec
= U_ZERO_ERROR
;
255 UnicodeSet
testSet2(pat
, ec
);
256 if (testSet2
!= testSet
) {
257 errln((UnicodeString
)"Fail toPattern: " + source
+ " => " + pat
);
264 UnicodeSetTest::TestPatterns(void) {
266 expectPattern(set
, UnicodeString("[[a-m]&[d-z]&[k-y]]", ""), "km");
267 expectPattern(set
, UnicodeString("[[a-z]-[m-y]-[d-r]]", ""), "aczz");
268 expectPattern(set
, UnicodeString("[a\\-z]", ""), "--aazz");
269 expectPattern(set
, UnicodeString("[-az]", ""), "--aazz");
270 expectPattern(set
, UnicodeString("[az-]", ""), "--aazz");
271 expectPattern(set
, UnicodeString("[[[a-z]-[aeiou]i]]", ""), "bdfnptvz");
273 // Throw in a test of complement
276 exp
.append((UChar
)0x0000).append("aeeoouu").append((UChar
)(0x007a+1)).append((UChar
)0xFFFF);
277 expectPairs(set
, exp
);
281 UnicodeSetTest::TestCategories(void) {
282 UErrorCode status
= U_ZERO_ERROR
;
283 const char* pat
= " [:Lu:] "; // Whitespace ok outside [:..:]
284 UnicodeSet
set(pat
, status
);
285 if (U_FAILURE(status
)) {
286 dataerrln((UnicodeString
)"Fail: Can't construct set with " + pat
+ " - " + UnicodeString(u_errorName(status
)));
289 expectContainment(set
, pat
, "ABC", "abc");
293 int32_t failures
= 0;
294 // Make sure generation of L doesn't pollute cached Lu set
295 // First generate L, then Lu
296 set
.applyPattern("[:L:]", status
);
297 if (U_FAILURE(status
)) { errln("FAIL"); return; }
298 for (i
=0; i
<0x200; ++i
) {
299 UBool l
= u_isalpha((UChar
)i
);
300 if (l
!= set
.contains(i
)) {
301 errln((UnicodeString
)"FAIL: L contains " + (unsigned short)i
+ " = " +
303 if (++failures
== 10) break;
307 set
.applyPattern("[:Lu:]", status
);
308 if (U_FAILURE(status
)) { errln("FAIL"); return; }
309 for (i
=0; i
<0x200; ++i
) {
310 UBool lu
= (u_charType((UChar
)i
) == U_UPPERCASE_LETTER
);
311 if (lu
!= set
.contains(i
)) {
312 errln((UnicodeString
)"FAIL: Lu contains " + (unsigned short)i
+ " = " +
314 if (++failures
== 20) break;
319 UnicodeSetTest::TestCloneEqualHash(void) {
320 UErrorCode status
= U_ZERO_ERROR
;
321 // set1 and set2 used to be built with the obsolete constructor taking
322 // UCharCategory values; replaced with pattern constructors
324 UnicodeSet
*set1
=new UnicodeSet(UNICODE_STRING_SIMPLE("\\p{Lowercase Letter}"), status
); // :Ll: Letter, lowercase
325 UnicodeSet
*set1a
=new UnicodeSet(UNICODE_STRING_SIMPLE("[:Ll:]"), status
); // Letter, lowercase
326 if (U_FAILURE(status
)){
327 dataerrln((UnicodeString
)"FAIL: Can't construst set with category->Ll" + " - " + UnicodeString(u_errorName(status
)));
330 UnicodeSet
*set2
=new UnicodeSet(UNICODE_STRING_SIMPLE("\\p{Decimal Number}"), status
); //Number, Decimal digit
331 UnicodeSet
*set2a
=new UnicodeSet(UNICODE_STRING_SIMPLE("[:Nd:]"), status
); //Number, Decimal digit
332 if (U_FAILURE(status
)){
333 errln((UnicodeString
)"FAIL: Can't construct set with category->Nd");
337 if (*set1
!= *set1a
) {
338 errln("FAIL: category constructor for Ll broken");
340 if (*set2
!= *set2a
) {
341 errln("FAIL: category constructor for Nd broken");
346 logln("Testing copy construction");
347 UnicodeSet
*set1copy
=new UnicodeSet(*set1
);
348 if(*set1
!= *set1copy
|| *set1
== *set2
||
349 getPairs(*set1
) != getPairs(*set1copy
) ||
350 set1
->hashCode() != set1copy
->hashCode()){
351 errln("FAIL : Error in copy construction");
355 logln("Testing =operator");
356 UnicodeSet set1equal
=*set1
;
357 UnicodeSet set2equal
=*set2
;
358 if(set1equal
!= *set1
|| set1equal
!= *set1copy
|| set2equal
!= *set2
||
359 set2equal
== *set1
|| set2equal
== *set1copy
|| set2equal
== set1equal
){
360 errln("FAIL: Error in =operator");
363 logln("Testing clone()");
364 UnicodeSet
*set1clone
=(UnicodeSet
*)set1
->clone();
365 UnicodeSet
*set2clone
=(UnicodeSet
*)set2
->clone();
366 if(*set1clone
!= *set1
|| *set1clone
!= *set1copy
|| *set1clone
!= set1equal
||
367 *set2clone
!= *set2
|| *set2clone
== *set1copy
|| *set2clone
!= set2equal
||
368 *set2clone
== *set1
|| *set2clone
== set1equal
|| *set2clone
== *set1clone
){
369 errln("FAIL: Error in clone");
372 logln("Testing hashcode");
373 if(set1
->hashCode() != set1equal
.hashCode() || set1
->hashCode() != set1clone
->hashCode() ||
374 set2
->hashCode() != set2equal
.hashCode() || set2
->hashCode() != set2clone
->hashCode() ||
375 set1copy
->hashCode() != set1equal
.hashCode() || set1copy
->hashCode() != set1clone
->hashCode() ||
376 set1
->hashCode() == set2
->hashCode() || set1copy
->hashCode() == set2
->hashCode() ||
377 set2
->hashCode() == set1clone
->hashCode() || set2
->hashCode() == set1equal
.hashCode() ){
378 errln("FAIL: Error in hashCode()");
390 UnicodeSetTest::TestAddRemove(void) {
391 UnicodeSet set
; // Construct empty set
392 doAssert(set
.isEmpty() == TRUE
, "set should be empty");
393 doAssert(set
.size() == 0, "size should be 0");
395 doAssert(set
.size() == 0x110000, "size should be 0x110000");
397 set
.add(0x0061, 0x007a);
398 expectPairs(set
, "az");
399 doAssert(set
.isEmpty() == FALSE
, "set should not be empty");
400 doAssert(set
.size() != 0, "size should not be equal to 0");
401 doAssert(set
.size() == 26, "size should be equal to 26");
402 set
.remove(0x006d, 0x0070);
403 expectPairs(set
, "alqz");
404 doAssert(set
.size() == 22, "size should be equal to 22");
405 set
.remove(0x0065, 0x0067);
406 expectPairs(set
, "adhlqz");
407 doAssert(set
.size() == 19, "size should be equal to 19");
408 set
.remove(0x0064, 0x0069);
409 expectPairs(set
, "acjlqz");
410 doAssert(set
.size() == 16, "size should be equal to 16");
411 set
.remove(0x0063, 0x0072);
412 expectPairs(set
, "absz");
413 doAssert(set
.size() == 10, "size should be equal to 10");
414 set
.add(0x0066, 0x0071);
415 expectPairs(set
, "abfqsz");
416 doAssert(set
.size() == 22, "size should be equal to 22");
417 set
.remove(0x0061, 0x0067);
418 expectPairs(set
, "hqsz");
419 set
.remove(0x0061, 0x007a);
420 expectPairs(set
, "");
421 doAssert(set
.isEmpty() == TRUE
, "set should be empty");
422 doAssert(set
.size() == 0, "size should be 0");
424 doAssert(set
.isEmpty() == FALSE
, "set should not be empty");
425 doAssert(set
.size() == 1, "size should not be equal to 1");
428 expectPairs(set
, "ac");
429 doAssert(set
.size() == 3, "size should not be equal to 3");
432 expectPairs(set
, "acpq");
433 doAssert(set
.size() == 5, "size should not be equal to 5");
435 expectPairs(set
, "");
436 doAssert(set
.isEmpty() == TRUE
, "set should be empty");
437 doAssert(set
.size() == 0, "size should be 0");
439 // Try removing an entire set from another set
440 expectPattern(set
, "[c-x]", "cx");
442 expectPattern(set2
, "[f-ky-za-bc[vw]]", "acfkvwyz");
444 expectPairs(set
, "deluxx");
446 // Try adding an entire set to another set
447 expectPattern(set
, "[jackiemclean]", "aacceein");
448 expectPattern(set2
, "[hitoshinamekatajamesanderson]", "aadehkmort");
450 expectPairs(set
, "aacehort");
451 doAssert(set
.containsAll(set2
) == TRUE
, "set should contain all the elements in set2");
453 // Try retaining an set of elements contained in another set (intersection)
455 expectPattern(set3
, "[a-c]", "ac");
456 doAssert(set
.containsAll(set3
) == FALSE
, "set doesn't contain all the elements in set3");
458 expectPairs(set3
, "aacc");
459 doAssert(set
.containsAll(set3
) == TRUE
, "set should contain all the elements in set3");
461 expectPairs(set
, "aacc");
462 doAssert(set
.size() == set3
.size(), "set.size() should be set3.size()");
463 doAssert(set
.containsAll(set3
) == TRUE
, "set should contain all the elements in set3");
465 doAssert(set
.size() != set3
.size(), "set.size() != set3.size()");
467 // Test commutativity
468 expectPattern(set
, "[hitoshinamekatajamesanderson]", "aadehkmort");
469 expectPattern(set2
, "[jackiemclean]", "aacceein");
471 expectPairs(set
, "aacehort");
472 doAssert(set
.containsAll(set2
) == TRUE
, "set should contain all the elements in set2");
480 * Make sure minimal representation is maintained.
482 void UnicodeSetTest::TestMinimalRep() {
483 UErrorCode status
= U_ZERO_ERROR
;
484 // This is pretty thoroughly tested by checkCanonicalRep()
485 // run against the exhaustive operation results. Use the code
486 // here for debugging specific spot problems.
488 // 1 overlap against 2
489 UnicodeSet
set("[h-km-q]", status
);
490 if (U_FAILURE(status
)) { errln("FAIL"); return; }
491 UnicodeSet
set2("[i-o]", status
);
492 if (U_FAILURE(status
)) { errln("FAIL"); return; }
494 expectPairs(set
, "hq");
496 set
.applyPattern("[a-m]", status
);
497 if (U_FAILURE(status
)) { errln("FAIL"); return; }
498 set2
.applyPattern("[e-o]", status
);
499 if (U_FAILURE(status
)) { errln("FAIL"); return; }
501 expectPairs(set
, "ao");
503 set
.applyPattern("[e-o]", status
);
504 if (U_FAILURE(status
)) { errln("FAIL"); return; }
505 set2
.applyPattern("[a-m]", status
);
506 if (U_FAILURE(status
)) { errln("FAIL"); return; }
508 expectPairs(set
, "ao");
509 // 1 overlap against 3
510 set
.applyPattern("[a-eg-mo-w]", status
);
511 if (U_FAILURE(status
)) { errln("FAIL"); return; }
512 set2
.applyPattern("[d-q]", status
);
513 if (U_FAILURE(status
)) { errln("FAIL"); return; }
515 expectPairs(set
, "aw");
518 void UnicodeSetTest::TestAPI() {
519 UErrorCode status
= U_ZERO_ERROR
;
522 if (!set
.isEmpty() || set
.getRangeCount() != 0) {
523 errln((UnicodeString
)"FAIL, set should be empty but isn't: " +
527 // clear(), isEmpty()
530 errln((UnicodeString
)"FAIL, set shouldn't be empty but is: " +
534 if (!set
.isEmpty()) {
535 errln((UnicodeString
)"FAIL, set should be empty but isn't: " +
541 if (set
.size() != 0) {
542 errln((UnicodeString
)"FAIL, size should be 0, but is " + set
.size() +
546 if (set
.size() != 1) {
547 errln((UnicodeString
)"FAIL, size should be 1, but is " + set
.size() +
550 set
.add(0x0031, 0x0039);
551 if (set
.size() != 10) {
552 errln((UnicodeString
)"FAIL, size should be 10, but is " + set
.size() +
556 // contains(first, last)
558 set
.applyPattern("[A-Y 1-8 b-d l-y]", status
);
559 if (U_FAILURE(status
)) { errln("FAIL"); return; }
560 for (int32_t i
= 0; i
<set
.getRangeCount(); ++i
) {
561 UChar32 a
= set
.getRangeStart(i
);
562 UChar32 b
= set
.getRangeEnd(i
);
563 if (!set
.contains(a
, b
)) {
564 errln((UnicodeString
)"FAIL, should contain " + (unsigned short)a
+ '-' + (unsigned short)b
+
565 " but doesn't: " + set
);
567 if (set
.contains((UChar32
)(a
-1), b
)) {
568 errln((UnicodeString
)"FAIL, shouldn't contain " +
569 (unsigned short)(a
-1) + '-' + (unsigned short)b
+
570 " but does: " + set
);
572 if (set
.contains(a
, (UChar32
)(b
+1))) {
573 errln((UnicodeString
)"FAIL, shouldn't contain " +
574 (unsigned short)a
+ '-' + (unsigned short)(b
+1) +
575 " but does: " + set
);
579 // Ported InversionList test.
580 UnicodeSet
a((UChar32
)3,(UChar32
)10);
581 UnicodeSet
b((UChar32
)7,(UChar32
)15);
584 logln((UnicodeString
)"a [3-10]: " + a
);
585 logln((UnicodeString
)"b [7-15]: " + b
);
588 UnicodeSet
exp((UChar32
)3,(UChar32
)15);
590 logln((UnicodeString
)"c.set(a).add(b): " + c
);
592 errln((UnicodeString
)"FAIL: c.set(a).add(b) = " + c
+ ", expect " + exp
);
595 exp
.set((UChar32
)0, (UChar32
)2);
596 exp
.add((UChar32
)16, UnicodeSet::MAX_VALUE
);
598 logln((UnicodeString
)"c.complement(): " + c
);
600 errln((UnicodeString
)"FAIL: c.complement() = " + c
+ ", expect " + exp
);
603 exp
.set((UChar32
)3, (UChar32
)15);
605 logln((UnicodeString
)"c.complement(): " + c
);
607 errln((UnicodeString
)"FAIL: c.complement() = " + c
+ ", expect " + exp
);
611 exp
.set((UChar32
)3,(UChar32
)6);
612 exp
.add((UChar32
)11,(UChar32
) 15);
614 logln((UnicodeString
)"c.set(a).exclusiveOr(b): " + c
);
616 errln((UnicodeString
)"FAIL: c.set(a).exclusiveOr(b) = " + c
+ ", expect " + exp
);
620 bitsToSet(setToBits(c
), c
);
622 logln((UnicodeString
)"bitsToSet(setToBits(c)): " + c
);
624 errln((UnicodeString
)"FAIL: bitsToSet(setToBits(c)) = " + c
+ ", expect " + exp
);
627 // Additional tests for coverage JB#2118
628 //UnicodeSet::complement(class UnicodeString const &)
629 //UnicodeSet::complementAll(class UnicodeString const &)
630 //UnicodeSet::containsNone(class UnicodeSet const &)
631 //UnicodeSet::containsNone(long,long)
632 //UnicodeSet::containsSome(class UnicodeSet const &)
633 //UnicodeSet::containsSome(long,long)
634 //UnicodeSet::removeAll(class UnicodeString const &)
635 //UnicodeSet::retain(long)
636 //UnicodeSet::retainAll(class UnicodeString const &)
637 //UnicodeSet::serialize(unsigned short *,long,enum UErrorCode &)
638 //UnicodeSetIterator::getString(void)
640 set
.complement("ab");
641 exp
.applyPattern("[{ab}]", status
);
642 if (U_FAILURE(status
)) { errln("FAIL"); return; }
643 if (set
!= exp
) { errln("FAIL: complement(\"ab\")"); return; }
645 UnicodeSetIterator
iset(set
);
646 if (!iset
.next() || !iset
.isString()) {
647 errln("FAIL: UnicodeSetIterator::next/isString");
648 } else if (iset
.getString() != "ab") {
649 errln("FAIL: UnicodeSetIterator::getString");
652 set
.add((UChar32
)0x61, (UChar32
)0x7A);
653 set
.complementAll("alan");
654 exp
.applyPattern("[{ab}b-kmo-z]", status
);
655 if (U_FAILURE(status
)) { errln("FAIL"); return; }
656 if (set
!= exp
) { errln("FAIL: complementAll(\"alan\")"); return; }
658 exp
.applyPattern("[a-z]", 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)"); }
662 exp
.applyPattern("[aln]", status
);
663 if (U_FAILURE(status
)) { errln("FAIL"); return; }
664 if (!set
.containsNone(exp
)) { errln("FAIL: containsNone(UnicodeSet)"); }
665 if (set
.containsSome(exp
)) { errln("FAIL: containsSome(UnicodeSet)"); }
667 if (set
.containsNone((UChar32
)0x61, (UChar32
)0x7A)) {
668 errln("FAIL: containsNone(UChar32, UChar32)");
670 if (!set
.containsSome((UChar32
)0x61, (UChar32
)0x7A)) {
671 errln("FAIL: containsSome(UChar32, UChar32)");
673 if (!set
.containsNone((UChar32
)0x41, (UChar32
)0x5A)) {
674 errln("FAIL: containsNone(UChar32, UChar32)");
676 if (set
.containsSome((UChar32
)0x41, (UChar32
)0x5A)) {
677 errln("FAIL: containsSome(UChar32, UChar32)");
680 set
.removeAll("liu");
681 exp
.applyPattern("[{ab}b-hj-kmo-tv-z]", status
);
682 if (U_FAILURE(status
)) { errln("FAIL"); return; }
683 if (set
!= exp
) { errln("FAIL: removeAll(\"liu\")"); return; }
685 set
.retainAll("star");
686 exp
.applyPattern("[rst]", status
);
687 if (U_FAILURE(status
)) { errln("FAIL"); return; }
688 if (set
!= exp
) { errln("FAIL: retainAll(\"star\")"); return; }
690 set
.retain((UChar32
)0x73);
691 exp
.applyPattern("[s]", status
);
692 if (U_FAILURE(status
)) { errln("FAIL"); return; }
693 if (set
!= exp
) { errln("FAIL: retain('s')"); return; }
696 int32_t slen
= set
.serialize(buf
, UPRV_LENGTHOF(buf
), status
);
697 if (U_FAILURE(status
)) { errln("FAIL: serialize"); return; }
698 if (slen
!= 3 || buf
[0] != 2 || buf
[1] != 0x73 || buf
[2] != 0x74) {
699 errln("FAIL: serialize");
703 // Conversions to and from USet
704 UnicodeSet
*uniset
= &set
;
705 USet
*uset
= uniset
->toUSet();
706 TEST_ASSERT((void *)uset
== (void *)uniset
);
707 UnicodeSet
*setx
= UnicodeSet::fromUSet(uset
);
708 TEST_ASSERT((void *)setx
== (void *)uset
);
709 const UnicodeSet
*constSet
= uniset
;
710 const USet
*constUSet
= constSet
->toUSet();
711 TEST_ASSERT((void *)constUSet
== (void *)constSet
);
712 const UnicodeSet
*constSetx
= UnicodeSet::fromUSet(constUSet
);
713 TEST_ASSERT((void *)constSetx
== (void *)constUSet
);
715 // span(UnicodeString) and spanBack(UnicodeString) convenience methods
716 UnicodeString longString
=UNICODE_STRING_SIMPLE("aaaaaaaaaabbbbbbbbbbcccccccccc");
717 UnicodeSet
ac(0x61, 0x63);
718 ac
.remove(0x62).freeze();
719 if( ac
.span(longString
, -5, USET_SPAN_CONTAINED
)!=10 ||
720 ac
.span(longString
, 0, USET_SPAN_CONTAINED
)!=10 ||
721 ac
.span(longString
, 5, USET_SPAN_CONTAINED
)!=10 ||
722 ac
.span(longString
, 10, USET_SPAN_CONTAINED
)!=10 ||
723 ac
.span(longString
, 15, USET_SPAN_CONTAINED
)!=15 ||
724 ac
.span(longString
, 20, USET_SPAN_CONTAINED
)!=30 ||
725 ac
.span(longString
, 25, USET_SPAN_CONTAINED
)!=30 ||
726 ac
.span(longString
, 30, USET_SPAN_CONTAINED
)!=30 ||
727 ac
.span(longString
, 35, USET_SPAN_CONTAINED
)!=30 ||
728 ac
.span(longString
, INT32_MAX
, USET_SPAN_CONTAINED
)!=30
730 errln("UnicodeSet.span(UnicodeString, ...) returns incorrect end indexes");
732 if( ac
.spanBack(longString
, -5, USET_SPAN_CONTAINED
)!=0 ||
733 ac
.spanBack(longString
, 0, USET_SPAN_CONTAINED
)!=0 ||
734 ac
.spanBack(longString
, 5, USET_SPAN_CONTAINED
)!=0 ||
735 ac
.spanBack(longString
, 10, USET_SPAN_CONTAINED
)!=0 ||
736 ac
.spanBack(longString
, 15, USET_SPAN_CONTAINED
)!=15 ||
737 ac
.spanBack(longString
, 20, USET_SPAN_CONTAINED
)!=20 ||
738 ac
.spanBack(longString
, 25, USET_SPAN_CONTAINED
)!=20 ||
739 ac
.spanBack(longString
, 30, USET_SPAN_CONTAINED
)!=20 ||
740 ac
.spanBack(longString
, 35, USET_SPAN_CONTAINED
)!=20 ||
741 ac
.spanBack(longString
, INT32_MAX
, USET_SPAN_CONTAINED
)!=20
743 errln("UnicodeSet.spanBack(UnicodeString, ...) returns incorrect start indexes");
747 void UnicodeSetTest::TestIteration() {
748 UErrorCode ec
= U_ZERO_ERROR
;
752 // 6 code points, 3 ranges, 2 strings, 8 total elements
753 // Iteration will access them in sorted order - a, b, c, y, z, U0001abcd, "str1", "str2"
754 UnicodeSet
set(UNICODE_STRING_SIMPLE("[zabyc\\U0001abcd{str1}{str2}]"), ec
);
755 TEST_ASSERT_SUCCESS(ec
);
756 UnicodeSetIterator
it(set
);
758 for (outerLoop
=0; outerLoop
<3; outerLoop
++) {
759 // Run the test multiple times, to check that iterator.reset() is working.
760 for (i
=0; i
<10; i
++) {
761 UBool nextv
= it
.next();
762 UBool isString
= it
.isString();
763 int32_t codePoint
= it
.getCodepoint();
764 //int32_t codePointEnd = it.getCodepointEnd();
765 UnicodeString s
= it
.getString();
768 TEST_ASSERT(nextv
== TRUE
);
769 TEST_ASSERT(isString
== FALSE
);
770 TEST_ASSERT(codePoint
==0x61);
771 TEST_ASSERT(s
== "a");
774 TEST_ASSERT(nextv
== TRUE
);
775 TEST_ASSERT(isString
== FALSE
);
776 TEST_ASSERT(codePoint
==0x62);
777 TEST_ASSERT(s
== "b");
780 TEST_ASSERT(nextv
== TRUE
);
781 TEST_ASSERT(isString
== FALSE
);
782 TEST_ASSERT(codePoint
==0x63);
783 TEST_ASSERT(s
== "c");
786 TEST_ASSERT(nextv
== TRUE
);
787 TEST_ASSERT(isString
== FALSE
);
788 TEST_ASSERT(codePoint
==0x79);
789 TEST_ASSERT(s
== "y");
792 TEST_ASSERT(nextv
== TRUE
);
793 TEST_ASSERT(isString
== FALSE
);
794 TEST_ASSERT(codePoint
==0x7a);
795 TEST_ASSERT(s
== "z");
798 TEST_ASSERT(nextv
== TRUE
);
799 TEST_ASSERT(isString
== FALSE
);
800 TEST_ASSERT(codePoint
==0x1abcd);
801 TEST_ASSERT(s
== UnicodeString((UChar32
)0x1abcd));
804 TEST_ASSERT(nextv
== TRUE
);
805 TEST_ASSERT(isString
== TRUE
);
806 TEST_ASSERT(s
== "str1");
809 TEST_ASSERT(nextv
== TRUE
);
810 TEST_ASSERT(isString
== TRUE
);
811 TEST_ASSERT(s
== "str2");
814 TEST_ASSERT(nextv
== FALSE
);
817 TEST_ASSERT(nextv
== FALSE
);
821 it
.reset(); // prepare to run the iteration again.
828 void UnicodeSetTest::TestStrings() {
829 UErrorCode ec
= U_ZERO_ERROR
;
831 UnicodeSet
* testList
[] = {
832 UnicodeSet::createFromAll("abc"),
833 new UnicodeSet("[a-c]", ec
),
835 &(UnicodeSet::createFrom("ch")->add('a','z').add("ll")),
836 new UnicodeSet("[{ll}{ch}a-z]", ec
),
838 UnicodeSet::createFrom("ab}c"),
839 new UnicodeSet("[{ab\\}c}]", ec
),
841 &((new UnicodeSet('a','z'))->add('A', 'Z').retain('M','m').complement('X')),
842 new UnicodeSet("[[a-zA-Z]&[M-m]-[X]]", ec
),
848 errln("FAIL: couldn't construct test sets");
851 for (int32_t i
= 0; testList
[i
] != NULL
; i
+=2) {
853 UnicodeString pat0
, pat1
;
854 testList
[i
]->toPattern(pat0
, TRUE
);
855 testList
[i
+1]->toPattern(pat1
, TRUE
);
856 if (*testList
[i
] == *testList
[i
+1]) {
857 logln((UnicodeString
)"Ok: " + pat0
+ " == " + pat1
);
859 logln((UnicodeString
)"FAIL: " + pat0
+ " != " + pat1
);
863 delete testList
[i
+1];
868 * Test the [:Latin:] syntax.
870 void UnicodeSetTest::TestScriptSet() {
871 expectContainment(UNICODE_STRING_SIMPLE("[:Latin:]"), "aA", CharsToUnicodeString("\\u0391\\u03B1"));
873 expectContainment(UNICODE_STRING_SIMPLE("[:Greek:]"), CharsToUnicodeString("\\u0391\\u03B1"), "aA");
876 expectContainment(UNICODE_STRING_SIMPLE("[[:Common:][:Inherited:]]"), CharsToUnicodeString("\\U00003099\\U0001D169\\u0000"), "aA");
881 * Test the [:Latin:] syntax.
883 void UnicodeSetTest::TestPropertySet() {
884 static const char* const DATA
[] = {
885 // Pattern, Chars IN, Chars NOT in
895 "\\P{ GENERAL Category = upper case letter }",
899 #if !UCONFIG_NO_NORMALIZATION
900 // Combining class: @since ICU 2.2
901 // Check both symbolic and numeric
906 "\\p{Canonical Combining Class = 11}",
910 "[:c c c = iota subscript :]",
915 // Bidi class: @since ICU 2.2
916 "\\p{bidiclass=lefttoright}",
920 // Binary properties: @since ICU 2.2
927 // weiv: )(and * were removed from math in Unicode 4.0.1
931 // JB#1767 \N{}, \p{ASCII}
936 "[\\N{ latin small letter a }[:name= latin small letter z:]]",
952 "\\u03D8\\u03D9", // 3.2
955 "\\u1800\\u3400\\U0002f800",
956 "\\u0220\\u034f\\u30ff\\u33ff\\ufe73\\U00010000\\U00050000",
958 // JB#2350: Case_Sensitive
959 "[:Case Sensitive:]",
960 "A\\u1FFC\\U00010410",
961 ";\\u00B4\\U00010500",
963 // JB#2832: C99-compatibility props
970 " \\u0003\\u0007\\u0009\\u000A\\u000D",
973 "!@#%&*()[]{}-_\\/;:,.?'\"",
980 // Regex compatibility test
981 "[-b]", // leading '-' is literal
985 "[^-b]", // leading '-' is literal
989 "[b-]", // trailing '-' is literal
993 "[^b-]", // trailing '-' is literal
997 "[a-b-]", // trailing '-' is literal
1001 "[[a-q]&[p-z]-]", // trailing '-' is literal
1005 "[\\s|\\)|:|$|\\>]", // from regex tests
1009 "[\\uDC00cd]", // JB#2906: isolated trail at start
1011 "ab\\uD800\\U00010000",
1013 "[ab\\uD800]", // JB#2906: isolated trail at start
1015 "cd\\uDC00\\U00010000",
1017 "[ab\\uD800cd]", // JB#2906: isolated lead in middle
1019 "ef\\uDC00\\U00010000",
1021 "[ab\\uDC00cd]", // JB#2906: isolated trail in middle
1023 "ef\\uD800\\U00010000",
1025 #if !UCONFIG_NO_NORMALIZATION
1026 "[:^lccc=0:]", // Lead canonical class
1028 "abcd\\u00c0\\u00c5",
1030 "[:^tccc=0:]", // Trail canonical class
1031 "\\u0300\\u0301\\u00c0\\u00c5",
1034 "[[:^lccc=0:][:^tccc=0:]]", // Lead and trail canonical class
1035 "\\u0300\\u0301\\u00c0\\u00c5",
1038 "[[:^lccc=0:]-[:^tccc=0:]]", // Stuff that starts with an accent but ends with a base (none right now)
1040 "abcd\\u0300\\u0301\\u00c0\\u00c5",
1042 "[[:ccc=0:]-[:lccc=0:]-[:tccc=0:]]", // Weirdos. Complete canonical class is zero, but both lead and trail are not
1043 "\\u0F73\\u0F75\\u0F81",
1044 "abcd\\u0300\\u0301\\u00c0\\u00c5",
1045 #endif /* !UCONFIG_NO_NORMALIZATION */
1048 "A\\uE000\\uF8FF\\uFDC7\\U00010000\\U0010FFFD",
1049 "\\u0888\\uFDD3\\uFFFE\\U00050005",
1051 // Script_Extensions, new in Unicode 6.0
1053 "\\u061E\\u061F\\u0620\\u0621\\u063F\\u0640\\u0650\\u065E\\uFDF1\\uFDF2\\uFDF3",
1054 "\\u061D\\uFDEF\\uFDFE",
1056 // U+FDF2 has Script=Arabic and also Arab in its Script_Extensions,
1057 // so scx-sc is missing U+FDF2.
1058 "[[:Script_Extensions=Arabic:]-[:Arab:]]",
1059 "\\u0640\\u064B\\u0650\\u0655",
1063 static const int32_t DATA_LEN
= UPRV_LENGTHOF(DATA
);
1065 for (int32_t i
=0; i
<DATA_LEN
; i
+=3) {
1066 expectContainment(UnicodeString(DATA
[i
], -1, US_INV
), CharsToUnicodeString(DATA
[i
+1]),
1067 CharsToUnicodeString(DATA
[i
+2]));
1072 * Test that Posix style character classes [:digit:], etc.
1073 * have the Unicode definitions from TR 18.
1075 void UnicodeSetTest::TestPosixClasses() {
1077 UErrorCode status
= U_ZERO_ERROR
;
1078 UnicodeSet
s1("[:alpha:]", status
);
1079 UnicodeSet
s2(UNICODE_STRING_SIMPLE("\\p{Alphabetic}"), status
);
1080 TEST_ASSERT_SUCCESS(status
);
1081 TEST_ASSERT(s1
==s2
);
1084 UErrorCode status
= U_ZERO_ERROR
;
1085 UnicodeSet
s1("[:lower:]", status
);
1086 UnicodeSet
s2(UNICODE_STRING_SIMPLE("\\p{lowercase}"), status
);
1087 TEST_ASSERT_SUCCESS(status
);
1088 TEST_ASSERT(s1
==s2
);
1091 UErrorCode status
= U_ZERO_ERROR
;
1092 UnicodeSet
s1("[:upper:]", status
);
1093 UnicodeSet
s2(UNICODE_STRING_SIMPLE("\\p{Uppercase}"), status
);
1094 TEST_ASSERT_SUCCESS(status
);
1095 TEST_ASSERT(s1
==s2
);
1098 UErrorCode status
= U_ZERO_ERROR
;
1099 UnicodeSet
s1("[:punct:]", status
);
1100 UnicodeSet
s2(UNICODE_STRING_SIMPLE("\\p{gc=Punctuation}"), status
);
1101 TEST_ASSERT_SUCCESS(status
);
1102 TEST_ASSERT(s1
==s2
);
1105 UErrorCode status
= U_ZERO_ERROR
;
1106 UnicodeSet
s1("[:digit:]", status
);
1107 UnicodeSet
s2(UNICODE_STRING_SIMPLE("\\p{gc=DecimalNumber}"), status
);
1108 TEST_ASSERT_SUCCESS(status
);
1109 TEST_ASSERT(s1
==s2
);
1112 UErrorCode status
= U_ZERO_ERROR
;
1113 UnicodeSet
s1("[:xdigit:]", status
);
1114 UnicodeSet
s2(UNICODE_STRING_SIMPLE("[\\p{DecimalNumber}\\p{HexDigit}]"), status
);
1115 TEST_ASSERT_SUCCESS(status
);
1116 TEST_ASSERT(s1
==s2
);
1119 UErrorCode status
= U_ZERO_ERROR
;
1120 UnicodeSet
s1("[:alnum:]", status
);
1121 UnicodeSet
s2(UNICODE_STRING_SIMPLE("[\\p{Alphabetic}\\p{DecimalNumber}]"), status
);
1122 TEST_ASSERT_SUCCESS(status
);
1123 TEST_ASSERT(s1
==s2
);
1126 UErrorCode status
= U_ZERO_ERROR
;
1127 UnicodeSet
s1("[:space:]", status
);
1128 UnicodeSet
s2(UNICODE_STRING_SIMPLE("\\p{Whitespace}"), status
);
1129 TEST_ASSERT_SUCCESS(status
);
1130 TEST_ASSERT(s1
==s2
);
1133 UErrorCode status
= U_ZERO_ERROR
;
1134 UnicodeSet
s1("[:blank:]", status
);
1135 TEST_ASSERT_SUCCESS(status
);
1136 UnicodeSet
s2(UNICODE_STRING_SIMPLE("[\\p{Whitespace}-[\\u000a\\u000B\\u000c\\u000d\\u0085\\p{LineSeparator}\\p{ParagraphSeparator}]]"),
1138 TEST_ASSERT_SUCCESS(status
);
1139 TEST_ASSERT(s1
==s2
);
1142 UErrorCode status
= U_ZERO_ERROR
;
1143 UnicodeSet
s1("[:cntrl:]", status
);
1144 TEST_ASSERT_SUCCESS(status
);
1145 UnicodeSet
s2(UNICODE_STRING_SIMPLE("\\p{Control}"), status
);
1146 TEST_ASSERT_SUCCESS(status
);
1147 TEST_ASSERT(s1
==s2
);
1150 UErrorCode status
= U_ZERO_ERROR
;
1151 UnicodeSet
s1("[:graph:]", status
);
1152 TEST_ASSERT_SUCCESS(status
);
1153 UnicodeSet
s2(UNICODE_STRING_SIMPLE("[^\\p{Whitespace}\\p{Control}\\p{Surrogate}\\p{Unassigned}]"), status
);
1154 TEST_ASSERT_SUCCESS(status
);
1155 TEST_ASSERT(s1
==s2
);
1158 UErrorCode status
= U_ZERO_ERROR
;
1159 UnicodeSet
s1("[:print:]", status
);
1160 TEST_ASSERT_SUCCESS(status
);
1161 UnicodeSet
s2(UNICODE_STRING_SIMPLE("[[:graph:][:blank:]-[\\p{Control}]]") ,status
);
1162 TEST_ASSERT_SUCCESS(status
);
1163 TEST_ASSERT(s1
==s2
);
1167 * Test cloning of UnicodeSet. For C++, we test the copy constructor.
1169 void UnicodeSetTest::TestClone() {
1170 UErrorCode ec
= U_ZERO_ERROR
;
1171 UnicodeSet
s("[abcxyz]", ec
);
1173 expectContainment(t
, "abc", "def");
1177 * Test the indexOf() and charAt() methods.
1179 void UnicodeSetTest::TestIndexOf() {
1180 UErrorCode ec
= U_ZERO_ERROR
;
1181 UnicodeSet
set("[a-cx-y3578]", ec
);
1182 if (U_FAILURE(ec
)) {
1183 errln("FAIL: UnicodeSet constructor");
1186 for (int32_t i
=0; i
<set
.size(); ++i
) {
1187 UChar32 c
= set
.charAt(i
);
1188 if (set
.indexOf(c
) != i
) {
1189 errln("FAIL: charAt(%d) = %X => indexOf() => %d",
1190 i
, c
, set
.indexOf(c
));
1193 UChar32 c
= set
.charAt(set
.size());
1195 errln("FAIL: charAt(<out of range>) = %X", c
);
1197 int32_t j
= set
.indexOf((UChar32
)0x71/*'q'*/);
1199 errln((UnicodeString
)"FAIL: indexOf('q') = " + j
);
1206 void UnicodeSetTest::TestCloseOver() {
1207 UErrorCode ec
= U_ZERO_ERROR
;
1209 char CASE
[] = {(char)USET_CASE_INSENSITIVE
};
1210 char CASE_MAPPINGS
[] = {(char)USET_ADD_CASE_MAPPINGS
};
1211 const char* DATA
[] = {
1212 // selector, input, output
1214 "[aq\\u00DF{Bc}{bC}{Fi}]",
1215 "[aAqQ\\u00DF\\u1E9E\\uFB01{ss}{bc}{fi}]", // U+1E9E LATIN CAPITAL LETTER SHARP S is new in Unicode 5.1
1218 "[\\u01F1]", // 'DZ'
1219 "[\\u01F1\\u01F2\\u01F3]",
1223 "[\\u1FB4{\\u03AC\\u03B9}]",
1229 CASE
, // make sure binary search finds limits
1231 "[aA\\uFF3A\\uFF5A]",
1234 "[a-z]","[A-Za-z\\u017F\\u212A]",
1240 CASE
, "[i]", "[iI]",
1242 CASE
, "[\\u0130]", "[\\u0130{i\\u0307}]", // dotted I
1243 CASE
, "[{i\\u0307}]", "[\\u0130{i\\u0307}]", // i with dot
1245 CASE
, "[\\u0131]", "[\\u0131]", // dotless i
1247 CASE
, "[\\u0390]", "[\\u0390\\u1FD3{\\u03B9\\u0308\\u0301}]",
1249 CASE
, "[\\u03c2]", "[\\u03a3\\u03c2\\u03c3]", // sigmas
1251 CASE
, "[\\u03f2]", "[\\u03f2\\u03f9]", // lunate sigmas
1253 CASE
, "[\\u03f7]", "[\\u03f7\\u03f8]",
1255 CASE
, "[\\u1fe3]", "[\\u03b0\\u1fe3{\\u03c5\\u0308\\u0301}]",
1257 CASE
, "[\\ufb05]", "[\\ufb05\\ufb06{st}]",
1258 CASE
, "[{st}]", "[\\ufb05\\ufb06{st}]",
1260 CASE
, "[\\U0001044F]", "[\\U00010427\\U0001044F]",
1262 CASE
, "[{a\\u02BE}]", "[\\u1E9A{a\\u02BE}]", // first in sorted table
1264 CASE
, "[{\\u1f7c\\u03b9}]", "[\\u1ff2{\\u1f7c\\u03b9}]", // last in sorted table
1266 #if !UCONFIG_NO_FILE_IO
1268 "[aq\\u00DF{Bc}{bC}{Fi}]",
1269 "[aAqQ\\u00DF{ss}{Ss}{SS}{Bc}{BC}{bC}{bc}{FI}{Fi}{fi}]",
1273 "[\\u01F1]", // 'DZ'
1274 "[\\u01F1\\u01F2\\u01F3]",
1286 for (int32_t i
=0; DATA
[i
]!=NULL
; i
+=3) {
1287 int32_t selector
= DATA
[i
][0];
1288 UnicodeString
pat(DATA
[i
+1], -1, US_INV
);
1289 UnicodeString
exp(DATA
[i
+2], -1, US_INV
);
1290 s
.applyPattern(pat
, ec
);
1291 s
.closeOver(selector
);
1292 t
.applyPattern(exp
, ec
);
1293 if (U_FAILURE(ec
)) {
1294 errln("FAIL: applyPattern failed");
1298 logln((UnicodeString
)"Ok: " + pat
+ ".closeOver(" + selector
+ ") => " + exp
);
1300 dataerrln((UnicodeString
)"FAIL: " + pat
+ ".closeOver(" + selector
+ ") => " +
1301 s
.toPattern(buf
, TRUE
) + ", expected " + exp
);
1308 * This was used to compare the old implementation (using USET_CASE)
1309 * with the new one (using 0x100 temporarily)
1310 * while transitioning from hardcoded case closure tables in uniset.cpp
1311 * (moved to uniset_props.cpp) to building the data by gencase into ucase.icu.
1312 * and using ucase.c functions for closure.
1313 * See Jitterbug 3432 RFE: Move uniset.cpp data to a data file
1315 * Note: The old and new implementation never fully matched because
1316 * the old implementation turned out to not map U+0130 and U+0131 correctly
1317 * (dotted I and dotless i) and because the old implementation's data tables
1318 * were outdated compared to Unicode 4.0.1 at the time of the change to the
1319 * new implementation. (So sigmas and some other characters were not handled
1320 * according to the newer Unicode version.)
1322 UnicodeSet
sens("[:case_sensitive:]", ec
), sens2
, s2
;
1323 UnicodeSetIterator
si(sens
);
1324 UnicodeString str
, buf2
;
1325 const UnicodeString
*pStr
;
1328 if(!si
.isString()) {
1329 c
=si
.getCodepoint();
1338 s
.closeOver(USET_CASE
);
1341 errln("FAIL: closeOver(U+%04x) differs: ", c
);
1342 errln((UnicodeString
)"old "+s
.toPattern(buf
, TRUE
)+" new: "+t
.toPattern(buf2
, TRUE
));
1346 // remove all code points
1347 // should contain all full case folding mapping strings
1348 sens2
.remove(0, 0x10ffff);
1352 pStr
=&si
.getString();
1356 s
.closeOver(USET_CASE
);
1359 errln((UnicodeString
)"FAIL: closeOver("+s2
.toPattern(buf
, TRUE
)+") differs: ");
1360 errln((UnicodeString
)"old "+s
.toPattern(buf
, TRUE
)+" new: "+t
.toPattern(buf2
, TRUE
));
1366 // Test the pattern API
1367 s
.applyPattern("[abc]", USET_CASE_INSENSITIVE
, NULL
, ec
);
1368 if (U_FAILURE(ec
)) {
1369 errln("FAIL: applyPattern failed");
1371 expectContainment(s
, "abcABC", "defDEF");
1373 UnicodeSet
v("[^abc]", USET_CASE_INSENSITIVE
, NULL
, ec
);
1374 if (U_FAILURE(ec
)) {
1375 errln("FAIL: constructor failed");
1377 expectContainment(v
, "defDEF", "abcABC");
1379 UnicodeSet
cm("[abck]", USET_ADD_CASE_MAPPINGS
, NULL
, ec
);
1380 if (U_FAILURE(ec
)) {
1381 errln("FAIL: construct w/case mappings failed");
1383 expectContainment(cm
, "abckABCK", CharsToUnicodeString("defDEF\\u212A"));
1387 void UnicodeSetTest::TestEscapePattern() {
1388 const char pattern
[] =
1389 "[\\uFEFF \\u200A-\\u200E \\U0001D173-\\U0001D17A \\U000F0000-\\U000FFFFD ]";
1391 "[\\u200A-\\u200E\\uFEFF\\U0001D173-\\U0001D17A\\U000F0000-\\U000FFFFD]";
1392 // We test this with two passes; in the second pass we
1393 // pre-unescape the pattern. Since U+200E is Pattern_White_Space,
1394 // this fails -- which is what we expect.
1395 for (int32_t pass
=1; pass
<=2; ++pass
) {
1396 UErrorCode ec
= U_ZERO_ERROR
;
1397 UnicodeString
pat(pattern
, -1, US_INV
);
1399 pat
= pat
.unescape();
1401 // Pattern is only good for pass 1
1402 UBool isPatternValid
= (pass
==1);
1404 UnicodeSet
set(pat
, ec
);
1405 if (U_SUCCESS(ec
) != isPatternValid
){
1406 errln((UnicodeString
)"FAIL: applyPattern(" +
1407 escape(pat
) + ") => " +
1411 if (U_FAILURE(ec
)) {
1414 if (set
.contains((UChar
)0x0644)){
1415 errln((UnicodeString
)"FAIL: " + escape(pat
) + " contains(U+0664)");
1418 UnicodeString newpat
;
1419 set
.toPattern(newpat
, TRUE
);
1420 if (newpat
== UnicodeString(exp
, -1, US_INV
)) {
1421 logln(escape(pat
) + " => " + newpat
);
1423 errln((UnicodeString
)"FAIL: " + escape(pat
) + " => " + newpat
);
1426 for (int32_t i
=0; i
<set
.getRangeCount(); ++i
) {
1427 UnicodeString
str("Range ");
1428 str
.append((UChar
)(0x30 + i
))
1430 .append((UChar32
)set
.getRangeStart(i
))
1432 .append((UChar32
)set
.getRangeEnd(i
));
1433 str
= str
+ " (" + set
.getRangeStart(i
) + " - " +
1434 set
.getRangeEnd(i
) + ")";
1435 if (set
.getRangeStart(i
) < 0) {
1436 errln((UnicodeString
)"FAIL: " + escape(str
));
1444 void UnicodeSetTest::expectRange(const UnicodeString
& label
,
1445 const UnicodeSet
& set
,
1446 UChar32 start
, UChar32 end
) {
1447 UnicodeSet
exp(start
, end
);
1450 logln(label
+ " => " + set
.toPattern(pat
, TRUE
));
1453 errln((UnicodeString
)"FAIL: " + label
+ " => " +
1454 set
.toPattern(pat
, TRUE
) +
1455 ", expected " + exp
.toPattern(xpat
, TRUE
));
1459 void UnicodeSetTest::TestInvalidCodePoint() {
1461 const UChar32 DATA
[] = {
1462 // Test range Expected range
1463 0, 0x10FFFF, 0, 0x10FFFF,
1464 (UChar32
)-1, 8, 0, 8,
1465 8, 0x110000, 8, 0x10FFFF
1467 const int32_t DATA_LENGTH
= UPRV_LENGTHOF(DATA
);
1472 for (i
=0; i
<DATA_LENGTH
; i
+=4) {
1473 UChar32 start
= DATA
[i
];
1474 UChar32 end
= DATA
[i
+1];
1475 UChar32 xstart
= DATA
[i
+2];
1476 UChar32 xend
= DATA
[i
+3];
1478 // Try various API using the test code points
1480 UnicodeSet
set(start
, end
);
1481 expectRange((UnicodeString
)"ct(" + start
+ "," + end
+ ")",
1485 set
.set(start
, end
);
1486 expectRange((UnicodeString
)"set(" + start
+ "," + end
+ ")",
1489 UBool b
= set
.contains(start
);
1490 b
= set
.contains(start
, end
);
1491 b
= set
.containsNone(start
, end
);
1492 b
= set
.containsSome(start
, end
);
1493 (void)b
; // Suppress set but not used warning.
1495 /*int32_t index = set.indexOf(start);*/
1499 set
.add(start
, end
);
1500 expectRange((UnicodeString
)"add(" + start
+ "," + end
+ ")",
1503 set
.set(0, 0x10FFFF);
1504 set
.retain(start
, end
);
1505 expectRange((UnicodeString
)"retain(" + start
+ "," + end
+ ")",
1509 set
.set(0, 0x10FFFF);
1511 set
.remove(start
, end
);
1513 expectRange((UnicodeString
)"!remove(" + start
+ "," + end
+ ")",
1516 set
.set(0, 0x10FFFF);
1517 set
.complement(start
, end
);
1519 expectRange((UnicodeString
)"!complement(" + start
+ "," + end
+ ")",
1521 set
.complement(start
);
1524 const UChar32 DATA2
[] = {
1530 const int32_t DATA2_LENGTH
= UPRV_LENGTHOF(DATA2
);
1532 for (i
=0; i
<DATA2_LENGTH
; ++i
) {
1533 UChar32 c
= DATA2
[i
], end
= 0x10FFFF;
1534 UBool valid
= (c
>= 0 && c
<= 0x10FFFF);
1536 UnicodeSet
set(0, 0x10FFFF);
1538 // For single-codepoint contains, invalid codepoints are NOT contained
1539 UBool b
= set
.contains(c
);
1541 logln((UnicodeString
)"[\\u0000-\\U0010FFFF].contains(" + c
+
1544 errln((UnicodeString
)"FAIL: [\\u0000-\\U0010FFFF].contains(" + c
+
1548 // For codepoint range contains, containsNone, and containsSome,
1549 // invalid or empty (start > end) ranges have UNDEFINED behavior.
1550 b
= set
.contains(c
, end
);
1551 logln((UnicodeString
)"* [\\u0000-\\U0010FFFF].contains(" + c
+
1552 "," + end
+ ") = " + b
);
1554 b
= set
.containsNone(c
, end
);
1555 logln((UnicodeString
)"* [\\u0000-\\U0010FFFF].containsNone(" + c
+
1556 "," + end
+ ") = " + b
);
1558 b
= set
.containsSome(c
, end
);
1559 logln((UnicodeString
)"* [\\u0000-\\U0010FFFF].containsSome(" + c
+
1560 "," + end
+ ") = " + b
);
1562 int32_t index
= set
.indexOf(c
);
1563 if ((index
>= 0) == valid
) {
1564 logln((UnicodeString
)"[\\u0000-\\U0010FFFF].indexOf(" + c
+
1567 errln((UnicodeString
)"FAIL: [\\u0000-\\U0010FFFF].indexOf(" + c
+
1573 // Used by TestSymbolTable
1574 class TokenSymbolTable
: public SymbolTable
{
1578 TokenSymbolTable(UErrorCode
& ec
) : contents(FALSE
, ec
) {
1579 contents
.setValueDeleter(uprv_deleteUObject
);
1582 ~TokenSymbolTable() {}
1585 * (Non-SymbolTable API) Add the given variable and value to
1586 * the table. Variable should NOT contain leading '$'.
1588 void add(const UnicodeString
& var
, const UnicodeString
& value
,
1590 if (U_SUCCESS(ec
)) {
1591 contents
.put(var
, new UnicodeString(value
), ec
);
1598 virtual const UnicodeString
* lookup(const UnicodeString
& s
) const {
1599 return (const UnicodeString
*) contents
.get(s
);
1605 virtual const UnicodeFunctor
* lookupMatcher(UChar32
/*ch*/) const {
1612 virtual UnicodeString
parseReference(const UnicodeString
& text
,
1613 ParsePosition
& pos
, int32_t limit
) const {
1614 int32_t start
= pos
.getIndex();
1616 UnicodeString result
;
1618 UChar c
= text
.charAt(i
);
1619 if ((i
==start
&& !u_isIDStart(c
)) || !u_isIDPart(c
)) {
1624 if (i
== start
) { // No valid name chars
1625 return result
; // Indicate failure with empty string
1628 text
.extractBetween(start
, i
, result
);
1633 void UnicodeSetTest::TestSymbolTable() {
1634 // Multiple test cases can be set up here. Each test case
1635 // is terminated by null:
1636 // var, value, var, value,..., input pat., exp. output pat., null
1637 const char* DATA
[] = {
1638 "us", "a-z", "[0-1$us]", "[0-1a-z]", NULL
,
1639 "us", "[a-z]", "[0-1$us]", "[0-1[a-z]]", NULL
,
1640 "us", "\\[a\\-z\\]", "[0-1$us]", "[-01\\[\\]az]", NULL
,
1644 for (int32_t i
=0; DATA
[i
]!=NULL
; ++i
) {
1645 UErrorCode ec
= U_ZERO_ERROR
;
1646 TokenSymbolTable
sym(ec
);
1647 if (U_FAILURE(ec
)) {
1648 errln("FAIL: couldn't construct TokenSymbolTable");
1653 while (DATA
[i
+2] != NULL
) {
1654 sym
.add(UnicodeString(DATA
[i
], -1, US_INV
), UnicodeString(DATA
[i
+1], -1, US_INV
), ec
);
1655 if (U_FAILURE(ec
)) {
1656 errln("FAIL: couldn't add to TokenSymbolTable");
1662 // Input pattern and expected output pattern
1663 UnicodeString inpat
= UnicodeString(DATA
[i
], -1, US_INV
), exppat
= UnicodeString(DATA
[i
+1], -1, US_INV
);
1666 ParsePosition
pos(0);
1667 UnicodeSet
us(inpat
, pos
, USET_IGNORE_SPACE
, &sym
, ec
);
1668 if (U_FAILURE(ec
)) {
1669 errln("FAIL: couldn't construct UnicodeSet");
1674 if (pos
.getIndex() != inpat
.length()) {
1675 errln((UnicodeString
)"Failed to read to end of string \""
1676 + inpat
+ "\": read to "
1677 + pos
.getIndex() + ", length is "
1681 UnicodeSet
us2(exppat
, ec
);
1682 if (U_FAILURE(ec
)) {
1683 errln("FAIL: couldn't construct expected UnicodeSet");
1689 errln((UnicodeString
)"Failed, got " + us
.toPattern(a
, TRUE
) +
1690 ", expected " + us2
.toPattern(b
, TRUE
));
1692 logln((UnicodeString
)"Ok, got " + us
.toPattern(a
, TRUE
));
1697 void UnicodeSetTest::TestSurrogate() {
1698 const char* DATA
[] = {
1699 // These should all behave identically
1700 "[abc\\uD800\\uDC00]",
1701 // "[abc\uD800\uDC00]", // Can't do this on C -- only Java
1705 for (int i
=0; DATA
[i
] != 0; ++i
) {
1706 UErrorCode ec
= U_ZERO_ERROR
;
1707 logln((UnicodeString
)"Test pattern " + i
+ " :" + UnicodeString(DATA
[i
], -1, US_INV
));
1708 UnicodeString str
= UnicodeString(DATA
[i
], -1, US_INV
);
1709 UnicodeSet
set(str
, ec
);
1710 if (U_FAILURE(ec
)) {
1711 errln("FAIL: UnicodeSet constructor");
1714 expectContainment(set
,
1715 CharsToUnicodeString("abc\\U00010000"),
1716 CharsToUnicodeString("\\uD800;\\uDC00")); // split apart surrogate-pair
1717 if (set
.size() != 4) {
1718 errln((UnicodeString
)"FAIL: " + UnicodeString(DATA
[i
], -1, US_INV
) + ".size() == " +
1719 set
.size() + ", expected 4");
1723 UErrorCode subErr
= U_ZERO_ERROR
;
1724 checkRoundTrip(set
);
1725 checkSerializeRoundTrip(set
, subErr
);
1730 void UnicodeSetTest::TestExhaustive() {
1731 // exhaustive tests. Simulate UnicodeSets with integers.
1732 // That gives us very solid tests (except for large memory tests).
1734 int32_t limit
= 128;
1736 UnicodeSet x
, y
, z
, aa
;
1738 for (int32_t i
= 0; i
< limit
; ++i
) {
1740 logln((UnicodeString
)"Testing " + i
+ ", " + x
);
1741 _testComplement(i
, x
, y
);
1743 UnicodeSet
&toTest
= bitsToSet(i
, aa
);
1745 // AS LONG AS WE ARE HERE, check roundtrip
1746 checkRoundTrip(toTest
);
1747 UErrorCode ec
= U_ZERO_ERROR
;
1748 checkSerializeRoundTrip(toTest
, ec
);
1750 for (int32_t j
= 0; j
< limit
; ++j
) {
1751 _testAdd(i
,j
, x
,y
,z
);
1752 _testXor(i
,j
, x
,y
,z
);
1753 _testRetain(i
,j
, x
,y
,z
);
1754 _testRemove(i
,j
, x
,y
,z
);
1759 void UnicodeSetTest::_testComplement(int32_t a
, UnicodeSet
& x
, UnicodeSet
& z
) {
1763 int32_t c
= setToBits(z
);
1765 errln((UnicodeString
)"FAILED: add: ~" + x
+ " != " + z
);
1766 errln((UnicodeString
)"FAILED: add: ~" + a
+ " != " + c
);
1768 checkCanonicalRep(z
, (UnicodeString
)"complement " + a
);
1771 void UnicodeSetTest::_testAdd(int32_t a
, int32_t b
, UnicodeSet
& x
, UnicodeSet
& y
, UnicodeSet
& z
) {
1776 int32_t c
= setToBits(z
);
1778 errln((UnicodeString
)"FAILED: add: " + x
+ " | " + y
+ " != " + z
);
1779 errln((UnicodeString
)"FAILED: add: " + a
+ " | " + b
+ " != " + c
);
1781 checkCanonicalRep(z
, (UnicodeString
)"add " + a
+ "," + b
);
1784 void UnicodeSetTest::_testRetain(int32_t a
, int32_t b
, UnicodeSet
& x
, UnicodeSet
& y
, UnicodeSet
& z
) {
1789 int32_t c
= setToBits(z
);
1791 errln((UnicodeString
)"FAILED: retain: " + x
+ " & " + y
+ " != " + z
);
1792 errln((UnicodeString
)"FAILED: retain: " + a
+ " & " + b
+ " != " + c
);
1794 checkCanonicalRep(z
, (UnicodeString
)"retain " + a
+ "," + b
);
1797 void UnicodeSetTest::_testRemove(int32_t a
, int32_t b
, UnicodeSet
& x
, UnicodeSet
& y
, UnicodeSet
& z
) {
1802 int32_t c
= setToBits(z
);
1803 if (c
!= (a
&~ b
)) {
1804 errln((UnicodeString
)"FAILED: remove: " + x
+ " &~ " + y
+ " != " + z
);
1805 errln((UnicodeString
)"FAILED: remove: " + a
+ " &~ " + b
+ " != " + c
);
1807 checkCanonicalRep(z
, (UnicodeString
)"remove " + a
+ "," + b
);
1810 void UnicodeSetTest::_testXor(int32_t a
, int32_t b
, UnicodeSet
& x
, UnicodeSet
& y
, UnicodeSet
& z
) {
1815 int32_t c
= setToBits(z
);
1817 errln((UnicodeString
)"FAILED: complement: " + x
+ " ^ " + y
+ " != " + z
);
1818 errln((UnicodeString
)"FAILED: complement: " + a
+ " ^ " + b
+ " != " + c
);
1820 checkCanonicalRep(z
, (UnicodeString
)"complement " + a
+ "," + b
);
1824 * Check that ranges are monotonically increasing and non-
1827 void UnicodeSetTest::checkCanonicalRep(const UnicodeSet
& set
, const UnicodeString
& msg
) {
1828 int32_t n
= set
.getRangeCount();
1830 errln((UnicodeString
)"FAIL result of " + msg
+
1831 ": range count should be >= 0 but is " +
1832 n
/*+ " for " + set.toPattern())*/);
1836 for (int32_t i
=0; i
<n
; ++i
) {
1837 UChar32 start
= set
.getRangeStart(i
);
1838 UChar32 end
= set
.getRangeEnd(i
);
1840 errln((UnicodeString
)"FAIL result of " + msg
+
1841 ": range " + (i
+1) +
1842 " start > end: " + (int)start
+ ", " + (int)end
+
1845 if (i
> 0 && start
<= last
) {
1846 errln((UnicodeString
)"FAIL result of " + msg
+
1847 ": range " + (i
+1) +
1848 " overlaps previous range: " + (int)start
+ ", " + (int)end
+
1856 * Convert a bitmask to a UnicodeSet.
1858 UnicodeSet
& UnicodeSetTest::bitsToSet(int32_t a
, UnicodeSet
& result
) {
1860 for (UChar32 i
= 0; i
< 32; ++i
) {
1861 if ((a
& (1<<i
)) != 0) {
1869 * Convert a UnicodeSet to a bitmask. Only the characters
1870 * U+0000 to U+0020 are represented in the bitmask.
1872 int32_t UnicodeSetTest::setToBits(const UnicodeSet
& x
) {
1874 for (int32_t i
= 0; i
< 32; ++i
) {
1875 if (x
.contains((UChar32
)i
)) {
1883 * Return the representation of an inversion list based UnicodeSet
1884 * as a pairs list. Ranges are listed in ascending Unicode order.
1885 * For example, the set [a-zA-M3] is represented as "33AMaz".
1887 UnicodeString
UnicodeSetTest::getPairs(const UnicodeSet
& set
) {
1888 UnicodeString pairs
;
1889 for (int32_t i
=0; i
<set
.getRangeCount(); ++i
) {
1890 UChar32 start
= set
.getRangeStart(i
);
1891 UChar32 end
= set
.getRangeEnd(i
);
1894 i
= set
.getRangeCount(); // Should be unnecessary
1896 pairs
.append((UChar
)start
).append((UChar
)end
);
1902 * Basic consistency check for a few items.
1903 * That the iterator works, and that we can create a pattern and
1904 * get the same thing back
1906 void UnicodeSetTest::checkRoundTrip(const UnicodeSet
& s
) {
1909 checkEqual(s
, t
, "copy ct");
1913 UnicodeSet
t(0xabcd, 0xdef0); // dummy contents should be overwritten
1915 checkEqual(s
, t
, "operator=");
1920 copyWithIterator(t
, s
, FALSE
);
1921 checkEqual(s
, t
, "iterator roundtrip");
1926 copyWithIterator(t
, s
, TRUE
); // try range
1927 checkEqual(s
, t
, "iterator roundtrip");
1933 UErrorCode ec
= U_ZERO_ERROR
;
1934 s
.toPattern(pat
, FALSE
);
1935 t
.applyPattern(pat
, ec
);
1936 if (U_FAILURE(ec
)) {
1937 errln("FAIL: toPattern(escapeUnprintable=FALSE), applyPattern - %s", u_errorName(ec
));
1940 checkEqual(s
, t
, "toPattern(false)");
1947 UErrorCode ec
= U_ZERO_ERROR
;
1948 s
.toPattern(pat
, TRUE
);
1949 t
.applyPattern(pat
, ec
);
1950 if (U_FAILURE(ec
)) {
1951 errln("FAIL: toPattern(escapeUnprintable=TRUE), applyPattern - %s", u_errorName(ec
));
1954 checkEqual(s
, t
, "toPattern(true)");
1959 void UnicodeSetTest::checkSerializeRoundTrip(const UnicodeSet
& t
, UErrorCode
&status
) {
1960 if(U_FAILURE(status
)) return;
1961 int32_t len
= t
.serialize(serializeBuffer
.getAlias(), serializeBuffer
.getCapacity(), status
);
1962 if(status
== U_BUFFER_OVERFLOW_ERROR
) {
1963 status
= U_ZERO_ERROR
;
1964 serializeBuffer
.resize(len
);
1965 len
= t
.serialize(serializeBuffer
.getAlias(), serializeBuffer
.getCapacity(), status
);
1966 // let 2nd error stand
1968 if(U_FAILURE(status
)) {
1969 errln("checkSerializeRoundTrip: error %s serializing buffer\n", u_errorName(status
));
1972 UnicodeSet
deserialized(serializeBuffer
.getAlias(), len
, UnicodeSet::kSerialized
, status
);
1973 if(U_FAILURE(status
)) {
1974 errln("checkSerializeRoundTrip: error %s deserializing buffer: buf %p len %d, original %d\n", u_errorName(status
), serializeBuffer
.getAlias(), len
, t
.getRangeCount());
1978 checkEqual(t
, deserialized
, "Set was unequal when deserialized");
1981 void UnicodeSetTest::copyWithIterator(UnicodeSet
& t
, const UnicodeSet
& s
, UBool withRange
) {
1983 UnicodeSetIterator
it(s
);
1985 while (it
.nextRange()) {
1986 if (it
.isString()) {
1987 t
.add(it
.getString());
1989 t
.add(it
.getCodepoint(), it
.getCodepointEnd());
1994 if (it
.isString()) {
1995 t
.add(it
.getString());
1997 t
.add(it
.getCodepoint());
2003 UBool
UnicodeSetTest::checkEqual(const UnicodeSet
& s
, const UnicodeSet
& t
, const char* message
) {
2004 assertEquals(UnicodeString("RangeCount: ","") + message
, s
.getRangeCount(), t
.getRangeCount());
2005 assertEquals(UnicodeString("size: ","") + message
, s
.size(), t
.size());
2006 UnicodeString source
; s
.toPattern(source
, TRUE
);
2007 UnicodeString result
; t
.toPattern(result
, TRUE
);
2009 errln((UnicodeString
)"FAIL: " + message
2010 + "; source = " + source
2011 + "; result = " + result
2015 logln((UnicodeString
)"Ok: " + message
2016 + "; source = " + source
2017 + "; result = " + result
2024 UnicodeSetTest::expectContainment(const UnicodeString
& pat
,
2025 const UnicodeString
& charsIn
,
2026 const UnicodeString
& charsOut
) {
2027 UErrorCode ec
= U_ZERO_ERROR
;
2028 UnicodeSet
set(pat
, ec
);
2029 if (U_FAILURE(ec
)) {
2030 dataerrln((UnicodeString
)"FAIL: pattern \"" +
2031 pat
+ "\" => " + u_errorName(ec
));
2034 expectContainment(set
, pat
, charsIn
, charsOut
);
2038 UnicodeSetTest::expectContainment(const UnicodeSet
& set
,
2039 const UnicodeString
& charsIn
,
2040 const UnicodeString
& charsOut
) {
2043 expectContainment(set
, pat
, charsIn
, charsOut
);
2047 UnicodeSetTest::expectContainment(const UnicodeSet
& set
,
2048 const UnicodeString
& setName
,
2049 const UnicodeString
& charsIn
,
2050 const UnicodeString
& charsOut
) {
2055 for (i
=0; i
<charsIn
.length(); i
+=U16_LENGTH(c
)) {
2056 c
= charsIn
.char32At(i
);
2057 if (!set
.contains(c
)) {
2061 if (bad
.length() > 0) {
2062 errln((UnicodeString
)"Fail: set " + setName
+ " does not contain " + prettify(bad
) +
2063 ", expected containment of " + prettify(charsIn
));
2065 logln((UnicodeString
)"Ok: set " + setName
+ " contains " + prettify(charsIn
));
2069 for (i
=0; i
<charsOut
.length(); i
+=U16_LENGTH(c
)) {
2070 c
= charsOut
.char32At(i
);
2071 if (set
.contains(c
)) {
2075 if (bad
.length() > 0) {
2076 errln((UnicodeString
)"Fail: set " + setName
+ " contains " + prettify(bad
) +
2077 ", expected non-containment of " + prettify(charsOut
));
2079 logln((UnicodeString
)"Ok: set " + setName
+ " does not contain " + prettify(charsOut
));
2084 UnicodeSetTest::expectPattern(UnicodeSet
& set
,
2085 const UnicodeString
& pattern
,
2086 const UnicodeString
& expectedPairs
){
2087 UErrorCode status
= U_ZERO_ERROR
;
2088 set
.applyPattern(pattern
, status
);
2089 if (U_FAILURE(status
)) {
2090 errln(UnicodeString("FAIL: applyPattern(\"") + pattern
+
2094 if (getPairs(set
) != expectedPairs
) {
2095 errln(UnicodeString("FAIL: applyPattern(\"") + pattern
+
2097 escape(getPairs(set
)) + "\", expected \"" +
2098 escape(expectedPairs
) + "\"");
2100 logln(UnicodeString("Ok: applyPattern(\"") + pattern
+
2102 escape(getPairs(set
)) + "\"");
2105 // the result of calling set.toPattern(), which is the string representation of
2106 // this set(set), is passed to a UnicodeSet constructor, and tested that it
2107 // will produce another set that is equal to this one.
2108 UnicodeString temppattern
;
2109 set
.toPattern(temppattern
);
2110 UnicodeSet
*tempset
=new UnicodeSet(temppattern
, status
);
2111 if (U_FAILURE(status
)) {
2112 errln(UnicodeString("FAIL: applyPattern(\""+ pattern
+ "\").toPattern() => " + temppattern
+ " => invalid pattern"));
2115 if(*tempset
!= set
|| getPairs(*tempset
) != getPairs(set
)){
2116 errln(UnicodeString("FAIL: applyPattern(\""+ pattern
+ "\").toPattern() => " + temppattern
+ " => pairs \""+ escape(getPairs(*tempset
)) + "\", expected pairs \"" +
2117 escape(getPairs(set
)) + "\""));
2119 logln(UnicodeString("Ok: applyPattern(\""+ pattern
+ "\").toPattern() => " + temppattern
+ " => pairs \"" + escape(getPairs(*tempset
)) + "\""));
2127 UnicodeSetTest::expectPairs(const UnicodeSet
& set
, const UnicodeString
& expectedPairs
) {
2128 if (getPairs(set
) != expectedPairs
) {
2129 errln(UnicodeString("FAIL: Expected pair list \"") +
2130 escape(expectedPairs
) + "\", got \"" +
2131 escape(getPairs(set
)) + "\"");
2135 void UnicodeSetTest::expectToPattern(const UnicodeSet
& set
,
2136 const UnicodeString
& expPat
,
2137 const char** expStrings
) {
2139 set
.toPattern(pat
, TRUE
);
2140 if (pat
== expPat
) {
2141 logln((UnicodeString
)"Ok: toPattern() => \"" + pat
+ "\"");
2143 errln((UnicodeString
)"FAIL: toPattern() => \"" + pat
+ "\", expected \"" + expPat
+ "\"");
2146 if (expStrings
== NULL
) {
2150 for (int32_t i
=0; expStrings
[i
] != NULL
; ++i
) {
2151 if (expStrings
[i
] == NOT
) { // sic; pointer comparison
2155 UnicodeString s
= CharsToUnicodeString(expStrings
[i
]);
2156 UBool contained
= set
.contains(s
);
2157 if (contained
== in
) {
2158 logln((UnicodeString
)"Ok: " + expPat
+
2159 (contained
? " contains {" : " does not contain {") +
2160 escape(expStrings
[i
]) + "}");
2162 errln((UnicodeString
)"FAIL: " + expPat
+
2163 (contained
? " contains {" : " does not contain {") +
2164 escape(expStrings
[i
]) + "}");
2169 static UChar
toHexString(int32_t i
) { return (UChar
)(i
+ (i
< 10 ? 0x30 : (0x41 - 10))); }
2172 UnicodeSetTest::doAssert(UBool condition
, const char *message
)
2175 errln(UnicodeString("ERROR : ") + message
);
2180 UnicodeSetTest::escape(const UnicodeString
& s
) {
2182 for (int32_t i
=0; i
<s
.length(); )
2184 UChar32 c
= s
.char32At(i
);
2185 if (0x0020 <= c
&& c
<= 0x007F) {
2189 buf
+= (UChar
)0x5c; buf
+= (UChar
)0x75;
2191 buf
+= (UChar
)0x5c; buf
+= (UChar
)0x55;
2192 buf
+= toHexString((c
& 0xF0000000) >> 28);
2193 buf
+= toHexString((c
& 0x0F000000) >> 24);
2194 buf
+= toHexString((c
& 0x00F00000) >> 20);
2195 buf
+= toHexString((c
& 0x000F0000) >> 16);
2197 buf
+= toHexString((c
& 0xF000) >> 12);
2198 buf
+= toHexString((c
& 0x0F00) >> 8);
2199 buf
+= toHexString((c
& 0x00F0) >> 4);
2200 buf
+= toHexString(c
& 0x000F);
2207 void UnicodeSetTest::TestFreezable() {
2208 UErrorCode errorCode
=U_ZERO_ERROR
;
2209 UnicodeString idPattern
=UNICODE_STRING("[:ID_Continue:]", 15);
2210 UnicodeSet
idSet(idPattern
, errorCode
);
2211 if(U_FAILURE(errorCode
)) {
2212 dataerrln("FAIL: unable to create UnicodeSet([:ID_Continue:]) - %s", u_errorName(errorCode
));
2216 UnicodeString wsPattern
=UNICODE_STRING("[:White_Space:]", 15);
2217 UnicodeSet
wsSet(wsPattern
, errorCode
);
2218 if(U_FAILURE(errorCode
)) {
2219 dataerrln("FAIL: unable to create UnicodeSet([:White_Space:]) - %s", u_errorName(errorCode
));
2223 idSet
.add(idPattern
);
2224 UnicodeSet
frozen(idSet
);
2227 if(idSet
.isFrozen() || !frozen
.isFrozen()) {
2228 errln("FAIL: isFrozen() is wrong");
2230 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2231 errln("FAIL: a copy-constructed frozen set differs from its original");
2235 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2236 errln("FAIL: a frozen set was modified by operator=");
2239 UnicodeSet
frozen2(frozen
);
2240 if(frozen2
!=frozen
|| frozen2
!=idSet
) {
2241 errln("FAIL: a copied frozen set differs from its frozen original");
2243 if(!frozen2
.isFrozen()) {
2244 errln("FAIL: copy-constructing a frozen set results in a thawed one");
2246 UnicodeSet
frozen3(5, 55); // Set to some values to really test assignment below, not copy construction.
2247 if(frozen3
.contains(0, 4) || !frozen3
.contains(5, 55) || frozen3
.contains(56, 0x10ffff)) {
2248 errln("FAIL: UnicodeSet(5, 55) failed");
2251 if(!frozen3
.isFrozen()) {
2252 errln("FAIL: copying a frozen set results in a thawed one");
2255 UnicodeSet
*cloned
=(UnicodeSet
*)frozen
.clone();
2256 if(!cloned
->isFrozen() || *cloned
!=frozen
|| cloned
->containsSome(0xd802, 0xd805)) {
2257 errln("FAIL: clone() failed");
2259 cloned
->add(0xd802, 0xd805);
2260 if(cloned
->containsSome(0xd802, 0xd805)) {
2261 errln("FAIL: unable to modify clone");
2265 UnicodeSet
*thawed
=(UnicodeSet
*)frozen
.cloneAsThawed();
2266 if(thawed
->isFrozen() || *thawed
!=frozen
|| thawed
->containsSome(0xd802, 0xd805)) {
2267 errln("FAIL: cloneAsThawed() failed");
2269 thawed
->add(0xd802, 0xd805);
2270 if(!thawed
->contains(0xd802, 0xd805)) {
2271 errln("FAIL: unable to modify thawed clone");
2276 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2277 errln("FAIL: UnicodeSet::set() modified a frozen set");
2281 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2282 errln("FAIL: UnicodeSet::clear() modified a frozen set");
2285 frozen
.closeOver(USET_CASE_INSENSITIVE
);
2286 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2287 errln("FAIL: UnicodeSet::closeOver() modified a frozen set");
2291 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2292 errln("FAIL: UnicodeSet::compact() modified a frozen set");
2297 applyPattern(wsPattern
, errorCode
).
2298 applyPattern(wsPattern
, USET_IGNORE_SPACE
, NULL
, errorCode
).
2299 applyPattern(wsPattern
, pos
, USET_IGNORE_SPACE
, NULL
, errorCode
).
2300 applyIntPropertyValue(UCHAR_CANONICAL_COMBINING_CLASS
, 230, errorCode
).
2301 applyPropertyAlias(UNICODE_STRING_SIMPLE("Assigned"), UnicodeString(), errorCode
);
2302 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2303 errln("FAIL: UnicodeSet::applyXYZ() modified a frozen set");
2308 add(0xd802, 0xd805).
2312 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2313 errln("FAIL: UnicodeSet::addXYZ() modified a frozen set");
2319 retainAll(wsPattern
).
2321 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2322 errln("FAIL: UnicodeSet::retainXYZ() modified a frozen set");
2329 removeAll(idPattern
).
2331 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2332 errln("FAIL: UnicodeSet::removeXYZ() modified a frozen set");
2338 complement(0x64, 0x69).
2339 complement(idPattern
).
2340 complementAll(idPattern
).
2341 complementAll(idSet
);
2342 if(frozen
!=idSet
|| !(frozen
==idSet
)) {
2343 errln("FAIL: UnicodeSet::complementXYZ() modified a frozen set");
2347 // Test span() etc. -------------------------------------------------------- ***
2349 // Append the UTF-8 version of the string to t and return the appended UTF-8 length.
2351 appendUTF8(const UChar
*s
, int32_t length
, char *t
, int32_t capacity
) {
2352 UErrorCode errorCode
=U_ZERO_ERROR
;
2354 u_strToUTF8(t
, capacity
, &length8
, s
, length
, &errorCode
);
2355 if(U_SUCCESS(errorCode
)) {
2358 // The string contains an unpaired surrogate.
2359 // Ignore this string.
2364 class UnicodeSetWithStringsIterator
;
2366 // Make the strings in a UnicodeSet easily accessible.
2367 class UnicodeSetWithStrings
{
2369 UnicodeSetWithStrings(const UnicodeSet
&normalSet
) :
2370 set(normalSet
), stringsLength(0), hasSurrogates(FALSE
) {
2371 int32_t size
=set
.size();
2372 if(size
>0 && set
.charAt(size
-1)<0) {
2373 // If a set's last element is not a code point, then it must contain strings.
2374 // Iterate over the set, skip all code point ranges, and cache the strings.
2375 // Convert them to UTF-8 for spanUTF8().
2376 UnicodeSetIterator
iter(set
);
2377 const UnicodeString
*s
;
2379 int32_t length8
, utf8Count
=0;
2380 while(iter
.nextRange() && stringsLength
<UPRV_LENGTHOF(strings
)) {
2381 if(iter
.isString()) {
2382 // Store the pointer to the set's string element
2383 // which we happen to know is a stable pointer.
2384 strings
[stringsLength
]=s
=&iter
.getString();
2386 utf8Lengths
[stringsLength
]=length8
=
2387 appendUTF8(s
->getBuffer(), s
->length(),
2388 s8
, (int32_t)(sizeof(utf8
)-utf8Count
));
2390 hasSurrogates
=TRUE
; // Contains unpaired surrogates.
2399 const UnicodeSet
&getSet() const {
2403 UBool
hasStrings() const {
2404 return (UBool
)(stringsLength
>0);
2407 UBool
hasStringsWithSurrogates() const {
2408 return hasSurrogates
;
2412 friend class UnicodeSetWithStringsIterator
;
2414 const UnicodeSet
&set
;
2416 const UnicodeString
*strings
[20];
2417 int32_t stringsLength
;
2418 UBool hasSurrogates
;
2421 int32_t utf8Lengths
[20];
2424 class UnicodeSetWithStringsIterator
{
2426 UnicodeSetWithStringsIterator(const UnicodeSetWithStrings
&set
) :
2427 fSet(set
), nextStringIndex(0), nextUTF8Start(0) {
2431 nextStringIndex
=nextUTF8Start
=0;
2434 const UnicodeString
*nextString() {
2435 if(nextStringIndex
<fSet
.stringsLength
) {
2436 return fSet
.strings
[nextStringIndex
++];
2442 // Do not mix with calls to nextString().
2443 const char *nextUTF8(int32_t &length
) {
2444 if(nextStringIndex
<fSet
.stringsLength
) {
2445 const char *s8
=fSet
.utf8
+nextUTF8Start
;
2446 nextUTF8Start
+=length
=fSet
.utf8Lengths
[nextStringIndex
++];
2455 const UnicodeSetWithStrings
&fSet
;
2456 int32_t nextStringIndex
;
2457 int32_t nextUTF8Start
;
2460 // Compare 16-bit Unicode strings (which may be malformed UTF-16)
2461 // at code point boundaries.
2462 // That is, each edge of a match must not be in the middle of a surrogate pair.
2464 matches16CPB(const UChar
*s
, int32_t start
, int32_t limit
, const UnicodeString
&t
) {
2467 int32_t length
=t
.length();
2468 return 0==t
.compare(s
, length
) &&
2469 !(0<start
&& U16_IS_LEAD(s
[-1]) && U16_IS_TRAIL(s
[0])) &&
2470 !(length
<limit
&& U16_IS_LEAD(s
[length
-1]) && U16_IS_TRAIL(s
[length
]));
2473 // Implement span() with contains() for comparison.
2474 static int32_t containsSpanUTF16(const UnicodeSetWithStrings
&set
, const UChar
*s
, int32_t length
,
2475 USetSpanCondition spanCondition
) {
2476 const UnicodeSet
&realSet(set
.getSet());
2477 if(!set
.hasStrings()) {
2478 if(spanCondition
!=USET_SPAN_NOT_CONTAINED
) {
2479 spanCondition
=USET_SPAN_CONTAINED
; // Pin to 0/1 values.
2483 int32_t start
=0, prev
;
2484 while((prev
=start
)<length
) {
2485 U16_NEXT(s
, start
, length
, c
);
2486 if(realSet
.contains(c
)!=spanCondition
) {
2491 } else if(spanCondition
==USET_SPAN_NOT_CONTAINED
) {
2492 UnicodeSetWithStringsIterator
iter(set
);
2494 int32_t start
, next
;
2495 for(start
=next
=0; start
<length
;) {
2496 U16_NEXT(s
, next
, length
, c
);
2497 if(realSet
.contains(c
)) {
2500 const UnicodeString
*str
;
2502 while((str
=iter
.nextString())!=NULL
) {
2503 if(str
->length()<=(length
-start
) && matches16CPB(s
, start
, length
, *str
)) {
2504 // spanNeedsStrings=TRUE;
2511 } else /* USET_SPAN_CONTAINED or USET_SPAN_SIMPLE */ {
2512 UnicodeSetWithStringsIterator
iter(set
);
2514 int32_t start
, next
, maxSpanLimit
=0;
2515 for(start
=next
=0; start
<length
;) {
2516 U16_NEXT(s
, next
, length
, c
);
2517 if(!realSet
.contains(c
)) {
2518 next
=start
; // Do not span this single, not-contained code point.
2520 const UnicodeString
*str
;
2522 while((str
=iter
.nextString())!=NULL
) {
2523 if(str
->length()<=(length
-start
) && matches16CPB(s
, start
, length
, *str
)) {
2524 // spanNeedsStrings=TRUE;
2525 int32_t matchLimit
=start
+str
->length();
2526 if(matchLimit
==length
) {
2529 if(spanCondition
==USET_SPAN_CONTAINED
) {
2530 // Iterate for the shortest match at each position.
2531 // Recurse for each but the shortest match.
2533 next
=matchLimit
; // First match from start.
2535 if(matchLimit
<next
) {
2536 // Remember shortest match from start for iteration.
2541 // Recurse for non-shortest match from start.
2542 int32_t spanLength
=containsSpanUTF16(set
, s
+matchLimit
, length
-matchLimit
,
2543 USET_SPAN_CONTAINED
);
2544 if((matchLimit
+spanLength
)>maxSpanLimit
) {
2545 maxSpanLimit
=matchLimit
+spanLength
;
2546 if(maxSpanLimit
==length
) {
2551 } else /* spanCondition==USET_SPAN_SIMPLE */ {
2552 if(matchLimit
>next
) {
2553 // Remember longest match from start.
2560 break; // No match from start.
2564 if(start
>maxSpanLimit
) {
2567 return maxSpanLimit
;
2572 static int32_t containsSpanBackUTF16(const UnicodeSetWithStrings
&set
, const UChar
*s
, int32_t length
,
2573 USetSpanCondition spanCondition
) {
2577 const UnicodeSet
&realSet(set
.getSet());
2578 if(!set
.hasStrings()) {
2579 if(spanCondition
!=USET_SPAN_NOT_CONTAINED
) {
2580 spanCondition
=USET_SPAN_CONTAINED
; // Pin to 0/1 values.
2584 int32_t prev
=length
;
2586 U16_PREV(s
, 0, length
, c
);
2587 if(realSet
.contains(c
)!=spanCondition
) {
2590 } while((prev
=length
)>0);
2592 } else if(spanCondition
==USET_SPAN_NOT_CONTAINED
) {
2593 UnicodeSetWithStringsIterator
iter(set
);
2595 int32_t prev
=length
, length0
=length
;
2597 U16_PREV(s
, 0, length
, c
);
2598 if(realSet
.contains(c
)) {
2601 const UnicodeString
*str
;
2603 while((str
=iter
.nextString())!=NULL
) {
2604 if(str
->length()<=prev
&& matches16CPB(s
, prev
-str
->length(), length0
, *str
)) {
2605 // spanNeedsStrings=TRUE;
2609 } while((prev
=length
)>0);
2611 } else /* USET_SPAN_CONTAINED or USET_SPAN_SIMPLE */ {
2612 UnicodeSetWithStringsIterator
iter(set
);
2614 int32_t prev
=length
, minSpanStart
=length
, length0
=length
;
2616 U16_PREV(s
, 0, length
, c
);
2617 if(!realSet
.contains(c
)) {
2618 length
=prev
; // Do not span this single, not-contained code point.
2620 const UnicodeString
*str
;
2622 while((str
=iter
.nextString())!=NULL
) {
2623 if(str
->length()<=prev
&& matches16CPB(s
, prev
-str
->length(), length0
, *str
)) {
2624 // spanNeedsStrings=TRUE;
2625 int32_t matchStart
=prev
-str
->length();
2629 if(spanCondition
==USET_SPAN_CONTAINED
) {
2630 // Iterate for the shortest match at each position.
2631 // Recurse for each but the shortest match.
2633 length
=matchStart
; // First match from prev.
2635 if(matchStart
>length
) {
2636 // Remember shortest match from prev for iteration.
2637 int32_t temp
=length
;
2641 // Recurse for non-shortest match from prev.
2642 int32_t spanStart
=containsSpanBackUTF16(set
, s
, matchStart
,
2643 USET_SPAN_CONTAINED
);
2644 if(spanStart
<minSpanStart
) {
2645 minSpanStart
=spanStart
;
2646 if(minSpanStart
==0) {
2651 } else /* spanCondition==USET_SPAN_SIMPLE */ {
2652 if(matchStart
<length
) {
2653 // Remember longest match from prev.
2660 break; // No match from prev.
2662 } while((prev
=length
)>0);
2663 if(prev
<minSpanStart
) {
2666 return minSpanStart
;
2671 static int32_t containsSpanUTF8(const UnicodeSetWithStrings
&set
, const char *s
, int32_t length
,
2672 USetSpanCondition spanCondition
) {
2673 const UnicodeSet
&realSet(set
.getSet());
2674 if(!set
.hasStrings()) {
2675 if(spanCondition
!=USET_SPAN_NOT_CONTAINED
) {
2676 spanCondition
=USET_SPAN_CONTAINED
; // Pin to 0/1 values.
2680 int32_t start
=0, prev
;
2681 while((prev
=start
)<length
) {
2682 U8_NEXT_OR_FFFD(s
, start
, length
, c
);
2683 if(realSet
.contains(c
)!=spanCondition
) {
2688 } else if(spanCondition
==USET_SPAN_NOT_CONTAINED
) {
2689 UnicodeSetWithStringsIterator
iter(set
);
2691 int32_t start
, next
;
2692 for(start
=next
=0; start
<length
;) {
2693 U8_NEXT_OR_FFFD(s
, next
, length
, c
);
2694 if(realSet
.contains(c
)) {
2700 while((s8
=iter
.nextUTF8(length8
))!=NULL
) {
2701 if(length8
!=0 && length8
<=(length
-start
) && 0==memcmp(s
+start
, s8
, length8
)) {
2702 // spanNeedsStrings=TRUE;
2709 } else /* USET_SPAN_CONTAINED or USET_SPAN_SIMPLE */ {
2710 UnicodeSetWithStringsIterator
iter(set
);
2712 int32_t start
, next
, maxSpanLimit
=0;
2713 for(start
=next
=0; start
<length
;) {
2714 U8_NEXT_OR_FFFD(s
, next
, length
, c
);
2715 if(!realSet
.contains(c
)) {
2716 next
=start
; // Do not span this single, not-contained code point.
2721 while((s8
=iter
.nextUTF8(length8
))!=NULL
) {
2722 if(length8
!=0 && length8
<=(length
-start
) && 0==memcmp(s
+start
, s8
, length8
)) {
2723 // spanNeedsStrings=TRUE;
2724 int32_t matchLimit
=start
+length8
;
2725 if(matchLimit
==length
) {
2728 if(spanCondition
==USET_SPAN_CONTAINED
) {
2729 // Iterate for the shortest match at each position.
2730 // Recurse for each but the shortest match.
2732 next
=matchLimit
; // First match from start.
2734 if(matchLimit
<next
) {
2735 // Remember shortest match from start for iteration.
2740 // Recurse for non-shortest match from start.
2741 int32_t spanLength
=containsSpanUTF8(set
, s
+matchLimit
, length
-matchLimit
,
2742 USET_SPAN_CONTAINED
);
2743 if((matchLimit
+spanLength
)>maxSpanLimit
) {
2744 maxSpanLimit
=matchLimit
+spanLength
;
2745 if(maxSpanLimit
==length
) {
2750 } else /* spanCondition==USET_SPAN_SIMPLE */ {
2751 if(matchLimit
>next
) {
2752 // Remember longest match from start.
2759 break; // No match from start.
2763 if(start
>maxSpanLimit
) {
2766 return maxSpanLimit
;
2771 static int32_t containsSpanBackUTF8(const UnicodeSetWithStrings
&set
, const char *s
, int32_t length
,
2772 USetSpanCondition spanCondition
) {
2776 const UnicodeSet
&realSet(set
.getSet());
2777 if(!set
.hasStrings()) {
2778 if(spanCondition
!=USET_SPAN_NOT_CONTAINED
) {
2779 spanCondition
=USET_SPAN_CONTAINED
; // Pin to 0/1 values.
2783 int32_t prev
=length
;
2785 U8_PREV_OR_FFFD(s
, 0, length
, c
);
2786 if(realSet
.contains(c
)!=spanCondition
) {
2789 } while((prev
=length
)>0);
2791 } else if(spanCondition
==USET_SPAN_NOT_CONTAINED
) {
2792 UnicodeSetWithStringsIterator
iter(set
);
2794 int32_t prev
=length
;
2796 U8_PREV_OR_FFFD(s
, 0, length
, c
);
2797 if(realSet
.contains(c
)) {
2803 while((s8
=iter
.nextUTF8(length8
))!=NULL
) {
2804 if(length8
!=0 && length8
<=prev
&& 0==memcmp(s
+prev
-length8
, s8
, length8
)) {
2805 // spanNeedsStrings=TRUE;
2809 } while((prev
=length
)>0);
2811 } else /* USET_SPAN_CONTAINED or USET_SPAN_SIMPLE */ {
2812 UnicodeSetWithStringsIterator
iter(set
);
2814 int32_t prev
=length
, minSpanStart
=length
;
2816 U8_PREV_OR_FFFD(s
, 0, length
, c
);
2817 if(!realSet
.contains(c
)) {
2818 length
=prev
; // Do not span this single, not-contained code point.
2823 while((s8
=iter
.nextUTF8(length8
))!=NULL
) {
2824 if(length8
!=0 && length8
<=prev
&& 0==memcmp(s
+prev
-length8
, s8
, length8
)) {
2825 // spanNeedsStrings=TRUE;
2826 int32_t matchStart
=prev
-length8
;
2830 if(spanCondition
==USET_SPAN_CONTAINED
) {
2831 // Iterate for the shortest match at each position.
2832 // Recurse for each but the shortest match.
2834 length
=matchStart
; // First match from prev.
2836 if(matchStart
>length
) {
2837 // Remember shortest match from prev for iteration.
2838 int32_t temp
=length
;
2842 // Recurse for non-shortest match from prev.
2843 int32_t spanStart
=containsSpanBackUTF8(set
, s
, matchStart
,
2844 USET_SPAN_CONTAINED
);
2845 if(spanStart
<minSpanStart
) {
2846 minSpanStart
=spanStart
;
2847 if(minSpanStart
==0) {
2852 } else /* spanCondition==USET_SPAN_SIMPLE */ {
2853 if(matchStart
<length
) {
2854 // Remember longest match from prev.
2861 break; // No match from prev.
2863 } while((prev
=length
)>0);
2864 if(prev
<minSpanStart
) {
2867 return minSpanStart
;
2872 // spans to be performed and compared
2886 SPAN_CONTAINED
=0x100,
2888 SPAN_CONDITION
=0x300,
2893 static inline USetSpanCondition
invertSpanCondition(USetSpanCondition spanCondition
, USetSpanCondition contained
) {
2894 return spanCondition
== USET_SPAN_NOT_CONTAINED
? contained
: USET_SPAN_NOT_CONTAINED
;
2897 static inline int32_t slen(const void *s
, UBool isUTF16
) {
2898 return isUTF16
? u_strlen((const UChar
*)s
) : strlen((const char *)s
);
2902 * Count spans on a string with the method according to type and set the span limits.
2903 * The set may be the complement of the original.
2904 * When using spanBack() and comparing with span(), use a span condition for the first spanBack()
2905 * according to the expected number of spans.
2906 * Sets typeName to an empty string if there is no such type.
2907 * Returns -1 if the span option is filtered out.
2909 static int32_t getSpans(const UnicodeSetWithStrings
&set
, UBool isComplement
,
2910 const void *s
, int32_t length
, UBool isUTF16
,
2911 uint32_t whichSpans
,
2912 int type
, const char *&typeName
,
2913 int32_t limits
[], int32_t limitsCapacity
,
2914 int32_t expectCount
) {
2915 const UnicodeSet
&realSet(set
.getSet());
2916 int32_t start
, count
;
2917 USetSpanCondition spanCondition
, firstSpanCondition
, contained
;
2920 if(type
<0 || 7<type
) {
2925 static const char *const typeNames16
[]={
2926 "contains", "contains(LM)",
2928 "containsBack", "containsBack(LM)",
2929 "spanBack", "spanBack(LM)"
2932 static const char *const typeNames8
[]={
2933 "containsUTF8", "containsUTF8(LM)",
2934 "spanUTF8", "spanUTF8(LM)",
2935 "containsBackUTF8", "containsBackUTF8(LM)", // not implemented
2936 "spanBackUTF8", "spanBackUTF8(LM)"
2939 typeName
= isUTF16
? typeNames16
[type
] : typeNames8
[type
];
2941 // filter span options
2944 if((whichSpans
&SPAN_FWD
)==0) {
2950 if((whichSpans
&SPAN_BACK
)==0) {
2956 // use USET_SPAN_CONTAINED
2957 if((whichSpans
&SPAN_CONTAINED
)==0) {
2960 contained
=USET_SPAN_CONTAINED
;
2962 // use USET_SPAN_SIMPLE
2963 if((whichSpans
&SPAN_SIMPLE
)==0) {
2966 contained
=USET_SPAN_SIMPLE
;
2969 // Default first span condition for going forward with an uncomplemented set.
2970 spanCondition
=USET_SPAN_NOT_CONTAINED
;
2972 spanCondition
=invertSpanCondition(spanCondition
, contained
);
2975 // First span condition for span(), used to terminate the spanBack() iteration.
2976 firstSpanCondition
=spanCondition
;
2978 // spanBack(): Its initial span condition is span()'s last span condition,
2979 // which is the opposite of span()'s first span condition
2980 // if we expect an even number of spans.
2981 // (The loop inverts spanCondition (expectCount-1) times
2982 // before the expectCount'th span() call.)
2983 // If we do not compare forward and backward directions, then we do not have an
2984 // expectCount and just start with firstSpanCondition.
2985 if(!isForward
&& (whichSpans
&SPAN_FWD
)!=0 && (expectCount
&1)==0) {
2986 spanCondition
=invertSpanCondition(spanCondition
, contained
);
2995 length
=slen(s
, isUTF16
);
2998 start
+= isUTF16
? containsSpanUTF16(set
, (const UChar
*)s
+start
, length
-start
, spanCondition
) :
2999 containsSpanUTF8(set
, (const char *)s
+start
, length
-start
, spanCondition
);
3000 if(count
<limitsCapacity
) {
3001 limits
[count
]=start
;
3007 spanCondition
=invertSpanCondition(spanCondition
, contained
);
3014 start
+= isUTF16
? realSet
.span((const UChar
*)s
+start
, length
>=0 ? length
-start
: length
, spanCondition
) :
3015 realSet
.spanUTF8((const char *)s
+start
, length
>=0 ? length
-start
: length
, spanCondition
);
3016 if(count
<limitsCapacity
) {
3017 limits
[count
]=start
;
3020 if(length
>=0 ? start
>=length
:
3021 isUTF16
? ((const UChar
*)s
)[start
]==0 :
3022 ((const char *)s
)[start
]==0
3026 spanCondition
=invertSpanCondition(spanCondition
, contained
);
3032 length
=slen(s
, isUTF16
);
3036 if(count
<=limitsCapacity
) {
3037 limits
[limitsCapacity
-count
]=length
;
3039 length
= isUTF16
? containsSpanBackUTF16(set
, (const UChar
*)s
, length
, spanCondition
) :
3040 containsSpanBackUTF8(set
, (const char *)s
, length
, spanCondition
);
3041 if(length
==0 && spanCondition
==firstSpanCondition
) {
3044 spanCondition
=invertSpanCondition(spanCondition
, contained
);
3046 if(count
<limitsCapacity
) {
3047 memmove(limits
, limits
+(limitsCapacity
-count
), count
*4);
3054 if(count
<=limitsCapacity
) {
3055 limits
[limitsCapacity
-count
]= length
>=0 ? length
: slen(s
, isUTF16
);
3057 // Note: Length<0 is tested only for the first spanBack().
3058 // If we wanted to keep length<0 for all spanBack()s, we would have to
3059 // temporarily modify the string by placing a NUL where the previous spanBack() stopped.
3060 length
= isUTF16
? realSet
.spanBack((const UChar
*)s
, length
, spanCondition
) :
3061 realSet
.spanBackUTF8((const char *)s
, length
, spanCondition
);
3062 if(length
==0 && spanCondition
==firstSpanCondition
) {
3065 spanCondition
=invertSpanCondition(spanCondition
, contained
);
3067 if(count
<limitsCapacity
) {
3068 memmove(limits
, limits
+(limitsCapacity
-count
), count
*4);
3079 // sets to be tested; odd index=isComplement
3088 static const char *const setNames
[SET_COUNT
]={
3096 * Verify that we get the same results whether we look at text with contains(),
3097 * span() or spanBack(), using unfrozen or frozen versions of the set,
3098 * and using the set or its complement (switching the spanConditions accordingly).
3099 * The latter verifies that
3100 * set.span(spanCondition) == set.complement().span(!spanCondition).
3102 * The expectLimits[] are either provided by the caller (with expectCount>=0)
3103 * or returned to the caller (with an input expectCount<0).
3105 void UnicodeSetTest::testSpan(const UnicodeSetWithStrings
*sets
[4],
3106 const void *s
, int32_t length
, UBool isUTF16
,
3107 uint32_t whichSpans
,
3108 int32_t expectLimits
[], int32_t &expectCount
,
3109 const char *testName
, int32_t index
) {
3110 int32_t limits
[500];
3111 int32_t limitsCount
;
3114 const char *typeName
;
3117 for(i
=0; i
<SET_COUNT
; ++i
) {
3119 // Even-numbered sets are original, uncomplemented sets.
3120 if((whichSpans
&SPAN_SET
)==0) {
3124 // Odd-numbered sets are complemented.
3125 if((whichSpans
&SPAN_COMPLEMENT
)==0) {
3129 for(type
=0;; ++type
) {
3130 limitsCount
=getSpans(*sets
[i
], (UBool
)(i
&1),
3134 limits
, UPRV_LENGTHOF(limits
), expectCount
);
3135 if(typeName
[0]==0) {
3136 break; // All types tried.
3139 continue; // Span option filtered out.
3142 expectCount
=limitsCount
;
3143 if(limitsCount
>UPRV_LENGTHOF(limits
)) {
3144 errln("FAIL: %s[0x%lx].%s.%s span count=%ld > %ld capacity - too many spans",
3145 testName
, (long)index
, setNames
[i
], typeName
, (long)limitsCount
, (long)UPRV_LENGTHOF(limits
));
3148 memcpy(expectLimits
, limits
, limitsCount
*4);
3149 } else if(limitsCount
!=expectCount
) {
3150 errln("FAIL: %s[0x%lx].%s.%s span count=%ld != %ld",
3151 testName
, (long)index
, setNames
[i
], typeName
, (long)limitsCount
, (long)expectCount
);
3153 for(j
=0; j
<limitsCount
; ++j
) {
3154 if(limits
[j
]!=expectLimits
[j
]) {
3155 errln("FAIL: %s[0x%lx].%s.%s span count=%ld limits[%d]=%ld != %ld",
3156 testName
, (long)index
, setNames
[i
], typeName
, (long)limitsCount
,
3157 j
, (long)limits
[j
], (long)expectLimits
[j
]);
3165 // Compare span() with containsAll()/containsNone(),
3166 // but only if we have expectLimits[] from the uncomplemented set.
3167 if(isUTF16
&& (whichSpans
&SPAN_SET
)!=0) {
3168 const UChar
*s16
=(const UChar
*)s
;
3169 UnicodeString string
;
3170 int32_t prev
=0, limit
, length
;
3171 for(i
=0; i
<expectCount
; ++i
) {
3172 limit
=expectLimits
[i
];
3175 string
.setTo(FALSE
, s16
+prev
, length
); // read-only alias
3177 if(!sets
[SLOW
]->getSet().containsAll(string
)) {
3178 errln("FAIL: %s[0x%lx].%s.containsAll(%ld..%ld)==FALSE contradicts span()",
3179 testName
, (long)index
, setNames
[SLOW
], (long)prev
, (long)limit
);
3182 if(!sets
[FAST
]->getSet().containsAll(string
)) {
3183 errln("FAIL: %s[0x%lx].%s.containsAll(%ld..%ld)==FALSE contradicts span()",
3184 testName
, (long)index
, setNames
[FAST
], (long)prev
, (long)limit
);
3188 if(!sets
[SLOW
]->getSet().containsNone(string
)) {
3189 errln("FAIL: %s[0x%lx].%s.containsNone(%ld..%ld)==FALSE contradicts span()",
3190 testName
, (long)index
, setNames
[SLOW
], (long)prev
, (long)limit
);
3193 if(!sets
[FAST
]->getSet().containsNone(string
)) {
3194 errln("FAIL: %s[0x%lx].%s.containsNone(%ld..%ld)==FALSE contradicts span()",
3195 testName
, (long)index
, setNames
[FAST
], (long)prev
, (long)limit
);
3205 // Specifically test either UTF-16 or UTF-8.
3206 void UnicodeSetTest::testSpan(const UnicodeSetWithStrings
*sets
[4],
3207 const void *s
, int32_t length
, UBool isUTF16
,
3208 uint32_t whichSpans
,
3209 const char *testName
, int32_t index
) {
3210 int32_t expectLimits
[500];
3211 int32_t expectCount
=-1;
3212 testSpan(sets
, s
, length
, isUTF16
, whichSpans
, expectLimits
, expectCount
, testName
, index
);
3215 UBool
stringContainsUnpairedSurrogate(const UChar
*s
, int32_t length
) {
3222 if(0xd800<=c
&& c
<0xe000) {
3223 if(c
>=0xdc00 || length
==0 || !U16_IS_TRAIL(c2
=*s
++)) {
3230 while((c
=*s
++)!=0) {
3231 if(0xd800<=c
&& c
<0xe000) {
3232 if(c
>=0xdc00 || !U16_IS_TRAIL(c2
=*s
++)) {
3241 // Test both UTF-16 and UTF-8 versions of span() etc. on the same sets and text,
3242 // unless either UTF is turned off in whichSpans.
3243 // Testing UTF-16 and UTF-8 together requires that surrogate code points
3244 // have the same contains(c) value as U+FFFD.
3245 void UnicodeSetTest::testSpanBothUTFs(const UnicodeSetWithStrings
*sets
[4],
3246 const UChar
*s16
, int32_t length16
,
3247 uint32_t whichSpans
,
3248 const char *testName
, int32_t index
) {
3249 int32_t expectLimits
[500];
3250 int32_t expectCount
;
3252 expectCount
=-1; // Get expectLimits[] from testSpan().
3254 if((whichSpans
&SPAN_UTF16
)!=0) {
3255 testSpan(sets
, s16
, length16
, TRUE
, whichSpans
, expectLimits
, expectCount
, testName
, index
);
3257 if((whichSpans
&SPAN_UTF8
)==0) {
3261 // Convert s16[] and expectLimits[] to UTF-8.
3263 int32_t offsets
[3000];
3265 const UChar
*s16Limit
=s16
+length16
;
3267 char *tLimit
=t
+sizeof(s8
);
3269 UErrorCode errorCode
=U_ZERO_ERROR
;
3271 // Convert with substitution: Turn unpaired surrogates into U+FFFD.
3272 ucnv_fromUnicode(openUTF8Converter(), &t
, tLimit
, &s16
, s16Limit
, o
, TRUE
, &errorCode
);
3273 if(U_FAILURE(errorCode
)) {
3274 errln("FAIL: %s[0x%lx] ucnv_fromUnicode(to UTF-8) fails with %s",
3275 testName
, (long)index
, u_errorName(errorCode
));
3276 ucnv_resetFromUnicode(utf8Cnv
);
3279 int32_t length8
=(int32_t)(t
-(char *)s8
);
3281 // Convert expectLimits[].
3282 int32_t i
, j
, expect
;
3283 for(i
=j
=0; i
<expectCount
; ++i
) {
3284 expect
=expectLimits
[i
];
3285 if(expect
==length16
) {
3286 expectLimits
[i
]=length8
;
3288 while(offsets
[j
]<expect
) {
3295 testSpan(sets
, s8
, length8
, FALSE
, whichSpans
, expectLimits
, expectCount
, testName
, index
);
3298 static UChar32
nextCodePoint(UChar32 c
) {
3299 // Skip some large and boring ranges.
3320 // Verify that all implementations represent the same set.
3321 void UnicodeSetTest::testSpanContents(const UnicodeSetWithStrings
*sets
[4], uint32_t whichSpans
, const char *testName
) {
3322 // contains(U+FFFD) is inconsistent with contains(some surrogates),
3323 // or the set contains strings with unpaired surrogates which don't translate to valid UTF-8:
3324 // Skip the UTF-8 part of the test - if the string contains surrogates -
3325 // because it is likely to produce a different result.
3326 UBool inconsistentSurrogates
=
3327 (!(sets
[0]->getSet().contains(0xfffd) ?
3328 sets
[0]->getSet().contains(0xd800, 0xdfff) :
3329 sets
[0]->getSet().containsNone(0xd800, 0xdfff)) ||
3330 sets
[0]->hasStringsWithSurrogates());
3334 uint32_t localWhichSpans
;
3337 for(first
=c
=0;; c
=nextCodePoint(c
)) {
3338 if(c
>0x10ffff || length
>(UPRV_LENGTHOF(s
)-U16_MAX_LENGTH
)) {
3339 localWhichSpans
=whichSpans
;
3340 if(stringContainsUnpairedSurrogate(s
, length
) && inconsistentSurrogates
) {
3341 localWhichSpans
&=~SPAN_UTF8
;
3343 testSpanBothUTFs(sets
, s
, length
, localWhichSpans
, testName
, first
);
3350 U16_APPEND_UNSAFE(s
, length
, c
);
3354 // Test with a particular, interesting string.
3355 // Specify length and try NUL-termination.
3356 void UnicodeSetTest::testSpanUTF16String(const UnicodeSetWithStrings
*sets
[4], uint32_t whichSpans
, const char *testName
) {
3357 static const UChar s
[]={
3358 0x61, 0x62, 0x20, // Latin, space
3359 0x3b1, 0x3b2, 0x3b3, // Greek
3360 0xd900, // lead surrogate
3361 0x3000, 0x30ab, 0x30ad, // wide space, Katakana
3362 0xdc05, // trail surrogate
3363 0xa0, 0xac00, 0xd7a3, // nbsp, Hangul
3364 0xd900, 0xdc05, // unassigned supplementary
3365 0xd840, 0xdfff, 0xd860, 0xdffe, // Han supplementary
3366 0xd7a4, 0xdc05, 0xd900, 0x2028, // unassigned, surrogates in wrong order, LS
3370 if((whichSpans
&SPAN_UTF16
)==0) {
3373 testSpan(sets
, s
, -1, TRUE
, (whichSpans
&~SPAN_UTF8
), testName
, 0);
3374 testSpan(sets
, s
, UPRV_LENGTHOF(s
)-1, TRUE
, (whichSpans
&~SPAN_UTF8
), testName
, 1);
3377 void UnicodeSetTest::testSpanUTF8String(const UnicodeSetWithStrings
*sets
[4], uint32_t whichSpans
, const char *testName
) {
3378 static const char s
[]={
3381 /* trail byte in lead position */
3386 /* truncated multi-byte sequences */
3398 "\xCE\xB1\xCE\xB2\xCE\xB3" // Greek
3400 /* trail byte in lead position */
3417 "\xE3\x80\x80\xE3\x82\xAB\xE3\x82\xAD" // wide space, Katakana
3419 /* trail byte in lead position */
3430 "\xC2\xA0\xEA\xB0\x80\xED\x9E\xA3" // nbsp, Hangul
3432 /* trail byte in lead position */
3438 "\xF1\x90\x80\x85" // unassigned supplementary
3440 /* trail byte in lead position */
3443 "\xfc\x80\x80\x80\x80"
3445 "\xF0\xA0\x8F\xBF\xF0\xA8\x8F\xBE" // Han supplementary
3447 /* trail byte in lead position */
3450 /* complete sequences but non-shortest forms or out of range etc. */
3456 "\xf8\x80\x80\x80\x80"
3457 "\xfc\x80\x80\x80\x80\x80"
3461 /* trail byte in lead position */
3464 "\xED\x9E\xA4\xE2\x80\xA8" // unassigned, LS, NUL-terminated
3467 if((whichSpans
&SPAN_UTF8
)==0) {
3470 testSpan(sets
, s
, -1, FALSE
, (whichSpans
&~SPAN_UTF16
), testName
, 0);
3471 testSpan(sets
, s
, UPRV_LENGTHOF(s
)-1, FALSE
, (whichSpans
&~SPAN_UTF16
), testName
, 1);
3474 // Take a set of span options and multiply them so that
3475 // each portion only has one of the options a, b and c.
3476 // If b==0, then the set of options is just modified with mask and a.
3477 // If b!=0 and c==0, then the set of options is just modified with mask, a and b.
3479 addAlternative(uint32_t whichSpans
[], int32_t whichSpansCount
,
3480 uint32_t mask
, uint32_t a
, uint32_t b
, uint32_t c
) {
3484 for(i
=0; i
<whichSpansCount
; ++i
) {
3485 s
=whichSpans
[i
]&mask
;
3488 whichSpans
[whichSpansCount
+i
]=s
|b
;
3490 whichSpans
[2*whichSpansCount
+i
]=s
|c
;
3494 return b
==0 ? whichSpansCount
: c
==0 ? 2*whichSpansCount
: 3*whichSpansCount
;
3497 #define _63_a "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3498 #define _64_a "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3499 #define _63_b "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
3500 #define _64_b "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
3502 void UnicodeSetTest::TestSpan() {
3503 // "[...]" is a UnicodeSet pattern.
3504 // "*" performs tests on all Unicode code points and on a selection of
3505 // malformed UTF-8/16 strings.
3506 // "-options" limits the scope of testing for the current set.
3507 // By default, the test verifies that equivalent boundaries are found
3508 // for UTF-16 and UTF-8, going forward and backward,
3509 // alternating USET_SPAN_NOT_CONTAINED with
3510 // either USET_SPAN_CONTAINED or USET_SPAN_SIMPLE.
3511 // Single-character options:
3512 // 8 -- UTF-16 and UTF-8 boundaries may differ.
3513 // Cause: contains(U+FFFD) is inconsistent with contains(some surrogates),
3514 // or the set contains strings with unpaired surrogates
3515 // which do not translate to valid UTF-8.
3516 // c -- set.span() and set.complement().span() boundaries may differ.
3517 // Cause: Set strings are not complemented.
3518 // b -- span() and spanBack() boundaries may differ.
3519 // Cause: Strings in the set overlap, and spanBack(USET_SPAN_CONTAINED)
3520 // and spanBack(USET_SPAN_SIMPLE) are defined to
3521 // match with non-overlapping substrings.
3522 // For example, with a set containing "ab" and "ba",
3523 // span() of "aba" yields boundaries { 0, 2, 3 }
3524 // because the initial "ab" matches from 0 to 2,
3525 // while spanBack() yields boundaries { 0, 1, 3 }
3526 // because the final "ba" matches from 1 to 3.
3527 // l -- USET_SPAN_CONTAINED and USET_SPAN_SIMPLE boundaries may differ.
3528 // Cause: Strings in the set overlap, and a longer match may
3529 // require a sequence including non-longest substrings.
3530 // For example, with a set containing "ab", "abc" and "cd",
3531 // span(contained) of "abcd" spans the entire string
3532 // but span(longest match) only spans the first 3 characters.
3533 // Each "-options" first resets all options and then applies the specified options.
3534 // A "-" without options resets the options.
3535 // The options are also reset for each new set.
3536 // Other strings will be spanned.
3537 static const char *const testdata
[]={
3544 "[\\u0000-\\U0010FFFF]",
3546 "[\\u0000\\u0080\\u0800\\U00010000]",
3548 "[\\u007F\\u07FF\\uFFFF\\U0010FFFF]",
3550 "[[[:ID_Continue:]-[\\u30ab\\u30ad]]{\\u3000\\u30ab}{\\u3000\\u30ab\\u30ad}]",
3553 "[[[:ID_Continue:]-[\\u30ab\\u30ad]]{\\u30ab\\u30ad}{\\u3000\\u30ab\\u30ad}]",
3557 // Overlapping strings cause overlapping attempts to match.
3558 "[x{xy}{xya}{axy}{ax}]",
3561 // More repetitions of "xya" would take too long with the recursive
3562 // reference implementation.
3563 // containsAll()=FALSE
3566 "xyaxyaxyaxya" // set.complement().span(longest match) will stop here.
3567 "xx" // set.complement().span(contained) will stop between the two 'x'es.
3570 "xyaxyaxyaxya" // span() ends here.
3573 // containsAll()=TRUE
3584 "byayaxya", // span() -> { 4, 7, 8 } spanBack() -> { 5, 8 }
3586 "byayaxy", // span() -> { 4, 7 } complement.span() -> { 7 }
3587 "byayax", // span() -> { 4, 6 } complement.span() -> { 6 }
3589 "byaya", // span() -> { 5 }
3590 "byay", // span() -> { 4 }
3591 "bya", // span() -> { 3 }
3593 // span(longest match) will not span the whole string.
3603 // spanBack(longest match) will not span the whole string.
3612 // Test with non-ASCII set strings - test proper handling of surrogate pairs
3613 // and UTF-8 trail bytes.
3614 // Copies of above test sets and strings, but transliterated to have
3615 // different code points with similar trail units.
3616 // Previous: a b c d
3617 // Unicode: 042B 30AB 200AB 204AB
3618 // UTF-16: 042B 30AB D840 DCAB D841 DCAB
3619 // UTF-8: D0 AB E3 82 AB F0 A0 82 AB F0 A0 92 AB
3620 "[\\u042B{\\u042B\\u30AB}{\\u042B\\u30AB\\U000200AB}{\\U000200AB\\U000204AB}]",
3622 "\\u042B\\U000200AB\\U000204AB\\u042B\\u30AB\\U000200AB\\U000204AB\\u042B\\u30AB\\U000200AB\\U000200AB\\U000204AB",
3624 "[\\U000204AB{\\U000200AB\\U000204AB}{\\u30AB\\U000200AB\\U000204AB}{\\u042B\\u30AB}]",
3626 "\\u042B\\u30AB\\u30AB\\U000200AB\\U000204AB\\u042B\\u30AB\\U000200AB\\U000204AB\\u042B\\u30AB\\U000204AB",
3628 // Stress bookkeeping and recursion.
3629 // The following strings are barely doable with the recursive
3630 // reference implementation.
3631 // The not-contained character at the end prevents an early exit from the span().
3635 "bbbbbbbbbbbbbbbbbbbbbbbb-",
3636 // On complement sets, span() and spanBack() get different results
3637 // because b is not in the complement set and there is an odd number of b's
3638 // in the test string.
3640 "bbbbbbbbbbbbbbbbbbbbbbbbb-",
3642 // Test with set strings with an initial or final code point span
3644 "[a{" _64_a _64_a _64_a _64_a
"b}"
3645 "{a" _64_b _64_b _64_b _64_b
"}]",
3647 _64_a _64_a _64_a _63_a
"b",
3648 _64_a _64_a _64_a _64_a
"b",
3649 _64_a _64_a _64_a _64_a
"aaaabbbb",
3650 "a" _64_b _64_b _64_b _63_b
,
3651 "a" _64_b _64_b _64_b _64_b
,
3652 "aaaabbbb" _64_b _64_b _64_b _64_b
,
3654 // Test with strings containing unpaired surrogates.
3655 // They are not representable in UTF-8, and a leading trail surrogate
3656 // and a trailing lead surrogate must not match in the middle of a proper surrogate pair.
3657 // U+20001 == \\uD840\\uDC01
3658 // U+20400 == \\uD841\\uDC00
3659 "[a\\U00020001\\U00020400{ab}{b\\uD840}{\\uDC00a}]",
3661 "aaab\\U00020001ba\\U00020400aba\\uD840ab\\uD840\\U00020000b\\U00020000a\\U00020000\\uDC00a\\uDC00babbb"
3663 uint32_t whichSpans
[96]={ SPAN_ALL
};
3664 int32_t whichSpansCount
=1;
3666 UnicodeSet
*sets
[SET_COUNT
]={ NULL
};
3667 const UnicodeSetWithStrings
*sets_with_str
[SET_COUNT
]={ NULL
};
3669 char testName
[1024];
3670 char *testNameLimit
=testName
;
3673 for(i
=0; i
<UPRV_LENGTHOF(testdata
); ++i
) {
3674 const char *s
=testdata
[i
];
3676 // Create new test sets from this pattern.
3677 for(j
=0; j
<SET_COUNT
; ++j
) {
3678 delete sets_with_str
[j
];
3681 UErrorCode errorCode
=U_ZERO_ERROR
;
3682 sets
[SLOW
]=new UnicodeSet(UnicodeString(s
, -1, US_INV
).unescape(), errorCode
);
3683 if(U_FAILURE(errorCode
)) {
3684 dataerrln("FAIL: Unable to create UnicodeSet(%s) - %s", s
, u_errorName(errorCode
));
3687 sets
[SLOW_NOT
]=new UnicodeSet(*sets
[SLOW
]);
3688 sets
[SLOW_NOT
]->complement();
3689 // Intermediate set: Test cloning of a frozen set.
3690 UnicodeSet
*fast
=new UnicodeSet(*sets
[SLOW
]);
3692 sets
[FAST
]=(UnicodeSet
*)fast
->clone();
3694 UnicodeSet
*fastNot
=new UnicodeSet(*sets
[SLOW_NOT
]);
3696 sets
[FAST_NOT
]=(UnicodeSet
*)fastNot
->clone();
3699 for(j
=0; j
<SET_COUNT
; ++j
) {
3700 sets_with_str
[j
]=new UnicodeSetWithStrings(*sets
[j
]);
3703 strcpy(testName
, s
);
3704 testNameLimit
=strchr(testName
, 0);
3705 *testNameLimit
++=':';
3708 whichSpans
[0]=SPAN_ALL
;
3710 } else if(s
[0]=='-') {
3711 whichSpans
[0]=SPAN_ALL
;
3717 whichSpansCount
=addAlternative(whichSpans
, whichSpansCount
,
3724 whichSpansCount
=addAlternative(whichSpans
, whichSpansCount
,
3731 // test USET_SPAN_CONTAINED FWD & BACK, and separately
3732 // USET_SPAN_SIMPLE only FWD, and separately
3733 // USET_SPAN_SIMPLE only BACK
3734 whichSpansCount
=addAlternative(whichSpans
, whichSpansCount
,
3735 ~(SPAN_DIRS
|SPAN_CONDITION
),
3736 SPAN_DIRS
|SPAN_CONTAINED
,
3737 SPAN_FWD
|SPAN_SIMPLE
,
3738 SPAN_BACK
|SPAN_SIMPLE
);
3741 whichSpansCount
=addAlternative(whichSpans
, whichSpansCount
,
3748 errln("FAIL: unrecognized span set option in \"%s\"", testdata
[i
]);
3752 } else if(0==strcmp(s
, "*")) {
3753 strcpy(testNameLimit
, "bad_string");
3754 for(j
=0; j
<whichSpansCount
; ++j
) {
3755 if(whichSpansCount
>1) {
3756 sprintf(testNameLimit
+10 /* strlen("bad_string") */,
3760 testSpanUTF16String(sets_with_str
, whichSpans
[j
], testName
);
3761 testSpanUTF8String(sets_with_str
, whichSpans
[j
], testName
);
3764 strcpy(testNameLimit
, "contents");
3765 for(j
=0; j
<whichSpansCount
; ++j
) {
3766 if(whichSpansCount
>1) {
3767 sprintf(testNameLimit
+8 /* strlen("contents") */,
3771 testSpanContents(sets_with_str
, whichSpans
[j
], testName
);
3774 UnicodeString string
=UnicodeString(s
, -1, US_INV
).unescape();
3775 strcpy(testNameLimit
, "test_string");
3776 for(j
=0; j
<whichSpansCount
; ++j
) {
3777 if(whichSpansCount
>1) {
3778 sprintf(testNameLimit
+11 /* strlen("test_string") */,
3782 testSpanBothUTFs(sets_with_str
, string
.getBuffer(), string
.length(), whichSpans
[j
], testName
, i
);
3786 for(j
=0; j
<SET_COUNT
; ++j
) {
3787 delete sets_with_str
[j
];
3792 // Test select patterns and strings, and test USET_SPAN_SIMPLE.
3793 void UnicodeSetTest::TestStringSpan() {
3794 static const char *pattern
="[x{xy}{xya}{axy}{ax}]";
3795 static const char *const string
=
3797 "xyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxya"
3799 "xyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxya"
3801 "xyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxyaxy"
3804 UErrorCode errorCode
=U_ZERO_ERROR
;
3805 UnicodeString pattern16
=UnicodeString(pattern
, -1, US_INV
);
3806 UnicodeSet
set(pattern16
, errorCode
);
3807 if(U_FAILURE(errorCode
)) {
3808 errln("FAIL: Unable to create UnicodeSet(%s) - %s", pattern
, u_errorName(errorCode
));
3812 UnicodeString string16
=UnicodeString(string
, -1, US_INV
).unescape();
3814 if(set
.containsAll(string16
)) {
3815 errln("FAIL: UnicodeSet(%s).containsAll(%s) should be FALSE", pattern
, string
);
3818 // Remove trailing "aaaa".
3819 string16
.truncate(string16
.length()-4);
3820 if(!set
.containsAll(string16
)) {
3821 errln("FAIL: UnicodeSet(%s).containsAll(%s[:-4]) should be TRUE", pattern
, string
);
3824 string16
=UNICODE_STRING_SIMPLE("byayaxya");
3825 const UChar
*s16
=string16
.getBuffer();
3826 int32_t length16
=string16
.length();
3827 (void)length16
; // Suppress set but not used warning.
3828 if( set
.span(s16
, 8, USET_SPAN_NOT_CONTAINED
)!=4 ||
3829 set
.span(s16
, 7, USET_SPAN_NOT_CONTAINED
)!=4 ||
3830 set
.span(s16
, 6, USET_SPAN_NOT_CONTAINED
)!=4 ||
3831 set
.span(s16
, 5, USET_SPAN_NOT_CONTAINED
)!=5 ||
3832 set
.span(s16
, 4, USET_SPAN_NOT_CONTAINED
)!=4 ||
3833 set
.span(s16
, 3, USET_SPAN_NOT_CONTAINED
)!=3
3835 errln("FAIL: UnicodeSet(%s).span(while not) returns the wrong value", pattern
);
3838 pattern
="[a{ab}{abc}{cd}]";
3839 pattern16
=UnicodeString(pattern
, -1, US_INV
);
3840 set
.applyPattern(pattern16
, errorCode
);
3841 if(U_FAILURE(errorCode
)) {
3842 errln("FAIL: Unable to create UnicodeSet(%s) - %s", pattern
, u_errorName(errorCode
));
3845 string16
=UNICODE_STRING_SIMPLE("acdabcdabccd");
3846 s16
=string16
.getBuffer();
3847 length16
=string16
.length();
3848 if( set
.span(s16
, 12, USET_SPAN_CONTAINED
)!=12 ||
3849 set
.span(s16
, 12, USET_SPAN_SIMPLE
)!=6 ||
3850 set
.span(s16
+7, 5, USET_SPAN_SIMPLE
)!=5
3852 errln("FAIL: UnicodeSet(%s).span(while longest match) returns the wrong value", pattern
);
3855 pattern
="[d{cd}{bcd}{ab}]";
3856 pattern16
=UnicodeString(pattern
, -1, US_INV
);
3857 set
.applyPattern(pattern16
, errorCode
).freeze();
3858 if(U_FAILURE(errorCode
)) {
3859 errln("FAIL: Unable to create UnicodeSet(%s) - %s", pattern
, u_errorName(errorCode
));
3862 string16
=UNICODE_STRING_SIMPLE("abbcdabcdabd");
3863 s16
=string16
.getBuffer();
3864 length16
=string16
.length();
3865 if( set
.spanBack(s16
, 12, USET_SPAN_CONTAINED
)!=0 ||
3866 set
.spanBack(s16
, 12, USET_SPAN_SIMPLE
)!=6 ||
3867 set
.spanBack(s16
, 5, USET_SPAN_SIMPLE
)!=0
3869 errln("FAIL: UnicodeSet(%s).spanBack(while longest match) returns the wrong value", pattern
);
3874 * Including collationroot.h fails here with
3875 1>c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\driverspecs.h(142): error C2008: '$' : unexpected in macro definition
3876 * .. so, we skip this test on Windows.
3878 * the cause is that intltest builds with /Za which disables language extensions - which means
3879 * windows header files can't be used.
3881 #if !UCONFIG_NO_COLLATION && !U_PLATFORM_HAS_WIN32_API
3882 #include "collationroot.h"
3883 #include "collationtailoring.h"
3886 void UnicodeSetTest::TestUCAUnsafeBackwards() {
3887 #if U_PLATFORM_HAS_WIN32_API
3888 infoln("Skipping TestUCAUnsafeBackwards() - can't include collationroot.h on Windows without language extensions!");
3889 #elif !UCONFIG_NO_COLLATION
3890 UErrorCode errorCode
= U_ZERO_ERROR
;
3892 // Get the unsafeBackwardsSet
3893 const CollationCacheEntry
*rootEntry
= CollationRoot::getRootCacheEntry(errorCode
);
3894 if(U_FAILURE(errorCode
)) {
3895 dataerrln("FAIL: %s getting root cache entry", u_errorName(errorCode
));
3898 //const UVersionInfo &version = rootEntry->tailoring->version;
3899 const UnicodeSet
*unsafeBackwardSet
= rootEntry
->tailoring
->unsafeBackwardSet
;
3901 checkSerializeRoundTrip(*unsafeBackwardSet
, errorCode
);
3903 if(!logKnownIssue("11891","UnicodeSet fails to round trip on CollationRoot...unsafeBackwards set")) {
3905 // TODO(ticket #11891): Simplify this test function to this simple case. Rename it appropriately.
3906 // TODO(ticket #11891): Port test to Java. Is this a bug there, too?
3907 UnicodeSet surrogates
;
3908 surrogates
.add(0xd83a); // a lead surrogate
3909 surrogates
.add(0xdc00, 0xdfff); // a range of trail surrogates
3911 surrogates
.toPattern(pat
, FALSE
); // bad: [ 0xd83a, 0xdc00, 0x2d, 0xdfff ]
3912 // TODO: Probably fix either UnicodeSet::_generatePattern() or _appendToPat()
3913 // so that at least one type of surrogate code points are escaped,
3914 // or (minimally) so that adjacent lead+trail surrogate code points are escaped.
3915 errorCode
= U_ZERO_ERROR
;
3917 s2
.applyPattern(pat
, errorCode
); // looks like invalid range [ 0x1e800, 0x2d, 0xdfff ]
3918 if(U_FAILURE(errorCode
)) {
3919 errln("FAIL: surrogates to/from pattern - %s", u_errorName(errorCode
));
3921 checkEqual(surrogates
, s2
, "surrogates to/from pattern");
3923 // This occurs in the UCA unsafe-backwards set.
3924 checkRoundTrip(*unsafeBackwardSet
);