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