Path: blob/master/test/jdk/java/lang/Character/Supplementary.java
41149 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4533872 4985214 4985217 4993841 5017268 501728026* @summary Unit tests for supplementary character support (JSR-204)27* @compile Supplementary.java28* @run main/timeout=600 Supplementary29*/3031public class Supplementary {32private static final char MIN_HIGH = '\uD800';33private static final char MAX_HIGH = '\uDBFF';34private static final char MIN_LOW = MAX_HIGH + 1;35private static final char MAX_LOW = '\uDFFF';36private static final int MIN_CODE_POINT = 0x000000;37private static final int MIN_SUPPLEMENTARY = 0x010000;38private static final int MAX_SUPPLEMENTARY = 0x10ffff;3940public static void main(String[] args) {41// Do not change the order of test method calls since there42// are some interdependencies.4344testConstants();4546test00();4748// Store all Unicode code points, except for surrogate code49// points, in cu[] through the loops below. Then, use the data50// for code point/code unit conversion and other tests later.51char[] cu = new char[(MAX_SUPPLEMENTARY+1) * 2];52int length = test01(cu);5354String str = new String(cu, 0, length);55cu = null;56test02(str);57test03(str.toCharArray());58test04(str);59test05(str);6061// Test for toString(int)62test06();6364// Test unpaired surrogates65testUnpaired();6667// Test exceptions68testExceptions00();69testExceptions01(str);70testExceptions02(str.toCharArray());71}7273static void testConstants() {74if (Character.MIN_HIGH_SURROGATE != MIN_HIGH) {75constantError("MIN_HIGH_SURROGATE", Character.MIN_HIGH_SURROGATE, MIN_HIGH);76}77if (Character.MAX_HIGH_SURROGATE != MAX_HIGH) {78constantError("MAX_HIGH_SURROGATE", Character.MAX_HIGH_SURROGATE, MAX_HIGH);79}80if (Character.MIN_LOW_SURROGATE != MIN_LOW) {81constantError("MIN_LOW_SURROGATE", Character.MIN_LOW_SURROGATE, MIN_LOW);82}83if (Character.MAX_LOW_SURROGATE != MAX_LOW) {84constantError("MAX_LOW_SURROGATE", Character.MAX_LOW_SURROGATE, MAX_LOW);85}86if (Character.MIN_SURROGATE != MIN_HIGH) {87constantError("MIN_SURROGATE", Character.MIN_SURROGATE, MIN_HIGH);88}89if (Character.MAX_SURROGATE != MAX_LOW) {90constantError("MAX_SURROGATE", Character.MAX_SURROGATE, MAX_LOW);91}92if (Character.MIN_SUPPLEMENTARY_CODE_POINT != MIN_SUPPLEMENTARY) {93constantError("MIN_SUPPLEMENTARY_CODE_POINT",94Character.MIN_SUPPLEMENTARY_CODE_POINT, MIN_SUPPLEMENTARY);95}96if (Character.MIN_CODE_POINT != MIN_CODE_POINT) {97constantError("MIN_CODE_POINT", Character.MIN_CODE_POINT, MIN_CODE_POINT);98}99if (Character.MAX_CODE_POINT != MAX_SUPPLEMENTARY) {100constantError("MAX_CODE_POINT", Character.MAX_CODE_POINT, MAX_SUPPLEMENTARY);101}102}103104static void constantError(String name, int value, int expectedValue) {105throw new RuntimeException("Character." + name + " has a wrong value: got "106+ toHexString(value)107+ ", expected " + toHexString(expectedValue));108}109110/*111* Test isValidCodePoint(int)112* isSupplementaryCodePoint(int)113* charCount(int)114*/115static void test00() {116for (int cp = -MAX_SUPPLEMENTARY; cp <= MAX_SUPPLEMENTARY*2; cp++) {117boolean isValid = cp >= 0 && cp <= MAX_SUPPLEMENTARY;118if (Character.isValidCodePoint(cp) != isValid) {119throw new RuntimeException("isValidCodePoint failed with "120+ toHexString(cp));121}122boolean isSupplementary = cp >= MIN_SUPPLEMENTARY && cp <= MAX_SUPPLEMENTARY;123if (Character.isSupplementaryCodePoint(cp) != isSupplementary) {124throw new RuntimeException("isSupplementaryCodePoint failed with "125+ toHexString(cp));126}127int len = Character.charCount(cp);128if (isValid) {129if ((isSupplementary && len != 2)130|| (!isSupplementary && len != 1)) {131throw new RuntimeException("wrong character length "+len+" for "132+ toHexString(cp));133}134} else if (len != 1 && len != 2) {135throw new RuntimeException("wrong character length "+len+" for "136+ toHexString(cp));137}138}139}140141/**142* Test toChar(int)143* toChar(int, char[], int)144* isHighSurrogate(char)145* isLowSurrogate(char)146* isSurrogatePair(int, int)147*148* While testing those methods, this method generates all Unicode149* code points (except for surrogate code points) and store them150* in cu.151*152* @return the number of code units generated in cu153*/154static int test01(char[] cu) {155int index = 0;156// Test toChar(int)157// toChar(int, char[], int)158// isHighSurrogate(char)159// isLowSurrogate(char)160// with BMP code points161for (int i = 0; i <= Character.MAX_VALUE; i++) {162char[] u = Character.toChars(i);163if (u.length != 1 || u[0] != i) {164throw new RuntimeException("wrong toChars(int) result for BMP: "165+ toHexString("u", u));166}167int n = Character.toChars(i, cu, index);168if (n != 1 || cu[index] != i) {169throw new RuntimeException("wrong toChars(int, char[], int) result for BMP:"170+ " len=" + n171+ ", cu["+index+"]="+toHexString(cu[index]));172}173boolean isHigh = i >= MIN_HIGH && i <= MAX_HIGH;174if (Character.isHighSurrogate((char) i) != isHigh) {175throw new RuntimeException("wrong high-surrogate test for "176+ toHexString(i));177}178boolean isLow = i >= MIN_LOW && i <= MAX_LOW;179if (Character.isLowSurrogate((char)i) != isLow) {180throw new RuntimeException("wrong low-surrogate test for "181+ toHexString(i));182}183if (!isHigh && !isLow) {184index++;185}186}187188// Test isSurrogatePair with all surrogate pairs189// Test toChars(int)190// toChars(int, char[], int)191// with all supplementary characters192int supplementary = MIN_SUPPLEMENTARY;193for (int i = Character.MAX_VALUE/2; i <= Character.MAX_VALUE; i++) {194char hi = (char) i;195boolean isHigh = Character.isHighSurrogate(hi);196197for (int j = Character.MAX_VALUE/2; j <= Character.MAX_VALUE; j++) {198char lo = (char) j;199boolean isLow = Character.isLowSurrogate(lo);200boolean isSurrogatePair = isHigh && isLow;201if (Character.isSurrogatePair(hi, lo) != isSurrogatePair) {202throw new RuntimeException("wrong surrogate pair test for hi="203+ toHexString(hi)204+ ", lo="+toHexString(lo));205}206if (isSurrogatePair) {207int cp = Character.toCodePoint(hi, lo);208if (cp != supplementary) {209throw new RuntimeException("wrong code point: got "210+ toHexString(cp)211+ ", expected="212+ toHexString(supplementary));213}214char[] u = Character.toChars(cp);215if (u.length != 2 || u[0] != hi || u[1] != lo) {216throw new RuntimeException("wrong toChars(int) result for supplementary: "+217toHexString("u", u));218}219int n = Character.toChars(cp, cu, index);220if (n != 2 || cu[index] != hi || cu[index+1] != lo) {221throw new RuntimeException("wrong toChars(int, char[], int) result "222+ "for supplementary: len=" + n223+ ", cu["+index+"]=" + toHexString(cu[index])224+ ", cu["+(index+1)+"]=" + toHexString(cu[index+1]));225}226index += n;227supplementary++;228}229}230}231if (supplementary != MAX_SUPPLEMENTARY + 1) {232throw new RuntimeException("wrong supplementary count (supplementary="233+ toHexString(supplementary)+")");234}235236int nCodeUnits = Character.MAX_VALUE + 1 - (MAX_LOW - MIN_HIGH + 1)237+ ((MAX_SUPPLEMENTARY - MIN_SUPPLEMENTARY + 1) * 2);238if (index != nCodeUnits) {239throw new RuntimeException("wrong number of code units: " + index240+ ", expected " + nCodeUnits);241}242return index;243}244245/**246* Test codePointAt(CharSequence, int)247* codePointBefore(CharSequence, int)248*/249static void test02(CharSequence cs) {250int cp = 0;251int ch;252for (int i = 0; i < cs.length(); i += Character.charCount(ch)) {253ch = Character.codePointAt(cs, i);254if (ch != cp) {255throw new RuntimeException("wrong codePointAt(CharSequence, "+i+") value: got "256+ toHexString(ch)257+ ", expected "+toHexString(cp));258}259cp++;260// Skip surrogates261if (cp == MIN_HIGH) {262cp = MAX_LOW + 1;263}264}265266cp--;267for (int i = cs.length(); i > 0; i -= Character.charCount(ch)) {268ch = Character.codePointBefore(cs, i);269if (ch != cp) {270throw new RuntimeException("codePointBefore(CharSequence, "+i+") returned "271+ toHexString(ch)272+ ", expected " + toHexString(cp));273}274cp--;275// Skip surrogates276if (cp == MAX_LOW) {277cp = MIN_HIGH - 1;278}279}280}281282/**283* Test codePointAt(char[], int)284* codePointAt(char[], int, int)285* codePointBefore(char[], int)286* codePointBefore(char[], int, int)287*/288static void test03(char[] a) {289int cp = 0;290int ch;291for (int i = 0; i < a.length; i += Character.charCount(ch)) {292ch = Character.codePointAt(a, i);293if (ch != cp) {294throw new RuntimeException("codePointAt(char[], "+i+") returned "295+ toHexString(ch)296+ ", expected "+toHexString(cp));297}298int x = Character.codePointAt(a, i, i+1);299if (x != a[i]) {300throw new RuntimeException(String.format(301"codePointAt(char[], %d, %d) returned 0x%04x, expected 0x%04x\n",302i, i+1, x, (int)a[i]));303}304cp++;305// Skip surrogates306if (cp == MIN_HIGH) {307cp = MAX_LOW + 1;308}309}310311cp--;312for (int i = a.length; i > 0; i -= Character.charCount(ch)) {313ch = Character.codePointBefore(a, i);314if (ch != cp) {315throw new RuntimeException("codePointBefore(char[], "+i+") returned "316+ toHexString(ch)317+ ", expected " + toHexString(cp));318}319int x = Character.codePointBefore(a, i, i-1);320if (x != a[i-1]) {321throw new RuntimeException(String.format(322"codePointAt(char[], %d, %d) returned 0x%04x, expected 0x%04x\n",323i, i-1, x, (int)a[i-1]));324}325cp--;326// Skip surrogates327if (cp == MAX_LOW) {328cp = MIN_HIGH - 1;329}330}331}332333/**334* Test codePointCount(CharSequence, int, int)335* codePointCount(char[], int, int, int, int)336*/337static void test04(String str) {338int length = str.length();339char[] a = str.toCharArray();340341for (int i = 0; i <= length; i += 99, length -= 29999) {342int n = Character.codePointCount(str, i, length);343int m = codePointCount(str.substring(i, length));344checkCodePointCount(str, n, m);345n = Character.codePointCount(a, i, length - i);346checkCodePointCount(a, n, m);347}348349// test special cases350length = str.length();351int n = Character.codePointCount(str, 0, 0);352checkCodePointCount(str, n, 0);353n = Character.codePointCount(str, length, length);354checkCodePointCount(str, n, 0);355n = Character.codePointCount(a, 0, 0);356checkCodePointCount(a, n, 0);357n = Character.codePointCount(a, length, 0);358checkCodePointCount(a, n, 0);359}360361// This method assumes that Character.codePointAt() and362// Character.charCount() work correctly.363private static int codePointCount(CharSequence seq) {364int n = 0, len;365for (int i = 0; i < seq.length(); i += len) {366int codepoint = Character.codePointAt(seq, i);367n++;368len = Character.charCount(codepoint);369}370return n;371}372373private static void checkCodePointCount(Object data, int n, int expected) {374String type = getType(data);375if (n != expected) {376throw new RuntimeException("codePointCount(" + type + "...) returned " + n377+ ", expected " + expected);378}379}380381/**382* Test offsetByCodePoints(CharSequence, int, int)383* offsetByCodePoints(char[], int, int, int, int)384*385* This test case assumes that Character.codePointCount()s work386* correctly.387*/388static void test05(String str) {389int length = str.length();390char[] a = str.toCharArray();391392for (int i = 0; i <= length; i += 99, length -= 29999) {393int nCodePoints = Character.codePointCount(a, i, length - i);394int index;395396// offsetByCodePoints(CharSequence, int, int)397398int expectedHighIndex = length;399// For forward CharSequence scan, we need to adjust the400// expected index in case the last char in the text range401// is a high surrogate and forms a valid supplementary402// code point with the next char.403if (length < a.length) {404int cp = Character.codePointAt(a, length - 1);405if (Character.isSupplementaryCodePoint(cp)) {406expectedHighIndex++;407}408}409index = Character.offsetByCodePoints(str, i, nCodePoints);410checkNewIndex(str, nCodePoints, index, expectedHighIndex);411int expectedLowIndex = i;412if (i > 0) {413int cp = Character.codePointBefore(a, i + 1);414if (Character.isSupplementaryCodePoint(cp)) {415expectedLowIndex--;416}417}418index = Character.offsetByCodePoints(str, length, -nCodePoints);419checkNewIndex(str, -nCodePoints, index, expectedLowIndex);420421// offsetByCodePoints(char[], int, int, int, int)422423int start = Math.max(0, i-1);424int limit = Math.min(a.length, length+1);425index = Character.offsetByCodePoints(a, start, limit - start,426i, nCodePoints);427checkNewIndex(a, nCodePoints, index, expectedHighIndex);428if (length != expectedHighIndex) {429index = Character.offsetByCodePoints(a, start, length - start,430i, nCodePoints);431checkNewIndex(a, nCodePoints, index, length);432}433index = Character.offsetByCodePoints(a, start, limit - start,434length, -nCodePoints);435checkNewIndex(a, -nCodePoints, index, expectedLowIndex);436if (i != expectedLowIndex) {437index = Character.offsetByCodePoints(a, i, limit - i,438length, -nCodePoints);439checkNewIndex(a, -nCodePoints, index, i);440}441}442443// test special cases for 0-length text ranges.444length = str.length();445int index = Character.offsetByCodePoints(str, 0, 0);446checkNewIndex(str, 0, index, 0);447index = Character.offsetByCodePoints(str, length, 0);448checkNewIndex(str, 0, index, length);449index = Character.offsetByCodePoints(a, 0, 0, 0, 0);450checkNewIndex(a, 0, index, 0);451index = Character.offsetByCodePoints(a, 0, length, 0, 0);452checkNewIndex(a, 0, index, 0);453index = Character.offsetByCodePoints(a, 0, length, length, 0);454checkNewIndex(a, 0, index, length);455index = Character.offsetByCodePoints(a, length, 0, length, 0);456checkNewIndex(a, 0, index, length);457}458459/**460* Test toString(int)461*462* This test case assumes that Character.toChars()/String(char[]) work463* correctly.464*/465static void test06() {466for (int cp = Character.MIN_CODE_POINT; cp <= Character.MAX_CODE_POINT; cp++) {467String result = Character.toString(cp);468String expected = new String(Character.toChars(cp));469if (!result.equals(expected)) {470throw new RuntimeException("Wrong string is created. code point: " +471cp + ", result: " + result + ", expected: " + expected);472}473}474}475476private static void checkNewIndex(Object data, int offset, int result, int expected) {477String type = getType(data);478String offsetType = (offset > 0) ? "positive" : (offset < 0) ? "negative" : "0";479if (result != expected) {480throw new RuntimeException("offsetByCodePoints(" + type + ", ...) ["481+ offsetType + " offset]"482+ " returned " + result483+ ", expected " + expected);484}485}486487// Test codePointAt(CharSequence, int)488// codePointBefore(CharSequence, int)489// codePointAt(char[], int)490// codePointBefore(char[], int)491// toChar(int)492// toChar(int, char[], int)493// with unpaired surrogates494static void testUnpaired() {495testCodePoint("\uD800", new int[] { 0xD800 });496testCodePoint("\uDC00", new int[] { 0xDC00 });497testCodePoint("a\uD800", new int[] { 'a', 0xD800 });498testCodePoint("a\uDC00", new int[] { 'a', 0xDC00 });499testCodePoint("\uD800a", new int[] { 0xD800, 'a' });500testCodePoint("\uDBFFa", new int[] { 0xDBFF, 'a' });501testCodePoint("a\uD800\uD801", new int[] { 'a', 0xD800, 0xD801 });502testCodePoint("a\uD800x\uDC00", new int[] { 'a', 0xD800, 'x', 0xDC00 });503testCodePoint("\uDC00\uD800", new int[] { 0xDC00, 0xD800 });504testCodePoint("\uD800\uDC00\uDC00", new int[] { 0x10000, 0xDC00 });505testCodePoint("\uD800\uD800\uDC00", new int[] { 0xD800, 0x10000 });506testCodePoint("\uD800\uD800\uD800\uD800\uDC00\uDC00\uDC00\uDC00",507new int[] { 0xD800, 0xD800, 0xD800, 0x10000, 0xDC00, 0xDC00, 0xDC00});508}509510static void testCodePoint(String str, int[] codepoints) {511int c;512// Test Character.codePointAt/Before(CharSequence, int)513int j = 0;514for (int i = 0; i < str.length(); i += Character.charCount(c)) {515c = Character.codePointAt(str, i);516if (c != codepoints[j++]) {517throw new RuntimeException("codePointAt(CharSequence, " + i + ") returned "518+ toHexString(c)519+ ", expected " + toHexString(codepoints[j-1]));520}521}522if (j != codepoints.length) {523throw new RuntimeException("j != codepoints.length after codePointAt(CharSequence, int)"524+ " (j=" + j + ")"525+ ", expected: " + codepoints.length);526}527528j = codepoints.length;529for (int i = str.length(); i > 0 ; i -= Character.charCount(c)) {530c = Character.codePointBefore(str, i);531if (c != codepoints[--j]) {532throw new RuntimeException("codePointBefore(CharSequence, " + i + ") returned "533+ toHexString(c)534+ ", expected " + toHexString(codepoints[j]));535}536}537if (j != 0) {538throw new RuntimeException("j != 0 after codePointBefore(CharSequence, int)"539+ " (j=" + j + ")");540}541542// Test Character.codePointAt/Before(char[], int)543char[] a = str.toCharArray();544j = 0;545for (int i = 0; i < a.length; i += Character.charCount(c)) {546c = Character.codePointAt(a, i);547if (c != codepoints[j++]) {548throw new RuntimeException("codePointAt(char[], " + i + ") returned "549+ toHexString(c)550+ ", expected " + toHexString(codepoints[j-1]));551}552}553if (j != codepoints.length) {554throw new RuntimeException("j != codepoints.length after codePointAt(char[], int)"555+ " (j=" + j + ")"556+ ", expected: " + codepoints.length);557}558559j = codepoints.length;560for (int i = a.length; i > 0 ; i -= Character.charCount(c)) {561c = Character.codePointBefore(a, i);562if (c != codepoints[--j]) {563throw new RuntimeException("codePointBefore(char[], " + i + ") returned "564+ toHexString(c)565+ ", expected " + toHexString(codepoints[j]));566}567}568if (j != 0) {569throw new RuntimeException("j != 0 after codePointBefore(char[], int)"570+ " (j=" + j + ")");571}572573// Test toChar(int)574j = 0;575for (int i = 0; i < codepoints.length; i++) {576a = Character.toChars(codepoints[i]);577for (int k = 0; k < a.length; k++) {578if (str.charAt(j++) != a[k]) {579throw new RuntimeException("toChars(int) returned " + toHexString("result", a)580+ " from codepoint=" + toHexString(codepoints[i]));581}582}583}584585// Test toChars(int, char[], int)586a = new char[codepoints.length * 2];587j = 0;588for (int i = 0; i < codepoints.length; i++) {589int n = Character.toChars(codepoints[i], a, j);590j += n;591}592String s = new String(a, 0, j);593if (!str.equals(s)) {594throw new RuntimeException("toChars(int, char[], int) returned "595+ toHexString("dst", s.toCharArray())596+ ", expected " + toHexString("data", str.toCharArray()));597}598}599600// Test toChar(int)601// toChar(int, char[], int)602// toString(int)603// for exceptions604static void testExceptions00() {605callToChars1(-1, IllegalArgumentException.class);606callToChars1(MAX_SUPPLEMENTARY + 1, IllegalArgumentException.class);607608callToChars3(MAX_SUPPLEMENTARY, null, 0, NullPointerException.class);609callToChars3(-MIN_SUPPLEMENTARY, new char[2], 0, IllegalArgumentException.class);610callToChars3(MAX_SUPPLEMENTARY + 1, new char[2], 0, IllegalArgumentException.class);611callToChars3('A', new char[0], 0, IndexOutOfBoundsException.class);612callToChars3('A', new char[1], -1, IndexOutOfBoundsException.class);613callToChars3('A', new char[1], 1, IndexOutOfBoundsException.class);614callToChars3(MIN_SUPPLEMENTARY, new char[0], 0, IndexOutOfBoundsException.class);615callToChars3(MIN_SUPPLEMENTARY, new char[1], 0, IndexOutOfBoundsException.class);616callToChars3(MIN_SUPPLEMENTARY, new char[2], -1, IndexOutOfBoundsException.class);617callToChars3(MIN_SUPPLEMENTARY, new char[2], 1, IndexOutOfBoundsException.class);618619callToString(Character.MIN_CODE_POINT - 1, IllegalArgumentException.class);620callToString(Character.MAX_CODE_POINT + 1, IllegalArgumentException.class);621}622623static final boolean At = true, Before = false;624625/**626* Test codePointAt(CharSequence, int)627* codePointBefore(CharSequence, int)628* codePointCount(CharSequence, int, int)629* offsetByCodePoints(CharSequence, int, int)630* for exceptions631*/632static void testExceptions01(CharSequence cs) {633CharSequence nullSeq = null;634// codePointAt635callCodePoint(At, nullSeq, 0, NullPointerException.class);636callCodePoint(At, cs, -1, IndexOutOfBoundsException.class);637callCodePoint(At, cs, cs.length(), IndexOutOfBoundsException.class);638callCodePoint(At, cs, cs.length()*3, IndexOutOfBoundsException.class);639640// codePointBefore641callCodePoint(Before, nullSeq, 0, NullPointerException.class);642callCodePoint(Before, cs, -1, IndexOutOfBoundsException.class);643callCodePoint(Before, cs, 0, IndexOutOfBoundsException.class);644callCodePoint(Before, cs, cs.length()+1, IndexOutOfBoundsException.class);645646// codePointCount647callCodePointCount(nullSeq, 0, 0, NullPointerException.class);648callCodePointCount(cs, -1, 1, IndexOutOfBoundsException.class);649callCodePointCount(cs, 0, cs.length()+1, IndexOutOfBoundsException.class);650callCodePointCount(cs, 3, 1, IndexOutOfBoundsException.class);651652// offsetByCodePoints653callOffsetByCodePoints(nullSeq, 0, 0, NullPointerException.class);654callOffsetByCodePoints(cs, -1, 1, IndexOutOfBoundsException.class);655callOffsetByCodePoints(cs, cs.length()+1, 1, IndexOutOfBoundsException.class);656callOffsetByCodePoints(cs, 0, cs.length()*2, IndexOutOfBoundsException.class);657callOffsetByCodePoints(cs, cs.length(), 1, IndexOutOfBoundsException.class);658callOffsetByCodePoints(cs, 0, -1, IndexOutOfBoundsException.class);659callOffsetByCodePoints(cs, cs.length(), -cs.length()*2,660IndexOutOfBoundsException.class);661callOffsetByCodePoints(cs, cs.length(), Integer.MIN_VALUE,662IndexOutOfBoundsException.class);663callOffsetByCodePoints(cs, 0, Integer.MAX_VALUE, IndexOutOfBoundsException.class);664}665666/**667* Test codePointAt(char[], int)668* codePointAt(char[], int, int)669* codePointBefore(char[], int)670* codePointBefore(char[], int, int)671* codePointCount(char[], int, int)672* offsetByCodePoints(char[], int, int, int, int)673* for exceptions674*/675static void testExceptions02(char[] a) {676char[] nullArray = null;677callCodePoint(At, nullArray, 0, NullPointerException.class);678callCodePoint(At, a, -1, IndexOutOfBoundsException.class);679callCodePoint(At, a, a.length, IndexOutOfBoundsException.class);680callCodePoint(At, a, a.length*3, IndexOutOfBoundsException.class);681callCodePoint(Before, nullArray, 0, NullPointerException.class);682callCodePoint(Before, a, -1, IndexOutOfBoundsException.class);683callCodePoint(Before, a, 0, IndexOutOfBoundsException.class);684callCodePoint(Before, a, a.length+1, IndexOutOfBoundsException.class);685686// tests for the methods with limit687callCodePoint(At, nullArray, 0, 1, NullPointerException.class);688callCodePoint(At, a, 0, -1, IndexOutOfBoundsException.class);689callCodePoint(At, a, 0, 0, IndexOutOfBoundsException.class);690callCodePoint(At, a, 0, a.length+1, IndexOutOfBoundsException.class);691callCodePoint(At, a, 2, 1, IndexOutOfBoundsException.class);692callCodePoint(At, a, -1, 1, IndexOutOfBoundsException.class);693callCodePoint(At, a, a.length, 1, IndexOutOfBoundsException.class);694callCodePoint(At, a, a.length*3, 1, IndexOutOfBoundsException.class);695callCodePoint(Before, nullArray, 1, 0, NullPointerException.class);696callCodePoint(Before, a, 2, -1, IndexOutOfBoundsException.class);697callCodePoint(Before, a, 2, 2, IndexOutOfBoundsException.class);698callCodePoint(Before, a, 2, 3, IndexOutOfBoundsException.class);699callCodePoint(Before, a, 2, a.length, IndexOutOfBoundsException.class);700callCodePoint(Before, a, -1, -1, IndexOutOfBoundsException.class);701callCodePoint(Before, a, 0, 0, IndexOutOfBoundsException.class);702callCodePoint(Before, a, a.length+1, a.length-1, IndexOutOfBoundsException.class);703704// codePointCount705callCodePointCount(nullArray, 0, 0, NullPointerException.class);706callCodePointCount(a, -1, 1, IndexOutOfBoundsException.class);707callCodePointCount(a, 0, -1, IndexOutOfBoundsException.class);708callCodePointCount(a, 0, a.length+1, IndexOutOfBoundsException.class);709callCodePointCount(a, 1, a.length, IndexOutOfBoundsException.class);710callCodePointCount(a, a.length, 1, IndexOutOfBoundsException.class);711callCodePointCount(a, a.length+1, -1, IndexOutOfBoundsException.class);712713// offsetByCodePoints714callOffsetByCodePoints(nullArray, 0, 0, 0, 0, NullPointerException.class);715callOffsetByCodePoints(a, -1, a.length, 1, 1, IndexOutOfBoundsException.class);716callOffsetByCodePoints(a, 0, a.length+1, 1, 1, IndexOutOfBoundsException.class);717callOffsetByCodePoints(a, 10, a.length, 1, 1, IndexOutOfBoundsException.class);718callOffsetByCodePoints(a, 10, a.length-10, 1, 1, IndexOutOfBoundsException.class);719callOffsetByCodePoints(a, 10, 10, 21, 1, IndexOutOfBoundsException.class);720callOffsetByCodePoints(a, 20, -10, 15, 1, IndexOutOfBoundsException.class);721callOffsetByCodePoints(a, 10, 10, 15, 20, IndexOutOfBoundsException.class);722callOffsetByCodePoints(a, 10, 10, 15, -20, IndexOutOfBoundsException.class);723callOffsetByCodePoints(a, 0, a.length, -1, 1, IndexOutOfBoundsException.class);724callOffsetByCodePoints(a, 0, a.length, a.length+1, 1, IndexOutOfBoundsException.class);725callOffsetByCodePoints(a, 0, a.length, 0, a.length*2, IndexOutOfBoundsException.class);726callOffsetByCodePoints(a, 0, a.length, a.length, 1, IndexOutOfBoundsException.class);727callOffsetByCodePoints(a, 0, a.length, 0, -1, IndexOutOfBoundsException.class);728callOffsetByCodePoints(a, 0, a.length, a.length, -a.length*2,729IndexOutOfBoundsException.class);730callOffsetByCodePoints(a, 0, a.length, a.length, Integer.MIN_VALUE,731IndexOutOfBoundsException.class);732callOffsetByCodePoints(a, 0, a.length, 0, Integer.MAX_VALUE,733IndexOutOfBoundsException.class);734}735736/**737* Test the 1-arg toChars(int) for exceptions738*/739private static void callToChars1(int codePoint, Class expectedException) {740try {741char[] a = Character.toChars(codePoint);742} catch (Exception e) {743if (expectedException.isInstance(e)) {744return;745}746throw new RuntimeException("Unspecified exception", e);747}748throw new RuntimeException("toChars(int) didn't throw " + expectedException.getName());749}750751/**752* Test the 3-arg toChars(int, char[], int) for exceptions753*/754private static void callToChars3(int codePoint, char[] dst, int index,755Class expectedException) {756try {757int n = Character.toChars(codePoint, dst, index);758} catch (Exception e) {759if (expectedException.isInstance(e)) {760return;761}762throw new RuntimeException("Unspecified exception", e);763}764throw new RuntimeException("toChars(int,char[],int) didn't throw "765+ expectedException.getName());766}767768private static void callCodePoint(boolean isAt, CharSequence cs, int index,769Class expectedException) {770try {771int c = isAt ? Character.codePointAt(cs, index)772: Character.codePointBefore(cs, index);773} catch (Exception e) {774if (expectedException.isInstance(e)) {775return;776}777throw new RuntimeException("Unspecified exception", e);778}779throw new RuntimeException("codePoint" + (isAt ? "At" : "Before")780+ " didn't throw " + expectedException.getName());781}782783private static void callCodePoint(boolean isAt, char[] a, int index,784Class expectedException) {785try {786int c = isAt ? Character.codePointAt(a, index)787: Character.codePointBefore(a, index);788} catch (Exception e) {789if (expectedException.isInstance(e)) {790return;791}792throw new RuntimeException("Unspecified exception", e);793}794throw new RuntimeException("codePoint" + (isAt ? "At" : "Before")795+ " didn't throw " + expectedException.getName());796}797798private static void callCodePoint(boolean isAt, char[] a, int index, int limit,799Class expectedException) {800try {801int c = isAt ? Character.codePointAt(a, index, limit)802: Character.codePointBefore(a, index, limit);803} catch (Exception e) {804if (expectedException.isInstance(e)) {805return;806}807throw new RuntimeException("Unspecified exception", e);808}809throw new RuntimeException("codePoint" + (isAt ? "At" : "Before")810+ " didn't throw " + expectedException.getName());811}812813private static void callCodePointCount(Object data, int beginIndex, int endIndex,814Class expectedException) {815String type = getType(data);816try {817int n = (data instanceof CharSequence) ?818Character.codePointCount((CharSequence) data, beginIndex, endIndex)819: Character.codePointCount((char[]) data, beginIndex, endIndex);820} catch (Exception e) {821if (expectedException.isInstance(e)) {822return;823}824throw new RuntimeException("Unspecified exception", e);825}826throw new RuntimeException("codePointCount(" + type + "...) didn't throw "827+ expectedException.getName());828}829830private static void callOffsetByCodePoints(CharSequence seq, int index, int offset,831Class expectedException) {832try {833int n = Character.offsetByCodePoints(seq, index, offset);834} catch (Exception e) {835if (expectedException.isInstance(e)) {836return;837}838throw new RuntimeException("Unspecified exception", e);839}840throw new RuntimeException("offsetCodePointCounts(CharSequnce...) didn't throw "841+ expectedException.getName());842}843844845private static void callOffsetByCodePoints(char[] a, int start, int count,846int index, int offset,847Class expectedException) {848try {849int n = Character.offsetByCodePoints(a, start, count, index, offset);850} catch (Exception e) {851if (expectedException.isInstance(e)) {852return;853}854throw new RuntimeException("Unspecified exception", e);855}856throw new RuntimeException("offsetCodePointCounts(char[]...) didn't throw "857+ expectedException.getName());858}859860private static void callToString(int codePoint, Class expectedException) {861try {862String s = Character.toString(codePoint);863} catch (Exception e) {864if (expectedException.isInstance(e)) {865return;866}867throw new RuntimeException("Unspecified exception", e);868}869throw new RuntimeException("toString(int) didn't throw "870+ expectedException.getName());871}872873private static String getType(Object data) {874return (data instanceof CharSequence) ? "CharSequence" : "char[]";875}876877private static String toHexString(int c) {878return "0x" + Integer.toHexString(c);879}880881private static String toHexString(String name, char[] a) {882StringBuffer sb = new StringBuffer();883for (int i = 0; i < a.length; i++) {884if (i > 0) {885sb.append(", ");886}887sb.append(name).append('[').append(i).append("]=");888sb.append(toHexString(a[i]));889}890return sb.toString();891}892}893894895