~ubuntu-branches/ubuntu/vivid/icu4j-4.4/vivid

« back to all changes in this revision

Viewing changes to main/tests/core/src/com/ibm/icu/dev/test/util/UtilityTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Niels Thykier
  • Date: 2011-08-02 15:50:33 UTC
  • Revision ID: james.westby@ubuntu.com-20110802155033-itjzsl21y2lqdonn
Tags: upstream-4.4.2
ImportĀ upstreamĀ versionĀ 4.4.2

Show diffs side-by-side

added added

removed removed

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