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