< prev index next >

test/jdk/java/lang/Character/Supplementary.java

Print this page
rev 49071 : [mq]: 4993841


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4533872 4985214 4985217 5017268 5017280
  27  * @summary Unit tests for supplementary character support (JSR-204)
  28  * @compile Supplementary.java
  29  * @run main/timeout=600 Supplementary
  30  */
  31 
  32 public class Supplementary {
  33     private static final char MIN_HIGH = '\uD800';
  34     private static final char MAX_HIGH = '\uDBFF';
  35     private static final char MIN_LOW = MAX_HIGH + 1;
  36     private static final char MAX_LOW = '\uDFFF';
  37     private static final int MIN_CODE_POINT = 0x000000;
  38     private static final int MIN_SUPPLEMENTARY = 0x010000;
  39     private static final int MAX_SUPPLEMENTARY = 0x10ffff;
  40 
  41     public static void main(String[] args) {
  42         // Do not change the order of test method calls since there
  43         // are some interdependencies.
  44 
  45         testConstants();
  46 
  47         test00();
  48 
  49         // Store all Unicode code points, except for surrogate code
  50         // points, in cu[] through the loops below. Then, use the data
  51         // for code point/code unit conversion and other tests later.
  52         char[] cu = new char[(MAX_SUPPLEMENTARY+1) * 2];
  53         int length = test01(cu);
  54 
  55         String str = new String(cu, 0, length);
  56         cu = null;
  57         test02(str);
  58         test03(str.toCharArray());
  59         test04(str);
  60         test05(str);
  61 



  62         // Test unpaired surrogates
  63         testUnpaired();
  64 
  65         // Test exceptions
  66         testExceptions00();
  67         testExceptions01(str);
  68         testExceptions02(str.toCharArray());
  69     }
  70 
  71     static void testConstants() {
  72         if (Character.MIN_HIGH_SURROGATE != MIN_HIGH) {
  73             constantError("MIN_HIGH_SURROGATE", Character.MIN_HIGH_SURROGATE, MIN_HIGH);
  74         }
  75         if (Character.MAX_HIGH_SURROGATE != MAX_HIGH) {
  76             constantError("MAX_HIGH_SURROGATE", Character.MAX_HIGH_SURROGATE, MAX_HIGH);
  77         }
  78         if (Character.MIN_LOW_SURROGATE != MIN_LOW) {
  79             constantError("MIN_LOW_SURROGATE", Character.MIN_LOW_SURROGATE, MIN_LOW);
  80         }
  81         if (Character.MAX_LOW_SURROGATE != MAX_LOW) {


 437                 checkNewIndex(a, -nCodePoints, index, i);
 438             }
 439         }
 440 
 441         // test special cases for 0-length text ranges.
 442         length = str.length();
 443         int index = Character.offsetByCodePoints(str, 0, 0);
 444         checkNewIndex(str, 0, index, 0);
 445         index = Character.offsetByCodePoints(str, length, 0);
 446         checkNewIndex(str, 0, index, length);
 447         index = Character.offsetByCodePoints(a, 0, 0, 0, 0);
 448         checkNewIndex(a, 0, index, 0);
 449         index = Character.offsetByCodePoints(a, 0, length, 0, 0);
 450         checkNewIndex(a, 0, index, 0);
 451         index = Character.offsetByCodePoints(a, 0, length, length, 0);
 452         checkNewIndex(a, 0, index, length);
 453         index = Character.offsetByCodePoints(a, length, 0, length, 0);
 454         checkNewIndex(a, 0, index, length);
 455     }
 456 

















 457     private static void checkNewIndex(Object data, int offset, int result, int expected) {
 458         String type = getType(data);
 459         String offsetType = (offset > 0) ? "positive" : (offset < 0) ? "negative" : "0";
 460         if (result != expected) {
 461             throw new RuntimeException("offsetByCodePoints(" + type + ", ...) ["
 462                                        + offsetType + " offset]"
 463                                        + " returned " + result
 464                                        + ", expected " + expected);
 465         }
 466     }
 467 
 468     // Test codePointAt(CharSequence, int)
 469     //      codePointBefore(CharSequence, int)
 470     //      codePointAt(char[], int)
 471     //      codePointBefore(char[], int)
 472     //      toChar(int)
 473     //      toChar(int, char[], int)
 474     // with unpaired surrogates
 475     static void testUnpaired() {
 476         testCodePoint("\uD800", new int[] { 0xD800 });


 563             }
 564         }
 565 
 566         // Test toChars(int, char[], int)
 567         a = new char[codepoints.length * 2];
 568         j = 0;
 569         for (int i = 0; i < codepoints.length; i++) {
 570             int n = Character.toChars(codepoints[i], a, j);
 571             j += n;
 572         }
 573         String s = new String(a, 0, j);
 574         if (!str.equals(s)) {
 575             throw new RuntimeException("toChars(int, char[], int) returned "
 576                                        + toHexString("dst", s.toCharArray())
 577                                        + ", expected " + toHexString("data", str.toCharArray()));
 578         }
 579     }
 580 
 581     // Test toChar(int)
 582     //      toChar(int, char[], int)

 583     // for exceptions
 584     static void testExceptions00() {
 585         callToChars1(-1, IllegalArgumentException.class);
 586         callToChars1(MAX_SUPPLEMENTARY + 1, IllegalArgumentException.class);
 587 
 588         callToChars3(MAX_SUPPLEMENTARY, null, 0, NullPointerException.class);
 589         callToChars3(-MIN_SUPPLEMENTARY,    new char[2], 0, IllegalArgumentException.class);
 590         callToChars3(MAX_SUPPLEMENTARY + 1, new char[2], 0, IllegalArgumentException.class);
 591         callToChars3('A', new char[0],  0, IndexOutOfBoundsException.class);
 592         callToChars3('A', new char[1], -1, IndexOutOfBoundsException.class);
 593         callToChars3('A', new char[1],  1, IndexOutOfBoundsException.class);
 594         callToChars3(MIN_SUPPLEMENTARY, new char[0],  0, IndexOutOfBoundsException.class);
 595         callToChars3(MIN_SUPPLEMENTARY, new char[1],  0, IndexOutOfBoundsException.class);
 596         callToChars3(MIN_SUPPLEMENTARY, new char[2], -1, IndexOutOfBoundsException.class);
 597         callToChars3(MIN_SUPPLEMENTARY, new char[2],  1, IndexOutOfBoundsException.class);



 598     }
 599 
 600     static final boolean At = true, Before = false;
 601 
 602     /**
 603      * Test codePointAt(CharSequence, int)
 604      *      codePointBefore(CharSequence, int)
 605      *      codePointCount(CharSequence, int, int)
 606      *      offsetByCodePoints(CharSequence, int, int)
 607      * for exceptions
 608      */
 609     static void testExceptions01(CharSequence cs) {
 610         CharSequence nullSeq = null;
 611         // codePointAt
 612         callCodePoint(At, nullSeq, 0, NullPointerException.class);
 613         callCodePoint(At, cs, -1, IndexOutOfBoundsException.class);
 614         callCodePoint(At, cs, cs.length(), IndexOutOfBoundsException.class);
 615         callCodePoint(At, cs, cs.length()*3, IndexOutOfBoundsException.class);
 616 
 617         // codePointBefore


 817         throw new RuntimeException("offsetCodePointCounts(CharSequnce...) didn't throw "
 818                                    + expectedException.getName());
 819     }
 820 
 821 
 822     private static void callOffsetByCodePoints(char[] a, int start, int count,
 823                                                int index, int offset,
 824                                                Class expectedException) {
 825         try {
 826             int n = Character.offsetByCodePoints(a, start, count, index, offset);
 827         } catch (Exception e) {
 828             if (expectedException.isInstance(e)) {
 829                 return;
 830             }
 831             throw new RuntimeException("Unspecified exception", e);
 832         }
 833         throw new RuntimeException("offsetCodePointCounts(char[]...) didn't throw "
 834                                    + expectedException.getName());
 835     }
 836 













 837     private static String getType(Object data) {
 838         return (data instanceof CharSequence) ? "CharSequence" : "char[]";
 839     }
 840 
 841     private static String toHexString(int c) {
 842         return "0x" + Integer.toHexString(c);
 843     }
 844 
 845     private static String toHexString(String name, char[] a) {
 846         StringBuffer sb = new StringBuffer();
 847         for (int i = 0; i < a.length; i++) {
 848             if (i > 0) {
 849                 sb.append(", ");
 850             }
 851             sb.append(name).append('[').append(i).append("]=");
 852             sb.append(toHexString(a[i]));
 853         }
 854         return sb.toString();
 855     }
 856 }


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4533872 4985214 4985217 4993841 5017268 5017280
  27  * @summary Unit tests for supplementary character support (JSR-204)
  28  * @compile Supplementary.java
  29  * @run main/timeout=600 Supplementary
  30  */
  31 
  32 public class Supplementary {
  33     private static final char MIN_HIGH = '\uD800';
  34     private static final char MAX_HIGH = '\uDBFF';
  35     private static final char MIN_LOW = MAX_HIGH + 1;
  36     private static final char MAX_LOW = '\uDFFF';
  37     private static final int MIN_CODE_POINT = 0x000000;
  38     private static final int MIN_SUPPLEMENTARY = 0x010000;
  39     private static final int MAX_SUPPLEMENTARY = 0x10ffff;
  40 
  41     public static void main(String[] args) {
  42         // Do not change the order of test method calls since there
  43         // are some interdependencies.
  44 
  45         testConstants();
  46 
  47         test00();
  48 
  49         // Store all Unicode code points, except for surrogate code
  50         // points, in cu[] through the loops below. Then, use the data
  51         // for code point/code unit conversion and other tests later.
  52         char[] cu = new char[(MAX_SUPPLEMENTARY+1) * 2];
  53         int length = test01(cu);
  54 
  55         String str = new String(cu, 0, length);
  56         cu = null;
  57         test02(str);
  58         test03(str.toCharArray());
  59         test04(str);
  60         test05(str);
  61 
  62         // Test for toString(int)
  63         test06();
  64 
  65         // Test unpaired surrogates
  66         testUnpaired();
  67 
  68         // Test exceptions
  69         testExceptions00();
  70         testExceptions01(str);
  71         testExceptions02(str.toCharArray());
  72     }
  73 
  74     static void testConstants() {
  75         if (Character.MIN_HIGH_SURROGATE != MIN_HIGH) {
  76             constantError("MIN_HIGH_SURROGATE", Character.MIN_HIGH_SURROGATE, MIN_HIGH);
  77         }
  78         if (Character.MAX_HIGH_SURROGATE != MAX_HIGH) {
  79             constantError("MAX_HIGH_SURROGATE", Character.MAX_HIGH_SURROGATE, MAX_HIGH);
  80         }
  81         if (Character.MIN_LOW_SURROGATE != MIN_LOW) {
  82             constantError("MIN_LOW_SURROGATE", Character.MIN_LOW_SURROGATE, MIN_LOW);
  83         }
  84         if (Character.MAX_LOW_SURROGATE != MAX_LOW) {


 440                 checkNewIndex(a, -nCodePoints, index, i);
 441             }
 442         }
 443 
 444         // test special cases for 0-length text ranges.
 445         length = str.length();
 446         int index = Character.offsetByCodePoints(str, 0, 0);
 447         checkNewIndex(str, 0, index, 0);
 448         index = Character.offsetByCodePoints(str, length, 0);
 449         checkNewIndex(str, 0, index, length);
 450         index = Character.offsetByCodePoints(a, 0, 0, 0, 0);
 451         checkNewIndex(a, 0, index, 0);
 452         index = Character.offsetByCodePoints(a, 0, length, 0, 0);
 453         checkNewIndex(a, 0, index, 0);
 454         index = Character.offsetByCodePoints(a, 0, length, length, 0);
 455         checkNewIndex(a, 0, index, length);
 456         index = Character.offsetByCodePoints(a, length, 0, length, 0);
 457         checkNewIndex(a, 0, index, length);
 458     }
 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 
 477     private static void checkNewIndex(Object data, int offset, int result, int expected) {
 478         String type = getType(data);
 479         String offsetType = (offset > 0) ? "positive" : (offset < 0) ? "negative" : "0";
 480         if (result != expected) {
 481             throw new RuntimeException("offsetByCodePoints(" + type + ", ...) ["
 482                                        + offsetType + " offset]"
 483                                        + " returned " + result
 484                                        + ", expected " + expected);
 485         }
 486     }
 487 
 488     // Test codePointAt(CharSequence, int)
 489     //      codePointBefore(CharSequence, int)
 490     //      codePointAt(char[], int)
 491     //      codePointBefore(char[], int)
 492     //      toChar(int)
 493     //      toChar(int, char[], int)
 494     // with unpaired surrogates
 495     static void testUnpaired() {
 496         testCodePoint("\uD800", new int[] { 0xD800 });


 583             }
 584         }
 585 
 586         // Test toChars(int, char[], int)
 587         a = new char[codepoints.length * 2];
 588         j = 0;
 589         for (int i = 0; i < codepoints.length; i++) {
 590             int n = Character.toChars(codepoints[i], a, j);
 591             j += n;
 592         }
 593         String s = new String(a, 0, j);
 594         if (!str.equals(s)) {
 595             throw new RuntimeException("toChars(int, char[], int) returned "
 596                                        + toHexString("dst", s.toCharArray())
 597                                        + ", expected " + toHexString("data", str.toCharArray()));
 598         }
 599     }
 600 
 601     // Test toChar(int)
 602     //      toChar(int, char[], int)
 603     //      toString(int)
 604     // for exceptions
 605     static void testExceptions00() {
 606         callToChars1(-1, IllegalArgumentException.class);
 607         callToChars1(MAX_SUPPLEMENTARY + 1, IllegalArgumentException.class);
 608 
 609         callToChars3(MAX_SUPPLEMENTARY, null, 0, NullPointerException.class);
 610         callToChars3(-MIN_SUPPLEMENTARY,    new char[2], 0, IllegalArgumentException.class);
 611         callToChars3(MAX_SUPPLEMENTARY + 1, new char[2], 0, IllegalArgumentException.class);
 612         callToChars3('A', new char[0],  0, IndexOutOfBoundsException.class);
 613         callToChars3('A', new char[1], -1, IndexOutOfBoundsException.class);
 614         callToChars3('A', new char[1],  1, IndexOutOfBoundsException.class);
 615         callToChars3(MIN_SUPPLEMENTARY, new char[0],  0, IndexOutOfBoundsException.class);
 616         callToChars3(MIN_SUPPLEMENTARY, new char[1],  0, IndexOutOfBoundsException.class);
 617         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);
 622     }
 623 
 624     static final boolean At = true, Before = false;
 625 
 626     /**
 627      * Test codePointAt(CharSequence, int)
 628      *      codePointBefore(CharSequence, int)
 629      *      codePointCount(CharSequence, int, int)
 630      *      offsetByCodePoints(CharSequence, int, int)
 631      * for exceptions
 632      */
 633     static void testExceptions01(CharSequence cs) {
 634         CharSequence nullSeq = null;
 635         // codePointAt
 636         callCodePoint(At, nullSeq, 0, NullPointerException.class);
 637         callCodePoint(At, cs, -1, IndexOutOfBoundsException.class);
 638         callCodePoint(At, cs, cs.length(), IndexOutOfBoundsException.class);
 639         callCodePoint(At, cs, cs.length()*3, IndexOutOfBoundsException.class);
 640 
 641         // codePointBefore


 841         throw new RuntimeException("offsetCodePointCounts(CharSequnce...) didn't throw "
 842                                    + expectedException.getName());
 843     }
 844 
 845 
 846     private static void callOffsetByCodePoints(char[] a, int start, int count,
 847                                                int index, int offset,
 848                                                Class expectedException) {
 849         try {
 850             int n = Character.offsetByCodePoints(a, start, count, index, offset);
 851         } catch (Exception e) {
 852             if (expectedException.isInstance(e)) {
 853                 return;
 854             }
 855             throw new RuntimeException("Unspecified exception", e);
 856         }
 857         throw new RuntimeException("offsetCodePointCounts(char[]...) didn't throw "
 858                                    + expectedException.getName());
 859     }
 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 
 874     private static String getType(Object data) {
 875         return (data instanceof CharSequence) ? "CharSequence" : "char[]";
 876     }
 877 
 878     private static String toHexString(int c) {
 879         return "0x" + Integer.toHexString(c);
 880     }
 881 
 882     private static String toHexString(String name, char[] a) {
 883         StringBuffer sb = new StringBuffer();
 884         for (int i = 0; i < a.length; i++) {
 885             if (i > 0) {
 886                 sb.append(", ");
 887             }
 888             sb.append(name).append('[').append(i).append("]=");
 889             sb.append(toHexString(a[i]));
 890         }
 891         return sb.toString();
 892     }
 893 }
< prev index next >