001: /*
002: *******************************************************************************
003: * Copyright (C) 2002-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.util;
008:
009: import com.ibm.icu.dev.test.TestFmwk;
010:
011: import com.ibm.icu.impl.Utility;
012: import com.ibm.icu.util.CompactByteArray;
013: import com.ibm.icu.util.CompactCharArray;
014:
015: /**
016: * @since release 2.2
017: */
018: public final class CompactArrayTest extends TestFmwk {
019: public static void main(String[] args) throws Exception {
020: new CompactArrayTest().run(args);
021: }
022:
023: public void TestByteArrayCoverage() {
024: CompactByteArray cba = new CompactByteArray();
025: cba.setElementAt((char) 0x5, (byte) 0xdf);
026: cba.setElementAt((char) 0x105, (byte) 0xdf);
027: cba.setElementAt((char) 0x205, (byte) 0xdf);
028: cba.setElementAt((char) 0x305, (byte) 0xdf);
029: CompactByteArray cba2 = new CompactByteArray((byte) 0xdf);
030: if (cba.equals(cba2)) {
031: errln("unequal byte arrays compare equal");
032: }
033: CompactByteArray cba3 = (CompactByteArray) cba.clone();
034:
035: logln("equals null: " + cba.equals(null));
036: logln("equals self: " + cba.equals(cba));
037: logln("equals clone: " + cba.equals(cba3));
038: logln("equals bogus: " + cba.equals(new Object()));
039: logln("hash: " + cba.hashCode());
040:
041: cba.compact(true);
042: cba.compact(true);
043:
044: char[] xa = cba.getIndexArray();
045: byte[] va = cba.getValueArray();
046: CompactByteArray cba4 = new CompactByteArray(xa, va);
047:
048: String xs = Utility.arrayToRLEString(xa);
049: String vs = Utility.arrayToRLEString(va);
050: CompactByteArray cba5 = new CompactByteArray(xs, vs);
051:
052: logln("equals: " + cba4.equals(cba5));
053: logln("equals: " + cba.equals(cba4));
054:
055: cba4.compact(false);
056: logln("equals: " + cba4.equals(cba5));
057:
058: cba5.compact(true);
059: logln("equals: " + cba4.equals(cba5));
060:
061: cba.setElementAt((char) 0x405, (byte) 0xdf); // force expand
062: logln("modified equals clone: " + cba.equals(cba3));
063:
064: cba3.setElementAt((char) 0x405, (byte) 0xdf); // equivalent modification
065: logln("modified equals modified clone: " + cba.equals(cba3));
066:
067: cba3.setElementAt((char) 0x405, (byte) 0xee); // different modification
068: logln("different mod equals: " + cba.equals(cba3));
069:
070: cba.compact();
071: CompactByteArray cba6 = (CompactByteArray) cba.clone();
072: logln("cloned compact equals: " + cba.equals(cba6));
073:
074: cba6.setElementAt((char) 0x405, (byte) 0xee);
075: logln("modified clone: " + cba3.equals(cba6));
076:
077: cba6.setElementAt((char) 0x100, (char) 0x104, (byte) 0xfe);
078: for (int i = 0x100; i < 0x105; ++i) {
079: cba3.setElementAt((char) i, (byte) 0xfe);
080: }
081: logln("double modified: " + cba3.equals(cba6));
082: }
083:
084: public void TestCharArrayCoverage() {
085: // v1.8 fails with extensive compaction, so set to false
086: final boolean EXTENSIVE = false;
087:
088: CompactCharArray cca = new CompactCharArray();
089: cca.setElementAt((char) 0x5, (char) 0xdf);
090: cca.setElementAt((char) 0x105, (char) 0xdf);
091: cca.setElementAt((char) 0x205, (char) 0xdf);
092: cca.setElementAt((char) 0x305, (char) 0xdf);
093: CompactCharArray cca2 = new CompactCharArray((char) 0xdf);
094: if (cca.equals(cca2)) {
095: errln("unequal char arrays compare equal");
096: }
097: CompactCharArray cca3 = (CompactCharArray) cca.clone();
098:
099: logln("equals null: " + cca.equals(null));
100: logln("equals self: " + cca.equals(cca));
101: logln("equals clone: " + cca.equals(cca3));
102: logln("equals bogus: " + cca.equals(new Object()));
103: logln("hash: " + cca.hashCode());
104:
105: cca.compact(EXTENSIVE);
106: cca.compact(EXTENSIVE);
107:
108: char[] xa = cca.getIndexArray();
109: char[] va = cca.getValueArray();
110: CompactCharArray cca4 = new CompactCharArray(xa, va);
111:
112: String xs = Utility.arrayToRLEString(xa);
113: String vs = Utility.arrayToRLEString(va);
114: CompactCharArray cca5 = new CompactCharArray(xs, vs);
115:
116: logln("equals: " + cca4.equals(cca5));
117: logln("equals: " + cca.equals(cca4));
118:
119: cca4.compact(false);
120: logln("equals: " + cca4.equals(cca5));
121:
122: cca5.compact(EXTENSIVE);
123: logln("equals: " + cca4.equals(cca5));
124:
125: cca.setElementAt((char) 0x405, (char) 0xdf); // force expand
126: logln("modified equals clone: " + cca.equals(cca3));
127:
128: cca3.setElementAt((char) 0x405, (char) 0xdf); // equivalent modification
129: logln("modified equals modified clone: " + cca.equals(cca3));
130:
131: cca3.setElementAt((char) 0x405, (char) 0xee); // different modification
132: logln("different mod equals: " + cca.equals(cca3));
133:
134: // after setElementAt isCompact is set to false
135: cca3.compact(true);
136: logln("different mod equals: " + cca.equals(cca3));
137:
138: cca3.setElementAt((char) 0x405, (char) 0xee); // different modification
139: logln("different mod equals: " + cca.equals(cca3));
140: // after setElementAt isCompact is set to false
141: cca3.compact();
142: logln("different mod equals: " + cca.equals(cca3));
143:
144: // v1.8 fails with extensive compaction, and defaults extensive, so don't compact
145: // cca.compact();
146: CompactCharArray cca6 = (CompactCharArray) cca.clone();
147: logln("cloned compact equals: " + cca.equals(cca6));
148:
149: cca6.setElementAt((char) 0x405, (char) 0xee);
150: logln("modified clone: " + cca3.equals(cca6));
151:
152: cca6.setElementAt((char) 0x100, (char) 0x104, (char) 0xfe);
153: for (int i = 0x100; i < 0x105; ++i) {
154: cca3.setElementAt((char) i, (char) 0xfe);
155: }
156: logln("double modified: " + cca3.equals(cca6));
157: }
158: }
|