jdk/test/java/lang/StringBuilder/Supplementary.java
author duke
Wed, 05 Jul 2017 21:46:33 +0200
changeset 38602 b72e50a81d43
parent 23010 6dadb192ad81
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 16714
diff changeset
     2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @bug 4533872 4915683 4985217 5017280
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Unit tests for supplementary character support (JSR-204)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
public class Supplementary {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
        test1();        // Test for codePointAt(int index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        test2();        // Test for codePointBefore(int index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        test3();        // Test for reverse()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        test4();        // Test for appendCodePoint(int codePoint)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        test5();        // Test for codePointCount(int beginIndex, int endIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        test6();        // Test for offsetByCodePoints(int index, int offset)
16714
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 7668
diff changeset
    40
        testDontReadOutOfBoundsTrailingSurrogate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /* Text strings which are used as input data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * The comment above each text string means the index of each 16-bit char
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * for convenience.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final String[] input = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
      /*                               111     1     111111     22222
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
         0123     4     5678     9     012     3     456789     01234 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        "abc\uD800\uDC00def\uD800\uD800ab\uD800\uDC00cdefa\uDC00bcdef",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
      /*                          1     1111     1111     1     222
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
         0     12345     6789     0     1234     5678     9     012     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        "\uD800defg\uD800hij\uD800\uDC00klm\uDC00nop\uDC00\uD800rt\uDC00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
      /*                          11     1     1111     1     112     222
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
         0     12345     6     78901     2     3456     7     890     123     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        "\uDC00abcd\uDBFF\uDFFFefgh\uD800\uDC009ik\uDC00\uDC00lm\uDC00no\uD800",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
      /*                                    111     111111     1 22     2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
         0     1     2345     678     9     012     345678     9 01     2     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        "\uD800\uDC00!#$\uD800%&\uD800\uDC00;+\uDC00<>;=^\uDC00\\@\uD800\uDC00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
    61
        // includes an undefined supplementary character in Unicode 4.0.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
      /*                                    1     11     1     1111     1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
         0     1     2345     6     789     0     12     3     4567     8     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        "\uDB40\uDE00abc\uDE01\uDB40de\uDB40\uDE02f\uDB40\uDE03ghi\uDB40\uDE02",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /* Expected results for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *     test1(): for codePointAt()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Each character in each array is the golden data for each text string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * in the above input data. For example, the first data in each array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * for the first input string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static final int[][] golden1 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        {'a',    0xD800, 0xDC00,  0x10000, 0xE0200}, // codePointAt(0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        {0xD800, 0x10000, 'g',    0xDC00,  0xE0202}, // codePointAt(9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        {'f',    0xDC00,  0xD800, 0xDC00,  0xDE02},  // codePointAt(length-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Test for codePointAt(int index) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static void test1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            StringBuilder sb = new StringBuilder(input[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
             * Normal case
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            testCodePoint(At, sb, 0, golden1[0][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            testCodePoint(At, sb, 9, golden1[1][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            testCodePoint(At, sb, sb.length()-1, golden1[2][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
             * Abnormal case - verify that an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            testCodePoint(At, sb, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            testCodePoint(At, sb, sb.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /* Expected results for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *     test2(): for codePointBefore()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Each character in each array is the golden data for each text string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * in the above input data. For example, the first data in each array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * for the first input string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static final int[][] golden2 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        {'a',    0xD800, 0xDC00,  0xD800,  0xDB40},  // codePointBefore(1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        {0xD800, 'l',    0x10000, 0xDC00,  0xDB40},  // codePointBefore(13)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        {'f',    0xDC00, 0xD800,  0x10000, 0xE0202}, // codePointBefore(length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Test for codePointBefore(int index) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    static void test2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            StringBuilder sb = new StringBuilder(input[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
             * Normal case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            testCodePoint(Before, sb, 1, golden2[0][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            testCodePoint(Before, sb, 13, golden2[1][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            testCodePoint(Before, sb, sb.length(), golden2[2][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
             * Abnormal case - verify that an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            testCodePoint(Before, sb, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            testCodePoint(Before, sb, sb.length()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /* Expected results for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *     test3(): for reverse()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Unlike golden1 and golden2, each array is the golden data for each text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * string in the above input data. For example, the first array is  for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * the first input string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    static final String[] golden3 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        "fedcb\uDC00afedc\uD800\uDC00ba\uD800\uD800fed\uD800\uDC00cba",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        "\uDC00tr\uD800\uDC00pon\uDC00mlk\uD800\uDC00jih\uD800gfed\uD800",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        "\uD800on\uDC00ml\uDC00\uDC00ki9\uD800\uDC00hgfe\uDBFF\uDFFFdcba\uDC00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        "\uD800\uDC00@\\\uDC00^=;><\uDC00+;\uD800\uDC00&%\uD800$#!\uD800\uDC00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   155
        // includes an undefined supplementary character in Unicode 4.0.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        "\uDB40\uDE02ihg\uDB40\uDE03f\uDB40\uDE02ed\uDB40\uDE01cba\uDB40\uDE00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // Additional input data & expected result for test3()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    static final String[][] testdata1 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        {"a\uD800\uDC00", "\uD800\uDC00a"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        {"a\uDC00\uD800", "\uD800\uDC00a"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        {"\uD800\uDC00a", "a\uD800\uDC00"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        {"\uDC00\uD800a", "a\uD800\uDC00"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        {"\uDC00\uD800\uD801", "\uD801\uD800\uDC00"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        {"\uDC00\uD800\uDC01", "\uD800\uDC01\uDC00"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        {"\uD801\uD800\uDC00", "\uD800\uDC00\uD801"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        {"\uD800\uDC01\uDC00", "\uDC00\uD800\uDC01"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        {"\uD800\uDC00\uDC01\uD801", "\uD801\uDC01\uD800\uDC00"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Test for reverse() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    static void test3() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            StringBuilder sb = new StringBuilder(input[i]).reverse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            check(!golden3[i].equals(sb.toString()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                 "reverse() for <" + toHexString(input[i]) + ">",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                 sb, golden3[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        for (int i = 0; i < testdata1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            StringBuilder sb = new StringBuilder(testdata1[i][0]).reverse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            check(!testdata1[i][1].equals(sb.toString()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                 "reverse() for <" + toHexString(testdata1[i][0]) + ">",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                 sb, testdata1[i][1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Test for appendCodePoint() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    static void test4() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            String s = input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            for (int j = 0; j < s.length(); j += Character.charCount(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                c = s.codePointAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                StringBuilder rsb = sb.appendCodePoint(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                check(sb != rsb, "appendCodePoint returned a wrong object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                int sbc = sb.codePointAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                check(sbc != c, "appendCodePoint("+j+") != c", sbc, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            check(!s.equals(sb.toString()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                  "appendCodePoint() produced a wrong result with input["+i+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // test exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        testAppendCodePoint(-1, IllegalArgumentException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        testAppendCodePoint(Character.MAX_CODE_POINT+1, IllegalArgumentException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Test codePointCount(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * This test case assumes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Character.codePointCount(CharSequence, int, int) works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    static void test5() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            String s = input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            StringBuilder sb = new StringBuilder(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            int length = sb.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            for (int j = 0; j <= length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                int result = sb.codePointCount(j, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                int expected = Character.codePointCount(sb, j, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                check(result != expected, "codePointCount(input["+i+"], "+j+", "+length+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            for (int j = length; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                int result = sb.codePointCount(0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                int expected = Character.codePointCount(sb, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                check(result != expected, "codePointCount(input["+i+"], 0, "+j+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            // test exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            testCodePointCount(null, 0, 0, NullPointerException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            testCodePointCount(sb, -1, length, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            testCodePointCount(sb, 0, length+1, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            testCodePointCount(sb, length, length-1, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Test offsetByCodePoints(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * This test case assumes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Character.codePointCount(CharSequence, int, int) works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    static void test6() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            String s = input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            StringBuilder sb = new StringBuilder(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            int length = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            for (int j = 0; j <= length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                int nCodePoints = Character.codePointCount(sb, j, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                int result = sb.offsetByCodePoints(j, nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                check(result != length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                      "offsetByCodePoints(input["+i+"], "+j+", "+nCodePoints+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                      result, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                result = sb.offsetByCodePoints(length, -nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                int expected = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                if (j > 0 && j < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    int cp = sb.codePointBefore(j+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    if (Character.isSupplementaryCodePoint(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        expected--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                check(result != expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                      "offsetByCodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            for (int j = length; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                int nCodePoints = Character.codePointCount(sb, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                int result = sb.offsetByCodePoints(0, nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                int expected = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                if (j > 0 && j < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    int cp = sb.codePointAt(j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                     if (Character.isSupplementaryCodePoint(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        expected++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                check(result != expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                      "offsetByCodePoints(input["+i+"], 0, "+nCodePoints+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                result = sb.offsetByCodePoints(j, -nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                check(result != 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                      "offsetBycodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                      result, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            // test exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            testOffsetByCodePoints(null, 0, 0, NullPointerException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            testOffsetByCodePoints(sb, -1, length, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            testOffsetByCodePoints(sb, 0, length+1, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            testOffsetByCodePoints(sb, 1, -2, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            testOffsetByCodePoints(sb, length, length-1, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            testOffsetByCodePoints(sb, length, -(length+1), IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
16714
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 7668
diff changeset
   309
    static void testDontReadOutOfBoundsTrailingSurrogate() {
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 7668
diff changeset
   310
        StringBuilder sb = new StringBuilder();
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 7668
diff changeset
   311
        int suppl = Character.MIN_SUPPLEMENTARY_CODE_POINT;
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 7668
diff changeset
   312
        sb.appendCodePoint(suppl);
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 7668
diff changeset
   313
        check(sb.codePointAt(0) != (int) suppl,
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 7668
diff changeset
   314
              "codePointAt(0)", sb.codePointAt(0), suppl);
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 7668
diff changeset
   315
        check(sb.length() != 2, "sb.length()");
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 7668
diff changeset
   316
        sb.setLength(1);
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 7668
diff changeset
   317
        check(sb.length() != 1, "sb.length()");
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 7668
diff changeset
   318
        check(sb.codePointAt(0) != Character.highSurrogate(suppl),
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 7668
diff changeset
   319
              "codePointAt(0)",
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 7668
diff changeset
   320
              sb.codePointAt(0), Character.highSurrogate(suppl));
cb235d5f8bd4 8010316: Improve handling of char sequences containing surrogates
martin
parents: 7668
diff changeset
   321
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    static final boolean At = true, Before = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    static void testCodePoint(boolean isAt, StringBuilder sb, int index, int expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int c = isAt ? sb.codePointAt(index) : sb.codePointBefore(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        check(c != expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
              "codePoint" + (isAt ? "At" : "Before") + "(" + index + ") for <"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
              + sb + ">", c, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    static void testCodePoint(boolean isAt, StringBuilder sb, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        boolean exceptionOccurred = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            int c = isAt ? sb.codePointAt(index) : sb.codePointBefore(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        catch (StringIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            exceptionOccurred = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        check(!exceptionOccurred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
              "codePoint" + (isAt ? "At" : "Before") + "(" + index + ") for <"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
              + sb + "> should throw StringIndexOutOfBoundsPointerException.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    static void testAppendCodePoint(int codePoint, Class expectedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            new StringBuilder().appendCodePoint(codePoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            if (expectedException.isInstance(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            throw new RuntimeException("Error: Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        check(true, "appendCodePoint(" + toHexString(codePoint) + ") didn't throw "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
              + expectedException.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    static void testCodePointCount(StringBuilder sb, int beginIndex, int endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                   Class expectedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            int n = sb.codePointCount(beginIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            if (expectedException.isInstance(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            throw new RuntimeException("Error: Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        check(true, "codePointCount() didn't throw " + expectedException.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    static void testOffsetByCodePoints(StringBuilder sb, int index, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                       Class expectedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            int n = sb.offsetByCodePoints(index, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            if (expectedException.isInstance(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            throw new RuntimeException("Error: Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        check(true, "offsetByCodePoints() didn't throw " + expectedException.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    static void check(boolean err, String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            throw new RuntimeException("Error: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    static void check(boolean err, String s, int got, int expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            throw new RuntimeException("Error: " + s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                       + " returned an unexpected value. got "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                                       + toHexString(got)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                                       + ", expected "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                                       + toHexString(expected));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    static void check(boolean err, String s, StringBuilder got, String expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            throw new RuntimeException("Error: " + s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                       + " returned an unexpected value. got <"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                       + toHexString(got.toString())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                                       + ">, expected <"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                                       + toHexString(expected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                                       + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    private static String toHexString(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return "0x" + Integer.toHexString(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    private static String toHexString(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        for (int i = 0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            sb.append(" 0x");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            if (c < 0x10) sb.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            if (c < 0x100) sb.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            if (c < 0x1000) sb.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            sb.append(Integer.toHexString(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        sb.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
}