2 *******************************************************************************
\r
3 * Copyright (C) 2002-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.util;
\r
9 import com.ibm.icu.dev.test.TestFmwk;
\r
10 import com.ibm.icu.impl.Utility;
\r
11 import com.ibm.icu.util.CompactByteArray;
\r
12 import com.ibm.icu.util.CompactCharArray;
\r
15 * @since release 2.2
\r
17 public final class CompactArrayTest extends TestFmwk
\r
19 public static void main(String[] args) throws Exception
\r
21 new CompactArrayTest().run(args);
\r
24 public void TestByteArrayCoverage() {
\r
25 CompactByteArray cba = new CompactByteArray();
\r
26 cba.setElementAt((char)0x5, (byte)0xdf);
\r
27 cba.setElementAt((char)0x105, (byte)0xdf);
\r
28 cba.setElementAt((char)0x205, (byte)0xdf);
\r
29 cba.setElementAt((char)0x305, (byte)0xdf);
\r
30 CompactByteArray cba2 = new CompactByteArray((byte)0xdf);
\r
31 if (cba.equals(cba2)) {
\r
32 errln("unequal byte arrays compare equal");
\r
34 CompactByteArray cba3 = (CompactByteArray)cba.clone();
\r
36 logln("equals null: " + cba.equals(null));
\r
37 logln("equals self: " + cba.equals(cba));
\r
38 logln("equals clone: " + cba.equals(cba3));
\r
39 logln("equals bogus: " + cba.equals(new Object()));
\r
40 logln("hash: " + cba.hashCode());
\r
45 char[] xa = cba.getIndexArray();
\r
46 byte[] va = cba.getValueArray();
\r
47 CompactByteArray cba4 = new CompactByteArray(xa, va);
\r
49 String xs = Utility.arrayToRLEString(xa);
\r
50 String vs = Utility.arrayToRLEString(va);
\r
51 CompactByteArray cba5 = new CompactByteArray(xs, vs);
\r
53 logln("equals: " + cba4.equals(cba5));
\r
54 logln("equals: " + cba.equals(cba4));
\r
56 cba4.compact(false);
\r
57 logln("equals: " + cba4.equals(cba5));
\r
60 logln("equals: " + cba4.equals(cba5));
\r
62 cba.setElementAt((char)0x405, (byte)0xdf); // force expand
\r
63 logln("modified equals clone: " + cba.equals(cba3));
\r
65 cba3.setElementAt((char)0x405, (byte)0xdf); // equivalent modification
\r
66 logln("modified equals modified clone: " + cba.equals(cba3));
\r
68 cba3.setElementAt((char)0x405, (byte)0xee); // different modification
\r
69 logln("different mod equals: " + cba.equals(cba3));
\r
72 CompactByteArray cba6 = (CompactByteArray)cba.clone();
\r
73 logln("cloned compact equals: " + cba.equals(cba6));
\r
75 cba6.setElementAt((char)0x405, (byte)0xee);
\r
76 logln("modified clone: " + cba3.equals(cba6));
\r
78 cba6.setElementAt((char)0x100, (char)0x104, (byte)0xfe);
\r
79 for (int i = 0x100; i < 0x105; ++i) {
\r
80 cba3.setElementAt((char)i, (byte)0xfe);
\r
82 logln("double modified: " + cba3.equals(cba6));
\r
85 public void TestCharArrayCoverage() {
\r
86 // v1.8 fails with extensive compaction, so set to false
\r
87 final boolean EXTENSIVE = false;
\r
89 CompactCharArray cca = new CompactCharArray();
\r
90 cca.setElementAt((char)0x5, (char)0xdf);
\r
91 cca.setElementAt((char)0x105, (char)0xdf);
\r
92 cca.setElementAt((char)0x205, (char)0xdf);
\r
93 cca.setElementAt((char)0x305, (char)0xdf);
\r
94 CompactCharArray cca2 = new CompactCharArray((char)0xdf);
\r
95 if (cca.equals(cca2)) {
\r
96 errln("unequal char arrays compare equal");
\r
98 CompactCharArray cca3 = (CompactCharArray)cca.clone();
\r
100 logln("equals null: " + cca.equals(null));
\r
101 logln("equals self: " + cca.equals(cca));
\r
102 logln("equals clone: " + cca.equals(cca3));
\r
103 logln("equals bogus: " + cca.equals(new Object()));
\r
104 logln("hash: " + cca.hashCode());
\r
106 cca.compact(EXTENSIVE);
\r
107 cca.compact(EXTENSIVE);
\r
109 char[] xa = cca.getIndexArray();
\r
110 char[] va = cca.getValueArray();
\r
111 CompactCharArray cca4 = new CompactCharArray(xa, va);
\r
113 String xs = Utility.arrayToRLEString(xa);
\r
114 String vs = Utility.arrayToRLEString(va);
\r
115 CompactCharArray cca5 = new CompactCharArray(xs, vs);
\r
117 logln("equals: " + cca4.equals(cca5));
\r
118 logln("equals: " + cca.equals(cca4));
\r
120 cca4.compact(false);
\r
121 logln("equals: " + cca4.equals(cca5));
\r
123 cca5.compact(EXTENSIVE);
\r
124 logln("equals: " + cca4.equals(cca5));
\r
126 cca.setElementAt((char)0x405, (char)0xdf); // force expand
\r
127 logln("modified equals clone: " + cca.equals(cca3));
\r
129 cca3.setElementAt((char)0x405, (char)0xdf); // equivalent modification
\r
130 logln("modified equals modified clone: " + cca.equals(cca3));
\r
132 cca3.setElementAt((char)0x405, (char)0xee); // different modification
\r
133 logln("different mod equals: " + cca.equals(cca3));
\r
135 // after setElementAt isCompact is set to false
\r
136 cca3.compact(true);
\r
137 logln("different mod equals: " + cca.equals(cca3));
\r
139 cca3.setElementAt((char)0x405, (char)0xee); // different modification
\r
140 logln("different mod equals: " + cca.equals(cca3));
\r
141 // after setElementAt isCompact is set to false
\r
143 logln("different mod equals: " + cca.equals(cca3));
\r
145 // v1.8 fails with extensive compaction, and defaults extensive, so don't compact
\r
147 CompactCharArray cca6 = (CompactCharArray)cca.clone();
\r
148 logln("cloned compact equals: " + cca.equals(cca6));
\r
150 cca6.setElementAt((char)0x405, (char)0xee);
\r
151 logln("modified clone: " + cca3.equals(cca6));
\r
153 cca6.setElementAt((char)0x100, (char)0x104, (char)0xfe);
\r
154 for (int i = 0x100; i < 0x105; ++i) {
\r
155 cca3.setElementAt((char)i, (char)0xfe);
\r
157 logln("double modified: " + cca3.equals(cca6));
\r