1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
|
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4533872 4985214 4985217 4993841 5017268 5017280
* @summary Unit tests for supplementary character support (JSR-204)
* @compile Supplementary.java
* @run main/timeout=600 Supplementary
*/
public class Supplementary {
private static final char MIN_HIGH = '\uD800';
private static final char MAX_HIGH = '\uDBFF';
private static final char MIN_LOW = MAX_HIGH + 1;
private static final char MAX_LOW = '\uDFFF';
private static final int MIN_CODE_POINT = 0x000000;
private static final int MIN_SUPPLEMENTARY = 0x010000;
private static final int MAX_SUPPLEMENTARY = 0x10ffff;
public static void main(String[] args) {
// Do not change the order of test method calls since there
// are some interdependencies.
testConstants();
test00();
// Store all Unicode code points, except for surrogate code
// points, in cu[] through the loops below. Then, use the data
// for code point/code unit conversion and other tests later.
char[] cu = new char[(MAX_SUPPLEMENTARY+1) * 2];
int length = test01(cu);
String str = new String(cu, 0, length);
cu = null;
test02(str);
test03(str.toCharArray());
test04(str);
test05(str);
// Test for toString(int)
test06();
// Test unpaired surrogates
testUnpaired();
// Test exceptions
testExceptions00();
testExceptions01(str);
testExceptions02(str.toCharArray());
}
static void testConstants() {
if (Character.MIN_HIGH_SURROGATE != MIN_HIGH) {
constantError("MIN_HIGH_SURROGATE", Character.MIN_HIGH_SURROGATE, MIN_HIGH);
}
if (Character.MAX_HIGH_SURROGATE != MAX_HIGH) {
constantError("MAX_HIGH_SURROGATE", Character.MAX_HIGH_SURROGATE, MAX_HIGH);
}
if (Character.MIN_LOW_SURROGATE != MIN_LOW) {
constantError("MIN_LOW_SURROGATE", Character.MIN_LOW_SURROGATE, MIN_LOW);
}
if (Character.MAX_LOW_SURROGATE != MAX_LOW) {
constantError("MAX_LOW_SURROGATE", Character.MAX_LOW_SURROGATE, MAX_LOW);
}
if (Character.MIN_SURROGATE != MIN_HIGH) {
constantError("MIN_SURROGATE", Character.MIN_SURROGATE, MIN_HIGH);
}
if (Character.MAX_SURROGATE != MAX_LOW) {
constantError("MAX_SURROGATE", Character.MAX_SURROGATE, MAX_LOW);
}
if (Character.MIN_SUPPLEMENTARY_CODE_POINT != MIN_SUPPLEMENTARY) {
constantError("MIN_SUPPLEMENTARY_CODE_POINT",
Character.MIN_SUPPLEMENTARY_CODE_POINT, MIN_SUPPLEMENTARY);
}
if (Character.MIN_CODE_POINT != MIN_CODE_POINT) {
constantError("MIN_CODE_POINT", Character.MIN_CODE_POINT, MIN_CODE_POINT);
}
if (Character.MAX_CODE_POINT != MAX_SUPPLEMENTARY) {
constantError("MAX_CODE_POINT", Character.MAX_CODE_POINT, MAX_SUPPLEMENTARY);
}
}
static void constantError(String name, int value, int expectedValue) {
throw new RuntimeException("Character." + name + " has a wrong value: got "
+ toHexString(value)
+ ", expected " + toHexString(expectedValue));
}
/*
* Test isValidCodePoint(int)
* isSupplementaryCodePoint(int)
* charCount(int)
*/
static void test00() {
for (int cp = -MAX_SUPPLEMENTARY; cp <= MAX_SUPPLEMENTARY*2; cp++) {
boolean isValid = cp >= 0 && cp <= MAX_SUPPLEMENTARY;
if (Character.isValidCodePoint(cp) != isValid) {
throw new RuntimeException("isValidCodePoint failed with "
+ toHexString(cp));
}
boolean isSupplementary = cp >= MIN_SUPPLEMENTARY && cp <= MAX_SUPPLEMENTARY;
if (Character.isSupplementaryCodePoint(cp) != isSupplementary) {
throw new RuntimeException("isSupplementaryCodePoint failed with "
+ toHexString(cp));
}
int len = Character.charCount(cp);
if (isValid) {
if ((isSupplementary && len != 2)
|| (!isSupplementary && len != 1)) {
throw new RuntimeException("wrong character length "+len+" for "
+ toHexString(cp));
}
} else if (len != 1 && len != 2) {
throw new RuntimeException("wrong character length "+len+" for "
+ toHexString(cp));
}
}
}
/**
* Test toChar(int)
* toChar(int, char[], int)
* isHighSurrogate(char)
* isLowSurrogate(char)
* isSurrogatePair(int, int)
*
* While testing those methods, this method generates all Unicode
* code points (except for surrogate code points) and store them
* in cu.
*
* @return the number of code units generated in cu
*/
static int test01(char[] cu) {
int index = 0;
// Test toChar(int)
// toChar(int, char[], int)
// isHighSurrogate(char)
// isLowSurrogate(char)
// with BMP code points
for (int i = 0; i <= Character.MAX_VALUE; i++) {
char[] u = Character.toChars(i);
if (u.length != 1 || u[0] != i) {
throw new RuntimeException("wrong toChars(int) result for BMP: "
+ toHexString("u", u));
}
int n = Character.toChars(i, cu, index);
if (n != 1 || cu[index] != i) {
throw new RuntimeException("wrong toChars(int, char[], int) result for BMP:"
+ " len=" + n
+ ", cu["+index+"]="+toHexString(cu[index]));
}
boolean isHigh = i >= MIN_HIGH && i <= MAX_HIGH;
if (Character.isHighSurrogate((char) i) != isHigh) {
throw new RuntimeException("wrong high-surrogate test for "
+ toHexString(i));
}
boolean isLow = i >= MIN_LOW && i <= MAX_LOW;
if (Character.isLowSurrogate((char)i) != isLow) {
throw new RuntimeException("wrong low-surrogate test for "
+ toHexString(i));
}
if (!isHigh && !isLow) {
index++;
}
}
// Test isSurrogatePair with all surrogate pairs
// Test toChars(int)
// toChars(int, char[], int)
// with all supplementary characters
int supplementary = MIN_SUPPLEMENTARY;
for (int i = Character.MAX_VALUE/2; i <= Character.MAX_VALUE; i++) {
char hi = (char) i;
boolean isHigh = Character.isHighSurrogate(hi);
for (int j = Character.MAX_VALUE/2; j <= Character.MAX_VALUE; j++) {
char lo = (char) j;
boolean isLow = Character.isLowSurrogate(lo);
boolean isSurrogatePair = isHigh && isLow;
if (Character.isSurrogatePair(hi, lo) != isSurrogatePair) {
throw new RuntimeException("wrong surrogate pair test for hi="
+ toHexString(hi)
+ ", lo="+toHexString(lo));
}
if (isSurrogatePair) {
int cp = Character.toCodePoint(hi, lo);
if (cp != supplementary) {
throw new RuntimeException("wrong code point: got "
+ toHexString(cp)
+ ", expected="
+ toHexString(supplementary));
}
char[] u = Character.toChars(cp);
if (u.length != 2 || u[0] != hi || u[1] != lo) {
throw new RuntimeException("wrong toChars(int) result for supplementary: "+
toHexString("u", u));
}
int n = Character.toChars(cp, cu, index);
if (n != 2 || cu[index] != hi || cu[index+1] != lo) {
throw new RuntimeException("wrong toChars(int, char[], int) result "
+ "for supplementary: len=" + n
+ ", cu["+index+"]=" + toHexString(cu[index])
+ ", cu["+(index+1)+"]=" + toHexString(cu[index+1]));
}
index += n;
supplementary++;
}
}
}
if (supplementary != MAX_SUPPLEMENTARY + 1) {
throw new RuntimeException("wrong supplementary count (supplementary="
+ toHexString(supplementary)+")");
}
int nCodeUnits = Character.MAX_VALUE + 1 - (MAX_LOW - MIN_HIGH + 1)
+ ((MAX_SUPPLEMENTARY - MIN_SUPPLEMENTARY + 1) * 2);
if (index != nCodeUnits) {
throw new RuntimeException("wrong number of code units: " + index
+ ", expected " + nCodeUnits);
}
return index;
}
/**
* Test codePointAt(CharSequence, int)
* codePointBefore(CharSequence, int)
*/
static void test02(CharSequence cs) {
int cp = 0;
int ch;
for (int i = 0; i < cs.length(); i += Character.charCount(ch)) {
ch = Character.codePointAt(cs, i);
if (ch != cp) {
throw new RuntimeException("wrong codePointAt(CharSequence, "+i+") value: got "
+ toHexString(ch)
+ ", expected "+toHexString(cp));
}
cp++;
// Skip surrogates
if (cp == MIN_HIGH) {
cp = MAX_LOW + 1;
}
}
cp--;
for (int i = cs.length(); i > 0; i -= Character.charCount(ch)) {
ch = Character.codePointBefore(cs, i);
if (ch != cp) {
throw new RuntimeException("codePointBefore(CharSequence, "+i+") returned "
+ toHexString(ch)
+ ", expected " + toHexString(cp));
}
cp--;
// Skip surrogates
if (cp == MAX_LOW) {
cp = MIN_HIGH - 1;
}
}
}
/**
* Test codePointAt(char[], int)
* codePointAt(char[], int, int)
* codePointBefore(char[], int)
* codePointBefore(char[], int, int)
*/
static void test03(char[] a) {
int cp = 0;
int ch;
for (int i = 0; i < a.length; i += Character.charCount(ch)) {
ch = Character.codePointAt(a, i);
if (ch != cp) {
throw new RuntimeException("codePointAt(char[], "+i+") returned "
+ toHexString(ch)
+ ", expected "+toHexString(cp));
}
int x = Character.codePointAt(a, i, i+1);
if (x != a[i]) {
throw new RuntimeException(String.format(
"codePointAt(char[], %d, %d) returned 0x%04x, expected 0x%04x\n",
i, i+1, x, (int)a[i]));
}
cp++;
// Skip surrogates
if (cp == MIN_HIGH) {
cp = MAX_LOW + 1;
}
}
cp--;
for (int i = a.length; i > 0; i -= Character.charCount(ch)) {
ch = Character.codePointBefore(a, i);
if (ch != cp) {
throw new RuntimeException("codePointBefore(char[], "+i+") returned "
+ toHexString(ch)
+ ", expected " + toHexString(cp));
}
int x = Character.codePointBefore(a, i, i-1);
if (x != a[i-1]) {
throw new RuntimeException(String.format(
"codePointAt(char[], %d, %d) returned 0x%04x, expected 0x%04x\n",
i, i-1, x, (int)a[i-1]));
}
cp--;
// Skip surrogates
if (cp == MAX_LOW) {
cp = MIN_HIGH - 1;
}
}
}
/**
* Test codePointCount(CharSequence, int, int)
* codePointCount(char[], int, int, int, int)
*/
static void test04(String str) {
int length = str.length();
char[] a = str.toCharArray();
for (int i = 0; i <= length; i += 99, length -= 29999) {
int n = Character.codePointCount(str, i, length);
int m = codePointCount(str.substring(i, length));
checkCodePointCount(str, n, m);
n = Character.codePointCount(a, i, length - i);
checkCodePointCount(a, n, m);
}
// test special cases
length = str.length();
int n = Character.codePointCount(str, 0, 0);
checkCodePointCount(str, n, 0);
n = Character.codePointCount(str, length, length);
checkCodePointCount(str, n, 0);
n = Character.codePointCount(a, 0, 0);
checkCodePointCount(a, n, 0);
n = Character.codePointCount(a, length, 0);
checkCodePointCount(a, n, 0);
}
// This method assumes that Character.codePointAt() and
// Character.charCount() work correctly.
private static int codePointCount(CharSequence seq) {
int n = 0, len;
for (int i = 0; i < seq.length(); i += len) {
int codepoint = Character.codePointAt(seq, i);
n++;
len = Character.charCount(codepoint);
}
return n;
}
private static void checkCodePointCount(Object data, int n, int expected) {
String type = getType(data);
if (n != expected) {
throw new RuntimeException("codePointCount(" + type + "...) returned " + n
+ ", expected " + expected);
}
}
/**
* Test offsetByCodePoints(CharSequence, int, int)
* offsetByCodePoints(char[], int, int, int, int)
*
* This test case assumes that Character.codePointCount()s work
* correctly.
*/
static void test05(String str) {
int length = str.length();
char[] a = str.toCharArray();
for (int i = 0; i <= length; i += 99, length -= 29999) {
int nCodePoints = Character.codePointCount(a, i, length - i);
int index;
// offsetByCodePoints(CharSequence, int, int)
int expectedHighIndex = length;
// For forward CharSequence scan, we need to adjust the
// expected index in case the last char in the text range
// is a high surrogate and forms a valid supplementary
// code point with the next char.
if (length < a.length) {
int cp = Character.codePointAt(a, length - 1);
if (Character.isSupplementaryCodePoint(cp)) {
expectedHighIndex++;
}
}
index = Character.offsetByCodePoints(str, i, nCodePoints);
checkNewIndex(str, nCodePoints, index, expectedHighIndex);
int expectedLowIndex = i;
if (i > 0) {
int cp = Character.codePointBefore(a, i + 1);
if (Character.isSupplementaryCodePoint(cp)) {
expectedLowIndex--;
}
}
index = Character.offsetByCodePoints(str, length, -nCodePoints);
checkNewIndex(str, -nCodePoints, index, expectedLowIndex);
// offsetByCodePoints(char[], int, int, int, int)
int start = Math.max(0, i-1);
int limit = Math.min(a.length, length+1);
index = Character.offsetByCodePoints(a, start, limit - start,
i, nCodePoints);
checkNewIndex(a, nCodePoints, index, expectedHighIndex);
if (length != expectedHighIndex) {
index = Character.offsetByCodePoints(a, start, length - start,
i, nCodePoints);
checkNewIndex(a, nCodePoints, index, length);
}
index = Character.offsetByCodePoints(a, start, limit - start,
length, -nCodePoints);
checkNewIndex(a, -nCodePoints, index, expectedLowIndex);
if (i != expectedLowIndex) {
index = Character.offsetByCodePoints(a, i, limit - i,
length, -nCodePoints);
checkNewIndex(a, -nCodePoints, index, i);
}
}
// test special cases for 0-length text ranges.
length = str.length();
int index = Character.offsetByCodePoints(str, 0, 0);
checkNewIndex(str, 0, index, 0);
index = Character.offsetByCodePoints(str, length, 0);
checkNewIndex(str, 0, index, length);
index = Character.offsetByCodePoints(a, 0, 0, 0, 0);
checkNewIndex(a, 0, index, 0);
index = Character.offsetByCodePoints(a, 0, length, 0, 0);
checkNewIndex(a, 0, index, 0);
index = Character.offsetByCodePoints(a, 0, length, length, 0);
checkNewIndex(a, 0, index, length);
index = Character.offsetByCodePoints(a, length, 0, length, 0);
checkNewIndex(a, 0, index, length);
}
/**
* Test toString(int)
*
* This test case assumes that Character.toChars()/String(char[]) work
* correctly.
*/
static void test06() {
for (int cp = Character.MIN_CODE_POINT; cp <= Character.MAX_CODE_POINT; cp++) {
String result = Character.toString(cp);
String expected = new String(Character.toChars(cp));
if (!result.equals(expected)) {
throw new RuntimeException("Wrong string is created. code point: " +
cp + ", result: " + result + ", expected: " + expected);
}
}
}
private static void checkNewIndex(Object data, int offset, int result, int expected) {
String type = getType(data);
String offsetType = (offset > 0) ? "positive" : (offset < 0) ? "negative" : "0";
if (result != expected) {
throw new RuntimeException("offsetByCodePoints(" + type + ", ...) ["
+ offsetType + " offset]"
+ " returned " + result
+ ", expected " + expected);
}
}
// Test codePointAt(CharSequence, int)
// codePointBefore(CharSequence, int)
// codePointAt(char[], int)
// codePointBefore(char[], int)
// toChar(int)
// toChar(int, char[], int)
// with unpaired surrogates
static void testUnpaired() {
testCodePoint("\uD800", new int[] { 0xD800 });
testCodePoint("\uDC00", new int[] { 0xDC00 });
testCodePoint("a\uD800", new int[] { 'a', 0xD800 });
testCodePoint("a\uDC00", new int[] { 'a', 0xDC00 });
testCodePoint("\uD800a", new int[] { 0xD800, 'a' });
testCodePoint("\uDBFFa", new int[] { 0xDBFF, 'a' });
testCodePoint("a\uD800\uD801", new int[] { 'a', 0xD800, 0xD801 });
testCodePoint("a\uD800x\uDC00", new int[] { 'a', 0xD800, 'x', 0xDC00 });
testCodePoint("\uDC00\uD800", new int[] { 0xDC00, 0xD800 });
testCodePoint("\uD800\uDC00\uDC00", new int[] { 0x10000, 0xDC00 });
testCodePoint("\uD800\uD800\uDC00", new int[] { 0xD800, 0x10000 });
testCodePoint("\uD800\uD800\uD800\uD800\uDC00\uDC00\uDC00\uDC00",
new int[] { 0xD800, 0xD800, 0xD800, 0x10000, 0xDC00, 0xDC00, 0xDC00});
}
static void testCodePoint(String str, int[] codepoints) {
int c;
// Test Character.codePointAt/Before(CharSequence, int)
int j = 0;
for (int i = 0; i < str.length(); i += Character.charCount(c)) {
c = Character.codePointAt(str, i);
if (c != codepoints[j++]) {
throw new RuntimeException("codePointAt(CharSequence, " + i + ") returned "
+ toHexString(c)
+ ", expected " + toHexString(codepoints[j-1]));
}
}
if (j != codepoints.length) {
throw new RuntimeException("j != codepoints.length after codePointAt(CharSequence, int)"
+ " (j=" + j + ")"
+ ", expected: " + codepoints.length);
}
j = codepoints.length;
for (int i = str.length(); i > 0 ; i -= Character.charCount(c)) {
c = Character.codePointBefore(str, i);
if (c != codepoints[--j]) {
throw new RuntimeException("codePointBefore(CharSequence, " + i + ") returned "
+ toHexString(c)
+ ", expected " + toHexString(codepoints[j]));
}
}
if (j != 0) {
throw new RuntimeException("j != 0 after codePointBefore(CharSequence, int)"
+ " (j=" + j + ")");
}
// Test Character.codePointAt/Before(char[], int)
char[] a = str.toCharArray();
j = 0;
for (int i = 0; i < a.length; i += Character.charCount(c)) {
c = Character.codePointAt(a, i);
if (c != codepoints[j++]) {
throw new RuntimeException("codePointAt(char[], " + i + ") returned "
+ toHexString(c)
+ ", expected " + toHexString(codepoints[j-1]));
}
}
if (j != codepoints.length) {
throw new RuntimeException("j != codepoints.length after codePointAt(char[], int)"
+ " (j=" + j + ")"
+ ", expected: " + codepoints.length);
}
j = codepoints.length;
for (int i = a.length; i > 0 ; i -= Character.charCount(c)) {
c = Character.codePointBefore(a, i);
if (c != codepoints[--j]) {
throw new RuntimeException("codePointBefore(char[], " + i + ") returned "
+ toHexString(c)
+ ", expected " + toHexString(codepoints[j]));
}
}
if (j != 0) {
throw new RuntimeException("j != 0 after codePointBefore(char[], int)"
+ " (j=" + j + ")");
}
// Test toChar(int)
j = 0;
for (int i = 0; i < codepoints.length; i++) {
a = Character.toChars(codepoints[i]);
for (int k = 0; k < a.length; k++) {
if (str.charAt(j++) != a[k]) {
throw new RuntimeException("toChars(int) returned " + toHexString("result", a)
+ " from codepoint=" + toHexString(codepoints[i]));
}
}
}
// Test toChars(int, char[], int)
a = new char[codepoints.length * 2];
j = 0;
for (int i = 0; i < codepoints.length; i++) {
int n = Character.toChars(codepoints[i], a, j);
j += n;
}
String s = new String(a, 0, j);
if (!str.equals(s)) {
throw new RuntimeException("toChars(int, char[], int) returned "
+ toHexString("dst", s.toCharArray())
+ ", expected " + toHexString("data", str.toCharArray()));
}
}
// Test toChar(int)
// toChar(int, char[], int)
// toString(int)
// for exceptions
static void testExceptions00() {
callToChars1(-1, IllegalArgumentException.class);
callToChars1(MAX_SUPPLEMENTARY + 1, IllegalArgumentException.class);
callToChars3(MAX_SUPPLEMENTARY, null, 0, NullPointerException.class);
callToChars3(-MIN_SUPPLEMENTARY, new char[2], 0, IllegalArgumentException.class);
callToChars3(MAX_SUPPLEMENTARY + 1, new char[2], 0, IllegalArgumentException.class);
callToChars3('A', new char[0], 0, IndexOutOfBoundsException.class);
callToChars3('A', new char[1], -1, IndexOutOfBoundsException.class);
callToChars3('A', new char[1], 1, IndexOutOfBoundsException.class);
callToChars3(MIN_SUPPLEMENTARY, new char[0], 0, IndexOutOfBoundsException.class);
callToChars3(MIN_SUPPLEMENTARY, new char[1], 0, IndexOutOfBoundsException.class);
callToChars3(MIN_SUPPLEMENTARY, new char[2], -1, IndexOutOfBoundsException.class);
callToChars3(MIN_SUPPLEMENTARY, new char[2], 1, IndexOutOfBoundsException.class);
callToString(Character.MIN_CODE_POINT - 1, IllegalArgumentException.class);
callToString(Character.MAX_CODE_POINT + 1, IllegalArgumentException.class);
}
static final boolean At = true, Before = false;
/**
* Test codePointAt(CharSequence, int)
* codePointBefore(CharSequence, int)
* codePointCount(CharSequence, int, int)
* offsetByCodePoints(CharSequence, int, int)
* for exceptions
*/
static void testExceptions01(CharSequence cs) {
CharSequence nullSeq = null;
// codePointAt
callCodePoint(At, nullSeq, 0, NullPointerException.class);
callCodePoint(At, cs, -1, IndexOutOfBoundsException.class);
callCodePoint(At, cs, cs.length(), IndexOutOfBoundsException.class);
callCodePoint(At, cs, cs.length()*3, IndexOutOfBoundsException.class);
// codePointBefore
callCodePoint(Before, nullSeq, 0, NullPointerException.class);
callCodePoint(Before, cs, -1, IndexOutOfBoundsException.class);
callCodePoint(Before, cs, 0, IndexOutOfBoundsException.class);
callCodePoint(Before, cs, cs.length()+1, IndexOutOfBoundsException.class);
// codePointCount
callCodePointCount(nullSeq, 0, 0, NullPointerException.class);
callCodePointCount(cs, -1, 1, IndexOutOfBoundsException.class);
callCodePointCount(cs, 0, cs.length()+1, IndexOutOfBoundsException.class);
callCodePointCount(cs, 3, 1, IndexOutOfBoundsException.class);
// offsetByCodePoints
callOffsetByCodePoints(nullSeq, 0, 0, NullPointerException.class);
callOffsetByCodePoints(cs, -1, 1, IndexOutOfBoundsException.class);
callOffsetByCodePoints(cs, cs.length()+1, 1, IndexOutOfBoundsException.class);
callOffsetByCodePoints(cs, 0, cs.length()*2, IndexOutOfBoundsException.class);
callOffsetByCodePoints(cs, cs.length(), 1, IndexOutOfBoundsException.class);
callOffsetByCodePoints(cs, 0, -1, IndexOutOfBoundsException.class);
callOffsetByCodePoints(cs, cs.length(), -cs.length()*2,
IndexOutOfBoundsException.class);
callOffsetByCodePoints(cs, cs.length(), Integer.MIN_VALUE,
IndexOutOfBoundsException.class);
callOffsetByCodePoints(cs, 0, Integer.MAX_VALUE, IndexOutOfBoundsException.class);
}
/**
* Test codePointAt(char[], int)
* codePointAt(char[], int, int)
* codePointBefore(char[], int)
* codePointBefore(char[], int, int)
* codePointCount(char[], int, int)
* offsetByCodePoints(char[], int, int, int, int)
* for exceptions
*/
static void testExceptions02(char[] a) {
char[] nullArray = null;
callCodePoint(At, nullArray, 0, NullPointerException.class);
callCodePoint(At, a, -1, IndexOutOfBoundsException.class);
callCodePoint(At, a, a.length, IndexOutOfBoundsException.class);
callCodePoint(At, a, a.length*3, IndexOutOfBoundsException.class);
callCodePoint(Before, nullArray, 0, NullPointerException.class);
callCodePoint(Before, a, -1, IndexOutOfBoundsException.class);
callCodePoint(Before, a, 0, IndexOutOfBoundsException.class);
callCodePoint(Before, a, a.length+1, IndexOutOfBoundsException.class);
// tests for the methods with limit
callCodePoint(At, nullArray, 0, 1, NullPointerException.class);
callCodePoint(At, a, 0, -1, IndexOutOfBoundsException.class);
callCodePoint(At, a, 0, 0, IndexOutOfBoundsException.class);
callCodePoint(At, a, 0, a.length+1, IndexOutOfBoundsException.class);
callCodePoint(At, a, 2, 1, IndexOutOfBoundsException.class);
callCodePoint(At, a, -1, 1, IndexOutOfBoundsException.class);
callCodePoint(At, a, a.length, 1, IndexOutOfBoundsException.class);
callCodePoint(At, a, a.length*3, 1, IndexOutOfBoundsException.class);
callCodePoint(Before, nullArray, 1, 0, NullPointerException.class);
callCodePoint(Before, a, 2, -1, IndexOutOfBoundsException.class);
callCodePoint(Before, a, 2, 2, IndexOutOfBoundsException.class);
callCodePoint(Before, a, 2, 3, IndexOutOfBoundsException.class);
callCodePoint(Before, a, 2, a.length, IndexOutOfBoundsException.class);
callCodePoint(Before, a, -1, -1, IndexOutOfBoundsException.class);
callCodePoint(Before, a, 0, 0, IndexOutOfBoundsException.class);
callCodePoint(Before, a, a.length+1, a.length-1, IndexOutOfBoundsException.class);
// codePointCount
callCodePointCount(nullArray, 0, 0, NullPointerException.class);
callCodePointCount(a, -1, 1, IndexOutOfBoundsException.class);
callCodePointCount(a, 0, -1, IndexOutOfBoundsException.class);
callCodePointCount(a, 0, a.length+1, IndexOutOfBoundsException.class);
callCodePointCount(a, 1, a.length, IndexOutOfBoundsException.class);
callCodePointCount(a, a.length, 1, IndexOutOfBoundsException.class);
callCodePointCount(a, a.length+1, -1, IndexOutOfBoundsException.class);
// offsetByCodePoints
callOffsetByCodePoints(nullArray, 0, 0, 0, 0, NullPointerException.class);
callOffsetByCodePoints(a, -1, a.length, 1, 1, IndexOutOfBoundsException.class);
callOffsetByCodePoints(a, 0, a.length+1, 1, 1, IndexOutOfBoundsException.class);
callOffsetByCodePoints(a, 10, a.length, 1, 1, IndexOutOfBoundsException.class);
callOffsetByCodePoints(a, 10, a.length-10, 1, 1, IndexOutOfBoundsException.class);
callOffsetByCodePoints(a, 10, 10, 21, 1, IndexOutOfBoundsException.class);
callOffsetByCodePoints(a, 20, -10, 15, 1, IndexOutOfBoundsException.class);
callOffsetByCodePoints(a, 10, 10, 15, 20, IndexOutOfBoundsException.class);
callOffsetByCodePoints(a, 10, 10, 15, -20, IndexOutOfBoundsException.class);
callOffsetByCodePoints(a, 0, a.length, -1, 1, IndexOutOfBoundsException.class);
callOffsetByCodePoints(a, 0, a.length, a.length+1, 1, IndexOutOfBoundsException.class);
callOffsetByCodePoints(a, 0, a.length, 0, a.length*2, IndexOutOfBoundsException.class);
callOffsetByCodePoints(a, 0, a.length, a.length, 1, IndexOutOfBoundsException.class);
callOffsetByCodePoints(a, 0, a.length, 0, -1, IndexOutOfBoundsException.class);
callOffsetByCodePoints(a, 0, a.length, a.length, -a.length*2,
IndexOutOfBoundsException.class);
callOffsetByCodePoints(a, 0, a.length, a.length, Integer.MIN_VALUE,
IndexOutOfBoundsException.class);
callOffsetByCodePoints(a, 0, a.length, 0, Integer.MAX_VALUE,
IndexOutOfBoundsException.class);
}
/**
* Test the 1-arg toChars(int) for exceptions
*/
private static void callToChars1(int codePoint, Class expectedException) {
try {
char[] a = Character.toChars(codePoint);
} catch (Exception e) {
if (expectedException.isInstance(e)) {
return;
}
throw new RuntimeException("Unspecified exception", e);
}
throw new RuntimeException("toChars(int) didn't throw " + expectedException.getName());
}
/**
* Test the 3-arg toChars(int, char[], int) for exceptions
*/
private static void callToChars3(int codePoint, char[] dst, int index,
Class expectedException) {
try {
int n = Character.toChars(codePoint, dst, index);
} catch (Exception e) {
if (expectedException.isInstance(e)) {
return;
}
throw new RuntimeException("Unspecified exception", e);
}
throw new RuntimeException("toChars(int,char[],int) didn't throw "
+ expectedException.getName());
}
private static void callCodePoint(boolean isAt, CharSequence cs, int index,
Class expectedException) {
try {
int c = isAt ? Character.codePointAt(cs, index)
: Character.codePointBefore(cs, index);
} catch (Exception e) {
if (expectedException.isInstance(e)) {
return;
}
throw new RuntimeException("Unspecified exception", e);
}
throw new RuntimeException("codePoint" + (isAt ? "At" : "Before")
+ " didn't throw " + expectedException.getName());
}
private static void callCodePoint(boolean isAt, char[] a, int index,
Class expectedException) {
try {
int c = isAt ? Character.codePointAt(a, index)
: Character.codePointBefore(a, index);
} catch (Exception e) {
if (expectedException.isInstance(e)) {
return;
}
throw new RuntimeException("Unspecified exception", e);
}
throw new RuntimeException("codePoint" + (isAt ? "At" : "Before")
+ " didn't throw " + expectedException.getName());
}
private static void callCodePoint(boolean isAt, char[] a, int index, int limit,
Class expectedException) {
try {
int c = isAt ? Character.codePointAt(a, index, limit)
: Character.codePointBefore(a, index, limit);
} catch (Exception e) {
if (expectedException.isInstance(e)) {
return;
}
throw new RuntimeException("Unspecified exception", e);
}
throw new RuntimeException("codePoint" + (isAt ? "At" : "Before")
+ " didn't throw " + expectedException.getName());
}
private static void callCodePointCount(Object data, int beginIndex, int endIndex,
Class expectedException) {
String type = getType(data);
try {
int n = (data instanceof CharSequence) ?
Character.codePointCount((CharSequence) data, beginIndex, endIndex)
: Character.codePointCount((char[]) data, beginIndex, endIndex);
} catch (Exception e) {
if (expectedException.isInstance(e)) {
return;
}
throw new RuntimeException("Unspecified exception", e);
}
throw new RuntimeException("codePointCount(" + type + "...) didn't throw "
+ expectedException.getName());
}
private static void callOffsetByCodePoints(CharSequence seq, int index, int offset,
Class expectedException) {
try {
int n = Character.offsetByCodePoints(seq, index, offset);
} catch (Exception e) {
if (expectedException.isInstance(e)) {
return;
}
throw new RuntimeException("Unspecified exception", e);
}
throw new RuntimeException("offsetCodePointCounts(CharSequnce...) didn't throw "
+ expectedException.getName());
}
private static void callOffsetByCodePoints(char[] a, int start, int count,
int index, int offset,
Class expectedException) {
try {
int n = Character.offsetByCodePoints(a, start, count, index, offset);
} catch (Exception e) {
if (expectedException.isInstance(e)) {
return;
}
throw new RuntimeException("Unspecified exception", e);
}
throw new RuntimeException("offsetCodePointCounts(char[]...) didn't throw "
+ expectedException.getName());
}
private static void callToString(int codePoint, Class expectedException) {
try {
String s = Character.toString(codePoint);
} catch (Exception e) {
if (expectedException.isInstance(e)) {
return;
}
throw new RuntimeException("Unspecified exception", e);
}
throw new RuntimeException("toString(int) didn't throw "
+ expectedException.getName());
}
private static String getType(Object data) {
return (data instanceof CharSequence) ? "CharSequence" : "char[]";
}
private static String toHexString(int c) {
return "0x" + Integer.toHexString(c);
}
private static String toHexString(String name, char[] a) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < a.length; i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(name).append('[').append(i).append("]=");
sb.append(toHexString(a[i]));
}
return sb.toString();
}
}
|