1999-10-26 17:16:13 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2000-03-28 23:51:25 +00:00
|
|
|
* Copyright (C) 1999 Alan Liu and others. All rights reserved.
|
1999-10-26 17:16:13 +00:00
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 10/20/99 alan Creation.
|
2000-03-28 23:51:25 +00:00
|
|
|
* 03/22/2000 Madhu Added additional tests
|
1999-10-26 17:16:13 +00:00
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/utypes.h"
|
1999-10-20 22:08:09 +00:00
|
|
|
#include "usettest.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/uniset.h"
|
2001-01-02 19:01:49 +00:00
|
|
|
#include "unicode/unicode.h"
|
1999-10-20 22:08:09 +00:00
|
|
|
|
2000-05-25 19:28:53 +00:00
|
|
|
UnicodeString operator+(const UnicodeString& left, const UnicodeSet& set) {
|
|
|
|
UnicodeString pat;
|
|
|
|
set.toPattern(pat);
|
|
|
|
return left + UnicodeSetTest::escape(pat);
|
|
|
|
}
|
|
|
|
|
1999-10-20 22:08:09 +00:00
|
|
|
#define CASE(id,test) case id: \
|
|
|
|
name = #test; \
|
|
|
|
if (exec) { \
|
|
|
|
logln(#test "---"); \
|
|
|
|
logln((UnicodeString)""); \
|
|
|
|
test(); \
|
|
|
|
} \
|
2000-05-25 19:28:53 +00:00
|
|
|
break
|
1999-10-20 22:08:09 +00:00
|
|
|
|
|
|
|
void
|
2000-05-18 22:08:39 +00:00
|
|
|
UnicodeSetTest::runIndexedTest(int32_t index, UBool exec,
|
2000-08-23 19:11:16 +00:00
|
|
|
const char* &name, char* /*par*/) {
|
1999-10-20 22:08:09 +00:00
|
|
|
// if (exec) logln((UnicodeString)"TestSuite UnicodeSetTest");
|
|
|
|
switch (index) {
|
2000-05-25 19:28:53 +00:00
|
|
|
CASE(0,TestPatterns);
|
|
|
|
CASE(1,TestAddRemove);
|
|
|
|
CASE(2,TestCategories);
|
|
|
|
CASE(3,TestCloneEqualHash);
|
|
|
|
CASE(4,TestMinimalRep);
|
|
|
|
CASE(5,TestAPI);
|
|
|
|
CASE(6,TestExhaustive);
|
2000-08-10 00:28:31 +00:00
|
|
|
default: name = ""; break;
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-12-22 22:57:04 +00:00
|
|
|
UnicodeSetTest::TestPatterns(void) {
|
1999-10-26 17:16:13 +00:00
|
|
|
UnicodeSet set;
|
2000-08-02 19:06:18 +00:00
|
|
|
expectPattern(set, UnicodeString("[[a-m]&[d-z]&[k-y]]", ""), "km");
|
|
|
|
expectPattern(set, UnicodeString("[[a-z]-[m-y]-[d-r]]", ""), "aczz");
|
|
|
|
expectPattern(set, UnicodeString("[a\\-z]", ""), "--aazz");
|
|
|
|
expectPattern(set, UnicodeString("[-az]", ""), "--aazz");
|
|
|
|
expectPattern(set, UnicodeString("[az-]", ""), "--aazz");
|
|
|
|
expectPattern(set, UnicodeString("[[[a-z]-[aeiou]i]]", ""), "bdfnptvz");
|
1999-10-26 17:16:13 +00:00
|
|
|
|
|
|
|
// Throw in a test of complement
|
|
|
|
set.complement();
|
|
|
|
UnicodeString exp;
|
2000-06-28 22:26:59 +00:00
|
|
|
exp.append((UChar)0x0000).append("aeeoouu").append((UChar)(0x007a+1)).append((UChar)0xFFFF);
|
1999-10-26 17:16:13 +00:00
|
|
|
expectPairs(set, exp);
|
|
|
|
}
|
|
|
|
|
2000-01-12 18:04:21 +00:00
|
|
|
void
|
|
|
|
UnicodeSetTest::TestCategories(void) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
const char* pat = " [:Lu:] "; // Whitespace ok outside [:..:]
|
|
|
|
UnicodeSet set(pat, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
errln((UnicodeString)"Fail: Can't construct set with " + pat);
|
|
|
|
} else {
|
|
|
|
expectContainment(set, pat, "ABC", "abc");
|
|
|
|
}
|
2000-05-25 19:28:53 +00:00
|
|
|
|
|
|
|
UChar32 i;
|
|
|
|
int32_t failures = 0;
|
|
|
|
// Make sure generation of L doesn't pollute cached Lu set
|
|
|
|
// First generate L, then Lu
|
|
|
|
set.applyPattern("[:L:]", status);
|
|
|
|
if (U_FAILURE(status)) { errln("FAIL"); return; }
|
|
|
|
for (i=0; i<0x200; ++i) {
|
|
|
|
UBool l = Unicode::isLetter((UChar)i);
|
|
|
|
if (l != set.contains(i)) {
|
2000-06-02 01:10:20 +00:00
|
|
|
errln((UnicodeString)"FAIL: L contains " + (unsigned short)i + " = " +
|
2000-05-25 19:28:53 +00:00
|
|
|
set.contains(i));
|
|
|
|
if (++failures == 10) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set.applyPattern("[:Lu:]", status);
|
|
|
|
if (U_FAILURE(status)) { errln("FAIL"); return; }
|
|
|
|
for (i=0; i<0x200; ++i) {
|
|
|
|
UBool lu = (Unicode::getType((UChar)i) == Unicode::UPPERCASE_LETTER);
|
|
|
|
if (lu != set.contains(i)) {
|
2000-06-02 01:10:20 +00:00
|
|
|
errln((UnicodeString)"FAIL: Lu contains " + (unsigned short)i + " = " +
|
2000-05-25 19:28:53 +00:00
|
|
|
set.contains(i));
|
|
|
|
if (++failures == 20) break;
|
|
|
|
}
|
|
|
|
}
|
2000-01-12 18:04:21 +00:00
|
|
|
}
|
2000-03-28 23:51:25 +00:00
|
|
|
void
|
|
|
|
UnicodeSetTest::TestCloneEqualHash(void) {
|
2000-08-14 21:42:36 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
int8_t category=Unicode::LOWERCASE_LETTER;
|
|
|
|
UnicodeSet *set1=new UnicodeSet(category, status); // :Li: Letter, lowercase
|
|
|
|
if (U_FAILURE(status)){
|
|
|
|
errln((UnicodeString)"FAIL: Can't construst set with cateegory->Ll");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
category=Unicode::DECIMAL_DIGIT_NUMBER;
|
|
|
|
UnicodeSet *set2=new UnicodeSet(category, status); //Number, Decimal digit
|
|
|
|
if (U_FAILURE(status)){
|
|
|
|
errln((UnicodeString)"FAIL: Can't construct set with cateegory->Nd");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
logln("Testing copy construction");
|
|
|
|
UnicodeSet *set1copy=new UnicodeSet(*set1);
|
|
|
|
if(*set1 != *set1copy || *set1 == *set2 ||
|
|
|
|
getPairs(*set1) != getPairs(*set1copy) ||
|
|
|
|
set1->hashCode() != set1copy->hashCode()){
|
|
|
|
errln("FAIL : Error in copy construction");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
logln("Testing =operator");
|
|
|
|
UnicodeSet set1equal=*set1;
|
|
|
|
UnicodeSet set2equal=*set2;
|
|
|
|
if(set1equal != *set1 || set1equal != *set1copy || set2equal != *set2 ||
|
|
|
|
set2equal == *set1 || set2equal == *set1copy || set2equal == set1equal){
|
|
|
|
errln("FAIL: Error in =operator");
|
|
|
|
}
|
|
|
|
|
|
|
|
logln("Testing clone()");
|
|
|
|
UnicodeSet *set1clone=(UnicodeSet*)set1->clone();
|
|
|
|
UnicodeSet *set2clone=(UnicodeSet*)set2->clone();
|
|
|
|
if(*set1clone != *set1 || *set1clone != *set1copy || *set1clone != set1equal ||
|
|
|
|
*set2clone != *set2 || *set2clone == *set1copy || *set2clone != set2equal ||
|
|
|
|
*set2clone == *set1 || *set2clone == set1equal || *set2clone == *set1clone){
|
|
|
|
errln("FAIL: Error in clone");
|
|
|
|
}
|
|
|
|
|
|
|
|
logln("Testing hashcode");
|
2000-03-28 23:51:25 +00:00
|
|
|
if(set1->hashCode() != set1equal.hashCode() || set1->hashCode() != set1clone->hashCode() ||
|
2000-08-14 21:42:36 +00:00
|
|
|
set2->hashCode() != set2equal.hashCode() || set2->hashCode() != set2clone->hashCode() ||
|
|
|
|
set1copy->hashCode() != set1equal.hashCode() || set1copy->hashCode() != set1clone->hashCode() ||
|
|
|
|
set1->hashCode() == set2->hashCode() || set1copy->hashCode() == set2->hashCode() ||
|
|
|
|
set2->hashCode() == set1clone->hashCode() || set2->hashCode() == set1equal.hashCode() ){
|
|
|
|
errln("FAIL: Error in hashCode()");
|
|
|
|
}
|
|
|
|
|
|
|
|
delete set1;
|
|
|
|
delete set1copy;
|
|
|
|
delete set2;
|
|
|
|
|
2000-03-28 23:51:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
1999-10-26 17:16:13 +00:00
|
|
|
void
|
1999-12-22 22:57:04 +00:00
|
|
|
UnicodeSetTest::TestAddRemove(void) {
|
1999-10-20 22:08:09 +00:00
|
|
|
UnicodeSet set; // Construct empty set
|
2000-08-14 21:42:36 +00:00
|
|
|
doAssert(set.isEmpty() == TRUE, "set should be empty");
|
|
|
|
doAssert(set.size() == 0, "size should be 0");
|
2000-06-28 22:26:59 +00:00
|
|
|
set.add(0x0061, 0x007a);
|
1999-10-26 17:16:13 +00:00
|
|
|
expectPairs(set, "az");
|
2000-08-14 21:42:36 +00:00
|
|
|
doAssert(set.isEmpty() == FALSE, "set should not be empty");
|
2000-03-28 23:51:25 +00:00
|
|
|
doAssert(set.size() != 0, "size should not be equal to 0");
|
2000-08-14 21:42:36 +00:00
|
|
|
doAssert(set.size() == 26, "size should be equal to 26");
|
2000-06-28 22:26:59 +00:00
|
|
|
set.remove(0x006d, 0x0070);
|
2000-08-14 21:42:36 +00:00
|
|
|
expectPairs(set, "alqz");
|
|
|
|
doAssert(set.size() == 22, "size should be equal to 22");
|
2000-06-28 22:26:59 +00:00
|
|
|
set.remove(0x0065, 0x0067);
|
1999-10-26 17:16:13 +00:00
|
|
|
expectPairs(set, "adhlqz");
|
2000-08-14 21:42:36 +00:00
|
|
|
doAssert(set.size() == 19, "size should be equal to 19");
|
2000-06-28 22:26:59 +00:00
|
|
|
set.remove(0x0064, 0x0069);
|
1999-10-26 17:16:13 +00:00
|
|
|
expectPairs(set, "acjlqz");
|
2000-08-14 21:42:36 +00:00
|
|
|
doAssert(set.size() == 16, "size should be equal to 16");
|
2000-06-28 22:26:59 +00:00
|
|
|
set.remove(0x0063, 0x0072);
|
1999-10-26 17:16:13 +00:00
|
|
|
expectPairs(set, "absz");
|
2000-08-14 21:42:36 +00:00
|
|
|
doAssert(set.size() == 10, "size should be equal to 10");
|
2000-06-28 22:26:59 +00:00
|
|
|
set.add(0x0066, 0x0071);
|
1999-10-26 17:16:13 +00:00
|
|
|
expectPairs(set, "abfqsz");
|
2000-08-14 21:42:36 +00:00
|
|
|
doAssert(set.size() == 22, "size should be equal to 22");
|
2000-06-28 22:26:59 +00:00
|
|
|
set.remove(0x0061, 0x0067);
|
1999-10-26 17:16:13 +00:00
|
|
|
expectPairs(set, "hqsz");
|
2000-06-28 22:26:59 +00:00
|
|
|
set.remove(0x0061, 0x007a);
|
2000-03-28 23:51:25 +00:00
|
|
|
expectPairs(set, "");
|
2000-08-14 21:42:36 +00:00
|
|
|
doAssert(set.isEmpty() == TRUE, "set should be empty");
|
|
|
|
doAssert(set.size() == 0, "size should be 0");
|
|
|
|
set.add(0x0061);
|
|
|
|
doAssert(set.isEmpty() == FALSE, "set should not be empty");
|
2000-03-28 23:51:25 +00:00
|
|
|
doAssert(set.size() == 1, "size should not be equal to 1");
|
2000-08-14 21:42:36 +00:00
|
|
|
set.add(0x0062);
|
|
|
|
set.add(0x0063);
|
|
|
|
expectPairs(set, "ac");
|
|
|
|
doAssert(set.size() == 3, "size should not be equal to 3");
|
|
|
|
set.add(0x0070);
|
|
|
|
set.add(0x0071);
|
|
|
|
expectPairs(set, "acpq");
|
|
|
|
doAssert(set.size() == 5, "size should not be equal to 5");
|
|
|
|
set.clear();
|
1999-10-26 17:16:13 +00:00
|
|
|
expectPairs(set, "");
|
2000-08-14 21:42:36 +00:00
|
|
|
doAssert(set.isEmpty() == TRUE, "set should be empty");
|
|
|
|
doAssert(set.size() == 0, "size should be 0");
|
1999-10-20 22:08:09 +00:00
|
|
|
|
1999-10-26 17:16:13 +00:00
|
|
|
// Try removing an entire set from another set
|
|
|
|
expectPattern(set, "[c-x]", "cx");
|
|
|
|
UnicodeSet set2;
|
|
|
|
expectPattern(set2, "[f-ky-za-bc[vw]]", "acfkvwyz");
|
|
|
|
set.removeAll(set2);
|
|
|
|
expectPairs(set, "deluxx");
|
1999-10-20 22:08:09 +00:00
|
|
|
|
1999-10-26 17:16:13 +00:00
|
|
|
// Try adding an entire set to another set
|
|
|
|
expectPattern(set, "[jackiemclean]", "aacceein");
|
|
|
|
expectPattern(set2, "[hitoshinamekatajamesanderson]", "aadehkmort");
|
|
|
|
set.addAll(set2);
|
|
|
|
expectPairs(set, "aacehort");
|
2000-03-28 23:51:25 +00:00
|
|
|
doAssert(set.containsAll(set2) == TRUE, "set should contain all the elements in set2");
|
|
|
|
|
2000-08-14 21:42:36 +00:00
|
|
|
// Try retaining an set of elements contained in another set (intersection)
|
|
|
|
UnicodeSet set3;
|
|
|
|
expectPattern(set3, "[a-c]", "ac");
|
|
|
|
doAssert(set.containsAll(set3) == FALSE, "set doesn't contain all the elements in set3");
|
|
|
|
set3.remove(0x0062);
|
|
|
|
expectPairs(set3, "aacc");
|
|
|
|
doAssert(set.containsAll(set3) == TRUE, "set should contain all the elements in set3");
|
|
|
|
set.retainAll(set3);
|
|
|
|
expectPairs(set, "aacc");
|
|
|
|
doAssert(set.size() == set3.size(), "set.size() should be set3.size()");
|
|
|
|
doAssert(set.containsAll(set3) == TRUE, "set should contain all the elements in set3");
|
|
|
|
set.clear();
|
|
|
|
doAssert(set.size() != set3.size(), "set.size() != set3.size()");
|
1999-10-20 22:08:09 +00:00
|
|
|
|
1999-10-26 17:16:13 +00:00
|
|
|
// Test commutativity
|
|
|
|
expectPattern(set, "[hitoshinamekatajamesanderson]", "aadehkmort");
|
|
|
|
expectPattern(set2, "[jackiemclean]", "aacceein");
|
|
|
|
set.addAll(set2);
|
|
|
|
expectPairs(set, "aacehort");
|
2000-08-14 21:42:36 +00:00
|
|
|
doAssert(set.containsAll(set2) == TRUE, "set should contain all the elements in set2");
|
2000-03-28 23:51:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
1999-10-26 17:16:13 +00:00
|
|
|
|
2000-03-28 23:51:25 +00:00
|
|
|
}
|
2000-05-25 19:28:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure minimal representation is maintained.
|
|
|
|
*/
|
|
|
|
void UnicodeSetTest::TestMinimalRep() {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
// This is pretty thoroughly tested by checkCanonicalRep()
|
|
|
|
// run against the exhaustive operation results. Use the code
|
|
|
|
// here for debugging specific spot problems.
|
|
|
|
|
|
|
|
// 1 overlap against 2
|
|
|
|
UnicodeSet set("[h-km-q]", status);
|
|
|
|
if (U_FAILURE(status)) { errln("FAIL"); return; }
|
|
|
|
UnicodeSet set2("[i-o]", status);
|
|
|
|
if (U_FAILURE(status)) { errln("FAIL"); return; }
|
|
|
|
set.addAll(set2);
|
|
|
|
expectPairs(set, "hq");
|
|
|
|
// right
|
|
|
|
set.applyPattern("[a-m]", status);
|
|
|
|
if (U_FAILURE(status)) { errln("FAIL"); return; }
|
|
|
|
set2.applyPattern("[e-o]", status);
|
|
|
|
if (U_FAILURE(status)) { errln("FAIL"); return; }
|
|
|
|
set.addAll(set2);
|
|
|
|
expectPairs(set, "ao");
|
|
|
|
// left
|
|
|
|
set.applyPattern("[e-o]", status);
|
|
|
|
if (U_FAILURE(status)) { errln("FAIL"); return; }
|
|
|
|
set2.applyPattern("[a-m]", status);
|
|
|
|
if (U_FAILURE(status)) { errln("FAIL"); return; }
|
|
|
|
set.addAll(set2);
|
|
|
|
expectPairs(set, "ao");
|
|
|
|
// 1 overlap against 3
|
|
|
|
set.applyPattern("[a-eg-mo-w]", status);
|
|
|
|
if (U_FAILURE(status)) { errln("FAIL"); return; }
|
|
|
|
set2.applyPattern("[d-q]", status);
|
|
|
|
if (U_FAILURE(status)) { errln("FAIL"); return; }
|
|
|
|
set.addAll(set2);
|
|
|
|
expectPairs(set, "aw");
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnicodeSetTest::TestAPI() {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
// default ct
|
|
|
|
UnicodeSet set;
|
|
|
|
if (!set.isEmpty() || set.getRangeCount() != 0) {
|
|
|
|
errln((UnicodeString)"FAIL, set should be empty but isn't: " +
|
|
|
|
set);
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear(), isEmpty()
|
2000-06-28 22:26:59 +00:00
|
|
|
set.add(0x0061);
|
2000-05-25 19:28:53 +00:00
|
|
|
if (set.isEmpty()) {
|
|
|
|
errln((UnicodeString)"FAIL, set shouldn't be empty but is: " +
|
|
|
|
set);
|
|
|
|
}
|
|
|
|
set.clear();
|
|
|
|
if (!set.isEmpty()) {
|
|
|
|
errln((UnicodeString)"FAIL, set should be empty but isn't: " +
|
|
|
|
set);
|
|
|
|
}
|
|
|
|
|
|
|
|
// size()
|
|
|
|
set.clear();
|
|
|
|
if (set.size() != 0) {
|
|
|
|
errln((UnicodeString)"FAIL, size should be 0, but is " + set.size() +
|
|
|
|
": " + set);
|
|
|
|
}
|
2000-06-28 22:26:59 +00:00
|
|
|
set.add(0x0061);
|
2000-05-25 19:28:53 +00:00
|
|
|
if (set.size() != 1) {
|
|
|
|
errln((UnicodeString)"FAIL, size should be 1, but is " + set.size() +
|
|
|
|
": " + set);
|
|
|
|
}
|
2000-06-28 22:26:59 +00:00
|
|
|
set.add(0x0031, 0x0039);
|
2000-05-25 19:28:53 +00:00
|
|
|
if (set.size() != 10) {
|
|
|
|
errln((UnicodeString)"FAIL, size should be 10, but is " + set.size() +
|
|
|
|
": " + set);
|
|
|
|
}
|
|
|
|
|
|
|
|
// contains(first, last)
|
|
|
|
set.clear();
|
|
|
|
set.applyPattern("[A-Y 1-8 b-d l-y]", status);
|
|
|
|
if (U_FAILURE(status)) { errln("FAIL"); return; }
|
|
|
|
for (int32_t i = 0; i<set.getRangeCount(); ++i) {
|
|
|
|
UChar32 a = set.getRangeStart(i);
|
|
|
|
UChar32 b = set.getRangeEnd(i);
|
|
|
|
if (!set.contains(a, b)) {
|
2000-06-02 01:10:20 +00:00
|
|
|
errln((UnicodeString)"FAIL, should contain " + (unsigned short)a + '-' + (unsigned short)b +
|
2000-05-25 19:28:53 +00:00
|
|
|
" but doesn't: " + set);
|
|
|
|
}
|
|
|
|
if (set.contains((UChar32)(a-1), b)) {
|
|
|
|
errln((UnicodeString)"FAIL, shouldn't contain " +
|
2000-06-02 01:10:20 +00:00
|
|
|
(unsigned short)(a-1) + '-' + (unsigned short)b +
|
2000-05-25 19:28:53 +00:00
|
|
|
" but does: " + set);
|
|
|
|
}
|
|
|
|
if (set.contains(a, (UChar32)(b+1))) {
|
|
|
|
errln((UnicodeString)"FAIL, shouldn't contain " +
|
2000-06-02 01:10:20 +00:00
|
|
|
(unsigned short)a + '-' + (unsigned short)(b+1) +
|
2000-05-25 19:28:53 +00:00
|
|
|
" but does: " + set);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ported InversionList test.
|
|
|
|
UnicodeSet a((UChar32)3,(UChar32)10);
|
|
|
|
UnicodeSet b((UChar32)7,(UChar32)15);
|
|
|
|
UnicodeSet c;
|
|
|
|
|
|
|
|
logln((UnicodeString)"a [3-10]: " + a);
|
|
|
|
logln((UnicodeString)"b [7-15]: " + b);
|
|
|
|
c = a; c.addAll(b);
|
|
|
|
UnicodeSet exp((UChar32)3,(UChar32)15);
|
|
|
|
if (c == exp) {
|
|
|
|
logln((UnicodeString)"c.set(a).add(b): " + c);
|
|
|
|
} else {
|
|
|
|
errln((UnicodeString)"FAIL: c.set(a).add(b) = " + c + ", expect " + exp);
|
|
|
|
}
|
|
|
|
c.complement();
|
|
|
|
exp.set((UChar32)0, (UChar32)2);
|
2000-05-26 02:07:25 +00:00
|
|
|
exp.add((UChar32)16, UnicodeSet::MAX_VALUE);
|
2000-05-25 19:28:53 +00:00
|
|
|
if (c == exp) {
|
|
|
|
logln((UnicodeString)"c.complement(): " + c);
|
|
|
|
} else {
|
|
|
|
errln((UnicodeString)"FAIL: c.complement() = " + c + ", expect " + exp);
|
|
|
|
}
|
|
|
|
c.complement();
|
|
|
|
exp.set((UChar32)3, (UChar32)15);
|
|
|
|
if (c == exp) {
|
|
|
|
logln((UnicodeString)"c.complement(): " + c);
|
|
|
|
} else {
|
|
|
|
errln((UnicodeString)"FAIL: c.complement() = " + c + ", expect " + exp);
|
|
|
|
}
|
2000-05-26 18:32:15 +00:00
|
|
|
c = a; c.complementAll(b);
|
2000-05-25 19:28:53 +00:00
|
|
|
exp.set((UChar32)3,(UChar32)6);
|
|
|
|
exp.add((UChar32)11,(UChar32) 15);
|
|
|
|
if (c == exp) {
|
2000-05-26 16:40:29 +00:00
|
|
|
logln((UnicodeString)"c.set(a).exclusiveOr(b): " + c);
|
2000-05-25 19:28:53 +00:00
|
|
|
} else {
|
2000-05-26 16:40:29 +00:00
|
|
|
errln((UnicodeString)"FAIL: c.set(a).exclusiveOr(b) = " + c + ", expect " + exp);
|
2000-05-25 19:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exp = c;
|
2000-05-25 20:57:30 +00:00
|
|
|
bitsToSet(setToBits(c), c);
|
2000-05-25 19:28:53 +00:00
|
|
|
if (c == exp) {
|
|
|
|
logln((UnicodeString)"bitsToSet(setToBits(c)): " + c);
|
|
|
|
} else {
|
|
|
|
errln((UnicodeString)"FAIL: bitsToSet(setToBits(c)) = " + c + ", expect " + exp);
|
2000-03-28 23:51:25 +00:00
|
|
|
}
|
|
|
|
}
|
2000-05-25 19:28:53 +00:00
|
|
|
|
|
|
|
void UnicodeSetTest::TestExhaustive() {
|
|
|
|
// exhaustive tests. Simulate UnicodeSets with integers.
|
|
|
|
// That gives us very solid tests (except for large memory tests).
|
|
|
|
|
|
|
|
UChar32 limit = (UChar32)128;
|
|
|
|
|
2000-05-25 20:57:30 +00:00
|
|
|
UnicodeSet x, y, z;
|
|
|
|
|
2000-05-25 19:28:53 +00:00
|
|
|
for (UChar32 i = 0; i < limit; ++i) {
|
2000-05-25 20:57:30 +00:00
|
|
|
bitsToSet(i, x);
|
|
|
|
logln((UnicodeString)"Testing " + i + ", " + x);
|
|
|
|
_testComplement(i, x, y);
|
2000-05-25 19:28:53 +00:00
|
|
|
for (UChar32 j = 0; j < limit; ++j) {
|
2000-05-25 20:57:30 +00:00
|
|
|
_testAdd(i,j, x,y,z);
|
|
|
|
_testXor(i,j, x,y,z);
|
|
|
|
_testRetain(i,j, x,y,z);
|
|
|
|
_testRemove(i,j, x,y,z);
|
2000-05-25 19:28:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-25 20:57:30 +00:00
|
|
|
void UnicodeSetTest::_testComplement(int32_t a, UnicodeSet& x, UnicodeSet& z) {
|
|
|
|
bitsToSet(a, x);
|
|
|
|
z = x;
|
2000-05-25 19:28:53 +00:00
|
|
|
z.complement();
|
|
|
|
int32_t c = setToBits(z);
|
|
|
|
if (c != (~a)) {
|
|
|
|
errln((UnicodeString)"FAILED: add: ~" + x + " != " + z);
|
|
|
|
errln((UnicodeString)"FAILED: add: ~" + a + " != " + c);
|
|
|
|
}
|
|
|
|
checkCanonicalRep(z, "complement " + a);
|
|
|
|
}
|
|
|
|
|
2000-05-25 20:57:30 +00:00
|
|
|
void UnicodeSetTest::_testAdd(int32_t a, int32_t b, UnicodeSet& x, UnicodeSet& y, UnicodeSet& z) {
|
|
|
|
bitsToSet(a, x);
|
|
|
|
bitsToSet(b, y);
|
|
|
|
z = x;
|
2000-05-25 19:28:53 +00:00
|
|
|
z.addAll(y);
|
|
|
|
int32_t c = setToBits(z);
|
|
|
|
if (c != (a | b)) {
|
|
|
|
errln((UnicodeString)"FAILED: add: " + x + " | " + y + " != " + z);
|
|
|
|
errln((UnicodeString)"FAILED: add: " + a + " | " + b + " != " + c);
|
|
|
|
}
|
|
|
|
checkCanonicalRep(z, (UnicodeString)"add " + a + "," + b);
|
|
|
|
}
|
|
|
|
|
2000-05-25 20:57:30 +00:00
|
|
|
void UnicodeSetTest::_testRetain(int32_t a, int32_t b, UnicodeSet& x, UnicodeSet& y, UnicodeSet& z) {
|
|
|
|
bitsToSet(a, x);
|
|
|
|
bitsToSet(b, y);
|
|
|
|
z = x;
|
2000-05-25 19:28:53 +00:00
|
|
|
z.retainAll(y);
|
|
|
|
int32_t c = setToBits(z);
|
|
|
|
if (c != (a & b)) {
|
|
|
|
errln((UnicodeString)"FAILED: retain: " + x + " & " + y + " != " + z);
|
|
|
|
errln((UnicodeString)"FAILED: retain: " + a + " & " + b + " != " + c);
|
|
|
|
}
|
|
|
|
checkCanonicalRep(z, (UnicodeString)"retain " + a + "," + b);
|
|
|
|
}
|
|
|
|
|
2000-05-25 20:57:30 +00:00
|
|
|
void UnicodeSetTest::_testRemove(int32_t a, int32_t b, UnicodeSet& x, UnicodeSet& y, UnicodeSet& z) {
|
|
|
|
bitsToSet(a, x);
|
|
|
|
bitsToSet(b, y);
|
|
|
|
z = x;
|
2000-05-25 19:28:53 +00:00
|
|
|
z.removeAll(y);
|
|
|
|
int32_t c = setToBits(z);
|
|
|
|
if (c != (a &~ b)) {
|
|
|
|
errln((UnicodeString)"FAILED: remove: " + x + " &~ " + y + " != " + z);
|
|
|
|
errln((UnicodeString)"FAILED: remove: " + a + " &~ " + b + " != " + c);
|
|
|
|
}
|
|
|
|
checkCanonicalRep(z, (UnicodeString)"remove " + a + "," + b);
|
|
|
|
}
|
|
|
|
|
2000-05-25 20:57:30 +00:00
|
|
|
void UnicodeSetTest::_testXor(int32_t a, int32_t b, UnicodeSet& x, UnicodeSet& y, UnicodeSet& z) {
|
|
|
|
bitsToSet(a, x);
|
|
|
|
bitsToSet(b, y);
|
|
|
|
z = x;
|
2000-05-26 18:32:15 +00:00
|
|
|
z.complementAll(y);
|
2000-05-25 19:28:53 +00:00
|
|
|
int32_t c = setToBits(z);
|
|
|
|
if (c != (a ^ b)) {
|
2000-05-26 18:32:15 +00:00
|
|
|
errln((UnicodeString)"FAILED: complement: " + x + " ^ " + y + " != " + z);
|
|
|
|
errln((UnicodeString)"FAILED: complement: " + a + " ^ " + b + " != " + c);
|
2000-05-25 19:28:53 +00:00
|
|
|
}
|
2000-05-26 18:32:15 +00:00
|
|
|
checkCanonicalRep(z, (UnicodeString)"complement " + a + "," + b);
|
2000-05-25 19:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that ranges are monotonically increasing and non-
|
|
|
|
* overlapping.
|
|
|
|
*/
|
|
|
|
void UnicodeSetTest::checkCanonicalRep(const UnicodeSet& set, const UnicodeString& msg) {
|
|
|
|
int32_t n = set.getRangeCount();
|
|
|
|
if (n < 0) {
|
|
|
|
errln((UnicodeString)"FAIL result of " + msg +
|
|
|
|
": range count should be >= 0 but is " +
|
|
|
|
n /*+ " for " + set.toPattern())*/);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UChar32 last = 0;
|
|
|
|
for (int32_t i=0; i<n; ++i) {
|
|
|
|
UChar32 start = set.getRangeStart(i);
|
|
|
|
UChar32 end = set.getRangeEnd(i);
|
|
|
|
if (start > end) {
|
|
|
|
errln((UnicodeString)"FAIL result of " + msg +
|
|
|
|
": range " + (i+1) +
|
|
|
|
" start > end: " + start + ", " + end +
|
|
|
|
" for " + set);
|
|
|
|
}
|
|
|
|
if (i > 0 && start <= last) {
|
|
|
|
errln((UnicodeString)"FAIL result of " + msg +
|
|
|
|
": range " + (i+1) +
|
|
|
|
" overlaps previous range: " + start + ", " + end +
|
|
|
|
" for " + set);
|
|
|
|
}
|
|
|
|
last = end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a bitmask to a UnicodeSet.
|
|
|
|
*/
|
2000-05-25 20:57:30 +00:00
|
|
|
void UnicodeSetTest::bitsToSet(int32_t a, UnicodeSet& result) {
|
|
|
|
result.clear();
|
|
|
|
for (UChar32 i = 0; i < 32; ++i) {
|
2000-05-25 19:28:53 +00:00
|
|
|
if ((a & (1<<i)) != 0) {
|
2000-05-25 20:57:30 +00:00
|
|
|
result.add(i);
|
2000-05-25 19:28:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a UnicodeSet to a bitmask. Only the characters
|
|
|
|
* U+0000 to U+0020 are represented in the bitmask.
|
|
|
|
*/
|
|
|
|
int32_t UnicodeSetTest::setToBits(const UnicodeSet& x) {
|
|
|
|
int32_t result = 0;
|
|
|
|
for (int32_t i = 0; i < 32; ++i) {
|
|
|
|
if (x.contains((UChar32)i)) {
|
|
|
|
result |= (1<<i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the representation of an inversion list based UnicodeSet
|
|
|
|
* as a pairs list. Ranges are listed in ascending Unicode order.
|
|
|
|
* For example, the set [a-zA-M3] is represented as "33AMaz".
|
|
|
|
*/
|
|
|
|
UnicodeString UnicodeSetTest::getPairs(const UnicodeSet& set) {
|
|
|
|
UnicodeString pairs;
|
|
|
|
for (int32_t i=0; i<set.getRangeCount(); ++i) {
|
|
|
|
UChar32 start = set.getRangeStart(i);
|
|
|
|
UChar32 end = set.getRangeEnd(i);
|
|
|
|
if (end > 0xFFFF) {
|
|
|
|
end = 0xFFFF;
|
|
|
|
i = set.getRangeCount(); // Should be unnecessary
|
|
|
|
}
|
|
|
|
pairs.append((UChar)start).append((UChar)end);
|
|
|
|
}
|
|
|
|
return pairs;
|
|
|
|
}
|
|
|
|
|
2000-01-12 18:04:21 +00:00
|
|
|
void
|
|
|
|
UnicodeSetTest::expectContainment(const UnicodeSet& set,
|
|
|
|
const UnicodeString& setName,
|
|
|
|
const UnicodeString& charsIn,
|
|
|
|
const UnicodeString& charsOut) {
|
|
|
|
UnicodeString bad;
|
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<charsIn.length(); ++i) {
|
|
|
|
UChar c = charsIn.charAt(i);
|
|
|
|
if (!set.contains(c)) {
|
|
|
|
bad.append(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bad.length() > 0) {
|
|
|
|
logln((UnicodeString)"Fail: set " + setName + " does not contain " + bad +
|
|
|
|
", expected containment of " + charsIn);
|
|
|
|
} else {
|
|
|
|
logln((UnicodeString)"Ok: set " + setName + " contains " + charsIn);
|
|
|
|
}
|
|
|
|
|
|
|
|
bad.truncate(0);
|
|
|
|
for (i=0; i<charsOut.length(); ++i) {
|
|
|
|
UChar c = charsOut.charAt(i);
|
|
|
|
if (set.contains(c)) {
|
|
|
|
bad.append(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bad.length() > 0) {
|
|
|
|
logln((UnicodeString)"Fail: set " + setName + " contains " + bad +
|
|
|
|
", expected non-containment of " + charsOut);
|
|
|
|
} else {
|
|
|
|
logln((UnicodeString)"Ok: set " + setName + " does not contain " + charsOut);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-26 17:16:13 +00:00
|
|
|
void
|
|
|
|
UnicodeSetTest::expectPattern(UnicodeSet& set,
|
|
|
|
const UnicodeString& pattern,
|
2000-03-28 23:51:25 +00:00
|
|
|
const UnicodeString& expectedPairs){
|
1999-10-26 17:16:13 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2000-08-14 21:42:36 +00:00
|
|
|
set.applyPattern(pattern, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
errln(UnicodeString("FAIL: applyPattern(\"") + pattern +
|
1999-10-26 17:16:13 +00:00
|
|
|
"\") failed");
|
2000-08-14 21:42:36 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2000-05-25 19:28:53 +00:00
|
|
|
if (getPairs(set) != expectedPairs ) {
|
1999-10-26 17:16:13 +00:00
|
|
|
errln(UnicodeString("FAIL: applyPattern(\"") + pattern +
|
|
|
|
"\") => pairs \"" +
|
2000-05-25 19:28:53 +00:00
|
|
|
escape(getPairs(set)) + "\", expected \"" +
|
1999-10-26 17:16:13 +00:00
|
|
|
escape(expectedPairs) + "\"");
|
|
|
|
} else {
|
|
|
|
logln(UnicodeString("Ok: applyPattern(\"") + pattern +
|
|
|
|
"\") => pairs \"" +
|
2000-05-25 19:28:53 +00:00
|
|
|
escape(getPairs(set)) + "\"");
|
1999-10-26 17:16:13 +00:00
|
|
|
}
|
|
|
|
}
|
2000-08-14 21:42:36 +00:00
|
|
|
// the result of calling set.toPattern(), which is the string representation of
|
|
|
|
// this set(set), is passed to a UnicodeSet constructor, and tested that it
|
|
|
|
// will produce another set that is equal to this one.
|
|
|
|
UnicodeString temppattern;
|
|
|
|
set.toPattern(temppattern);
|
|
|
|
UnicodeSet *tempset=new UnicodeSet(temppattern, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
errln(UnicodeString("FAIL: Construction with the pattern derived from toPattern() failed"));
|
2000-03-28 23:51:25 +00:00
|
|
|
return;
|
2000-08-14 21:42:36 +00:00
|
|
|
}
|
|
|
|
if(*tempset != set || getPairs(*tempset) != getPairs(set)){
|
|
|
|
errln(UnicodeString("FAIL: "+ pattern + "!=>" + temppattern + ". Pairs \""+ escape(getPairs(*tempset)) + "\" expected->\"" +
|
|
|
|
escape(getPairs(set)) + "\""));
|
|
|
|
} else{
|
|
|
|
logln(UnicodeString("OK: "+ pattern + "==>" + temppattern + ". Pairs \"" + escape(getPairs(*tempset)) + "\""));
|
|
|
|
}
|
|
|
|
|
|
|
|
delete tempset;
|
|
|
|
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-10-26 17:16:13 +00:00
|
|
|
UnicodeSetTest::expectPairs(const UnicodeSet& set, const UnicodeString& expectedPairs) {
|
2000-05-25 19:28:53 +00:00
|
|
|
if (getPairs(set) != expectedPairs) {
|
1999-10-20 22:08:09 +00:00
|
|
|
errln(UnicodeString("FAIL: Expected pair list \"") +
|
|
|
|
escape(expectedPairs) + "\", got \"" +
|
2000-05-25 19:28:53 +00:00
|
|
|
escape(getPairs(set)) + "\"");
|
1999-10-20 22:08:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-10 00:28:31 +00:00
|
|
|
static UChar toHexString(int32_t i) { return (UChar)(i + (i < 10 ? 0x30 : (0x41 - 10))); }
|
1999-10-20 22:08:09 +00:00
|
|
|
|
2000-05-25 19:28:53 +00:00
|
|
|
void
|
|
|
|
UnicodeSetTest::doAssert(UBool condition, const char *message)
|
|
|
|
{
|
|
|
|
if (!condition) {
|
|
|
|
errln(UnicodeString("ERROR : ") + message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-20 22:08:09 +00:00
|
|
|
UnicodeString
|
|
|
|
UnicodeSetTest::escape(const UnicodeString& s) {
|
|
|
|
UnicodeString buf;
|
1999-12-08 02:11:04 +00:00
|
|
|
for (int32_t i=0; i<s.length(); ++i)
|
1999-10-20 22:08:09 +00:00
|
|
|
{
|
|
|
|
UChar c = s[(UTextOffset)i];
|
2000-06-28 22:26:59 +00:00
|
|
|
if (0x0020 <= c && c <= 0x007F) {
|
1999-10-26 17:16:13 +00:00
|
|
|
buf += c;
|
|
|
|
} else {
|
2000-04-03 23:53:59 +00:00
|
|
|
buf += (UChar)0x5c; buf += (UChar)0x55;
|
1999-10-20 22:08:09 +00:00
|
|
|
buf += toHexString((c & 0xF000) >> 12);
|
|
|
|
buf += toHexString((c & 0x0F00) >> 8);
|
|
|
|
buf += toHexString((c & 0x00F0) >> 4);
|
|
|
|
buf += toHexString(c & 0x000F);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|