Path: blob/master/test/jdk/java/text/BreakIterator/Bug4533872.java
41149 views
/*1* Copyright (c) 2003, 2016, 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 4533872 464085326* @library /java/text/testlib27* @summary Unit tests for supplementary character support (JSR-204) and Unicode 4.0 support28*/2930import java.text.BreakIterator;31import java.util.Locale;3233public class Bug4533872 extends IntlTest {3435public static void main(String[] args) throws Exception {36new Bug4533872().run(args);37}3839static final String[] given = {40/* Lu Nd Lu Ll */41"XYZ12345 ABCDE abcde",42/* Nd Lo Nd Lu Po Lu Ll */43"123\uD800\uDC00345 ABC\uFF61XYZ abc",44/* Nd Lo Nd Lu Po Lu Ll */45"123\uD800\uDC00345 ABC\uD800\uDD00XYZ abc",46/* Lu Ll Cs Ll Cs Lu Lo Lu */47"ABCabc\uDC00xyz\uD800ABC\uD800\uDC00XYZ",48};4950// Golden data for TestNext(), TestBoundar() and TestPrintEach*ward()51static final String[][] expected = {52{"XYZ12345", " ", "ABCDE", " ", "abcde"},53{"123\uD800\uDC00345", " ", "ABC", "\uFF61", "XYZ", " ", "abc"},54{"123\uD800\uDC00345", " ", "ABC", "\uD800\uDD00", "XYZ", " ", "abc"},55{"ABCabc", "\uDC00", "xyz", "\uD800", "ABC\uD800\uDC00XYZ"},56};5758BreakIterator iter;59int start, end, current;6061/*62* Test for next(int n)63*/64void TestNext() {65iter = BreakIterator.getWordInstance(Locale.US);6667for (int i = 0; i < given.length; i++) {68iter.setText(given[i]);69start = iter.first();70int j = expected[i].length - 1;71start = iter.next(j);72end = iter.next();7374if (!expected[i][j].equals(given[i].substring(start, end))) {75errln("Word break failure: printEachForward() expected:<" +76expected[i][j] + ">, got:<" +77given[i].substring(start, end) +78"> start=" + start + " end=" + end);79}80}81}8283/*84* Test for isBoundary(int n)85*/86void TestIsBoundary() {87iter = BreakIterator.getWordInstance(Locale.US);8889for (int i = 0; i < given.length; i++) {90iter.setText(given[i]);9192start = iter.first();93end = iter.next();9495while (end < given[i].length()) {96if (!iter.isBoundary(end)) {97errln("Word break failure: isBoundary() This should be a boundary. Index=" +98end + " for " + given[i]);99}100end = iter.next();101}102}103}104105106/*107* The followig test cases were made based on examples in BreakIterator's108* API Doc.109*/110111/*112* Test mainly for next() and current()113*/114void TestPrintEachForward() {115iter = BreakIterator.getWordInstance(Locale.US);116117for (int i = 0; i < given.length; i++) {118iter.setText(given[i]);119start = iter.first();120121// Check current()'s return value - should be same as first()'s.122current = iter.current();123if (start != current) {124errln("Word break failure: printEachForward() Unexpected current value: current()=" +125current + ", expected(=first())=" + start);126}127128int j = 0;129for (end = iter.next();130end != BreakIterator.DONE;131start = end, end = iter.next(), j++) {132133// Check current()'s return value - should be same as next()'s.134current = iter.current();135if (end != current) {136errln("Word break failure: printEachForward() Unexpected current value: current()=" +137current + ", expected(=next())=" + end);138}139140if (!expected[i][j].equals(given[i].substring(start, end))) {141errln("Word break failure: printEachForward() expected:<" +142expected[i][j] + ">, got:<" +143given[i].substring(start, end) +144"> start=" + start + " end=" + end);145}146}147}148}149150/*151* Test mainly for previous() and current()152*/153void TestPrintEachBackward() {154iter = BreakIterator.getWordInstance(Locale.US);155156for (int i = 0; i < given.length; i++) {157iter.setText(given[i]);158end = iter.last();159160// Check current()'s return value - should be same as last()'s.161current = iter.current();162if (end != current) {163errln("Word break failure: printEachBackward() Unexpected current value: current()=" +164current + ", expected(=last())=" + end);165}166167int j;168for (start = iter.previous(), j = expected[i].length-1;169start != BreakIterator.DONE;170end = start, start = iter.previous(), j--) {171172// Check current()'s return value - should be same as previous()'s.173current = iter.current();174if (start != current) {175errln("Word break failure: printEachBackward() Unexpected current value: current()=" +176current + ", expected(=previous())=" + start);177}178179if (!expected[i][j].equals(given[i].substring(start, end))) {180errln("Word break failure: printEachBackward() expected:<" +181expected[i][j] + ">, got:<" +182given[i].substring(start, end) +183"> start=" + start + " end=" + end);184}185}186}187}188189/*190* Test mainly for following() and previous()191*/192void TestPrintAt_1() {193iter = BreakIterator.getWordInstance(Locale.US);194195int[][] index = {196{2, 8, 10, 15, 17},197{1, 8, 10, 12, 15, 17, 20},198{3, 8, 10, 13, 16, 18, 20},199{4, 6, 9, 10, 16},200};201202for (int i = 0; i < given.length; i++) {203iter.setText(given[i]);204for (int j = index[i].length-1; j >= 0; j--) {205end = iter.following(index[i][j]);206start = iter.previous();207208if (!expected[i][j].equals(given[i].substring(start, end))) {209errln("Word break failure: printAt_1() expected:<" +210expected[i][j] + ">, got:<" +211given[i].substring(start, end) +212"> start=" + start + " end=" + end);213}214}215}216}217218/*219* Test mainly for preceding() and next()220*/221void TestPrintAt_2() {222iter = BreakIterator.getWordInstance(Locale.US);223224int[][] index = {225{2, 9, 10, 15, 17},226{1, 9, 10, 13, 16, 18, 20},227{4, 9, 10, 13, 16, 18, 20},228{6, 7, 10, 11, 15},229};230231for (int i = 0; i < given.length; i++) {232iter.setText(given[i]);233234// Check preceding(0)'s return value - should equals BreakIterator.DONE.235if (iter.preceding(0) != BreakIterator.DONE) {236errln("Word break failure: printAt_2() expected:-1(BreakIterator.DONE), got:" +237iter.preceding(0));238}239240for (int j = 0; j < index[i].length; j++) {241start = iter.preceding(index[i][j]);242end = iter.next();243244if (!expected[i][j].equals(given[i].substring(start, end))) {245errln("Word break failure: printAt_2() expected:<" +246expected[i][j] + ">, got:<" +247given[i].substring(start, end) +248"> start=" + start + " end=" + end);249}250}251252// Check next()'s return value - should equals BreakIterator.DONE.253end = iter.last();254start = iter.next();255if (start != BreakIterator.DONE) {256errln("Word break failure: printAt_2() expected:-1(BreakIterator.DONE), got:" + start);257}258}259}260}261262263