Path: blob/master/test/jdk/java/text/Bidi/BidiSurrogateTest.java
41149 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 488884326* @summary verify that surrogate pairs representing codepoints with R or AL directionality27* and correctly recognized and reordered.28*/2930import java.text.Bidi;3132public class BidiSurrogateTest {33private static final String RTLS = new String(Character.toChars(0x10800)); // surrogate code point with R directionality34private static final String LTRS = new String(Character.toChars(0x107ff)); // surrogate code point with L directionality35private static final String LRE = "\u202a";36private static final String RLE = "\u202b";37private static final String PDF = "\u202c";383940public static void main(String[] args) {41new BidiSurrogateTest().test();42}4344void test() {45test0();46test1();47}4849void test0() {50// test unpaired surrogates - should have L directionality51testRequiresBidi("\ud800", false); // unpaired lead surrogate52testRequiresBidi("\udc00", false); // unpaired trail surrogate53testRequiresBidi("\udc00\ud800", false); // out of order surrogates54testRequiresBidi("a\udc00b\ud800c", false); // out of order surrogates split55testRequiresBidi(LTRS, false); // supplementary with L56testRequiresBidi(RTLS, true); // supplementary with R57testRequiresBidi("a" + RTLS + "b", true); // R supplementary in LTR text58testRequiresBidi(LTRS + RTLS, true); // R supplementary in LTR supplementary text59testRequiresBidi(LRE, false); // LRE lone embedding60testRequiresBidi(RLE, true); // RLE lone embedding61testRequiresBidi(PDF, false); // PDF lone pop embedding62}6364void testRequiresBidi(String string, boolean requiresBidi) {65char[] text = string.toCharArray();66if (Bidi.requiresBidi(text, 0, text.length) != requiresBidi) {67throw new RuntimeException("testRequiresBidi failed with '" + string + "', " + requiresBidi);68}69}7071void test1() {72// test that strings with surrogate runs process surrogate directionality ok73testBidi("This is a string with " + LTRS + " in it.", false);74testBidi("This is a string with \ud800 in it.", false);75testBidi("This is a string with \u0640 in it.", 22, 1);76testBidi(RTLS, true);77testBidi("This is a string with " + RTLS + RTLS + RTLS + " in it.", 22, 6);78}7980void testBidi(String string, boolean directionIsRTL) {81Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);82if (bidi.isMixed()) {83throw new RuntimeException("bidi is mixed");84}85if (bidi.isRightToLeft() != directionIsRTL) {86throw new RuntimeException("bidi is not " + (directionIsRTL ? "rtl" : "ltr"));87}88}8990void testBidi(String string, int rtlstart, int rtllength) {91Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);92for (int i = 0; i < bidi.getRunCount(); ++i) {93if ((bidi.getRunLevel(i) & 1) != 0) {94if (bidi.getRunStart(i) != rtlstart ||95bidi.getRunLimit(i) != rtlstart + rtllength) {96throw new RuntimeException("first rtl run didn't match " + rtlstart + ", " + rtllength);97}98break;99}100}101}102}103104105