+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
-* Copyright (C) 2010-2011, International Business Machines
+* Copyright (C) 2010-2014, International Business Machines
* Corporation and others. All Rights Reserved.
*******************************************************************************
* file name: bytetrietest.cpp
-* encoding: US-ASCII
+* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
#include "unicode/localpointer.h"
#include "unicode/stringpiece.h"
#include "intltest.h"
-
-#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
+#include "cmemory.h"
struct StringAndValue {
const char *s;
void TestTruncatingIteratorFromLinearMatchShort();
void TestTruncatingIteratorFromLinearMatchLong();
void TestIteratorFromBytes();
+ void TestFailedIterator();
void checkData(const StringAndValue data[], int32_t dataLength);
void checkData(const StringAndValue data[], int32_t dataLength, UStringTrieBuildOption buildOption);
TESTCASE_AUTO(TestTruncatingIteratorFromLinearMatchShort);
TESTCASE_AUTO(TestTruncatingIteratorFromLinearMatchLong);
TESTCASE_AUTO(TestIteratorFromBytes);
+ TESTCASE_AUTO(TestFailedIterator);
TESTCASE_AUTO_END;
}
static const StringAndValue data[]={
{ "", 0 }
};
- checkData(data, LENGTHOF(data));
+ checkData(data, UPRV_LENGTHOF(data));
}
void BytesTrieTest::Test_a() {
static const StringAndValue data[]={
{ "a", 1 }
};
- checkData(data, LENGTHOF(data));
+ checkData(data, UPRV_LENGTHOF(data));
}
void BytesTrieTest::Test_a_ab() {
{ "a", 1 },
{ "ab", 100 }
};
- checkData(data, LENGTHOF(data));
+ checkData(data, UPRV_LENGTHOF(data));
}
void BytesTrieTest::TestShortestBranch() {
{ "a", 1000 },
{ "b", 2000 }
};
- checkData(data, LENGTHOF(data));
+ checkData(data, UPRV_LENGTHOF(data));
}
void BytesTrieTest::TestBranches() {
{ "t", 0x400000 },
{ "uu", 0x800000 },
{ "vv", 0x7fffffff },
- { "zz", 0x80000000 }
+ { "zz", (int32_t)0x80000000 }
};
- for(int32_t length=2; length<=LENGTHOF(data); ++length) {
- infoln("TestBranches length=%d", (int)length);
+ for(int32_t length=2; length<=UPRV_LENGTHOF(data); ++length) {
+ logln("TestBranches length=%d", (int)length);
checkData(data, length);
}
}
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -3 }
};
- checkData(data, LENGTHOF(data));
+ checkData(data, UPRV_LENGTHOF(data));
}
void BytesTrieTest::TestLongBranch() {
{ "r", 0x333333 },
{ "s2345", 0x4444444 },
{ "t234567890", 0x77777777 },
- { "z", 0x80000001 }
+ { "z", (int32_t)0x80000001 }
};
- checkData(data, LENGTHOF(data));
+ checkData(data, UPRV_LENGTHOF(data));
}
void BytesTrieTest::TestValuesForState() {
{ "abcde", -5 },
{ "abcdef", -6 }
};
- checkData(data, LENGTHOF(data));
+ checkData(data, UPRV_LENGTHOF(data));
}
void BytesTrieTest::TestCompact() {
{ "xjuly", 7 },
{ "xjune", 6 }
};
- checkData(data, LENGTHOF(data));
+ checkData(data, UPRV_LENGTHOF(data));
}
BytesTrie *BytesTrieTest::buildMonthsTrie(UStringTrieBuildOption buildOption) {
{ "jun.", 6 },
{ "june", 6 }
};
- return buildTrie(data, LENGTHOF(data), buildOption);
+ return buildTrie(data, UPRV_LENGTHOF(data), buildOption);
}
void BytesTrieTest::TestHasUniqueValue() {
return; // buildTrie() reported an error
}
char buffer[40];
- CheckedArrayByteSink sink(buffer, LENGTHOF(buffer));
+ CheckedArrayByteSink sink(buffer, UPRV_LENGTHOF(buffer));
int32_t count=trie->getNextBytes(sink);
if(count!=2 || sink.NumberOfBytesAppended()!=2 || buffer[0]!='a' || buffer[1]!='j') {
errln("months getNextBytes()!=[aj] at root");
trie->next('n');
IcuTestErrorCode errorCode(*this, "TestIteratorFromBranch()");
BytesTrie::Iterator iter(*trie, 0, errorCode);
- if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
+ if(errorCode.errIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
return;
}
// Expected data: Same as in buildMonthsTrie(), except only the suffixes
{ "uar", 1 },
{ "uary", 1 }
};
- checkIterator(iter, data, LENGTHOF(data));
+ checkIterator(iter, data, UPRV_LENGTHOF(data));
// Reset, and we should get the same result.
logln("after iter.reset()");
- checkIterator(iter.reset(), data, LENGTHOF(data));
+ checkIterator(iter.reset(), data, UPRV_LENGTHOF(data));
}
void BytesTrieTest::TestIteratorFromLinearMatch() {
trie->next('a');
IcuTestErrorCode errorCode(*this, "TestIteratorFromLinearMatch()");
BytesTrie::Iterator iter(*trie, 0, errorCode);
- if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
+ if(errorCode.errIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
return;
}
// Expected data: Same as in buildMonthsTrie(), except only the suffixes
{ "r", 1 },
{ "ry", 1 }
};
- checkIterator(iter, data, LENGTHOF(data));
+ checkIterator(iter, data, UPRV_LENGTHOF(data));
// Reset, and we should get the same result.
logln("after iter.reset()");
- checkIterator(iter.reset(), data, LENGTHOF(data));
+ checkIterator(iter.reset(), data, UPRV_LENGTHOF(data));
}
void BytesTrieTest::TestTruncatingIteratorFromRoot() {
}
IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromRoot()");
BytesTrie::Iterator iter(*trie, 4, errorCode);
- if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
+ if(errorCode.errIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
return;
}
// Expected data: Same as in buildMonthsTrie(), except only the first 4 characters
{ "jun.", 6 },
{ "june", 6 }
};
- checkIterator(iter, data, LENGTHOF(data));
+ checkIterator(iter, data, UPRV_LENGTHOF(data));
// Reset, and we should get the same result.
logln("after iter.reset()");
- checkIterator(iter.reset(), data, LENGTHOF(data));
+ checkIterator(iter.reset(), data, UPRV_LENGTHOF(data));
}
void BytesTrieTest::TestTruncatingIteratorFromLinearMatchShort() {
{ "abcdepq", 200 },
{ "abcdeyz", 3000 }
};
- LocalPointer<BytesTrie> trie(buildTrie(data, LENGTHOF(data), USTRINGTRIE_BUILD_FAST));
+ LocalPointer<BytesTrie> trie(buildTrie(data, UPRV_LENGTHOF(data), USTRINGTRIE_BUILD_FAST));
if(trie.isNull()) {
return; // buildTrie() reported an error
}
IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromLinearMatchShort()");
// Truncate within the linear-match node.
BytesTrie::Iterator iter(*trie, 2, errorCode);
- if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
+ if(errorCode.errIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
return;
}
static const StringAndValue expected[]={
{ "cd", -1 }
};
- checkIterator(iter, expected, LENGTHOF(expected));
+ checkIterator(iter, expected, UPRV_LENGTHOF(expected));
// Reset, and we should get the same result.
logln("after iter.reset()");
- checkIterator(iter.reset(), expected, LENGTHOF(expected));
+ checkIterator(iter.reset(), expected, UPRV_LENGTHOF(expected));
}
void BytesTrieTest::TestTruncatingIteratorFromLinearMatchLong() {
{ "abcdepq", 200 },
{ "abcdeyz", 3000 }
};
- LocalPointer<BytesTrie> trie(buildTrie(data, LENGTHOF(data), USTRINGTRIE_BUILD_FAST));
+ LocalPointer<BytesTrie> trie(buildTrie(data, UPRV_LENGTHOF(data), USTRINGTRIE_BUILD_FAST));
if(trie.isNull()) {
return; // buildTrie() reported an error
}
IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromLinearMatchLong()");
// Truncate after the linear-match node.
BytesTrie::Iterator iter(*trie, 3, errorCode);
- if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
+ if(errorCode.errIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
return;
}
static const StringAndValue expected[]={
{ "dep", -1 },
{ "dey", -1 }
};
- checkIterator(iter, expected, LENGTHOF(expected));
+ checkIterator(iter, expected, UPRV_LENGTHOF(expected));
// Reset, and we should get the same result.
logln("after iter.reset()");
- checkIterator(iter.reset(), expected, LENGTHOF(expected));
+ checkIterator(iter.reset(), expected, UPRV_LENGTHOF(expected));
}
void BytesTrieTest::TestIteratorFromBytes() {
};
builder_->clear();
IcuTestErrorCode errorCode(*this, "TestIteratorFromBytes()");
- for(int32_t i=0; i<LENGTHOF(data); ++i) {
+ for(int32_t i=0; i<UPRV_LENGTHOF(data); ++i) {
builder_->add(data[i].s, data[i].value, errorCode);
}
StringPiece trieBytes=builder_->buildStringPiece(USTRINGTRIE_BUILD_FAST, errorCode);
BytesTrie::Iterator iter(trieBytes.data(), 0, errorCode);
- checkIterator(iter, data, LENGTHOF(data));
+ checkIterator(iter, data, UPRV_LENGTHOF(data));
+}
+
+void BytesTrieTest::TestFailedIterator() {
+ UErrorCode failure = U_ILLEGAL_ARGUMENT_ERROR;
+ BytesTrie::Iterator iter(NULL, 0, failure);
+ StringPiece sp = iter.getString();
+ if (!sp.empty()) {
+ errln("failed iterator returned garbage data");
+ }
}
void BytesTrieTest::checkData(const StringAndValue data[], int32_t dataLength) {
}
StringPiece sp=builder_->buildStringPiece(buildOption, errorCode);
LocalPointer<BytesTrie> trie(builder_->build(buildOption, errorCode));
- if(!errorCode.logIfFailureAndReset("add()/build()")) {
+ if(!errorCode.errIfFailureAndReset("add()/build()")) {
builder_->add("zzz", 999, errorCode);
if(errorCode.reset()!=U_NO_WRITE_PERMISSION) {
errln("builder.build().add(zzz) did not set U_NO_WRITE_PERMISSION");
const StringAndValue data[], int32_t dataLength) {
IcuTestErrorCode errorCode(*this, "checkIterator()");
BytesTrie::Iterator iter(trie, 0, errorCode);
- if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
+ if(errorCode.errIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) {
return;
}
checkIterator(iter, data, dataLength);
break;
}
UBool hasNext=iter.next(errorCode);
- if(errorCode.logIfFailureAndReset("trie iterator next() for item %d: %s", (int)i, data[i].s)) {
+ if(errorCode.errIfFailureAndReset("trie iterator next() for item %d: %s", (int)i, data[i].s)) {
break;
}
if(!hasNext) {
errln("trie iterator hasNext()=TRUE after all items");
}
UBool hasNext=iter.next(errorCode);
- errorCode.logIfFailureAndReset("trie iterator next() after all items");
+ errorCode.errIfFailureAndReset("trie iterator next() after all items");
if(hasNext) {
errln("trie iterator next()=TRUE after all items");
}