]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/lang/UCharacterSurrogateTest.java
Added flags.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / lang / UCharacterSurrogateTest.java
1 /**
2  *******************************************************************************
3  * Copyright (C) 2004-2008, International Business Machines Corporation and         *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7
8 package com.ibm.icu.dev.test.lang;
9
10 import com.ibm.icu.dev.test.TestFmwk;
11 import com.ibm.icu.impl.Utility;
12 import com.ibm.icu.lang.UCharacter;
13 import com.ibm.icu.text.UTF16;
14
15 /**
16  * Test JDK 1.5 cover APIs.
17  */
18 public final class UCharacterSurrogateTest extends TestFmwk {
19
20     public static void main(String[] args) {
21         new UCharacterSurrogateTest().run(args);
22     }
23
24     public void TestUnicodeBlockForName() {
25       String[] names = {"Latin-1 Supplement", 
26                         "Optical Character Recognition",
27                         "CJK Unified Ideographs Extension A", 
28                         "Supplemental Arrows-B",
29                         "Supplemental arrows b", 
30                         "supp-lement-al arrowsb",
31                         "Supplementary Private Use Area-B",
32                         "supplementary_Private_Use_Area-b",
33                         "supplementary_PRIVATE_Use_Area_b"};
34         for (int i = 0; i < names.length; ++i) {
35             try {
36                 UCharacter.UnicodeBlock b = UCharacter.UnicodeBlock
37                         .forName(names[i]);
38                 logln("found: " + b + " for name: " + names[i]);
39             } catch (Exception e) {
40                 errln("could not find block for name: " + names[i]);
41                 break;
42             }
43         }
44     }
45
46     public void TestIsValidCodePoint() {
47         if (UCharacter.isValidCodePoint(-1))
48             errln("-1");
49         if (!UCharacter.isValidCodePoint(0))
50             errln("0");
51         if (!UCharacter.isValidCodePoint(UCharacter.MAX_CODE_POINT))
52             errln("0x10ffff");
53         if (UCharacter.isValidCodePoint(UCharacter.MAX_CODE_POINT + 1))
54             errln("0x110000");
55     }
56
57     public void TestIsSupplementaryCodePoint() {
58         if (UCharacter.isSupplementaryCodePoint(-1))
59             errln("-1");
60         if (UCharacter.isSupplementaryCodePoint(0))
61             errln("0");
62         if (UCharacter
63                 .isSupplementaryCodePoint(UCharacter.MIN_SUPPLEMENTARY_CODE_POINT - 1))
64             errln("0xffff");
65         if (!UCharacter
66                 .isSupplementaryCodePoint(UCharacter.MIN_SUPPLEMENTARY_CODE_POINT))
67             errln("0x10000");
68         if (!UCharacter.isSupplementaryCodePoint(UCharacter.MAX_CODE_POINT))
69             errln("0x10ffff");
70         if (UCharacter.isSupplementaryCodePoint(UCharacter.MAX_CODE_POINT + 1))
71             errln("0x110000");
72     }
73
74     public void TestIsHighSurrogate() {
75         if (UCharacter
76                 .isHighSurrogate((char) (UCharacter.MIN_HIGH_SURROGATE - 1)))
77             errln("0xd7ff");
78         if (!UCharacter.isHighSurrogate(UCharacter.MIN_HIGH_SURROGATE))
79             errln("0xd800");
80         if (!UCharacter.isHighSurrogate(UCharacter.MAX_HIGH_SURROGATE))
81             errln("0xdbff");
82         if (UCharacter
83                 .isHighSurrogate((char) (UCharacter.MAX_HIGH_SURROGATE + 1)))
84             errln("0xdc00");
85     }
86
87     public void TestIsLowSurrogate() {
88         if (UCharacter
89                 .isLowSurrogate((char) (UCharacter.MIN_LOW_SURROGATE - 1)))
90             errln("0xdbff");
91         if (!UCharacter.isLowSurrogate(UCharacter.MIN_LOW_SURROGATE))
92             errln("0xdc00");
93         if (!UCharacter.isLowSurrogate(UCharacter.MAX_LOW_SURROGATE))
94             errln("0xdfff");
95         if (UCharacter
96                 .isLowSurrogate((char) (UCharacter.MAX_LOW_SURROGATE + 1)))
97             errln("0xe000");
98     }
99
100     public void TestIsSurrogatePair() {
101         if (UCharacter.isSurrogatePair(
102                 (char) (UCharacter.MIN_HIGH_SURROGATE - 1),
103                 UCharacter.MIN_LOW_SURROGATE))
104             errln("0xd7ff,0xdc00");
105         if (UCharacter.isSurrogatePair(
106                 (char) (UCharacter.MAX_HIGH_SURROGATE + 1),
107                 UCharacter.MIN_LOW_SURROGATE))
108             errln("0xd800,0xdc00");
109         if (UCharacter.isSurrogatePair(UCharacter.MIN_HIGH_SURROGATE,
110                 (char) (UCharacter.MIN_LOW_SURROGATE - 1)))
111             errln("0xd800,0xdbff");
112         if (UCharacter.isSurrogatePair(UCharacter.MIN_HIGH_SURROGATE,
113                 (char) (UCharacter.MAX_LOW_SURROGATE + 1)))
114             errln("0xd800,0xe000");
115         if (!UCharacter.isSurrogatePair(UCharacter.MIN_HIGH_SURROGATE,
116                 UCharacter.MIN_LOW_SURROGATE))
117             errln("0xd800,0xdc00");
118     }
119
120     public void TestCharCount() {
121         UCharacter.charCount(-1);
122         UCharacter.charCount(UCharacter.MAX_CODE_POINT + 1);
123         if (UCharacter.charCount(UCharacter.MIN_SUPPLEMENTARY_CODE_POINT - 1) != 1)
124             errln("0xffff");
125         if (UCharacter.charCount(UCharacter.MIN_SUPPLEMENTARY_CODE_POINT) != 2)
126             errln("0x010000");
127     }
128
129     public void TestToCodePoint() {
130         final char[] pairs = {(char) (UCharacter.MIN_HIGH_SURROGATE + 0),
131                 (char) (UCharacter.MIN_LOW_SURROGATE + 0),
132                 (char) (UCharacter.MIN_HIGH_SURROGATE + 1),
133                 (char) (UCharacter.MIN_LOW_SURROGATE + 1),
134                 (char) (UCharacter.MIN_HIGH_SURROGATE + 2),
135                 (char) (UCharacter.MIN_LOW_SURROGATE + 2),
136                 (char) (UCharacter.MAX_HIGH_SURROGATE - 2),
137                 (char) (UCharacter.MAX_LOW_SURROGATE - 2),
138                 (char) (UCharacter.MAX_HIGH_SURROGATE - 1),
139                 (char) (UCharacter.MAX_LOW_SURROGATE - 1),
140                 (char) (UCharacter.MAX_HIGH_SURROGATE - 0),
141                 (char) (UCharacter.MAX_LOW_SURROGATE - 0),};
142         for (int i = 0; i < pairs.length; i += 2) {
143             int cp = UCharacter.toCodePoint(pairs[i], pairs[i + 1]);
144             if (pairs[i] != UTF16.getLeadSurrogate(cp)
145                     || pairs[i + 1] != UTF16.getTrailSurrogate(cp)) {
146
147                 errln(Integer.toHexString(pairs[i]) + ", " + pairs[i + 1]);
148                 break;
149             }
150         }
151     }
152
153     public void TestCodePointAtBefore() {
154         String s = "" + UCharacter.MIN_HIGH_SURROGATE + // isolated high
155                 UCharacter.MIN_HIGH_SURROGATE + // pair
156                 UCharacter.MIN_LOW_SURROGATE + UCharacter.MIN_LOW_SURROGATE; // isolated
157                                                                              // low
158         char[] c = s.toCharArray();
159         int[] avalues = {
160                 UCharacter.MIN_HIGH_SURROGATE,
161                 UCharacter.toCodePoint(UCharacter.MIN_HIGH_SURROGATE,
162                         UCharacter.MIN_LOW_SURROGATE),
163                 UCharacter.MIN_LOW_SURROGATE, UCharacter.MIN_LOW_SURROGATE};
164         int[] bvalues = {
165                 UCharacter.MIN_HIGH_SURROGATE,
166                 UCharacter.MIN_HIGH_SURROGATE,
167                 UCharacter.toCodePoint(UCharacter.MIN_HIGH_SURROGATE,
168                         UCharacter.MIN_LOW_SURROGATE),
169                 UCharacter.MIN_LOW_SURROGATE,};
170         StringBuffer b = new StringBuffer(s);
171         for (int i = 0; i < avalues.length; ++i) {
172             if (UCharacter.codePointAt(s, i) != avalues[i])
173                 errln("string at: " + i);
174             if (UCharacter.codePointAt(c, i) != avalues[i])
175                 errln("chars at: " + i);
176             if (UCharacter.codePointAt(b, i) != avalues[i])
177                 errln("stringbuffer at: " + i);
178
179             if (UCharacter.codePointBefore(s, i + 1) != bvalues[i])
180                 errln("string before: " + i);
181             if (UCharacter.codePointBefore(c, i + 1) != bvalues[i])
182                 errln("chars before: " + i);
183             if (UCharacter.codePointBefore(b, i + 1) != bvalues[i])
184                 errln("stringbuffer before: " + i);
185         }
186
187         //cover codePointAtBefore with limit
188         logln("Testing codePointAtBefore with limit ...");
189         for (int i = 0; i < avalues.length; ++i) {
190             if (UCharacter.codePointAt(c, i, 4) != avalues[i])
191                 errln("chars at: " + i);
192             if (UCharacter.codePointBefore(c, i + 1, 0) != bvalues[i])
193                 errln("chars before: " + i);
194         }
195
196     }
197
198     public void TestToChars() {
199         char[] chars = new char[3];
200         int cp = UCharacter.toCodePoint(UCharacter.MIN_HIGH_SURROGATE,
201                 UCharacter.MIN_LOW_SURROGATE);
202         UCharacter.toChars(cp, chars, 1);
203         if (chars[1] != UCharacter.MIN_HIGH_SURROGATE
204                 || chars[2] != UCharacter.MIN_LOW_SURROGATE) {
205
206             errln("fail");
207         }
208
209         chars = UCharacter.toChars(cp);
210         if (chars[0] != UCharacter.MIN_HIGH_SURROGATE
211                 || chars[1] != UCharacter.MIN_LOW_SURROGATE) {
212
213             errln("fail");
214         }
215     }
216
217     public void TestCodePointCount() {
218         class Test {
219             String str(String s, int start, int limit) {
220                 if(s==null){
221                     s="";
222                 }
223                 return "codePointCount('" + Utility.escape(s) + "' " + start
224                         + ", " + limit + ")";
225             }
226
227             void test(String s, int start, int limit, int expected) {
228                 int val1 = UCharacter.codePointCount(s.toCharArray(), start,
229                         limit);
230                 int val2 = UCharacter.codePointCount(s, start, limit);
231                 if (val1 != expected) {
232                     errln("char[] " + str(s, start, limit) + "(" + val1
233                             + ") != " + expected);
234                 } else if (val2 != expected) {
235                     errln("String " + str(s, start, limit) + "(" + val2
236                             + ") != " + expected);
237                 } else if (isVerbose()) {
238                     logln(str(s, start, limit) + " == " + expected);
239                 }
240             }
241
242             void fail(String s, int start, int limit, Class exc) {
243                 try {
244                     UCharacter.codePointCount(s, start, limit);
245                     errln("unexpected success " + str(s, start, limit));
246                 } catch (Throwable e) {
247                     if (!exc.isInstance(e)) {
248                         warnln("bad exception " + str(s, start, limit)
249                                 + e.getClass().getName());
250                     }
251                 }
252             }
253         }
254
255         Test test = new Test();
256         test.fail(null, 0, 1, NullPointerException.class);
257         test.fail("a", -1, 0, IndexOutOfBoundsException.class);
258         test.fail("a", 1, 2, IndexOutOfBoundsException.class);
259         test.fail("a", 1, 0, IndexOutOfBoundsException.class);
260         test.test("", 0, 0, 0);
261         test.test("\ud800", 0, 1, 1);
262         test.test("\udc00", 0, 1, 1);
263         test.test("\ud800\udc00", 0, 1, 1);
264         test.test("\ud800\udc00", 1, 2, 1);
265         test.test("\ud800\udc00", 0, 2, 1);
266         test.test("\udc00\ud800", 0, 1, 1);
267         test.test("\udc00\ud800", 1, 2, 1);
268         test.test("\udc00\ud800", 0, 2, 2);
269         test.test("\ud800\ud800\udc00", 0, 2, 2);
270         test.test("\ud800\ud800\udc00", 1, 3, 1);
271         test.test("\ud800\ud800\udc00", 0, 3, 2);
272         test.test("\ud800\udc00\udc00", 0, 2, 1);
273         test.test("\ud800\udc00\udc00", 1, 3, 2);
274         test.test("\ud800\udc00\udc00", 0, 3, 2);
275     }
276
277     public void TestOffsetByCodePoints() {
278         class Test {
279             String str(String s, int start, int count, int index, int offset) {
280                 return "offsetByCodePoints('" + Utility.escape(s) + "' "
281                         + start + ", " + count + ", " + index + ", " + offset
282                         + ")";
283             }
284
285             void test(String s, int start, int count, int index, int offset,
286                     int expected, boolean flip) {
287                 char[] chars = s.toCharArray();
288                 String string = s.substring(start, start + count);
289                 int val1 = UCharacter.offsetByCodePoints(chars, start, count,
290                         index, offset);
291                 int val2 = UCharacter.offsetByCodePoints(string, index - start,
292                         offset)
293                         + start;
294
295                 if (val1 != expected) {
296                     errln("char[] " + str(s, start, count, index, offset) + "("
297                             + val1 + ") != " + expected);
298                 } else if (val2 != expected) {
299                     errln("String " + str(s, start, count, index, offset) + "("
300                             + val2 + ") != " + expected);
301                 } else if (isVerbose()) {
302                     logln(str(s, start, count, index, offset) + " == "
303                             + expected);
304                 }
305
306                 if (flip) {
307                     val1 = UCharacter.offsetByCodePoints(chars, start, count,
308                             expected, -offset);
309                     val2 = UCharacter.offsetByCodePoints(string, expected
310                             - start, -offset)
311                             + start;
312                     if (val1 != index) {
313                         errln("char[] "
314                                 + str(s, start, count, expected, -offset) + "("
315                                 + val1 + ") != " + index);
316                     } else if (val2 != index) {
317                         errln("String "
318                                 + str(s, start, count, expected, -offset) + "("
319                                 + val2 + ") != " + index);
320                     } else if (isVerbose()) {
321                         logln(str(s, start, count, expected, -offset) + " == "
322                                 + index);
323                     }
324                 }
325             }
326
327             void fail(char[] text, int start, int count, int index, int offset,
328                     Class exc) {
329                 try {
330                     UCharacter.offsetByCodePoints(text, start, count, index,
331                             offset);
332                     errln("unexpected success "
333                             + str(new String(text), start, count, index, offset));
334                 } catch (Throwable e) {
335                     if (!exc.isInstance(e)) {
336                         errln("bad exception "
337                                 + str(new String(text), start, count, index,
338                                         offset) + e.getClass().getName());
339                     }
340                 }
341             }
342
343             void fail(String text, int index, int offset, Class exc) {
344                 try {
345                     UCharacter.offsetByCodePoints(text, index, offset);
346                     errln("unexpected success "
347                             + str(text, index, offset, 0, text.length()));
348                 } catch (Throwable e) {
349                     if (!exc.isInstance(e)) {
350                         errln("bad exception "
351                                 + str(text, 0, text.length(), index, offset)
352                                 + e.getClass().getName());
353                     }
354                 }
355             }
356         }
357
358         Test test = new Test();
359
360         test.test("\ud800\ud800\udc00", 0, 2, 0, 1, 1, true);
361
362         test.fail((char[]) null, 0, 1, 0, 1, NullPointerException.class);
363         test.fail((String) null, 0, 1, NullPointerException.class);
364         test.fail("abc", -1, 0, IndexOutOfBoundsException.class);
365         test.fail("abc", 4, 0, IndexOutOfBoundsException.class);
366         test.fail("abc", 1, -2, IndexOutOfBoundsException.class);
367         test.fail("abc", 2, 2, IndexOutOfBoundsException.class);
368         char[] abc = "abc".toCharArray();
369         test.fail(abc, -1, 2, 0, 0, IndexOutOfBoundsException.class);
370         test.fail(abc, 2, 2, 3, 0, IndexOutOfBoundsException.class);
371         test.fail(abc, 1, -1, 0, 0, IndexOutOfBoundsException.class);
372         test.fail(abc, 1, 1, 2, -2, IndexOutOfBoundsException.class);
373         test.fail(abc, 1, 1, 1, 2, IndexOutOfBoundsException.class);
374         test.fail(abc, 1, 2, 1, 3, IndexOutOfBoundsException.class);
375         test.fail(abc, 0, 2, 2, -3, IndexOutOfBoundsException.class);
376         test.test("", 0, 0, 0, 0, 0, false);
377         test.test("\ud800", 0, 1, 0, 1, 1, true);
378         test.test("\udc00", 0, 1, 0, 1, 1, true);
379
380         String s = "\ud800\udc00";
381         test.test(s, 0, 1, 0, 1, 1, true);
382         test.test(s, 0, 2, 0, 1, 2, true);
383         test.test(s, 0, 2, 1, 1, 2, false);
384         test.test(s, 1, 1, 1, 1, 2, true);
385
386         s = "\udc00\ud800";
387         test.test(s, 0, 1, 0, 1, 1, true);
388         test.test(s, 0, 2, 0, 1, 1, true);
389         test.test(s, 0, 2, 0, 2, 2, true);
390         test.test(s, 0, 2, 1, 1, 2, true);
391         test.test(s, 1, 1, 1, 1, 2, true);
392
393         s = "\ud800\ud800\udc00";
394         test.test(s, 0, 1, 0, 1, 1, true);
395         test.test(s, 0, 2, 0, 1, 1, true);
396         test.test(s, 0, 2, 0, 2, 2, true);
397         test.test(s, 0, 2, 1, 1, 2, true);
398         test.test(s, 0, 3, 0, 1, 1, true);
399         test.test(s, 0, 3, 0, 2, 3, true);
400         test.test(s, 0, 3, 1, 1, 3, true);
401         test.test(s, 0, 3, 2, 1, 3, false);
402         test.test(s, 1, 1, 1, 1, 2, true);
403         test.test(s, 1, 2, 1, 1, 3, true);
404         test.test(s, 1, 2, 2, 1, 3, false);
405         test.test(s, 2, 1, 2, 1, 3, true);
406
407         s = "\ud800\udc00\udc00";
408         test.test(s, 0, 1, 0, 1, 1, true);
409         test.test(s, 0, 2, 0, 1, 2, true);
410         test.test(s, 0, 2, 1, 1, 2, false);
411         test.test(s, 0, 3, 0, 1, 2, true);
412         test.test(s, 0, 3, 0, 2, 3, true);
413         test.test(s, 0, 3, 1, 1, 2, false);
414         test.test(s, 0, 3, 1, 2, 3, false);
415         test.test(s, 0, 3, 2, 1, 3, true);
416         test.test(s, 1, 1, 1, 1, 2, true);
417         test.test(s, 1, 2, 1, 1, 2, true);
418         test.test(s, 1, 2, 1, 2, 3, true);
419         test.test(s, 1, 2, 2, 1, 3, true);
420         test.test(s, 2, 1, 2, 1, 3, true);
421     }
422 }