001: //##header
002: /*
003: **********************************************************************
004: * Copyright (c) 2003-2005, International Business Machines
005: * Corporation and others. All Rights Reserved.
006: **********************************************************************
007: * Author: Alan Liu
008: * Created: March 8 2003
009: * Since: ICU 2.6
010: **********************************************************************
011: */
012: package com.ibm.icu.dev.test.util;
013:
014: import java.util.Arrays;
015: import java.util.HashSet;
016: import java.util.List;
017: import java.util.Set;
018:
019: import com.ibm.icu.dev.test.TestFmwk;
020: import com.ibm.icu.impl.Utility;
021: import com.ibm.icu.text.UnicodeSet;
022: import com.ibm.icu.util.ByteArrayWrapper;
023:
024: /**
025: * @test
026: * @summary Test of internal Utility class
027: */
028: public class UtilityTest extends TestFmwk {
029:
030: public static void main(String[] args) throws Exception {
031: new UtilityTest().run(args);
032: }
033:
034: public void TestUnescape() {
035: final String input = "Sch\\u00f6nes Auto: \\u20ac 11240.\\fPrivates Zeichen: \\U00102345\\e\\cC\\n \\x1b\\x{263a}";
036:
037: final String expect = "Sch\u00F6nes Auto: \u20AC 11240.\u000CPrivates Zeichen: \uDBC8\uDF45\u001B\u0003\012 \u001B\u263A";
038:
039: String result = Utility.unescape(input);
040: if (!result.equals(expect)) {
041: errln("FAIL: Utility.unescape() returned " + result
042: + ", exp. " + expect);
043: }
044: }
045:
046: public void TestFormat() {
047: String data[] = {
048: "the quick brown fox jumps over the lazy dog",
049: // result of this conversion will exceed the original length and
050: // cause a newline to be inserted
051: "testing space , quotations \"",
052: "testing weird supplementary characters \ud800\udc00",
053: "testing control characters \u0001 and line breaking!! \n are we done yet?" };
054: String result[] = {
055: " \"the quick brown fox jumps over the lazy dog\"",
056: " \"testing space , quotations \\042\"",
057: " \"testing weird supplementary characters \\uD800\\uDC00\"",
058: " \"testing control characters \\001 and line breaking!! \\n are we done ye\"+"
059: + Utility.LINE_SEPARATOR + " \"t?\"" };
060: String result1[] = {
061: "\"the quick brown fox jumps over the lazy dog\"",
062: "\"testing space , quotations \\042\"",
063: "\"testing weird supplementary characters \\uD800\\uDC00\"",
064: "\"testing control characters \\001 and line breaking!! \\n are we done yet?\"" };
065:
066: for (int i = 0; i < data.length; i++) {
067: assertEquals("formatForSource(\"" + data[i] + "\")",
068: result[i], Utility.formatForSource(data[i]));
069: }
070: for (int i = 0; i < data.length; i++) {
071: assertEquals("format1ForSource(\"" + data[i] + "\")",
072: result1[i], Utility.format1ForSource(data[i]));
073: }
074: }
075:
076: public void TestHighBit() {
077: int data[] = { -1, -1276, 0, 0xFFFF, 0x1234 };
078: byte result[] = { -1, -1, -1, 15, 12 };
079: for (int i = 0; i < data.length; i++) {
080: if (Utility.highBit(data[i]) != result[i]) {
081: errln("Fail: Highest bit of \\u"
082: + Integer.toHexString(data[i]) + " should be "
083: + result[i]);
084: }
085: }
086: }
087:
088: public void TestCompareUnsigned() {
089: int data[] = { 0, 1, 0x8fffffff, -1, Integer.MAX_VALUE,
090: Integer.MIN_VALUE, 2342423, -2342423 };
091: for (int i = 0; i < data.length; i++) {
092: for (int j = 0; j < data.length; j++) {
093: if (Utility.compareUnsigned(data[i], data[j]) != compareLongUnsigned(
094: data[i], data[j])) {
095: errln("Fail: Unsigned comparison failed with "
096: + data[i] + " " + data[i + 1]);
097: }
098: }
099: }
100: }
101:
102: // This test indends to test the utility class ByteArrayWrapper
103: // Seems that the class is somewhat incomplete, for example
104: // - getHashCode(Object) is weird
105: // - PatternMatch feature(search part of array within the whole one) lacks
106: public void TestByteArrayWrapper() {
107: byte[] ba = { 0x00, 0x01, 0x02 };
108: byte[] bb = { 0x00, 0x01, 0x02, -1 };
109: //#ifndef FOUNDATION
110: java.nio.ByteBuffer buffer = java.nio.ByteBuffer.wrap(ba);
111: //#else
112: //## com.ibm.icu.impl.ByteBuffer buffer = com.ibm.icu.impl.ByteBuffer.wrap(ba);
113: //#endif
114: ByteArrayWrapper x = new ByteArrayWrapper(buffer);
115:
116: ByteArrayWrapper y = new ByteArrayWrapper(ba, 3);
117: ByteArrayWrapper z = new ByteArrayWrapper(bb, 3);
118:
119: if (!y.toString().equals("00 01 02")) {
120: errln("FAIL: test toString : Failed!");
121: }
122:
123: // test equality
124: if (!x.equals(y) || !x.equals(z))
125: errln("FAIL: test (operator ==): Failed!");
126: if (x.hashCode() != y.hashCode())
127: errln("FAIL: identical objects have different hash codes.");
128:
129: // test non-equality
130: y = new ByteArrayWrapper(bb, 4);
131: if (x.equals(y))
132: errln("FAIL: test (operator !=): Failed!");
133:
134: // test sign of unequal comparison
135: if ((x.compareTo(y) > 0) != (y.compareTo(x) < 0)) {
136: errln("FAIL: comparisons not opposite sign");
137: }
138: }
139:
140: private int compareLongUnsigned(int x, int y) {
141: long x1 = x & 0xFFFFFFFFl;
142: long y1 = y & 0xFFFFFFFFl;
143: if (x1 < y1) {
144: return -1;
145: } else if (x1 > y1) {
146: return 1;
147: }
148: return 0;
149: }
150:
151: public void TestUnicodeSet() {
152: String[] array = new String[] { "a", "b", "c", "{de}" };
153: List list = Arrays.asList(array);
154: Set aset = new HashSet(list);
155: logln(" *** The source set's size is: " + aset.size());
156: //The size reads 4
157: UnicodeSet set = new UnicodeSet();
158: set.clear();
159: set.addAll(aset);
160: logln(" *** After addAll, the UnicodeSet size is: "
161: + set.size());
162: //The size should also read 4, but 0 is seen instead
163:
164: }
165: }
|