1 /******************************************************************** 2 * COPYRIGHT: 3 * Copyright (c) 1997-2011, International Business Machines Corporation and 4 * others. All Rights Reserved. 5 ********************************************************************/ 6 /******************************************************************************** 7 * 8 * File CBIAPTS.C 9 * 10 * Modification History: 11 * Name Description 12 * Madhu Katragadda Creation 13 *********************************************************************************/ 14 /*C API TEST FOR BREAKITERATOR */ 15 /** 16 * This is an API test. It doesn't test very many cases, and doesn't 17 * try to test the full functionality. It just calls each function in the class and 18 * verifies that it works on a basic level. 19 **/ 20 21 #include "unicode/utypes.h" 22 23 #if !UCONFIG_NO_BREAK_ITERATION 24 25 #include <stdlib.h> 26 #include <string.h> 27 #include "unicode/uloc.h" 28 #include "unicode/ubrk.h" 29 #include "unicode/ustring.h" 30 #include "unicode/ucnv.h" 31 #include "unicode/utext.h" 32 #include "cintltst.h" 33 #include "cbiapts.h" 34 35 #define TEST_ASSERT_SUCCESS(status) {if (U_FAILURE(status)) { \ 36 log_data_err("Failure at file %s, line %d, error = %s (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status));}} 37 38 #define TEST_ASSERT(expr) {if ((expr)==FALSE) { \ 39 log_data_err("Test Failure at file %s, line %d (Are you missing data?)\n", __FILE__, __LINE__);}} 40 41 static void TestBreakIteratorSafeClone(void); 42 static void TestBreakIteratorRules(void); 43 static void TestBreakIteratorRuleError(void); 44 static void TestBreakIteratorStatusVec(void); 45 static void TestBreakIteratorUText(void); 46 static void TestBreakIteratorTailoring(void); 47 48 void addBrkIterAPITest(TestNode** root); 49 50 void addBrkIterAPITest(TestNode** root) 51 { 52 #if !UCONFIG_NO_FILE_IO 53 addTest(root, &TestBreakIteratorCAPI, "tstxtbd/cbiapts/TestBreakIteratorCAPI"); 54 addTest(root, &TestBreakIteratorSafeClone, "tstxtbd/cbiapts/TestBreakIteratorSafeClone"); 55 addTest(root, &TestBreakIteratorUText, "tstxtbd/cbiapts/TestBreakIteratorUText"); 56 #endif 57 addTest(root, &TestBreakIteratorRules, "tstxtbd/cbiapts/TestBreakIteratorRules"); 58 addTest(root, &TestBreakIteratorRuleError, "tstxtbd/cbiapts/TestBreakIteratorRuleError"); 59 addTest(root, &TestBreakIteratorStatusVec, "tstxtbd/cbiapts/TestBreakIteratorStatusVec"); 60 addTest(root, &TestBreakIteratorTailoring, "tstxtbd/cbiapts/TestBreakIteratorTailoring"); 61 } 62 63 #define CLONETEST_ITERATOR_COUNT 2 64 65 /* 66 * Utility function for converting char * to UChar * strings, to 67 * simplify the test code. Converted strings are put in heap allocated 68 * storage. A hook (probably a local in the caller's code) allows all 69 * strings converted with that hook to be freed with a single call. 70 */ 71 typedef struct StringStruct { 72 struct StringStruct *link; 73 UChar str[1]; 74 } StringStruct; 75 76 77 static UChar* toUChar(const char *src, void **freeHook) { 78 /* Structure of the memory that we allocate on the heap */ 79 80 int32_t numUChars; 81 int32_t destSize; 82 UChar stackBuf[2000 + sizeof(void *)/sizeof(UChar)]; 83 StringStruct *dest; 84 UConverter *cnv; 85 86 UErrorCode status = U_ZERO_ERROR; 87 if (src == NULL) { 88 return NULL; 89 }; 90 91 cnv = ucnv_open(NULL, &status); 92 if(U_FAILURE(status) || cnv == NULL) { 93 return NULL; 94 } 95 ucnv_reset(cnv); 96 numUChars = ucnv_toUChars(cnv, 97 stackBuf, 98 2000, 99 src, -1, 100 &status); 101 102 destSize = (numUChars+1) * sizeof(UChar) + sizeof(struct StringStruct); 103 dest = (StringStruct *)malloc(destSize); 104 if (dest != NULL) { 105 if (status == U_BUFFER_OVERFLOW_ERROR || status == U_STRING_NOT_TERMINATED_WARNING) { 106 ucnv_toUChars(cnv, dest->str, numUChars+1, src, -1, &status); 107 } else if (status == U_ZERO_ERROR) { 108 u_strcpy(dest->str, stackBuf); 109 } else { 110 free(dest); 111 dest = NULL; 112 } 113 } 114 115 ucnv_reset(cnv); /* be good citizens */ 116 ucnv_close(cnv); 117 if (dest == NULL) { 118 return NULL; 119 } 120 121 dest->link = (StringStruct*)(*freeHook); 122 *freeHook = dest; 123 return dest->str; 124 } 125 126 static void freeToUCharStrings(void **hook) { 127 StringStruct *s = *(StringStruct **)hook; 128 while (s != NULL) { 129 StringStruct *next = s->link; 130 free(s); 131 s = next; 132 } 133 } 134 135 136 static void TestBreakIteratorCAPI() 137 { 138 UErrorCode status = U_ZERO_ERROR; 139 UBreakIterator *word, *sentence, *line, *character, *b, *bogus; 140 int32_t start,pos,end,to; 141 int32_t i; 142 int32_t count = 0; 143 144 UChar text[50]; 145 146 /* Note: the adjacent "" are concatenating strings, not adding a \" to the 147 string, which is probably what whoever wrote this intended. Don't fix, 148 because it would throw off the hard coded break positions in the following 149 tests. */ 150 u_uastrcpy(text, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah"); 151 152 153 /*test ubrk_open()*/ 154 log_verbose("\nTesting BreakIterator open functions\n"); 155 156 /* Use french for fun */ 157 word = ubrk_open(UBRK_WORD, "en_US", text, u_strlen(text), &status); 158 if(status == U_FILE_ACCESS_ERROR) { 159 log_data_err("Check your data - it doesn't seem to be around\n"); 160 return; 161 } else if(U_FAILURE(status)){ 162 log_err_status(status, "FAIL: Error in ubrk_open() for word breakiterator: %s\n", myErrorName(status)); 163 } 164 else{ 165 log_verbose("PASS: Successfully opened word breakiterator\n"); 166 } 167 168 sentence = ubrk_open(UBRK_SENTENCE, "en_US", text, u_strlen(text), &status); 169 if(U_FAILURE(status)){ 170 log_err_status(status, "FAIL: Error in ubrk_open() for sentence breakiterator: %s\n", myErrorName(status)); 171 return; 172 } 173 else{ 174 log_verbose("PASS: Successfully opened sentence breakiterator\n"); 175 } 176 177 line = ubrk_open(UBRK_LINE, "en_US", text, u_strlen(text), &status); 178 if(U_FAILURE(status)){ 179 log_err("FAIL: Error in ubrk_open() for line breakiterator: %s\n", myErrorName(status)); 180 return; 181 } 182 else{ 183 log_verbose("PASS: Successfully opened line breakiterator\n"); 184 } 185 186 character = ubrk_open(UBRK_CHARACTER, "en_US", text, u_strlen(text), &status); 187 if(U_FAILURE(status)){ 188 log_err("FAIL: Error in ubrk_open() for character breakiterator: %s\n", myErrorName(status)); 189 return; 190 } 191 else{ 192 log_verbose("PASS: Successfully opened character breakiterator\n"); 193 } 194 /*trying to open an illegal iterator*/ 195 bogus = ubrk_open((UBreakIteratorType)5, "en_US", text, u_strlen(text), &status); 196 if(U_SUCCESS(status)){ 197 log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_ILLEGAL_ARGUMENT_ERROR\n"); 198 } 199 if(U_FAILURE(status)){ 200 if(status != U_ILLEGAL_ARGUMENT_ERROR){ 201 log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_ILLEGAL_ARGUMENT_ERROR\n Got %s\n", myErrorName(status)); 202 } 203 } 204 status=U_ZERO_ERROR; 205 206 207 /* ======= Test ubrk_countAvialable() and ubrk_getAvialable() */ 208 209 log_verbose("\nTesting ubrk_countAvailable() and ubrk_getAvailable()\n"); 210 count=ubrk_countAvailable(); 211 /* use something sensible w/o hardcoding the count */ 212 if(count < 0){ 213 log_err("FAIL: Error in ubrk_countAvialable() returned %d\n", count); 214 } 215 else{ 216 log_verbose("PASS: ubrk_countAvialable() successful returned %d\n", count); 217 } 218 for(i=0;i<count;i++) 219 { 220 log_verbose("%s\n", ubrk_getAvailable(i)); 221 if (ubrk_getAvailable(i) == 0) 222 log_err("No locale for which breakiterator is applicable\n"); 223 else 224 log_verbose("A locale %s for which breakiterator is applicable\n",ubrk_getAvailable(i)); 225 } 226 227 /*========Test ubrk_first(), ubrk_last()...... and other functions*/ 228 229 log_verbose("\nTesting the functions for word\n"); 230 start = ubrk_first(word); 231 if(start!=0) 232 log_err("error ubrk_start(word) did not return 0\n"); 233 log_verbose("first (word = %d\n", (int32_t)start); 234 pos=ubrk_next(word); 235 if(pos!=4) 236 log_err("error ubrk_next(word) did not return 4\n"); 237 log_verbose("next (word = %d\n", (int32_t)pos); 238 pos=ubrk_following(word, 4); 239 if(pos!=5) 240 log_err("error ubrl_following(word,4) did not return 6\n"); 241 log_verbose("next (word = %d\n", (int32_t)pos); 242 end=ubrk_last(word); 243 if(end!=49) 244 log_err("error ubrk_last(word) did not return 49\n"); 245 log_verbose("last (word = %d\n", (int32_t)end); 246 247 pos=ubrk_previous(word); 248 log_verbose("%d %d\n", end, pos); 249 250 pos=ubrk_previous(word); 251 log_verbose("%d \n", pos); 252 253 if (ubrk_isBoundary(word, 2) != FALSE) { 254 log_err("error ubrk_isBoundary(word, 2) did not return FALSE\n"); 255 } 256 pos=ubrk_current(word); 257 if (pos != 4) { 258 log_err("error ubrk_current() != 4 after ubrk_isBoundary(word, 2)\n"); 259 } 260 if (ubrk_isBoundary(word, 4) != TRUE) { 261 log_err("error ubrk_isBoundary(word, 4) did not return TRUE\n"); 262 } 263 264 265 266 log_verbose("\nTesting the functions for character\n"); 267 ubrk_first(character); 268 pos = ubrk_following(character, 5); 269 if(pos!=6) 270 log_err("error ubrk_following(character,5) did not return 6\n"); 271 log_verbose("Following (character,5) = %d\n", (int32_t)pos); 272 pos=ubrk_following(character, 18); 273 if(pos!=19) 274 log_err("error ubrk_following(character,18) did not return 19\n"); 275 log_verbose("Followingcharacter,18) = %d\n", (int32_t)pos); 276 pos=ubrk_preceding(character, 22); 277 if(pos!=21) 278 log_err("error ubrk_preceding(character,22) did not return 21\n"); 279 log_verbose("preceding(character,22) = %d\n", (int32_t)pos); 280 281 282 log_verbose("\nTesting the functions for line\n"); 283 pos=ubrk_first(line); 284 if(pos != 0) 285 log_err("error ubrk_first(line) returned %d, expected 0\n", (int32_t)pos); 286 pos = ubrk_next(line); 287 pos=ubrk_following(line, 18); 288 if(pos!=22) 289 log_err("error ubrk_following(line) did not return 22\n"); 290 log_verbose("following (line) = %d\n", (int32_t)pos); 291 292 293 log_verbose("\nTesting the functions for sentence\n"); 294 ubrk_first(sentence); 295 pos = ubrk_current(sentence); 296 log_verbose("Current(sentence) = %d\n", (int32_t)pos); 297 pos = ubrk_last(sentence); 298 if(pos!=49) 299 log_err("error ubrk_last for sentence did not return 49\n"); 300 log_verbose("Last (sentence) = %d\n", (int32_t)pos); 301 ubrk_first(sentence); 302 to = ubrk_following( sentence, 0 ); 303 if (to == 0) log_err("ubrk_following returned 0\n"); 304 to = ubrk_preceding( sentence, to ); 305 if (to != 0) log_err("ubrk_preceding didn't return 0\n"); 306 if (ubrk_first(sentence)!=ubrk_current(sentence)) { 307 log_err("error in ubrk_first() or ubrk_current()\n"); 308 } 309 310 311 /*---- */ 312 /*Testing ubrk_open and ubrk_close()*/ 313 log_verbose("\nTesting open and close for us locale\n"); 314 b = ubrk_open(UBRK_WORD, "fr_FR", text, u_strlen(text), &status); 315 if (U_FAILURE(status)) { 316 log_err("ubrk_open for word returned NULL: %s\n", myErrorName(status)); 317 } 318 ubrk_close(b); 319 320 /* Test setText and setUText */ 321 { 322 UChar s1[] = {0x41, 0x42, 0x20, 0}; 323 UChar s2[] = {0x41, 0x42, 0x43, 0x44, 0x45, 0}; 324 UText *ut = NULL; 325 UBreakIterator *bb; 326 int j; 327 328 log_verbose("\nTesting ubrk_setText() and ubrk_setUText()\n"); 329 status = U_ZERO_ERROR; 330 bb = ubrk_open(UBRK_WORD, "en_US", NULL, 0, &status); 331 TEST_ASSERT_SUCCESS(status); 332 ubrk_setText(bb, s1, -1, &status); 333 TEST_ASSERT_SUCCESS(status); 334 ubrk_first(bb); 335 j = ubrk_next(bb); 336 TEST_ASSERT(j == 2); 337 ut = utext_openUChars(ut, s2, -1, &status); 338 ubrk_setUText(bb, ut, &status); 339 TEST_ASSERT_SUCCESS(status); 340 j = ubrk_next(bb); 341 TEST_ASSERT(j == 5); 342 343 ubrk_close(bb); 344 utext_close(ut); 345 } 346 347 ubrk_close(word); 348 ubrk_close(sentence); 349 ubrk_close(line); 350 ubrk_close(character); 351 } 352 353 static void TestBreakIteratorSafeClone(void) 354 { 355 UChar text[51]; /* Keep this odd to test for 64-bit memory alignment */ 356 /* NOTE: This doesn't reliably force mis-alignment of following items. */ 357 uint8_t buffer [CLONETEST_ITERATOR_COUNT] [U_BRK_SAFECLONE_BUFFERSIZE]; 358 int32_t bufferSize = U_BRK_SAFECLONE_BUFFERSIZE; 359 360 UBreakIterator * someIterators [CLONETEST_ITERATOR_COUNT]; 361 UBreakIterator * someClonedIterators [CLONETEST_ITERATOR_COUNT]; 362 363 UBreakIterator * brk; 364 UErrorCode status = U_ZERO_ERROR; 365 int32_t start,pos; 366 int32_t i; 367 368 /*Testing ubrk_safeClone */ 369 370 /* Note: the adjacent "" are concatenating strings, not adding a \" to the 371 string, which is probably what whoever wrote this intended. Don't fix, 372 because it would throw off the hard coded break positions in the following 373 tests. */ 374 u_uastrcpy(text, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah"); 375 376 /* US & Thai - rule-based & dictionary based */ 377 someIterators[0] = ubrk_open(UBRK_WORD, "en_US", text, u_strlen(text), &status); 378 if(!someIterators[0] || U_FAILURE(status)) { 379 log_data_err("Couldn't open en_US word break iterator - %s\n", u_errorName(status)); 380 return; 381 } 382 383 someIterators[1] = ubrk_open(UBRK_WORD, "th_TH", text, u_strlen(text), &status); 384 if(!someIterators[1] || U_FAILURE(status)) { 385 log_data_err("Couldn't open th_TH word break iterator - %s\n", u_errorName(status)); 386 return; 387 } 388 389 /* test each type of iterator */ 390 for (i = 0; i < CLONETEST_ITERATOR_COUNT; i++) 391 { 392 393 /* Check the various error & informational states */ 394 395 /* Null status - just returns NULL */ 396 if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, 0)) 397 { 398 log_err("FAIL: Cloned Iterator failed to deal correctly with null status\n"); 399 } 400 /* error status - should return 0 & keep error the same */ 401 status = U_MEMORY_ALLOCATION_ERROR; 402 if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status) || status != U_MEMORY_ALLOCATION_ERROR) 403 { 404 log_err("FAIL: Cloned Iterator failed to deal correctly with incoming error status\n"); 405 } 406 status = U_ZERO_ERROR; 407 408 /* Null buffer size pointer - just returns NULL & set error to U_ILLEGAL_ARGUMENT_ERROR*/ 409 if (0 != ubrk_safeClone(someIterators[i], buffer[i], 0, &status) || status != U_ILLEGAL_ARGUMENT_ERROR) 410 { 411 log_err("FAIL: Cloned Iterator failed to deal correctly with null bufferSize pointer\n"); 412 } 413 status = U_ZERO_ERROR; 414 415 /* buffer size pointer is 0 - fill in pbufferSize with a size */ 416 bufferSize = 0; 417 if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status) || U_FAILURE(status) || bufferSize <= 0) 418 { 419 log_err("FAIL: Cloned Iterator failed a sizing request ('preflighting')\n"); 420 } 421 /* Verify our define is large enough */ 422 if (U_BRK_SAFECLONE_BUFFERSIZE < bufferSize) 423 { 424 log_err("FAIL: Pre-calculated buffer size is too small - %d but needed %d\n", U_BRK_SAFECLONE_BUFFERSIZE, bufferSize); 425 } 426 /* Verify we can use this run-time calculated size */ 427 if (0 == (brk = ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status)) || U_FAILURE(status)) 428 { 429 log_err("FAIL: Iterator can't be cloned with run-time size\n"); 430 } 431 if (brk) 432 ubrk_close(brk); 433 /* size one byte too small - should allocate & let us know */ 434 --bufferSize; 435 if (0 == (brk = ubrk_safeClone(someIterators[i], 0, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_WARNING) 436 { 437 log_err("FAIL: Cloned Iterator failed to deal correctly with too-small buffer size\n"); 438 } 439 if (brk) 440 ubrk_close(brk); 441 status = U_ZERO_ERROR; 442 bufferSize = U_BRK_SAFECLONE_BUFFERSIZE; 443 444 /* Null buffer pointer - return Iterator & set error to U_SAFECLONE_ALLOCATED_ERROR */ 445 if (0 == (brk = ubrk_safeClone(someIterators[i], 0, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_WARNING) 446 { 447 log_err("FAIL: Cloned Iterator failed to deal correctly with null buffer pointer\n"); 448 } 449 if (brk) 450 ubrk_close(brk); 451 status = U_ZERO_ERROR; 452 453 /* Mis-aligned buffer pointer. */ 454 { 455 char stackBuf[U_BRK_SAFECLONE_BUFFERSIZE+sizeof(void *)]; 456 void *p; 457 int32_t offset; 458 459 brk = ubrk_safeClone(someIterators[i], &stackBuf[1], &bufferSize, &status); 460 if (U_FAILURE(status) || brk == 0) { 461 log_err("FAIL: Cloned Iterator failed with misaligned buffer pointer\n"); 462 } 463 if (status == U_SAFECLONE_ALLOCATED_WARNING) { 464 log_err("FAIL: Cloned Iterator allocated when using a mis-aligned buffer.\n"); 465 } 466 offset = (int32_t)((char *)&p-(char*)brk); 467 if (offset < 0) { 468 offset = -offset; 469 } 470 if (offset % sizeof(void *) != 0) { 471 log_err("FAIL: Cloned Iterator failed to align correctly with misaligned buffer pointer\n"); 472 } 473 if (brk) 474 ubrk_close(brk); 475 } 476 477 478 /* Null Iterator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */ 479 if (0 != ubrk_safeClone(0, buffer[i], &bufferSize, &status) || status != U_ILLEGAL_ARGUMENT_ERROR) 480 { 481 log_err("FAIL: Cloned Iterator failed to deal correctly with null Iterator pointer\n"); 482 } 483 status = U_ZERO_ERROR; 484 485 /* Do these cloned Iterators work at all - make a first & next call */ 486 bufferSize = U_BRK_SAFECLONE_BUFFERSIZE; 487 someClonedIterators[i] = ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status); 488 489 start = ubrk_first(someClonedIterators[i]); 490 if(start!=0) 491 log_err("error ubrk_start(clone) did not return 0\n"); 492 pos=ubrk_next(someClonedIterators[i]); 493 if(pos!=4) 494 log_err("error ubrk_next(clone) did not return 4\n"); 495 496 ubrk_close(someClonedIterators[i]); 497 ubrk_close(someIterators[i]); 498 } 499 } 500 501 502 /* 503 // Open a break iterator from char * rules. Take care of conversion 504 // of the rules and error checking. 505 */ 506 static UBreakIterator * testOpenRules(char *rules) { 507 UErrorCode status = U_ZERO_ERROR; 508 UChar *ruleSourceU = NULL; 509 void *strCleanUp = NULL; 510 UParseError parseErr; 511 UBreakIterator *bi; 512 513 ruleSourceU = toUChar(rules, &strCleanUp); 514 515 bi = ubrk_openRules(ruleSourceU, -1, /* The rules */ 516 NULL, -1, /* The text to be iterated over. */ 517 &parseErr, &status); 518 519 if (U_FAILURE(status)) { 520 log_data_err("FAIL: ubrk_openRules: ICU Error \"%s\" (Are you missing data?)\n", u_errorName(status)); 521 bi = 0; 522 }; 523 freeToUCharStrings(&strCleanUp); 524 return bi; 525 526 } 527 528 /* 529 * TestBreakIteratorRules - Verify that a break iterator can be created from 530 * a set of source rules. 531 */ 532 static void TestBreakIteratorRules() { 533 /* Rules will keep together any run of letters not including 'a', OR 534 * keep together 'abc', but only when followed by 'def', OTHERWISE 535 * just return one char at a time. 536 */ 537 char rules[] = "abc{666}/def;\n [\\p{L} - [a]]* {2}; . {1};"; 538 /* 0123456789012345678 */ 539 char data[] = "abcdex abcdefgh-def"; /* the test data string */ 540 char breaks[] = "** ** * ** *"; /* * the expected break positions */ 541 char tags[] = "01 21 6 21 2"; /* expected tag values at break positions */ 542 int32_t tagMap[] = {0, 1, 2, 3, 4, 5, 666}; 543 544 UChar *uData; 545 void *freeHook = NULL; 546 UErrorCode status = U_ZERO_ERROR; 547 int32_t pos; 548 int i; 549 550 UBreakIterator *bi = testOpenRules(rules); 551 if (bi == NULL) {return;} 552 uData = toUChar(data, &freeHook); 553 ubrk_setText(bi, uData, -1, &status); 554 555 pos = ubrk_first(bi); 556 for (i=0; i<sizeof(breaks); i++) { 557 if (pos == i && breaks[i] != '*') { 558 log_err("FAIL: unexpected break at position %d found\n", pos); 559 break; 560 } 561 if (pos != i && breaks[i] == '*') { 562 log_err("FAIL: expected break at position %d not found.\n", i); 563 break; 564 } 565 if (pos == i) { 566 int32_t tag, expectedTag; 567 tag = ubrk_getRuleStatus(bi); 568 expectedTag = tagMap[tags[i]&0xf]; 569 if (tag != expectedTag) { 570 log_err("FAIL: incorrect tag value. Position = %d; expected tag %d, got %d", 571 pos, expectedTag, tag); 572 break; 573 } 574 pos = ubrk_next(bi); 575 } 576 } 577 578 freeToUCharStrings(&freeHook); 579 ubrk_close(bi); 580 } 581 582 static void TestBreakIteratorRuleError() { 583 /* 584 * TestBreakIteratorRuleError - Try to create a BI from rules with syntax errors, 585 * check that the error is reported correctly. 586 */ 587 char rules[] = " # This is a rule comment on line 1\n" 588 "[:L:]; # this rule is OK.\n" 589 "abcdefg); # Error, mismatched parens\n"; 590 UChar *uRules; 591 void *freeHook = NULL; 592 UErrorCode status = U_ZERO_ERROR; 593 UParseError parseErr; 594 UBreakIterator *bi; 595 596 uRules = toUChar(rules, &freeHook); 597 bi = ubrk_openRules(uRules, -1, /* The rules */ 598 NULL, -1, /* The text to be iterated over. */ 599 &parseErr, &status); 600 if (U_SUCCESS(status)) { 601 log_err("FAIL: construction of break iterator succeeded when it should have failed.\n"); 602 ubrk_close(bi); 603 } else { 604 if (parseErr.line != 3 || parseErr.offset != 8) { 605 log_data_err("FAIL: incorrect error position reported. Got line %d, char %d, expected line 3, char 7 (Are you missing data?)\n", 606 parseErr.line, parseErr.offset); 607 } 608 } 609 freeToUCharStrings(&freeHook); 610 } 611 612 613 /* 614 * TestsBreakIteratorStatusVals() Test the ubrk_getRuleStatusVec() funciton 615 */ 616 static void TestBreakIteratorStatusVec() { 617 #define RULE_STRING_LENGTH 200 618 UChar rules[RULE_STRING_LENGTH]; 619 620 #define TEST_STRING_LENGTH 25 621 UChar testString[TEST_STRING_LENGTH]; 622 UBreakIterator *bi = NULL; 623 int32_t pos = 0; 624 int32_t vals[10]; 625 int32_t numVals; 626 UErrorCode status = U_ZERO_ERROR; 627 628 u_uastrncpy(rules, "[A-N]{100}; \n" 629 "[a-w]{200}; \n" 630 "[\\p{L}]{300}; \n" 631 "[\\p{N}]{400}; \n" 632 "[0-5]{500}; \n" 633 "!.*;\n", RULE_STRING_LENGTH); 634 u_uastrncpy(testString, "ABC", TEST_STRING_LENGTH); 635 636 637 bi = ubrk_openRules(rules, -1, testString, -1, NULL, &status); 638 TEST_ASSERT_SUCCESS(status); 639 TEST_ASSERT(bi != NULL); 640 641 /* The TEST_ASSERT above should change too... */ 642 if (bi != NULL) { 643 pos = ubrk_next(bi); 644 TEST_ASSERT(pos == 1); 645 646 memset(vals, -1, sizeof(vals)); 647 numVals = ubrk_getRuleStatusVec(bi, vals, 10, &status); 648 TEST_ASSERT_SUCCESS(status); 649 TEST_ASSERT(numVals == 2); 650 TEST_ASSERT(vals[0] == 100); 651 TEST_ASSERT(vals[1] == 300); 652 TEST_ASSERT(vals[2] == -1); 653 654 numVals = ubrk_getRuleStatusVec(bi, vals, 0, &status); 655 TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR); 656 TEST_ASSERT(numVals == 2); 657 } 658 659 ubrk_close(bi); 660 } 661 662 663 /* 664 * static void TestBreakIteratorUText(void); 665 * 666 * Test that ubrk_setUText() is present and works for a simple case. 667 */ 668 static void TestBreakIteratorUText(void) { 669 const char *UTF8Str = "\x41\xc3\x85\x5A\x20\x41\x52\x69\x6E\x67"; /* c3 85 is utf-8 for A with a ring on top */ 670 /* 0 1 2 34567890 */ 671 672 UErrorCode status = U_ZERO_ERROR; 673 UBreakIterator *bi = NULL; 674 int32_t pos = 0; 675 676 677 UText *ut = utext_openUTF8(NULL, UTF8Str, -1, &status); 678 TEST_ASSERT_SUCCESS(status); 679 680 bi = ubrk_open(UBRK_WORD, "en_US", NULL, 0, &status); 681 if (U_FAILURE(status)) { 682 log_err_status(status, "Failure at file %s, line %d, error = %s\n", __FILE__, __LINE__, u_errorName(status)); 683 return; 684 } 685 686 ubrk_setUText(bi, ut, &status); 687 if (U_FAILURE(status)) { 688 log_err("Failure at file %s, line %d, error = %s\n", __FILE__, __LINE__, u_errorName(status)); 689 return; 690 } 691 692 pos = ubrk_first(bi); 693 TEST_ASSERT(pos == 0); 694 695 pos = ubrk_next(bi); 696 TEST_ASSERT(pos == 4); 697 698 pos = ubrk_next(bi); 699 TEST_ASSERT(pos == 5); 700 701 pos = ubrk_next(bi); 702 TEST_ASSERT(pos == 10); 703 704 pos = ubrk_next(bi); 705 TEST_ASSERT(pos == UBRK_DONE); 706 ubrk_close(bi); 707 utext_close(ut); 708 } 709 710 /* 711 * static void TestBreakIteratorTailoring(void); 712 * 713 * Test break iterator tailorings from CLDR data. 714 */ 715 716 /* Thai/Lao grapheme break tailoring */ 717 static const UChar thTest[] = { 0x0020, 0x0E40, 0x0E01, 0x0020, 718 0x0E01, 0x0E30, 0x0020, 0x0E01, 0x0E33, 0x0020, 0 }; 719 static const int32_t thTestOffs_enFwd[] = { 1, 3, 4, 6, 7, 9, 10 }; 720 static const int32_t thTestOffs_thFwd[] = { 1, 2, 3, 4, 5, 6, 7, 9, 10 }; 721 static const int32_t thTestOffs_enRev[] = { 9, 7, 6, 4, 3, 1, 0 }; 722 static const int32_t thTestOffs_thRev[] = { 9, 7, 6, 5, 4, 3, 2, 1, 0 }; 723 724 /* Hebrew line break tailoring, for cldrbug 3028 */ 725 static const UChar heTest[] = { 0x0020, 0x002D, 0x0031, 0x0032, 0x0020, 726 0x0061, 0x002D, 0x006B, 0x0020, 727 0x0061, 0x0300, 0x2010, 0x006B, 0x0020, 728 0x05DE, 0x05D4, 0x002D, 0x0069, 0x0020, 729 0x05D1, 0x05BC, 0x2010, 0x0047, 0x0020, 0 }; 730 static const int32_t heTestOffs_enFwd[] = { 1, 5, 7, 9, 12, 14, 17, 19, 22, 24 }; 731 static const int32_t heTestOffs_heFwd[] = { 1, 5, 7, 9, 12, 14, 19, 24 }; 732 static const int32_t heTestOffs_enRev[] = { 22, 19, 17, 14, 12, 9, 7, 5, 1, 0 }; 733 static const int32_t heTestOffs_heRev[] = { 19, 14, 12, 9, 7, 5, 1, 0 }; 734 735 /* Finnish line break tailoring, for cldrbug 3029 */ 736 static const UChar fiTest[] = { /* 00 */ 0x0020, 0x002D, 0x0031, 0x0032, 0x0020, 737 /* 05 */ 0x0061, 0x002D, 0x006B, 0x0020, 738 /* 09 */ 0x0061, 0x0300, 0x2010, 0x006B, 0x0020, 739 /* 14 */ 0x0061, 0x0020, 0x002D, 0x006B, 0x0020, 740 /* 19 */ 0x0061, 0x0300, 0x0020, 0x2010, 0x006B, 0x0020, 0 }; 741 static const int32_t fiTestOffs_enFwd[] = { 1, 5, 7, 9, 12, 14, 16, 17, 19, 22, 23, 25 }; 742 static const int32_t fiTestOffs_fiFwd[] = { 1, 5, 7, 9, 12, 14, 16, 19, 22, 25 }; 743 static const int32_t fiTestOffs_enRev[] = { 23, 22, 19, 17, 16, 14, 12, 9, 7, 5, 1, 0 }; 744 static const int32_t fiTestOffs_fiRev[] = { 22, 19, 16, 14, 12, 9, 7, 5, 1, 0 }; 745 746 /* Khmer dictionary-based work break, for ICU ticket #8329 */ 747 static const UChar kmTest[] = { /* 00 */ 0x179F, 0x17BC, 0x1798, 0x1785, 0x17C6, 0x178E, 0x17B6, 0x1799, 0x1796, 0x17C1, 748 /* 10 */ 0x179B, 0x1794, 0x1793, 0x17D2, 0x178F, 0x17B7, 0x1785, 0x178A, 0x17BE, 0x1798, 749 /* 20 */ 0x17D2, 0x1794, 0x17B8, 0x17A2, 0x1792, 0x17B7, 0x179F, 0x17D2, 0x178B, 0x17B6, 750 /* 30 */ 0x1793, 0x17A2, 0x179A, 0x1796, 0x17D2, 0x179A, 0x17C7, 0x1782, 0x17BB, 0x178E, 751 /* 40 */ 0x178A, 0x179B, 0x17CB, 0x1796, 0x17D2, 0x179A, 0x17C7, 0x17A2, 0x1784, 0x17D2, 752 /* 50 */ 0x1782, 0 }; 753 static const int32_t kmTestOffs_kmFwd[] = { 3, /*8,*/ 11, 17, 23, 31, /*33,*/ 40, 43, 51 }; /* TODO: Investigate failure to break at offset 8 */ 754 static const int32_t kmTestOffs_kmRev[] = { 43, 40, /*33,*/ 31, 23, 17, 11, /*8,*/ 3, 0 }; 755 756 typedef struct { 757 const char * locale; 758 UBreakIteratorType type; 759 const UChar * test; 760 const int32_t * offsFwd; 761 const int32_t * offsRev; 762 int32_t numOffsets; 763 } RBBITailoringTest; 764 765 static const RBBITailoringTest tailoringTests[] = { 766 { "en", UBRK_CHARACTER, thTest, thTestOffs_enFwd, thTestOffs_enRev, sizeof(thTestOffs_enFwd)/sizeof(thTestOffs_enFwd[0]) }, 767 { "th", UBRK_CHARACTER, thTest, thTestOffs_thFwd, thTestOffs_thRev, sizeof(thTestOffs_thFwd)/sizeof(thTestOffs_thFwd[0]) }, 768 { "en", UBRK_LINE, heTest, heTestOffs_enFwd, heTestOffs_enRev, sizeof(heTestOffs_enFwd)/sizeof(heTestOffs_enFwd[0]) }, 769 { "he", UBRK_LINE, heTest, heTestOffs_heFwd, heTestOffs_heRev, sizeof(heTestOffs_heFwd)/sizeof(heTestOffs_heFwd[0]) }, 770 { "en", UBRK_LINE, fiTest, fiTestOffs_enFwd, fiTestOffs_enRev, sizeof(fiTestOffs_enFwd)/sizeof(fiTestOffs_enFwd[0]) }, 771 { "fi", UBRK_LINE, fiTest, fiTestOffs_fiFwd, fiTestOffs_fiRev, sizeof(fiTestOffs_fiFwd)/sizeof(fiTestOffs_fiFwd[0]) }, 772 { "km", UBRK_WORD, kmTest, kmTestOffs_kmFwd, kmTestOffs_kmRev, sizeof(kmTestOffs_kmFwd)/sizeof(kmTestOffs_kmFwd[0]) }, 773 { NULL, 0, NULL, NULL, NULL, 0 }, 774 }; 775 776 static void TestBreakIteratorTailoring(void) { 777 const RBBITailoringTest * testPtr; 778 for (testPtr = tailoringTests; testPtr->locale != NULL; ++testPtr) { 779 UErrorCode status = U_ZERO_ERROR; 780 UBreakIterator* ubrkiter = ubrk_open(testPtr->type, testPtr->locale, testPtr->test, -1, &status); 781 if ( U_SUCCESS(status) ) { 782 int32_t offset, offsindx; 783 UBool foundError; 784 785 foundError = FALSE; 786 for (offsindx = 0; (offset = ubrk_next(ubrkiter)) != UBRK_DONE; ++offsindx) { 787 if (!foundError && offsindx >= testPtr->numOffsets) { 788 log_err("FAIL: locale %s, break type %d, ubrk_next expected UBRK_DONE, got %d\n", 789 testPtr->locale, testPtr->type, offset); 790 foundError = TRUE; 791 } else if (!foundError && offset != testPtr->offsFwd[offsindx]) { 792 log_err("FAIL: locale %s, break type %d, ubrk_next expected %d, got %d\n", 793 testPtr->locale, testPtr->type, testPtr->offsFwd[offsindx], offset); 794 foundError = TRUE; 795 } 796 } 797 if (!foundError && offsindx < testPtr->numOffsets) { 798 log_err("FAIL: locale %s, break type %d, ubrk_next expected %d, got UBRK_DONE\n", 799 testPtr->locale, testPtr->type, testPtr->offsFwd[offsindx]); 800 } 801 802 foundError = FALSE; 803 for (offsindx = 0; (offset = ubrk_previous(ubrkiter)) != UBRK_DONE; ++offsindx) { 804 if (!foundError && offsindx >= testPtr->numOffsets) { 805 log_err("FAIL: locale %s, break type %d, ubrk_previous expected UBRK_DONE, got %d\n", 806 testPtr->locale, testPtr->type, offset); 807 foundError = TRUE; 808 } else if (!foundError && offset != testPtr->offsRev[offsindx]) { 809 log_err("FAIL: locale %s, break type %d, ubrk_previous expected %d, got %d\n", 810 testPtr->locale, testPtr->type, testPtr->offsRev[offsindx], offset); 811 foundError = TRUE; 812 } 813 } 814 if (!foundError && offsindx < testPtr->numOffsets) { 815 log_err("FAIL: locale %s, break type %d, ubrk_previous expected %d, got UBRK_DONE\n", 816 testPtr->locale, testPtr->type, testPtr->offsRev[offsindx]); 817 } 818 819 ubrk_close(ubrkiter); 820 } else { 821 log_err_status(status, "FAIL: locale %s, break type %d, ubrk_open status: %s\n", testPtr->locale, testPtr->type, u_errorName(status)); 822 } 823 } 824 } 825 826 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */ 827