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