]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/util/UtilityTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / util / UtilityTest.java
1 /*\r
2 **********************************************************************\r
3 * Copyright (c) 2003-2009, International Business Machines\r
4 * Corporation and others.  All Rights Reserved.\r
5 **********************************************************************\r
6 * Author: Alan Liu\r
7 * Created: March 8 2003\r
8 * Since: ICU 2.6\r
9 **********************************************************************\r
10 */\r
11 package com.ibm.icu.dev.test.util;\r
12 \r
13 import java.util.Arrays;\r
14 import java.util.HashSet;\r
15 import java.util.List;\r
16 import java.util.Set;\r
17 \r
18 import com.ibm.icu.dev.test.TestFmwk;\r
19 import com.ibm.icu.impl.Assert;\r
20 import com.ibm.icu.impl.InvalidFormatException;\r
21 import com.ibm.icu.impl.Utility;\r
22 import com.ibm.icu.text.UnicodeSet;\r
23 import com.ibm.icu.util.ByteArrayWrapper;\r
24 import com.ibm.icu.util.CaseInsensitiveString;\r
25 \r
26 /**\r
27  * @test\r
28  * @summary Test of internal Utility class\r
29  */\r
30 public class UtilityTest extends TestFmwk {\r
31 \r
32     public static void main(String[] args) throws Exception {\r
33         new UtilityTest().run(args);\r
34     }\r
35 \r
36     public void TestUnescape() {\r
37         final String input =\r
38             "Sch\\u00f6nes Auto: \\u20ac 11240.\\fPrivates Zeichen: \\U00102345\\e\\cC\\n \\x1b\\x{263a}";\r
39 \r
40         final String expect = \r
41             "Sch\u00F6nes Auto: \u20AC 11240.\u000CPrivates Zeichen: \uDBC8\uDF45\u001B\u0003\012 \u001B\u263A";\r
42 \r
43         String result = Utility.unescape(input);\r
44         if (!result.equals(expect)) {\r
45             errln("FAIL: Utility.unescape() returned " + result + ", exp. " + expect);\r
46         }\r
47     }\r
48     \r
49     public void TestFormat()\r
50     {\r
51         String data[] = {\r
52             "the quick brown fox jumps over the lazy dog",\r
53             // result of this conversion will exceed the original length and\r
54             // cause a newline to be inserted\r
55             "testing space , quotations \"",\r
56             "testing weird supplementary characters \ud800\udc00",\r
57             "testing control characters \u0001 and line breaking!! \n are we done yet?"\r
58         };\r
59         String result[] = {\r
60             "        \"the quick brown fox jumps over the lazy dog\"",\r
61             "        \"testing space , quotations \\042\"",\r
62             "        \"testing weird supplementary characters \\uD800\\uDC00\"",\r
63             "        \"testing control characters \\001 and line breaking!! \\n are we done ye\"+"\r
64                      + Utility.LINE_SEPARATOR + "        \"t?\""\r
65         };\r
66         String result1[] = {\r
67             "\"the quick brown fox jumps over the lazy dog\"",\r
68             "\"testing space , quotations \\042\"",\r
69             "\"testing weird supplementary characters \\uD800\\uDC00\"",\r
70             "\"testing control characters \\001 and line breaking!! \\n are we done yet?\""\r
71         };\r
72         \r
73         for (int i = 0; i < data.length; i ++) {\r
74             assertEquals("formatForSource(\"" + data[i] + "\")",\r
75                          result[i], Utility.formatForSource(data[i]));\r
76         }\r
77         for (int i = 0; i < data.length; i ++) {\r
78             assertEquals("format1ForSource(\"" + data[i] + "\")",\r
79                          result1[i], Utility.format1ForSource(data[i]));\r
80         }\r
81     }\r
82     \r
83     public void TestHighBit()\r
84     {\r
85         int data[] = {-1, -1276, 0, 0xFFFF, 0x1234};\r
86         byte result[] = {-1, -1, -1, 15, 12};\r
87         for (int i = 0; i < data.length; i ++) {\r
88             if (Utility.highBit(data[i]) != result[i]) {\r
89                 errln("Fail: Highest bit of \\u" \r
90                       + Integer.toHexString(data[i]) + " should be "\r
91                       + result[i]);\r
92             }\r
93         }\r
94     }\r
95     \r
96     public void TestCompareUnsigned()\r
97     {\r
98         int data[] = {0, 1, 0x8fffffff, -1, Integer.MAX_VALUE, \r
99                       Integer.MIN_VALUE, 2342423, -2342423};\r
100         for (int i = 0; i < data.length; i ++) {\r
101             for (int j = 0; j < data.length; j ++) {\r
102                 if (Utility.compareUnsigned(data[i], data[j]) \r
103                     != compareLongUnsigned(data[i], data[j])) {\r
104                     errln("Fail: Unsigned comparison failed with " + data[i] \r
105                           + " " + data[i + 1]);\r
106                 }\r
107             }\r
108         }\r
109     }\r
110 \r
111     // This test indends to test the utility class ByteArrayWrapper\r
112     // Seems that the class is somewhat incomplete, for example\r
113     //      - getHashCode(Object) is weird\r
114     //      - PatternMatch feature(search part of array within the whole one) lacks\r
115     public void TestByteArrayWrapper()\r
116     {\r
117         byte[] ba = {0x00, 0x01, 0x02};\r
118         byte[] bb = {0x00, 0x01, 0x02, -1};\r
119 \r
120         java.nio.ByteBuffer buffer = java.nio.ByteBuffer.wrap(ba);\r
121         ByteArrayWrapper x = new ByteArrayWrapper(buffer);\r
122         \r
123         ByteArrayWrapper y = new ByteArrayWrapper(ba, 3);\r
124         ByteArrayWrapper z = new ByteArrayWrapper(bb, 3);\r
125 \r
126         \r
127         if (!y.toString().equals("00 01 02")){\r
128             errln("FAIL: test toString : Failed!");\r
129         }\r
130         \r
131         // test equality\r
132         if (!x.equals(y) || !x.equals(z))\r
133             errln("FAIL: test (operator ==): Failed!");\r
134         if (x.hashCode()!=y.hashCode())\r
135             errln("FAIL: identical objects have different hash codes.");\r
136 \r
137         // test non-equality\r
138         y = new ByteArrayWrapper(bb, 4);\r
139         if (x.equals(y))\r
140             errln("FAIL: test (operator !=): Failed!");\r
141 \r
142         // test sign of unequal comparison\r
143         if ((x.compareTo(y) > 0) != (y.compareTo(x) < 0)) {\r
144             errln("FAIL: comparisons not opposite sign");\r
145         }\r
146     }\r
147 \r
148     private int compareLongUnsigned(int x, int y)\r
149     {\r
150         long x1 = x & 0xFFFFFFFFl;\r
151         long y1 = y & 0xFFFFFFFFl;\r
152         if (x1 < y1) {\r
153             return -1;\r
154         }\r
155         else if (x1 > y1) {\r
156             return 1;\r
157         }\r
158         return 0;\r
159     }\r
160     public void TestUnicodeSet(){\r
161         String[] array = new String[]{"a", "b", "c", "{de}"};\r
162         List list = Arrays.asList(array);\r
163         Set aset = new HashSet(list);\r
164         logln(" *** The source set's size is: " + aset.size());\r
165     //The size reads 4\r
166         UnicodeSet set = new UnicodeSet();\r
167         set.clear();\r
168         set.addAll(aset);\r
169         logln(" *** After addAll, the UnicodeSet size is: " + set.size());\r
170     //The size should also read 4, but 0 is seen instead\r
171 \r
172     }\r
173 \r
174     public void TestAssert(){\r
175         try {\r
176             Assert.assrt(false);\r
177             errln("FAIL: Assert.assrt(false)");\r
178         }\r
179         catch (IllegalStateException e) {\r
180             if (e.getMessage().equals("assert failed")) {\r
181                 logln("Assert.assrt(false) works");\r
182             }\r
183             else {\r
184                 errln("FAIL: Assert.assrt(false) returned " + e.getMessage());\r
185             }\r
186         }\r
187         try {\r
188             Assert.assrt("Assert message", false);\r
189             errln("FAIL: Assert.assrt(false)");\r
190         }\r
191         catch (IllegalStateException e) {\r
192             if (e.getMessage().equals("assert 'Assert message' failed")) {\r
193                 logln("Assert.assrt(false) works");\r
194             }\r
195             else {\r
196                 errln("FAIL: Assert.assrt(false) returned " + e.getMessage());\r
197             }\r
198         }\r
199         try {\r
200             Assert.fail("Assert message");\r
201             errln("FAIL: Assert.fail");\r
202         }\r
203         catch (IllegalStateException e) {\r
204             if (e.getMessage().equals("failure 'Assert message'")) {\r
205                 logln("Assert.fail works");\r
206             }\r
207             else {\r
208                 errln("FAIL: Assert.fail returned " + e.getMessage());\r
209             }\r
210         }\r
211         try {\r
212             Assert.fail(new InvalidFormatException());\r
213             errln("FAIL: Assert.fail with an exception");\r
214         }\r
215         catch (IllegalStateException e) {\r
216             logln("Assert.fail works");\r
217         }\r
218     }\r
219     \r
220     public void TestCaseInsensitiveString() {\r
221         CaseInsensitiveString str1 = new CaseInsensitiveString("ThIs is A tEst");\r
222         CaseInsensitiveString str2 = new CaseInsensitiveString("This IS a test");\r
223         if (!str1.equals(str2)\r
224             || !str1.toString().equals(str1.getString())\r
225             || str1.toString().equals(str2.toString()))\r
226         {\r
227             errln("FAIL: str1("+str1+") != str2("+str2+")");\r
228         }\r
229     }\r
230 }\r