2 *******************************************************************************
3 * Copyright (C) 2004-2008, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 package com.ibm.icu.dev.test.lang;
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;
16 * Test JDK 1.5 cover APIs.
18 public final class UCharacterSurrogateTest extends TestFmwk {
20 public static void main(String[] args) {
21 new UCharacterSurrogateTest().run(args);
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) {
36 UCharacter.UnicodeBlock b = UCharacter.UnicodeBlock
38 logln("found: " + b + " for name: " + names[i]);
39 } catch (Exception e) {
40 errln("could not find block for name: " + names[i]);
46 public void TestIsValidCodePoint() {
47 if (UCharacter.isValidCodePoint(-1))
49 if (!UCharacter.isValidCodePoint(0))
51 if (!UCharacter.isValidCodePoint(UCharacter.MAX_CODE_POINT))
53 if (UCharacter.isValidCodePoint(UCharacter.MAX_CODE_POINT + 1))
57 public void TestIsSupplementaryCodePoint() {
58 if (UCharacter.isSupplementaryCodePoint(-1))
60 if (UCharacter.isSupplementaryCodePoint(0))
63 .isSupplementaryCodePoint(UCharacter.MIN_SUPPLEMENTARY_CODE_POINT - 1))
66 .isSupplementaryCodePoint(UCharacter.MIN_SUPPLEMENTARY_CODE_POINT))
68 if (!UCharacter.isSupplementaryCodePoint(UCharacter.MAX_CODE_POINT))
70 if (UCharacter.isSupplementaryCodePoint(UCharacter.MAX_CODE_POINT + 1))
74 public void TestIsHighSurrogate() {
76 .isHighSurrogate((char) (UCharacter.MIN_HIGH_SURROGATE - 1)))
78 if (!UCharacter.isHighSurrogate(UCharacter.MIN_HIGH_SURROGATE))
80 if (!UCharacter.isHighSurrogate(UCharacter.MAX_HIGH_SURROGATE))
83 .isHighSurrogate((char) (UCharacter.MAX_HIGH_SURROGATE + 1)))
87 public void TestIsLowSurrogate() {
89 .isLowSurrogate((char) (UCharacter.MIN_LOW_SURROGATE - 1)))
91 if (!UCharacter.isLowSurrogate(UCharacter.MIN_LOW_SURROGATE))
93 if (!UCharacter.isLowSurrogate(UCharacter.MAX_LOW_SURROGATE))
96 .isLowSurrogate((char) (UCharacter.MAX_LOW_SURROGATE + 1)))
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");
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)
125 if (UCharacter.charCount(UCharacter.MIN_SUPPLEMENTARY_CODE_POINT) != 2)
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)) {
147 errln(Integer.toHexString(pairs[i]) + ", " + pairs[i + 1]);
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
158 char[] c = s.toCharArray();
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};
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);
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);
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);
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) {
209 chars = UCharacter.toChars(cp);
210 if (chars[0] != UCharacter.MIN_HIGH_SURROGATE
211 || chars[1] != UCharacter.MIN_LOW_SURROGATE) {
217 public void TestCodePointCount() {
219 String str(String s, int start, int limit) {
223 return "codePointCount('" + Utility.escape(s) + "' " + start
224 + ", " + limit + ")";
227 void test(String s, int start, int limit, int expected) {
228 int val1 = UCharacter.codePointCount(s.toCharArray(), start,
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);
242 void fail(String s, int start, int limit, Class exc) {
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());
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);
277 public void TestOffsetByCodePoints() {
279 String str(String s, int start, int count, int index, int offset) {
280 return "offsetByCodePoints('" + Utility.escape(s) + "' "
281 + start + ", " + count + ", " + index + ", " + offset
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,
291 int val2 = UCharacter.offsetByCodePoints(string, index - start,
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) + " == "
307 val1 = UCharacter.offsetByCodePoints(chars, start, count,
309 val2 = UCharacter.offsetByCodePoints(string, expected
314 + str(s, start, count, expected, -offset) + "("
315 + val1 + ") != " + index);
316 } else if (val2 != index) {
318 + str(s, start, count, expected, -offset) + "("
319 + val2 + ") != " + index);
320 } else if (isVerbose()) {
321 logln(str(s, start, count, expected, -offset) + " == "
327 void fail(char[] text, int start, int count, int index, int offset,
330 UCharacter.offsetByCodePoints(text, start, count, index,
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());
343 void fail(String text, int index, int offset, Class exc) {
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());
358 Test test = new Test();
360 test.test("\ud800\ud800\udc00", 0, 2, 0, 1, 1, true);
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);
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);
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);
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);
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);