test/jdk/java/lang/String/Supplementary.java
author jlaskey
Fri, 18 May 2018 08:43:49 -0300
changeset 50175 589ed2770141
parent 47216 71c04702a3d5
permissions -rw-r--r--
8200436: String::isBlank Reviewed-by: sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5987
diff changeset
     2
 * Copyright (c) 2003, 2010, 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 4922962 4985217 5017280 6242664 6588260
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 indexOf(int ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        test4();        // Test for indexOf(int ch, int fromIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        test5();        // Test for lastIndexOf(int ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        test6();        // Test for lastIndexOf(int ch, int fromIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        test7();        // Test for String(int[] codePoint, int offset, int count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        test8();        // Test for codePointCount(int beginIndex, int endIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        test9();        // Test for offsetByCodePoints(int index, int offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        test10();       // Test for offsetByCodePoints(int index, int offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                        // To verify the fix for 6242664
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /* Text strings which are used as input data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * The comment above each text string means the index of each 16-bit char
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * for convenience.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static final String[] input = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
      /*                               111     1     111111     22222
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
         0123     4     5678     9     012     3     456789     01234 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        "abc\uD800\uDC00def\uD800\uD800ab\uD800\uDC00cdefa\uDC00bcdef",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
      /*                          1     1111     1111     1     222
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
         0     12345     6789     0     1234     5678     9     012     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        "\uD800defg\uD800hij\uD800\uDC00klm\uDC00nop\uDC00\uD800rt\uDC00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
      /*                          11     1     1111     1     112     222
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
         0     12345     6     78901     2     3456     7     890     123     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        "\uDC00abcd\uDBFF\uDFFFefgh\uD800\uDC009ik\uDC00\uDC00lm\uDC00no\uD800",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
      /*                                    111     111111     1 22     2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
         0     1     2345     678     9     012     345678     9 01     2     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        "\uD800\uDC00!#$\uD800%&\uD800\uDC00;+\uDC00<>;=^\uDC00\\@\uD800\uDC00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
    65
        // includes an undefined supplementary character in Unicode 4.0.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
      /*                                    1     11     1     1111     1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         0     1     2345     6     789     0     12     3     4567     8     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        "\uDB40\uDE00abc\uDE01\uDB40de\uDB40\uDE02f\uDB40\uDE03ghi\uDB40\uDE02",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // all supplementary characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
      /*                                                             1     1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         0     1     2     3     4     5     6     7     8     9     0     1     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        "\uD800\uDC00\uD800\uDC01\uD800\uDC02\uD800\uDC03\uD800\uDC04\uD800\uDC05"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
      /* 1     1     1     1     1     1     1     1     2     2     2     2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         2     3     4     5     6     7     8     9     0     1     2     3     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        "\uD800\uDC06\uD800\uDC07\uD800\uDC08\uD800\uDC08\uD800\uDC09\uD800\uDC0A"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
      /* 2     2     2     2     2     2     3     3     3     3     3     3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
         4     5     6     7     8     9     0     1     2     3     4     5     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        "\uD800\uDC0B\uD800\uDC0C\uD800\uDC0D\uD800\uDC0A\uD800\uDC0F\uD800\uDC10"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /* Expected results for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *     test1(): for codePointAt()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Each character in each array is the golden data for each text string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * in the above input data. For example, the first data in each array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * for the first input string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static final int[][] golden1 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        {'a',    0xD800,  0xDC00, 0x10000, 0xE0200, 0x10000},// codePointAt(0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        {0xD800, 0x10000, 'g',    0xDC00,  0xE0202, 0xDC04}, // codePointAt(9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        {'f',    0xDC00,  0xD800, 0xDC00,  0xDE02,  0xDC10}, // codePointAt(length-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        {'f',    'p',     0xDC00, '^',     0xE0202, 0xDC08}, // codePointAt() for a substring
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Test for codePointAt(int index) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static void test1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            String s = input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
             * Normal case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            testCodePoint(At, s, 0, golden1[0][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            testCodePoint(At, s, 9, golden1[1][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            testCodePoint(At, s, s.length()-1, golden1[2][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            testCodePoint(At, s.substring(17), 0, golden1[3][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
             * Abnormal case - verify that an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            testCodePoint(At, s, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            testCodePoint(At, s, s.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /* Expected results for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *     test2(): for codePointBefore()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Each character in each array is the golden data for each text string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * in the above input data. For example, the first data in each array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * for the first input string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    static final int[][] golden2 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        {'a',    0xD800, 0xDC00,  0xD800,  0xDB40,  0xD800}, // codePointBefore(1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        {0xD800, 'l',    0x10000, 0xDC00,  0xDB40,  0xD800}, // codePointBefore(13)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        {'f',    0xDC00, 0xD800,  0x10000, 0xE0202, 0x10010},// codePointBefore(length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        {'b',    'd',    'a',     0xDC00,  0xDE00,  0xDC00}, // codePointBefore() for a substring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Test for codePointBefore(int index) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    static void test2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            String s = input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
             * Normal case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            testCodePoint(Before, s, 1, golden2[0][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            testCodePoint(Before, s, 13, golden2[1][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            testCodePoint(Before, s, s.length(), golden2[2][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            testCodePoint(Before, s.substring(1), 1, golden2[3][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
             * Abnormal case - verify that an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            testCodePoint(Before, s, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            testCodePoint(Before, s, s.length()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /* Expected results for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *     test3(): for indexOf(int ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *     test4(): for indexOf(int ch, int fromIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *     test5(): for lastIndexOf(int ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *     test6(): for lastIndexOf(int ch, int fromIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Unlike golden1 and golden2, golden3[m][] is the golden data for text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * string in input[m].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * The meaning of each element in golden3[][n]
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   171
     *   golden3[][0]: character which is searched.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *   golden3[][2]: the golden data for indexOf(int ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *   From golden3[][2] to golden3[][n-1]:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *       the golden data for indexOf(int ch, int fromIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *   The golden3[][n-2]: the golden data for lastIndexOf(int ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *   From golden3[][1] to golden3[][n-2]:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *       the golden data for lastIndexOf(int ch, int fromIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *   In other words, the data format is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *     { ch, -1, index1, index2, ..., -1}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *   where index1, index2, ... are all indices of the ch occurrences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    static final int[][] golden3 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
      /* ch       indices */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        {'b',     -1,  1, 11, 20,  -1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        {0xD800,  -1,  0,  5,  9,  19, -1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        {0xDC00,  -1,  0, 12, 16,  17, 20, -1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        {0x10000, -1,  0,  8, 21,  -1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        {0xE0202, -1,  9, 17, -1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        {0x1000A, -1, 22, 30, -1}
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 indexOf(int ch) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    static void test3() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            String s = input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
             * Normal case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
             */
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   204
            testIndexOf(s, golden3[i][0], golden3[i][2]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
             * Abnormal case - char which isn't included in the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
             */
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   209
            testIndexOf(s, 'Z', -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   210
            testIndexOf(s, 0xDB98, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   211
            testIndexOf(s, 0xDE76, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   212
            testIndexOf(s, 0x12345, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   213
            testIndexOf(s, -1, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   214
            testIndexOf(s, 0x110000, -1);
2
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Test for indexOf(int ch, int fromIndex) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    static void test4() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            String s = input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            int ch = golden3[i][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
             * Normal case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            int fromIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            for (int j = 2; j < golden3[i].length; j++) {
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   232
                fromIndex = testIndexOf(s, fromIndex, ch,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                        golden3[i][j]) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
             * Abnormal case1 - char is included in the string but fromIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
             *                  is incorrect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
             */
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   240
            testIndexOf(s, -1, ch, golden3[i][2]);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   241
            testIndexOf(s, s.length(), ch,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        golden3[i][golden3[i].length-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
             * Abnormal case2 - char which isn't included in the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
             */
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   247
            testIndexOf(s, 0, 'Z', -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   248
            testIndexOf(s, 0, 0xDB98, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   249
            testIndexOf(s, 0, 0xDE76, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   250
            testIndexOf(s, 0, 0x12345, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   251
            testIndexOf(s, 0, -1, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   252
            testIndexOf(s, 0, 0x110000, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Test for lastIndexOf(int ch) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    static void test5() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            String s = input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
             * Normal case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
             */
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   267
            testLastIndexOf(s, golden3[i][0],
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        golden3[i][golden3[i].length-2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
             * Abnormal case - char which isn't included in the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
             */
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   273
            testLastIndexOf(s, 'Z', -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   274
            testLastIndexOf(s, 0xDB98, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   275
            testLastIndexOf(s, 0xDE76, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   276
            testLastIndexOf(s, 0x12345, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   277
            testLastIndexOf(s, -1, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   278
            testLastIndexOf(s, 0x110000, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Test for lastIndexOf(int ch, int fromIndex) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    static void test6() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            String s = input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            int ch = golden3[i][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
             * Normal case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            int fromIndex = len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            for (int j = golden3[i].length - 2; j > 0; j--) {
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   297
                fromIndex = testLastIndexOf(s, fromIndex, ch,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                        golden3[i][j]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
             * Abnormal case1 - char is included in the string but fromIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
             *                  is incorrect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
             */
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   305
            testLastIndexOf(s, -1, ch, golden3[i][1]);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   306
            testLastIndexOf(s, len, ch, golden3[i][golden3[i].length-2]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
             * Abnormal case2 - char which isn't included in the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
             */
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   311
            testLastIndexOf(s, len, 'Z', -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   312
            testLastIndexOf(s, len, 0xDB98, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   313
            testLastIndexOf(s, len, 0xDE76, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   314
            testLastIndexOf(s, len, 0x12345, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   315
            testLastIndexOf(s, len, -1, -1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   316
            testLastIndexOf(s, len, 0x110000, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Test for String(int[] codePoint, int offset, int count).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    static void test7() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            String s = input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            int nCodePoints = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            for (int j = 0; j < s.length(); j += Character.charCount(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                c = s.codePointAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                nCodePoints++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            int[] codePoints = new int[nCodePoints];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            int count = 0, mid = 0, offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            for (int j = 0; j < s.length(); j += Character.charCount(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                if (mid == 0 && j >= s.length()/2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    mid = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    offset = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                c = s.codePointAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                codePoints[count++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            String cps = new String(codePoints, 0, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            check(!s.equals(cps), "new String(int[]...) with input[" + i + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            cps = new String(codePoints, 0, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            check(!s.substring(0, mid).equals(cps),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                  "first half: new String(int[]...) with input[" + i + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            cps = new String(codePoints, offset, count - offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            check(!s.substring(mid).equals(cps),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                  "second half: new String(int[]...) with input[" + i + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            // test exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            testNewString(null, 0, count, NullPointerException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            testNewString(codePoints, -1, count, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            testNewString(codePoints, 0, count+1, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            testNewString(codePoints, offset, count, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            testNewString(codePoints, offset, -1, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            testNewString(codePoints, count, 1, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            codePoints[offset] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            testNewString(codePoints, 0, count, IllegalArgumentException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            codePoints[offset] = Character.MAX_CODE_POINT+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            testNewString(codePoints, 0, count, IllegalArgumentException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            // 6588260: (str) ArrayIndexOutOfBoundsException when trying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            // to create a String from codePoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            //int[] x = new int[Character.MAX_CODE_POINT+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            int[] x = new int[Character.MAX_CODE_POINT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            for (int i = 0; i < x.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                if (i != 0xdbff) // For round-trip safety
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    x[i] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            final String s = new String(x, 0, x.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            check(s.codePointCount(0, s.length()) != x.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                  "s.codePointCount(0, s.length()) != x.length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            check(s.length() <= x.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                  "s.length() <= x.length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            for (int i = 0, j = 0; i < x.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                int c = s.codePointAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                check(c != x[i], "c != x[i]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                j += Character.charCount(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Test codePointCount(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * This test case assumes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Character.codePointCount(CharSequence, int, int) works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    static void test8() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            String str = input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            int length = str.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            for (int j = 0; j <= length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                int result = str.codePointCount(j, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                int expected = Character.codePointCount(str, j, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                check(result != expected, "codePointCount(input["+i+"], "+j+", "+length+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                // Create a substring of the text range. It shares the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                // underlying char[] of the String str.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                String substr = str.substring(j, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                result = substr.codePointCount(0, substr.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                check(result != expected, "substring:codePointCount(input["+i+"], "+j+", "+length+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            for (int j = length; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                int result = str.codePointCount(0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                int expected = Character.codePointCount(str, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                check(result != expected, "codePointCount(input["+i+"], 0, "+j+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                String substr = str.substring(0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                result = substr.codePointCount(0, substr.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                check(result != expected, "substring:codePointCount(input["+i+"], 0, "+j+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            // test exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            testCodePointCount(null, 0, 0, NullPointerException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            testCodePointCount(str, -1, length, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            testCodePointCount(str, 0, length+1, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            testCodePointCount(str, length, length-1, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Test offsetByCodePoints(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * This test case assumes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * Character.codePointCount(CharSequence, int, int) works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    static void test9() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            String str = input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            int length = str.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            for (int j = 0; j <= length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                int nCodePoints = Character.codePointCount(str, j, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                int result = str.offsetByCodePoints(j, nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                check(result != length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                      "offsetByCodePoints(input["+i+"], "+j+", "+nCodePoints+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                      result, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                result = str.offsetByCodePoints(length, -nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                int expected = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                if (j > 0 && j < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    int cp = str.codePointBefore(j+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    if (Character.isSupplementaryCodePoint(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        expected--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                check(result != expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                      "offsetByCodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            for (int j = length; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                int nCodePoints = Character.codePointCount(str, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                int result = str.offsetByCodePoints(0, nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                int expected = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                if (j > 0 && j < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    int cp = str.codePointAt(j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                     if (Character.isSupplementaryCodePoint(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        expected++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                check(result != expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                      "offsetByCodePoints(input["+i+"], 0, "+nCodePoints+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                result = str.offsetByCodePoints(j, -nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                check(result != 0,
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   474
                      "offsetByCodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                      result, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            // test exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            testOffsetByCodePoints(null, 0, 0, NullPointerException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            testOffsetByCodePoints(str, -1, length, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            testOffsetByCodePoints(str, 0, length+1, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            testOffsetByCodePoints(str, 1, -2, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            testOffsetByCodePoints(str, length, length-1, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            testOffsetByCodePoints(str, length, -(length+1), IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Test offsetByCodePoints(int, int) - to verify the fix for 6242664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * This test case assumes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * Character.codePointCount(CharSequence, int, int) works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    static void test10() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        String header = "H\uD800e\uDFFFa\uDBFF\uDC00der<";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            String wholeString = header + input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            String str = wholeString.substring(header.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            int length = str.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            for (int j = 0; j <= length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                int nCodePoints = Character.codePointCount(str, j, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                int result = str.offsetByCodePoints(j, nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                check(result != length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                      "offsetByCodePoints(input["+i+"], "+j+", "+nCodePoints+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                      result, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                result = str.offsetByCodePoints(length, -nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                int expected = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                if (j > 0 && j < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    int cp = str.codePointBefore(j+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    if (Character.isSupplementaryCodePoint(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                        expected--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                check(result != expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                      "offsetByCodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            for (int j = length; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                int nCodePoints = Character.codePointCount(str, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                int result = str.offsetByCodePoints(0, nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                int expected = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                if (j > 0 && j < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    int cp = str.codePointAt(j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                     if (Character.isSupplementaryCodePoint(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                        expected++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                check(result != expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                      "offsetByCodePoints(input["+i+"], 0, "+nCodePoints+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                result = str.offsetByCodePoints(j, -nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                check(result != 0,
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   534
                      "offsetByCodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                      result, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    static final boolean At = true, Before = false;
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   542
    static final boolean FIRST = true, LAST = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    static void testCodePoint(boolean isAt, String s, int index, int expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        int c = isAt ? s.codePointAt(index) : s.codePointBefore(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        check(c != expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
              "codePoint" + (isAt ? "At" : "Before") + "(" + index + ") for <"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
              + s + ">", c, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    static void testCodePoint(boolean isAt, String s, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        boolean exceptionOccurred = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            int c = isAt ? s.codePointAt(index) : s.codePointBefore(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        catch (StringIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            exceptionOccurred = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        check(!exceptionOccurred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
              "codePoint" + (isAt ? "At" : "Before") + "(" + index + ") for <"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
              + s + "> should throw StringIndexOutOfBoundsPointerException.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   566
    static void testIndexOf(String s, int c, int expected) {
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   567
        testIndexOf2(s, c, expected);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   568
        if (s.indexOf(c) != -1) {
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   569
            testIndexOf2(s + (char) c, c, expected);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   570
            if (Character.isSupplementaryCodePoint(c)) {
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   571
                char[] surrogates = Character.toChars(c);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   572
                testIndexOf2(s + new String(surrogates), c, expected);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   573
                testIndexOf2(s + surrogates[0], c, expected);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   574
                testIndexOf2(s + surrogates[1], c, expected);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   575
                testIndexOf2(new String(surrogates) + s, c, 0);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   576
                testIndexOf2(surrogates[0] + s, c, expected + 1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   577
                testIndexOf2(surrogates[1] + s, c, expected + 1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   578
            }
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   579
        }
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   580
    }
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   581
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   582
    static void testIndexOf2(String s, int c, int expected) {
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   583
        int index = s.indexOf(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        check(index != expected,
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   586
              "indexOf(" + toHexString(c) + ") for <" + s + ">",
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   587
              index, expected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   590
    static void testLastIndexOf(String s, int c, int expected) {
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   591
        testLastIndexOf2(s, c, expected);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   592
        if (s.lastIndexOf(c) != -1) {
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   593
            testLastIndexOf2((char) c + s, c, expected + 1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   594
            if (Character.isSupplementaryCodePoint(c)) {
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   595
                char[] surrogates = Character.toChars(c);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   596
                testLastIndexOf2(s + new String(surrogates), c, s.length());
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   597
                testLastIndexOf2(s + surrogates[0], c, expected);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   598
                testLastIndexOf2(s + surrogates[1], c, expected);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   599
                testLastIndexOf2(new String(surrogates) + s, c, expected + 2);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   600
                testLastIndexOf2(surrogates[0] + s, c, expected + 1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   601
                testLastIndexOf2(surrogates[1] + s, c, expected + 1);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   602
            }
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   603
        }
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   604
    }
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   605
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   606
    static void testLastIndexOf2(String s, int c, int expected) {
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   607
        int index = s.lastIndexOf(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        check(index != expected,
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   610
              "lastIndexOf(" + toHexString(c) + ") for <" + s + ">",
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   611
              index, expected);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   612
    }
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   613
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   614
    static int testIndexOf(String s, int fromIndex, int c, int expected) {
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   615
        int index = s.indexOf(c, fromIndex);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   616
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   617
        check(index != expected,
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   618
              "indexOf(" + toHexString(c) + ", "
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   619
              + fromIndex + ") for <" + s + ">",
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   620
              index, expected);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   621
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   622
        return index;
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   623
    }
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   624
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   625
    static int testLastIndexOf(String s, int fromIndex, int c, int expected) {
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   626
        int index = s.lastIndexOf(c, fromIndex);
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   627
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   628
        check(index != expected,
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   629
              "lastIndexOf(" + toHexString(c) + ", "
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   630
              + fromIndex + ") for <" + s + ">",
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   631
              index, expected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    static void testNewString(int[] codePoints, int offset, int count, Class expectedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            String s = new String(codePoints, offset, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            if (expectedException.isInstance(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            throw new RuntimeException("Error: Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        check(true, "new String(int[]...) didn't throw " + expectedException.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    static void testCodePointCount(String str, int beginIndex, int endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                                   Class expectedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            int n = str.codePointCount(beginIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            if (expectedException.isInstance(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            throw new RuntimeException("Error: Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        check(true, "codePointCount() didn't throw " + expectedException.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    static void testOffsetByCodePoints(String str, int index, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                                       Class expectedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            int n = str.offsetByCodePoints(index, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            if (expectedException.isInstance(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            throw new RuntimeException("Error: Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        check(true, "offsetByCodePoints() didn't throw " + expectedException.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    static void check(boolean err, String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            throw new RuntimeException("Error: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    static void check(boolean err, String s, int got, int expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            throw new RuntimeException("Error: " + s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                                       + " returned an unexpected value. got "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                                       + toHexString(got)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                                       + ", expected "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                                       + toHexString(expected));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    private static String toHexString(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        return "0x" + Integer.toHexString(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
}