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