001: /**
002: *******************************************************************************
003: * Copyright (C) 2004-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */package com.ibm.icu.dev.test.lang;
007:
008: import com.ibm.icu.dev.test.TestFmwk;
009: import com.ibm.icu.impl.Utility;
010: import com.ibm.icu.lang.UCharacter;
011: import com.ibm.icu.text.UTF16;
012:
013: /**
014: * Test JDK 1.5 cover APIs.
015: */
016: public final class UCharacterSurrogateTest extends TestFmwk {
017:
018: public static void main(String[] args) {
019: new UCharacterSurrogateTest().run(args);
020: }
021:
022: public void TestUnicodeBlockForName() {
023: String[] names = { "Optical Character Recognition",
024: "CJK Unified Ideographs Extension A",
025: "Supplemental Arrows-B",
026: "Supplementary Private Use Area-B",
027: "supplementary_Private_Use_Area-b",
028: "supplementary_PRIVATE_Use_Area_b" };
029: for (int i = 0; i < names.length; ++i) {
030: try {
031: UCharacter.UnicodeBlock b = UCharacter.UnicodeBlock
032: .forName(names[i]);
033: logln("found: " + b + " for name: " + names[i]);
034: } catch (Exception e) {
035: errln("could not find block for name: " + names[i]);
036: break;
037: }
038: }
039: }
040:
041: public void TestIsValidCodePoint() {
042: if (UCharacter.isValidCodePoint(-1))
043: errln("-1");
044: if (!UCharacter.isValidCodePoint(0))
045: errln("0");
046: if (!UCharacter.isValidCodePoint(UCharacter.MAX_CODE_POINT))
047: errln("0x10ffff");
048: if (UCharacter.isValidCodePoint(UCharacter.MAX_CODE_POINT + 1))
049: errln("0x110000");
050: }
051:
052: public void TestIsSupplementaryCodePoint() {
053: if (UCharacter.isSupplementaryCodePoint(-1))
054: errln("-1");
055: if (UCharacter.isSupplementaryCodePoint(0))
056: errln("0");
057: if (UCharacter
058: .isSupplementaryCodePoint(UCharacter.MIN_SUPPLEMENTARY_CODE_POINT - 1))
059: errln("0xffff");
060: if (!UCharacter
061: .isSupplementaryCodePoint(UCharacter.MIN_SUPPLEMENTARY_CODE_POINT))
062: errln("0x10000");
063: if (!UCharacter
064: .isSupplementaryCodePoint(UCharacter.MAX_CODE_POINT))
065: errln("0x10ffff");
066: if (UCharacter
067: .isSupplementaryCodePoint(UCharacter.MAX_CODE_POINT + 1))
068: errln("0x110000");
069: }
070:
071: public void TestIsHighSurrogate() {
072: if (UCharacter
073: .isHighSurrogate((char) (UCharacter.MIN_HIGH_SURROGATE - 1)))
074: errln("0xd7ff");
075: if (!UCharacter.isHighSurrogate(UCharacter.MIN_HIGH_SURROGATE))
076: errln("0xd800");
077: if (!UCharacter.isHighSurrogate(UCharacter.MAX_HIGH_SURROGATE))
078: errln("0xdbff");
079: if (UCharacter
080: .isHighSurrogate((char) (UCharacter.MAX_HIGH_SURROGATE + 1)))
081: errln("0xdc00");
082: }
083:
084: public void TestIsLowSurrogate() {
085: if (UCharacter
086: .isLowSurrogate((char) (UCharacter.MIN_LOW_SURROGATE - 1)))
087: errln("0xdbff");
088: if (!UCharacter.isLowSurrogate(UCharacter.MIN_LOW_SURROGATE))
089: errln("0xdc00");
090: if (!UCharacter.isLowSurrogate(UCharacter.MAX_LOW_SURROGATE))
091: errln("0xdfff");
092: if (UCharacter
093: .isLowSurrogate((char) (UCharacter.MAX_LOW_SURROGATE + 1)))
094: errln("0xe000");
095: }
096:
097: public void TestIsSurrogatePair() {
098: if (UCharacter.isSurrogatePair(
099: (char) (UCharacter.MIN_HIGH_SURROGATE - 1),
100: UCharacter.MIN_LOW_SURROGATE))
101: errln("0xd7ff,0xdc00");
102: if (UCharacter.isSurrogatePair(
103: (char) (UCharacter.MAX_HIGH_SURROGATE + 1),
104: UCharacter.MIN_LOW_SURROGATE))
105: errln("0xd800,0xdc00");
106: if (UCharacter.isSurrogatePair(UCharacter.MIN_HIGH_SURROGATE,
107: (char) (UCharacter.MIN_LOW_SURROGATE - 1)))
108: errln("0xd800,0xdbff");
109: if (UCharacter.isSurrogatePair(UCharacter.MIN_HIGH_SURROGATE,
110: (char) (UCharacter.MAX_LOW_SURROGATE + 1)))
111: errln("0xd800,0xe000");
112: if (!UCharacter.isSurrogatePair(UCharacter.MIN_HIGH_SURROGATE,
113: UCharacter.MIN_LOW_SURROGATE))
114: errln("0xd800,0xdc00");
115: }
116:
117: public void TestCharCount() {
118: UCharacter.charCount(-1);
119: UCharacter.charCount(UCharacter.MAX_CODE_POINT + 1);
120: if (UCharacter
121: .charCount(UCharacter.MIN_SUPPLEMENTARY_CODE_POINT - 1) != 1)
122: errln("0xffff");
123: if (UCharacter
124: .charCount(UCharacter.MIN_SUPPLEMENTARY_CODE_POINT) != 2)
125: errln("0x010000");
126: }
127:
128: public void TestToCodePoint() {
129: final char[] pairs = {
130: (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]) + ", "
148: + pairs[i + 1]);
149: break;
150: }
151: }
152: }
153:
154: public void TestCodePointAtBefore() {
155: String s = "" + UCharacter.MIN_HIGH_SURROGATE
156: + // isolated high
157: UCharacter.MIN_HIGH_SURROGATE
158: + // pair
159: UCharacter.MIN_LOW_SURROGATE
160: + UCharacter.MIN_LOW_SURROGATE; // isolated
161: // low
162: char[] c = s.toCharArray();
163: int[] avalues = {
164: UCharacter.MIN_HIGH_SURROGATE,
165: UCharacter.toCodePoint(UCharacter.MIN_HIGH_SURROGATE,
166: UCharacter.MIN_LOW_SURROGATE),
167: UCharacter.MIN_LOW_SURROGATE,
168: UCharacter.MIN_LOW_SURROGATE };
169: int[] bvalues = {
170: UCharacter.MIN_HIGH_SURROGATE,
171: UCharacter.MIN_HIGH_SURROGATE,
172: UCharacter.toCodePoint(UCharacter.MIN_HIGH_SURROGATE,
173: UCharacter.MIN_LOW_SURROGATE),
174: UCharacter.MIN_LOW_SURROGATE, };
175: StringBuffer b = new StringBuffer(s);
176: for (int i = 0; i < avalues.length; ++i) {
177: if (UCharacter.codePointAt(s, i) != avalues[i])
178: errln("string at: " + i);
179: if (UCharacter.codePointAt(c, i) != avalues[i])
180: errln("chars at: " + i);
181: if (UCharacter.codePointAt(b, i) != avalues[i])
182: errln("stringbuffer at: " + i);
183:
184: if (UCharacter.codePointBefore(s, i + 1) != bvalues[i])
185: errln("string before: " + i);
186: if (UCharacter.codePointBefore(c, i + 1) != bvalues[i])
187: errln("chars before: " + i);
188: if (UCharacter.codePointBefore(b, i + 1) != bvalues[i])
189: errln("stringbuffer before: " + i);
190: }
191:
192: //cover codePointAtBefore with limit
193: logln("Testing codePointAtBefore with limit ...");
194: for (int i = 0; i < avalues.length; ++i) {
195: if (UCharacter.codePointAt(c, i, 4) != avalues[i])
196: errln("chars at: " + i);
197: if (UCharacter.codePointBefore(c, i + 1, 0) != bvalues[i])
198: errln("chars before: " + i);
199: }
200:
201: }
202:
203: public void TestToChars() {
204: char[] chars = new char[3];
205: int cp = UCharacter.toCodePoint(UCharacter.MIN_HIGH_SURROGATE,
206: UCharacter.MIN_LOW_SURROGATE);
207: UCharacter.toChars(cp, chars, 1);
208: if (chars[1] != UCharacter.MIN_HIGH_SURROGATE
209: || chars[2] != UCharacter.MIN_LOW_SURROGATE) {
210:
211: errln("fail");
212: }
213:
214: chars = UCharacter.toChars(cp);
215: if (chars[0] != UCharacter.MIN_HIGH_SURROGATE
216: || chars[1] != UCharacter.MIN_LOW_SURROGATE) {
217:
218: errln("fail");
219: }
220: }
221:
222: public void TestCodePointCount() {
223: class Test {
224: String str(String s, int start, int limit) {
225: if (s == null) {
226: s = "";
227: }
228: return "codePointCount('" + Utility.escape(s) + "' "
229: + start + ", " + limit + ")";
230: }
231:
232: void test(String s, int start, int limit, int expected) {
233: int val1 = UCharacter.codePointCount(s.toCharArray(),
234: start, limit);
235: int val2 = UCharacter.codePointCount(s, start, limit);
236: if (val1 != expected) {
237: errln("char[] " + str(s, start, limit) + "(" + val1
238: + ") != " + expected);
239: } else if (val2 != expected) {
240: errln("String " + str(s, start, limit) + "(" + val2
241: + ") != " + expected);
242: } else if (isVerbose()) {
243: logln(str(s, start, limit) + " == " + expected);
244: }
245: }
246:
247: void fail(String s, int start, int limit, Class exc) {
248: try {
249: UCharacter.codePointCount(s, start, limit);
250: errln("unexpected success " + str(s, start, limit));
251: } catch (Throwable e) {
252: if (!exc.isInstance(e)) {
253: warnln("bad exception " + str(s, start, limit)
254: + e.getClass().getName());
255: }
256: }
257: }
258: }
259:
260: Test test = new Test();
261: test.fail(null, 0, 1, NullPointerException.class);
262: test.fail("a", -1, 0, IndexOutOfBoundsException.class);
263: test.fail("a", 1, 2, IndexOutOfBoundsException.class);
264: test.fail("a", 1, 0, IndexOutOfBoundsException.class);
265: test.test("", 0, 0, 0);
266: test.test("\ud800", 0, 1, 1);
267: test.test("\udc00", 0, 1, 1);
268: test.test("\ud800\udc00", 0, 1, 1);
269: test.test("\ud800\udc00", 1, 2, 1);
270: test.test("\ud800\udc00", 0, 2, 1);
271: test.test("\udc00\ud800", 0, 1, 1);
272: test.test("\udc00\ud800", 1, 2, 1);
273: test.test("\udc00\ud800", 0, 2, 2);
274: test.test("\ud800\ud800\udc00", 0, 2, 2);
275: test.test("\ud800\ud800\udc00", 1, 3, 1);
276: test.test("\ud800\ud800\udc00", 0, 3, 2);
277: test.test("\ud800\udc00\udc00", 0, 2, 1);
278: test.test("\ud800\udc00\udc00", 1, 3, 2);
279: test.test("\ud800\udc00\udc00", 0, 3, 2);
280: }
281:
282: public void TestOffsetByCodePoints() {
283: class Test {
284: String str(String s, int start, int count, int index,
285: int offset) {
286: return "offsetByCodePoints('" + Utility.escape(s)
287: + "' " + start + ", " + count + ", " + index
288: + ", " + offset + ")";
289: }
290:
291: void test(String s, int start, int count, int index,
292: int offset, int expected, boolean flip) {
293: char[] chars = s.toCharArray();
294: String string = s.substring(start, start + count);
295: int val1 = UCharacter.offsetByCodePoints(chars, start,
296: count, index, offset);
297: int val2 = UCharacter.offsetByCodePoints(string, index
298: - start, offset)
299: + start;
300:
301: if (val1 != expected) {
302: errln("char[] "
303: + str(s, start, count, index, offset) + "("
304: + val1 + ") != " + expected);
305: } else if (val2 != expected) {
306: errln("String "
307: + str(s, start, count, index, offset) + "("
308: + val2 + ") != " + expected);
309: } else if (isVerbose()) {
310: logln(str(s, start, count, index, offset) + " == "
311: + expected);
312: }
313:
314: if (flip) {
315: val1 = UCharacter.offsetByCodePoints(chars, start,
316: count, expected, -offset);
317: val2 = UCharacter.offsetByCodePoints(string,
318: expected - start, -offset)
319: + start;
320: if (val1 != index) {
321: errln("char[] "
322: + str(s, start, count, expected,
323: -offset) + "(" + val1 + ") != "
324: + index);
325: } else if (val2 != index) {
326: errln("String "
327: + str(s, start, count, expected,
328: -offset) + "(" + val2 + ") != "
329: + index);
330: } else if (isVerbose()) {
331: logln(str(s, start, count, expected, -offset)
332: + " == " + index);
333: }
334: }
335: }
336:
337: void fail(char[] text, int start, int count, int index,
338: int offset, Class exc) {
339: try {
340: UCharacter.offsetByCodePoints(text, start, count,
341: index, offset);
342: errln("unexpected success "
343: + str(new String(text), start, count,
344: index, offset));
345: } catch (Throwable e) {
346: if (!exc.isInstance(e)) {
347: errln("bad exception "
348: + str(new String(text), start, count,
349: index, offset)
350: + e.getClass().getName());
351: }
352: }
353: }
354:
355: void fail(String text, int index, int offset, Class exc) {
356: try {
357: UCharacter.offsetByCodePoints(text, index, offset);
358: errln("unexpected success "
359: + str(text, index, offset, 0, text.length()));
360: } catch (Throwable e) {
361: if (!exc.isInstance(e)) {
362: errln("bad exception "
363: + str(text, 0, text.length(), index,
364: offset)
365: + e.getClass().getName());
366: }
367: }
368: }
369: }
370:
371: Test test = new Test();
372:
373: test.test("\ud800\ud800\udc00", 0, 2, 0, 1, 1, true);
374:
375: test
376: .fail((char[]) null, 0, 1, 0, 1,
377: NullPointerException.class);
378: test.fail((String) null, 0, 1, NullPointerException.class);
379: test.fail("abc", -1, 0, IndexOutOfBoundsException.class);
380: test.fail("abc", 4, 0, IndexOutOfBoundsException.class);
381: test.fail("abc", 1, -2, IndexOutOfBoundsException.class);
382: test.fail("abc", 2, 2, IndexOutOfBoundsException.class);
383: char[] abc = "abc".toCharArray();
384: test.fail(abc, -1, 2, 0, 0, IndexOutOfBoundsException.class);
385: test.fail(abc, 2, 2, 3, 0, IndexOutOfBoundsException.class);
386: test.fail(abc, 1, -1, 0, 0, IndexOutOfBoundsException.class);
387: test.fail(abc, 1, 1, 2, -2, IndexOutOfBoundsException.class);
388: test.fail(abc, 1, 1, 1, 2, IndexOutOfBoundsException.class);
389: test.fail(abc, 1, 2, 1, 3, IndexOutOfBoundsException.class);
390: test.fail(abc, 0, 2, 2, -3, IndexOutOfBoundsException.class);
391: test.test("", 0, 0, 0, 0, 0, false);
392: test.test("\ud800", 0, 1, 0, 1, 1, true);
393: test.test("\udc00", 0, 1, 0, 1, 1, true);
394:
395: String s = "\ud800\udc00";
396: test.test(s, 0, 1, 0, 1, 1, true);
397: test.test(s, 0, 2, 0, 1, 2, true);
398: test.test(s, 0, 2, 1, 1, 2, false);
399: test.test(s, 1, 1, 1, 1, 2, true);
400:
401: s = "\udc00\ud800";
402: test.test(s, 0, 1, 0, 1, 1, true);
403: test.test(s, 0, 2, 0, 1, 1, true);
404: test.test(s, 0, 2, 0, 2, 2, true);
405: test.test(s, 0, 2, 1, 1, 2, true);
406: test.test(s, 1, 1, 1, 1, 2, true);
407:
408: s = "\ud800\ud800\udc00";
409: test.test(s, 0, 1, 0, 1, 1, true);
410: test.test(s, 0, 2, 0, 1, 1, true);
411: test.test(s, 0, 2, 0, 2, 2, true);
412: test.test(s, 0, 2, 1, 1, 2, true);
413: test.test(s, 0, 3, 0, 1, 1, true);
414: test.test(s, 0, 3, 0, 2, 3, true);
415: test.test(s, 0, 3, 1, 1, 3, true);
416: test.test(s, 0, 3, 2, 1, 3, false);
417: test.test(s, 1, 1, 1, 1, 2, true);
418: test.test(s, 1, 2, 1, 1, 3, true);
419: test.test(s, 1, 2, 2, 1, 3, false);
420: test.test(s, 2, 1, 2, 1, 3, true);
421:
422: s = "\ud800\udc00\udc00";
423: test.test(s, 0, 1, 0, 1, 1, true);
424: test.test(s, 0, 2, 0, 1, 2, true);
425: test.test(s, 0, 2, 1, 1, 2, false);
426: test.test(s, 0, 3, 0, 1, 2, true);
427: test.test(s, 0, 3, 0, 2, 3, true);
428: test.test(s, 0, 3, 1, 1, 2, false);
429: test.test(s, 0, 3, 1, 2, 3, false);
430: test.test(s, 0, 3, 2, 1, 3, true);
431: test.test(s, 1, 1, 1, 1, 2, true);
432: test.test(s, 1, 2, 1, 1, 2, true);
433: test.test(s, 1, 2, 1, 2, 3, true);
434: test.test(s, 1, 2, 2, 1, 3, true);
435: test.test(s, 2, 1, 2, 1, 3, true);
436: }
437: }
|