jdk/test/java/text/Bidi/BidiSurrogateTest.java
author peytoia
Fri, 17 Oct 2008 13:34:03 +0900
changeset 1834 6eefd0d6804d
child 5506 202f599c92aa
permissions -rw-r--r--
6759521: Move Bidi test programs from closed to open. Reviewed-by: okutsu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1834
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     1
/*
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     2
 * Copyright (c) 2008 Sun Microsystems, Inc.  All Rights Reserved.
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     4
 *
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     8
 *
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    13
 * accompanied this code).
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    14
 *
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    18
 *
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    21
 * have any questions.
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    22
 */
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    23
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    24
/*
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    25
 * @test
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    26
 * @bug 4888843
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    27
 * @summary verify that surrogate pairs representing codepoints with R or AL directionality
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    28
 * and correctly recognized and reordered.
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    29
 */
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    30
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    31
import java.text.Bidi;
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    32
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    33
public class BidiSurrogateTest {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    34
    private static final String RTLS = new String(Character.toChars(0x10800)); // surrogate code point with R directionality
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    35
    private static final String LTRS = new String(Character.toChars(0x107ff)); // surrogate code point with L directionality
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    36
    private static final String LRE = "\u202a";
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    37
    private static final String RLE = "\u202b";
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    38
    private static final String PDF = "\u202c";
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    39
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    40
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    41
    public static void main(String[] args) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    42
        new BidiSurrogateTest().test();
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    43
    }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    44
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    45
    void test() {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    46
        test0();
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    47
        test1();
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    48
    }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    49
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    50
    void test0() {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    51
        // test unpaired surrogates - should have L directionality
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    52
        testRequiresBidi("\ud800", false);           // unpaired lead surrogate
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    53
        testRequiresBidi("\udc00", false);           // unpaired trail surrogate
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    54
        testRequiresBidi("\udc00\ud800", false);     // out of order surrogates
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    55
        testRequiresBidi("a\udc00b\ud800c", false);  // out of order surrogates split
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    56
        testRequiresBidi(LTRS, false);               // supplementary with L
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    57
        testRequiresBidi(RTLS, true);                // supplementary with R
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    58
        testRequiresBidi("a" + RTLS + "b", true);    // R supplementary in LTR text
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    59
        testRequiresBidi(LTRS + RTLS, true);         // R supplementary in LTR supplementary text
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    60
        testRequiresBidi(LRE, false);                // LRE lone embedding
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    61
        testRequiresBidi(RLE, true);                 // RLE lone embedding
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    62
        testRequiresBidi(PDF, false);                // PDF lone pop embedding
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    63
    }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    64
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    65
    void testRequiresBidi(String string, boolean requiresBidi) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    66
        char[] text = string.toCharArray();
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    67
        if (Bidi.requiresBidi(text, 0, text.length) != requiresBidi) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    68
            throw new RuntimeException("testRequiresBidi failed with '" + string + "', " + requiresBidi);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    69
        }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    70
    }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    71
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    72
    void test1() {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    73
        // test that strings with surrogate runs process surrogate directionality ok
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    74
        testBidi("This is a string with " + LTRS + " in it.", false);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    75
        testBidi("This is a string with \ud800 in it.", false);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    76
        testBidi("This is a string with \u0640 in it.", 22, 1);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    77
        testBidi(RTLS, true);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    78
        testBidi("This is a string with " + RTLS + RTLS + RTLS + " in it.", 22, 6);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    79
    }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    80
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    81
    void testBidi(String string, boolean directionIsRTL) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    82
        Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    83
        if (bidi.isMixed()) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    84
            throw new RuntimeException("bidi is mixed");
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    85
        }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    86
        if (bidi.isRightToLeft() != directionIsRTL) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    87
            throw new RuntimeException("bidi is not " + (directionIsRTL ? "rtl" : "ltr"));
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    88
        }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    89
    }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    90
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    91
    void testBidi(String string, int rtlstart, int rtllength) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    92
        Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    93
        for (int i = 0; i < bidi.getRunCount(); ++i) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    94
            if ((bidi.getRunLevel(i) & 1) != 0) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    95
                if (bidi.getRunStart(i) != rtlstart ||
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    96
                    bidi.getRunLimit(i) != rtlstart + rtllength) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    97
                    throw new RuntimeException("first rtl run didn't match " + rtlstart + ", " + rtllength);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    98
                }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    99
                break;
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   100
            }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   101
        }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   102
    }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   103
}