1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /** 4 ******************************************************************************* 5 * Copyright (C) 1999-2010, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 10 package com.ibm.icu.dev.test.lang; 11 12 import org.junit.Test; 13 import org.junit.runner.RunWith; 14 import org.junit.runners.JUnit4; 15 16 import com.ibm.icu.dev.test.TestFmwk; 17 import com.ibm.icu.lang.UScript; 18 import com.ibm.icu.lang.UScriptRun; 19 20 @RunWith(JUnit4.class) 21 public class TestUScriptRun extends TestFmwk 22 { 23 public TestUScriptRun() 24 { 25 // nothing 26 } 27 28 private static final class RunTestData 29 { 30 String runText; 31 int runScript; 32 33 public RunTestData(String theText, int theScriptCode) 34 { 35 runText = theText; 36 runScript = theScriptCode; 37 } 38 } 39 40 private static final RunTestData[][] m_testData = { 41 { 42 new RunTestData("\u0020\u0946\u0939\u093F\u0928\u094D\u0926\u0940\u0020", UScript.DEVANAGARI), 43 new RunTestData("\u0627\u0644\u0639\u0631\u0628\u064A\u0629\u0020", UScript.ARABIC), 44 new RunTestData("\u0420\u0443\u0441\u0441\u043A\u0438\u0439\u0020", UScript.CYRILLIC), 45 new RunTestData("English (", UScript.LATIN), 46 new RunTestData("\u0E44\u0E17\u0E22", UScript.THAI), 47 new RunTestData(") ", UScript.LATIN), 48 new RunTestData("\u6F22\u5B75", UScript.HAN), 49 new RunTestData("\u3068\u3072\u3089\u304C\u306A\u3068", UScript.HIRAGANA), 50 new RunTestData("\u30AB\u30BF\u30AB\u30CA", UScript.KATAKANA), 51 new RunTestData("\uD801\uDC00\uD801\uDC01\uD801\uDC02\uD801\uDC03", UScript.DESERET), 52 }, 53 { 54 new RunTestData("((((((((((abc))))))))))", UScript.LATIN) 55 } 56 }; 57 58 private static final String padding = "This string is used for padding..."; 59 60 private void CheckScriptRuns(UScriptRun scriptRun, int[] runStarts, RunTestData[] testData) 61 { 62 int run, runStart, runLimit; 63 int runScript; 64 65 /* iterate over all the runs */ 66 run = 0; 67 while (scriptRun.next()) { 68 runStart = scriptRun.getScriptStart(); 69 runLimit = scriptRun.getScriptLimit(); 70 runScript = scriptRun.getScriptCode(); 71 72 if (runStart != runStarts[run]) { 73 errln("Incorrect start offset for run " + run + ": expected " + runStarts[run] + ", got " + runStart); 74 } 75 76 if (runLimit != runStarts[run + 1]) { 77 errln("Incorrect limit offset for run " + run + ": expected " + runStarts[run + 1] + ", got " + runLimit); 78 } 79 80 if (runScript != testData[run].runScript) { 81 errln("Incorrect script for run " + run + ": expected \"" + UScript.getName(testData[run].runScript) + "\", got \"" + UScript.getName(runScript) + "\""); 82 } 83 84 run += 1; 85 86 /* stop when we've seen all the runs we expect to see */ 87 if (run >= testData.length) { 88 break; 89 } 90 } 91 92 /* Complain if we didn't see then number of runs we expected */ 93 if (run != testData.length) { 94 errln("Incorrect number of runs: expected " + testData.length + ", got " + run); 95 } 96 } 97 98 @Test 99 public void TestContstruction() 100 { 101 UScriptRun scriptRun = null; 102 char[] nullChars = null, dummyChars = {'d', 'u', 'm', 'm', 'y'}; 103 String nullString = null, dummyString = new String(dummyChars); 104 105 try { 106 scriptRun = new UScriptRun(nullString, 0, 100); 107 errln("new UScriptRun(nullString, 0, 100) did not produce an IllegalArgumentException!"); 108 } catch (IllegalArgumentException iae) { 109 logln("PASS: UScriptRun failed as expected"); 110 } 111 112 try { 113 scriptRun = new UScriptRun(nullString, 100, 0); 114 errln("new UScriptRun(nullString, 100, 0) did not produce an IllegalArgumentException!"); 115 } catch (IllegalArgumentException iae) { 116 logln("PASS: UScriptRun failed as expected"); 117 } 118 119 try { 120 scriptRun = new UScriptRun(nullString, 0, -100); 121 errln("new UScriptRun(nullString, 0, -100) did not produce an IllegalArgumentException!"); 122 } catch (IllegalArgumentException iae) { 123 logln("PASS: UScriptRun failed as expected"); 124 } 125 126 try { 127 scriptRun = new UScriptRun(nullString, -100, 0); 128 errln("new UScriptRun(nullString, -100, 0) did not produce an IllegalArgumentException!"); 129 } catch (IllegalArgumentException iae) { 130 logln("PASS: UScriptRun failed as expected"); 131 } 132 133 try { 134 scriptRun = new UScriptRun(nullChars, 0, 100); 135 errln("new UScriptRun(nullChars, 0, 100) did not produce an IllegalArgumentException!"); 136 } catch (IllegalArgumentException iae) { 137 logln("PASS: UScriptRun failed as expected"); 138 } 139 140 try { 141 scriptRun = new UScriptRun(nullChars, 100, 0); 142 errln("new UScriptRun(nullChars, 100, 0) did not produce an IllegalArgumentException!"); 143 } catch (IllegalArgumentException iae) { 144 logln("PASS: UScriptRun failed as expected"); 145 } 146 147 try { 148 scriptRun = new UScriptRun(nullChars, 0, -100); 149 errln("new UScriptRun(nullChars, 0, -100) did not produce an IllegalArgumentException!"); 150 } catch (IllegalArgumentException iae) { 151 logln("PASS: UScriptRun failed as expected"); 152 } 153 154 try { 155 scriptRun = new UScriptRun(nullChars, -100, 0); 156 errln("new UScriptRun(nullChars, -100, 0) did not produce an IllegalArgumentException!"); 157 } catch (IllegalArgumentException iae) { 158 logln("PASS: UScriptRun failed as expected"); 159 } 160 161 try { 162 scriptRun = new UScriptRun(dummyString, 0, 6); 163 errln("new UScriptRun(dummyString, 0, 6) did not produce an IllegalArgumentException!"); 164 } catch (IllegalArgumentException iae) { 165 logln("PASS: UScriptRun failed as expected"); 166 } 167 168 try { 169 scriptRun = new UScriptRun(dummyString, 6, 0); 170 errln("new UScriptRun(dummy, 6, 0) did not produce an IllegalArgumentException!"); 171 }catch (IllegalArgumentException iae) { 172 logln("PASS: UScriptRun failed as expected"); 173 } 174 175 try { 176 scriptRun = new UScriptRun(dummyString, 0, -100); 177 errln("new UScriptRun(dummyString, 0, -100) did not produce an IllegalArgumentException!"); 178 } catch (IllegalArgumentException iae) { 179 logln("PASS: UScriptRun failed as expected"); 180 } 181 182 try { 183 scriptRun = new UScriptRun(dummyString, -100, 0); 184 errln("new UScriptRun(dummy, -100, 0) did not produce an IllegalArgumentException!"); 185 } catch (IllegalArgumentException iae) { 186 logln("PASS: UScriptRun failed as expected"); 187 } 188 189 try { 190 scriptRun = new UScriptRun(dummyChars, 0, 6); 191 errln("new UScriptRun(dummyChars, 0, 6) did not produce an IllegalArgumentException!"); 192 } catch (IllegalArgumentException iae) { 193 logln("PASS: UScriptRun failed as expected"); 194 } 195 196 try { 197 scriptRun = new UScriptRun(dummyChars, 6, 0); 198 errln("new UScriptRun(dummyChars, 6, 0) did not produce an IllegalArgumentException!"); 199 }catch (IllegalArgumentException iae) { 200 logln("PASS: UScriptRun failed as expected"); 201 } 202 203 try { 204 scriptRun = new UScriptRun(dummyChars, 0, -100); 205 errln("new UScriptRun(dummyChars, 0, -100) did not produce an IllegalArgumentException!"); 206 } catch (IllegalArgumentException iae) { 207 logln("PASS: UScriptRun failed as expected"); 208 } 209 210 try { 211 scriptRun = new UScriptRun(dummyChars, -100, 0); 212 errln("new UScriptRun(dummy, -100, 0) did not produce an IllegalArgumentException!"); 213 } catch (IllegalArgumentException iae) { 214 logln("PASS: UScriptRun failed as expected"); 215 } 216 if(scriptRun!=null){ 217 errln("Did not get the expected Exception"); 218 } 219 } 220 221 @Test 222 public void TestReset() 223 { 224 UScriptRun scriptRun = null; 225 char[] dummy = {'d', 'u', 'm', 'm', 'y'}; 226 227 try { 228 scriptRun = new UScriptRun(); 229 } catch (IllegalArgumentException iae) { 230 errln("new UScriptRun() produced an IllegalArgumentException!"); 231 } 232 233 try { 234 scriptRun.reset(0, 100); 235 errln("scriptRun.reset(0, 100) did not produce an IllegalArgumentException!"); 236 } catch (IllegalArgumentException iae) { 237 logln("PASS: scriptRun.reset failed as expected"); 238 } 239 240 try { 241 scriptRun.reset(100, 0); 242 errln("scriptRun.reset(100, 0) did not produce an IllegalArgumentException!"); 243 } catch (IllegalArgumentException iae) { 244 logln("PASS: scriptRun.reset failed as expected"); 245 } 246 247 try { 248 scriptRun.reset(0, -100); 249 errln("scriptRun.reset(0, -100) did not produce an IllegalArgumentException!"); 250 } catch (IllegalArgumentException iae) { 251 logln("PASS: scriptRun.reset failed as expected"); 252 } 253 254 try { 255 scriptRun.reset(-100, 0); 256 errln("scriptRun.reset(-100, 0) did not produce an IllegalArgumentException!"); 257 } catch (IllegalArgumentException iae) { 258 logln("PASS: scriptRun.reset failed as expected"); 259 } 260 261 try { 262 scriptRun.reset(dummy, 0, 6); 263 errln("scriptRun.reset(dummy, 0, 6) did not produce an IllegalArgumentException!"); 264 } catch (IllegalArgumentException iae) { 265 logln("PASS: scriptRun.reset failed as expected"); 266 } 267 268 try { 269 scriptRun.reset(dummy, 6, 0); 270 errln("scriptRun.reset(dummy, 6, 0) did not produce an IllegalArgumentException!"); 271 }catch (IllegalArgumentException iae) { 272 logln("PASS: scriptRun.reset failed as expected"); 273 } 274 275 try { 276 scriptRun.reset(dummy, 0, -100); 277 errln("scriptRun.reset(dummy, 0, -100) did not produce an IllegalArgumentException!"); 278 } catch (IllegalArgumentException iae) { 279 logln("PASS: scriptRun.reset failed as expected"); 280 } 281 282 try { 283 scriptRun.reset(dummy, -100, 0); 284 errln("scriptRun.reset(dummy, -100, 0) did not produce an IllegalArgumentException!"); 285 } catch (IllegalArgumentException iae) { 286 logln("PASS: scriptRun.reset failed as expected"); 287 } 288 289 try { 290 scriptRun.reset(dummy, 0, dummy.length); 291 } catch (IllegalArgumentException iae) { 292 errln("scriptRun.reset(dummy, 0, dummy.length) produced an IllegalArgumentException!"); 293 } 294 295 296 try { 297 scriptRun.reset(0, 6); 298 errln("scriptRun.reset(0, 6) did not produce an IllegalArgumentException!"); 299 } catch (IllegalArgumentException iae) { 300 logln("PASS: scriptRun.reset failed as expected"); 301 } 302 303 try { 304 scriptRun.reset(6, 0); 305 errln("scriptRun.reset(6, 0) did not produce an IllegalArgumentException!"); 306 } catch (IllegalArgumentException iae) { 307 logln("PASS: scriptRun.reset failed as expected"); 308 } 309 310 try { 311 scriptRun.reset(dummy, 0, dummy.length); 312 scriptRun.reset(); 313 } catch(IllegalArgumentException iae){ 314 errln("scriptRun.reset() produced an IllegalArgumentException!"); 315 } 316 317 try { 318 scriptRun.reset((char[]) null); 319 } catch(IllegalArgumentException iae){ 320 errln("scriptRun.reset((char[])null) produced an IllegalArgumentException!"); 321 } 322 323 try { 324 scriptRun.reset((String) null); 325 } catch(IllegalArgumentException iae){ 326 errln("scriptRun.reset((String)null) produced an IllegalArgumentException!"); 327 } 328 } 329 330 @Test 331 public void TestRuns() 332 { 333 for (int i = 0; i < m_testData.length; i += 1) { 334 RunTestData[] test = m_testData[i]; 335 int stringLimit = 0; 336 int[] runStarts = new int[test.length + 1]; 337 String testString = ""; 338 UScriptRun scriptRun = null; 339 340 /* 341 * Fill in the test string and the runStarts array. 342 */ 343 for (int run = 0; run < test.length; run += 1) { 344 runStarts[run] = stringLimit; 345 stringLimit += test[run].runText.length(); 346 testString += test[run].runText; 347 } 348 349 /* The limit of the last run */ 350 runStarts[test.length] = stringLimit; 351 352 try { 353 scriptRun = new UScriptRun(testString); 354 CheckScriptRuns(scriptRun, runStarts, test); 355 } catch (IllegalArgumentException iae) { 356 errln("new UScriptRun(testString) produced an IllegalArgumentException!"); 357 } 358 359 try { 360 scriptRun.reset(); 361 CheckScriptRuns(scriptRun, runStarts, test); 362 } catch (IllegalArgumentException iae) { 363 errln("scriptRun.reset() on a valid UScriptRun produced an IllegalArgumentException!"); 364 } 365 366 try { 367 scriptRun = new UScriptRun(testString.toCharArray()); 368 CheckScriptRuns(scriptRun, runStarts, test); 369 } catch (IllegalArgumentException iae) { 370 errln("new UScriptRun(testString.toCharArray()) produced an IllegalArgumentException!"); 371 } 372 373 try { 374 scriptRun.reset(); 375 CheckScriptRuns(scriptRun, runStarts, test); 376 } catch (IllegalArgumentException iae) { 377 errln("scriptRun.reset() on a valid UScriptRun produced an IllegalArgumentException!"); 378 } 379 380 try { 381 scriptRun = new UScriptRun(); 382 383 if (scriptRun.next()) { 384 errln("scriptRun.next() on an empty UScriptRun returned true!"); 385 } 386 } catch (IllegalArgumentException iae) { 387 errln("new UScriptRun() produced an IllegalArgumentException!"); 388 } 389 390 try { 391 scriptRun.reset(testString, 0, testString.length()); 392 CheckScriptRuns(scriptRun, runStarts, test); 393 } catch (IllegalArgumentException iae) { 394 errln("scriptRun.reset(testString, 0, testString.length) produced an IllegalArgumentException!"); 395 } 396 397 try { 398 scriptRun.reset(testString.toCharArray(), 0, testString.length()); 399 CheckScriptRuns(scriptRun, runStarts, test); 400 } catch (IllegalArgumentException iae) { 401 errln("scriptRun.reset(testString.toCharArray(), 0, testString.length) produced an IllegalArgumentException!"); 402 } 403 404 String paddedTestString = padding + testString + padding; 405 int startOffset = padding.length(); 406 int count = testString.length(); 407 408 for (int run = 0; run < runStarts.length; run += 1) { 409 runStarts[run] += startOffset; 410 } 411 412 try { 413 scriptRun.reset(paddedTestString, startOffset, count); 414 CheckScriptRuns(scriptRun, runStarts, test); 415 } catch (IllegalArgumentException iae) { 416 errln("scriptRun.reset(paddedTestString, startOffset, count) produced an IllegalArgumentException!"); 417 } 418 419 try { 420 scriptRun.reset(paddedTestString.toCharArray(), startOffset, count); 421 CheckScriptRuns(scriptRun, runStarts, test); 422 } catch (IllegalArgumentException iae) { 423 errln("scriptRun.reset(paddedTestString.toCharArray(), startOffset, count) produced an IllegalArgumentException!"); 424 } 425 426 /* Tests "public final void reset()" */ 427 // Tests when "while (stackIsNotEmpty())" is true 428 try{ 429 UScriptRun usr = new UScriptRun((String)null); 430 usr.reset(); 431 } catch (Exception e){ 432 errln("scriptRun.reset() was not suppose to produce an exception."); 433 } 434 } 435 } 436 } 437