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