2 *******************************************************************************
\r
3 * Copyright (C) 2001-2010, 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 java.text.CharacterIterator;
\r
16 import java.text.StringCharacterIterator;
\r
17 import java.util.Locale;
\r
19 import com.ibm.icu.text.BreakIterator;
\r
20 import com.ibm.icu.text.RuleBasedBreakIterator;
\r
23 * API Test the RuleBasedBreakIterator class
\r
25 public class RBBIAPITest extends com.ibm.icu.dev.test.TestFmwk {
\r
27 public static void main(String[] args) throws Exception {
\r
28 new RBBIAPITest().run(args);
\r
32 * Tests clone() and equals() methods of RuleBasedBreakIterator
\r
34 public void TestCloneEquals() {
\r
35 RuleBasedBreakIterator bi1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
36 RuleBasedBreakIterator biequal = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
37 RuleBasedBreakIterator bi3 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
38 RuleBasedBreakIterator bi2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
\r
40 String testString = "Testing word break iterators's clone() and equals()";
\r
41 bi1.setText(testString);
\r
42 bi2.setText(testString);
\r
43 biequal.setText(testString);
\r
45 bi3.setText("hello");
\r
46 logln("Testing equals()");
\r
47 logln("Testing == and !=");
\r
48 if (!bi1.equals(biequal) || bi1.equals(bi2) || bi1.equals(bi3))
\r
49 errln("ERROR:1 RBBI's == and !- operator failed.");
\r
50 if (bi2.equals(biequal) || bi2.equals(bi1) || biequal.equals(bi3))
\r
51 errln("ERROR:2 RBBI's == and != operator failed.");
\r
52 logln("Testing clone()");
\r
53 RuleBasedBreakIterator bi1clone = (RuleBasedBreakIterator) bi1.clone();
\r
54 RuleBasedBreakIterator bi2clone = (RuleBasedBreakIterator) bi2.clone();
\r
55 if (!bi1clone.equals(bi1)
\r
56 || !bi1clone.equals(biequal)
\r
57 || bi1clone.equals(bi3)
\r
58 || bi1clone.equals(bi2))
\r
59 errln("ERROR:1 RBBI's clone() method failed");
\r
61 if (bi2clone.equals(bi1)
\r
62 || bi2clone.equals(biequal)
\r
63 || bi2clone.equals(bi3)
\r
64 || !bi2clone.equals(bi2))
\r
65 errln("ERROR:2 RBBI's clone() method failed");
\r
67 if (!bi1.getText().equals(bi1clone.getText())
\r
68 || !bi2clone.getText().equals(bi2.getText())
\r
69 || bi2clone.equals(bi1clone))
\r
70 errln("ERROR: RBBI's clone() method failed");
\r
74 * Tests toString() method of RuleBasedBreakIterator
\r
76 public void TestToString() {
\r
77 RuleBasedBreakIterator bi1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
78 RuleBasedBreakIterator bi2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
\r
79 logln("Testing toString()");
\r
80 bi1.setText("Hello there");
\r
81 RuleBasedBreakIterator bi3 = (RuleBasedBreakIterator) bi1.clone();
\r
82 String temp = bi1.toString();
\r
83 String temp2 = bi2.toString();
\r
84 String temp3 = bi3.toString();
\r
85 if (temp2.equals(temp3) || temp.equals(temp2) || !temp.equals(temp3))
\r
86 errln("ERROR: error in toString() method");
\r
90 * Tests the method hashCode() of RuleBasedBreakIterator
\r
92 public void TestHashCode() {
\r
93 RuleBasedBreakIterator bi1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
94 RuleBasedBreakIterator bi3 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
95 RuleBasedBreakIterator bi2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
\r
96 logln("Testing hashCode()");
\r
97 bi1.setText("Hash code");
\r
98 bi2.setText("Hash code");
\r
99 bi3.setText("Hash code");
\r
100 RuleBasedBreakIterator bi1clone = (RuleBasedBreakIterator) bi1.clone();
\r
101 RuleBasedBreakIterator bi2clone = (RuleBasedBreakIterator) bi2.clone();
\r
102 if (bi1.hashCode() != bi1clone.hashCode()
\r
103 || bi1.hashCode() != bi3.hashCode()
\r
104 || bi1clone.hashCode() != bi3.hashCode()
\r
105 || bi2.hashCode() != bi2clone.hashCode())
\r
106 errln("ERROR: identical objects have different hashcodes");
\r
108 if (bi1.hashCode() == bi2.hashCode()
\r
109 || bi2.hashCode() == bi3.hashCode()
\r
110 || bi1clone.hashCode() == bi2clone.hashCode()
\r
111 || bi1clone.hashCode() == bi2.hashCode())
\r
112 errln("ERROR: different objects have same hashcodes");
\r
116 * Tests the methods getText() and setText() of RuleBasedBreakIterator
\r
118 public void TestGetSetText() {
\r
119 logln("Testing getText setText ");
\r
120 String str1 = "first string.";
\r
121 String str2 = "Second string.";
\r
122 //RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
123 RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
\r
124 CharacterIterator text1 = new StringCharacterIterator(str1);
\r
125 //CharacterIterator text1Clone = (CharacterIterator) text1.clone();
\r
126 //CharacterIterator text2 = new StringCharacterIterator(str2);
\r
127 wordIter1.setText(str1);
\r
128 if (!wordIter1.getText().equals(text1))
\r
129 errln("ERROR:1 error in setText or getText ");
\r
130 if (wordIter1.current() != 0)
\r
131 errln("ERROR:1 setText did not set the iteration position to the beginning of the text, it is"
\r
132 + wordIter1.current() + "\n");
\r
134 wordIter1.setText(str2);
\r
135 if (wordIter1.current() != 0)
\r
136 errln("ERROR:2 setText did not reset the iteration position to the beginning of the text, it is"
\r
137 + wordIter1.current() + "\n");
\r
138 //ICU4J has remove the method adoptText
\r
140 charIter1.adoptText(text1Clone);
\r
141 if (wordIter1.getText() == charIter1.getText()
\r
142 || wordIter1.getText() != text2
\r
143 || charIter1.getText() != text1)
\r
144 errln((UnicodeString) "ERROR:2 error is getText or setText()");
\r
146 RuleBasedBreakIterator rb = (RuleBasedBreakIterator) wordIter1.clone();
\r
147 rb.adoptText(text1);
\r
148 if (rb.getText() != text1)
\r
149 errln((UnicodeString) "ERROR:1 error in adoptText ");
\r
150 rb.adoptText(text2);
\r
151 if (rb.getText() != text2)
\r
152 errln((UnicodeString) "ERROR:2 error in adoptText ");
\r
157 * Testing the methods first(), next(), next(int) and following() of RuleBasedBreakIterator
\r
158 * TODO: Most of this test should be retired, rule behavior is much better covered by
\r
159 * TestExtended, which is also easier to understand and maintain.
\r
161 public void TestFirstNextFollowing() {
\r
163 String testString = "This is a word break. Isn't it? 2.25";
\r
164 logln("Testing first() and next(), following() with custom rules");
\r
165 logln("testing word iterator - string :- \"" + testString + "\"\n");
\r
166 RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
\r
167 wordIter1.setText(testString);
\r
168 p = wordIter1.first();
\r
170 errln("ERROR: first() returned" + p + "instead of 0");
\r
171 q = wordIter1.next(9);
\r
172 doTest(testString, p, q, 20, "This is a word break");
\r
174 q = wordIter1.next();
\r
175 doTest(testString, p, q, 21, ".");
\r
177 q = wordIter1.next(3);
\r
178 doTest(testString, p, q, 28, " Isn't ");
\r
180 q = wordIter1.next(2);
\r
181 doTest(testString, p, q, 31, "it?");
\r
182 q = wordIter1.following(2);
\r
183 doTest(testString, 2, q, 4, "is");
\r
184 q = wordIter1.following(22);
\r
185 doTest(testString, 22, q, 27, "Isn't");
\r
187 p = wordIter1.next();
\r
188 q = wordIter1.following(wordIter1.last());
\r
189 if (p != BreakIterator.DONE || q != BreakIterator.DONE)
\r
190 errln("ERROR: next()/following() at last position returned #"
\r
191 + p + " and " + q + " instead of" + testString.length() + "\n");
\r
192 RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
193 testString = "Write hindi here. ";
\r
194 logln("testing char iter - string:- \"" + testString + "\"");
\r
195 charIter1.setText(testString);
\r
196 p = charIter1.first();
\r
198 errln("ERROR: first() returned" + p + "instead of 0");
\r
199 q = charIter1.next();
\r
200 doTest(testString, p, q, 1, "W");
\r
202 q = charIter1.next(4);
\r
203 doTest(testString, p, q, 5, "rite");
\r
205 q = charIter1.next(12);
\r
206 doTest(testString, p, q, 17, " hindi here.");
\r
208 q = charIter1.next(-6);
\r
209 doTest(testString, p, q, 11, " here.");
\r
211 q = charIter1.next(6);
\r
212 doTest(testString, p, q, 17, " here.");
\r
213 p = charIter1.following(charIter1.last());
\r
214 q = charIter1.next(charIter1.last());
\r
215 if (p != BreakIterator.DONE || q != BreakIterator.DONE)
\r
216 errln("ERROR: following()/next() at last position returned #"
\r
217 + p + " and " + q + " instead of" + testString.length());
\r
218 testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This costs $20,00,000.";
\r
219 RuleBasedBreakIterator sentIter1 = (RuleBasedBreakIterator) BreakIterator.getSentenceInstance(Locale.getDefault());
\r
220 logln("testing sentence iter - String:- \"" + testString + "\"");
\r
221 sentIter1.setText(testString);
\r
222 p = sentIter1.first();
\r
224 errln("ERROR: first() returned" + p + "instead of 0");
\r
225 q = sentIter1.next();
\r
226 doTest(testString, p, q, 7, "Hello! ");
\r
228 q = sentIter1.next(2);
\r
229 doTest(testString, p, q, 31, "how are you? I'am fine. ");
\r
231 q = sentIter1.next(-2);
\r
232 doTest(testString, p, q, 7, "how are you? I'am fine. ");
\r
234 q = sentIter1.next(4);
\r
235 doTest(testString, p, q, 60, "how are you? I'am fine. Thankyou. How are you doing? ");
\r
237 q = sentIter1.next();
\r
238 doTest(testString, p, q, 83, "This costs $20,00,000.");
\r
239 q = sentIter1.following(1);
\r
240 doTest(testString, 1, q, 7, "ello! ");
\r
241 q = sentIter1.following(10);
\r
242 doTest(testString, 10, q, 20, " are you? ");
\r
243 q = sentIter1.following(20);
\r
244 doTest(testString, 20, q, 31, "I'am fine. ");
\r
245 p = sentIter1.following(sentIter1.last());
\r
246 q = sentIter1.next(sentIter1.last());
\r
247 if (p != BreakIterator.DONE || q != BreakIterator.DONE)
\r
248 errln("ERROR: following()/next() at last position returned #"
\r
249 + p + " and " + q + " instead of" + testString.length());
\r
250 testString = "Hello! how\r\n (are)\r you? I'am fine- Thankyou. foo\u00a0bar How, are, you? This, costs $20,00,000.";
\r
251 logln("(UnicodeString)testing line iter - String:- \"" + testString + "\"");
\r
252 RuleBasedBreakIterator lineIter1 = (RuleBasedBreakIterator) BreakIterator.getLineInstance(Locale.getDefault());
\r
253 lineIter1.setText(testString);
\r
254 p = lineIter1.first();
\r
256 errln("ERROR: first() returned" + p + "instead of 0");
\r
257 q = lineIter1.next();
\r
258 doTest(testString, p, q, 7, "Hello! ");
\r
261 q = lineIter1.next(4);
\r
262 doTest(testString, p, q, 20, "how\r\n (are)\r ");
\r
264 q = lineIter1.next(-4);
\r
265 doTest(testString, p, q, 7, "how\r\n (are)\r ");
\r
267 q = lineIter1.next(6);
\r
268 doTest(testString, p, q, 30, "how\r\n (are)\r you? I'am ");
\r
270 q = lineIter1.next();
\r
271 doTest(testString, p, q, 36, "fine- ");
\r
273 q = lineIter1.next(2);
\r
274 doTest(testString, p, q, 54, "Thankyou. foo\u00a0bar ");
\r
275 q = lineIter1.following(60);
\r
276 doTest(testString, 60, q, 64, "re, ");
\r
277 q = lineIter1.following(1);
\r
278 doTest(testString, 1, q, 7, "ello! ");
\r
279 q = lineIter1.following(10);
\r
280 doTest(testString, 10, q, 12, "\r\n");
\r
281 q = lineIter1.following(20);
\r
282 doTest(testString, 20, q, 25, "you? ");
\r
283 p = lineIter1.following(lineIter1.last());
\r
284 q = lineIter1.next(lineIter1.last());
\r
285 if (p != BreakIterator.DONE || q != BreakIterator.DONE)
\r
286 errln("ERROR: following()/next() at last position returned #"
\r
287 + p + " and " + q + " instead of" + testString.length());
\r
291 * Testing the methods lastt(), previous(), and preceding() of RuleBasedBreakIterator
\r
293 public void TestLastPreviousPreceding() {
\r
295 String testString = "This is a word break. Isn't it? 2.25 dollars";
\r
296 logln("Testing last(),previous(), preceding() with custom rules");
\r
297 logln("testing word iteration for string \"" + testString + "\"");
\r
298 RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
\r
299 wordIter1.setText(testString);
\r
300 p = wordIter1.last();
\r
301 if (p != testString.length()) {
\r
302 errln("ERROR: first() returned" + p + "instead of" + testString.length());
\r
304 q = wordIter1.previous();
\r
305 doTest(testString, p, q, 37, "dollars");
\r
307 q = wordIter1.previous();
\r
308 doTest(testString, p, q, 36, " ");
\r
309 q = wordIter1.preceding(25);
\r
310 doTest(testString, 25, q, 22, "Isn");
\r
312 q = wordIter1.previous();
\r
313 doTest(testString, p, q, 21, " ");
\r
314 q = wordIter1.preceding(20);
\r
315 doTest(testString, 20, q, 15, "break");
\r
316 p = wordIter1.preceding(wordIter1.first());
\r
317 if (p != BreakIterator.DONE)
\r
318 errln("ERROR: preceding() at starting position returned #" + p + " instead of 0");
\r
319 testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This costs $20,00,000.";
\r
320 logln("testing sentence iter - String:- \"" + testString + "\"");
\r
321 RuleBasedBreakIterator sentIter1 = (RuleBasedBreakIterator) BreakIterator.getSentenceInstance(Locale.getDefault());
\r
322 sentIter1.setText(testString);
\r
323 p = sentIter1.last();
\r
324 if (p != testString.length())
\r
325 errln("ERROR: last() returned" + p + "instead of " + testString.length());
\r
326 q = sentIter1.previous();
\r
327 doTest(testString, p, q, 60, "This costs $20,00,000.");
\r
329 q = sentIter1.previous();
\r
330 doTest(testString, p, q, 41, "How are you doing? ");
\r
331 q = sentIter1.preceding(40);
\r
332 doTest(testString, 40, q, 31, "Thankyou.");
\r
333 q = sentIter1.preceding(25);
\r
334 doTest(testString, 25, q, 20, "I'am ");
\r
336 p = sentIter1.previous();
\r
337 q = sentIter1.preceding(sentIter1.first());
\r
338 if (p != BreakIterator.DONE || q != BreakIterator.DONE)
\r
339 errln("ERROR: previous()/preceding() at starting position returned #"
\r
340 + p + " and " + q + " instead of 0\n");
\r
341 testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This\n costs $20,00,000.";
\r
342 logln("testing line iter - String:- \"" + testString + "\"");
\r
343 RuleBasedBreakIterator lineIter1 = (RuleBasedBreakIterator) BreakIterator.getLineInstance(Locale.getDefault());
\r
344 lineIter1.setText(testString);
\r
345 p = lineIter1.last();
\r
346 if (p != testString.length())
\r
347 errln("ERROR: last() returned" + p + "instead of " + testString.length());
\r
348 q = lineIter1.previous();
\r
349 doTest(testString, p, q, 72, "$20,00,000.");
\r
351 q = lineIter1.previous();
\r
352 doTest(testString, p, q, 66, "costs ");
\r
353 q = lineIter1.preceding(40);
\r
354 doTest(testString, 40, q, 31, "Thankyou.");
\r
355 q = lineIter1.preceding(25);
\r
356 doTest(testString, 25, q, 20, "I'am ");
\r
358 p = lineIter1.previous();
\r
359 q = lineIter1.preceding(sentIter1.first());
\r
360 if (p != BreakIterator.DONE || q != BreakIterator.DONE)
\r
361 errln("ERROR: previous()/preceding() at starting position returned #"
\r
362 + p + " and " + q + " instead of 0\n");
\r
366 * Tests the method IsBoundary() of RuleBasedBreakIterator
\r
368 public void TestIsBoundary() {
\r
369 String testString1 = "Write here. \u092d\u0301\u0930\u0924 \u0938\u0941\u0902\u0926\u0930 a\u0301u";
\r
370 RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
\r
371 charIter1.setText(testString1);
\r
372 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
373 doBoundaryTest(charIter1, testString1, bounds1);
\r
374 RuleBasedBreakIterator wordIter2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault());
\r
375 wordIter2.setText(testString1);
\r
376 int bounds2[] = {0, 5, 6, 10, 11, 12, 16, 17, 22, 23, 26};
\r
377 doBoundaryTest(wordIter2, testString1, bounds2);
\r
380 //---------------------------------------------
\r
381 //Internal subroutines
\r
382 //---------------------------------------------
\r
384 /* Internal subroutine used by TestIsBoundary() */
\r
385 public void doBoundaryTest(BreakIterator bi, String text, int[] boundaries) {
\r
386 logln("testIsBoundary():");
\r
389 for (int i = 0; i < text.length(); i++) {
\r
390 isB = bi.isBoundary(i);
\r
391 logln("bi.isBoundary(" + i + ") -> " + isB);
\r
392 if (i == boundaries[p]) {
\r
394 errln("Wrong result from isBoundary() for " + i + ": expected true, got false");
\r
398 errln("Wrong result from isBoundary() for " + i + ": expected false, got true");
\r
403 /*Internal subroutine used for comparision of expected and acquired results */
\r
404 public void doTest(String testString, int start, int gotoffset, int expectedOffset, String expectedString) {
\r
406 String expected = expectedString;
\r
407 if (gotoffset != expectedOffset)
\r
408 errln("ERROR:****returned #" + gotoffset + " instead of #" + expectedOffset);
\r
409 if (start <= gotoffset) {
\r
410 selected = testString.substring(start, gotoffset);
\r
412 selected = testString.substring(gotoffset, start);
\r
414 if (!selected.equals(expected))
\r
415 errln("ERROR:****selected \"" + selected + "\" instead of \"" + expected + "\"");
\r
417 logln("****selected \"" + selected + "\"");
\r