File: Supplementary.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (417 lines) | stat: -rw-r--r-- 16,470 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 *
 * @test
 * @bug 4533872 4915683 4985217 5017280 6937112
 * @summary Unit tests for supplementary character support (JSR-204)
 */

public class Supplementary {

    public static void main(String[] args) {
        test1();        // Test for codePointAt(int index)
        test2();        // Test for codePointBefore(int index)
        test3();        // Test for reverse()
        test4();        // Test for appendCodePoint(int codePoint)
        test5();        // Test for codePointCount(int beginIndex, int endIndex)
        test6();        // Test for offsetByCodePoints(int index, int offset)
    }

    /* Text strings which are used as input data.
     * The comment above each text string means the index of each 16-bit char
     * for convenience.
     */
    static final String[] input = {
      /*                               111     1     111111     22222
         0123     4     5678     9     012     3     456789     01234 */
        "abc\uD800\uDC00def\uD800\uD800ab\uD800\uDC00cdefa\uDC00bcdef",
      /*                          1     1111     1111     1     222
         0     12345     6789     0     1234     5678     9     012     */
        "\uD800defg\uD800hij\uD800\uDC00klm\uDC00nop\uDC00\uD800rt\uDC00",
      /*                          11     1     1111     1     112     222
         0     12345     6     78901     2     3456     7     890     123     */
        "\uDC00abcd\uDBFF\uDFFFefgh\uD800\uDC009ik\uDC00\uDC00lm\uDC00no\uD800",
      /*                                    111     111111     1 22     2
         0     1     2345     678     9     012     345678     9 01     2     */
        "\uD800\uDC00!#$\uD800%&\uD800\uDC00;+\uDC00<>;=^\uDC00\\@\uD800\uDC00",

        // includes an undefined supplementary character in Unicode 4.0.0
      /*                                    1     11     1     1111     1
         0     1     2345     6     789     0     12     3     4567     8     */
        "\uDB40\uDE00abc\uDE01\uDB40de\uDB40\uDE02f\uDB40\uDE03ghi\uDB40\uDE02",
    };


    /* Expected results for:
     *     test1(): for codePointAt()
     *
     * Each character in each array is the golden data for each text string
     * in the above input data. For example, the first data in each array is
     * for the first input string.
     */
    static final int[][] golden1 = {
        {'a',    0xD800, 0xDC00,  0x10000, 0xE0200}, // codePointAt(0)
        {0xD800, 0x10000, 'g',    0xDC00,  0xE0202}, // codePointAt(9)
        {'f',    0xDC00,  0xD800, 0xDC00,  0xDE02},  // codePointAt(length-1)
    };

    /*
     * Test for codePointAt(int index) method
     */
    static void test1() {

        for (int i = 0; i < input.length; i++) {
            StringBuffer sb = new StringBuffer(input[i]);

            /*
             * Normal case
             */
            testCodePoint(At, sb, 0, golden1[0][i]);
            testCodePoint(At, sb, 9, golden1[1][i]);
            testCodePoint(At, sb, sb.length()-1, golden1[2][i]);

            /*
             * Abnormal case - verify that an exception is thrown.
             */
            testCodePoint(At, sb, -1);
            testCodePoint(At, sb, sb.length());
        }
    }


    /* Expected results for:
     *     test2(): for codePointBefore()
     *
     * Each character in each array is the golden data for each text string
     * in the above input data. For example, the first data in each array is
     * for the first input string.
     */
    static final int[][] golden2 = {
        {'a',    0xD800, 0xDC00,  0xD800,  0xDB40},  // codePointBefore(1)
        {0xD800, 'l',    0x10000, 0xDC00,  0xDB40},  // codePointBefore(13)
        {'f',    0xDC00, 0xD800,  0x10000, 0xE0202}, // codePointBefore(length)
    };

    /*
     * Test for codePointBefore(int index) method
     */
    static void test2() {

        for (int i = 0; i < input.length; i++) {
            StringBuffer sb = new StringBuffer(input[i]);

            /*
             * Normal case
             */
            testCodePoint(Before, sb, 1, golden2[0][i]);
            testCodePoint(Before, sb, 13, golden2[1][i]);
            testCodePoint(Before, sb, sb.length(), golden2[2][i]);

            /*
             * Abnormal case - verify that an exception is thrown.
             */
            testCodePoint(Before, sb, 0);
            testCodePoint(Before, sb, sb.length()+1);
        }
    }


    /* Expected results for:
     *     test3(): for reverse()
     *
     * Unlike golden1 and golden2, each array is the golden data for each text
     * string in the above input data. For example, the first array is  for
     * the first input string.
     */
    static final String[] golden3 = {
        "fedcb\uDC00afedc\uD800\uDC00ba\uD800\uD800fed\uD800\uDC00cba",
        "\uDC00tr\uD800\uDC00pon\uDC00mlk\uD800\uDC00jih\uD800gfed\uD800",
        "\uD800on\uDC00ml\uDC00\uDC00ki9\uD800\uDC00hgfe\uDBFF\uDFFFdcba\uDC00",
        "\uD800\uDC00@\\\uDC00^=;><\uDC00+;\uD800\uDC00&%\uD800$#!\uD800\uDC00",

        // includes an undefined supplementary character in Unicode 4.0.0
        "\uDB40\uDE02ihg\uDB40\uDE03f\uDB40\uDE02ed\uDB40\uDE01cba\uDB40\uDE00",
    };

    // Additional input data & expected result for test3()
    static final String[][] testdata1 = {
        {"a\uD800\uDC00", "\uD800\uDC00a"},
        {"a\uDC00\uD800", "\uD800\uDC00a"},
        {"\uD800\uDC00a", "a\uD800\uDC00"},
        {"\uDC00\uD800a", "a\uD800\uDC00"},
        {"\uDC00\uD800\uD801", "\uD801\uD800\uDC00"},
        {"\uDC00\uD800\uDC01", "\uD800\uDC01\uDC00"},
        {"\uD801\uD800\uDC00", "\uD800\uDC00\uD801"},
        {"\uD800\uDC01\uDC00", "\uDC00\uD800\uDC01"},
        {"\uD800\uDC00\uDC01\uD801", "\uD801\uDC01\uD800\uDC00"},
    };

    /*
     * Test for reverse() method
     */
    static void test3() {
        for (int i = 0; i < input.length; i++) {
            StringBuffer sb = new StringBuffer(input[i]).reverse();

            check(!golden3[i].equals(new String(sb)),
                 "reverse() for <" + toHexString(input[i]) + ">",
                 sb, golden3[i]);
        }

        for (int i = 0; i < testdata1.length; i++) {
            StringBuffer sb = new StringBuffer(testdata1[i][0]).reverse();

            check(!testdata1[i][1].equals(new String(sb)),
                 "reverse() for <" + toHexString(testdata1[i][0]) + ">",
                 sb, testdata1[i][1]);
        }
    }

    /**
     * Test for appendCodePoint() method
     */
    static void test4() {
        for (int i = 0; i < input.length; i++) {
            String s = input[i];
            StringBuffer sb = new StringBuffer();
            int c;
            for (int j = 0; j < s.length(); j += Character.charCount(c)) {
                c = s.codePointAt(j);
                StringBuffer rsb = sb.appendCodePoint(c);
                check(sb != rsb, "appendCodePoint returned a wrong object");
                int sbc = sb.codePointAt(j);
                check(sbc != c, "appendCodePoint(j) != c", sbc, c);
            }
            check(!s.equals(sb.toString()),
                  "appendCodePoint() produced a wrong result with input["+i+"]");
        }

        // test exception
        testAppendCodePoint(-1, IllegalArgumentException.class);
        testAppendCodePoint(Character.MAX_CODE_POINT+1, IllegalArgumentException.class);
    }

    /**
     * Test codePointCount(int, int)
     *
     * This test case assumes that
     * Character.codePointCount(CharSequence, int, int) works
     * correctly.
     */
    static void test5() {
        for (int i = 0; i < input.length; i++) {
            String s = input[i];
            StringBuffer sb = new StringBuffer(s);
            int length = sb.length();
            for (int j = 0; j <= length; j++) {
                int result = sb.codePointCount(j, length);
                int expected = Character.codePointCount(sb, j, length);
                check(result != expected, "codePointCount(input["+i+"], "+j+", "+length+")",
                      result, expected);
            }
            for (int j = length; j >= 0; j--) {
                int result = sb.codePointCount(0, j);
                int expected = Character.codePointCount(sb, 0, j);
                check(result != expected, "codePointCount(input["+i+"], 0, "+j+")",
                      result, expected);
            }

            // test exceptions
            testCodePointCount(null, 0, 0, NullPointerException.class);
            testCodePointCount(sb, -1, length, IndexOutOfBoundsException.class);
            testCodePointCount(sb, 0, length+1, IndexOutOfBoundsException.class);
            testCodePointCount(sb, length, length-1, IndexOutOfBoundsException.class);
        }
    }

    /**
     * Test offsetByCodePoints(int, int)
     *
     * This test case assumes that
     * Character.codePointCount(CharSequence, int, int) works
     * correctly.
     */
    static void test6() {
        for (int i = 0; i < input.length; i++) {
            String s = input[i];
            StringBuffer sb = new StringBuffer(s);
            int length = s.length();
            for (int j = 0; j <= length; j++) {
                int nCodePoints = Character.codePointCount(sb, j, length);
                int result = sb.offsetByCodePoints(j, nCodePoints);
                check(result != length,
                      "offsetByCodePoints(input["+i+"], "+j+", "+nCodePoints+")",
                      result, length);
                result = sb.offsetByCodePoints(length, -nCodePoints);
                int expected = j;
                if (j > 0 && j < length) {
                    int cp = sb.codePointBefore(j+1);
                    if (Character.isSupplementaryCodePoint(cp)) {
                        expected--;
                    }
                }
                check(result != expected,
                      "offsetByCodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
                      result, expected);
            }
            for (int j = length; j >= 0; j--) {
                int nCodePoints = Character.codePointCount(sb, 0, j);
                int result = sb.offsetByCodePoints(0, nCodePoints);
                int expected = j;
                if (j > 0 && j < length) {
                    int cp = sb.codePointAt(j-1);
                     if (Character.isSupplementaryCodePoint(cp)) {
                        expected++;
                    }
                }
                check(result != expected,
                      "offsetByCodePoints(input["+i+"], 0, "+nCodePoints+")",
                      result, expected);
                result = sb.offsetByCodePoints(j, -nCodePoints);
                check(result != 0,
                      "offsetBycodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
                      result, 0);
            }

            // test exceptions
            testOffsetByCodePoints(null, 0, 0, NullPointerException.class);
            testOffsetByCodePoints(sb, -1, length, IndexOutOfBoundsException.class);
            testOffsetByCodePoints(sb, 0, length+1, IndexOutOfBoundsException.class);
            testOffsetByCodePoints(sb, 1, -2, IndexOutOfBoundsException.class);
            testOffsetByCodePoints(sb, length, length-1, IndexOutOfBoundsException.class);
            testOffsetByCodePoints(sb, length, -(length+1), IndexOutOfBoundsException.class);
        }
    }


    static final boolean At = true, Before = false;

    static void testCodePoint(boolean isAt, StringBuffer sb, int index, int expected) {
        int c = isAt ? sb.codePointAt(index) : sb.codePointBefore(index);

        check(c != expected,
              "codePoint" + (isAt ? "At" : "Before") + "(" + index + ") for <"
              + sb + ">", c, expected);
    }

    static void testCodePoint(boolean isAt, StringBuffer sb, int index) {
        boolean exceptionOccurred = false;

        try {
            int c = isAt ? sb.codePointAt(index) : sb.codePointBefore(index);
        }
        catch (StringIndexOutOfBoundsException e) {
            exceptionOccurred = true;
        }
        check(!exceptionOccurred,
              "codePoint" + (isAt ? "At" : "Before") + "(" + index + ") for <"
              + sb + "> should throw StringIndexOutOfBoundsPointerException.");
    }

    static void testAppendCodePoint(int codePoint, Class expectedException) {
        try {
            new StringBuffer().appendCodePoint(codePoint);
        } catch (Exception e) {
            if (expectedException.isInstance(e)) {
                return;
            }
            throw new RuntimeException("Error: Unexpected exception", e);
        }
        check(true, "appendCodePoint(" + toHexString(codePoint) + ") didn't throw "
              + expectedException.getName());
    }

    static void testCodePointCount(StringBuffer sb, int beginIndex, int endIndex,
                                   Class expectedException) {
        try {
            int n = sb.codePointCount(beginIndex, endIndex);
        } catch (Exception e) {
            if (expectedException.isInstance(e)) {
                return;
            }
            throw new RuntimeException("Error: Unexpected exception", e);
        }
        check(true, "codePointCount() didn't throw " + expectedException.getName());
    }

    static void testOffsetByCodePoints(StringBuffer sb, int index, int offset,
                                       Class expectedException) {
        try {
            int n = sb.offsetByCodePoints(index, offset);
        } catch (Exception e) {
            if (expectedException.isInstance(e)) {
                return;
            }
            throw new RuntimeException("Error: Unexpected exception", e);
        }
        check(true, "offsetByCodePoints() didn't throw " + expectedException.getName());
    }

    static void check(boolean err, String msg) {
        if (err) {
            throw new RuntimeException("Error: " + msg);
        }
    }

    static void check(boolean err, String s, int got, int expected) {
        if (err) {
            throw new RuntimeException("Error: " + s
                                       + " returned an unexpected value. got "
                                       + toHexString(got)
                                       + ", expected "
                                       + toHexString(expected));
        }
    }

    static void check(boolean err, String s, StringBuffer got, String expected) {
        if (err) {
            throw new RuntimeException("Error: " + s
                                       + " returned an unexpected value. got <"
                                       + toHexString(new String(got))
                                       + ">, expected <"
                                       + toHexString(expected)
                                       + ">");
        }
    }

    private static String toHexString(int c) {
        return "0x" + Integer.toHexString(c);
    }

    private static String toHexString(String s) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            sb.append(" 0x");
            if (c < 0x10) sb.append('0');
            if (c < 0x100) sb.append('0');
            if (c < 0x1000) sb.append('0');
            sb.append(Integer.toHexString(c));
        }
        sb.append(' ');
        return sb.toString();
    }
}