]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/util/CompactArrayTest.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / util / CompactArrayTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2002-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.test.util;
8
9 import com.ibm.icu.dev.test.TestFmwk;
10 import com.ibm.icu.impl.Utility;
11 import com.ibm.icu.util.CompactByteArray;
12 import com.ibm.icu.util.CompactCharArray;
13
14 /**
15  * @since release 2.2
16  */
17 public final class CompactArrayTest extends TestFmwk 
18
19     public static void main(String[] args) throws Exception
20     {
21         new CompactArrayTest().run(args);
22     }
23     
24     public void TestByteArrayCoverage() {
25     CompactByteArray cba = new CompactByteArray();
26     cba.setElementAt((char)0x5, (byte)0xdf);
27     cba.setElementAt((char)0x105, (byte)0xdf);
28     cba.setElementAt((char)0x205, (byte)0xdf);
29     cba.setElementAt((char)0x305, (byte)0xdf);
30     CompactByteArray cba2 = new CompactByteArray((byte)0xdf);
31     if (cba.equals(cba2)) {
32         errln("unequal byte arrays compare equal");
33     }
34     CompactByteArray cba3 = (CompactByteArray)cba.clone();
35
36     logln("equals null: " + cba.equals(null));
37     logln("equals self: " + cba.equals(cba));
38     logln("equals clone: " + cba.equals(cba3));
39     logln("equals bogus: " + cba.equals(new Object()));
40     logln("hash: " + cba.hashCode());
41
42     cba.compact(true);
43     cba.compact(true);
44
45     char[] xa = cba.getIndexArray();
46     byte[] va = cba.getValueArray();
47     CompactByteArray cba4 = new CompactByteArray(xa, va);
48
49     String xs = Utility.arrayToRLEString(xa);
50     String vs = Utility.arrayToRLEString(va);
51     CompactByteArray cba5 = new CompactByteArray(xs, vs);
52
53     logln("equals: " + cba4.equals(cba5));
54     logln("equals: " + cba.equals(cba4));
55     
56       cba4.compact(false);
57     logln("equals: " + cba4.equals(cba5));
58
59     cba5.compact(true);
60     logln("equals: " + cba4.equals(cba5));
61
62     cba.setElementAt((char)0x405, (byte)0xdf); // force expand
63     logln("modified equals clone: " + cba.equals(cba3));
64
65     cba3.setElementAt((char)0x405, (byte)0xdf); // equivalent modification
66     logln("modified equals modified clone: " + cba.equals(cba3));
67
68     cba3.setElementAt((char)0x405, (byte)0xee); // different modification
69     logln("different mod equals: " + cba.equals(cba3));
70
71     cba.compact();
72     CompactByteArray cba6 = (CompactByteArray)cba.clone();
73     logln("cloned compact equals: " + cba.equals(cba6));
74
75     cba6.setElementAt((char)0x405, (byte)0xee);
76     logln("modified clone: " + cba3.equals(cba6));
77
78     cba6.setElementAt((char)0x100, (char)0x104, (byte)0xfe);
79     for (int i = 0x100; i < 0x105; ++i) {
80         cba3.setElementAt((char)i, (byte)0xfe);
81     }
82     logln("double modified: " + cba3.equals(cba6));
83     }
84
85     public void TestCharArrayCoverage() {
86     // v1.8 fails with extensive compaction, so set to false
87     final boolean EXTENSIVE = false;
88
89     CompactCharArray cca = new CompactCharArray();
90     cca.setElementAt((char)0x5, (char)0xdf);
91     cca.setElementAt((char)0x105, (char)0xdf);
92     cca.setElementAt((char)0x205, (char)0xdf);
93     cca.setElementAt((char)0x305, (char)0xdf);
94     CompactCharArray cca2 = new CompactCharArray((char)0xdf);
95     if (cca.equals(cca2)) {
96         errln("unequal char arrays compare equal");
97     }
98     CompactCharArray cca3 = (CompactCharArray)cca.clone();
99
100     logln("equals null: " + cca.equals(null));
101     logln("equals self: " + cca.equals(cca));
102     logln("equals clone: " + cca.equals(cca3));
103     logln("equals bogus: " + cca.equals(new Object()));
104     logln("hash: " + cca.hashCode());
105
106     cca.compact(EXTENSIVE);
107     cca.compact(EXTENSIVE);
108
109     char[] xa = cca.getIndexArray();
110     char[] va = cca.getValueArray();
111     CompactCharArray cca4 = new CompactCharArray(xa, va);
112
113     String xs = Utility.arrayToRLEString(xa);
114     String vs = Utility.arrayToRLEString(va);
115     CompactCharArray cca5 = new CompactCharArray(xs, vs);
116
117     logln("equals: " + cca4.equals(cca5));
118     logln("equals: " + cca.equals(cca4));
119
120     cca4.compact(false);
121     logln("equals: " + cca4.equals(cca5));
122
123     cca5.compact(EXTENSIVE);
124     logln("equals: " + cca4.equals(cca5));
125
126     cca.setElementAt((char)0x405, (char)0xdf); // force expand
127     logln("modified equals clone: " + cca.equals(cca3));
128
129     cca3.setElementAt((char)0x405, (char)0xdf); // equivalent modification
130     logln("modified equals modified clone: " + cca.equals(cca3));
131
132     cca3.setElementAt((char)0x405, (char)0xee); // different modification
133     logln("different mod equals: " + cca.equals(cca3));
134     
135     // after setElementAt isCompact is set to false
136     cca3.compact(true);
137     logln("different mod equals: " + cca.equals(cca3));
138     
139     cca3.setElementAt((char)0x405, (char)0xee); // different modification
140     logln("different mod equals: " + cca.equals(cca3));
141         // after setElementAt isCompact is set to false
142     cca3.compact();
143     logln("different mod equals: " + cca.equals(cca3));
144     
145     // v1.8 fails with extensive compaction, and defaults extensive, so don't compact
146     // cca.compact();
147     CompactCharArray cca6 = (CompactCharArray)cca.clone();
148     logln("cloned compact equals: " + cca.equals(cca6));
149
150     cca6.setElementAt((char)0x405, (char)0xee);
151     logln("modified clone: " + cca3.equals(cca6));
152
153     cca6.setElementAt((char)0x100, (char)0x104, (char)0xfe);
154     for (int i = 0x100; i < 0x105; ++i) {
155         cca3.setElementAt((char)i, (char)0xfe);
156     }
157     logln("double modified: " + cca3.equals(cca6));
158     }
159 }