test/jdk/java/lang/Character/Supplementary.java
changeset 49129 fb9f590b9eee
parent 48742 364944ba4e2f
equal deleted inserted replaced
49128:97288886180c 49129:fb9f590b9eee
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 4533872 4985214 4985217 5017268 5017280
    26  * @bug 4533872 4985214 4985217 4993841 5017268 5017280
    27  * @summary Unit tests for supplementary character support (JSR-204)
    27  * @summary Unit tests for supplementary character support (JSR-204)
    28  * @compile Supplementary.java
    28  * @compile Supplementary.java
    29  * @run main/timeout=600 Supplementary
    29  * @run main/timeout=600 Supplementary
    30  */
    30  */
    31 
    31 
    56         cu = null;
    56         cu = null;
    57         test02(str);
    57         test02(str);
    58         test03(str.toCharArray());
    58         test03(str.toCharArray());
    59         test04(str);
    59         test04(str);
    60         test05(str);
    60         test05(str);
       
    61 
       
    62         // Test for toString(int)
       
    63         test06();
    61 
    64 
    62         // Test unpaired surrogates
    65         // Test unpaired surrogates
    63         testUnpaired();
    66         testUnpaired();
    64 
    67 
    65         // Test exceptions
    68         // Test exceptions
   452         checkNewIndex(a, 0, index, length);
   455         checkNewIndex(a, 0, index, length);
   453         index = Character.offsetByCodePoints(a, length, 0, length, 0);
   456         index = Character.offsetByCodePoints(a, length, 0, length, 0);
   454         checkNewIndex(a, 0, index, length);
   457         checkNewIndex(a, 0, index, length);
   455     }
   458     }
   456 
   459 
       
   460     /**
       
   461      * Test toString(int)
       
   462      *
       
   463      * This test case assumes that Character.toChars()/String(char[]) work
       
   464      * correctly.
       
   465      */
       
   466     static void test06() {
       
   467         for (int cp = Character.MIN_CODE_POINT; cp <= Character.MAX_CODE_POINT; cp++) {
       
   468             String result = Character.toString(cp);
       
   469             String expected = new String(Character.toChars(cp));
       
   470             if (!result.equals(expected)) {
       
   471                 throw new RuntimeException("Wrong string is created. code point: " +
       
   472                     cp + ", result: " + result + ", expected: " + expected);
       
   473             }
       
   474         }
       
   475     }
       
   476 
   457     private static void checkNewIndex(Object data, int offset, int result, int expected) {
   477     private static void checkNewIndex(Object data, int offset, int result, int expected) {
   458         String type = getType(data);
   478         String type = getType(data);
   459         String offsetType = (offset > 0) ? "positive" : (offset < 0) ? "negative" : "0";
   479         String offsetType = (offset > 0) ? "positive" : (offset < 0) ? "negative" : "0";
   460         if (result != expected) {
   480         if (result != expected) {
   461             throw new RuntimeException("offsetByCodePoints(" + type + ", ...) ["
   481             throw new RuntimeException("offsetByCodePoints(" + type + ", ...) ["
   578         }
   598         }
   579     }
   599     }
   580 
   600 
   581     // Test toChar(int)
   601     // Test toChar(int)
   582     //      toChar(int, char[], int)
   602     //      toChar(int, char[], int)
       
   603     //      toString(int)
   583     // for exceptions
   604     // for exceptions
   584     static void testExceptions00() {
   605     static void testExceptions00() {
   585         callToChars1(-1, IllegalArgumentException.class);
   606         callToChars1(-1, IllegalArgumentException.class);
   586         callToChars1(MAX_SUPPLEMENTARY + 1, IllegalArgumentException.class);
   607         callToChars1(MAX_SUPPLEMENTARY + 1, IllegalArgumentException.class);
   587 
   608 
   593         callToChars3('A', new char[1],  1, IndexOutOfBoundsException.class);
   614         callToChars3('A', new char[1],  1, IndexOutOfBoundsException.class);
   594         callToChars3(MIN_SUPPLEMENTARY, new char[0],  0, IndexOutOfBoundsException.class);
   615         callToChars3(MIN_SUPPLEMENTARY, new char[0],  0, IndexOutOfBoundsException.class);
   595         callToChars3(MIN_SUPPLEMENTARY, new char[1],  0, IndexOutOfBoundsException.class);
   616         callToChars3(MIN_SUPPLEMENTARY, new char[1],  0, IndexOutOfBoundsException.class);
   596         callToChars3(MIN_SUPPLEMENTARY, new char[2], -1, IndexOutOfBoundsException.class);
   617         callToChars3(MIN_SUPPLEMENTARY, new char[2], -1, IndexOutOfBoundsException.class);
   597         callToChars3(MIN_SUPPLEMENTARY, new char[2],  1, IndexOutOfBoundsException.class);
   618         callToChars3(MIN_SUPPLEMENTARY, new char[2],  1, IndexOutOfBoundsException.class);
       
   619 
       
   620         callToString(Character.MIN_CODE_POINT - 1, IllegalArgumentException.class);
       
   621         callToString(Character.MAX_CODE_POINT + 1, IllegalArgumentException.class);
   598     }
   622     }
   599 
   623 
   600     static final boolean At = true, Before = false;
   624     static final boolean At = true, Before = false;
   601 
   625 
   602     /**
   626     /**
   832         }
   856         }
   833         throw new RuntimeException("offsetCodePointCounts(char[]...) didn't throw "
   857         throw new RuntimeException("offsetCodePointCounts(char[]...) didn't throw "
   834                                    + expectedException.getName());
   858                                    + expectedException.getName());
   835     }
   859     }
   836 
   860 
       
   861     private static void callToString(int codePoint, Class expectedException) {
       
   862         try {
       
   863             String s = Character.toString(codePoint);
       
   864         } catch (Exception e) {
       
   865             if (expectedException.isInstance(e)) {
       
   866                 return;
       
   867             }
       
   868             throw new RuntimeException("Unspecified exception", e);
       
   869         }
       
   870         throw new RuntimeException("toString(int) didn't throw "
       
   871                                    + expectedException.getName());
       
   872     }
       
   873 
   837     private static String getType(Object data) {
   874     private static String getType(Object data) {
   838         return (data instanceof CharSequence) ? "CharSequence" : "char[]";
   875         return (data instanceof CharSequence) ? "CharSequence" : "char[]";
   839     }
   876     }
   840 
   877 
   841     private static String toHexString(int c) {
   878     private static String toHexString(int c) {