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: ucharstrietest.cpp | 6 * file name: ucharstrietest.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/appendable.h" | 18 #include "unicode/appendable.h" |
19 #include "unicode/localpointer.h" | 19 #include "unicode/localpointer.h" |
20 #include "unicode/ucharstrie.h" | 20 #include "unicode/ucharstrie.h" |
21 #include "unicode/ucharstriebuilder.h" | 21 #include "unicode/ucharstriebuilder.h" |
22 #include "unicode/uniset.h" | 22 #include "unicode/uniset.h" |
23 #include "unicode/unistr.h" | 23 #include "unicode/unistr.h" |
24 #include "intltest.h" | 24 #include "intltest.h" |
25 | 25 #include "cmemory.h" |
26 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) | |
27 | 26 |
28 struct StringAndValue { | 27 struct StringAndValue { |
29 const char *s; | 28 const char *s; |
30 int32_t value; | 29 int32_t value; |
31 }; | 30 }; |
32 | 31 |
33 class UCharsTrieTest : public IntlTest { | 32 class UCharsTrieTest : public IntlTest { |
34 public: | 33 public: |
35 UCharsTrieTest(); | 34 UCharsTrieTest(); |
36 virtual ~UCharsTrieTest(); | 35 virtual ~UCharsTrieTest(); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 if(errorCode.reset()!=U_ILLEGAL_ARGUMENT_ERROR) { | 130 if(errorCode.reset()!=U_ILLEGAL_ARGUMENT_ERROR) { |
132 errln("UCharsTrieBuilder.add() did not detect duplicates"); | 131 errln("UCharsTrieBuilder.add() did not detect duplicates"); |
133 return; | 132 return; |
134 } | 133 } |
135 } | 134 } |
136 | 135 |
137 void UCharsTrieTest::TestEmpty() { | 136 void UCharsTrieTest::TestEmpty() { |
138 static const StringAndValue data[]={ | 137 static const StringAndValue data[]={ |
139 { "", 0 } | 138 { "", 0 } |
140 }; | 139 }; |
141 checkData(data, LENGTHOF(data)); | 140 checkData(data, UPRV_LENGTHOF(data)); |
142 } | 141 } |
143 | 142 |
144 void UCharsTrieTest::Test_a() { | 143 void UCharsTrieTest::Test_a() { |
145 static const StringAndValue data[]={ | 144 static const StringAndValue data[]={ |
146 { "a", 1 } | 145 { "a", 1 } |
147 }; | 146 }; |
148 checkData(data, LENGTHOF(data)); | 147 checkData(data, UPRV_LENGTHOF(data)); |
149 } | 148 } |
150 | 149 |
151 void UCharsTrieTest::Test_a_ab() { | 150 void UCharsTrieTest::Test_a_ab() { |
152 static const StringAndValue data[]={ | 151 static const StringAndValue data[]={ |
153 { "a", 1 }, | 152 { "a", 1 }, |
154 { "ab", 100 } | 153 { "ab", 100 } |
155 }; | 154 }; |
156 checkData(data, LENGTHOF(data)); | 155 checkData(data, UPRV_LENGTHOF(data)); |
157 } | 156 } |
158 | 157 |
159 void UCharsTrieTest::TestShortestBranch() { | 158 void UCharsTrieTest::TestShortestBranch() { |
160 static const StringAndValue data[]={ | 159 static const StringAndValue data[]={ |
161 { "a", 1000 }, | 160 { "a", 1000 }, |
162 { "b", 2000 } | 161 { "b", 2000 } |
163 }; | 162 }; |
164 checkData(data, LENGTHOF(data)); | 163 checkData(data, UPRV_LENGTHOF(data)); |
165 } | 164 } |
166 | 165 |
167 void UCharsTrieTest::TestBranches() { | 166 void UCharsTrieTest::TestBranches() { |
168 static const StringAndValue data[]={ | 167 static const StringAndValue data[]={ |
169 { "a", 0x10 }, | 168 { "a", 0x10 }, |
170 { "cc", 0x40 }, | 169 { "cc", 0x40 }, |
171 { "e", 0x100 }, | 170 { "e", 0x100 }, |
172 { "ggg", 0x400 }, | 171 { "ggg", 0x400 }, |
173 { "i", 0x1000 }, | 172 { "i", 0x1000 }, |
174 { "kkkk", 0x4000 }, | 173 { "kkkk", 0x4000 }, |
175 { "n", 0x10000 }, | 174 { "n", 0x10000 }, |
176 { "ppppp", 0x40000 }, | 175 { "ppppp", 0x40000 }, |
177 { "r", 0x100000 }, | 176 { "r", 0x100000 }, |
178 { "sss", 0x200000 }, | 177 { "sss", 0x200000 }, |
179 { "t", 0x400000 }, | 178 { "t", 0x400000 }, |
180 { "uu", 0x800000 }, | 179 { "uu", 0x800000 }, |
181 { "vv", 0x7fffffff }, | 180 { "vv", 0x7fffffff }, |
182 { "zz", (int32_t)0x80000000 } | 181 { "zz", (int32_t)0x80000000 } |
183 }; | 182 }; |
184 for(int32_t length=2; length<=LENGTHOF(data); ++length) { | 183 for(int32_t length=2; length<=UPRV_LENGTHOF(data); ++length) { |
185 logln("TestBranches length=%d", (int)length); | 184 logln("TestBranches length=%d", (int)length); |
186 checkData(data, length); | 185 checkData(data, length); |
187 } | 186 } |
188 } | 187 } |
189 | 188 |
190 void UCharsTrieTest::TestLongSequence() { | 189 void UCharsTrieTest::TestLongSequence() { |
191 static const StringAndValue data[]={ | 190 static const StringAndValue data[]={ |
192 { "a", -1 }, | 191 { "a", -1 }, |
193 // sequence of linear-match nodes | 192 // sequence of linear-match nodes |
194 { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -2 }, | 193 { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -2 }, |
195 // more than 256 units | 194 // more than 256 units |
196 { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | 195 { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
197 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | 196 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
198 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | 197 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
199 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | 198 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
200 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | 199 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
201 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -3 } | 200 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -3 } |
202 }; | 201 }; |
203 checkData(data, LENGTHOF(data)); | 202 checkData(data, UPRV_LENGTHOF(data)); |
204 } | 203 } |
205 | 204 |
206 void UCharsTrieTest::TestLongBranch() { | 205 void UCharsTrieTest::TestLongBranch() { |
207 // Split-branch and interesting compact-integer values. | 206 // Split-branch and interesting compact-integer values. |
208 static const StringAndValue data[]={ | 207 static const StringAndValue data[]={ |
209 { "a", -2 }, | 208 { "a", -2 }, |
210 { "b", -1 }, | 209 { "b", -1 }, |
211 { "c", 0 }, | 210 { "c", 0 }, |
212 { "d2", 1 }, | 211 { "d2", 1 }, |
213 { "f", 0x3f }, | 212 { "f", 0x3f }, |
214 { "g", 0x40 }, | 213 { "g", 0x40 }, |
215 { "h", 0x41 }, | 214 { "h", 0x41 }, |
216 { "j23", 0x1900 }, | 215 { "j23", 0x1900 }, |
217 { "j24", 0x19ff }, | 216 { "j24", 0x19ff }, |
218 { "j25", 0x1a00 }, | 217 { "j25", 0x1a00 }, |
219 { "k2", 0x1a80 }, | 218 { "k2", 0x1a80 }, |
220 { "k3", 0x1aff }, | 219 { "k3", 0x1aff }, |
221 { "l234567890", 0x1b00 }, | 220 { "l234567890", 0x1b00 }, |
222 { "l234567890123", 0x1b01 }, | 221 { "l234567890123", 0x1b01 }, |
223 { "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn", 0x10ffff }, | 222 { "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn", 0x10ffff }, |
224 { "oooooooooooooooooooooooooooooooooooooooooooooooooooooo", 0x110000 }, | 223 { "oooooooooooooooooooooooooooooooooooooooooooooooooooooo", 0x110000 }, |
225 { "pppppppppppppppppppppppppppppppppppppppppppppppppppppp", 0x120000 }, | 224 { "pppppppppppppppppppppppppppppppppppppppppppppppppppppp", 0x120000 }, |
226 { "r", 0x333333 }, | 225 { "r", 0x333333 }, |
227 { "s2345", 0x4444444 }, | 226 { "s2345", 0x4444444 }, |
228 { "t234567890", 0x77777777 }, | 227 { "t234567890", 0x77777777 }, |
229 { "z", (int32_t)0x80000001 } | 228 { "z", (int32_t)0x80000001 } |
230 }; | 229 }; |
231 checkData(data, LENGTHOF(data)); | 230 checkData(data, UPRV_LENGTHOF(data)); |
232 } | 231 } |
233 | 232 |
234 void UCharsTrieTest::TestValuesForState() { | 233 void UCharsTrieTest::TestValuesForState() { |
235 // Check that saveState() and resetToState() interact properly | 234 // Check that saveState() and resetToState() interact properly |
236 // with next() and current(). | 235 // with next() and current(). |
237 static const StringAndValue data[]={ | 236 static const StringAndValue data[]={ |
238 { "a", -1 }, | 237 { "a", -1 }, |
239 { "ab", -2 }, | 238 { "ab", -2 }, |
240 { "abc", -3 }, | 239 { "abc", -3 }, |
241 { "abcd", -4 }, | 240 { "abcd", -4 }, |
242 { "abcde", -5 }, | 241 { "abcde", -5 }, |
243 { "abcdef", -6 } | 242 { "abcdef", -6 } |
244 }; | 243 }; |
245 checkData(data, LENGTHOF(data)); | 244 checkData(data, UPRV_LENGTHOF(data)); |
246 } | 245 } |
247 | 246 |
248 void UCharsTrieTest::TestCompact() { | 247 void UCharsTrieTest::TestCompact() { |
249 // Duplicate trailing strings and values provide opportunities for compactin
g. | 248 // Duplicate trailing strings and values provide opportunities for compactin
g. |
250 static const StringAndValue data[]={ | 249 static const StringAndValue data[]={ |
251 { "+", 0 }, | 250 { "+", 0 }, |
252 { "+august", 8 }, | 251 { "+august", 8 }, |
253 { "+december", 12 }, | 252 { "+december", 12 }, |
254 { "+july", 7 }, | 253 { "+july", 7 }, |
255 { "+june", 6 }, | 254 { "+june", 6 }, |
256 { "+november", 11 }, | 255 { "+november", 11 }, |
257 { "+october", 10 }, | 256 { "+october", 10 }, |
258 { "+september", 9 }, | 257 { "+september", 9 }, |
259 { "-", 0 }, | 258 { "-", 0 }, |
260 { "-august", 8 }, | 259 { "-august", 8 }, |
261 { "-december", 12 }, | 260 { "-december", 12 }, |
262 { "-july", 7 }, | 261 { "-july", 7 }, |
263 { "-june", 6 }, | 262 { "-june", 6 }, |
264 { "-november", 11 }, | 263 { "-november", 11 }, |
265 { "-october", 10 }, | 264 { "-october", 10 }, |
266 { "-september", 9 }, | 265 { "-september", 9 }, |
267 // The l+n branch (with its sub-nodes) is a duplicate but will be writte
n | 266 // The l+n branch (with its sub-nodes) is a duplicate but will be writte
n |
268 // both times because each time it follows a different linear-match node
. | 267 // both times because each time it follows a different linear-match node
. |
269 { "xjuly", 7 }, | 268 { "xjuly", 7 }, |
270 { "xjune", 6 } | 269 { "xjune", 6 } |
271 }; | 270 }; |
272 checkData(data, LENGTHOF(data)); | 271 checkData(data, UPRV_LENGTHOF(data)); |
273 } | 272 } |
274 | 273 |
275 void UCharsTrieTest::TestFirstForCodePoint() { | 274 void UCharsTrieTest::TestFirstForCodePoint() { |
276 static const StringAndValue data[]={ | 275 static const StringAndValue data[]={ |
277 { "a", 1 }, | 276 { "a", 1 }, |
278 { "a\\ud800", 2 }, | 277 { "a\\ud800", 2 }, |
279 { "a\\U00010000", 3 }, | 278 { "a\\U00010000", 3 }, |
280 { "\\ud840", 4 }, | 279 { "\\ud840", 4 }, |
281 { "\\U00020000\\udbff", 5 }, | 280 { "\\U00020000\\udbff", 5 }, |
282 { "\\U00020000\\U0010ffff", 6 }, | 281 { "\\U00020000\\U0010ffff", 6 }, |
283 { "\\U00020000\\U0010ffffz", 7 }, | 282 { "\\U00020000\\U0010ffffz", 7 }, |
284 { "\\U00050000xy", 8 }, | 283 { "\\U00050000xy", 8 }, |
285 { "\\U00050000xyz", 9 } | 284 { "\\U00050000xyz", 9 } |
286 }; | 285 }; |
287 checkData(data, LENGTHOF(data)); | 286 checkData(data, UPRV_LENGTHOF(data)); |
288 } | 287 } |
289 | 288 |
290 void UCharsTrieTest::TestNextForCodePoint() { | 289 void UCharsTrieTest::TestNextForCodePoint() { |
291 static const StringAndValue data[]={ | 290 static const StringAndValue data[]={ |
292 { "\\u4dff\\U00010000\\u9999\\U00020000\\udfff\\U0010ffff", 2000000000 }
, | 291 { "\\u4dff\\U00010000\\u9999\\U00020000\\udfff\\U0010ffff", 2000000000 }
, |
293 { "\\u4dff\\U00010000\\u9999\\U00020002", 44444 }, | 292 { "\\u4dff\\U00010000\\u9999\\U00020002", 44444 }, |
294 { "\\u4dff\\U000103ff", 99999 } | 293 { "\\u4dff\\U000103ff", 99999 } |
295 }; | 294 }; |
296 LocalPointer<UCharsTrie> trie(buildTrie(data, LENGTHOF(data), USTRINGTRIE_BU
ILD_FAST)); | 295 LocalPointer<UCharsTrie> trie(buildTrie(data, UPRV_LENGTHOF(data), USTRINGTR
IE_BUILD_FAST)); |
297 if(trie.isNull()) { | 296 if(trie.isNull()) { |
298 return; // buildTrie() reported an error | 297 return; // buildTrie() reported an error |
299 } | 298 } |
300 UStringTrieResult result; | 299 UStringTrieResult result; |
301 if( (result=trie->nextForCodePoint(0x4dff))!=USTRINGTRIE_NO_VALUE || result!
=trie->current() || | 300 if( (result=trie->nextForCodePoint(0x4dff))!=USTRINGTRIE_NO_VALUE || result!
=trie->current() || |
302 (result=trie->nextForCodePoint(0x10000))!=USTRINGTRIE_NO_VALUE || result
!=trie->current() || | 301 (result=trie->nextForCodePoint(0x10000))!=USTRINGTRIE_NO_VALUE || result
!=trie->current() || |
303 (result=trie->nextForCodePoint(0x9999))!=USTRINGTRIE_NO_VALUE || result!
=trie->current() || | 302 (result=trie->nextForCodePoint(0x9999))!=USTRINGTRIE_NO_VALUE || result!
=trie->current() || |
304 (result=trie->nextForCodePoint(0x20000))!=USTRINGTRIE_NO_VALUE || result
!=trie->current() || | 303 (result=trie->nextForCodePoint(0x20000))!=USTRINGTRIE_NO_VALUE || result
!=trie->current() || |
305 (result=trie->nextForCodePoint(0xdfff))!=USTRINGTRIE_NO_VALUE || result!
=trie->current() || | 304 (result=trie->nextForCodePoint(0xdfff))!=USTRINGTRIE_NO_VALUE || result!
=trie->current() || |
306 (result=trie->nextForCodePoint(0x10ffff))!=USTRINGTRIE_FINAL_VALUE || re
sult!=trie->current() || | 305 (result=trie->nextForCodePoint(0x10ffff))!=USTRINGTRIE_FINAL_VALUE || re
sult!=trie->current() || |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 { "janpp", 1 }, | 449 { "janpp", 1 }, |
451 { "janqqq", 1 }, | 450 { "janqqq", 1 }, |
452 { "janr", 1 }, | 451 { "janr", 1 }, |
453 { "januar", 1 }, | 452 { "januar", 1 }, |
454 { "january", 1 }, | 453 { "january", 1 }, |
455 { "july", 7 }, | 454 { "july", 7 }, |
456 { "jun", 6 }, | 455 { "jun", 6 }, |
457 { "jun.", 6 }, | 456 { "jun.", 6 }, |
458 { "june", 6 } | 457 { "june", 6 } |
459 }; | 458 }; |
460 return buildTrie(data, LENGTHOF(data), buildOption); | 459 return buildTrie(data, UPRV_LENGTHOF(data), buildOption); |
461 } | 460 } |
462 | 461 |
463 void UCharsTrieTest::TestHasUniqueValue() { | 462 void UCharsTrieTest::TestHasUniqueValue() { |
464 LocalPointer<UCharsTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_FAST)); | 463 LocalPointer<UCharsTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_FAST)); |
465 if(trie.isNull()) { | 464 if(trie.isNull()) { |
466 return; // buildTrie() reported an error | 465 return; // buildTrie() reported an error |
467 } | 466 } |
468 int32_t uniqueValue; | 467 int32_t uniqueValue; |
469 if(trie->hasUniqueValue(uniqueValue)) { | 468 if(trie->hasUniqueValue(uniqueValue)) { |
470 errln("unique value at root"); | 469 errln("unique value at root"); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 { "l", 1 }, | 581 { "l", 1 }, |
583 { "m", 1 }, | 582 { "m", 1 }, |
584 { "nnnnnnnnnnnnnnnnnnnnnnnnnnnn", 1 }, | 583 { "nnnnnnnnnnnnnnnnnnnnnnnnnnnn", 1 }, |
585 { "o", 1 }, | 584 { "o", 1 }, |
586 { "pp", 1 }, | 585 { "pp", 1 }, |
587 { "qqq", 1 }, | 586 { "qqq", 1 }, |
588 { "r", 1 }, | 587 { "r", 1 }, |
589 { "uar", 1 }, | 588 { "uar", 1 }, |
590 { "uary", 1 } | 589 { "uary", 1 } |
591 }; | 590 }; |
592 checkIterator(iter, data, LENGTHOF(data)); | 591 checkIterator(iter, data, UPRV_LENGTHOF(data)); |
593 // Reset, and we should get the same result. | 592 // Reset, and we should get the same result. |
594 logln("after iter.reset()"); | 593 logln("after iter.reset()"); |
595 checkIterator(iter.reset(), data, LENGTHOF(data)); | 594 checkIterator(iter.reset(), data, UPRV_LENGTHOF(data)); |
596 } | 595 } |
597 | 596 |
598 void UCharsTrieTest::TestIteratorFromLinearMatch() { | 597 void UCharsTrieTest::TestIteratorFromLinearMatch() { |
599 LocalPointer<UCharsTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_SMALL)); | 598 LocalPointer<UCharsTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_SMALL)); |
600 if(trie.isNull()) { | 599 if(trie.isNull()) { |
601 return; // buildTrie() reported an error | 600 return; // buildTrie() reported an error |
602 } | 601 } |
603 // Go into a linear-match node. | 602 // Go into a linear-match node. |
604 trie->next(u_j); | 603 trie->next(u_j); |
605 trie->next(u_a); | 604 trie->next(u_a); |
606 trie->next(u_n); | 605 trie->next(u_n); |
607 trie->next(u_u); | 606 trie->next(u_u); |
608 trie->next(u_a); | 607 trie->next(u_a); |
609 IcuTestErrorCode errorCode(*this, "TestIteratorFromLinearMatch()"); | 608 IcuTestErrorCode errorCode(*this, "TestIteratorFromLinearMatch()"); |
610 UCharsTrie::Iterator iter(*trie, 0, errorCode); | 609 UCharsTrie::Iterator iter(*trie, 0, errorCode); |
611 if(errorCode.logIfFailureAndReset("UCharsTrie::Iterator(trie) constructor"))
{ | 610 if(errorCode.logIfFailureAndReset("UCharsTrie::Iterator(trie) constructor"))
{ |
612 return; | 611 return; |
613 } | 612 } |
614 // Expected data: Same as in buildMonthsTrie(), except only the suffixes | 613 // Expected data: Same as in buildMonthsTrie(), except only the suffixes |
615 // following "janua". | 614 // following "janua". |
616 static const StringAndValue data[]={ | 615 static const StringAndValue data[]={ |
617 { "r", 1 }, | 616 { "r", 1 }, |
618 { "ry", 1 } | 617 { "ry", 1 } |
619 }; | 618 }; |
620 checkIterator(iter, data, LENGTHOF(data)); | 619 checkIterator(iter, data, UPRV_LENGTHOF(data)); |
621 // Reset, and we should get the same result. | 620 // Reset, and we should get the same result. |
622 logln("after iter.reset()"); | 621 logln("after iter.reset()"); |
623 checkIterator(iter.reset(), data, LENGTHOF(data)); | 622 checkIterator(iter.reset(), data, UPRV_LENGTHOF(data)); |
624 } | 623 } |
625 | 624 |
626 void UCharsTrieTest::TestTruncatingIteratorFromRoot() { | 625 void UCharsTrieTest::TestTruncatingIteratorFromRoot() { |
627 LocalPointer<UCharsTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_FAST)); | 626 LocalPointer<UCharsTrie> trie(buildMonthsTrie(USTRINGTRIE_BUILD_FAST)); |
628 if(trie.isNull()) { | 627 if(trie.isNull()) { |
629 return; // buildTrie() reported an error | 628 return; // buildTrie() reported an error |
630 } | 629 } |
631 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromRoot()"); | 630 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromRoot()"); |
632 UCharsTrie::Iterator iter(*trie, 4, errorCode); | 631 UCharsTrie::Iterator iter(*trie, 4, errorCode); |
633 if(errorCode.logIfFailureAndReset("UCharsTrie::Iterator(trie) constructor"))
{ | 632 if(errorCode.logIfFailureAndReset("UCharsTrie::Iterator(trie) constructor"))
{ |
(...skipping 22 matching lines...) Expand all Loading... |
656 { "jano", 1 }, | 655 { "jano", 1 }, |
657 { "janp", -1 }, | 656 { "janp", -1 }, |
658 { "janq", -1 }, | 657 { "janq", -1 }, |
659 { "janr", 1 }, | 658 { "janr", 1 }, |
660 { "janu", -1 }, | 659 { "janu", -1 }, |
661 { "july", 7 }, | 660 { "july", 7 }, |
662 { "jun", 6 }, | 661 { "jun", 6 }, |
663 { "jun.", 6 }, | 662 { "jun.", 6 }, |
664 { "june", 6 } | 663 { "june", 6 } |
665 }; | 664 }; |
666 checkIterator(iter, data, LENGTHOF(data)); | 665 checkIterator(iter, data, UPRV_LENGTHOF(data)); |
667 // Reset, and we should get the same result. | 666 // Reset, and we should get the same result. |
668 logln("after iter.reset()"); | 667 logln("after iter.reset()"); |
669 checkIterator(iter.reset(), data, LENGTHOF(data)); | 668 checkIterator(iter.reset(), data, UPRV_LENGTHOF(data)); |
670 } | 669 } |
671 | 670 |
672 void UCharsTrieTest::TestTruncatingIteratorFromLinearMatchShort() { | 671 void UCharsTrieTest::TestTruncatingIteratorFromLinearMatchShort() { |
673 static const StringAndValue data[]={ | 672 static const StringAndValue data[]={ |
674 { "abcdef", 10 }, | 673 { "abcdef", 10 }, |
675 { "abcdepq", 200 }, | 674 { "abcdepq", 200 }, |
676 { "abcdeyz", 3000 } | 675 { "abcdeyz", 3000 } |
677 }; | 676 }; |
678 LocalPointer<UCharsTrie> trie(buildTrie(data, LENGTHOF(data), USTRINGTRIE_BU
ILD_FAST)); | 677 LocalPointer<UCharsTrie> trie(buildTrie(data, UPRV_LENGTHOF(data), USTRINGTR
IE_BUILD_FAST)); |
679 if(trie.isNull()) { | 678 if(trie.isNull()) { |
680 return; // buildTrie() reported an error | 679 return; // buildTrie() reported an error |
681 } | 680 } |
682 // Go into a linear-match node. | 681 // Go into a linear-match node. |
683 trie->next(u_a); | 682 trie->next(u_a); |
684 trie->next(u_b); | 683 trie->next(u_b); |
685 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromLinearMatchShor
t()"); | 684 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromLinearMatchShor
t()"); |
686 // Truncate within the linear-match node. | 685 // Truncate within the linear-match node. |
687 UCharsTrie::Iterator iter(*trie, 2, errorCode); | 686 UCharsTrie::Iterator iter(*trie, 2, errorCode); |
688 if(errorCode.logIfFailureAndReset("UCharsTrie::Iterator(trie) constructor"))
{ | 687 if(errorCode.logIfFailureAndReset("UCharsTrie::Iterator(trie) constructor"))
{ |
689 return; | 688 return; |
690 } | 689 } |
691 static const StringAndValue expected[]={ | 690 static const StringAndValue expected[]={ |
692 { "cd", -1 } | 691 { "cd", -1 } |
693 }; | 692 }; |
694 checkIterator(iter, expected, LENGTHOF(expected)); | 693 checkIterator(iter, expected, UPRV_LENGTHOF(expected)); |
695 // Reset, and we should get the same result. | 694 // Reset, and we should get the same result. |
696 logln("after iter.reset()"); | 695 logln("after iter.reset()"); |
697 checkIterator(iter.reset(), expected, LENGTHOF(expected)); | 696 checkIterator(iter.reset(), expected, UPRV_LENGTHOF(expected)); |
698 } | 697 } |
699 | 698 |
700 void UCharsTrieTest::TestTruncatingIteratorFromLinearMatchLong() { | 699 void UCharsTrieTest::TestTruncatingIteratorFromLinearMatchLong() { |
701 static const StringAndValue data[]={ | 700 static const StringAndValue data[]={ |
702 { "abcdef", 10 }, | 701 { "abcdef", 10 }, |
703 { "abcdepq", 200 }, | 702 { "abcdepq", 200 }, |
704 { "abcdeyz", 3000 } | 703 { "abcdeyz", 3000 } |
705 }; | 704 }; |
706 LocalPointer<UCharsTrie> trie(buildTrie(data, LENGTHOF(data), USTRINGTRIE_BU
ILD_FAST)); | 705 LocalPointer<UCharsTrie> trie(buildTrie(data, UPRV_LENGTHOF(data), USTRINGTR
IE_BUILD_FAST)); |
707 if(trie.isNull()) { | 706 if(trie.isNull()) { |
708 return; // buildTrie() reported an error | 707 return; // buildTrie() reported an error |
709 } | 708 } |
710 // Go into a linear-match node. | 709 // Go into a linear-match node. |
711 trie->next(u_a); | 710 trie->next(u_a); |
712 trie->next(u_b); | 711 trie->next(u_b); |
713 trie->next(u_c); | 712 trie->next(u_c); |
714 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromLinearMatchLong
()"); | 713 IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromLinearMatchLong
()"); |
715 // Truncate after the linear-match node. | 714 // Truncate after the linear-match node. |
716 UCharsTrie::Iterator iter(*trie, 3, errorCode); | 715 UCharsTrie::Iterator iter(*trie, 3, errorCode); |
717 if(errorCode.logIfFailureAndReset("UCharsTrie::Iterator(trie) constructor"))
{ | 716 if(errorCode.logIfFailureAndReset("UCharsTrie::Iterator(trie) constructor"))
{ |
718 return; | 717 return; |
719 } | 718 } |
720 static const StringAndValue expected[]={ | 719 static const StringAndValue expected[]={ |
721 { "def", 10 }, | 720 { "def", 10 }, |
722 { "dep", -1 }, | 721 { "dep", -1 }, |
723 { "dey", -1 } | 722 { "dey", -1 } |
724 }; | 723 }; |
725 checkIterator(iter, expected, LENGTHOF(expected)); | 724 checkIterator(iter, expected, UPRV_LENGTHOF(expected)); |
726 // Reset, and we should get the same result. | 725 // Reset, and we should get the same result. |
727 logln("after iter.reset()"); | 726 logln("after iter.reset()"); |
728 checkIterator(iter.reset(), expected, LENGTHOF(expected)); | 727 checkIterator(iter.reset(), expected, UPRV_LENGTHOF(expected)); |
729 } | 728 } |
730 | 729 |
731 void UCharsTrieTest::TestIteratorFromUChars() { | 730 void UCharsTrieTest::TestIteratorFromUChars() { |
732 static const StringAndValue data[]={ | 731 static const StringAndValue data[]={ |
733 { "mm", 3 }, | 732 { "mm", 3 }, |
734 { "mmm", 33 }, | 733 { "mmm", 33 }, |
735 { "mmnop", 333 } | 734 { "mmnop", 333 } |
736 }; | 735 }; |
737 builder_->clear(); | 736 builder_->clear(); |
738 IcuTestErrorCode errorCode(*this, "TestIteratorFromUChars()"); | 737 IcuTestErrorCode errorCode(*this, "TestIteratorFromUChars()"); |
739 for(int32_t i=0; i<LENGTHOF(data); ++i) { | 738 for(int32_t i=0; i<UPRV_LENGTHOF(data); ++i) { |
740 builder_->add(data[i].s, data[i].value, errorCode); | 739 builder_->add(data[i].s, data[i].value, errorCode); |
741 } | 740 } |
742 UnicodeString trieUChars; | 741 UnicodeString trieUChars; |
743 builder_->buildUnicodeString(USTRINGTRIE_BUILD_FAST, trieUChars, errorCode); | 742 builder_->buildUnicodeString(USTRINGTRIE_BUILD_FAST, trieUChars, errorCode); |
744 UCharsTrie::Iterator iter(trieUChars.getBuffer(), 0, errorCode); | 743 UCharsTrie::Iterator iter(trieUChars.getBuffer(), 0, errorCode); |
745 checkIterator(iter, data, LENGTHOF(data)); | 744 checkIterator(iter, data, UPRV_LENGTHOF(data)); |
746 } | 745 } |
747 | 746 |
748 void UCharsTrieTest::checkData(const StringAndValue data[], int32_t dataLength)
{ | 747 void UCharsTrieTest::checkData(const StringAndValue data[], int32_t dataLength)
{ |
749 logln("checkData(dataLength=%d, fast)", (int)dataLength); | 748 logln("checkData(dataLength=%d, fast)", (int)dataLength); |
750 checkData(data, dataLength, USTRINGTRIE_BUILD_FAST); | 749 checkData(data, dataLength, USTRINGTRIE_BUILD_FAST); |
751 logln("checkData(dataLength=%d, small)", (int)dataLength); | 750 logln("checkData(dataLength=%d, small)", (int)dataLength); |
752 checkData(data, dataLength, USTRINGTRIE_BUILD_SMALL); | 751 checkData(data, dataLength, USTRINGTRIE_BUILD_SMALL); |
753 } | 752 } |
754 | 753 |
755 void UCharsTrieTest::checkData(const StringAndValue data[], int32_t dataLength,
UStringTrieBuildOption buildOption) { | 754 void UCharsTrieTest::checkData(const StringAndValue data[], int32_t dataLength,
UStringTrieBuildOption buildOption) { |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1028 break; | 1027 break; |
1029 } | 1028 } |
1030 if(!hasNext) { | 1029 if(!hasNext) { |
1031 errln("trie iterator next()=FALSE for item %d: %s", (int)i, data[i].
s); | 1030 errln("trie iterator next()=FALSE for item %d: %s", (int)i, data[i].
s); |
1032 break; | 1031 break; |
1033 } | 1032 } |
1034 UnicodeString expectedString=UnicodeString(data[i].s, -1, US_INV).unesca
pe(); | 1033 UnicodeString expectedString=UnicodeString(data[i].s, -1, US_INV).unesca
pe(); |
1035 if(iter.getString()!=expectedString) { | 1034 if(iter.getString()!=expectedString) { |
1036 char buffer[1000]; | 1035 char buffer[1000]; |
1037 UnicodeString invString(prettify(iter.getString())); | 1036 UnicodeString invString(prettify(iter.getString())); |
1038 invString.extract(0, invString.length(), buffer, LENGTHOF(buffer), U
S_INV); | 1037 invString.extract(0, invString.length(), buffer, UPRV_LENGTHOF(buffe
r), US_INV); |
1039 errln("trie iterator next().getString()=%s but expected %s for item
%d", | 1038 errln("trie iterator next().getString()=%s but expected %s for item
%d", |
1040 buffer, data[i].s, (int)i); | 1039 buffer, data[i].s, (int)i); |
1041 } | 1040 } |
1042 if(iter.getValue()!=data[i].value) { | 1041 if(iter.getValue()!=data[i].value) { |
1043 errln("trie iterator next().getValue()=%ld=0x%lx but expected %ld=0x
%lx for item %d: %s", | 1042 errln("trie iterator next().getValue()=%ld=0x%lx but expected %ld=0x
%lx for item %d: %s", |
1044 (long)iter.getValue(), (long)iter.getValue(), | 1043 (long)iter.getValue(), (long)iter.getValue(), |
1045 (long)data[i].value, (long)data[i].value, | 1044 (long)data[i].value, (long)data[i].value, |
1046 (int)i, data[i].s); | 1045 (int)i, data[i].s); |
1047 } | 1046 } |
1048 } | 1047 } |
1049 if(iter.hasNext()) { | 1048 if(iter.hasNext()) { |
1050 errln("trie iterator hasNext()=TRUE after all items"); | 1049 errln("trie iterator hasNext()=TRUE after all items"); |
1051 } | 1050 } |
1052 UBool hasNext=iter.next(errorCode); | 1051 UBool hasNext=iter.next(errorCode); |
1053 errorCode.logIfFailureAndReset("trie iterator next() after all items"); | 1052 errorCode.logIfFailureAndReset("trie iterator next() after all items"); |
1054 if(hasNext) { | 1053 if(hasNext) { |
1055 errln("trie iterator next()=TRUE after all items"); | 1054 errln("trie iterator next()=TRUE after all items"); |
1056 } | 1055 } |
1057 } | 1056 } |
OLD | NEW |