1 /* 2 ******************************************************************************* 3 * Copyright (C) 2001-2014, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ******************************************************************************* 6 */ 7 8 /** 9 * Port From: ICU4C v1.8.1 : rbbi : RBBIAPITest 10 * Source File: $ICU4CRoot/source/test/intltest/rbbiapts.cpp 11 **/ 12 13 package com.ibm.icu.dev.test.rbbi; 14 15 import java.text.CharacterIterator; 16 import java.text.StringCharacterIterator; 17 import java.util.Locale; 18 19 import com.ibm.icu.text.BreakIterator; 20 import com.ibm.icu.text.RuleBasedBreakIterator; 21 import com.ibm.icu.util.ULocale; 22 23 /** 24 * API Test the RuleBasedBreakIterator class 25 */ 26 public class RBBIAPITest extends com.ibm.icu.dev.test.TestFmwk { 27 main(String[] args)28 public static void main(String[] args) throws Exception { 29 new RBBIAPITest().run(args); 30 } 31 32 /** 33 * Tests clone() and equals() methods of RuleBasedBreakIterator 34 **/ TestCloneEquals()35 public void TestCloneEquals() { 36 RuleBasedBreakIterator bi1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 37 RuleBasedBreakIterator biequal = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 38 RuleBasedBreakIterator bi3 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 39 RuleBasedBreakIterator bi2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault()); 40 41 String testString = "Testing word break iterators's clone() and equals()"; 42 bi1.setText(testString); 43 bi2.setText(testString); 44 biequal.setText(testString); 45 46 bi3.setText("hello"); 47 logln("Testing equals()"); 48 logln("Testing == and !="); 49 if (!bi1.equals(biequal) || bi1.equals(bi2) || bi1.equals(bi3)) 50 errln("ERROR:1 RBBI's == and !- operator failed."); 51 if (bi2.equals(biequal) || bi2.equals(bi1) || biequal.equals(bi3)) 52 errln("ERROR:2 RBBI's == and != operator failed."); 53 logln("Testing clone()"); 54 RuleBasedBreakIterator bi1clone = (RuleBasedBreakIterator) bi1.clone(); 55 RuleBasedBreakIterator bi2clone = (RuleBasedBreakIterator) bi2.clone(); 56 if (!bi1clone.equals(bi1) 57 || !bi1clone.equals(biequal) 58 || bi1clone.equals(bi3) 59 || bi1clone.equals(bi2)) 60 errln("ERROR:1 RBBI's clone() method failed"); 61 62 if (bi2clone.equals(bi1) 63 || bi2clone.equals(biequal) 64 || bi2clone.equals(bi3) 65 || !bi2clone.equals(bi2)) 66 errln("ERROR:2 RBBI's clone() method failed"); 67 68 if (!bi1.getText().equals(bi1clone.getText()) 69 || !bi2clone.getText().equals(bi2.getText()) 70 || bi2clone.equals(bi1clone)) 71 errln("ERROR: RBBI's clone() method failed"); 72 } 73 74 /** 75 * Tests toString() method of RuleBasedBreakIterator 76 **/ TestToString()77 public void TestToString() { 78 RuleBasedBreakIterator bi1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 79 RuleBasedBreakIterator bi2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault()); 80 logln("Testing toString()"); 81 bi1.setText("Hello there"); 82 RuleBasedBreakIterator bi3 = (RuleBasedBreakIterator) bi1.clone(); 83 String temp = bi1.toString(); 84 String temp2 = bi2.toString(); 85 String temp3 = bi3.toString(); 86 if (temp2.equals(temp3) || temp.equals(temp2) || !temp.equals(temp3)) 87 errln("ERROR: error in toString() method"); 88 } 89 90 /** 91 * Tests the method hashCode() of RuleBasedBreakIterator 92 **/ TestHashCode()93 public void TestHashCode() { 94 RuleBasedBreakIterator bi1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 95 RuleBasedBreakIterator bi3 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 96 RuleBasedBreakIterator bi2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault()); 97 logln("Testing hashCode()"); 98 bi1.setText("Hash code"); 99 bi2.setText("Hash code"); 100 bi3.setText("Hash code"); 101 RuleBasedBreakIterator bi1clone = (RuleBasedBreakIterator) bi1.clone(); 102 RuleBasedBreakIterator bi2clone = (RuleBasedBreakIterator) bi2.clone(); 103 if (bi1.hashCode() != bi1clone.hashCode() 104 || bi1.hashCode() != bi3.hashCode() 105 || bi1clone.hashCode() != bi3.hashCode() 106 || bi2.hashCode() != bi2clone.hashCode()) 107 errln("ERROR: identical objects have different hashcodes"); 108 109 if (bi1.hashCode() == bi2.hashCode() 110 || bi2.hashCode() == bi3.hashCode() 111 || bi1clone.hashCode() == bi2clone.hashCode() 112 || bi1clone.hashCode() == bi2.hashCode()) 113 errln("ERROR: different objects have same hashcodes"); 114 } 115 116 /** 117 * Tests the methods getText() and setText() of RuleBasedBreakIterator 118 **/ TestGetSetText()119 public void TestGetSetText() { 120 logln("Testing getText setText "); 121 String str1 = "first string."; 122 String str2 = "Second string."; 123 //RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 124 RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault()); 125 CharacterIterator text1 = new StringCharacterIterator(str1); 126 //CharacterIterator text1Clone = (CharacterIterator) text1.clone(); 127 //CharacterIterator text2 = new StringCharacterIterator(str2); 128 wordIter1.setText(str1); 129 if (!wordIter1.getText().equals(text1)) 130 errln("ERROR:1 error in setText or getText "); 131 if (wordIter1.current() != 0) 132 errln("ERROR:1 setText did not set the iteration position to the beginning of the text, it is" 133 + wordIter1.current() + "\n"); 134 wordIter1.next(2); 135 wordIter1.setText(str2); 136 if (wordIter1.current() != 0) 137 errln("ERROR:2 setText did not reset the iteration position to the beginning of the text, it is" 138 + wordIter1.current() + "\n"); 139 //ICU4J has remove the method adoptText 140 /* 141 charIter1.adoptText(text1Clone); 142 if (wordIter1.getText() == charIter1.getText() 143 || wordIter1.getText() != text2 144 || charIter1.getText() != text1) 145 errln((UnicodeString) "ERROR:2 error is getText or setText()"); 146 147 RuleBasedBreakIterator rb = (RuleBasedBreakIterator) wordIter1.clone(); 148 rb.adoptText(text1); 149 if (rb.getText() != text1) 150 errln((UnicodeString) "ERROR:1 error in adoptText "); 151 rb.adoptText(text2); 152 if (rb.getText() != text2) 153 errln((UnicodeString) "ERROR:2 error in adoptText "); 154 */ 155 } 156 157 /** 158 * Testing the methods first(), next(), next(int) and following() of RuleBasedBreakIterator 159 * TODO: Most of this test should be retired, rule behavior is much better covered by 160 * TestExtended, which is also easier to understand and maintain. 161 **/ TestFirstNextFollowing()162 public void TestFirstNextFollowing() { 163 int p, q; 164 String testString = "This is a word break. Isn't it? 2.25"; 165 logln("Testing first() and next(), following() with custom rules"); 166 logln("testing word iterator - string :- \"" + testString + "\"\n"); 167 RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault()); 168 wordIter1.setText(testString); 169 p = wordIter1.first(); 170 if (p != 0) 171 errln("ERROR: first() returned" + p + "instead of 0"); 172 q = wordIter1.next(9); 173 doTest(testString, p, q, 20, "This is a word break"); 174 p = q; 175 q = wordIter1.next(); 176 doTest(testString, p, q, 21, "."); 177 p = q; 178 q = wordIter1.next(3); 179 doTest(testString, p, q, 28, " Isn't "); 180 p = q; 181 q = wordIter1.next(2); 182 doTest(testString, p, q, 31, "it?"); 183 q = wordIter1.following(2); 184 doTest(testString, 2, q, 4, "is"); 185 q = wordIter1.following(22); 186 doTest(testString, 22, q, 27, "Isn't"); 187 wordIter1.last(); 188 p = wordIter1.next(); 189 q = wordIter1.following(wordIter1.last()); 190 if (p != BreakIterator.DONE || q != BreakIterator.DONE) 191 errln("ERROR: next()/following() at last position returned #" 192 + p + " and " + q + " instead of" + testString.length() + "\n"); 193 RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 194 testString = "Write hindi here. "; 195 logln("testing char iter - string:- \"" + testString + "\""); 196 charIter1.setText(testString); 197 p = charIter1.first(); 198 if (p != 0) 199 errln("ERROR: first() returned" + p + "instead of 0"); 200 q = charIter1.next(); 201 doTest(testString, p, q, 1, "W"); 202 p = q; 203 q = charIter1.next(4); 204 doTest(testString, p, q, 5, "rite"); 205 p = q; 206 q = charIter1.next(12); 207 doTest(testString, p, q, 17, " hindi here."); 208 p = q; 209 q = charIter1.next(-6); 210 doTest(testString, p, q, 11, " here."); 211 p = q; 212 q = charIter1.next(6); 213 doTest(testString, p, q, 17, " here."); 214 p = charIter1.following(charIter1.last()); 215 q = charIter1.next(charIter1.last()); 216 if (p != BreakIterator.DONE || q != BreakIterator.DONE) 217 errln("ERROR: following()/next() at last position returned #" 218 + p + " and " + q + " instead of" + testString.length()); 219 testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This costs $20,00,000."; 220 RuleBasedBreakIterator sentIter1 = (RuleBasedBreakIterator) BreakIterator.getSentenceInstance(Locale.getDefault()); 221 logln("testing sentence iter - String:- \"" + testString + "\""); 222 sentIter1.setText(testString); 223 p = sentIter1.first(); 224 if (p != 0) 225 errln("ERROR: first() returned" + p + "instead of 0"); 226 q = sentIter1.next(); 227 doTest(testString, p, q, 7, "Hello! "); 228 p = q; 229 q = sentIter1.next(2); 230 doTest(testString, p, q, 31, "how are you? I'am fine. "); 231 p = q; 232 q = sentIter1.next(-2); 233 doTest(testString, p, q, 7, "how are you? I'am fine. "); 234 p = q; 235 q = sentIter1.next(4); 236 doTest(testString, p, q, 60, "how are you? I'am fine. Thankyou. How are you doing? "); 237 p = q; 238 q = sentIter1.next(); 239 doTest(testString, p, q, 83, "This costs $20,00,000."); 240 q = sentIter1.following(1); 241 doTest(testString, 1, q, 7, "ello! "); 242 q = sentIter1.following(10); 243 doTest(testString, 10, q, 20, " are you? "); 244 q = sentIter1.following(20); 245 doTest(testString, 20, q, 31, "I'am fine. "); 246 p = sentIter1.following(sentIter1.last()); 247 q = sentIter1.next(sentIter1.last()); 248 if (p != BreakIterator.DONE || q != BreakIterator.DONE) 249 errln("ERROR: following()/next() at last position returned #" 250 + p + " and " + q + " instead of" + testString.length()); 251 testString = "Hello! how\r\n (are)\r you? I'am fine- Thankyou. foo\u00a0bar How, are, you? This, costs $20,00,000."; 252 logln("(UnicodeString)testing line iter - String:- \"" + testString + "\""); 253 RuleBasedBreakIterator lineIter1 = (RuleBasedBreakIterator) BreakIterator.getLineInstance(Locale.getDefault()); 254 lineIter1.setText(testString); 255 p = lineIter1.first(); 256 if (p != 0) 257 errln("ERROR: first() returned" + p + "instead of 0"); 258 q = lineIter1.next(); 259 doTest(testString, p, q, 7, "Hello! "); 260 p = q; 261 p = q; 262 q = lineIter1.next(4); 263 doTest(testString, p, q, 20, "how\r\n (are)\r "); 264 p = q; 265 q = lineIter1.next(-4); 266 doTest(testString, p, q, 7, "how\r\n (are)\r "); 267 p = q; 268 q = lineIter1.next(6); 269 doTest(testString, p, q, 30, "how\r\n (are)\r you? I'am "); 270 p = q; 271 q = lineIter1.next(); 272 doTest(testString, p, q, 36, "fine- "); 273 p = q; 274 q = lineIter1.next(2); 275 doTest(testString, p, q, 54, "Thankyou. foo\u00a0bar "); 276 q = lineIter1.following(60); 277 doTest(testString, 60, q, 64, "re, "); 278 q = lineIter1.following(1); 279 doTest(testString, 1, q, 7, "ello! "); 280 q = lineIter1.following(10); 281 doTest(testString, 10, q, 12, "\r\n"); 282 q = lineIter1.following(20); 283 doTest(testString, 20, q, 25, "you? "); 284 p = lineIter1.following(lineIter1.last()); 285 q = lineIter1.next(lineIter1.last()); 286 if (p != BreakIterator.DONE || q != BreakIterator.DONE) 287 errln("ERROR: following()/next() at last position returned #" 288 + p + " and " + q + " instead of" + testString.length()); 289 } 290 291 /** 292 * Testing the methods lastt(), previous(), and preceding() of RuleBasedBreakIterator 293 **/ TestLastPreviousPreceding()294 public void TestLastPreviousPreceding() { 295 int p, q; 296 String testString = "This is a word break. Isn't it? 2.25 dollars"; 297 logln("Testing last(),previous(), preceding() with custom rules"); 298 logln("testing word iteration for string \"" + testString + "\""); 299 RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault()); 300 wordIter1.setText(testString); 301 p = wordIter1.last(); 302 if (p != testString.length()) { 303 errln("ERROR: first() returned" + p + "instead of" + testString.length()); 304 } 305 q = wordIter1.previous(); 306 doTest(testString, p, q, 37, "dollars"); 307 p = q; 308 q = wordIter1.previous(); 309 doTest(testString, p, q, 36, " "); 310 q = wordIter1.preceding(25); 311 doTest(testString, 25, q, 22, "Isn"); 312 p = q; 313 q = wordIter1.previous(); 314 doTest(testString, p, q, 21, " "); 315 q = wordIter1.preceding(20); 316 doTest(testString, 20, q, 15, "break"); 317 p = wordIter1.preceding(wordIter1.first()); 318 if (p != BreakIterator.DONE) 319 errln("ERROR: preceding() at starting position returned #" + p + " instead of 0"); 320 testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This costs $20,00,000."; 321 logln("testing sentence iter - String:- \"" + testString + "\""); 322 RuleBasedBreakIterator sentIter1 = (RuleBasedBreakIterator) BreakIterator.getSentenceInstance(Locale.getDefault()); 323 sentIter1.setText(testString); 324 p = sentIter1.last(); 325 if (p != testString.length()) 326 errln("ERROR: last() returned" + p + "instead of " + testString.length()); 327 q = sentIter1.previous(); 328 doTest(testString, p, q, 60, "This costs $20,00,000."); 329 p = q; 330 q = sentIter1.previous(); 331 doTest(testString, p, q, 41, "How are you doing? "); 332 q = sentIter1.preceding(40); 333 doTest(testString, 40, q, 31, "Thankyou."); 334 q = sentIter1.preceding(25); 335 doTest(testString, 25, q, 20, "I'am "); 336 sentIter1.first(); 337 p = sentIter1.previous(); 338 q = sentIter1.preceding(sentIter1.first()); 339 if (p != BreakIterator.DONE || q != BreakIterator.DONE) 340 errln("ERROR: previous()/preceding() at starting position returned #" 341 + p + " and " + q + " instead of 0\n"); 342 testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This\n costs $20,00,000."; 343 logln("testing line iter - String:- \"" + testString + "\""); 344 RuleBasedBreakIterator lineIter1 = (RuleBasedBreakIterator) BreakIterator.getLineInstance(Locale.getDefault()); 345 lineIter1.setText(testString); 346 p = lineIter1.last(); 347 if (p != testString.length()) 348 errln("ERROR: last() returned" + p + "instead of " + testString.length()); 349 q = lineIter1.previous(); 350 doTest(testString, p, q, 72, "$20,00,000."); 351 p = q; 352 q = lineIter1.previous(); 353 doTest(testString, p, q, 66, "costs "); 354 q = lineIter1.preceding(40); 355 doTest(testString, 40, q, 31, "Thankyou."); 356 q = lineIter1.preceding(25); 357 doTest(testString, 25, q, 20, "I'am "); 358 lineIter1.first(); 359 p = lineIter1.previous(); 360 q = lineIter1.preceding(sentIter1.first()); 361 if (p != BreakIterator.DONE || q != BreakIterator.DONE) 362 errln("ERROR: previous()/preceding() at starting position returned #" 363 + p + " and " + q + " instead of 0\n"); 364 } 365 366 /** 367 * Tests the method IsBoundary() of RuleBasedBreakIterator 368 **/ TestIsBoundary()369 public void TestIsBoundary() { 370 String testString1 = "Write here. \u092d\u0301\u0930\u0924 \u0938\u0941\u0902\u0926\u0930 a\u0301u"; 371 RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault()); 372 charIter1.setText(testString1); 373 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}; 374 doBoundaryTest(charIter1, testString1, bounds1); 375 RuleBasedBreakIterator wordIter2 = (RuleBasedBreakIterator) BreakIterator.getWordInstance(Locale.getDefault()); 376 wordIter2.setText(testString1); 377 int bounds2[] = {0, 5, 6, 10, 11, 12, 16, 17, 22, 23, 26}; 378 doBoundaryTest(wordIter2, testString1, bounds2); 379 } 380 381 /** 382 * Tests the rule status return value constants 383 */ TestRuleStatus()384 public void TestRuleStatus() { 385 BreakIterator bi = BreakIterator.getWordInstance(ULocale.ENGLISH); 386 387 bi.setText("# "); 388 assertEquals(null, bi.next(), 1); 389 assertTrue(null, bi.getRuleStatus() >= RuleBasedBreakIterator.WORD_NONE); 390 assertTrue(null, bi.getRuleStatus() < RuleBasedBreakIterator.WORD_NONE_LIMIT); 391 392 bi.setText("3 "); 393 assertEquals(null, bi.next(), 1); 394 assertTrue(null, bi.getRuleStatus() >= RuleBasedBreakIterator.WORD_NUMBER); 395 assertTrue(null, bi.getRuleStatus() < RuleBasedBreakIterator.WORD_NUMBER_LIMIT); 396 397 bi.setText("a "); 398 assertEquals(null, bi.next(), 1); 399 assertTrue(null, bi.getRuleStatus() >= RuleBasedBreakIterator.WORD_LETTER ); 400 assertTrue(null, bi.getRuleStatus() < RuleBasedBreakIterator.WORD_LETTER_LIMIT); 401 402 403 bi.setText("イ "); 404 assertEquals(null, bi.next(), 1); 405 assertTrue(null, bi.getRuleStatus() >= RuleBasedBreakIterator.WORD_KANA ); 406 // TODO: ticket #10261, Kana is not returning the correct status. 407 // assertTrue(null, bi.getRuleStatus() < RuleBasedBreakIterator.WORD_KANA_LIMIT); 408 // System.out.println("\n" + bi.getRuleStatus()); 409 410 bi.setText("退 "); 411 assertEquals(null, bi.next(), 1); 412 assertTrue(null, bi.getRuleStatus() >= RuleBasedBreakIterator.WORD_IDEO ); 413 assertTrue(null, bi.getRuleStatus() < RuleBasedBreakIterator.WORD_IDEO_LIMIT); 414 } 415 416 //--------------------------------------------- 417 //Internal subroutines 418 //--------------------------------------------- 419 420 /* Internal subroutine used by TestIsBoundary() */ doBoundaryTest(BreakIterator bi, String text, int[] boundaries)421 public void doBoundaryTest(BreakIterator bi, String text, int[] boundaries) { 422 logln("testIsBoundary():"); 423 int p = 0; 424 boolean isB; 425 for (int i = 0; i < text.length(); i++) { 426 isB = bi.isBoundary(i); 427 logln("bi.isBoundary(" + i + ") -> " + isB); 428 if (i == boundaries[p]) { 429 if (!isB) 430 errln("Wrong result from isBoundary() for " + i + ": expected true, got false"); 431 p++; 432 } else { 433 if (isB) 434 errln("Wrong result from isBoundary() for " + i + ": expected false, got true"); 435 } 436 } 437 } 438 439 /*Internal subroutine used for comparision of expected and acquired results */ doTest(String testString, int start, int gotoffset, int expectedOffset, String expectedString)440 public void doTest(String testString, int start, int gotoffset, int expectedOffset, String expectedString) { 441 String selected; 442 String expected = expectedString; 443 if (gotoffset != expectedOffset) 444 errln("ERROR:****returned #" + gotoffset + " instead of #" + expectedOffset); 445 if (start <= gotoffset) { 446 selected = testString.substring(start, gotoffset); 447 } else { 448 selected = testString.substring(gotoffset, start); 449 } 450 if (!selected.equals(expected)) 451 errln("ERROR:****selected \"" + selected + "\" instead of \"" + expected + "\""); 452 else 453 logln("****selected \"" + selected + "\""); 454 } 455 } 456