817N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
817N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
817N/A *
817N/A * This code is free software; you can redistribute it and/or modify it
817N/A * under the terms of the GNU General Public License version 2 only, as
817N/A * published by the Free Software Foundation.
817N/A *
817N/A * This code is distributed in the hope that it will be useful, but WITHOUT
817N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
817N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
817N/A * version 2 for more details (a copy is included in the LICENSE file that
817N/A * accompanied this code).
817N/A *
817N/A * You should have received a copy of the GNU General Public License version
817N/A * 2 along with this work; if not, write to the Free Software Foundation,
817N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
817N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
817N/A */
817N/A
817N/A/*
817N/A * @test
817N/A * @bug 4888843
817N/A * @summary verify that surrogate pairs representing codepoints with R or AL directionality
817N/A * and correctly recognized and reordered.
817N/A */
817N/A
817N/Aimport java.text.Bidi;
817N/A
817N/Apublic class BidiSurrogateTest {
817N/A private static final String RTLS = new String(Character.toChars(0x10800)); // surrogate code point with R directionality
817N/A private static final String LTRS = new String(Character.toChars(0x107ff)); // surrogate code point with L directionality
817N/A private static final String LRE = "\u202a";
817N/A private static final String RLE = "\u202b";
817N/A private static final String PDF = "\u202c";
817N/A
817N/A
817N/A public static void main(String[] args) {
817N/A new BidiSurrogateTest().test();
817N/A }
817N/A
817N/A void test() {
817N/A test0();
817N/A test1();
817N/A }
817N/A
817N/A void test0() {
817N/A // test unpaired surrogates - should have L directionality
817N/A testRequiresBidi("\ud800", false); // unpaired lead surrogate
817N/A testRequiresBidi("\udc00", false); // unpaired trail surrogate
817N/A testRequiresBidi("\udc00\ud800", false); // out of order surrogates
817N/A testRequiresBidi("a\udc00b\ud800c", false); // out of order surrogates split
817N/A testRequiresBidi(LTRS, false); // supplementary with L
817N/A testRequiresBidi(RTLS, true); // supplementary with R
817N/A testRequiresBidi("a" + RTLS + "b", true); // R supplementary in LTR text
817N/A testRequiresBidi(LTRS + RTLS, true); // R supplementary in LTR supplementary text
817N/A testRequiresBidi(LRE, false); // LRE lone embedding
817N/A testRequiresBidi(RLE, true); // RLE lone embedding
817N/A testRequiresBidi(PDF, false); // PDF lone pop embedding
817N/A }
817N/A
817N/A void testRequiresBidi(String string, boolean requiresBidi) {
817N/A char[] text = string.toCharArray();
817N/A if (Bidi.requiresBidi(text, 0, text.length) != requiresBidi) {
817N/A throw new RuntimeException("testRequiresBidi failed with '" + string + "', " + requiresBidi);
817N/A }
817N/A }
817N/A
817N/A void test1() {
817N/A // test that strings with surrogate runs process surrogate directionality ok
817N/A testBidi("This is a string with " + LTRS + " in it.", false);
817N/A testBidi("This is a string with \ud800 in it.", false);
817N/A testBidi("This is a string with \u0640 in it.", 22, 1);
817N/A testBidi(RTLS, true);
817N/A testBidi("This is a string with " + RTLS + RTLS + RTLS + " in it.", 22, 6);
817N/A }
817N/A
817N/A void testBidi(String string, boolean directionIsRTL) {
817N/A Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
817N/A if (bidi.isMixed()) {
817N/A throw new RuntimeException("bidi is mixed");
817N/A }
817N/A if (bidi.isRightToLeft() != directionIsRTL) {
817N/A throw new RuntimeException("bidi is not " + (directionIsRTL ? "rtl" : "ltr"));
817N/A }
817N/A }
817N/A
817N/A void testBidi(String string, int rtlstart, int rtllength) {
817N/A Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
817N/A for (int i = 0; i < bidi.getRunCount(); ++i) {
817N/A if ((bidi.getRunLevel(i) & 1) != 0) {
817N/A if (bidi.getRunStart(i) != rtlstart ||
817N/A bidi.getRunLimit(i) != rtlstart + rtllength) {
817N/A throw new RuntimeException("first rtl run didn't match " + rtlstart + ", " + rtllength);
817N/A }
817N/A break;
817N/A }
817N/A }
817N/A }
817N/A}