• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2016 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 /*
5 **********************************************************************
6 * Copyright (c) 2003-2015, International Business Machines
7 * Corporation and others.  All Rights Reserved.
8 **********************************************************************
9 * Author: Alan Liu
10 * Created: March 8 2003
11 * Since: ICU 2.6
12 **********************************************************************
13 */
14 package ohos.global.icu.dev.test.util;
15 
16 import java.util.Arrays;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Random;
20 import java.util.Set;
21 
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.junit.runners.JUnit4;
25 
26 import ohos.global.icu.dev.test.TestFmwk;
27 import ohos.global.icu.impl.Assert;
28 import ohos.global.icu.impl.InvalidFormatException;
29 import ohos.global.icu.impl.Utility;
30 import ohos.global.icu.text.UnicodeSet;
31 import ohos.global.icu.util.ByteArrayWrapper;
32 import ohos.global.icu.util.CaseInsensitiveString;
33 
34 
35 /**
36  * @test
37  * @summary Test of internal Utility class
38  */
39 
40 @RunWith(JUnit4.class)
41 public class UtilityTest extends TestFmwk {
42     @Test
TestUnescape()43     public void TestUnescape() {
44         final String input =
45             "Sch\\u00f6nes Auto: \\u20ac 11240.\\fPrivates Zeichen: \\U00102345\\e\\cC\\n \\x1b\\x{263a}";
46 
47         final String expect =
48             "Sch\u00F6nes Auto: \u20AC 11240.\u000CPrivates Zeichen: \uDBC8\uDF45\u001B\u0003\012 \u001B\u263A";
49 
50         String result = Utility.unescape(input);
51         if (!result.equals(expect)) {
52             errln("FAIL: Utility.unescape() returned " + result + ", exp. " + expect);
53         }
54     }
55 
56     @Test
TestFormat()57     public void TestFormat()
58     {
59         String data[] = {
60             "the quick brown fox jumps over the lazy dog",
61             // result of this conversion will exceed the original length and
62             // cause a newline to be inserted
63             "testing space , quotations \"",
64             "testing weird supplementary characters \ud800\udc00",
65             "testing control characters \u0001 and line breaking!! \n are we done yet?"
66         };
67         String result[] = {
68             "        \"the quick brown fox jumps over the lazy dog\"",
69             "        \"testing space , quotations \\042\"",
70             "        \"testing weird supplementary characters \\uD800\\uDC00\"",
71             "        \"testing control characters \\001 and line breaking!! \\n are we done ye\"+"
72                      + Utility.LINE_SEPARATOR + "        \"t?\""
73         };
74         String result1[] = {
75             "\"the quick brown fox jumps over the lazy dog\"",
76             "\"testing space , quotations \\042\"",
77             "\"testing weird supplementary characters \\uD800\\uDC00\"",
78             "\"testing control characters \\001 and line breaking!! \\n are we done yet?\""
79         };
80 
81         for (int i = 0; i < data.length; i ++) {
82             assertEquals("formatForSource(\"" + data[i] + "\")",
83                          result[i], Utility.formatForSource(data[i]));
84         }
85         for (int i = 0; i < data.length; i ++) {
86             assertEquals("format1ForSource(\"" + data[i] + "\")",
87                          result1[i], Utility.format1ForSource(data[i]));
88         }
89     }
90 
91     @Test
TestHighBit()92     public void TestHighBit()
93     {
94         int data[] = {-1, -1276, 0, 0xFFFF, 0x1234};
95         byte result[] = {-1, -1, -1, 15, 12};
96         for (int i = 0; i < data.length; i ++) {
97             if (Utility.highBit(data[i]) != result[i]) {
98                 errln("Fail: Highest bit of \\u"
99                       + Integer.toHexString(data[i]) + " should be "
100                       + result[i]);
101             }
102         }
103     }
104 
105     @Test
TestCompareUnsigned()106     public void TestCompareUnsigned()
107     {
108         int data[] = {0, 1, 0x8fffffff, -1, Integer.MAX_VALUE,
109                       Integer.MIN_VALUE, 2342423, -2342423};
110         for (int i = 0; i < data.length; i ++) {
111             for (int j = 0; j < data.length; j ++) {
112                 if (Utility.compareUnsigned(data[i], data[j])
113                     != compareLongUnsigned(data[i], data[j])) {
114                     errln("Fail: Unsigned comparison failed with " + data[i]
115                           + " " + data[i + 1]);
116                 }
117             }
118         }
119     }
120 
121     // This test indends to test the utility class ByteArrayWrapper
122     // Seems that the class is somewhat incomplete, for example
123     //      - getHashCode(Object) is weird
124     //      - PatternMatch feature(search part of array within the whole one) lacks
125     @Test
TestByteArrayWrapper()126     public void TestByteArrayWrapper()
127     {
128         byte[] ba = {0x00, 0x01, 0x02};
129         byte[] bb = {0x00, 0x01, 0x02, -1};
130 
131         java.nio.ByteBuffer buffer = java.nio.ByteBuffer.wrap(ba);
132         ByteArrayWrapper x = new ByteArrayWrapper(buffer);
133 
134         ByteArrayWrapper y = new ByteArrayWrapper(ba, 3);
135         ByteArrayWrapper z = new ByteArrayWrapper(bb, 3);
136 
137 
138         if (!y.toString().equals("00 01 02")){
139             errln("FAIL: test toString : Failed!");
140         }
141 
142         // test equality
143         assertEquals("x==y", x, y);
144         assertEquals("y==z", y, z);
145         assertEquals("x.hashCode()==y.hashCode()", x.hashCode(), y.hashCode());
146         assertEquals("y.hashCode()==z.hashCode()", y.hashCode(), z.hashCode());
147 
148         // test non-equality
149         y = new ByteArrayWrapper(bb, 4);
150         if (x.equals(y))
151             errln("FAIL: test (operator !=): Failed!");
152 
153         // test sign of unequal comparison
154         if ((x.compareTo(y) > 0) != (y.compareTo(x) < 0)) {
155             errln("FAIL: comparisons not opposite sign");
156         }
157     }
158 
compareLongUnsigned(int x, int y)159     private int compareLongUnsigned(int x, int y)
160     {
161         long x1 = x & 0xFFFFFFFFl;
162         long y1 = y & 0xFFFFFFFFl;
163         if (x1 < y1) {
164             return -1;
165         }
166         else if (x1 > y1) {
167             return 1;
168         }
169         return 0;
170     }
171     @Test
TestUnicodeSet()172     public void TestUnicodeSet(){
173         String[] array = new String[]{"a", "b", "c", "{de}"};
174         List list = Arrays.asList(array);
175         Set aset = new HashSet(list);
176         logln(" *** The source set's size is: " + aset.size());
177     //The size reads 4
178         UnicodeSet set = new UnicodeSet();
179         set.clear();
180         set.addAll(aset);
181         logln(" *** After addAll, the UnicodeSet size is: " + set.size());
182     //The size should also read 4, but 0 is seen instead
183 
184     }
185 
186     @Test
TestAssert()187     public void TestAssert(){
188         try {
189             Assert.assrt(false);
190             errln("FAIL: Assert.assrt(false)");
191         }
192         catch (IllegalStateException e) {
193             if (e.getMessage().equals("assert failed")) {
194                 logln("Assert.assrt(false) works");
195             }
196             else {
197                 errln("FAIL: Assert.assrt(false) returned " + e.getMessage());
198             }
199         }
200         try {
201             Assert.assrt("Assert message", false);
202             errln("FAIL: Assert.assrt(false)");
203         }
204         catch (IllegalStateException e) {
205             if (e.getMessage().equals("assert 'Assert message' failed")) {
206                 logln("Assert.assrt(false) works");
207             }
208             else {
209                 errln("FAIL: Assert.assrt(false) returned " + e.getMessage());
210             }
211         }
212         try {
213             Assert.fail("Assert message");
214             errln("FAIL: Assert.fail");
215         }
216         catch (IllegalStateException e) {
217             if (e.getMessage().equals("failure 'Assert message'")) {
218                 logln("Assert.fail works");
219             }
220             else {
221                 errln("FAIL: Assert.fail returned " + e.getMessage());
222             }
223         }
224         try {
225             Assert.fail(new InvalidFormatException());
226             errln("FAIL: Assert.fail with an exception");
227         }
228         catch (IllegalStateException e) {
229             logln("Assert.fail works");
230         }
231     }
232 
233     @Test
TestCaseInsensitiveString()234     public void TestCaseInsensitiveString() {
235         CaseInsensitiveString str1 = new CaseInsensitiveString("ThIs is A tEst");
236         CaseInsensitiveString str2 = new CaseInsensitiveString("This IS a test");
237         if (!str1.equals(str2)
238             || !str1.toString().equals(str1.getString())
239             || str1.toString().equals(str2.toString()))
240         {
241             errln("FAIL: str1("+str1+") != str2("+str2+")");
242         }
243     }
244 
245     @Test
TestSourceLocation()246     public void TestSourceLocation() {
247         String here = TestFmwk.sourceLocation();
248         String there = CheckSourceLocale();
249         String hereAgain = TestFmwk.sourceLocation();
250         assertTrue("here < there < hereAgain", here.compareTo(there) < 0 && there.compareTo(hereAgain) < 0);
251     }
252 
CheckSourceLocale()253     public String CheckSourceLocale() {
254         return TestFmwk.sourceLocation();
255     }
256 
257     static final String RANDOM_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
258     static final Random RANDOM = new Random(2018);
259 
260     @Test
TestCharSequenceEqualsAndHashCode()261     public void TestCharSequenceEqualsAndHashCode() {
262         for (int t=0; t<1000; t++) {
263             int length = RANDOM.nextInt(5);
264             CharSequence a = randomCharSequence(length);
265             CharSequence b = randomCharSequence(length);
266             CharSequence c = randomCharSequence(length + 3);
267             String message = "a=" + a + "; b=" + b + "; c=" + c;
268 
269             assertTrue(message, Utility.charSequenceEquals(a, a));
270             assertFalse(message, Utility.charSequenceEquals(a, c));
271             assertTrue(message, Utility.charSequenceEquals(b, b));
272             assertFalse(message, Utility.charSequenceEquals(b, c));
273             assertFalse(message, Utility.charSequenceEquals(c, a));
274             assertFalse(message, Utility.charSequenceEquals(c, b));
275             assertTrue(message, Utility.charSequenceEquals(c, c));
276             if (length == 0 || a.toString().equals(b.toString())) {
277                 assertTrue(message, Utility.charSequenceEquals(a, b));
278                 assertTrue(message, Utility.charSequenceEquals(b, a));
279             } else {
280                 assertFalse(message, Utility.charSequenceEquals(a, b));
281                 assertFalse(message, Utility.charSequenceEquals(b, a));
282             }
283 
284             assertEquals(message, Utility.charSequenceHashCode(a), a.toString().hashCode());
285             assertEquals(message, Utility.charSequenceHashCode(b), b.toString().hashCode());
286             assertEquals(message, Utility.charSequenceHashCode(c), c.toString().hashCode());
287         }
288     }
289 
randomCharSequence(int length)290     private CharSequence randomCharSequence(int length) {
291         StringBuilder sb = new StringBuilder();
292         for (int i=0; i<length; i++) {
293             sb.append(RANDOM_CHARS.charAt(RANDOM.nextInt(RANDOM_CHARS.length())));
294         }
295         return sb;
296     }
297 }
298