jdk/test/java/lang/StringBuilder/Supplementary.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5987 caec61968454
child 16714 cb235d5f8bd4
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
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 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)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /* Text strings which are used as input data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * The comment above each text string means the index of each 16-bit char
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * for convenience.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static final String[] input = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
      /*                               111     1     111111     22222
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
         0123     4     5678     9     012     3     456789     01234 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        "abc\uD800\uDC00def\uD800\uD800ab\uD800\uDC00cdefa\uDC00bcdef",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
      /*                          1     1111     1111     1     222
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
         0     12345     6789     0     1234     5678     9     012     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        "\uD800defg\uD800hij\uD800\uDC00klm\uDC00nop\uDC00\uD800rt\uDC00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
      /*                          11     1     1111     1     112     222
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
         0     12345     6     78901     2     3456     7     890     123     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        "\uDC00abcd\uDBFF\uDFFFefgh\uD800\uDC009ik\uDC00\uDC00lm\uDC00no\uD800",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
      /*                                    111     111111     1 22     2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
         0     1     2345     678     9     012     345678     9 01     2     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        "\uD800\uDC00!#$\uD800%&\uD800\uDC00;+\uDC00<>;=^\uDC00\\@\uD800\uDC00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
    60
        // includes an undefined supplementary character in Unicode 4.0.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
      /*                                    1     11     1     1111     1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
         0     1     2345     6     789     0     12     3     4567     8     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        "\uDB40\uDE00abc\uDE01\uDB40de\uDB40\uDE02f\uDB40\uDE03ghi\uDB40\uDE02",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /* Expected results for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *     test1(): for codePointAt()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Each character in each array is the golden data for each text string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * in the above input data. For example, the first data in each array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * for the first input string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static final int[][] golden1 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        {'a',    0xD800, 0xDC00,  0x10000, 0xE0200}, // codePointAt(0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        {0xD800, 0x10000, 'g',    0xDC00,  0xE0202}, // codePointAt(9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        {'f',    0xDC00,  0xD800, 0xDC00,  0xDE02},  // codePointAt(length-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Test for codePointAt(int index) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static void test1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            StringBuilder sb = new StringBuilder(input[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
             * Normal case
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            testCodePoint(At, sb, 0, golden1[0][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            testCodePoint(At, sb, 9, golden1[1][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            testCodePoint(At, sb, sb.length()-1, golden1[2][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
             * Abnormal case - verify that an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            testCodePoint(At, sb, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            testCodePoint(At, sb, sb.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
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
    /* Expected results for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *     test2(): for codePointBefore()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Each character in each array is the golden data for each text string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * in the above input data. For example, the first data in each array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * for the first input string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static final int[][] golden2 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        {'a',    0xD800, 0xDC00,  0xD800,  0xDB40},  // codePointBefore(1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        {0xD800, 'l',    0x10000, 0xDC00,  0xDB40},  // codePointBefore(13)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        {'f',    0xDC00, 0xD800,  0x10000, 0xE0202}, // codePointBefore(length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Test for codePointBefore(int index) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static void test2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            StringBuilder sb = new StringBuilder(input[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
             * Normal case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            testCodePoint(Before, sb, 1, golden2[0][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            testCodePoint(Before, sb, 13, golden2[1][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            testCodePoint(Before, sb, sb.length(), golden2[2][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
             * Abnormal case - verify that an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            testCodePoint(Before, sb, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            testCodePoint(Before, sb, sb.length()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
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
    /* Expected results for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *     test3(): for reverse()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Unlike golden1 and golden2, each array is the golden data for each text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * string in the above input data. For example, the first array is  for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * the first input string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    static final String[] golden3 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        "fedcb\uDC00afedc\uD800\uDC00ba\uD800\uD800fed\uD800\uDC00cba",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        "\uDC00tr\uD800\uDC00pon\uDC00mlk\uD800\uDC00jih\uD800gfed\uD800",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        "\uD800on\uDC00ml\uDC00\uDC00ki9\uD800\uDC00hgfe\uDBFF\uDFFFdcba\uDC00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        "\uD800\uDC00@\\\uDC00^=;><\uDC00+;\uD800\uDC00&%\uD800$#!\uD800\uDC00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
5987
caec61968454 6937112: String.lastIndexOf confused by unpaired trailing surrogate
martin
parents: 5506
diff changeset
   154
        // includes an undefined supplementary character in Unicode 4.0.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        "\uDB40\uDE02ihg\uDB40\uDE03f\uDB40\uDE02ed\uDB40\uDE01cba\uDB40\uDE00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // Additional input data & expected result for test3()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    static final String[][] testdata1 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        {"a\uD800\uDC00", "\uD800\uDC00a"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        {"a\uDC00\uD800", "\uD800\uDC00a"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        {"\uD800\uDC00a", "a\uD800\uDC00"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        {"\uDC00\uD800a", "a\uD800\uDC00"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        {"\uDC00\uD800\uD801", "\uD801\uD800\uDC00"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        {"\uDC00\uD800\uDC01", "\uD800\uDC01\uDC00"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        {"\uD801\uD800\uDC00", "\uD800\uDC00\uD801"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        {"\uD800\uDC01\uDC00", "\uDC00\uD800\uDC01"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        {"\uD800\uDC00\uDC01\uD801", "\uD801\uDC01\uD800\uDC00"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Test for reverse() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    static void test3() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            StringBuilder sb = new StringBuilder(input[i]).reverse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            check(!golden3[i].equals(sb.toString()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                 "reverse() for <" + toHexString(input[i]) + ">",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                 sb, golden3[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        for (int i = 0; i < testdata1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            StringBuilder sb = new StringBuilder(testdata1[i][0]).reverse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            check(!testdata1[i][1].equals(sb.toString()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                 "reverse() for <" + toHexString(testdata1[i][0]) + ">",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                 sb, testdata1[i][1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
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
     * Test for appendCodePoint() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    static void test4() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            String s = input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            for (int j = 0; j < s.length(); j += Character.charCount(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                c = s.codePointAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                StringBuilder rsb = sb.appendCodePoint(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                check(sb != rsb, "appendCodePoint returned a wrong object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                int sbc = sb.codePointAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                check(sbc != c, "appendCodePoint("+j+") != c", sbc, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            check(!s.equals(sb.toString()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                  "appendCodePoint() produced a wrong result with input["+i+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // test exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        testAppendCodePoint(-1, IllegalArgumentException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        testAppendCodePoint(Character.MAX_CODE_POINT+1, IllegalArgumentException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Test codePointCount(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * This test case assumes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Character.codePointCount(CharSequence, int, int) works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    static void test5() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            String s = input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            StringBuilder sb = new StringBuilder(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            int length = sb.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            for (int j = 0; j <= length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                int result = sb.codePointCount(j, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                int expected = Character.codePointCount(sb, j, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                check(result != expected, "codePointCount(input["+i+"], "+j+", "+length+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            for (int j = length; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                int result = sb.codePointCount(0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                int expected = Character.codePointCount(sb, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                check(result != expected, "codePointCount(input["+i+"], 0, "+j+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            // test exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            testCodePointCount(null, 0, 0, NullPointerException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            testCodePointCount(sb, -1, length, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            testCodePointCount(sb, 0, length+1, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            testCodePointCount(sb, length, length-1, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
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
     * Test offsetByCodePoints(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * This test case assumes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Character.codePointCount(CharSequence, int, int) works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    static void test6() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        for (int i = 0; i < input.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            String s = input[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            StringBuilder sb = new StringBuilder(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            int length = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            for (int j = 0; j <= length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                int nCodePoints = Character.codePointCount(sb, j, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                int result = sb.offsetByCodePoints(j, nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                check(result != length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                      "offsetByCodePoints(input["+i+"], "+j+", "+nCodePoints+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                      result, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                result = sb.offsetByCodePoints(length, -nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                int expected = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                if (j > 0 && j < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    int cp = sb.codePointBefore(j+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    if (Character.isSupplementaryCodePoint(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        expected--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                check(result != expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                      "offsetByCodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            for (int j = length; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                int nCodePoints = Character.codePointCount(sb, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                int result = sb.offsetByCodePoints(0, nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                int expected = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                if (j > 0 && j < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    int cp = sb.codePointAt(j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                     if (Character.isSupplementaryCodePoint(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        expected++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                check(result != expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                      "offsetByCodePoints(input["+i+"], 0, "+nCodePoints+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                      result, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                result = sb.offsetByCodePoints(j, -nCodePoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                check(result != 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                      "offsetBycodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                      result, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            // test exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            testOffsetByCodePoints(null, 0, 0, NullPointerException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            testOffsetByCodePoints(sb, -1, length, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            testOffsetByCodePoints(sb, 0, length+1, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            testOffsetByCodePoints(sb, 1, -2, IndexOutOfBoundsException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            testOffsetByCodePoints(sb, length, length-1, 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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    static final boolean At = true, Before = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    static void testCodePoint(boolean isAt, StringBuilder sb, int index, int expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        int c = isAt ? sb.codePointAt(index) : sb.codePointBefore(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        check(c != expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
              "codePoint" + (isAt ? "At" : "Before") + "(" + index + ") for <"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
              + sb + ">", c, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    static void testCodePoint(boolean isAt, StringBuilder sb, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        boolean exceptionOccurred = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            int c = isAt ? sb.codePointAt(index) : sb.codePointBefore(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        catch (StringIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            exceptionOccurred = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        check(!exceptionOccurred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
              "codePoint" + (isAt ? "At" : "Before") + "(" + index + ") for <"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
              + sb + "> should throw StringIndexOutOfBoundsPointerException.");
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 testAppendCodePoint(int codePoint, Class expectedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            new StringBuilder().appendCodePoint(codePoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            if (expectedException.isInstance(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            throw new RuntimeException("Error: Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        check(true, "appendCodePoint(" + toHexString(codePoint) + ") didn't throw "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
              + expectedException.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    static void testCodePointCount(StringBuilder sb, int beginIndex, int endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                   Class expectedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            int n = sb.codePointCount(beginIndex, endIndex);
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, "codePointCount() didn't throw " + expectedException.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    static void testOffsetByCodePoints(StringBuilder sb, int index, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                       Class expectedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            int n = sb.offsetByCodePoints(index, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            if (expectedException.isInstance(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            throw new RuntimeException("Error: Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        check(true, "offsetByCodePoints() didn't throw " + expectedException.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    static void check(boolean err, String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            throw new RuntimeException("Error: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    static void check(boolean err, String s, int got, int expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            throw new RuntimeException("Error: " + s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                                       + " returned an unexpected value. got "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                       + toHexString(got)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                       + ", expected "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                       + toHexString(expected));
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
    static void check(boolean err, String s, StringBuilder got, String expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            throw new RuntimeException("Error: " + s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                       + " returned an unexpected value. got <"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                                       + toHexString(got.toString())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                                       + ">, expected <"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                       + toHexString(expected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                       + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    private static String toHexString(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        return "0x" + Integer.toHexString(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    private static String toHexString(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        for (int i = 0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            sb.append(" 0x");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (c < 0x10) sb.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            if (c < 0x100) sb.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            if (c < 0x1000) sb.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            sb.append(Integer.toHexString(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        sb.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
}