1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2010-2014, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: ucharstrietest.cpp
10 * tab size: 8 (not used)
13 * created on: 2010nov16
14 * created by: Markus W. Scherer
19 #include "unicode/utypes.h"
20 #include "unicode/appendable.h"
21 #include "unicode/localpointer.h"
22 #include "unicode/ucharstrie.h"
23 #include "unicode/ucharstriebuilder.h"
24 #include "unicode/uniset.h"
25 #include "unicode/unistr.h"
29 struct StringAndValue {
34 class UCharsTrieTest : public IntlTest {
37 virtual ~UCharsTrieTest();
39 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=NULL);
44 void TestShortestBranch();
46 void TestLongSequence();
47 void TestLongBranch();
48 void TestValuesForState();
50 void TestFirstForCodePoint();
51 void TestNextForCodePoint();
53 UCharsTrie *buildLargeTrie(int32_t numUniqueFirst);
56 UCharsTrie *buildMonthsTrie(UStringTrieBuildOption buildOption);
57 void TestHasUniqueValue();
58 void TestGetNextUChars();
59 void TestIteratorFromBranch();
60 void TestIteratorFromLinearMatch();
61 void TestTruncatingIteratorFromRoot();
62 void TestTruncatingIteratorFromLinearMatchShort();
63 void TestTruncatingIteratorFromLinearMatchLong();
64 void TestIteratorFromUChars();
66 void checkData(const StringAndValue data[], int32_t dataLength);
67 void checkData(const StringAndValue data[], int32_t dataLength, UStringTrieBuildOption buildOption);
68 UCharsTrie *buildTrie(const StringAndValue data[], int32_t dataLength,
69 UStringTrieBuildOption buildOption);
70 void checkFirst(UCharsTrie &trie, const StringAndValue data[], int32_t dataLength);
71 void checkNext(UCharsTrie &trie, const StringAndValue data[], int32_t dataLength);
72 void checkNextWithState(UCharsTrie &trie, const StringAndValue data[], int32_t dataLength);
73 void checkNextString(UCharsTrie &trie, const StringAndValue data[], int32_t dataLength);
74 void checkIterator(UCharsTrie &trie, const StringAndValue data[], int32_t dataLength);
75 void checkIterator(UCharsTrie::Iterator &iter, const StringAndValue data[], int32_t dataLength);
78 UCharsTrieBuilder *builder_;
81 extern IntlTest *createUCharsTrieTest() {
82 return new UCharsTrieTest();
85 UCharsTrieTest::UCharsTrieTest() : builder_(NULL) {
86 IcuTestErrorCode errorCode(*this, "UCharsTrieTest()");
87 builder_=new UCharsTrieBuilder(errorCode);
90 UCharsTrieTest::~UCharsTrieTest() {
94 void UCharsTrieTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
96 logln("TestSuite UCharsTrieTest: ");
99 TESTCASE_AUTO(TestBuilder);
100 TESTCASE_AUTO(TestEmpty);
101 TESTCASE_AUTO(Test_a);
102 TESTCASE_AUTO(Test_a_ab);
103 TESTCASE_AUTO(TestShortestBranch);
104 TESTCASE_AUTO(TestBranches);
105 TESTCASE_AUTO(TestLongSequence);
106 TESTCASE_AUTO(TestLongBranch);
107 TESTCASE_AUTO(TestValuesForState);
108 TESTCASE_AUTO(TestCompact);
109 TESTCASE_AUTO(TestFirstForCodePoint);
110 TESTCASE_AUTO(TestNextForCodePoint);
111 TESTCASE_AUTO(TestLargeTrie);
112 TESTCASE_AUTO(TestHasUniqueValue);
113 TESTCASE_AUTO(TestGetNextUChars);
114 TESTCASE_AUTO(TestIteratorFromBranch);
115 TESTCASE_AUTO(TestIteratorFromLinearMatch);
116 TESTCASE_AUTO(TestTruncatingIteratorFromRoot);
117 TESTCASE_AUTO(TestTruncatingIteratorFromLinearMatchShort);
118 TESTCASE_AUTO(TestTruncatingIteratorFromLinearMatchLong);
119 TESTCASE_AUTO(TestIteratorFromUChars);
123 void UCharsTrieTest::TestBuilder() {
124 IcuTestErrorCode errorCode(*this, "TestBuilder()");
125 delete builder_->build(USTRINGTRIE_BUILD_FAST, errorCode);
126 if(errorCode.reset()!=U_INDEX_OUTOFBOUNDS_ERROR) {
127 errln("UCharsTrieBuilder().build() did not set U_INDEX_OUTOFBOUNDS_ERROR");
130 // TODO: remove .build(...) once add() checks for duplicates.
131 builder_->add("=", 0, errorCode).add("=", 1, errorCode).build(USTRINGTRIE_BUILD_FAST, errorCode);
132 if(errorCode.reset()!=U_ILLEGAL_ARGUMENT_ERROR) {
133 errln("UCharsTrieBuilder.add() did not detect duplicates");
138 void UCharsTrieTest::TestEmpty() {
139 static const StringAndValue data[]={
142 checkData(data, UPRV_LENGTHOF(data));
145 void UCharsTrieTest::Test_a() {
146 static const StringAndValue data[]={
149 checkData(data, UPRV_LENGTHOF(data));
152 void UCharsTrieTest::Test_a_ab() {
153 static const StringAndValue data[]={
157 checkData(data, UPRV_LENGTHOF(data));
160 void UCharsTrieTest::TestShortestBranch() {
161 static const StringAndValue data[]={
165 checkData(data, UPRV_LENGTHOF(data));
168 void UCharsTrieTest::TestBranches() {
169 static const StringAndValue data[]={
177 { "ppppp", 0x40000 },
182 { "vv", 0x7fffffff },
183 { "zz", (int32_t)0x80000000 }
185 for(int32_t length=2; length<=UPRV_LENGTHOF(data); ++length) {
186 logln("TestBranches length=%d", (int)length);
187 checkData(data, length);
191 void UCharsTrieTest::TestLongSequence() {
192 static const StringAndValue data[]={
194 // sequence of linear-match nodes
195 { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -2 },
196 // more than 256 units
197 { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
198 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
199 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
200 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
201 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
202 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -3 }
204 checkData(data, UPRV_LENGTHOF(data));
207 void UCharsTrieTest::TestLongBranch() {
208 // Split-branch and interesting compact-integer values.
209 static const StringAndValue data[]={
222 { "l234567890", 0x1b00 },
223 { "l234567890123", 0x1b01 },
224 { "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn", 0x10ffff },
225 { "oooooooooooooooooooooooooooooooooooooooooooooooooooooo", 0x110000 },
226 { "pppppppppppppppppppppppppppppppppppppppppppppppppppppp", 0x120000 },
228 { "s2345", 0x4444444 },
229 { "t234567890", 0x77777777 },
230 { "z", (int32_t)0x80000001 }
232 checkData(data, UPRV_LENGTHOF(data));
235 void UCharsTrieTest::TestValuesForState() {
236 // Check that saveState() and resetToState() interact properly
237 // with next() and current().
238 static const StringAndValue data[]={
246 checkData(data, UPRV_LENGTHOF(data));
249 void UCharsTrieTest::TestCompact() {
250 // Duplicate trailing strings and values provide opportunities for compacting.
251 static const StringAndValue data[]={
268 // The l+n branch (with its sub-nodes) is a duplicate but will be written
269 // both times because each time it follows a different linear-match node.
273 checkData(data, UPRV_LENGTHOF(data));
276 void UCharsTrieTest::TestFirstForCodePoint() {
277 static const StringAndValue data[]={
280 { "a\\U00010000", 3 },
282 { "\\U00020000\\udbff", 5 },
283 { "\\U00020000\\U0010ffff", 6 },
284 { "\\U00020000\\U0010ffffz", 7 },
285 { "\\U00050000xy", 8 },
286 { "\\U00050000xyz", 9 }
288 checkData(data, UPRV_LENGTHOF(data));
291 void UCharsTrieTest::TestNextForCodePoint() {
292 static const StringAndValue data[]={
293 { "\\u4dff\\U00010000\\u9999\\U00020000\\udfff\\U0010ffff", 2000000000 },
294 { "\\u4dff\\U00010000\\u9999\\U00020002", 44444 },
295 { "\\u4dff\\U000103ff", 99999 }
297 LocalPointer<UCharsTrie> trie(buildTrie(data, UPRV_LENGTHOF(data), USTRINGTRIE_BUILD_FAST));
299 return; // buildTrie() reported an error
301 UStringTrieResult result;
302 if( (result=trie->nextForCodePoint(0x4dff))!=USTRINGTRIE_NO_VALUE || result!=trie->current() ||
303 (result=trie->nextForCodePoint(0x10000))!=USTRINGTRIE_NO_VALUE || result!=trie->current() ||
304 (result=trie->nextForCodePoint(0x9999))!=USTRINGTRIE_NO_VALUE || result!=trie->current() ||
305 (result=trie->nextForCodePoint(0x20000))!=USTRINGTRIE_NO_VALUE || result!=trie->current() ||
306 (result=trie->nextForCodePoint(0xdfff))!=USTRINGTRIE_NO_VALUE || result!=trie->current() ||
307 (result=trie->nextForCodePoint(0x10ffff))!=USTRINGTRIE_FINAL_VALUE || result!=trie->current() ||
308 trie->getValue()!=2000000000
310 errln("UCharsTrie.nextForCodePoint() fails for %s", data[0].s);
312 if( (result=trie->firstForCodePoint(0x4dff))!=USTRINGTRIE_NO_VALUE || result!=trie->current() ||
313 (result=trie->nextForCodePoint(0x10000))!=USTRINGTRIE_NO_VALUE || result!=trie->current() ||
314 (result=trie->nextForCodePoint(0x9999))!=USTRINGTRIE_NO_VALUE || result!=trie->current() ||
315 (result=trie->nextForCodePoint(0x20002))!=USTRINGTRIE_FINAL_VALUE || result!=trie->current() ||
316 trie->getValue()!=44444
318 errln("UCharsTrie.nextForCodePoint() fails for %s", data[1].s);
320 if( (result=trie->reset().nextForCodePoint(0x4dff))!=USTRINGTRIE_NO_VALUE || result!=trie->current() ||
321 (result=trie->nextForCodePoint(0x10000))!=USTRINGTRIE_NO_VALUE || result!=trie->current() ||
322 (result=trie->nextForCodePoint(0x9999))!=USTRINGTRIE_NO_VALUE || result!=trie->current() ||
323 (result=trie->nextForCodePoint(0x20222))!=USTRINGTRIE_NO_MATCH || result!=trie->current() // no match for trail surrogate
325 errln("UCharsTrie.nextForCodePoint() fails for \\u4dff\\U00010000\\u9999\\U00020222");
327 if( (result=trie->reset().nextForCodePoint(0x4dff))!=USTRINGTRIE_NO_VALUE || result!=trie->current() ||
328 (result=trie->nextForCodePoint(0x103ff))!=USTRINGTRIE_FINAL_VALUE || result!=trie->current() ||
329 trie->getValue()!=99999
331 errln("UCharsTrie.nextForCodePoint() fails for %s", data[2].s);
335 // Definitions in the anonymous namespace are invisible outside this file.
338 // Generate (string, value) pairs.
339 // The first string (before next()) will be empty.
342 Generator() : value(4711), num(0) {}
346 s.append(c=(UChar)(value>>16));
347 s.append((UChar)(value>>4));
349 s.append((UChar)value);
352 value+=((value>>5)&0x7ff)*3+1;
355 const UnicodeString &getString() const { return s; }
356 int32_t getValue() const { return value; }
357 int32_t countUniqueFirstChars() const { return set.size(); }
358 int32_t getIndex() const { return num; }
369 UCharsTrie *UCharsTrieTest::buildLargeTrie(int32_t numUniqueFirst) {
370 IcuTestErrorCode errorCode(*this, "buildLargeTrie()");
373 while(gen.countUniqueFirstChars()<numUniqueFirst) {
374 builder_->add(gen.getString(), gen.getValue(), errorCode);
377 logln("buildLargeTrie(%ld) added %ld strings", (long)numUniqueFirst, (long)gen.getIndex());
378 UnicodeString trieUChars;
379 builder_->buildUnicodeString(USTRINGTRIE_BUILD_FAST, trieUChars, errorCode);
380 logln("serialized trie size: %ld UChars\n", (long)trieUChars.length());
381 return new UCharsTrie(trieUChars.getBuffer());
384 // Exercise a large branch node.
385 void UCharsTrieTest::TestLargeTrie() {
386 LocalPointer<UCharsTrie> trie(buildLargeTrie(1111));
388 return; // buildTrie() reported an error
391 while(gen.countUniqueFirstChars()<1111) {
392 UnicodeString x(gen.getString());
393 int32_t value=gen.getValue();
395 if(trie->first(x[0])==USTRINGTRIE_NO_MATCH) {
396 errln("first(first char U+%04X)=USTRINGTRIE_NO_MATCH for string %ld\n",
397 x[0], (long)gen.getIndex());
402 UStringTrieResult result=trie->next(x.getBuffer(), x.length());
403 if(!USTRINGTRIE_HAS_VALUE(result) || result!=trie->current() || value!=trie->getValue()) {
404 errln("next(%d chars U+%04X U+%04X)!=hasValue or "
405 "next()!=current() or getValue() wrong "
406 "for string %ld\n", (int)x.length(), x[0], x[1], (long)gen.getIndex());
424 UCharsTrie *UCharsTrieTest::buildMonthsTrie(UStringTrieBuildOption buildOption) {
425 // All types of nodes leading to the same value,
426 // for code coverage of recursive functions.
427 // In particular, we need a lot of branches on some single level
428 // to exercise a split-branch node.
429 static const StringAndValue data[]={
449 { "jannnnnnnnnnnnnnnnnnnnnnnnnnnnn", 1 },
461 return buildTrie(data, UPRV_LENGTHOF(data), buildOption);
464 void UCharsTrieTest::TestHasUniqueValue() {
465 LocalPointer<UCharsTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_FAST));
467 return; // buildTrie() reported an error
470 if(trie->hasUniqueValue(uniqueValue)) {
471 errln("unique value at root");
476 // hasUniqueValue() directly after next()
477 if(!trie->hasUniqueValue(uniqueValue) || uniqueValue!=1) {
478 errln("not unique value 1 after \"jan\"");
482 if(trie->hasUniqueValue(uniqueValue)) {
483 errln("unique value after \"ju\"");
485 if(trie->next(u_n)!=USTRINGTRIE_INTERMEDIATE_VALUE || 6!=trie->getValue()) {
486 errln("not normal value 6 after \"jun\"");
488 // hasUniqueValue() after getValue()
489 if(!trie->hasUniqueValue(uniqueValue) || uniqueValue!=6) {
490 errln("not unique value 6 after \"jun\"");
492 // hasUniqueValue() from within a linear-match node
495 if(!trie->hasUniqueValue(uniqueValue) || uniqueValue!=8) {
496 errln("not unique value 8 after \"au\"");
500 void UCharsTrieTest::TestGetNextUChars() {
501 LocalPointer<UCharsTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_SMALL));
503 return; // buildTrie() reported an error
505 UnicodeString buffer;
506 UnicodeStringAppendable app(buffer);
507 int32_t count=trie->getNextUChars(app);
508 if(count!=2 || buffer.length()!=2 || buffer[0]!=u_a || buffer[1]!=u_j) {
509 errln("months getNextUChars()!=[aj] at root");
514 // getNextUChars() directly after next()
516 count=trie->getNextUChars(app);
517 if(count!=20 || buffer!=UNICODE_STRING_SIMPLE(".abcdefghijklmnopqru")) {
518 errln("months getNextUChars()!=[.abcdefghijklmnopqru] after \"jan\"");
520 // getNextUChars() after getValue()
521 trie->getValue(); // next() had returned USTRINGTRIE_INTERMEDIATE_VALUE.
523 count=trie->getNextUChars(app);
524 if(count!=20 || buffer!=UNICODE_STRING_SIMPLE(".abcdefghijklmnopqru")) {
525 errln("months getNextUChars()!=[.abcdefghijklmnopqru] after \"jan\"+getValue()");
527 // getNextUChars() from a linear-match node
530 count=trie->getNextUChars(app);
531 if(count!=1 || buffer.length()!=1 || buffer[0]!=u_a) {
532 errln("months getNextUChars()!=[a] after \"janu\"");
536 count=trie->getNextUChars(app);
537 if(count!=1 || buffer.length()!=1 || buffer[0]!=u_r) {
538 errln("months getNextUChars()!=[r] after \"janua\"");
542 // getNextUChars() after a final match
544 count=trie->getNextUChars(app);
545 if(count!=0 || buffer.length()!=0) {
546 errln("months getNextUChars()!=[] after \"january\"");
550 void UCharsTrieTest::TestIteratorFromBranch() {
551 LocalPointer<UCharsTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_FAST));
553 return; // buildTrie() reported an error
555 // Go to a branch node.
559 IcuTestErrorCode errorCode(*this, "TestIteratorFromBranch()");
560 UCharsTrie::Iterator iter(*trie, 0, errorCode);
561 if(errorCode.logIfFailureAndReset("UCharsTrie::Iterator(trie) constructor")) {
564 // Expected data: Same as in buildMonthsTrie(), except only the suffixes
566 static const StringAndValue data[]={
585 { "nnnnnnnnnnnnnnnnnnnnnnnnnnnn", 1 },
593 checkIterator(iter, data, UPRV_LENGTHOF(data));
594 // Reset, and we should get the same result.
595 logln("after iter.reset()");
596 checkIterator(iter.reset(), data, UPRV_LENGTHOF(data));
599 void UCharsTrieTest::TestIteratorFromLinearMatch() {
600 LocalPointer<UCharsTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_SMALL));
602 return; // buildTrie() reported an error
604 // Go into a linear-match node.
610 IcuTestErrorCode errorCode(*this, "TestIteratorFromLinearMatch()");
611 UCharsTrie::Iterator iter(*trie, 0, errorCode);
612 if(errorCode.logIfFailureAndReset("UCharsTrie::Iterator(trie) constructor")) {
615 // Expected data: Same as in buildMonthsTrie(), except only the suffixes
616 // following "janua".
617 static const StringAndValue data[]={
621 checkIterator(iter, data, UPRV_LENGTHOF(data));
622 // Reset, and we should get the same result.
623 logln("after iter.reset()");
624 checkIterator(iter.reset(), data, UPRV_LENGTHOF(data));
627 void UCharsTrieTest::TestTruncatingIteratorFromRoot() {
628 LocalPointer<UCharsTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_FAST));
630 return; // buildTrie() reported an error
632 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromRoot()");
633 UCharsTrie::Iterator iter(*trie, 4, errorCode);
634 if(errorCode.logIfFailureAndReset("UCharsTrie::Iterator(trie) constructor")) {
637 // Expected data: Same as in buildMonthsTrie(), except only the first 4 characters
638 // of each string, and no string duplicates from the truncation.
639 static const StringAndValue data[]={
667 checkIterator(iter, data, UPRV_LENGTHOF(data));
668 // Reset, and we should get the same result.
669 logln("after iter.reset()");
670 checkIterator(iter.reset(), data, UPRV_LENGTHOF(data));
673 void UCharsTrieTest::TestTruncatingIteratorFromLinearMatchShort() {
674 static const StringAndValue data[]={
679 LocalPointer<UCharsTrie> trie(buildTrie(data, UPRV_LENGTHOF(data), USTRINGTRIE_BUILD_FAST));
681 return; // buildTrie() reported an error
683 // Go into a linear-match node.
686 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromLinearMatchShort()");
687 // Truncate within the linear-match node.
688 UCharsTrie::Iterator iter(*trie, 2, errorCode);
689 if(errorCode.logIfFailureAndReset("UCharsTrie::Iterator(trie) constructor")) {
692 static const StringAndValue expected[]={
695 checkIterator(iter, expected, UPRV_LENGTHOF(expected));
696 // Reset, and we should get the same result.
697 logln("after iter.reset()");
698 checkIterator(iter.reset(), expected, UPRV_LENGTHOF(expected));
701 void UCharsTrieTest::TestTruncatingIteratorFromLinearMatchLong() {
702 static const StringAndValue data[]={
707 LocalPointer<UCharsTrie> trie(buildTrie(data, UPRV_LENGTHOF(data), USTRINGTRIE_BUILD_FAST));
709 return; // buildTrie() reported an error
711 // Go into a linear-match node.
715 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromLinearMatchLong()");
716 // Truncate after the linear-match node.
717 UCharsTrie::Iterator iter(*trie, 3, errorCode);
718 if(errorCode.logIfFailureAndReset("UCharsTrie::Iterator(trie) constructor")) {
721 static const StringAndValue expected[]={
726 checkIterator(iter, expected, UPRV_LENGTHOF(expected));
727 // Reset, and we should get the same result.
728 logln("after iter.reset()");
729 checkIterator(iter.reset(), expected, UPRV_LENGTHOF(expected));
732 void UCharsTrieTest::TestIteratorFromUChars() {
733 static const StringAndValue data[]={
739 IcuTestErrorCode errorCode(*this, "TestIteratorFromUChars()");
740 for(int32_t i=0; i<UPRV_LENGTHOF(data); ++i) {
741 builder_->add(data[i].s, data[i].value, errorCode);
743 UnicodeString trieUChars;
744 builder_->buildUnicodeString(USTRINGTRIE_BUILD_FAST, trieUChars, errorCode);
745 UCharsTrie::Iterator iter(trieUChars.getBuffer(), 0, errorCode);
746 checkIterator(iter, data, UPRV_LENGTHOF(data));
749 void UCharsTrieTest::checkData(const StringAndValue data[], int32_t dataLength) {
750 logln("checkData(dataLength=%d, fast)", (int)dataLength);
751 checkData(data, dataLength, USTRINGTRIE_BUILD_FAST);
752 logln("checkData(dataLength=%d, small)", (int)dataLength);
753 checkData(data, dataLength, USTRINGTRIE_BUILD_SMALL);
756 void UCharsTrieTest::checkData(const StringAndValue data[], int32_t dataLength, UStringTrieBuildOption buildOption) {
757 LocalPointer<UCharsTrie> trie(buildTrie(data, dataLength, buildOption));
759 return; // buildTrie() reported an error
761 checkFirst(*trie, data, dataLength);
762 checkNext(*trie, data, dataLength);
763 checkNextWithState(*trie, data, dataLength);
764 checkNextString(*trie, data, dataLength);
765 checkIterator(*trie, data, dataLength);
768 UCharsTrie *UCharsTrieTest::buildTrie(const StringAndValue data[], int32_t dataLength,
769 UStringTrieBuildOption buildOption) {
770 IcuTestErrorCode errorCode(*this, "buildTrie()");
771 // Add the items to the trie builder in an interesting (not trivial, not random) order.
774 // Odd number of items.
777 } else if((dataLength%3)!=0) {
778 // Not a multiple of 3.
786 for(int32_t i=0; i<dataLength; ++i) {
787 builder_->add(UnicodeString(data[index].s, -1, US_INV).unescape(),
788 data[index].value, errorCode);
789 index=(index+step)%dataLength;
791 UnicodeString trieUChars;
792 builder_->buildUnicodeString(buildOption, trieUChars, errorCode);
793 LocalPointer<UCharsTrie> trie(builder_->build(buildOption, errorCode));
794 if(!errorCode.logIfFailureAndReset("add()/build()")) {
795 builder_->add("zzz", 999, errorCode);
796 if(errorCode.reset()!=U_NO_WRITE_PERMISSION) {
797 errln("builder.build().add(zzz) did not set U_NO_WRITE_PERMISSION");
800 logln("serialized trie size: %ld UChars\n", (long)trieUChars.length());
801 UnicodeString trieUChars2;
802 builder_->buildUnicodeString(buildOption, trieUChars2, errorCode);
803 if(trieUChars.getBuffer()==trieUChars2.getBuffer()) {
804 errln("builder.buildUnicodeString() before & after build() returned same array");
806 if(errorCode.isFailure()) {
809 // Tries from either build() method should be identical but
810 // UCharsTrie does not implement equals().
811 // We just return either one.
812 if((dataLength&1)!=0) {
813 return trie.orphan();
815 return new UCharsTrie(trieUChars2.getBuffer());
819 void UCharsTrieTest::checkFirst(UCharsTrie &trie,
820 const StringAndValue data[], int32_t dataLength) {
821 for(int32_t i=0; i<dataLength; ++i) {
823 continue; // skip empty string
825 UnicodeString expectedString=UnicodeString(data[i].s, -1, US_INV).unescape();
826 UChar32 c=expectedString[0];
827 UChar32 nextCp=expectedString.length()>1 ? expectedString[1] : 0;
828 UStringTrieResult firstResult=trie.first(c);
829 int32_t firstValue=USTRINGTRIE_HAS_VALUE(firstResult) ? trie.getValue() : -1;
830 UStringTrieResult nextResult=trie.next(nextCp);
831 if(firstResult!=trie.reset().next(c) ||
832 firstResult!=trie.current() ||
833 firstValue!=(USTRINGTRIE_HAS_VALUE(firstResult) ? trie.getValue() : -1) ||
834 nextResult!=trie.next(nextCp)
836 errln("trie.first(U+%04X)!=trie.reset().next(same) for %s",
839 c=expectedString.char32At(0);
840 int32_t cLength=U16_LENGTH(c);
841 nextCp=expectedString.length()>cLength ? expectedString.char32At(cLength) : 0;
842 firstResult=trie.firstForCodePoint(c);
843 firstValue=USTRINGTRIE_HAS_VALUE(firstResult) ? trie.getValue() : -1;
844 nextResult=trie.nextForCodePoint(nextCp);
845 if(firstResult!=trie.reset().nextForCodePoint(c) ||
846 firstResult!=trie.current() ||
847 firstValue!=(USTRINGTRIE_HAS_VALUE(firstResult) ? trie.getValue() : -1) ||
848 nextResult!=trie.nextForCodePoint(nextCp)
850 errln("trie.firstForCodePoint(U+%04X)!=trie.reset().nextForCodePoint(same) for %s",
857 void UCharsTrieTest::checkNext(UCharsTrie &trie,
858 const StringAndValue data[], int32_t dataLength) {
859 UCharsTrie::State state;
860 for(int32_t i=0; i<dataLength; ++i) {
861 UnicodeString expectedString=UnicodeString(data[i].s, -1, US_INV).unescape();
862 int32_t stringLength= (i&1) ? -1 : expectedString.length();
863 UStringTrieResult result;
864 if( !USTRINGTRIE_HAS_VALUE(
865 result=trie.next(expectedString.getTerminatedBuffer(), stringLength)) ||
866 result!=trie.current()
868 errln("trie does not seem to contain %s", data[i].s);
869 } else if(trie.getValue()!=data[i].value) {
870 errln("trie value for %s is %ld=0x%lx instead of expected %ld=0x%lx",
872 (long)trie.getValue(), (long)trie.getValue(),
873 (long)data[i].value, (long)data[i].value);
874 } else if(result!=trie.current() || trie.getValue()!=data[i].value) {
875 errln("trie value for %s changes when repeating current()/getValue()", data[i].s);
878 stringLength=expectedString.length();
879 result=trie.current();
880 for(int32_t j=0; j<stringLength; ++j) {
881 if(!USTRINGTRIE_HAS_NEXT(result)) {
882 errln("trie.current()!=hasNext before end of %s (at index %d)", data[i].s, j);
885 if(result==USTRINGTRIE_INTERMEDIATE_VALUE) {
887 if(trie.current()!=USTRINGTRIE_INTERMEDIATE_VALUE) {
888 errln("trie.getValue().current()!=USTRINGTRIE_INTERMEDIATE_VALUE before end of %s (at index %d)", data[i].s, j);
892 result=trie.next(expectedString[j]);
893 if(!USTRINGTRIE_MATCHES(result)) {
894 errln("trie.next()=USTRINGTRIE_NO_MATCH before end of %s (at index %d)", data[i].s, j);
897 if(result!=trie.current()) {
898 errln("trie.next()!=following current() before end of %s (at index %d)", data[i].s, j);
902 if(!USTRINGTRIE_HAS_VALUE(result)) {
903 errln("trie.next()!=hasValue at the end of %s", data[i].s);
907 if(result!=trie.current()) {
908 errln("trie.current() != current()+getValue()+current() after end of %s",
911 // Compare the final current() with whether next() can actually continue.
912 trie.saveState(state);
913 UBool nextContinues=FALSE;
914 for(int32_t c=0x20; c<0xe000; ++c) {
916 c=0xd800; // Check for ASCII and surrogates but not all of the BMP.
918 if(trie.resetToState(state).next(c)) {
923 if((result==USTRINGTRIE_INTERMEDIATE_VALUE)!=nextContinues) {
924 errln("(trie.current()==USTRINGTRIE_INTERMEDIATE_VALUE) contradicts "
925 "(trie.next(some UChar)!=USTRINGTRIE_NO_MATCH) after end of %s", data[i].s);
931 void UCharsTrieTest::checkNextWithState(UCharsTrie &trie,
932 const StringAndValue data[], int32_t dataLength) {
933 UCharsTrie::State noState, state;
934 for(int32_t i=0; i<dataLength; ++i) {
936 // This should have no effect.
937 trie.resetToState(noState);
939 UnicodeString expectedString=UnicodeString(data[i].s, -1, US_INV).unescape();
940 int32_t stringLength=expectedString.length();
941 int32_t partialLength=stringLength/3;
942 for(int32_t j=0; j<partialLength; ++j) {
943 if(!USTRINGTRIE_MATCHES(trie.next(expectedString[j]))) {
944 errln("trie.next()=USTRINGTRIE_NO_MATCH for a prefix of %s", data[i].s);
948 trie.saveState(state);
949 UStringTrieResult resultAtState=trie.current();
950 UStringTrieResult result;
951 int32_t valueAtState=-99;
952 if(USTRINGTRIE_HAS_VALUE(resultAtState)) {
953 valueAtState=trie.getValue();
955 result=trie.next(0); // mismatch
956 if(result!=USTRINGTRIE_NO_MATCH || result!=trie.current()) {
957 errln("trie.next(0) matched after part of %s", data[i].s);
959 if( resultAtState!=trie.resetToState(state).current() ||
960 (USTRINGTRIE_HAS_VALUE(resultAtState) && valueAtState!=trie.getValue())
962 errln("trie.next(part of %s) changes current()/getValue() after "
963 "saveState/next(0)/resetToState",
965 } else if(!USTRINGTRIE_HAS_VALUE(
966 result=trie.next(expectedString.getTerminatedBuffer()+partialLength,
967 stringLength-partialLength)) ||
968 result!=trie.current()) {
969 errln("trie.next(rest of %s) does not seem to contain %s after "
970 "saveState/next(0)/resetToState",
971 data[i].s, data[i].s);
972 } else if(!USTRINGTRIE_HAS_VALUE(
973 result=trie.resetToState(state).
974 next(expectedString.getTerminatedBuffer()+partialLength,
975 stringLength-partialLength)) ||
976 result!=trie.current()) {
977 errln("trie does not seem to contain %s after saveState/next(rest)/resetToState",
979 } else if(trie.getValue()!=data[i].value) {
980 errln("trie value for %s is %ld=0x%lx instead of expected %ld=0x%lx",
982 (long)trie.getValue(), (long)trie.getValue(),
983 (long)data[i].value, (long)data[i].value);
989 // next(string) is also tested in other functions,
990 // but here we try to go partway through the string, and then beyond it.
991 void UCharsTrieTest::checkNextString(UCharsTrie &trie,
992 const StringAndValue data[], int32_t dataLength) {
993 for(int32_t i=0; i<dataLength; ++i) {
994 UnicodeString expectedString=UnicodeString(data[i].s, -1, US_INV).unescape();
995 int32_t stringLength=expectedString.length();
996 if(!trie.next(expectedString.getTerminatedBuffer(), stringLength/2)) {
997 errln("trie.next(up to middle of string)=USTRINGTRIE_NO_MATCH for %s", data[i].s);
1000 // Test that we stop properly at the end of the string.
1001 if(trie.next(expectedString.getTerminatedBuffer()+stringLength/2,
1002 stringLength+1-stringLength/2)) {
1003 errln("trie.next(string+NUL)!=USTRINGTRIE_NO_MATCH for %s", data[i].s);
1009 void UCharsTrieTest::checkIterator(UCharsTrie &trie,
1010 const StringAndValue data[], int32_t dataLength) {
1011 IcuTestErrorCode errorCode(*this, "checkIterator()");
1012 UCharsTrie::Iterator iter(trie, 0, errorCode);
1013 if(errorCode.logIfFailureAndReset("UCharsTrie::Iterator(trieUChars) constructor")) {
1016 checkIterator(iter, data, dataLength);
1019 void UCharsTrieTest::checkIterator(UCharsTrie::Iterator &iter,
1020 const StringAndValue data[], int32_t dataLength) {
1021 IcuTestErrorCode errorCode(*this, "checkIterator()");
1022 for(int32_t i=0; i<dataLength; ++i) {
1023 if(!iter.hasNext()) {
1024 errln("trie iterator hasNext()=FALSE for item %d: %s", (int)i, data[i].s);
1027 UBool hasNext=iter.next(errorCode);
1028 if(errorCode.logIfFailureAndReset("trie iterator next() for item %d: %s", (int)i, data[i].s)) {
1032 errln("trie iterator next()=FALSE for item %d: %s", (int)i, data[i].s);
1035 UnicodeString expectedString=UnicodeString(data[i].s, -1, US_INV).unescape();
1036 if(iter.getString()!=expectedString) {
1038 UnicodeString invString(prettify(iter.getString()));
1039 invString.extract(0, invString.length(), buffer, UPRV_LENGTHOF(buffer), US_INV);
1040 errln("trie iterator next().getString()=%s but expected %s for item %d",
1041 buffer, data[i].s, (int)i);
1043 if(iter.getValue()!=data[i].value) {
1044 errln("trie iterator next().getValue()=%ld=0x%lx but expected %ld=0x%lx for item %d: %s",
1045 (long)iter.getValue(), (long)iter.getValue(),
1046 (long)data[i].value, (long)data[i].value,
1050 if(iter.hasNext()) {
1051 errln("trie iterator hasNext()=TRUE after all items");
1053 UBool hasNext=iter.next(errorCode);
1054 errorCode.logIfFailureAndReset("trie iterator next() after all items");
1056 errln("trie iterator next()=TRUE after all items");