1 /*
2  * Copyright (c) 2003, 2016, 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 4533872 4640853
27  * @library /java/text/testlib
28  * @summary Unit tests for supplementary character support (JSR-204) and Unicode 4.0 support
29  */
30 
31 package test.java.text.BreakIterator;
32 
33 import java.text.BreakIterator;
34 import java.util.Locale;
35 
36 import test.java.text.testlib.IntlTest;
37 
38 public class Bug4533872 extends IntlTest {
39 
main(String[] args)40     public static void main(String[] args) throws Exception {
41         new Bug4533872().run(args);
42     }
43 
44     static final String[] given = {
45       /* Lu Nd    Lu     Ll    */
46         "XYZ12345 ABCDE  abcde",
47       /* Nd Lo          Nd  Lu Po    Lu   Ll    */
48         "123\uD800\uDC00345 ABC\uFF61XYZ  abc",
49       /* Nd Lo          Nd  Lu Po          Lu   Ll    */
50         "123\uD800\uDC00345 ABC\uD800\uDD00XYZ  abc",
51       /* Lu Ll Cs    Ll Cs    Lu Lo          Lu  */
52         "ABCabc\uDC00xyz\uD800ABC\uD800\uDC00XYZ",
53     };
54 
55     // Golden data for TestNext(), TestBoundar() and TestPrintEach*ward()
56     static final String[][] expected = {
57         {"XYZ12345", " ", "ABCDE", "  ", "abcde"},
58         {"123\uD800\uDC00345", " ", "ABC", "\uFF61", "XYZ", "  ", "abc"},
59         {"123\uD800\uDC00345", " ", "ABC", "\uD800\uDD00", "XYZ", "  ", "abc"},
60         {"ABCabc", "\uDC00", "xyz", "\uD800", "ABC\uD800\uDC00XYZ"},
61     };
62 
63     BreakIterator iter;
64     int start, end, current;
65 
66     /*
67      * Test for next(int n)
68      */
TestNext()69     void TestNext() {
70         iter = BreakIterator.getWordInstance(Locale.US);
71 
72         for (int i = 0; i < given.length; i++) {
73             iter.setText(given[i]);
74             start = iter.first();
75             int j = expected[i].length - 1;
76             start = iter.next(j);
77             end = iter.next();
78 
79             if (!expected[i][j].equals(given[i].substring(start, end))) {
80                 errln("Word break failure: printEachForward() expected:<" +
81                       expected[i][j] + ">, got:<" +
82                       given[i].substring(start, end) +
83                       "> start=" + start + "  end=" + end);
84             }
85         }
86     }
87 
88     /*
89      * Test for isBoundary(int n)
90      */
TestIsBoundary()91     void TestIsBoundary() {
92         iter = BreakIterator.getWordInstance(Locale.US);
93 
94         for (int i = 0; i < given.length; i++) {
95             iter.setText(given[i]);
96 
97             start = iter.first();
98             end = iter.next();
99 
100             while (end < given[i].length()) {
101                 if (!iter.isBoundary(end)) {
102                     errln("Word break failure: isBoundary() This should be a boundary. Index=" +
103                           end + " for " + given[i]);
104                 }
105                 end = iter.next();
106             }
107         }
108     }
109 
110 
111     /*
112      * The followig test cases were made based on examples in BreakIterator's
113      * API Doc.
114      */
115 
116     /*
117      * Test mainly for next() and current()
118      */
TestPrintEachForward()119     void TestPrintEachForward() {
120         iter = BreakIterator.getWordInstance(Locale.US);
121 
122         for (int i = 0; i < given.length; i++) {
123             iter.setText(given[i]);
124             start = iter.first();
125 
126             // Check current()'s return value - should be same as first()'s.
127             current = iter.current();
128             if (start != current) {
129                 errln("Word break failure: printEachForward() Unexpected current value: current()=" +
130                       current + ", expected(=first())=" + start);
131             }
132 
133             int j = 0;
134             for (end = iter.next();
135                  end != BreakIterator.DONE;
136                  start = end, end = iter.next(), j++) {
137 
138                 // Check current()'s return value - should be same as next()'s.
139                 current = iter.current();
140                 if (end != current) {
141                     errln("Word break failure: printEachForward() Unexpected current value: current()=" +
142                           current + ", expected(=next())=" + end);
143                 }
144 
145                 if (!expected[i][j].equals(given[i].substring(start, end))) {
146                     errln("Word break failure: printEachForward() expected:<" +
147                           expected[i][j] + ">, got:<" +
148                           given[i].substring(start, end) +
149                           "> start=" + start + "  end=" + end);
150                 }
151             }
152         }
153     }
154 
155     /*
156      * Test mainly for previous() and current()
157      */
TestPrintEachBackward()158     void TestPrintEachBackward() {
159         iter = BreakIterator.getWordInstance(Locale.US);
160 
161         for (int i = 0; i < given.length; i++) {
162             iter.setText(given[i]);
163             end = iter.last();
164 
165             // Check current()'s return value - should be same as last()'s.
166             current = iter.current();
167             if (end != current) {
168                 errln("Word break failure: printEachBackward() Unexpected current value: current()=" +
169                       current + ", expected(=last())=" + end);
170             }
171 
172             int j;
173             for (start = iter.previous(), j = expected[i].length-1;
174                  start != BreakIterator.DONE;
175                  end = start, start = iter.previous(), j--) {
176 
177                 // Check current()'s return value - should be same as previous()'s.
178                 current = iter.current();
179                 if (start != current) {
180                     errln("Word break failure: printEachBackward() Unexpected current value: current()=" +
181                           current + ", expected(=previous())=" + start);
182                 }
183 
184                 if (!expected[i][j].equals(given[i].substring(start, end))) {
185                     errln("Word break failure: printEachBackward() expected:<" +
186                           expected[i][j] + ">, got:<" +
187                           given[i].substring(start, end) +
188                           "> start=" + start + "  end=" + end);
189                 }
190             }
191         }
192     }
193 
194     /*
195      * Test mainly for following() and previous()
196      */
TestPrintAt_1()197     void TestPrintAt_1() {
198         iter = BreakIterator.getWordInstance(Locale.US);
199 
200         int[][] index = {
201             {2, 8, 10, 15, 17},
202             {1, 8, 10, 12, 15, 17, 20},
203             {3, 8, 10, 13, 16, 18, 20},
204             {4, 6,  9, 10, 16},
205         };
206 
207         for (int i = 0; i < given.length; i++) {
208             iter.setText(given[i]);
209             for (int j = index[i].length-1; j >= 0; j--) {
210                 end = iter.following(index[i][j]);
211                 start = iter.previous();
212 
213                 if (!expected[i][j].equals(given[i].substring(start, end))) {
214                     errln("Word break failure: printAt_1() expected:<" +
215                           expected[i][j] + ">, got:<" +
216                           given[i].substring(start, end) +
217                           "> start=" + start + "  end=" + end);
218                 }
219             }
220         }
221     }
222 
223     /*
224      * Test mainly for preceding() and next()
225      */
TestPrintAt_2()226     void TestPrintAt_2() {
227         iter = BreakIterator.getWordInstance(Locale.US);
228 
229         int[][] index = {
230             {2, 9, 10, 15, 17},
231             {1, 9, 10, 13, 16, 18, 20},
232             {4, 9, 10, 13, 16, 18, 20},
233             {6, 7, 10, 11, 15},
234         };
235 
236         for (int i = 0; i < given.length; i++) {
237             iter.setText(given[i]);
238 
239             // Check preceding(0)'s return value - should equals BreakIterator.DONE.
240             if (iter.preceding(0) != BreakIterator.DONE) {
241                  errln("Word break failure: printAt_2() expected:-1(BreakIterator.DONE), got:" +
242                        iter.preceding(0));
243             }
244 
245             for (int j = 0; j < index[i].length; j++) {
246                 start = iter.preceding(index[i][j]);
247                 end = iter.next();
248 
249                 if (!expected[i][j].equals(given[i].substring(start, end))) {
250                     errln("Word break failure: printAt_2() expected:<" +
251                           expected[i][j] + ">, got:<" +
252                           given[i].substring(start, end) +
253                           "> start=" + start + "  end=" + end);
254                 }
255             }
256 
257             // Check next()'s return value - should equals BreakIterator.DONE.
258             end = iter.last();
259             start = iter.next();
260             if (start != BreakIterator.DONE) {
261                  errln("Word break failure: printAt_2() expected:-1(BreakIterator.DONE), got:" + start);
262             }
263         }
264     }
265 }
266