Trie2Test.java revision 2e13a2bdade5cd0a635f0bd89805931a6fd710da
1/* GENERATED SOURCE. DO NOT MODIFY. */ 2// © 2016 and later: Unicode, Inc. and others. 3// License & terms of use: http://www.unicode.org/copyright.html#License 4/* 5 ******************************************************************************* 6 * Copyright (C) 2009-2015, International Business Machines Corporation and 7 * others. All Rights Reserved. 8 ******************************************************************************* 9 */ 10 11package android.icu.dev.test.util; 12 13import java.io.ByteArrayInputStream; 14import java.io.ByteArrayOutputStream; 15import java.io.IOException; 16import java.io.InputStream; 17import java.nio.ByteBuffer; 18import java.util.Iterator; 19 20import org.junit.Test; 21 22import android.icu.dev.test.TestFmwk; 23import android.icu.impl.ICUBinary; 24import android.icu.impl.Trie2; 25import android.icu.impl.Trie2Writable; 26import android.icu.impl.Trie2_16; 27import android.icu.impl.Trie2_32; 28import android.icu.testsharding.MainTestShard; 29 30@MainTestShard 31public class Trie2Test extends TestFmwk { 32 /** 33 * Constructor 34 */ 35 public Trie2Test() 36 { 37 } 38 39 // public methods ----------------------------------------------- 40 41 // 42 // TestAPI. Check that all API methods can be called, and do at least some minimal 43 // operation correctly. This is not a full test of correct behavior. 44 // 45 @Test 46 public void TestTrie2API() { 47 // Trie2.createFromSerialized() 48 // This function is well exercised by TestRanges(). 49 50 // Trie2.getVersion(InputStream is, boolean anyEndianOk) 51 // 52 53 try { 54 Trie2Writable trie = new Trie2Writable(0,0); 55 ByteArrayOutputStream os = new ByteArrayOutputStream(); 56 trie.toTrie2_16().serialize(os); 57 ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); 58 assertEquals(null, 2, Trie2.getVersion(is, true)); 59 } catch (IOException e) { 60 errln(where() + e.toString()); 61 } 62 63 // Equals & hashCode 64 // 65 { 66 Trie2Writable trieWA = new Trie2Writable(0,0); 67 Trie2Writable trieWB = new Trie2Writable(0,0); 68 Trie2 trieA = trieWA; 69 Trie2 trieB = trieWB; 70 assertTrue("", trieA.equals(trieB)); 71 assertEquals("", trieA, trieB); 72 assertEquals("", trieA.hashCode(), trieB.hashCode()); 73 trieWA.set(500, 2); 74 assertNotEquals("", trieA, trieB); 75 // Note that the hash codes do not strictly need to be different, 76 // but it's highly likely that something is wrong if they are the same. 77 assertNotEquals("", trieA.hashCode(), trieB.hashCode()); 78 trieWB.set(500, 2); 79 trieA = trieWA.toTrie2_16(); 80 assertEquals("", trieA, trieB); 81 assertEquals("", trieA.hashCode(), trieB.hashCode()); 82 } 83 84 // 85 // Iterator creation 86 // 87 { 88 Trie2Writable trie = new Trie2Writable(17,0); 89 Iterator<Trie2.Range> it; 90 it = trie.iterator(); 91 92 Trie2.Range r = it.next(); 93 assertEquals("", 0, r.startCodePoint); 94 assertEquals("", 0x10ffff, r.endCodePoint); 95 assertEquals("", 17, r.value); 96 assertEquals("", false, r.leadSurrogate); 97 98 r = it.next(); 99 assertEquals("", 0xd800, r.startCodePoint); 100 assertEquals("", 0xdbff, r.endCodePoint); 101 assertEquals("", 17, r.value); 102 assertEquals("", true, r.leadSurrogate); 103 104 105 int i = 0; 106 for (Trie2.Range rr: trie) { 107 switch (i) { 108 case 0: 109 assertEquals("", 0, rr.startCodePoint); 110 assertEquals("", 0x10ffff, rr.endCodePoint); 111 assertEquals("", 17, rr.value); 112 assertEquals("", false, rr.leadSurrogate); 113 break; 114 case 1: 115 assertEquals("", 0xd800, rr.startCodePoint); 116 assertEquals("", 0xdbff, rr.endCodePoint); 117 assertEquals("", 17, rr.value); 118 assertEquals("", true, rr.leadSurrogate); 119 break; 120 default: 121 errln(where() + " Unexpected iteration result"); 122 } 123 i++; 124 } 125 } 126 127 // Iteration with a value mapping function 128 // 129 { 130 Trie2Writable trie = new Trie2Writable(0xbadfeed, 0); 131 trie.set(0x10123, 42); 132 133 Trie2.ValueMapper vm = new Trie2.ValueMapper() { 134 public int map(int v) { 135 if (v == 0xbadfeed) { 136 v = 42; 137 } 138 return v; 139 } 140 }; 141 Iterator<Trie2.Range> it = trie.iterator(vm); 142 Trie2.Range r = it.next(); 143 assertEquals("", 0, r.startCodePoint); 144 assertEquals("", 0x10ffff, r.endCodePoint); 145 assertEquals("", 42, r.value); 146 assertEquals("", false, r.leadSurrogate); 147 } 148 149 150 // Iteration over a leading surrogate range. 151 // 152 { 153 Trie2Writable trie = new Trie2Writable(0xdefa17, 0); 154 trie.set(0x2f810, 10); 155 Iterator<Trie2.Range> it = trie.iteratorForLeadSurrogate((char)0xd87e); 156 Trie2.Range r = it.next(); 157 assertEquals("", 0x2f800, r.startCodePoint); 158 assertEquals("", 0x2f80f, r.endCodePoint); 159 assertEquals("", 0xdefa17, r.value); 160 assertEquals("", false, r.leadSurrogate); 161 162 r = it.next(); 163 assertEquals("", 0x2f810, r.startCodePoint); 164 assertEquals("", 0x2f810, r.endCodePoint); 165 assertEquals("", 10, r.value); 166 assertEquals("", false, r.leadSurrogate); 167 168 r = it.next(); 169 assertEquals("", 0x2f811, r.startCodePoint); 170 assertEquals("", 0x2fbff, r.endCodePoint); 171 assertEquals("", 0xdefa17, r.value); 172 assertEquals("", false, r.leadSurrogate); 173 174 assertFalse("", it.hasNext()); 175 } 176 177 // Iteration over a leading surrogate range with a ValueMapper. 178 // 179 { 180 Trie2Writable trie = new Trie2Writable(0xdefa17, 0); 181 trie.set(0x2f810, 10); 182 Trie2.ValueMapper m = new Trie2.ValueMapper() { 183 public int map(int in) { 184 if (in==10) { 185 in = 0xdefa17; 186 } 187 return in; 188 } 189 }; 190 Iterator<Trie2.Range> it = trie.iteratorForLeadSurrogate((char)0xd87e, m); 191 Trie2.Range r = it.next(); 192 assertEquals("", 0x2f800, r.startCodePoint); 193 assertEquals("", 0x2fbff, r.endCodePoint); 194 assertEquals("", 0xdefa17, r.value); 195 assertEquals("", false, r.leadSurrogate); 196 197 assertFalse("", it.hasNext()); 198 } 199 200 // Trie2.serialize() 201 // Test the implementation in Trie2, which is used with Read Only Tries. 202 // 203 { 204 Trie2Writable trie = new Trie2Writable(101, 0); 205 trie.setRange(0xf000, 0x3c000, 200, true); 206 trie.set(0xffee, 300); 207 Trie2_16 frozen16 = trie.toTrie2_16(); 208 Trie2_32 frozen32 = trie.toTrie2_32(); 209 assertEquals("", trie, frozen16); 210 assertEquals("", trie, frozen32); 211 assertEquals("", frozen16, frozen32); 212 ByteArrayOutputStream os = new ByteArrayOutputStream(); 213 try { 214 frozen16.serialize(os); 215 Trie2 unserialized16 = Trie2.createFromSerialized(ByteBuffer.wrap(os.toByteArray())); 216 assertEquals("", trie, unserialized16); 217 assertEquals("", Trie2_16.class, unserialized16.getClass()); 218 219 os.reset(); 220 frozen32.serialize(os); 221 Trie2 unserialized32 = Trie2.createFromSerialized(ByteBuffer.wrap(os.toByteArray())); 222 assertEquals("", trie, unserialized32); 223 assertEquals("", Trie2_32.class, unserialized32.getClass()); 224 } catch (IOException e) { 225 errln(where() + " Unexpected exception: " + e); 226 } 227 228 229 } 230 } 231 232 233 @Test 234 public void TestTrie2WritableAPI() { 235 // 236 // Trie2Writable methods. Check that all functions are present and 237 // nominally working. Not an in-depth test. 238 // 239 240 // Trie2Writable constructor 241 Trie2 t1 = new Trie2Writable(6, 666); 242 243 // Constructor from another Trie2 244 Trie2 t2 = new Trie2Writable(t1); 245 assertTrue("", t1.equals(t2)); 246 247 // Set / Get 248 Trie2Writable t1w = new Trie2Writable(10, 666); 249 t1w.set(0x4567, 99); 250 assertEquals("", 10, t1w.get(0x4566)); 251 assertEquals("", 99, t1w.get(0x4567)); 252 assertEquals("", 666, t1w.get(-1)); 253 assertEquals("", 666, t1w.get(0x110000)); 254 255 256 // SetRange 257 t1w = new Trie2Writable(10, 666); 258 t1w.setRange(13 /*start*/, 6666 /*end*/, 7788 /*value*/, false /*overwrite */); 259 t1w.setRange(6000, 7000, 9900, true); 260 assertEquals("", 10, t1w.get(12)); 261 assertEquals("", 7788, t1w.get(13)); 262 assertEquals("", 7788, t1w.get(5999)); 263 assertEquals("", 9900, t1w.get(6000)); 264 assertEquals("", 9900, t1w.get(7000)); 265 assertEquals("", 10, t1w.get(7001)); 266 assertEquals("", 666, t1w.get(0x110000)); 267 268 // setRange from a Trie2.Range 269 // (Ranges are more commonly created by iterating over a Trie2, 270 // but create one by hand here) 271 Trie2.Range r = new Trie2.Range(); 272 r.startCodePoint = 50; 273 r.endCodePoint = 52; 274 r.value = 0x12345678; 275 r.leadSurrogate = false; 276 t1w = new Trie2Writable(0, 0xbad); 277 t1w.setRange(r, true); 278 assertEquals(null, 0, t1w.get(49)); 279 assertEquals("", 0x12345678, t1w.get(50)); 280 assertEquals("", 0x12345678, t1w.get(52)); 281 assertEquals("", 0, t1w.get(53)); 282 283 284 // setForLeadSurrogateCodeUnit / getFromU16SingleLead 285 t1w = new Trie2Writable(10, 0xbad); 286 assertEquals("", 10, t1w.getFromU16SingleLead((char)0x0d801)); 287 t1w.setForLeadSurrogateCodeUnit((char)0xd801, 5000); 288 t1w.set(0xd801, 6000); 289 assertEquals("", 5000, t1w.getFromU16SingleLead((char)0x0d801)); 290 assertEquals("", 6000, t1w.get(0x0d801)); 291 292 // get(). Is covered by nearly every other test. 293 294 295 // Trie2_16 getAsFrozen_16() 296 t1w = new Trie2Writable(10, 666); 297 t1w.set(42, 5555); 298 t1w.set(0x1ff00, 224); 299 Trie2_16 t1_16 = t1w.toTrie2_16(); 300 assertTrue("", t1w.equals(t1_16)); 301 // alter the writable Trie2 and then re-freeze. 302 t1w.set(152, 129); 303 t1_16 = t1w.toTrie2_16(); 304 assertTrue("", t1w.equals(t1_16)); 305 assertEquals("", 129, t1w.get(152)); 306 307 // Trie2_32 getAsFrozen_32() 308 // 309 t1w = new Trie2Writable(10, 666); 310 t1w.set(42, 5555); 311 t1w.set(0x1ff00, 224); 312 Trie2_32 t1_32 = t1w.toTrie2_32(); 313 assertTrue("", t1w.equals(t1_32)); 314 // alter the writable Trie2 and then re-freeze. 315 t1w.set(152, 129); 316 assertNotEquals("", t1_32, t1w); 317 t1_32 = t1w.toTrie2_32(); 318 assertTrue("", t1w.equals(t1_32)); 319 assertEquals("", 129, t1w.get(152)); 320 321 322 // serialize(OutputStream os, ValueWidth width) 323 // 324 ByteArrayOutputStream os = new ByteArrayOutputStream(); 325 t1w = new Trie2Writable(0, 0xbad); 326 t1w.set(0x41, 0x100); 327 t1w.set(0xc2, 0x200); 328 t1w.set(0x404, 0x300); 329 t1w.set(0xd903, 0x500); 330 t1w.set(0xdd29, 0x600); 331 t1w.set(0x1055d3, 0x700); 332 t1w.setForLeadSurrogateCodeUnit((char)0xda1a, 0x800); 333 try { 334 // Serialize to 16 bits. 335 int serializedLen = t1w.toTrie2_16().serialize(os); 336 // Fragile test. Serialized length could change with changes to compaction. 337 // But it should not change unexpectedly. 338 assertEquals("", 3508, serializedLen); 339 Trie2 t1ws16 = Trie2.createFromSerialized(ByteBuffer.wrap(os.toByteArray())); 340 assertEquals("", t1ws16.getClass(), Trie2_16.class); 341 assertEquals("", t1w, t1ws16); 342 343 // Serialize to 32 bits 344 os.reset(); 345 serializedLen = t1w.toTrie2_32().serialize(os); 346 // Fragile test. Serialized length could change with changes to compaction. 347 // But it should not change unexpectedly. 348 assertEquals("", 4332, serializedLen); 349 Trie2 t1ws32 = Trie2.createFromSerialized(ByteBuffer.wrap(os.toByteArray())); 350 assertEquals("", t1ws32.getClass(), Trie2_32.class); 351 assertEquals("", t1w, t1ws32); 352 } catch (IOException e) { 353 errln(where() + e.toString()); 354 } 355 356 357 } 358 359 @Test 360 public void TestCharSequenceIterator() { 361 String text = "abc123\ud800\udc01 "; // Includes a Unicode supplemental character 362 String vals = "LLLNNNX?S"; 363 364 Trie2Writable tw = new Trie2Writable(0, 666); 365 tw.setRange('a', 'z', 'L', false); 366 tw.setRange('1', '9', 'N', false); 367 tw.set(' ', 'S'); 368 tw.set(0x10001, 'X'); 369 370 Trie2.CharSequenceIterator it = tw.charSequenceIterator(text, 0); 371 372 // Check forwards iteration. 373 Trie2.CharSequenceValues ir; 374 int i; 375 for (i=0; it.hasNext(); i++) { 376 ir = it.next(); 377 int expectedCP = Character.codePointAt(text, i); 378 assertEquals("" + " i="+i, expectedCP, ir.codePoint); 379 assertEquals("" + " i="+i, i, ir.index); 380 assertEquals("" + " i="+i, vals.charAt(i), ir.value); 381 if (expectedCP >= 0x10000) { 382 i++; 383 } 384 } 385 assertEquals("", text.length(), i); 386 387 // Check reverse iteration, starting at an intermediate point. 388 it.set(5); 389 for (i=5; it.hasPrevious(); ) { 390 ir = it.previous(); 391 int expectedCP = Character.codePointBefore(text, i); 392 i -= (expectedCP < 0x10000? 1 : 2); 393 assertEquals("" + " i="+i, expectedCP, ir.codePoint); 394 assertEquals("" + " i="+i, i, ir.index); 395 assertEquals("" + " i="+i, vals.charAt(i), ir.value); 396 } 397 assertEquals("", 0, i); 398 399 } 400 401 402 // 403 // Port of Tests from ICU4C ... 404 // 405 // setRanges array elements are 406 // {start Code point, limit CP, value, overwrite} 407 // 408 // There must be an entry with limit 0 and with the intialValue. 409 // It may be preceded by an entry with negative limit and the errorValue. 410 // 411 // checkRanges array elemets are 412 // { limit code point, value} 413 // 414 // The expected value range is from the previous boundary's limit to before 415 // this boundary's limit 416 417 // 418 String[] trieNames = {"setRanges1", "setRanges2", "setRanges3", "setRangesEmpty", "setRangesSingleValue"}; 419 /* set consecutive ranges, even with value 0 */ 420 421 422 423 private static int[][] setRanges1 ={ 424 { 0, 0, 0, 0 }, 425 { 0, 0x40, 0, 0 }, 426 { 0x40, 0xe7, 0x1234, 0 }, 427 { 0xe7, 0x3400, 0, 0 }, 428 { 0x3400, 0x9fa6, 0x6162, 0 }, 429 { 0x9fa6, 0xda9e, 0x3132, 0 }, 430 { 0xdada, 0xeeee, 0x87ff, 0 }, 431 { 0xeeee, 0x11111, 1, 0 }, 432 { 0x11111, 0x44444, 0x6162, 0 }, 433 { 0x44444, 0x60003, 0, 0 }, 434 { 0xf0003, 0xf0004, 0xf, 0 }, 435 { 0xf0004, 0xf0006, 0x10, 0 }, 436 { 0xf0006, 0xf0007, 0x11, 0 }, 437 { 0xf0007, 0xf0040, 0x12, 0 }, 438 { 0xf0040, 0x110000, 0, 0 } 439 }; 440 441 private static int[][] checkRanges1 = { 442 { 0, 0 }, 443 { 0x40, 0 }, 444 { 0xe7, 0x1234 }, 445 { 0x3400, 0 }, 446 { 0x9fa6, 0x6162 }, 447 { 0xda9e, 0x3132 }, 448 { 0xdada, 0 }, 449 { 0xeeee, 0x87ff }, 450 { 0x11111, 1 }, 451 { 0x44444, 0x6162 }, 452 { 0xf0003, 0 }, 453 { 0xf0004, 0xf }, 454 { 0xf0006, 0x10 }, 455 { 0xf0007, 0x11 }, 456 { 0xf0040, 0x12 }, 457 { 0x110000, 0 } 458 }; 459 460 /* set some interesting overlapping ranges */ 461 private static int [][] setRanges2={ 462 { 0, 0, 0, 0 }, 463 { 0x21, 0x7f, 0x5555, 1 }, 464 { 0x2f800, 0x2fedc, 0x7a, 1 }, 465 { 0x72, 0xdd, 3, 1 }, 466 { 0xdd, 0xde, 4, 0 }, 467 { 0x201, 0x240, 6, 1 }, /* 3 consecutive blocks with the same pattern but */ 468 { 0x241, 0x280, 6, 1 }, /* discontiguous value ranges, testing utrie2_enum() */ 469 { 0x281, 0x2c0, 6, 1 }, 470 { 0x2f987, 0x2fa98, 5, 1 }, 471 { 0x2f777, 0x2f883, 0, 1 }, 472 { 0x2f900, 0x2ffaa, 1, 0 }, 473 { 0x2ffaa, 0x2ffab, 2, 1 }, 474 { 0x2ffbb, 0x2ffc0, 7, 1 } 475 }; 476 477 private static int[] [] checkRanges2={ 478 { 0, 0 }, 479 { 0x21, 0 }, 480 { 0x72, 0x5555 }, 481 { 0xdd, 3 }, 482 { 0xde, 4 }, 483 { 0x201, 0 }, 484 { 0x240, 6 }, 485 { 0x241, 0 }, 486 { 0x280, 6 }, 487 { 0x281, 0 }, 488 { 0x2c0, 6 }, 489 { 0x2f883, 0 }, 490 { 0x2f987, 0x7a }, 491 { 0x2fa98, 5 }, 492 { 0x2fedc, 0x7a }, 493 { 0x2ffaa, 1 }, 494 { 0x2ffab, 2 }, 495 { 0x2ffbb, 0 }, 496 { 0x2ffc0, 7 }, 497 { 0x110000, 0 } 498 }; 499 500/* 501 private static int[] [] checkRanges2_d800={ 502 { 0x10000, 0 }, 503 { 0x10400, 0 } 504 }; 505 506 private static int[][] checkRanges2_d87e={ 507 { 0x2f800, 6 }, 508 { 0x2f883, 0 }, 509 { 0x2f987, 0x7a }, 510 { 0x2fa98, 5 }, 511 { 0x2fc00, 0x7a } 512 }; 513 514 private static int[][] checkRanges2_d87f={ 515 { 0x2fc00, 0 }, 516 { 0x2fedc, 0x7a }, 517 { 0x2ffaa, 1 }, 518 { 0x2ffab, 2 }, 519 { 0x2ffbb, 0 }, 520 { 0x2ffc0, 7 }, 521 { 0x30000, 0 } 522 }; 523 524 private static int[][] checkRanges2_dbff={ 525 { 0x10fc00, 0 }, 526 { 0x110000, 0 } 527 }; 528*/ 529 530 /* use a non-zero initial value */ 531 private static int[][] setRanges3={ 532 { 0, 0, 9, 0 }, // non-zero initial value. 533 { 0x31, 0xa4, 1, 0 }, 534 { 0x3400, 0x6789, 2, 0 }, 535 { 0x8000, 0x89ab, 9, 1 }, 536 { 0x9000, 0xa000, 4, 1 }, 537 { 0xabcd, 0xbcde, 3, 1 }, 538 { 0x55555, 0x110000, 6, 1 }, /* highStart<U+ffff with non-initialValue */ 539 { 0xcccc, 0x55555, 6, 1 } 540 }; 541 542 private static int[][] checkRanges3={ 543 { 0, 9 }, /* non-zero initialValue */ 544 { 0x31, 9 }, 545 { 0xa4, 1 }, 546 { 0x3400, 9 }, 547 { 0x6789, 2 }, 548 { 0x9000, 9 }, 549 { 0xa000, 4 }, 550 { 0xabcd, 9 }, 551 { 0xbcde, 3 }, 552 { 0xcccc, 9 }, 553 { 0x110000, 6 } 554 }; 555 556 /* empty or single-value tries, testing highStart==0 */ 557 private static int[][] setRangesEmpty={ 558 { 0, 0, 3, 0 } // Only the element with the initial value. 559 }; 560 561 private static int[][] checkRangesEmpty={ 562 { 0, 3 }, 563 { 0x110000, 3 } 564 }; 565 566 private static int[][] setRangesSingleValue={ 567 { 0, 0, 3, 0 }, // Initial value = 3 568 { 0, 0x110000, 5, 1 }, 569 }; 570 571 private static int[][] checkRangesSingleValue={ 572 { 0, 3 }, 573 { 0x110000, 5 } 574 }; 575 576 577 // 578 // Create a test Trie2 from a setRanges test array. 579 // Range data ported from C. 580 // 581 private Trie2Writable genTrieFromSetRanges(int [][] ranges) { 582 int i = 0; 583 int initialValue = 0; 584 int errorValue = 0x0bad; 585 586 if (ranges[i][1] < 0) { 587 errorValue = ranges[i][2]; 588 i++; 589 } 590 initialValue = ranges[i++][2]; 591 Trie2Writable trie = new Trie2Writable(initialValue, errorValue); 592 593 for (; i<ranges.length; i++) { 594 int rangeStart = ranges[i][0]; 595 int rangeEnd = ranges[i][1]-1; 596 int value = ranges[i][2]; 597 boolean overwrite = (ranges[i][3] != 0); 598 trie.setRange(rangeStart, rangeEnd, value, overwrite); 599 } 600 601 // Insert some non-default values for lead surrogates. 602 // TODO: this should be represented in the data. 603 trie.setForLeadSurrogateCodeUnit((char)0xd800, 90); 604 trie.setForLeadSurrogateCodeUnit((char)0xd999, 94); 605 trie.setForLeadSurrogateCodeUnit((char)0xdbff, 99); 606 607 return trie; 608 } 609 610 611 // 612 // Check the expected values from a single Trie2. 613 // 614 private void trieGettersTest(String testName, 615 Trie2 trie, // The Trie2 to test. 616 int[][] checkRanges) // Expected data. 617 // Tuples of (value, high limit code point) 618 // High limit is first code point following the range 619 // with the indicated value. 620 // (Structures copied from ICU4C tests.) 621 { 622 int countCheckRanges = checkRanges.length; 623 624 int initialValue, errorValue; 625 int value, value2; 626 int start, limit; 627 int i, countSpecials; 628 629 countSpecials=0; /*getSpecialValues(checkRanges, countCheckRanges, &initialValue, &errorValue);*/ 630 errorValue = 0x0bad; 631 initialValue = 0; 632 if (checkRanges[countSpecials][0] == 0) { 633 initialValue = checkRanges[countSpecials][1]; 634 countSpecials++; 635 } 636 637 start=0; 638 for(i=countSpecials; i<countCheckRanges; ++i) { 639 limit=checkRanges[i][0]; 640 value=checkRanges[i][1]; 641 642 while(start<limit) { 643 value2=trie.get(start); 644 if (value != value2) { 645 // The redundant if, outside of the assert, is for speed. 646 // It makes a significant difference for this test. 647 assertEquals("wrong value for " + testName + " of " + Integer.toHexString(start), value, value2); 648 } 649 ++start; 650 } 651 } 652 653 654 if(!testName.startsWith("dummy") && !testName.startsWith("trie1")) { 655 /* Test values for lead surrogate code units. 656 * For non-lead-surrogate code units, getFromU16SingleLead() and get() 657 * should be the same. 658 */ 659 for(start=0xd7ff; start<0xdc01; ++start) { 660 switch(start) { 661 case 0xd7ff: 662 case 0xdc00: 663 value=trie.get(start); 664 break; 665 case 0xd800: 666 value=90; 667 break; 668 case 0xd999: 669 value=94; 670 break; 671 case 0xdbff: 672 value=99; 673 break; 674 default: 675 value=initialValue; 676 break; 677 } 678 value2 = trie.getFromU16SingleLead((char)start); 679 if(value2!=value) { 680 errln(where() + " testName: " + testName + " getFromU16SingleLead() failed." + 681 "char, exected, actual = " + Integer.toHexString(start) + ", " + 682 Integer.toHexString(value) + ", " + Integer.toHexString(value2)); 683 } 684 } 685 } 686 687 /* test errorValue */ 688 value=trie.get(-1); 689 value2=trie.get(0x110000); 690 if(value!=errorValue || value2!=errorValue) { 691 errln("trie2.get() error value test. Expected, actual1, actual2 = " + 692 errorValue + ", " + value + ", " + value2); 693 } 694 695 // Check that Trie enumeration produces the same contents as simple get() 696 for (Trie2.Range range: trie) { 697 for (int cp=range.startCodePoint; cp<=range.endCodePoint; cp++) { 698 if (range.leadSurrogate) { 699 assertTrue(testName, cp>=(char)0xd800 && cp<(char)0xdc00); 700 assertEquals(testName, range.value, trie.getFromU16SingleLead((char)cp)); 701 } else { 702 assertEquals(testName, range.value, trie.get(cp)); 703 } 704 } 705 } 706 } 707 708 // Was testTrieRanges in ICU4C. Renamed to not conflict with ICU4J test framework. 709 private void checkTrieRanges(String testName, String serializedName, boolean withClone, 710 int[][] setRanges, int [][] checkRanges) throws IOException { 711 712 // Run tests against Tries that were built by ICU4C and serialized. 713 String fileName16 = "Trie2Test." + serializedName + ".16.tri2"; 714 String fileName32 = "Trie2Test." + serializedName + ".32.tri2"; 715 716 InputStream is = Trie2Test.class.getResourceAsStream(fileName16); 717 Trie2 trie16; 718 try { 719 trie16 = Trie2.createFromSerialized(ICUBinary.getByteBufferFromInputStreamAndCloseStream(is)); 720 } finally { 721 is.close(); 722 } 723 trieGettersTest(testName, trie16, checkRanges); 724 725 is = Trie2Test.class.getResourceAsStream(fileName32); 726 Trie2 trie32; 727 try { 728 trie32 = Trie2.createFromSerialized(ICUBinary.getByteBufferFromInputStreamAndCloseStream(is)); 729 } finally { 730 is.close(); 731 } 732 trieGettersTest(testName, trie32, checkRanges); 733 734 // Run the same tests against locally contructed Tries. 735 Trie2Writable trieW = genTrieFromSetRanges(setRanges); 736 trieGettersTest(testName, trieW, checkRanges); 737 assertEquals("", trieW, trie16); // Locally built tries must be 738 assertEquals("", trieW, trie32); // the same as those imported from ICU4C 739 740 741 Trie2_32 trie32a = trieW.toTrie2_32(); 742 trieGettersTest(testName, trie32a, checkRanges); 743 744 Trie2_16 trie16a = trieW.toTrie2_16(); 745 trieGettersTest(testName, trie16a, checkRanges); 746 747 } 748 749 // Was "TrieTest" in trie2test.c 750 @Test 751 public void TestRanges() throws IOException { 752 checkTrieRanges("set1", "setRanges1", false, setRanges1, checkRanges1); 753 checkTrieRanges("set2-overlap", "setRanges2", false, setRanges2, checkRanges2); 754 checkTrieRanges("set3-initial-9", "setRanges3", false, setRanges3, checkRanges3); 755 checkTrieRanges("set-empty", "setRangesEmpty", false, setRangesEmpty, checkRangesEmpty); 756 checkTrieRanges("set-single-value", "setRangesSingleValue", false, setRangesSingleValue, 757 checkRangesSingleValue); 758 checkTrieRanges("set2-overlap.withClone", "setRanges2", true, setRanges2, checkRanges2); 759 } 760 761 762 private String where() { 763 StackTraceElement[] st = new Throwable().getStackTrace(); 764 String w = "File: " + st[1].getFileName() + ", Line " + st[1].getLineNumber(); 765 return w; 766 } 767} 768