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: bytetrietest.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/bytestrie.h"
21 #include "unicode/bytestriebuilder.h"
22 #include "unicode/localpointer.h"
23 #include "unicode/stringpiece.h"
27 struct StringAndValue {
32 class BytesTrieTest : public IntlTest {
35 virtual ~BytesTrieTest();
37 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=NULL);
42 void TestShortestBranch();
44 void TestLongSequence();
45 void TestLongBranch();
46 void TestValuesForState();
49 BytesTrie *buildMonthsTrie(UStringTrieBuildOption buildOption);
50 void TestHasUniqueValue();
51 void TestGetNextBytes();
52 void TestIteratorFromBranch();
53 void TestIteratorFromLinearMatch();
54 void TestTruncatingIteratorFromRoot();
55 void TestTruncatingIteratorFromLinearMatchShort();
56 void TestTruncatingIteratorFromLinearMatchLong();
57 void TestIteratorFromBytes();
58 void TestFailedIterator();
60 void checkData(const StringAndValue data[], int32_t dataLength);
61 void checkData(const StringAndValue data[], int32_t dataLength, UStringTrieBuildOption buildOption);
62 BytesTrie *buildTrie(const StringAndValue data[], int32_t dataLength,
63 UStringTrieBuildOption buildOption);
64 void checkFirst(BytesTrie &trie, const StringAndValue data[], int32_t dataLength);
65 void checkNext(BytesTrie &trie, const StringAndValue data[], int32_t dataLength);
66 void checkNextWithState(BytesTrie &trie, const StringAndValue data[], int32_t dataLength);
67 void checkNextString(BytesTrie &trie, const StringAndValue data[], int32_t dataLength);
68 void checkIterator(const BytesTrie &trie, const StringAndValue data[], int32_t dataLength);
69 void checkIterator(BytesTrie::Iterator &iter, const StringAndValue data[], int32_t dataLength);
72 BytesTrieBuilder *builder_;
75 extern IntlTest *createBytesTrieTest() {
76 return new BytesTrieTest();
79 BytesTrieTest::BytesTrieTest() : builder_(NULL) {
80 IcuTestErrorCode errorCode(*this, "BytesTrieTest()");
81 builder_=new BytesTrieBuilder(errorCode);
84 BytesTrieTest::~BytesTrieTest() {
88 void BytesTrieTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
90 logln("TestSuite BytesTrieTest: ");
93 TESTCASE_AUTO(TestBuilder);
94 TESTCASE_AUTO(TestEmpty);
95 TESTCASE_AUTO(Test_a);
96 TESTCASE_AUTO(Test_a_ab);
97 TESTCASE_AUTO(TestShortestBranch);
98 TESTCASE_AUTO(TestBranches);
99 TESTCASE_AUTO(TestLongSequence);
100 TESTCASE_AUTO(TestLongBranch);
101 TESTCASE_AUTO(TestValuesForState);
102 TESTCASE_AUTO(TestCompact);
103 TESTCASE_AUTO(TestHasUniqueValue);
104 TESTCASE_AUTO(TestGetNextBytes);
105 TESTCASE_AUTO(TestIteratorFromBranch);
106 TESTCASE_AUTO(TestIteratorFromLinearMatch);
107 TESTCASE_AUTO(TestTruncatingIteratorFromRoot);
108 TESTCASE_AUTO(TestTruncatingIteratorFromLinearMatchShort);
109 TESTCASE_AUTO(TestTruncatingIteratorFromLinearMatchLong);
110 TESTCASE_AUTO(TestIteratorFromBytes);
111 TESTCASE_AUTO(TestFailedIterator);
115 void BytesTrieTest::TestBuilder() {
116 IcuTestErrorCode errorCode(*this, "TestBuilder()");
118 delete builder_->build(USTRINGTRIE_BUILD_FAST, errorCode);
119 if(errorCode.reset()!=U_INDEX_OUTOFBOUNDS_ERROR) {
120 errln("BytesTrieBuilder().build() did not set U_INDEX_OUTOFBOUNDS_ERROR");
123 // TODO: remove .build(...) once add() checks for duplicates.
124 builder_->add("=", 0, errorCode).add("=", 1, errorCode).build(USTRINGTRIE_BUILD_FAST, errorCode);
125 if(errorCode.reset()!=U_ILLEGAL_ARGUMENT_ERROR) {
126 errln("BytesTrieBuilder.add() did not detect duplicates");
131 void BytesTrieTest::TestEmpty() {
132 static const StringAndValue data[]={
135 checkData(data, UPRV_LENGTHOF(data));
138 void BytesTrieTest::Test_a() {
139 static const StringAndValue data[]={
142 checkData(data, UPRV_LENGTHOF(data));
145 void BytesTrieTest::Test_a_ab() {
146 static const StringAndValue data[]={
150 checkData(data, UPRV_LENGTHOF(data));
153 void BytesTrieTest::TestShortestBranch() {
154 static const StringAndValue data[]={
158 checkData(data, UPRV_LENGTHOF(data));
161 void BytesTrieTest::TestBranches() {
162 static const StringAndValue data[]={
170 { "ppppp", 0x40000 },
175 { "vv", 0x7fffffff },
176 { "zz", (int32_t)0x80000000 }
178 for(int32_t length=2; length<=UPRV_LENGTHOF(data); ++length) {
179 logln("TestBranches length=%d", (int)length);
180 checkData(data, length);
184 void BytesTrieTest::TestLongSequence() {
185 static const StringAndValue data[]={
187 // sequence of linear-match nodes
188 { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -2 },
189 // more than 256 bytes
190 { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
191 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
192 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
193 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
194 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
195 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -3 }
197 checkData(data, UPRV_LENGTHOF(data));
200 void BytesTrieTest::TestLongBranch() {
201 // Split-branch and interesting compact-integer values.
202 static const StringAndValue data[]={
215 { "l234567890", 0x1b00 },
216 { "l234567890123", 0x1b01 },
217 { "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn", 0x10ffff },
218 { "oooooooooooooooooooooooooooooooooooooooooooooooooooooo", 0x110000 },
219 { "pppppppppppppppppppppppppppppppppppppppppppppppppppppp", 0x120000 },
221 { "s2345", 0x4444444 },
222 { "t234567890", 0x77777777 },
223 { "z", (int32_t)0x80000001 }
225 checkData(data, UPRV_LENGTHOF(data));
228 void BytesTrieTest::TestValuesForState() {
229 // Check that saveState() and resetToState() interact properly
230 // with next() and current().
231 static const StringAndValue data[]={
239 checkData(data, UPRV_LENGTHOF(data));
242 void BytesTrieTest::TestCompact() {
243 // Duplicate trailing strings and values provide opportunities for compacting.
244 static const StringAndValue data[]={
261 // The l+n branch (with its sub-nodes) is a duplicate but will be written
262 // both times because each time it follows a different linear-match node.
266 checkData(data, UPRV_LENGTHOF(data));
269 BytesTrie *BytesTrieTest::buildMonthsTrie(UStringTrieBuildOption buildOption) {
270 // All types of nodes leading to the same value,
271 // for code coverage of recursive functions.
272 // In particular, we need a lot of branches on some single level
273 // to exercise a split-branch node.
274 static const StringAndValue data[]={
294 { "jannnnnnnnnnnnnnnnnnnnnnnnnnnnn", 1 },
306 return buildTrie(data, UPRV_LENGTHOF(data), buildOption);
309 void BytesTrieTest::TestHasUniqueValue() {
310 LocalPointer<BytesTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_FAST));
312 return; // buildTrie() reported an error
315 if(trie->hasUniqueValue(uniqueValue)) {
316 errln("unique value at root");
321 // hasUniqueValue() directly after next()
322 if(!trie->hasUniqueValue(uniqueValue) || uniqueValue!=1) {
323 errln("not unique value 1 after \"jan\"");
327 if(trie->hasUniqueValue(uniqueValue)) {
328 errln("unique value after \"ju\"");
330 if(trie->next('n')!=USTRINGTRIE_INTERMEDIATE_VALUE || 6!=trie->getValue()) {
331 errln("not normal value 6 after \"jun\"");
333 // hasUniqueValue() after getValue()
334 if(!trie->hasUniqueValue(uniqueValue) || uniqueValue!=6) {
335 errln("not unique value 6 after \"jun\"");
337 // hasUniqueValue() from within a linear-match node
340 if(!trie->hasUniqueValue(uniqueValue) || uniqueValue!=8) {
341 errln("not unique value 8 after \"au\"");
345 void BytesTrieTest::TestGetNextBytes() {
346 LocalPointer<BytesTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_SMALL));
348 return; // buildTrie() reported an error
351 CheckedArrayByteSink sink(buffer, UPRV_LENGTHOF(buffer));
352 int32_t count=trie->getNextBytes(sink);
353 if(count!=2 || sink.NumberOfBytesAppended()!=2 || buffer[0]!='a' || buffer[1]!='j') {
354 errln("months getNextBytes()!=[aj] at root");
359 // getNextBytes() directly after next()
360 count=trie->getNextBytes(sink.Reset());
362 if(count!=20 || sink.NumberOfBytesAppended()!=20 || 0!=strcmp(buffer, ".abcdefghijklmnopqru")) {
363 errln("months getNextBytes()!=[.abcdefghijklmnopqru] after \"jan\"");
365 // getNextBytes() after getValue()
366 trie->getValue(); // next() had returned USTRINGTRIE_INTERMEDIATE_VALUE.
367 memset(buffer, 0, sizeof(buffer));
368 count=trie->getNextBytes(sink.Reset());
369 if(count!=20 || sink.NumberOfBytesAppended()!=20 || 0!=strcmp(buffer, ".abcdefghijklmnopqru")) {
370 errln("months getNextBytes()!=[.abcdefghijklmnopqru] after \"jan\"+getValue()");
372 // getNextBytes() from a linear-match node
374 memset(buffer, 0, sizeof(buffer));
375 count=trie->getNextBytes(sink.Reset());
376 if(count!=1 || sink.NumberOfBytesAppended()!=1 || buffer[0]!='a') {
377 errln("months getNextBytes()!=[a] after \"janu\"");
380 memset(buffer, 0, sizeof(buffer));
381 count=trie->getNextBytes(sink.Reset());
382 if(count!=1 || sink.NumberOfBytesAppended()!=1 || buffer[0]!='r') {
383 errln("months getNextBytes()!=[r] after \"janua\"");
387 // getNextBytes() after a final match
388 count=trie->getNextBytes(sink.Reset());
389 if(count!=0 || sink.NumberOfBytesAppended()!=0) {
390 errln("months getNextBytes()!=[] after \"january\"");
394 void BytesTrieTest::TestIteratorFromBranch() {
395 LocalPointer<BytesTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_FAST));
397 return; // buildTrie() reported an error
399 // Go to a branch node.
403 IcuTestErrorCode errorCode(*this, "TestIteratorFromBranch()");
404 BytesTrie::Iterator iter(*trie, 0, errorCode);
405 if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
408 // Expected data: Same as in buildMonthsTrie(), except only the suffixes
410 static const StringAndValue data[]={
429 { "nnnnnnnnnnnnnnnnnnnnnnnnnnnn", 1 },
437 checkIterator(iter, data, UPRV_LENGTHOF(data));
438 // Reset, and we should get the same result.
439 logln("after iter.reset()");
440 checkIterator(iter.reset(), data, UPRV_LENGTHOF(data));
443 void BytesTrieTest::TestIteratorFromLinearMatch() {
444 LocalPointer<BytesTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_SMALL));
446 return; // buildTrie() reported an error
448 // Go into a linear-match node.
454 IcuTestErrorCode errorCode(*this, "TestIteratorFromLinearMatch()");
455 BytesTrie::Iterator iter(*trie, 0, errorCode);
456 if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
459 // Expected data: Same as in buildMonthsTrie(), except only the suffixes
460 // following "janua".
461 static const StringAndValue data[]={
465 checkIterator(iter, data, UPRV_LENGTHOF(data));
466 // Reset, and we should get the same result.
467 logln("after iter.reset()");
468 checkIterator(iter.reset(), data, UPRV_LENGTHOF(data));
471 void BytesTrieTest::TestTruncatingIteratorFromRoot() {
472 LocalPointer<BytesTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_FAST));
474 return; // buildTrie() reported an error
476 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromRoot()");
477 BytesTrie::Iterator iter(*trie, 4, errorCode);
478 if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
481 // Expected data: Same as in buildMonthsTrie(), except only the first 4 characters
482 // of each string, and no string duplicates from the truncation.
483 static const StringAndValue data[]={
511 checkIterator(iter, data, UPRV_LENGTHOF(data));
512 // Reset, and we should get the same result.
513 logln("after iter.reset()");
514 checkIterator(iter.reset(), data, UPRV_LENGTHOF(data));
517 void BytesTrieTest::TestTruncatingIteratorFromLinearMatchShort() {
518 static const StringAndValue data[]={
523 LocalPointer<BytesTrie> trie(buildTrie(data, UPRV_LENGTHOF(data), USTRINGTRIE_BUILD_FAST));
525 return; // buildTrie() reported an error
527 // Go into a linear-match node.
530 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromLinearMatchShort()");
531 // Truncate within the linear-match node.
532 BytesTrie::Iterator iter(*trie, 2, errorCode);
533 if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
536 static const StringAndValue expected[]={
539 checkIterator(iter, expected, UPRV_LENGTHOF(expected));
540 // Reset, and we should get the same result.
541 logln("after iter.reset()");
542 checkIterator(iter.reset(), expected, UPRV_LENGTHOF(expected));
545 void BytesTrieTest::TestTruncatingIteratorFromLinearMatchLong() {
546 static const StringAndValue data[]={
551 LocalPointer<BytesTrie> trie(buildTrie(data, UPRV_LENGTHOF(data), USTRINGTRIE_BUILD_FAST));
553 return; // buildTrie() reported an error
555 // Go into a linear-match node.
559 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromLinearMatchLong()");
560 // Truncate after the linear-match node.
561 BytesTrie::Iterator iter(*trie, 3, errorCode);
562 if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
565 static const StringAndValue expected[]={
570 checkIterator(iter, expected, UPRV_LENGTHOF(expected));
571 // Reset, and we should get the same result.
572 logln("after iter.reset()");
573 checkIterator(iter.reset(), expected, UPRV_LENGTHOF(expected));
576 void BytesTrieTest::TestIteratorFromBytes() {
577 static const StringAndValue data[]={
583 IcuTestErrorCode errorCode(*this, "TestIteratorFromBytes()");
584 for(int32_t i=0; i<UPRV_LENGTHOF(data); ++i) {
585 builder_->add(data[i].s, data[i].value, errorCode);
587 StringPiece trieBytes=builder_->buildStringPiece(USTRINGTRIE_BUILD_FAST, errorCode);
588 BytesTrie::Iterator iter(trieBytes.data(), 0, errorCode);
589 checkIterator(iter, data, UPRV_LENGTHOF(data));
592 void BytesTrieTest::TestFailedIterator() {
593 UErrorCode failure = U_ILLEGAL_ARGUMENT_ERROR;
594 BytesTrie::Iterator iter(NULL, 0, failure);
595 StringPiece sp = iter.getString();
597 errln("failed iterator returned garbage data");
601 void BytesTrieTest::checkData(const StringAndValue data[], int32_t dataLength) {
602 logln("checkData(dataLength=%d, fast)", (int)dataLength);
603 checkData(data, dataLength, USTRINGTRIE_BUILD_FAST);
604 logln("checkData(dataLength=%d, small)", (int)dataLength);
605 checkData(data, dataLength, USTRINGTRIE_BUILD_SMALL);
608 void BytesTrieTest::checkData(const StringAndValue data[], int32_t dataLength, UStringTrieBuildOption buildOption) {
609 LocalPointer<BytesTrie> trie(buildTrie(data, dataLength, buildOption));
611 return; // buildTrie() reported an error
613 checkFirst(*trie, data, dataLength);
614 checkNext(*trie, data, dataLength);
615 checkNextWithState(*trie, data, dataLength);
616 checkNextString(*trie, data, dataLength);
617 checkIterator(*trie, data, dataLength);
620 BytesTrie *BytesTrieTest::buildTrie(const StringAndValue data[], int32_t dataLength,
621 UStringTrieBuildOption buildOption) {
622 IcuTestErrorCode errorCode(*this, "buildTrie()");
623 // Add the items to the trie builder in an interesting (not trivial, not random) order.
626 // Odd number of items.
629 } else if((dataLength%3)!=0) {
630 // Not a multiple of 3.
638 for(int32_t i=0; i<dataLength; ++i) {
639 builder_->add(data[index].s, data[index].value, errorCode);
640 index=(index+step)%dataLength;
642 StringPiece sp=builder_->buildStringPiece(buildOption, errorCode);
643 LocalPointer<BytesTrie> trie(builder_->build(buildOption, errorCode));
644 if(!errorCode.logIfFailureAndReset("add()/build()")) {
645 builder_->add("zzz", 999, errorCode);
646 if(errorCode.reset()!=U_NO_WRITE_PERMISSION) {
647 errln("builder.build().add(zzz) did not set U_NO_WRITE_PERMISSION");
650 logln("serialized trie size: %ld bytes\n", (long)sp.length());
651 StringPiece sp2=builder_->buildStringPiece(buildOption, errorCode);
652 if(sp.data()==sp2.data()) {
653 errln("builder.buildStringPiece() before & after build() returned same array");
655 if(errorCode.isFailure()) {
658 // Tries from either build() method should be identical but
659 // BytesTrie does not implement equals().
660 // We just return either one.
661 if((dataLength&1)!=0) {
662 return trie.orphan();
664 return new BytesTrie(sp2.data());
668 void BytesTrieTest::checkFirst(BytesTrie &trie,
669 const StringAndValue data[], int32_t dataLength) {
670 for(int32_t i=0; i<dataLength; ++i) {
673 continue; // skip empty string
675 UStringTrieResult firstResult=trie.first(c);
676 int32_t firstValue=USTRINGTRIE_HAS_VALUE(firstResult) ? trie.getValue() : -1;
677 UStringTrieResult nextResult=trie.next(data[i].s[1]);
678 if(firstResult!=trie.reset().next(c) ||
679 firstResult!=trie.current() ||
680 firstValue!=(USTRINGTRIE_HAS_VALUE(firstResult) ? trie.getValue() : -1) ||
681 nextResult!=trie.next(data[i].s[1])
683 errln("trie.first(%c)!=trie.reset().next(same) for %s",
690 void BytesTrieTest::checkNext(BytesTrie &trie,
691 const StringAndValue data[], int32_t dataLength) {
692 BytesTrie::State state;
693 for(int32_t i=0; i<dataLength; ++i) {
694 int32_t stringLength= (i&1) ? -1 : strlen(data[i].s);
695 UStringTrieResult result;
696 if( !USTRINGTRIE_HAS_VALUE(result=trie.next(data[i].s, stringLength)) ||
697 result!=trie.current()
699 errln("trie does not seem to contain %s", data[i].s);
700 } else if(trie.getValue()!=data[i].value) {
701 errln("trie value for %s is %ld=0x%lx instead of expected %ld=0x%lx",
703 (long)trie.getValue(), (long)trie.getValue(),
704 (long)data[i].value, (long)data[i].value);
705 } else if(result!=trie.current() || trie.getValue()!=data[i].value) {
706 errln("trie value for %s changes when repeating current()/getValue()", data[i].s);
709 stringLength=strlen(data[i].s);
710 result=trie.current();
711 for(int32_t j=0; j<stringLength; ++j) {
712 if(!USTRINGTRIE_HAS_NEXT(result)) {
713 errln("trie.current()!=hasNext before end of %s (at index %d)", data[i].s, j);
716 if(result==USTRINGTRIE_INTERMEDIATE_VALUE) {
718 if(trie.current()!=USTRINGTRIE_INTERMEDIATE_VALUE) {
719 errln("trie.getValue().current()!=USTRINGTRIE_INTERMEDIATE_VALUE before end of %s (at index %d)", data[i].s, j);
723 result=trie.next(data[i].s[j]);
724 if(!USTRINGTRIE_MATCHES(result)) {
725 errln("trie.next()=USTRINGTRIE_NO_MATCH before end of %s (at index %d)", data[i].s, j);
728 if(result!=trie.current()) {
729 errln("trie.next()!=following current() before end of %s (at index %d)", data[i].s, j);
733 if(!USTRINGTRIE_HAS_VALUE(result)) {
734 errln("trie.next()!=hasValue at the end of %s", data[i].s);
738 if(result!=trie.current()) {
739 errln("trie.current() != current()+getValue()+current() after end of %s",
742 // Compare the final current() with whether next() can actually continue.
743 trie.saveState(state);
744 UBool nextContinues=FALSE;
745 // Try all graphic characters; we only use those in test strings in this file.
746 #if U_CHARSET_FAMILY==U_ASCII_FAMILY
747 const int32_t minChar=0x20;
748 const int32_t maxChar=0x7e;
749 #elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
750 const int32_t minChar=0x40;
751 const int32_t maxChar=0xfe;
753 const int32_t minChar=0;
754 const int32_t maxChar=0xff;
756 for(int32_t c=minChar; c<=maxChar; ++c) {
757 if(trie.resetToState(state).next(c)) {
762 if((result==USTRINGTRIE_INTERMEDIATE_VALUE)!=nextContinues) {
763 errln("(trie.current()==USTRINGTRIE_INTERMEDIATE_VALUE) contradicts "
764 "(trie.next(some byte)!=USTRINGTRIE_NO_MATCH) after end of %s", data[i].s);
770 void BytesTrieTest::checkNextWithState(BytesTrie &trie,
771 const StringAndValue data[], int32_t dataLength) {
772 BytesTrie::State noState, state;
773 for(int32_t i=0; i<dataLength; ++i) {
775 // This should have no effect.
776 trie.resetToState(noState);
778 const char *expectedString=data[i].s;
779 int32_t stringLength=strlen(expectedString);
780 int32_t partialLength=stringLength/3;
781 for(int32_t j=0; j<partialLength; ++j) {
782 if(!USTRINGTRIE_MATCHES(trie.next(expectedString[j]))) {
783 errln("trie.next()=USTRINGTRIE_NO_MATCH for a prefix of %s", data[i].s);
787 trie.saveState(state);
788 UStringTrieResult resultAtState=trie.current();
789 UStringTrieResult result;
790 int32_t valueAtState=-99;
791 if(USTRINGTRIE_HAS_VALUE(resultAtState)) {
792 valueAtState=trie.getValue();
794 result=trie.next(0); // mismatch
795 if(result!=USTRINGTRIE_NO_MATCH || result!=trie.current()) {
796 errln("trie.next(0) matched after part of %s", data[i].s);
798 if( resultAtState!=trie.resetToState(state).current() ||
799 (USTRINGTRIE_HAS_VALUE(resultAtState) && valueAtState!=trie.getValue())
801 errln("trie.next(part of %s) changes current()/getValue() after "
802 "saveState/next(0)/resetToState",
804 } else if(!USTRINGTRIE_HAS_VALUE(
805 result=trie.next(expectedString+partialLength,
806 stringLength-partialLength)) ||
807 result!=trie.current()) {
808 errln("trie.next(rest of %s) does not seem to contain %s after "
809 "saveState/next(0)/resetToState",
810 data[i].s, data[i].s);
811 } else if(!USTRINGTRIE_HAS_VALUE(
812 result=trie.resetToState(state).
813 next(expectedString+partialLength,
814 stringLength-partialLength)) ||
815 result!=trie.current()) {
816 errln("trie does not seem to contain %s after saveState/next(rest)/resetToState",
818 } else if(trie.getValue()!=data[i].value) {
819 errln("trie value for %s is %ld=0x%lx instead of expected %ld=0x%lx",
821 (long)trie.getValue(), (long)trie.getValue(),
822 (long)data[i].value, (long)data[i].value);
828 // next(string) is also tested in other functions,
829 // but here we try to go partway through the string, and then beyond it.
830 void BytesTrieTest::checkNextString(BytesTrie &trie,
831 const StringAndValue data[], int32_t dataLength) {
832 for(int32_t i=0; i<dataLength; ++i) {
833 const char *expectedString=data[i].s;
834 int32_t stringLength=strlen(expectedString);
835 if(!trie.next(expectedString, stringLength/2)) {
836 errln("trie.next(up to middle of string)=USTRINGTRIE_NO_MATCH for %s", data[i].s);
839 // Test that we stop properly at the end of the string.
840 if(trie.next(expectedString+stringLength/2, stringLength+1-stringLength/2)) {
841 errln("trie.next(string+NUL)!=USTRINGTRIE_NO_MATCH for %s", data[i].s);
847 void BytesTrieTest::checkIterator(const BytesTrie &trie,
848 const StringAndValue data[], int32_t dataLength) {
849 IcuTestErrorCode errorCode(*this, "checkIterator()");
850 BytesTrie::Iterator iter(trie, 0, errorCode);
851 if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
854 checkIterator(iter, data, dataLength);
857 void BytesTrieTest::checkIterator(BytesTrie::Iterator &iter,
858 const StringAndValue data[], int32_t dataLength) {
859 IcuTestErrorCode errorCode(*this, "checkIterator()");
860 for(int32_t i=0; i<dataLength; ++i) {
861 if(!iter.hasNext()) {
862 errln("trie iterator hasNext()=FALSE for item %d: %s", (int)i, data[i].s);
865 UBool hasNext=iter.next(errorCode);
866 if(errorCode.logIfFailureAndReset("trie iterator next() for item %d: %s", (int)i, data[i].s)) {
870 errln("trie iterator next()=FALSE for item %d: %s", (int)i, data[i].s);
873 if(iter.getString()!=StringPiece(data[i].s)) {
874 errln("trie iterator next().getString()=%s but expected %s for item %d",
875 iter.getString().data(), data[i].s, (int)i);
877 if(iter.getValue()!=data[i].value) {
878 errln("trie iterator next().getValue()=%ld=0x%lx but expected %ld=0x%lx for item %d: %s",
879 (long)iter.getValue(), (long)iter.getValue(),
880 (long)data[i].value, (long)data[i].value,
885 errln("trie iterator hasNext()=TRUE after all items");
887 UBool hasNext=iter.next(errorCode);
888 errorCode.logIfFailureAndReset("trie iterator next() after all items");
890 errln("trie iterator next()=TRUE after all items");