2 *******************************************************************************
\r
3 * Copyright (C) 2001-2008, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
9 * Port From: ICU4C v1.8.1 : rbbi : RBBIAPITest
\r
10 * Source File: $ICU4CRoot/source/test/intltest/rbbiapts.cpp
\r
13 package com.ibm.icu.dev.test.rbbi;
\r
15 import com.ibm.icu.text.BreakIterator;
\r
16 import com.ibm.icu.text.RuleBasedBreakIterator;
\r
17 import java.util.Locale;
\r
18 import java.text.CharacterIterator;
\r
19 import java.text.StringCharacterIterator;
\r
22 * API Test the RuleBasedBreakIterator class
\r
24 public class RBBIAPITest extends com.ibm.icu.dev.test.TestFmwk {
\r
26 public static void main(String[] args) throws Exception {
\r
27 new RBBIAPITest().run(args);
\r
31 * Tests clone() and equals() methods of RuleBasedBreakIterator
\r
33 public void TestCloneEquals() {
\r
34 RuleBasedBreakIterator bi1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
35 RuleBasedBreakIterator biequal = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
36 RuleBasedBreakIterator bi3 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
37 RuleBasedBreakIterator bi2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
\r
39 String testString = "Testing word break iterators's clone() and equals()";
\r
40 bi1.setText(testString);
\r
41 bi2.setText(testString);
\r
42 biequal.setText(testString);
\r
44 bi3.setText("hello");
\r
45 logln("Testing equals()");
\r
46 logln("Testing == and !=");
\r
47 if (!bi1.equals(biequal) || bi1.equals(bi2) || bi1.equals(bi3))
\r
48 errln("ERROR:1 RBBI's == and !- operator failed.");
\r
49 if (bi2.equals(biequal) || bi2.equals(bi1) || biequal.equals(bi3))
\r
50 errln("ERROR:2 RBBI's == and != operator failed.");
\r
51 logln("Testing clone()");
\r
52 RuleBasedBreakIterator bi1clone = (RuleBasedBreakIterator) bi1.clone();
\r
53 RuleBasedBreakIterator bi2clone = (RuleBasedBreakIterator) bi2.clone();
\r
54 if (!bi1clone.equals(bi1)
\r
55 || !bi1clone.equals(biequal)
\r
56 || bi1clone.equals(bi3)
\r
57 || bi1clone.equals(bi2))
\r
58 errln("ERROR:1 RBBI's clone() method failed");
\r
60 if (bi2clone.equals(bi1)
\r
61 || bi2clone.equals(biequal)
\r
62 || bi2clone.equals(bi3)
\r
63 || !bi2clone.equals(bi2))
\r
64 errln("ERROR:2 RBBI's clone() method failed");
\r
66 if (!bi1.getText().equals(bi1clone.getText())
\r
67 || !bi2clone.getText().equals(bi2.getText())
\r
68 || bi2clone.equals(bi1clone))
\r
69 errln("ERROR: RBBI's clone() method failed");
\r
73 * Tests toString() method of RuleBasedBreakIterator
\r
75 public void TestToString() {
\r
76 RuleBasedBreakIterator bi1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
77 RuleBasedBreakIterator bi2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
\r
78 logln("Testing toString()");
\r
79 bi1.setText("Hello there");
\r
80 RuleBasedBreakIterator bi3 = (RuleBasedBreakIterator) bi1.clone();
\r
81 String temp = bi1.toString();
\r
82 String temp2 = bi2.toString();
\r
83 String temp3 = bi3.toString();
\r
84 if (temp2.equals(temp3) || temp.equals(temp2) || !temp.equals(temp3))
\r
85 errln("ERROR: error in toString() method");
\r
89 * Tests the method hashCode() of RuleBasedBreakIterator
\r
91 public void TestHashCode() {
\r
92 RuleBasedBreakIterator bi1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
93 RuleBasedBreakIterator bi3 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
94 RuleBasedBreakIterator bi2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
\r
95 logln("Testing hashCode()");
\r
96 bi1.setText("Hash code");
\r
97 bi2.setText("Hash code");
\r
98 bi3.setText("Hash code");
\r
99 RuleBasedBreakIterator bi1clone = (RuleBasedBreakIterator) bi1.clone();
\r
100 RuleBasedBreakIterator bi2clone = (RuleBasedBreakIterator) bi2.clone();
\r
101 if (bi1.hashCode() != bi1clone.hashCode()
\r
102 || bi1.hashCode() != bi3.hashCode()
\r
103 || bi1clone.hashCode() != bi3.hashCode()
\r
104 || bi2.hashCode() != bi2clone.hashCode())
\r
105 errln("ERROR: identical objects have different hashcodes");
\r
107 if (bi1.hashCode() == bi2.hashCode()
\r
108 || bi2.hashCode() == bi3.hashCode()
\r
109 || bi1clone.hashCode() == bi2clone.hashCode()
\r
110 || bi1clone.hashCode() == bi2.hashCode())
\r
111 errln("ERROR: different objects have same hashcodes");
\r
115 * Tests the methods getText() and setText() of RuleBasedBreakIterator
\r
117 public void TestGetSetText() {
\r
118 logln("Testing getText setText ");
\r
119 String str1 = "first string.";
\r
120 String str2 = "Second string.";
\r
121 //RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
122 RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
\r
123 CharacterIterator text1 = new StringCharacterIterator(str1);
\r
124 //CharacterIterator text1Clone = (CharacterIterator) text1.clone();
\r
125 //CharacterIterator text2 = new StringCharacterIterator(str2);
\r
126 wordIter1.setText(str1);
\r
127 if (!wordIter1.getText().equals(text1))
\r
128 errln("ERROR:1 error in setText or getText ");
\r
129 if (wordIter1.current() != 0)
\r
130 errln("ERROR:1 setText did not set the iteration position to the beginning of the text, it is"
\r
131 + wordIter1.current() + "\n");
\r
133 wordIter1.setText(str2);
\r
134 if (wordIter1.current() != 0)
\r
135 errln("ERROR:2 setText did not reset the iteration position to the beginning of the text, it is"
\r
136 + wordIter1.current() + "\n");
\r
137 //ICU4J has remove the method adoptText
\r
139 charIter1.adoptText(text1Clone);
\r
140 if (wordIter1.getText() == charIter1.getText()
\r
141 || wordIter1.getText() != text2
\r
142 || charIter1.getText() != text1)
\r
143 errln((UnicodeString) "ERROR:2 error is getText or setText()");
\r
145 RuleBasedBreakIterator rb = (RuleBasedBreakIterator) wordIter1.clone();
\r
146 rb.adoptText(text1);
\r
147 if (rb.getText() != text1)
\r
148 errln((UnicodeString) "ERROR:1 error in adoptText ");
\r
149 rb.adoptText(text2);
\r
150 if (rb.getText() != text2)
\r
151 errln((UnicodeString) "ERROR:2 error in adoptText ");
\r
156 * Testing the methods first(), next(), next(int) and following() of RuleBasedBreakIterator
\r
157 * TODO: Most of this test should be retired, rule behavior is much better covered by
\r
158 * TestExtended, which is also easier to understand and maintain.
\r
160 public void TestFirstNextFollowing() {
\r
162 String testString = "This is a word break. Isn't it? 2.25";
\r
163 logln("Testing first() and next(), following() with custom rules");
\r
164 logln("testing word iterator - string :- \"" + testString + "\"\n");
\r
165 RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
\r
166 wordIter1.setText(testString);
\r
167 p = wordIter1.first();
\r
169 errln("ERROR: first() returned" + p + "instead of 0");
\r
170 q = wordIter1.next(9);
\r
171 doTest(testString, p, q, 20, "This is a word break");
\r
173 q = wordIter1.next();
\r
174 doTest(testString, p, q, 21, ".");
\r
176 q = wordIter1.next(3);
\r
177 doTest(testString, p, q, 28, " Isn't ");
\r
179 q = wordIter1.next(2);
\r
180 doTest(testString, p, q, 31, "it?");
\r
181 q = wordIter1.following(2);
\r
182 doTest(testString, 2, q, 4, "is");
\r
183 q = wordIter1.following(22);
\r
184 doTest(testString, 22, q, 27, "Isn't");
\r
186 p = wordIter1.next();
\r
187 q = wordIter1.following(wordIter1.last());
\r
188 if (p != BreakIterator.DONE || q != BreakIterator.DONE)
\r
189 errln("ERROR: next()/following() at last position returned #"
\r
190 + p + " and " + q + " instead of" + testString.length() + "\n");
\r
191 RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
192 testString = "Write hindi here. ";
\r
193 logln("testing char iter - string:- \"" + testString + "\"");
\r
194 charIter1.setText(testString);
\r
195 p = charIter1.first();
\r
197 errln("ERROR: first() returned" + p + "instead of 0");
\r
198 q = charIter1.next();
\r
199 doTest(testString, p, q, 1, "W");
\r
201 q = charIter1.next(4);
\r
202 doTest(testString, p, q, 5, "rite");
\r
204 q = charIter1.next(12);
\r
205 doTest(testString, p, q, 17, " hindi here.");
\r
207 q = charIter1.next(-6);
\r
208 doTest(testString, p, q, 11, " here.");
\r
210 q = charIter1.next(6);
\r
211 doTest(testString, p, q, 17, " here.");
\r
212 p = charIter1.following(charIter1.last());
\r
213 q = charIter1.next(charIter1.last());
\r
214 if (p != BreakIterator.DONE || q != BreakIterator.DONE)
\r
215 errln("ERROR: following()/next() at last position returned #"
\r
216 + p + " and " + q + " instead of" + testString.length());
\r
217 testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This costs $20,00,000.";
\r
218 RuleBasedBreakIterator sentIter1 = (RuleBasedBreakIterator) BreakIterator.getSentenceInstance(Locale.getDefault());
\r
219 logln("testing sentence iter - String:- \"" + testString + "\"");
\r
220 sentIter1.setText(testString);
\r
221 p = sentIter1.first();
\r
223 errln("ERROR: first() returned" + p + "instead of 0");
\r
224 q = sentIter1.next();
\r
225 doTest(testString, p, q, 7, "Hello! ");
\r
227 q = sentIter1.next(2);
\r
228 doTest(testString, p, q, 31, "how are you? I'am fine. ");
\r
230 q = sentIter1.next(-2);
\r
231 doTest(testString, p, q, 7, "how are you? I'am fine. ");
\r
233 q = sentIter1.next(4);
\r
234 doTest(testString, p, q, 60, "how are you? I'am fine. Thankyou. How are you doing? ");
\r
236 q = sentIter1.next();
\r
237 doTest(testString, p, q, 83, "This costs $20,00,000.");
\r
238 q = sentIter1.following(1);
\r
239 doTest(testString, 1, q, 7, "ello! ");
\r
240 q = sentIter1.following(10);
\r
241 doTest(testString, 10, q, 20, " are you? ");
\r
242 q = sentIter1.following(20);
\r
243 doTest(testString, 20, q, 31, "I'am fine. ");
\r
244 p = sentIter1.following(sentIter1.last());
\r
245 q = sentIter1.next(sentIter1.last());
\r
246 if (p != BreakIterator.DONE || q != BreakIterator.DONE)
\r
247 errln("ERROR: following()/next() at last position returned #"
\r
248 + p + " and " + q + " instead of" + testString.length());
\r
249 testString = "Hello! how\r\n (are)\r you? I'am fine- Thankyou. foo\u00a0bar How, are, you? This, costs $20,00,000.";
\r
250 logln("(UnicodeString)testing line iter - String:- \"" + testString + "\"");
\r
251 RuleBasedBreakIterator lineIter1 = (RuleBasedBreakIterator) BreakIterator.getLineInstance(Locale.getDefault());
\r
252 lineIter1.setText(testString);
\r
253 p = lineIter1.first();
\r
255 errln("ERROR: first() returned" + p + "instead of 0");
\r
256 q = lineIter1.next();
\r
257 doTest(testString, p, q, 7, "Hello! ");
\r
260 q = lineIter1.next(4);
\r
261 doTest(testString, p, q, 20, "how\r\n (are)\r ");
\r
263 q = lineIter1.next(-4);
\r
264 doTest(testString, p, q, 7, "how\r\n (are)\r ");
\r
266 q = lineIter1.next(6);
\r
267 doTest(testString, p, q, 30, "how\r\n (are)\r you? I'am ");
\r
269 q = lineIter1.next();
\r
270 doTest(testString, p, q, 36, "fine- ");
\r
272 q = lineIter1.next(2);
\r
273 doTest(testString, p, q, 54, "Thankyou. foo\u00a0bar ");
\r
274 q = lineIter1.following(60);
\r
275 doTest(testString, 60, q, 64, "re, ");
\r
276 q = lineIter1.following(1);
\r
277 doTest(testString, 1, q, 7, "ello! ");
\r
278 q = lineIter1.following(10);
\r
279 doTest(testString, 10, q, 12, "\r\n");
\r
280 q = lineIter1.following(20);
\r
281 doTest(testString, 20, q, 25, "you? ");
\r
282 p = lineIter1.following(lineIter1.last());
\r
283 q = lineIter1.next(lineIter1.last());
\r
284 if (p != BreakIterator.DONE || q != BreakIterator.DONE)
\r
285 errln("ERROR: following()/next() at last position returned #"
\r
286 + p + " and " + q + " instead of" + testString.length());
\r
290 * Testing the methods lastt(), previous(), and preceding() of RuleBasedBreakIterator
\r
292 public void TestLastPreviousPreceding() {
\r
294 String testString = "This is a word break. Isn't it? 2.25 dollars";
\r
295 logln("Testing last(),previous(), preceding() with custom rules");
\r
296 logln("testing word iteration for string \"" + testString + "\"");
\r
297 RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
\r
298 wordIter1.setText(testString);
\r
299 p = wordIter1.last();
\r
300 if (p != testString.length()) {
\r
301 errln("ERROR: first() returned" + p + "instead of" + testString.length());
\r
303 q = wordIter1.previous();
\r
304 doTest(testString, p, q, 37, "dollars");
\r
306 q = wordIter1.previous();
\r
307 doTest(testString, p, q, 36, " ");
\r
308 q = wordIter1.preceding(25);
\r
309 doTest(testString, 25, q, 22, "Isn");
\r
311 q = wordIter1.previous();
\r
312 doTest(testString, p, q, 21, " ");
\r
313 q = wordIter1.preceding(20);
\r
314 doTest(testString, 20, q, 15, "break");
\r
315 p = wordIter1.preceding(wordIter1.first());
\r
316 if (p != BreakIterator.DONE)
\r
317 errln("ERROR: preceding() at starting position returned #" + p + " instead of 0");
\r
318 testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This costs $20,00,000.";
\r
319 logln("testing sentence iter - String:- \"" + testString + "\"");
\r
320 RuleBasedBreakIterator sentIter1 = (RuleBasedBreakIterator) BreakIterator.getSentenceInstance(Locale.getDefault());
\r
321 sentIter1.setText(testString);
\r
322 p = sentIter1.last();
\r
323 if (p != testString.length())
\r
324 errln("ERROR: last() returned" + p + "instead of " + testString.length());
\r
325 q = sentIter1.previous();
\r
326 doTest(testString, p, q, 60, "This costs $20,00,000.");
\r
328 q = sentIter1.previous();
\r
329 doTest(testString, p, q, 41, "How are you doing? ");
\r
330 q = sentIter1.preceding(40);
\r
331 doTest(testString, 40, q, 31, "Thankyou.");
\r
332 q = sentIter1.preceding(25);
\r
333 doTest(testString, 25, q, 20, "I'am ");
\r
335 p = sentIter1.previous();
\r
336 q = sentIter1.preceding(sentIter1.first());
\r
337 if (p != BreakIterator.DONE || q != BreakIterator.DONE)
\r
338 errln("ERROR: previous()/preceding() at starting position returned #"
\r
339 + p + " and " + q + " instead of 0\n");
\r
340 testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This\n costs $20,00,000.";
\r
341 logln("testing line iter - String:- \"" + testString + "\"");
\r
342 RuleBasedBreakIterator lineIter1 = (RuleBasedBreakIterator) BreakIterator.getLineInstance(Locale.getDefault());
\r
343 lineIter1.setText(testString);
\r
344 p = lineIter1.last();
\r
345 if (p != testString.length())
\r
346 errln("ERROR: last() returned" + p + "instead of " + testString.length());
\r
347 q = lineIter1.previous();
\r
348 doTest(testString, p, q, 72, "$20,00,000.");
\r
350 q = lineIter1.previous();
\r
351 doTest(testString, p, q, 66, "costs ");
\r
352 q = lineIter1.preceding(40);
\r
353 doTest(testString, 40, q, 31, "Thankyou.");
\r
354 q = lineIter1.preceding(25);
\r
355 doTest(testString, 25, q, 20, "I'am ");
\r
357 p = lineIter1.previous();
\r
358 q = lineIter1.preceding(sentIter1.first());
\r
359 if (p != BreakIterator.DONE || q != BreakIterator.DONE)
\r
360 errln("ERROR: previous()/preceding() at starting position returned #"
\r
361 + p + " and " + q + " instead of 0\n");
\r
365 * Tests the method IsBoundary() of RuleBasedBreakIterator
\r
367 public void TestIsBoundary() {
\r
368 String testString1 = "Write here. \u092d\u0301\u0930\u0924 \u0938\u0941\u0902\u0926\u0930 a\u0301u";
\r
369 RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
370 charIter1.setText(testString1);
\r
371 int bounds1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 20, 21, 22, 23, 25, 26};
\r
372 doBoundaryTest(charIter1, testString1, bounds1);
\r
373 RuleBasedBreakIterator wordIter2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
\r
374 wordIter2.setText(testString1);
\r
375 int bounds2[] = {0, 5, 6, 10, 11, 12, 16, 17, 22, 23, 26};
\r
376 doBoundaryTest(wordIter2, testString1, bounds2);
\r
379 //---------------------------------------------
\r
380 //Internal subroutines
\r
381 //---------------------------------------------
\r
383 /* Internal subroutine used by TestIsBoundary() */
\r
384 public void doBoundaryTest(BreakIterator bi, String text, int[] boundaries) {
\r
385 logln("testIsBoundary():");
\r
388 for (int i = 0; i < text.length(); i++) {
\r
389 isB = bi.isBoundary(i);
\r
390 logln("bi.isBoundary(" + i + ") -> " + isB);
\r
391 if (i == boundaries[p]) {
\r
393 errln("Wrong result from isBoundary() for " + i + ": expected true, got false");
\r
397 errln("Wrong result from isBoundary() for " + i + ": expected false, got true");
\r
402 /*Internal subroutine used for comparision of expected and acquired results */
\r
403 public void doTest(String testString, int start, int gotoffset, int expectedOffset, String expectedString) {
\r
405 String expected = expectedString;
\r
406 if (gotoffset != expectedOffset)
\r
407 errln("ERROR:****returned #" + gotoffset + " instead of #" + expectedOffset);
\r
408 if (start <= gotoffset) {
\r
409 selected = testString.substring(start, gotoffset);
\r
411 selected = testString.substring(gotoffset, start);
\r
413 if (!selected.equals(expected))
\r
414 errln("ERROR:****selected \"" + selected + "\" instead of \"" + expected + "\"");
\r
416 logln("****selected \"" + selected + "\"");
\r