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