2 *******************************************************************************
3 * Copyright (C) 2009, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.util;
9 import java.io.ByteArrayInputStream;
10 import java.io.ByteArrayOutputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.util.Iterator;
15 import com.ibm.icu.dev.test.TestFmwk;
16 import com.ibm.icu.impl.Trie2;
17 import com.ibm.icu.impl.Trie2Writable;
18 import com.ibm.icu.impl.Trie2_16;
19 import com.ibm.icu.impl.Trie2_32;
21 public class Trie2Test extends TestFmwk {
29 // public methods -----------------------------------------------
31 public static void main(String arg[])
33 Trie2Test test = new Trie2Test();
36 } catch (Exception e) {
37 test.errln("Error testing trietest");
43 // TestAPI. Check that all API methods can be called, and do at least some minimal
44 // operation correctly. This is not a full test of correct behavior.
46 public void TestTrie2API() {
47 // Trie2.createFromSerialized()
48 // This function is well exercised by TestRanges().
50 // Trie2.getVersion(InputStream is, boolean anyEndianOk)
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());
66 Trie2Writable trieWA = new Trie2Writable(0,0);
67 Trie2Writable trieWB = new Trie2Writable(0,0);
70 assertTrue("", trieA.equals(trieB));
71 assertEquals("", trieA, trieB);
72 assertEquals("", trieA.hashCode(), trieB.hashCode());
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());
79 trieA = trieWA.toTrie2_16();
80 assertEquals("", trieA, trieB);
81 assertEquals("", trieA.hashCode(), trieB.hashCode());
88 Trie2Writable trie = new Trie2Writable(17,0);
89 Iterator<Trie2.Range> it;
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);
99 assertEquals("", 0xd800, r.startCodePoint);
100 assertEquals("", 0xdbff, r.endCodePoint);
101 assertEquals("", 17, r.value);
102 assertEquals("", true, r.leadSurrogate);
106 for (Trie2.Range rr: trie) {
109 assertEquals("", 0, rr.startCodePoint);
110 assertEquals("", 0x10ffff, rr.endCodePoint);
111 assertEquals("", 17, rr.value);
112 assertEquals("", false, rr.leadSurrogate);
115 assertEquals("", 0xd800, rr.startCodePoint);
116 assertEquals("", 0xdbff, rr.endCodePoint);
117 assertEquals("", 17, rr.value);
118 assertEquals("", true, rr.leadSurrogate);
121 errln(where() + " Unexpected iteration result");
127 // Iteration with a value mapping function
130 Trie2Writable trie = new Trie2Writable(0xbadfeed, 0);
131 trie.set(0x10123, 42);
133 Trie2.ValueMapper vm = new Trie2.ValueMapper() {
134 public int map(int v) {
135 if (v == 0xbadfeed) {
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);
150 // Iteration over a leading surrogate range.
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);
163 assertEquals("", 0x2f810, r.startCodePoint);
164 assertEquals("", 0x2f810, r.endCodePoint);
165 assertEquals("", 10, r.value);
166 assertEquals("", false, r.leadSurrogate);
169 assertEquals("", 0x2f811, r.startCodePoint);
170 assertEquals("", 0x2fbff, r.endCodePoint);
171 assertEquals("", 0xdefa17, r.value);
172 assertEquals("", false, r.leadSurrogate);
174 assertFalse("", it.hasNext());
177 // Iteration over a leading surrogate range with a ValueMapper.
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) {
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);
197 assertFalse("", it.hasNext());
201 // Test the implementation in Trie2, which is used with Read Only Tries.
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();
214 frozen16.serialize(os);
215 ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
216 Trie2 unserialized16 = Trie2.createFromSerialized(is);
217 assertEquals("", trie, unserialized16);
218 assertEquals("", Trie2_16.class, unserialized16.getClass());
221 frozen32.serialize(os);
222 is = new ByteArrayInputStream(os.toByteArray());
223 Trie2 unserialized32 = Trie2.createFromSerialized(is);
224 assertEquals("", trie, unserialized32);
225 assertEquals("", Trie2_32.class, unserialized32.getClass());
226 } catch (IOException e) {
227 errln(where() + " Unexpected exception: " + e);
235 public void TestTrie2WritableAPI() {
237 // Trie2Writable methods. Check that all functions are present and
238 // nominally working. Not an in-depth test.
241 // Trie2Writable constructor
242 Trie2 t1 = new Trie2Writable(6, 666);
244 // Constructor from another Trie2
245 Trie2 t2 = new Trie2Writable(t1);
246 assertTrue("", t1.equals(t2));
249 Trie2Writable t1w = new Trie2Writable(10, 666);
251 assertEquals("", 10, t1w.get(0x4566));
252 assertEquals("", 99, t1w.get(0x4567));
253 assertEquals("", 666, t1w.get(-1));
254 assertEquals("", 666, t1w.get(0x110000));
258 t1w = new Trie2Writable(10, 666);
259 t1w.setRange(13 /*start*/, 6666 /*end*/, 7788 /*value*/, false /*overwrite */);
260 t1w.setRange(6000, 7000, 9900, true);
261 assertEquals("", 10, t1w.get(12));
262 assertEquals("", 7788, t1w.get(13));
263 assertEquals("", 7788, t1w.get(5999));
264 assertEquals("", 9900, t1w.get(6000));
265 assertEquals("", 9900, t1w.get(7000));
266 assertEquals("", 10, t1w.get(7001));
267 assertEquals("", 666, t1w.get(0x110000));
269 // setRange from a Trie2.Range
270 // (Ranges are more commonly created by iterating over a Trie2,
271 // but create one by hand here)
272 Trie2.Range r = new Trie2.Range();
273 r.startCodePoint = 50;
275 r.value = 0x12345678;
276 r.leadSurrogate = false;
277 t1w = new Trie2Writable(0, 0xbad);
278 t1w.setRange(r, true);
279 assertEquals(null, 0, t1w.get(49));
280 assertEquals("", 0x12345678, t1w.get(50));
281 assertEquals("", 0x12345678, t1w.get(52));
282 assertEquals("", 0, t1w.get(53));
285 // setForLeadSurrogateCodeUnit / getFromU16SingleLead
286 t1w = new Trie2Writable(10, 0xbad);
287 assertEquals("", 10, t1w.getFromU16SingleLead((char)0x0d801));
288 t1w.setForLeadSurrogateCodeUnit((char)0xd801, 5000);
289 t1w.set(0xd801, 6000);
290 assertEquals("", 5000, t1w.getFromU16SingleLead((char)0x0d801));
291 assertEquals("", 6000, t1w.get(0x0d801));
293 // get(). Is covered by nearly every other test.
296 // Trie2_16 getAsFrozen_16()
297 t1w = new Trie2Writable(10, 666);
299 t1w.set(0x1ff00, 224);
300 Trie2_16 t1_16 = t1w.toTrie2_16();
301 assertTrue("", t1w.equals(t1_16));
302 // alter the writable Trie2 and then re-freeze.
304 t1_16 = t1w.toTrie2_16();
305 assertTrue("", t1w.equals(t1_16));
306 assertEquals("", 129, t1w.get(152));
308 // Trie2_32 getAsFrozen_32()
310 t1w = new Trie2Writable(10, 666);
312 t1w.set(0x1ff00, 224);
313 Trie2_32 t1_32 = t1w.toTrie2_32();
314 assertTrue("", t1w.equals(t1_32));
315 // alter the writable Trie2 and then re-freeze.
317 assertNotEquals("", t1_32, t1w);
318 t1_32 = t1w.toTrie2_32();
319 assertTrue("", t1w.equals(t1_32));
320 assertEquals("", 129, t1w.get(152));
323 // serialize(OutputStream os, ValueWidth width)
325 ByteArrayOutputStream os = new ByteArrayOutputStream();
326 t1w = new Trie2Writable(0, 0xbad);
327 t1w.set(0x41, 0x100);
328 t1w.set(0xc2, 0x200);
329 t1w.set(0x404, 0x300);
330 t1w.set(0xd903, 0x500);
331 t1w.set(0xdd29, 0x600);
332 t1w.set(0x1055d3, 0x700);
333 t1w.setForLeadSurrogateCodeUnit((char)0xda1a, 0x800);
335 // Serialize to 16 bits.
336 int serializedLen = t1w.toTrie2_16().serialize(os);
337 // Fragile test. Serialized length could change with changes to compaction.
338 // But it should not change unexpectedly.
339 assertEquals("", 3508, serializedLen);
340 ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
341 Trie2 t1ws16 = Trie2.createFromSerialized(is);
342 assertEquals("", t1ws16.getClass(), Trie2_16.class);
343 assertEquals("", t1w, t1ws16);
345 // Serialize to 32 bits
347 serializedLen = t1w.toTrie2_32().serialize(os);
348 // Fragile test. Serialized length could change with changes to compaction.
349 // But it should not change unexpectedly.
350 assertEquals("", 4332, serializedLen);
351 is = new ByteArrayInputStream(os.toByteArray());
352 Trie2 t1ws32 = Trie2.createFromSerialized(is);
353 assertEquals("", t1ws32.getClass(), Trie2_32.class);
354 assertEquals("", t1w, t1ws32);
355 } catch (IOException e) {
356 errln(where() + e.toString());
362 public void TestCharSequenceIterator() {
363 String text = "abc123\ud800\udc01 "; // Includes a Unicode supplemental character
364 String vals = "LLLNNNX?S";
366 Trie2Writable tw = new Trie2Writable(0, 666);
367 tw.setRange('a', 'z', 'L', false);
368 tw.setRange('1', '9', 'N', false);
370 tw.set(0x10001, 'X');
372 Trie2.CharSequenceIterator it = tw.charSequenceIterator(text, 0);
374 // Check forwards iteration.
375 Trie2.CharSequenceValues ir;
377 for (i=0; it.hasNext(); i++) {
379 int expectedCP = Character.codePointAt(text, i);
380 assertEquals("" + " i="+i, expectedCP, ir.codePoint);
381 assertEquals("" + " i="+i, i, ir.index);
382 assertEquals("" + " i="+i, vals.charAt(i), ir.value);
383 if (expectedCP >= 0x10000) {
387 assertEquals("", text.length(), i);
389 // Check reverse iteration, starting at an intermediate point.
391 for (i=5; it.hasPrevious(); ) {
393 int expectedCP = Character.codePointBefore(text, i);
394 i -= (expectedCP < 0x10000? 1 : 2);
395 assertEquals("" + " i="+i, expectedCP, ir.codePoint);
396 assertEquals("" + " i="+i, i, ir.index);
397 assertEquals("" + " i="+i, vals.charAt(i), ir.value);
399 assertEquals("", 0, i);
405 // Port of Tests from ICU4C ...
407 // setRanges array elements are
408 // {start Code point, limit CP, value, overwrite}
410 // There must be an entry with limit 0 and with the intialValue.
411 // It may be preceded by an entry with negative limit and the errorValue.
413 // checkRanges array elemets are
414 // { limit code point, value}
416 // The expected value range is from the previous boundary's limit to before
417 // this boundary's limit
420 String[] trieNames = {"setRanges1", "setRanges2", "setRanges3", "setRangesEmpty", "setRangesSingleValue"};
421 /* set consecutive ranges, even with value 0 */
425 private static int[][] setRanges1 ={
428 { 0x40, 0xe7, 0x1234, 0 },
429 { 0xe7, 0x3400, 0, 0 },
430 { 0x3400, 0x9fa6, 0x6162, 0 },
431 { 0x9fa6, 0xda9e, 0x3132, 0 },
432 { 0xdada, 0xeeee, 0x87ff, 0 },
433 { 0xeeee, 0x11111, 1, 0 },
434 { 0x11111, 0x44444, 0x6162, 0 },
435 { 0x44444, 0x60003, 0, 0 },
436 { 0xf0003, 0xf0004, 0xf, 0 },
437 { 0xf0004, 0xf0006, 0x10, 0 },
438 { 0xf0006, 0xf0007, 0x11, 0 },
439 { 0xf0007, 0xf0040, 0x12, 0 },
440 { 0xf0040, 0x110000, 0, 0 }
443 private static int[][] checkRanges1 = {
462 /* set some interesting overlapping ranges */
463 private static int [][] setRanges2={
465 { 0x21, 0x7f, 0x5555, 1 },
466 { 0x2f800, 0x2fedc, 0x7a, 1 },
467 { 0x72, 0xdd, 3, 1 },
468 { 0xdd, 0xde, 4, 0 },
469 { 0x201, 0x240, 6, 1 }, /* 3 consecutive blocks with the same pattern but */
470 { 0x241, 0x280, 6, 1 }, /* discontiguous value ranges, testing utrie2_enum() */
471 { 0x281, 0x2c0, 6, 1 },
472 { 0x2f987, 0x2fa98, 5, 1 },
473 { 0x2f777, 0x2f883, 0, 1 },
474 { 0x2f900, 0x2ffaa, 1, 0 },
475 { 0x2ffaa, 0x2ffab, 2, 1 },
476 { 0x2ffbb, 0x2ffc0, 7, 1 }
479 private static int[] [] checkRanges2={
503 private static int[] [] checkRanges2_d800={
508 private static int[][] checkRanges2_d87e={
516 private static int[][] checkRanges2_d87f={
526 private static int[][] checkRanges2_dbff={
532 /* use a non-zero initial value */
533 private static int[][] setRanges3={
534 { 0, 0, 9, 0 }, // non-zero initial value.
535 { 0x31, 0xa4, 1, 0 },
536 { 0x3400, 0x6789, 2, 0 },
537 { 0x8000, 0x89ab, 9, 1 },
538 { 0x9000, 0xa000, 4, 1 },
539 { 0xabcd, 0xbcde, 3, 1 },
540 { 0x55555, 0x110000, 6, 1 }, /* highStart<U+ffff with non-initialValue */
541 { 0xcccc, 0x55555, 6, 1 }
544 private static int[][] checkRanges3={
545 { 0, 9 }, /* non-zero initialValue */
558 /* empty or single-value tries, testing highStart==0 */
559 private static int[][] setRangesEmpty={
560 { 0, 0, 3, 0 } // Only the element with the initial value.
563 private static int[][] checkRangesEmpty={
568 private static int[][] setRangesSingleValue={
569 { 0, 0, 3, 0 }, // Initial value = 3
570 { 0, 0x110000, 5, 1 },
573 private static int[][] checkRangesSingleValue={
580 // Create a test Trie2 from a setRanges test array.
581 // Range data ported from C.
583 private Trie2Writable genTrieFromSetRanges(int [][] ranges) {
585 int initialValue = 0;
586 int errorValue = 0x0bad;
588 if (ranges[i][1] < 0) {
589 errorValue = ranges[i][2];
592 initialValue = ranges[i++][2];
593 Trie2Writable trie = new Trie2Writable(initialValue, errorValue);
595 for (; i<ranges.length; i++) {
596 int rangeStart = ranges[i][0];
597 int rangeEnd = ranges[i][1]-1;
598 int value = ranges[i][2];
599 boolean overwrite = (ranges[i][3] != 0);
600 trie.setRange(rangeStart, rangeEnd, value, overwrite);
603 // Insert some non-default values for lead surrogates.
604 // TODO: this should be represented in the data.
605 trie.setForLeadSurrogateCodeUnit((char)0xd800, 90);
606 trie.setForLeadSurrogateCodeUnit((char)0xd999, 94);
607 trie.setForLeadSurrogateCodeUnit((char)0xdbff, 99);
614 // Check the expected values from a single Trie2.
616 private void trieGettersTest(String testName,
617 Trie2 trie, // The Trie2 to test.
618 int[][] checkRanges) // Expected data.
619 // Tuples of (value, high limit code point)
620 // High limit is first code point following the range
621 // with the indicated value.
622 // (Structures copied from ICU4C tests.)
624 int countCheckRanges = checkRanges.length;
626 int initialValue, errorValue;
629 int i, countSpecials;
631 countSpecials=0; /*getSpecialValues(checkRanges, countCheckRanges, &initialValue, &errorValue);*/
634 if (checkRanges[countSpecials][0] == 0) {
635 initialValue = checkRanges[countSpecials][1];
640 for(i=countSpecials; i<countCheckRanges; ++i) {
641 limit=checkRanges[i][0];
642 value=checkRanges[i][1];
645 value2=trie.get(start);
646 if (value != value2) {
647 // The redundant if, outside of the assert, is for speed.
648 // It makes a significant difference for this test.
649 assertEquals("wrong value for " + testName + " of " + Integer.toHexString(start), value, value2);
656 if(!testName.startsWith("dummy") && !testName.startsWith("trie1")) {
657 /* Test values for lead surrogate code units.
658 * For non-lead-surrogate code units, getFromU16SingleLead() and get()
659 * should be the same.
661 for(start=0xd7ff; start<0xdc01; ++start) {
665 value=trie.get(start);
680 value2 = trie.getFromU16SingleLead((char)start);
682 errln(where() + " testName: " + testName + " getFromU16SingleLead() failed." +
683 "char, exected, actual = " + Integer.toHexString(start) + ", " +
684 Integer.toHexString(value) + ", " + Integer.toHexString(value2));
689 /* test errorValue */
691 value2=trie.get(0x110000);
692 if(value!=errorValue || value2!=errorValue) {
693 errln("trie2.get() error value test. Expected, actual1, actual2 = " +
694 errorValue + ", " + value + ", " + value2);
697 // Check that Trie enumeration produces the same contents as simple get()
698 for (Trie2.Range range: trie) {
699 for (int cp=range.startCodePoint; cp<=range.endCodePoint; cp++) {
700 if (range.leadSurrogate) {
701 assertTrue(testName, cp>=(char)0xd800 && cp<(char)0xdc00);
702 assertEquals(testName, range.value, trie.getFromU16SingleLead((char)cp));
704 assertEquals(testName, range.value, trie.get(cp));
710 // Was testTrieRanges in ICU4C. Renamed to not conflict with ICU4J test framework.
711 private void checkTrieRanges(String testName, String serializedName, boolean withClone,
712 int[][] setRanges, int [][] checkRanges) throws IOException {
714 // Run tests against Tries that were built by ICU4C and serialized.
715 String fileName16 = "Trie2Test." + serializedName + ".16.tri2";
716 String fileName32 = "Trie2Test." + serializedName + ".32.tri2";
718 InputStream is = Trie2Test.class.getResourceAsStream(fileName16);
719 Trie2 trie16 = Trie2.createFromSerialized(is);
722 trieGettersTest(testName, trie16, checkRanges);
723 is = Trie2Test.class.getResourceAsStream(fileName32);
724 Trie2 trie32 = Trie2.createFromSerialized(is);
727 trieGettersTest(testName, trie32, checkRanges);
729 // Run the same tests against locally contructed Tries.
730 Trie2Writable trieW = genTrieFromSetRanges(setRanges);
731 trieGettersTest(testName, trieW, checkRanges);
732 assertEquals("", trieW, trie16); // Locally built tries must be
733 assertEquals("", trieW, trie32); // the same as those imported from ICU4C
736 Trie2_32 trie32a = trieW.toTrie2_32();
737 trieGettersTest(testName, trie32a, checkRanges);
739 Trie2_16 trie16a = trieW.toTrie2_16();
740 trieGettersTest(testName, trie16a, checkRanges);
744 // Was "TrieTest" in trie2test.c
745 public void TestRanges() throws IOException {
746 checkTrieRanges("set1", "setRanges1", false, setRanges1, checkRanges1);
747 checkTrieRanges("set2-overlap", "setRanges2", false, setRanges2, checkRanges2);
748 checkTrieRanges("set3-initial-9", "setRanges3", false, setRanges3, checkRanges3);
749 checkTrieRanges("set-empty", "setRangesEmpty", false, setRangesEmpty, checkRangesEmpty);
750 checkTrieRanges("set-single-value", "setRangesSingleValue", false, setRangesSingleValue,
751 checkRangesSingleValue);
752 checkTrieRanges("set2-overlap.withClone", "setRanges2", true, setRanges2, checkRanges2);
756 private String where() {
757 StackTraceElement[] st = new Throwable().getStackTrace();
758 String w = "File: " + st[1].getFileName() + ", Line " + st[1].getLineNumber();