| OLD | NEW |
| 1 /* | 1 /* |
| 2 ******************************************************************************* | 2 ******************************************************************************* |
| 3 * Copyright (C) 2010-2013, International Business Machines | 3 * Copyright (C) 2010-2014, International Business Machines |
| 4 * Corporation and others. All Rights Reserved. | 4 * Corporation and others. All Rights Reserved. |
| 5 ******************************************************************************* | 5 ******************************************************************************* |
| 6 * file name: bytetrietest.cpp | 6 * file name: bytetrietest.cpp |
| 7 * encoding: US-ASCII | 7 * encoding: US-ASCII |
| 8 * tab size: 8 (not used) | 8 * tab size: 8 (not used) |
| 9 * indentation:4 | 9 * indentation:4 |
| 10 * | 10 * |
| 11 * created on: 2010nov16 | 11 * created on: 2010nov16 |
| 12 * created by: Markus W. Scherer | 12 * created by: Markus W. Scherer |
| 13 */ | 13 */ |
| 14 | 14 |
| 15 #include <string.h> | 15 #include <string.h> |
| 16 | 16 |
| 17 #include "unicode/utypes.h" | 17 #include "unicode/utypes.h" |
| 18 #include "unicode/bytestrie.h" | 18 #include "unicode/bytestrie.h" |
| 19 #include "unicode/bytestriebuilder.h" | 19 #include "unicode/bytestriebuilder.h" |
| 20 #include "unicode/localpointer.h" | 20 #include "unicode/localpointer.h" |
| 21 #include "unicode/stringpiece.h" | 21 #include "unicode/stringpiece.h" |
| 22 #include "intltest.h" | 22 #include "intltest.h" |
| 23 | 23 #include "cmemory.h" |
| 24 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) | |
| 25 | 24 |
| 26 struct StringAndValue { | 25 struct StringAndValue { |
| 27 const char *s; | 26 const char *s; |
| 28 int32_t value; | 27 int32_t value; |
| 29 }; | 28 }; |
| 30 | 29 |
| 31 class BytesTrieTest : public IntlTest { | 30 class BytesTrieTest : public IntlTest { |
| 32 public: | 31 public: |
| 33 BytesTrieTest(); | 32 BytesTrieTest(); |
| 34 virtual ~BytesTrieTest(); | 33 virtual ~BytesTrieTest(); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 if(errorCode.reset()!=U_ILLEGAL_ARGUMENT_ERROR) { | 121 if(errorCode.reset()!=U_ILLEGAL_ARGUMENT_ERROR) { |
| 123 errln("BytesTrieBuilder.add() did not detect duplicates"); | 122 errln("BytesTrieBuilder.add() did not detect duplicates"); |
| 124 return; | 123 return; |
| 125 } | 124 } |
| 126 } | 125 } |
| 127 | 126 |
| 128 void BytesTrieTest::TestEmpty() { | 127 void BytesTrieTest::TestEmpty() { |
| 129 static const StringAndValue data[]={ | 128 static const StringAndValue data[]={ |
| 130 { "", 0 } | 129 { "", 0 } |
| 131 }; | 130 }; |
| 132 checkData(data, LENGTHOF(data)); | 131 checkData(data, UPRV_LENGTHOF(data)); |
| 133 } | 132 } |
| 134 | 133 |
| 135 void BytesTrieTest::Test_a() { | 134 void BytesTrieTest::Test_a() { |
| 136 static const StringAndValue data[]={ | 135 static const StringAndValue data[]={ |
| 137 { "a", 1 } | 136 { "a", 1 } |
| 138 }; | 137 }; |
| 139 checkData(data, LENGTHOF(data)); | 138 checkData(data, UPRV_LENGTHOF(data)); |
| 140 } | 139 } |
| 141 | 140 |
| 142 void BytesTrieTest::Test_a_ab() { | 141 void BytesTrieTest::Test_a_ab() { |
| 143 static const StringAndValue data[]={ | 142 static const StringAndValue data[]={ |
| 144 { "a", 1 }, | 143 { "a", 1 }, |
| 145 { "ab", 100 } | 144 { "ab", 100 } |
| 146 }; | 145 }; |
| 147 checkData(data, LENGTHOF(data)); | 146 checkData(data, UPRV_LENGTHOF(data)); |
| 148 } | 147 } |
| 149 | 148 |
| 150 void BytesTrieTest::TestShortestBranch() { | 149 void BytesTrieTest::TestShortestBranch() { |
| 151 static const StringAndValue data[]={ | 150 static const StringAndValue data[]={ |
| 152 { "a", 1000 }, | 151 { "a", 1000 }, |
| 153 { "b", 2000 } | 152 { "b", 2000 } |
| 154 }; | 153 }; |
| 155 checkData(data, LENGTHOF(data)); | 154 checkData(data, UPRV_LENGTHOF(data)); |
| 156 } | 155 } |
| 157 | 156 |
| 158 void BytesTrieTest::TestBranches() { | 157 void BytesTrieTest::TestBranches() { |
| 159 static const StringAndValue data[]={ | 158 static const StringAndValue data[]={ |
| 160 { "a", 0x10 }, | 159 { "a", 0x10 }, |
| 161 { "cc", 0x40 }, | 160 { "cc", 0x40 }, |
| 162 { "e", 0x100 }, | 161 { "e", 0x100 }, |
| 163 { "ggg", 0x400 }, | 162 { "ggg", 0x400 }, |
| 164 { "i", 0x1000 }, | 163 { "i", 0x1000 }, |
| 165 { "kkkk", 0x4000 }, | 164 { "kkkk", 0x4000 }, |
| 166 { "n", 0x10000 }, | 165 { "n", 0x10000 }, |
| 167 { "ppppp", 0x40000 }, | 166 { "ppppp", 0x40000 }, |
| 168 { "r", 0x100000 }, | 167 { "r", 0x100000 }, |
| 169 { "sss", 0x200000 }, | 168 { "sss", 0x200000 }, |
| 170 { "t", 0x400000 }, | 169 { "t", 0x400000 }, |
| 171 { "uu", 0x800000 }, | 170 { "uu", 0x800000 }, |
| 172 { "vv", 0x7fffffff }, | 171 { "vv", 0x7fffffff }, |
| 173 { "zz", (int32_t)0x80000000 } | 172 { "zz", (int32_t)0x80000000 } |
| 174 }; | 173 }; |
| 175 for(int32_t length=2; length<=LENGTHOF(data); ++length) { | 174 for(int32_t length=2; length<=UPRV_LENGTHOF(data); ++length) { |
| 176 logln("TestBranches length=%d", (int)length); | 175 logln("TestBranches length=%d", (int)length); |
| 177 checkData(data, length); | 176 checkData(data, length); |
| 178 } | 177 } |
| 179 } | 178 } |
| 180 | 179 |
| 181 void BytesTrieTest::TestLongSequence() { | 180 void BytesTrieTest::TestLongSequence() { |
| 182 static const StringAndValue data[]={ | 181 static const StringAndValue data[]={ |
| 183 { "a", -1 }, | 182 { "a", -1 }, |
| 184 // sequence of linear-match nodes | 183 // sequence of linear-match nodes |
| 185 { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -2 }, | 184 { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -2 }, |
| 186 // more than 256 bytes | 185 // more than 256 bytes |
| 187 { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | 186 { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 188 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | 187 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 189 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | 188 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 190 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | 189 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 191 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | 190 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 192 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -3 } | 191 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -3 } |
| 193 }; | 192 }; |
| 194 checkData(data, LENGTHOF(data)); | 193 checkData(data, UPRV_LENGTHOF(data)); |
| 195 } | 194 } |
| 196 | 195 |
| 197 void BytesTrieTest::TestLongBranch() { | 196 void BytesTrieTest::TestLongBranch() { |
| 198 // Split-branch and interesting compact-integer values. | 197 // Split-branch and interesting compact-integer values. |
| 199 static const StringAndValue data[]={ | 198 static const StringAndValue data[]={ |
| 200 { "a", -2 }, | 199 { "a", -2 }, |
| 201 { "b", -1 }, | 200 { "b", -1 }, |
| 202 { "c", 0 }, | 201 { "c", 0 }, |
| 203 { "d2", 1 }, | 202 { "d2", 1 }, |
| 204 { "f", 0x3f }, | 203 { "f", 0x3f }, |
| 205 { "g", 0x40 }, | 204 { "g", 0x40 }, |
| 206 { "h", 0x41 }, | 205 { "h", 0x41 }, |
| 207 { "j23", 0x1900 }, | 206 { "j23", 0x1900 }, |
| 208 { "j24", 0x19ff }, | 207 { "j24", 0x19ff }, |
| 209 { "j25", 0x1a00 }, | 208 { "j25", 0x1a00 }, |
| 210 { "k2", 0x1a80 }, | 209 { "k2", 0x1a80 }, |
| 211 { "k3", 0x1aff }, | 210 { "k3", 0x1aff }, |
| 212 { "l234567890", 0x1b00 }, | 211 { "l234567890", 0x1b00 }, |
| 213 { "l234567890123", 0x1b01 }, | 212 { "l234567890123", 0x1b01 }, |
| 214 { "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn", 0x10ffff }, | 213 { "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn", 0x10ffff }, |
| 215 { "oooooooooooooooooooooooooooooooooooooooooooooooooooooo", 0x110000 }, | 214 { "oooooooooooooooooooooooooooooooooooooooooooooooooooooo", 0x110000 }, |
| 216 { "pppppppppppppppppppppppppppppppppppppppppppppppppppppp", 0x120000 }, | 215 { "pppppppppppppppppppppppppppppppppppppppppppppppppppppp", 0x120000 }, |
| 217 { "r", 0x333333 }, | 216 { "r", 0x333333 }, |
| 218 { "s2345", 0x4444444 }, | 217 { "s2345", 0x4444444 }, |
| 219 { "t234567890", 0x77777777 }, | 218 { "t234567890", 0x77777777 }, |
| 220 { "z", (int32_t)0x80000001 } | 219 { "z", (int32_t)0x80000001 } |
| 221 }; | 220 }; |
| 222 checkData(data, LENGTHOF(data)); | 221 checkData(data, UPRV_LENGTHOF(data)); |
| 223 } | 222 } |
| 224 | 223 |
| 225 void BytesTrieTest::TestValuesForState() { | 224 void BytesTrieTest::TestValuesForState() { |
| 226 // Check that saveState() and resetToState() interact properly | 225 // Check that saveState() and resetToState() interact properly |
| 227 // with next() and current(). | 226 // with next() and current(). |
| 228 static const StringAndValue data[]={ | 227 static const StringAndValue data[]={ |
| 229 { "a", -1 }, | 228 { "a", -1 }, |
| 230 { "ab", -2 }, | 229 { "ab", -2 }, |
| 231 { "abc", -3 }, | 230 { "abc", -3 }, |
| 232 { "abcd", -4 }, | 231 { "abcd", -4 }, |
| 233 { "abcde", -5 }, | 232 { "abcde", -5 }, |
| 234 { "abcdef", -6 } | 233 { "abcdef", -6 } |
| 235 }; | 234 }; |
| 236 checkData(data, LENGTHOF(data)); | 235 checkData(data, UPRV_LENGTHOF(data)); |
| 237 } | 236 } |
| 238 | 237 |
| 239 void BytesTrieTest::TestCompact() { | 238 void BytesTrieTest::TestCompact() { |
| 240 // Duplicate trailing strings and values provide opportunities for compactin
g. | 239 // Duplicate trailing strings and values provide opportunities for compactin
g. |
| 241 static const StringAndValue data[]={ | 240 static const StringAndValue data[]={ |
| 242 { "+", 0 }, | 241 { "+", 0 }, |
| 243 { "+august", 8 }, | 242 { "+august", 8 }, |
| 244 { "+december", 12 }, | 243 { "+december", 12 }, |
| 245 { "+july", 7 }, | 244 { "+july", 7 }, |
| 246 { "+june", 6 }, | 245 { "+june", 6 }, |
| 247 { "+november", 11 }, | 246 { "+november", 11 }, |
| 248 { "+october", 10 }, | 247 { "+october", 10 }, |
| 249 { "+september", 9 }, | 248 { "+september", 9 }, |
| 250 { "-", 0 }, | 249 { "-", 0 }, |
| 251 { "-august", 8 }, | 250 { "-august", 8 }, |
| 252 { "-december", 12 }, | 251 { "-december", 12 }, |
| 253 { "-july", 7 }, | 252 { "-july", 7 }, |
| 254 { "-june", 6 }, | 253 { "-june", 6 }, |
| 255 { "-november", 11 }, | 254 { "-november", 11 }, |
| 256 { "-october", 10 }, | 255 { "-october", 10 }, |
| 257 { "-september", 9 }, | 256 { "-september", 9 }, |
| 258 // The l+n branch (with its sub-nodes) is a duplicate but will be writte
n | 257 // The l+n branch (with its sub-nodes) is a duplicate but will be writte
n |
| 259 // both times because each time it follows a different linear-match node
. | 258 // both times because each time it follows a different linear-match node
. |
| 260 { "xjuly", 7 }, | 259 { "xjuly", 7 }, |
| 261 { "xjune", 6 } | 260 { "xjune", 6 } |
| 262 }; | 261 }; |
| 263 checkData(data, LENGTHOF(data)); | 262 checkData(data, UPRV_LENGTHOF(data)); |
| 264 } | 263 } |
| 265 | 264 |
| 266 BytesTrie *BytesTrieTest::buildMonthsTrie(UStringTrieBuildOption buildOption) { | 265 BytesTrie *BytesTrieTest::buildMonthsTrie(UStringTrieBuildOption buildOption) { |
| 267 // All types of nodes leading to the same value, | 266 // All types of nodes leading to the same value, |
| 268 // for code coverage of recursive functions. | 267 // for code coverage of recursive functions. |
| 269 // In particular, we need a lot of branches on some single level | 268 // In particular, we need a lot of branches on some single level |
| 270 // to exercise a split-branch node. | 269 // to exercise a split-branch node. |
| 271 static const StringAndValue data[]={ | 270 static const StringAndValue data[]={ |
| 272 { "august", 8 }, | 271 { "august", 8 }, |
| 273 { "jan", 1 }, | 272 { "jan", 1 }, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 293 { "janpp", 1 }, | 292 { "janpp", 1 }, |
| 294 { "janqqq", 1 }, | 293 { "janqqq", 1 }, |
| 295 { "janr", 1 }, | 294 { "janr", 1 }, |
| 296 { "januar", 1 }, | 295 { "januar", 1 }, |
| 297 { "january", 1 }, | 296 { "january", 1 }, |
| 298 { "july", 7 }, | 297 { "july", 7 }, |
| 299 { "jun", 6 }, | 298 { "jun", 6 }, |
| 300 { "jun.", 6 }, | 299 { "jun.", 6 }, |
| 301 { "june", 6 } | 300 { "june", 6 } |
| 302 }; | 301 }; |
| 303 return buildTrie(data, LENGTHOF(data), buildOption); | 302 return buildTrie(data, UPRV_LENGTHOF(data), buildOption); |
| 304 } | 303 } |
| 305 | 304 |
| 306 void BytesTrieTest::TestHasUniqueValue() { | 305 void BytesTrieTest::TestHasUniqueValue() { |
| 307 LocalPointer<BytesTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_FAST)); | 306 LocalPointer<BytesTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_FAST)); |
| 308 if(trie.isNull()) { | 307 if(trie.isNull()) { |
| 309 return; // buildTrie() reported an error | 308 return; // buildTrie() reported an error |
| 310 } | 309 } |
| 311 int32_t uniqueValue; | 310 int32_t uniqueValue; |
| 312 if(trie->hasUniqueValue(uniqueValue)) { | 311 if(trie->hasUniqueValue(uniqueValue)) { |
| 313 errln("unique value at root"); | 312 errln("unique value at root"); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 338 errln("not unique value 8 after \"au\""); | 337 errln("not unique value 8 after \"au\""); |
| 339 } | 338 } |
| 340 } | 339 } |
| 341 | 340 |
| 342 void BytesTrieTest::TestGetNextBytes() { | 341 void BytesTrieTest::TestGetNextBytes() { |
| 343 LocalPointer<BytesTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_SMALL)); | 342 LocalPointer<BytesTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_SMALL)); |
| 344 if(trie.isNull()) { | 343 if(trie.isNull()) { |
| 345 return; // buildTrie() reported an error | 344 return; // buildTrie() reported an error |
| 346 } | 345 } |
| 347 char buffer[40]; | 346 char buffer[40]; |
| 348 CheckedArrayByteSink sink(buffer, LENGTHOF(buffer)); | 347 CheckedArrayByteSink sink(buffer, UPRV_LENGTHOF(buffer)); |
| 349 int32_t count=trie->getNextBytes(sink); | 348 int32_t count=trie->getNextBytes(sink); |
| 350 if(count!=2 || sink.NumberOfBytesAppended()!=2 || buffer[0]!='a' || buffer[1
]!='j') { | 349 if(count!=2 || sink.NumberOfBytesAppended()!=2 || buffer[0]!='a' || buffer[1
]!='j') { |
| 351 errln("months getNextBytes()!=[aj] at root"); | 350 errln("months getNextBytes()!=[aj] at root"); |
| 352 } | 351 } |
| 353 trie->next('j'); | 352 trie->next('j'); |
| 354 trie->next('a'); | 353 trie->next('a'); |
| 355 trie->next('n'); | 354 trie->next('n'); |
| 356 // getNextBytes() directly after next() | 355 // getNextBytes() directly after next() |
| 357 count=trie->getNextBytes(sink.Reset()); | 356 count=trie->getNextBytes(sink.Reset()); |
| 358 buffer[count]=0; | 357 buffer[count]=0; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 { "l", 1 }, | 423 { "l", 1 }, |
| 425 { "m", 1 }, | 424 { "m", 1 }, |
| 426 { "nnnnnnnnnnnnnnnnnnnnnnnnnnnn", 1 }, | 425 { "nnnnnnnnnnnnnnnnnnnnnnnnnnnn", 1 }, |
| 427 { "o", 1 }, | 426 { "o", 1 }, |
| 428 { "pp", 1 }, | 427 { "pp", 1 }, |
| 429 { "qqq", 1 }, | 428 { "qqq", 1 }, |
| 430 { "r", 1 }, | 429 { "r", 1 }, |
| 431 { "uar", 1 }, | 430 { "uar", 1 }, |
| 432 { "uary", 1 } | 431 { "uary", 1 } |
| 433 }; | 432 }; |
| 434 checkIterator(iter, data, LENGTHOF(data)); | 433 checkIterator(iter, data, UPRV_LENGTHOF(data)); |
| 435 // Reset, and we should get the same result. | 434 // Reset, and we should get the same result. |
| 436 logln("after iter.reset()"); | 435 logln("after iter.reset()"); |
| 437 checkIterator(iter.reset(), data, LENGTHOF(data)); | 436 checkIterator(iter.reset(), data, UPRV_LENGTHOF(data)); |
| 438 } | 437 } |
| 439 | 438 |
| 440 void BytesTrieTest::TestIteratorFromLinearMatch() { | 439 void BytesTrieTest::TestIteratorFromLinearMatch() { |
| 441 LocalPointer<BytesTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_SMALL)); | 440 LocalPointer<BytesTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_SMALL)); |
| 442 if(trie.isNull()) { | 441 if(trie.isNull()) { |
| 443 return; // buildTrie() reported an error | 442 return; // buildTrie() reported an error |
| 444 } | 443 } |
| 445 // Go into a linear-match node. | 444 // Go into a linear-match node. |
| 446 trie->next('j'); | 445 trie->next('j'); |
| 447 trie->next('a'); | 446 trie->next('a'); |
| 448 trie->next('n'); | 447 trie->next('n'); |
| 449 trie->next('u'); | 448 trie->next('u'); |
| 450 trie->next('a'); | 449 trie->next('a'); |
| 451 IcuTestErrorCode errorCode(*this, "TestIteratorFromLinearMatch()"); | 450 IcuTestErrorCode errorCode(*this, "TestIteratorFromLinearMatch()"); |
| 452 BytesTrie::Iterator iter(*trie, 0, errorCode); | 451 BytesTrie::Iterator iter(*trie, 0, errorCode); |
| 453 if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor"))
{ | 452 if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor"))
{ |
| 454 return; | 453 return; |
| 455 } | 454 } |
| 456 // Expected data: Same as in buildMonthsTrie(), except only the suffixes | 455 // Expected data: Same as in buildMonthsTrie(), except only the suffixes |
| 457 // following "janua". | 456 // following "janua". |
| 458 static const StringAndValue data[]={ | 457 static const StringAndValue data[]={ |
| 459 { "r", 1 }, | 458 { "r", 1 }, |
| 460 { "ry", 1 } | 459 { "ry", 1 } |
| 461 }; | 460 }; |
| 462 checkIterator(iter, data, LENGTHOF(data)); | 461 checkIterator(iter, data, UPRV_LENGTHOF(data)); |
| 463 // Reset, and we should get the same result. | 462 // Reset, and we should get the same result. |
| 464 logln("after iter.reset()"); | 463 logln("after iter.reset()"); |
| 465 checkIterator(iter.reset(), data, LENGTHOF(data)); | 464 checkIterator(iter.reset(), data, UPRV_LENGTHOF(data)); |
| 466 } | 465 } |
| 467 | 466 |
| 468 void BytesTrieTest::TestTruncatingIteratorFromRoot() { | 467 void BytesTrieTest::TestTruncatingIteratorFromRoot() { |
| 469 LocalPointer<BytesTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_FAST)); | 468 LocalPointer<BytesTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_FAST)); |
| 470 if(trie.isNull()) { | 469 if(trie.isNull()) { |
| 471 return; // buildTrie() reported an error | 470 return; // buildTrie() reported an error |
| 472 } | 471 } |
| 473 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromRoot()"); | 472 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromRoot()"); |
| 474 BytesTrie::Iterator iter(*trie, 4, errorCode); | 473 BytesTrie::Iterator iter(*trie, 4, errorCode); |
| 475 if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor"))
{ | 474 if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor"))
{ |
| (...skipping 22 matching lines...) Expand all Loading... |
| 498 { "jano", 1 }, | 497 { "jano", 1 }, |
| 499 { "janp", -1 }, | 498 { "janp", -1 }, |
| 500 { "janq", -1 }, | 499 { "janq", -1 }, |
| 501 { "janr", 1 }, | 500 { "janr", 1 }, |
| 502 { "janu", -1 }, | 501 { "janu", -1 }, |
| 503 { "july", 7 }, | 502 { "july", 7 }, |
| 504 { "jun", 6 }, | 503 { "jun", 6 }, |
| 505 { "jun.", 6 }, | 504 { "jun.", 6 }, |
| 506 { "june", 6 } | 505 { "june", 6 } |
| 507 }; | 506 }; |
| 508 checkIterator(iter, data, LENGTHOF(data)); | 507 checkIterator(iter, data, UPRV_LENGTHOF(data)); |
| 509 // Reset, and we should get the same result. | 508 // Reset, and we should get the same result. |
| 510 logln("after iter.reset()"); | 509 logln("after iter.reset()"); |
| 511 checkIterator(iter.reset(), data, LENGTHOF(data)); | 510 checkIterator(iter.reset(), data, UPRV_LENGTHOF(data)); |
| 512 } | 511 } |
| 513 | 512 |
| 514 void BytesTrieTest::TestTruncatingIteratorFromLinearMatchShort() { | 513 void BytesTrieTest::TestTruncatingIteratorFromLinearMatchShort() { |
| 515 static const StringAndValue data[]={ | 514 static const StringAndValue data[]={ |
| 516 { "abcdef", 10 }, | 515 { "abcdef", 10 }, |
| 517 { "abcdepq", 200 }, | 516 { "abcdepq", 200 }, |
| 518 { "abcdeyz", 3000 } | 517 { "abcdeyz", 3000 } |
| 519 }; | 518 }; |
| 520 LocalPointer<BytesTrie> trie(buildTrie(data, LENGTHOF(data), USTRINGTRIE_BUI
LD_FAST)); | 519 LocalPointer<BytesTrie> trie(buildTrie(data, UPRV_LENGTHOF(data), USTRINGTRI
E_BUILD_FAST)); |
| 521 if(trie.isNull()) { | 520 if(trie.isNull()) { |
| 522 return; // buildTrie() reported an error | 521 return; // buildTrie() reported an error |
| 523 } | 522 } |
| 524 // Go into a linear-match node. | 523 // Go into a linear-match node. |
| 525 trie->next('a'); | 524 trie->next('a'); |
| 526 trie->next('b'); | 525 trie->next('b'); |
| 527 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromLinearMatchShor
t()"); | 526 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromLinearMatchShor
t()"); |
| 528 // Truncate within the linear-match node. | 527 // Truncate within the linear-match node. |
| 529 BytesTrie::Iterator iter(*trie, 2, errorCode); | 528 BytesTrie::Iterator iter(*trie, 2, errorCode); |
| 530 if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor"))
{ | 529 if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor"))
{ |
| 531 return; | 530 return; |
| 532 } | 531 } |
| 533 static const StringAndValue expected[]={ | 532 static const StringAndValue expected[]={ |
| 534 { "cd", -1 } | 533 { "cd", -1 } |
| 535 }; | 534 }; |
| 536 checkIterator(iter, expected, LENGTHOF(expected)); | 535 checkIterator(iter, expected, UPRV_LENGTHOF(expected)); |
| 537 // Reset, and we should get the same result. | 536 // Reset, and we should get the same result. |
| 538 logln("after iter.reset()"); | 537 logln("after iter.reset()"); |
| 539 checkIterator(iter.reset(), expected, LENGTHOF(expected)); | 538 checkIterator(iter.reset(), expected, UPRV_LENGTHOF(expected)); |
| 540 } | 539 } |
| 541 | 540 |
| 542 void BytesTrieTest::TestTruncatingIteratorFromLinearMatchLong() { | 541 void BytesTrieTest::TestTruncatingIteratorFromLinearMatchLong() { |
| 543 static const StringAndValue data[]={ | 542 static const StringAndValue data[]={ |
| 544 { "abcdef", 10 }, | 543 { "abcdef", 10 }, |
| 545 { "abcdepq", 200 }, | 544 { "abcdepq", 200 }, |
| 546 { "abcdeyz", 3000 } | 545 { "abcdeyz", 3000 } |
| 547 }; | 546 }; |
| 548 LocalPointer<BytesTrie> trie(buildTrie(data, LENGTHOF(data), USTRINGTRIE_BUI
LD_FAST)); | 547 LocalPointer<BytesTrie> trie(buildTrie(data, UPRV_LENGTHOF(data), USTRINGTRI
E_BUILD_FAST)); |
| 549 if(trie.isNull()) { | 548 if(trie.isNull()) { |
| 550 return; // buildTrie() reported an error | 549 return; // buildTrie() reported an error |
| 551 } | 550 } |
| 552 // Go into a linear-match node. | 551 // Go into a linear-match node. |
| 553 trie->next('a'); | 552 trie->next('a'); |
| 554 trie->next('b'); | 553 trie->next('b'); |
| 555 trie->next('c'); | 554 trie->next('c'); |
| 556 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromLinearMatchLong
()"); | 555 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromLinearMatchLong
()"); |
| 557 // Truncate after the linear-match node. | 556 // Truncate after the linear-match node. |
| 558 BytesTrie::Iterator iter(*trie, 3, errorCode); | 557 BytesTrie::Iterator iter(*trie, 3, errorCode); |
| 559 if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor"))
{ | 558 if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor"))
{ |
| 560 return; | 559 return; |
| 561 } | 560 } |
| 562 static const StringAndValue expected[]={ | 561 static const StringAndValue expected[]={ |
| 563 { "def", 10 }, | 562 { "def", 10 }, |
| 564 { "dep", -1 }, | 563 { "dep", -1 }, |
| 565 { "dey", -1 } | 564 { "dey", -1 } |
| 566 }; | 565 }; |
| 567 checkIterator(iter, expected, LENGTHOF(expected)); | 566 checkIterator(iter, expected, UPRV_LENGTHOF(expected)); |
| 568 // Reset, and we should get the same result. | 567 // Reset, and we should get the same result. |
| 569 logln("after iter.reset()"); | 568 logln("after iter.reset()"); |
| 570 checkIterator(iter.reset(), expected, LENGTHOF(expected)); | 569 checkIterator(iter.reset(), expected, UPRV_LENGTHOF(expected)); |
| 571 } | 570 } |
| 572 | 571 |
| 573 void BytesTrieTest::TestIteratorFromBytes() { | 572 void BytesTrieTest::TestIteratorFromBytes() { |
| 574 static const StringAndValue data[]={ | 573 static const StringAndValue data[]={ |
| 575 { "mm", 3 }, | 574 { "mm", 3 }, |
| 576 { "mmm", 33 }, | 575 { "mmm", 33 }, |
| 577 { "mmnop", 333 } | 576 { "mmnop", 333 } |
| 578 }; | 577 }; |
| 579 builder_->clear(); | 578 builder_->clear(); |
| 580 IcuTestErrorCode errorCode(*this, "TestIteratorFromBytes()"); | 579 IcuTestErrorCode errorCode(*this, "TestIteratorFromBytes()"); |
| 581 for(int32_t i=0; i<LENGTHOF(data); ++i) { | 580 for(int32_t i=0; i<UPRV_LENGTHOF(data); ++i) { |
| 582 builder_->add(data[i].s, data[i].value, errorCode); | 581 builder_->add(data[i].s, data[i].value, errorCode); |
| 583 } | 582 } |
| 584 StringPiece trieBytes=builder_->buildStringPiece(USTRINGTRIE_BUILD_FAST, err
orCode); | 583 StringPiece trieBytes=builder_->buildStringPiece(USTRINGTRIE_BUILD_FAST, err
orCode); |
| 585 BytesTrie::Iterator iter(trieBytes.data(), 0, errorCode); | 584 BytesTrie::Iterator iter(trieBytes.data(), 0, errorCode); |
| 586 checkIterator(iter, data, LENGTHOF(data)); | 585 checkIterator(iter, data, UPRV_LENGTHOF(data)); |
| 587 } | 586 } |
| 588 | 587 |
| 589 void BytesTrieTest::checkData(const StringAndValue data[], int32_t dataLength) { | 588 void BytesTrieTest::checkData(const StringAndValue data[], int32_t dataLength) { |
| 590 logln("checkData(dataLength=%d, fast)", (int)dataLength); | 589 logln("checkData(dataLength=%d, fast)", (int)dataLength); |
| 591 checkData(data, dataLength, USTRINGTRIE_BUILD_FAST); | 590 checkData(data, dataLength, USTRINGTRIE_BUILD_FAST); |
| 592 logln("checkData(dataLength=%d, small)", (int)dataLength); | 591 logln("checkData(dataLength=%d, small)", (int)dataLength); |
| 593 checkData(data, dataLength, USTRINGTRIE_BUILD_SMALL); | 592 checkData(data, dataLength, USTRINGTRIE_BUILD_SMALL); |
| 594 } | 593 } |
| 595 | 594 |
| 596 void BytesTrieTest::checkData(const StringAndValue data[], int32_t dataLength, U
StringTrieBuildOption buildOption) { | 595 void BytesTrieTest::checkData(const StringAndValue data[], int32_t dataLength, U
StringTrieBuildOption buildOption) { |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 871 } | 870 } |
| 872 if(iter.hasNext()) { | 871 if(iter.hasNext()) { |
| 873 errln("trie iterator hasNext()=TRUE after all items"); | 872 errln("trie iterator hasNext()=TRUE after all items"); |
| 874 } | 873 } |
| 875 UBool hasNext=iter.next(errorCode); | 874 UBool hasNext=iter.next(errorCode); |
| 876 errorCode.logIfFailureAndReset("trie iterator next() after all items"); | 875 errorCode.logIfFailureAndReset("trie iterator next() after all items"); |
| 877 if(hasNext) { | 876 if(hasNext) { |
| 878 errln("trie iterator next()=TRUE after all items"); | 877 errln("trie iterator next()=TRUE after all items"); |
| 879 } | 878 } |
| 880 } | 879 } |
| OLD | NEW |