1// © 2016 and later: Unicode, Inc. and others. 2// License & terms of use: http://www.unicode.org/copyright.html 3/******************************************************************** 4 * COPYRIGHT: 5 * Copyright (c) 1997-2016, International Business Machines Corporation and 6 * others. All Rights Reserved. 7 ********************************************************************/ 8/******************************************************************************** 9* 10* File CBIAPTS.C 11* 12* Modification History: 13* Name Description 14* Madhu Katragadda Creation 15*********************************************************************************/ 16/*C API TEST FOR BREAKITERATOR */ 17/** 18* This is an API test. It doesn't test very many cases, and doesn't 19* try to test the full functionality. It just calls each function in the class and 20* verifies that it works on a basic level. 21**/ 22 23#include "unicode/utypes.h" 24 25#if !UCONFIG_NO_BREAK_ITERATION 26 27#include <stdlib.h> 28#include <string.h> 29#include "unicode/uloc.h" 30#include "unicode/ubrk.h" 31#include "unicode/ustring.h" 32#include "unicode/ucnv.h" 33#include "unicode/utext.h" 34#include "cintltst.h" 35#include "cbiapts.h" 36#include "cmemory.h" 37 38#define TEST_ASSERT_SUCCESS(status) {if (U_FAILURE(status)) { \ 39log_data_err("Failure at file %s, line %d, error = %s (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status));}} 40 41#define TEST_ASSERT(expr) {if ((expr)==FALSE) { \ 42log_data_err("Test Failure at file %s, line %d (Are you missing data?)\n", __FILE__, __LINE__);}} 43 44#if !UCONFIG_NO_FILE_IO 45static void TestBreakIteratorSafeClone(void); 46#endif 47static void TestBreakIteratorRules(void); 48static void TestBreakIteratorRuleError(void); 49static void TestBreakIteratorStatusVec(void); 50static void TestBreakIteratorUText(void); 51static void TestBreakIteratorTailoring(void); 52static void TestBreakIteratorRefresh(void); 53static void TestBug11665(void); 54static void TestBreakIteratorSuppressions(void); 55 56void addBrkIterAPITest(TestNode** root); 57 58void addBrkIterAPITest(TestNode** root) 59{ 60#if !UCONFIG_NO_FILE_IO 61 addTest(root, &TestBreakIteratorCAPI, "tstxtbd/cbiapts/TestBreakIteratorCAPI"); 62 addTest(root, &TestBreakIteratorSafeClone, "tstxtbd/cbiapts/TestBreakIteratorSafeClone"); 63 addTest(root, &TestBreakIteratorUText, "tstxtbd/cbiapts/TestBreakIteratorUText"); 64#endif 65 addTest(root, &TestBreakIteratorRules, "tstxtbd/cbiapts/TestBreakIteratorRules"); 66 addTest(root, &TestBreakIteratorRuleError, "tstxtbd/cbiapts/TestBreakIteratorRuleError"); 67 addTest(root, &TestBreakIteratorStatusVec, "tstxtbd/cbiapts/TestBreakIteratorStatusVec"); 68 addTest(root, &TestBreakIteratorTailoring, "tstxtbd/cbiapts/TestBreakIteratorTailoring"); 69 addTest(root, &TestBreakIteratorRefresh, "tstxtbd/cbiapts/TestBreakIteratorRefresh"); 70 addTest(root, &TestBug11665, "tstxtbd/cbiapts/TestBug11665"); 71#if !UCONFIG_NO_FILTERED_BREAK_ITERATION 72 addTest(root, &TestBreakIteratorSuppressions, "tstxtbd/cbiapts/TestBreakIteratorSuppressions"); 73#endif 74} 75 76#define CLONETEST_ITERATOR_COUNT 2 77 78/* 79 * Utility function for converting char * to UChar * strings, to 80 * simplify the test code. Converted strings are put in heap allocated 81 * storage. A hook (probably a local in the caller's code) allows all 82 * strings converted with that hook to be freed with a single call. 83 */ 84typedef struct StringStruct { 85 struct StringStruct *link; 86 UChar str[1]; 87 } StringStruct; 88 89 90static UChar* toUChar(const char *src, void **freeHook) { 91 /* Structure of the memory that we allocate on the heap */ 92 93 int32_t numUChars; 94 int32_t destSize; 95 UChar stackBuf[2000 + sizeof(void *)/sizeof(UChar)]; 96 StringStruct *dest; 97 UConverter *cnv; 98 99 UErrorCode status = U_ZERO_ERROR; 100 if (src == NULL) { 101 return NULL; 102 }; 103 104 cnv = ucnv_open(NULL, &status); 105 if(U_FAILURE(status) || cnv == NULL) { 106 return NULL; 107 } 108 ucnv_reset(cnv); 109 numUChars = ucnv_toUChars(cnv, 110 stackBuf, 111 2000, 112 src, -1, 113 &status); 114 115 destSize = (numUChars+1) * sizeof(UChar) + sizeof(struct StringStruct); 116 dest = (StringStruct *)malloc(destSize); 117 if (dest != NULL) { 118 if (status == U_BUFFER_OVERFLOW_ERROR || status == U_STRING_NOT_TERMINATED_WARNING) { 119 ucnv_toUChars(cnv, dest->str, numUChars+1, src, -1, &status); 120 } else if (status == U_ZERO_ERROR) { 121 u_strcpy(dest->str, stackBuf); 122 } else { 123 free(dest); 124 dest = NULL; 125 } 126 } 127 128 ucnv_reset(cnv); /* be good citizens */ 129 ucnv_close(cnv); 130 if (dest == NULL) { 131 return NULL; 132 } 133 134 dest->link = (StringStruct*)(*freeHook); 135 *freeHook = dest; 136 return dest->str; 137} 138 139static void freeToUCharStrings(void **hook) { 140 StringStruct *s = *(StringStruct **)hook; 141 while (s != NULL) { 142 StringStruct *next = s->link; 143 free(s); 144 s = next; 145 } 146} 147 148 149#if !UCONFIG_NO_FILE_IO 150static void TestBreakIteratorCAPI() 151{ 152 UErrorCode status = U_ZERO_ERROR; 153 UBreakIterator *word, *sentence, *line, *character, *b, *bogus; 154 int32_t start,pos,end,to; 155 int32_t i; 156 int32_t count = 0; 157 158 UChar text[50]; 159 160 /* Note: the adjacent "" are concatenating strings, not adding a \" to the 161 string, which is probably what whoever wrote this intended. Don't fix, 162 because it would throw off the hard coded break positions in the following 163 tests. */ 164 u_uastrcpy(text, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah"); 165 166 167/*test ubrk_open()*/ 168 log_verbose("\nTesting BreakIterator open functions\n"); 169 170 /* Use french for fun */ 171 word = ubrk_open(UBRK_WORD, "en_US", text, u_strlen(text), &status); 172 if(status == U_FILE_ACCESS_ERROR) { 173 log_data_err("Check your data - it doesn't seem to be around\n"); 174 return; 175 } else if(U_FAILURE(status)){ 176 log_err_status(status, "FAIL: Error in ubrk_open() for word breakiterator: %s\n", myErrorName(status)); 177 } 178 else{ 179 log_verbose("PASS: Successfully opened word breakiterator\n"); 180 } 181 182 sentence = ubrk_open(UBRK_SENTENCE, "en_US", text, u_strlen(text), &status); 183 if(U_FAILURE(status)){ 184 log_err_status(status, "FAIL: Error in ubrk_open() for sentence breakiterator: %s\n", myErrorName(status)); 185 return; 186 } 187 else{ 188 log_verbose("PASS: Successfully opened sentence breakiterator\n"); 189 } 190 191 line = ubrk_open(UBRK_LINE, "en_US", text, u_strlen(text), &status); 192 if(U_FAILURE(status)){ 193 log_err("FAIL: Error in ubrk_open() for line breakiterator: %s\n", myErrorName(status)); 194 return; 195 } 196 else{ 197 log_verbose("PASS: Successfully opened line breakiterator\n"); 198 } 199 200 character = ubrk_open(UBRK_CHARACTER, "en_US", text, u_strlen(text), &status); 201 if(U_FAILURE(status)){ 202 log_err("FAIL: Error in ubrk_open() for character breakiterator: %s\n", myErrorName(status)); 203 return; 204 } 205 else{ 206 log_verbose("PASS: Successfully opened character breakiterator\n"); 207 } 208 /*trying to open an illegal iterator*/ 209 bogus = ubrk_open((UBreakIteratorType)5, "en_US", text, u_strlen(text), &status); 210 if(bogus != NULL) { 211 log_err("FAIL: expected NULL from opening an invalid break iterator.\n"); 212 } 213 if(U_SUCCESS(status)){ 214 log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_ILLEGAL_ARGUMENT_ERROR\n"); 215 } 216 if(U_FAILURE(status)){ 217 if(status != U_ILLEGAL_ARGUMENT_ERROR){ 218 log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_ILLEGAL_ARGUMENT_ERROR\n Got %s\n", myErrorName(status)); 219 } 220 } 221 status=U_ZERO_ERROR; 222 223 224/* ======= Test ubrk_countAvialable() and ubrk_getAvialable() */ 225 226 log_verbose("\nTesting ubrk_countAvailable() and ubrk_getAvailable()\n"); 227 count=ubrk_countAvailable(); 228 /* use something sensible w/o hardcoding the count */ 229 if(count < 0){ 230 log_err("FAIL: Error in ubrk_countAvialable() returned %d\n", count); 231 } 232 else{ 233 log_verbose("PASS: ubrk_countAvialable() successful returned %d\n", count); 234 } 235 for(i=0;i<count;i++) 236 { 237 log_verbose("%s\n", ubrk_getAvailable(i)); 238 if (ubrk_getAvailable(i) == 0) 239 log_err("No locale for which breakiterator is applicable\n"); 240 else 241 log_verbose("A locale %s for which breakiterator is applicable\n",ubrk_getAvailable(i)); 242 } 243 244/*========Test ubrk_first(), ubrk_last()...... and other functions*/ 245 246 log_verbose("\nTesting the functions for word\n"); 247 start = ubrk_first(word); 248 if(start!=0) 249 log_err("error ubrk_start(word) did not return 0\n"); 250 log_verbose("first (word = %d\n", (int32_t)start); 251 pos=ubrk_next(word); 252 if(pos!=4) 253 log_err("error ubrk_next(word) did not return 4\n"); 254 log_verbose("next (word = %d\n", (int32_t)pos); 255 pos=ubrk_following(word, 4); 256 if(pos!=5) 257 log_err("error ubrl_following(word,4) did not return 6\n"); 258 log_verbose("next (word = %d\n", (int32_t)pos); 259 end=ubrk_last(word); 260 if(end!=49) 261 log_err("error ubrk_last(word) did not return 49\n"); 262 log_verbose("last (word = %d\n", (int32_t)end); 263 264 pos=ubrk_previous(word); 265 log_verbose("%d %d\n", end, pos); 266 267 pos=ubrk_previous(word); 268 log_verbose("%d \n", pos); 269 270 if (ubrk_isBoundary(word, 2) != FALSE) { 271 log_err("error ubrk_isBoundary(word, 2) did not return FALSE\n"); 272 } 273 pos=ubrk_current(word); 274 if (pos != 4) { 275 log_err("error ubrk_current() != 4 after ubrk_isBoundary(word, 2)\n"); 276 } 277 if (ubrk_isBoundary(word, 4) != TRUE) { 278 log_err("error ubrk_isBoundary(word, 4) did not return TRUE\n"); 279 } 280 281 282 283 log_verbose("\nTesting the functions for character\n"); 284 ubrk_first(character); 285 pos = ubrk_following(character, 5); 286 if(pos!=6) 287 log_err("error ubrk_following(character,5) did not return 6\n"); 288 log_verbose("Following (character,5) = %d\n", (int32_t)pos); 289 pos=ubrk_following(character, 18); 290 if(pos!=19) 291 log_err("error ubrk_following(character,18) did not return 19\n"); 292 log_verbose("Followingcharacter,18) = %d\n", (int32_t)pos); 293 pos=ubrk_preceding(character, 22); 294 if(pos!=21) 295 log_err("error ubrk_preceding(character,22) did not return 21\n"); 296 log_verbose("preceding(character,22) = %d\n", (int32_t)pos); 297 298 299 log_verbose("\nTesting the functions for line\n"); 300 pos=ubrk_first(line); 301 if(pos != 0) 302 log_err("error ubrk_first(line) returned %d, expected 0\n", (int32_t)pos); 303 pos = ubrk_next(line); 304 pos=ubrk_following(line, 18); 305 if(pos!=22) 306 log_err("error ubrk_following(line) did not return 22\n"); 307 log_verbose("following (line) = %d\n", (int32_t)pos); 308 309 310 log_verbose("\nTesting the functions for sentence\n"); 311 ubrk_first(sentence); 312 pos = ubrk_current(sentence); 313 log_verbose("Current(sentence) = %d\n", (int32_t)pos); 314 pos = ubrk_last(sentence); 315 if(pos!=49) 316 log_err("error ubrk_last for sentence did not return 49\n"); 317 log_verbose("Last (sentence) = %d\n", (int32_t)pos); 318 ubrk_first(sentence); 319 to = ubrk_following( sentence, 0 ); 320 if (to == 0) log_err("ubrk_following returned 0\n"); 321 to = ubrk_preceding( sentence, to ); 322 if (to != 0) log_err("ubrk_preceding didn't return 0\n"); 323 if (ubrk_first(sentence)!=ubrk_current(sentence)) { 324 log_err("error in ubrk_first() or ubrk_current()\n"); 325 } 326 327 328 /*---- */ 329 /*Testing ubrk_open and ubrk_close()*/ 330 log_verbose("\nTesting open and close for us locale\n"); 331 b = ubrk_open(UBRK_WORD, "fr_FR", text, u_strlen(text), &status); 332 if (U_FAILURE(status)) { 333 log_err("ubrk_open for word returned NULL: %s\n", myErrorName(status)); 334 } 335 ubrk_close(b); 336 337 /* Test setText and setUText */ 338 { 339 UChar s1[] = {0x41, 0x42, 0x20, 0}; 340 UChar s2[] = {0x41, 0x42, 0x43, 0x44, 0x45, 0}; 341 UText *ut = NULL; 342 UBreakIterator *bb; 343 int j; 344 345 log_verbose("\nTesting ubrk_setText() and ubrk_setUText()\n"); 346 status = U_ZERO_ERROR; 347 bb = ubrk_open(UBRK_WORD, "en_US", NULL, 0, &status); 348 TEST_ASSERT_SUCCESS(status); 349 ubrk_setText(bb, s1, -1, &status); 350 TEST_ASSERT_SUCCESS(status); 351 ubrk_first(bb); 352 j = ubrk_next(bb); 353 TEST_ASSERT(j == 2); 354 ut = utext_openUChars(ut, s2, -1, &status); 355 ubrk_setUText(bb, ut, &status); 356 TEST_ASSERT_SUCCESS(status); 357 j = ubrk_next(bb); 358 TEST_ASSERT(j == 5); 359 360 ubrk_close(bb); 361 utext_close(ut); 362 } 363 364 ubrk_close(word); 365 ubrk_close(sentence); 366 ubrk_close(line); 367 ubrk_close(character); 368} 369 370static void TestBreakIteratorSafeClone(void) 371{ 372 UChar text[51]; /* Keep this odd to test for 64-bit memory alignment */ 373 /* NOTE: This doesn't reliably force mis-alignment of following items. */ 374 uint8_t buffer [CLONETEST_ITERATOR_COUNT] [U_BRK_SAFECLONE_BUFFERSIZE]; 375 int32_t bufferSize = U_BRK_SAFECLONE_BUFFERSIZE; 376 377 UBreakIterator * someIterators [CLONETEST_ITERATOR_COUNT]; 378 UBreakIterator * someClonedIterators [CLONETEST_ITERATOR_COUNT]; 379 380 UBreakIterator * brk; 381 UErrorCode status = U_ZERO_ERROR; 382 int32_t start,pos; 383 int32_t i; 384 385 /*Testing ubrk_safeClone */ 386 387 /* Note: the adjacent "" are concatenating strings, not adding a \" to the 388 string, which is probably what whoever wrote this intended. Don't fix, 389 because it would throw off the hard coded break positions in the following 390 tests. */ 391 u_uastrcpy(text, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah"); 392 393 /* US & Thai - rule-based & dictionary based */ 394 someIterators[0] = ubrk_open(UBRK_WORD, "en_US", text, u_strlen(text), &status); 395 if(!someIterators[0] || U_FAILURE(status)) { 396 log_data_err("Couldn't open en_US word break iterator - %s\n", u_errorName(status)); 397 return; 398 } 399 400 someIterators[1] = ubrk_open(UBRK_WORD, "th_TH", text, u_strlen(text), &status); 401 if(!someIterators[1] || U_FAILURE(status)) { 402 log_data_err("Couldn't open th_TH word break iterator - %s\n", u_errorName(status)); 403 return; 404 } 405 406 /* test each type of iterator */ 407 for (i = 0; i < CLONETEST_ITERATOR_COUNT; i++) 408 { 409 410 /* Check the various error & informational states */ 411 412 /* Null status - just returns NULL */ 413 if (NULL != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, NULL)) 414 { 415 log_err("FAIL: Cloned Iterator failed to deal correctly with null status\n"); 416 } 417 /* error status - should return 0 & keep error the same */ 418 status = U_MEMORY_ALLOCATION_ERROR; 419 if (NULL != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status) || status != U_MEMORY_ALLOCATION_ERROR) 420 { 421 log_err("FAIL: Cloned Iterator failed to deal correctly with incoming error status\n"); 422 } 423 status = U_ZERO_ERROR; 424 425 /* Null buffer size pointer is ok */ 426 if (NULL == (brk = ubrk_safeClone(someIterators[i], buffer[i], NULL, &status)) || U_FAILURE(status)) 427 { 428 log_err("FAIL: Cloned Iterator failed to deal correctly with null bufferSize pointer\n"); 429 } 430 ubrk_close(brk); 431 status = U_ZERO_ERROR; 432 433 /* buffer size pointer is 0 - fill in pbufferSize with a size */ 434 bufferSize = 0; 435 if (NULL != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status) || 436 U_FAILURE(status) || bufferSize <= 0) 437 { 438 log_err("FAIL: Cloned Iterator failed a sizing request ('preflighting')\n"); 439 } 440 /* Verify our define is large enough */ 441 if (U_BRK_SAFECLONE_BUFFERSIZE < bufferSize) 442 { 443 log_err("FAIL: Pre-calculated buffer size is too small - %d but needed %d\n", U_BRK_SAFECLONE_BUFFERSIZE, bufferSize); 444 } 445 /* Verify we can use this run-time calculated size */ 446 if (NULL == (brk = ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status)) || U_FAILURE(status)) 447 { 448 log_err("FAIL: Iterator can't be cloned with run-time size\n"); 449 } 450 if (brk) 451 ubrk_close(brk); 452 /* size one byte too small - should allocate & let us know */ 453 if (bufferSize > 1) { 454 --bufferSize; 455 } 456 if (NULL == (brk = ubrk_safeClone(someIterators[i], NULL, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_WARNING) 457 { 458 log_err("FAIL: Cloned Iterator failed to deal correctly with too-small buffer size\n"); 459 } 460 if (brk) 461 ubrk_close(brk); 462 status = U_ZERO_ERROR; 463 bufferSize = U_BRK_SAFECLONE_BUFFERSIZE; 464 465 /* Null buffer pointer - return Iterator & set error to U_SAFECLONE_ALLOCATED_ERROR */ 466 if (NULL == (brk = ubrk_safeClone(someIterators[i], NULL, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_WARNING) 467 { 468 log_err("FAIL: Cloned Iterator failed to deal correctly with null buffer pointer\n"); 469 } 470 if (brk) 471 ubrk_close(brk); 472 status = U_ZERO_ERROR; 473 474 /* Mis-aligned buffer pointer. */ 475 { 476 char stackBuf[U_BRK_SAFECLONE_BUFFERSIZE+sizeof(void *)]; 477 478 brk = ubrk_safeClone(someIterators[i], &stackBuf[1], &bufferSize, &status); 479 if (U_FAILURE(status) || brk == NULL) { 480 log_err("FAIL: Cloned Iterator failed with misaligned buffer pointer\n"); 481 } 482 if (status == U_SAFECLONE_ALLOCATED_WARNING) { 483 log_verbose("Cloned Iterator allocated when using a mis-aligned buffer.\n"); 484 } 485 if (brk) 486 ubrk_close(brk); 487 } 488 489 490 /* Null Iterator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */ 491 if (NULL != ubrk_safeClone(NULL, buffer[i], &bufferSize, &status) || status != U_ILLEGAL_ARGUMENT_ERROR) 492 { 493 log_err("FAIL: Cloned Iterator failed to deal correctly with null Iterator pointer\n"); 494 } 495 status = U_ZERO_ERROR; 496 497 /* Do these cloned Iterators work at all - make a first & next call */ 498 bufferSize = U_BRK_SAFECLONE_BUFFERSIZE; 499 someClonedIterators[i] = ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status); 500 501 start = ubrk_first(someClonedIterators[i]); 502 if(start!=0) 503 log_err("error ubrk_start(clone) did not return 0\n"); 504 pos=ubrk_next(someClonedIterators[i]); 505 if(pos!=4) 506 log_err("error ubrk_next(clone) did not return 4\n"); 507 508 ubrk_close(someClonedIterators[i]); 509 ubrk_close(someIterators[i]); 510 } 511} 512#endif 513 514 515/* 516// Open a break iterator from char * rules. Take care of conversion 517// of the rules and error checking. 518*/ 519static UBreakIterator * testOpenRules(char *rules) { 520 UErrorCode status = U_ZERO_ERROR; 521 UChar *ruleSourceU = NULL; 522 void *strCleanUp = NULL; 523 UParseError parseErr; 524 UBreakIterator *bi; 525 526 ruleSourceU = toUChar(rules, &strCleanUp); 527 528 bi = ubrk_openRules(ruleSourceU, -1, /* The rules */ 529 NULL, -1, /* The text to be iterated over. */ 530 &parseErr, &status); 531 532 if (U_FAILURE(status)) { 533 log_data_err("FAIL: ubrk_openRules: ICU Error \"%s\" (Are you missing data?)\n", u_errorName(status)); 534 bi = 0; 535 }; 536 freeToUCharStrings(&strCleanUp); 537 return bi; 538 539} 540 541/* 542 * TestBreakIteratorRules - Verify that a break iterator can be created from 543 * a set of source rules. 544 */ 545static void TestBreakIteratorRules() { 546 /* Rules will keep together any run of letters not including 'a', OR 547 * keep together 'abc', but only when followed by 'def', OTHERWISE 548 * just return one char at a time. 549 */ 550 char rules[] = "abc/def{666};\n [\\p{L} - [a]]* {2}; . {1};"; 551 /* 0123456789012345678 */ 552 char data[] = "abcdex abcdefgh-def"; /* the test data string */ 553 char breaks[] = "** ** * ** *"; /* * the expected break positions */ 554 char tags[] = "01 21 6 21 2"; /* expected tag values at break positions */ 555 int32_t tagMap[] = {0, 1, 2, 3, 4, 5, 666}; 556 557 UChar *uData; 558 void *freeHook = NULL; 559 UErrorCode status = U_ZERO_ERROR; 560 int32_t pos; 561 int i; 562 563 UBreakIterator *bi = testOpenRules(rules); 564 if (bi == NULL) {return;} 565 uData = toUChar(data, &freeHook); 566 ubrk_setText(bi, uData, -1, &status); 567 568 pos = ubrk_first(bi); 569 for (i=0; i<sizeof(breaks); i++) { 570 if (pos == i && breaks[i] != '*') { 571 log_err("FAIL: unexpected break at position %d found\n", pos); 572 break; 573 } 574 if (pos != i && breaks[i] == '*') { 575 log_err("FAIL: expected break at position %d not found.\n", i); 576 break; 577 } 578 if (pos == i) { 579 int32_t tag, expectedTag; 580 tag = ubrk_getRuleStatus(bi); 581 expectedTag = tagMap[tags[i]&0xf]; 582 if (tag != expectedTag) { 583 log_err("FAIL: incorrect tag value. Position = %d; expected tag %d, got %d", 584 pos, expectedTag, tag); 585 break; 586 } 587 pos = ubrk_next(bi); 588 } 589 } 590 591 /* #12914 add basic sanity test for ubrk_getBinaryRules, ubrk_openBinaryRules */ 592 /* Underlying functionality checked in C++ rbbiapts.cpp TestRoundtripRules */ 593 status = U_ZERO_ERROR; 594 int32_t rulesLength = ubrk_getBinaryRules(bi, NULL, 0, &status); /* preflight */ 595 if (U_FAILURE(status)) { 596 log_err("FAIL: ubrk_getBinaryRules preflight err: %s", u_errorName(status)); 597 } else { 598 uint8_t* binaryRules = (uint8_t*)uprv_malloc(rulesLength); 599 if (binaryRules == NULL) { 600 log_err("FAIL: unable to malloc rules buffer, size %u", rulesLength); 601 } else { 602 rulesLength = ubrk_getBinaryRules(bi, binaryRules, rulesLength, &status); 603 if (U_FAILURE(status)) { 604 log_err("FAIL: ubrk_getBinaryRules err: %s", u_errorName(status)); 605 } else { 606 UBreakIterator* bi2 = ubrk_openBinaryRules(binaryRules, rulesLength, uData, -1, &status); 607 if (U_FAILURE(status)) { 608 log_err("FAIL: ubrk_openBinaryRules err: %s", u_errorName(status)); 609 } else { 610 int32_t maxCount = sizeof(breaks); /* fail-safe test limit */ 611 int32_t pos2 = ubrk_first(bi2); 612 pos = ubrk_first(bi); 613 do { 614 if (pos2 != pos) { 615 log_err("FAIL: interator from ubrk_openBinaryRules does not match original, get pos = %d instead of %d", pos2, pos); 616 } 617 pos2 = ubrk_next(bi2); 618 pos = ubrk_next(bi); 619 } while ((pos != UBRK_DONE || pos2 != UBRK_DONE) && maxCount-- > 0); 620 621 ubrk_close(bi2); 622 } 623 } 624 uprv_free(binaryRules); 625 } 626 } 627 628 freeToUCharStrings(&freeHook); 629 ubrk_close(bi); 630} 631 632static void TestBreakIteratorRuleError() { 633/* 634 * TestBreakIteratorRuleError - Try to create a BI from rules with syntax errors, 635 * check that the error is reported correctly. 636 */ 637 char rules[] = " # This is a rule comment on line 1\n" 638 "[:L:]; # this rule is OK.\n" 639 "abcdefg); # Error, mismatched parens\n"; 640 UChar *uRules; 641 void *freeHook = NULL; 642 UErrorCode status = U_ZERO_ERROR; 643 UParseError parseErr; 644 UBreakIterator *bi; 645 646 uRules = toUChar(rules, &freeHook); 647 bi = ubrk_openRules(uRules, -1, /* The rules */ 648 NULL, -1, /* The text to be iterated over. */ 649 &parseErr, &status); 650 if (U_SUCCESS(status)) { 651 log_err("FAIL: construction of break iterator succeeded when it should have failed.\n"); 652 ubrk_close(bi); 653 } else { 654 if (parseErr.line != 3 || parseErr.offset != 8) { 655 log_data_err("FAIL: incorrect error position reported. Got line %d, char %d, expected line 3, char 7 (Are you missing data?)\n", 656 parseErr.line, parseErr.offset); 657 } 658 } 659 freeToUCharStrings(&freeHook); 660} 661 662 663/* 664* TestsBreakIteratorStatusVals() Test the ubrk_getRuleStatusVec() funciton 665*/ 666static void TestBreakIteratorStatusVec() { 667 #define RULE_STRING_LENGTH 200 668 UChar rules[RULE_STRING_LENGTH]; 669 670 #define TEST_STRING_LENGTH 25 671 UChar testString[TEST_STRING_LENGTH]; 672 UBreakIterator *bi = NULL; 673 int32_t pos = 0; 674 int32_t vals[10]; 675 int32_t numVals; 676 UErrorCode status = U_ZERO_ERROR; 677 678 u_uastrncpy(rules, "[A-N]{100}; \n" 679 "[a-w]{200}; \n" 680 "[\\p{L}]{300}; \n" 681 "[\\p{N}]{400}; \n" 682 "[0-5]{500}; \n" 683 "!.*;\n", RULE_STRING_LENGTH); 684 u_uastrncpy(testString, "ABC", TEST_STRING_LENGTH); 685 686 687 bi = ubrk_openRules(rules, -1, testString, -1, NULL, &status); 688 TEST_ASSERT_SUCCESS(status); 689 TEST_ASSERT(bi != NULL); 690 691 /* The TEST_ASSERT above should change too... */ 692 if (bi != NULL) { 693 pos = ubrk_next(bi); 694 TEST_ASSERT(pos == 1); 695 696 memset(vals, -1, sizeof(vals)); 697 numVals = ubrk_getRuleStatusVec(bi, vals, 10, &status); 698 TEST_ASSERT_SUCCESS(status); 699 TEST_ASSERT(numVals == 2); 700 TEST_ASSERT(vals[0] == 100); 701 TEST_ASSERT(vals[1] == 300); 702 TEST_ASSERT(vals[2] == -1); 703 704 numVals = ubrk_getRuleStatusVec(bi, vals, 0, &status); 705 TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR); 706 TEST_ASSERT(numVals == 2); 707 } 708 709 ubrk_close(bi); 710} 711 712 713/* 714 * static void TestBreakIteratorUText(void); 715 * 716 * Test that ubrk_setUText() is present and works for a simple case. 717 */ 718static void TestBreakIteratorUText(void) { 719 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 */ 720 /* 0 1 2 34567890 */ 721 722 UErrorCode status = U_ZERO_ERROR; 723 UBreakIterator *bi = NULL; 724 int32_t pos = 0; 725 726 727 UText *ut = utext_openUTF8(NULL, UTF8Str, -1, &status); 728 TEST_ASSERT_SUCCESS(status); 729 730 bi = ubrk_open(UBRK_WORD, "en_US", NULL, 0, &status); 731 if (U_FAILURE(status)) { 732 log_err_status(status, "Failure at file %s, line %d, error = %s\n", __FILE__, __LINE__, u_errorName(status)); 733 return; 734 } 735 736 ubrk_setUText(bi, ut, &status); 737 if (U_FAILURE(status)) { 738 log_err("Failure at file %s, line %d, error = %s\n", __FILE__, __LINE__, u_errorName(status)); 739 return; 740 } 741 742 pos = ubrk_first(bi); 743 TEST_ASSERT(pos == 0); 744 745 pos = ubrk_next(bi); 746 TEST_ASSERT(pos == 4); 747 748 pos = ubrk_next(bi); 749 TEST_ASSERT(pos == 5); 750 751 pos = ubrk_next(bi); 752 TEST_ASSERT(pos == 10); 753 754 pos = ubrk_next(bi); 755 TEST_ASSERT(pos == UBRK_DONE); 756 ubrk_close(bi); 757 utext_close(ut); 758} 759 760/* 761 * static void TestBreakIteratorTailoring(void); 762 * 763 * Test break iterator tailorings from CLDR data. 764 */ 765 766/* Thai/Lao grapheme break tailoring */ 767static const UChar thTest[] = { 0x0020, 0x0E40, 0x0E01, 0x0020, 768 0x0E01, 0x0E30, 0x0020, 0x0E01, 0x0E33, 0x0020, 0 }; 769/*in Unicode 6.1 en should behave just like th for this*/ 770/*static const int32_t thTestOffs_enFwd[] = { 1, 3, 4, 6, 7, 9, 10 };*/ 771static const int32_t thTestOffs_thFwd[] = { 1, 2, 3, 4, 5, 6, 7, 9, 10 }; 772/*static const int32_t thTestOffs_enRev[] = { 9, 7, 6, 4, 3, 1, 0 };*/ 773static const int32_t thTestOffs_thRev[] = { 9, 7, 6, 5, 4, 3, 2, 1, 0 }; 774 775/* Hebrew line break tailoring, for cldrbug 3028 */ 776static const UChar heTest[] = { 0x0020, 0x002D, 0x0031, 0x0032, 0x0020, 777 0x0061, 0x002D, 0x006B, 0x0020, 778 0x0061, 0x0300, 0x2010, 0x006B, 0x0020, 779 0x05DE, 0x05D4, 0x002D, 0x0069, 0x0020, 780 0x05D1, 0x05BC, 0x2010, 0x0047, 0x0020, 0 }; 781/*in Unicode 6.1 en should behave just like he for this*/ 782/*static const int32_t heTestOffs_enFwd[] = { 1, 5, 7, 9, 12, 14, 17, 19, 22, 24 };*/ 783static const int32_t heTestOffs_heFwd[] = { 1, 5, 7, 9, 12, 14, 19, 24 }; 784/*static const int32_t heTestOffs_enRev[] = { 22, 19, 17, 14, 12, 9, 7, 5, 1, 0 };*/ 785static const int32_t heTestOffs_heRev[] = { 19, 14, 12, 9, 7, 5, 1, 0 }; 786 787/* Finnish line break tailoring, for cldrbug 3029 */ 788static const UChar fiTest[] = { /* 00 */ 0x0020, 0x002D, 0x0031, 0x0032, 0x0020, 789 /* 05 */ 0x0061, 0x002D, 0x006B, 0x0020, 790 /* 09 */ 0x0061, 0x0300, 0x2010, 0x006B, 0x0020, 791 /* 14 */ 0x0061, 0x0020, 0x002D, 0x006B, 0x0020, 792 /* 19 */ 0x0061, 0x0300, 0x0020, 0x2010, 0x006B, 0x0020, 0 }; 793static const int32_t fiTestOffs_enFwd[] = { 1, 5, 7, 9, 12, 14, 16, 17, 19, 22, 23, 25 }; 794static const int32_t fiTestOffs_fiFwd[] = { 1, 5, 7, 9, 12, 14, 16, 19, 22, 25 }; 795static const int32_t fiTestOffs_enRev[] = { 23, 22, 19, 17, 16, 14, 12, 9, 7, 5, 1, 0 }; 796static const int32_t fiTestOffs_fiRev[] = { 22, 19, 16, 14, 12, 9, 7, 5, 1, 0 }; 797 798/* Khmer dictionary-based work break, for ICU ticket #8329 */ 799static const UChar kmTest[] = { /* 00 */ 0x179F, 0x17BC, 0x1798, 0x1785, 0x17C6, 0x178E, 0x17B6, 0x1799, 0x1796, 0x17C1, 800 /* 10 */ 0x179B, 0x1794, 0x1793, 0x17D2, 0x178F, 0x17B7, 0x1785, 0x178A, 0x17BE, 0x1798, 801 /* 20 */ 0x17D2, 0x1794, 0x17B8, 0x17A2, 0x1792, 0x17B7, 0x179F, 0x17D2, 0x178B, 0x17B6, 802 /* 30 */ 0x1793, 0x17A2, 0x179A, 0x1796, 0x17D2, 0x179A, 0x17C7, 0x1782, 0x17BB, 0x178E, 803 /* 40 */ 0x178A, 0x179B, 0x17CB, 0x1796, 0x17D2, 0x179A, 0x17C7, 0x17A2, 0x1784, 0x17D2, 804 /* 50 */ 0x1782, 0 }; 805static const int32_t kmTestOffs_kmFwd[] = { 3, /*8,*/ 11, 17, 23, 31, /*33,*/ 40, 43, 51 }; /* TODO: Investigate failure to break at offset 8 */ 806static const int32_t kmTestOffs_kmRev[] = { 43, 40, /*33,*/ 31, 23, 17, 11, /*8,*/ 3, 0 }; 807 808typedef struct { 809 const char * locale; 810 UBreakIteratorType type; 811 const UChar * test; 812 const int32_t * offsFwd; 813 const int32_t * offsRev; 814 int32_t numOffsets; 815} RBBITailoringTest; 816 817static const RBBITailoringTest tailoringTests[] = { 818 { "en", UBRK_CHARACTER, thTest, thTestOffs_thFwd, thTestOffs_thRev, UPRV_LENGTHOF(thTestOffs_thFwd) }, 819 { "en_US_POSIX", UBRK_CHARACTER, thTest, thTestOffs_thFwd, thTestOffs_thRev, UPRV_LENGTHOF(thTestOffs_thFwd) }, 820 { "en", UBRK_LINE, heTest, heTestOffs_heFwd, heTestOffs_heRev, UPRV_LENGTHOF(heTestOffs_heFwd) }, 821 { "he", UBRK_LINE, heTest, heTestOffs_heFwd, heTestOffs_heRev, UPRV_LENGTHOF(heTestOffs_heFwd) }, 822 { "en", UBRK_LINE, fiTest, fiTestOffs_enFwd, fiTestOffs_enRev, UPRV_LENGTHOF(fiTestOffs_enFwd) }, 823 { "fi", UBRK_LINE, fiTest, fiTestOffs_fiFwd, fiTestOffs_fiRev, UPRV_LENGTHOF(fiTestOffs_fiFwd) }, 824 { "km", UBRK_WORD, kmTest, kmTestOffs_kmFwd, kmTestOffs_kmRev, UPRV_LENGTHOF(kmTestOffs_kmFwd) }, 825 { NULL, 0, NULL, NULL, NULL, 0 }, 826}; 827 828static void TestBreakIteratorTailoring(void) { 829 const RBBITailoringTest * testPtr; 830 for (testPtr = tailoringTests; testPtr->locale != NULL; ++testPtr) { 831 UErrorCode status = U_ZERO_ERROR; 832 UBreakIterator* ubrkiter = ubrk_open(testPtr->type, testPtr->locale, testPtr->test, -1, &status); 833 if ( U_SUCCESS(status) ) { 834 int32_t offset, offsindx; 835 UBool foundError; 836 837 foundError = FALSE; 838 for (offsindx = 0; (offset = ubrk_next(ubrkiter)) != UBRK_DONE; ++offsindx) { 839 if (!foundError && offsindx >= testPtr->numOffsets) { 840 log_err("FAIL: locale %s, break type %d, ubrk_next expected UBRK_DONE, got %d\n", 841 testPtr->locale, testPtr->type, offset); 842 foundError = TRUE; 843 } else if (!foundError && offset != testPtr->offsFwd[offsindx]) { 844 log_err("FAIL: locale %s, break type %d, ubrk_next expected %d, got %d\n", 845 testPtr->locale, testPtr->type, testPtr->offsFwd[offsindx], offset); 846 foundError = TRUE; 847 } 848 } 849 if (!foundError && offsindx < testPtr->numOffsets) { 850 log_err("FAIL: locale %s, break type %d, ubrk_next expected %d, got UBRK_DONE\n", 851 testPtr->locale, testPtr->type, testPtr->offsFwd[offsindx]); 852 } 853 854 foundError = FALSE; 855 for (offsindx = 0; (offset = ubrk_previous(ubrkiter)) != UBRK_DONE; ++offsindx) { 856 if (!foundError && offsindx >= testPtr->numOffsets) { 857 log_err("FAIL: locale %s, break type %d, ubrk_previous expected UBRK_DONE, got %d\n", 858 testPtr->locale, testPtr->type, offset); 859 foundError = TRUE; 860 } else if (!foundError && offset != testPtr->offsRev[offsindx]) { 861 log_err("FAIL: locale %s, break type %d, ubrk_previous expected %d, got %d\n", 862 testPtr->locale, testPtr->type, testPtr->offsRev[offsindx], offset); 863 foundError = TRUE; 864 } 865 } 866 if (!foundError && offsindx < testPtr->numOffsets) { 867 log_err("FAIL: locale %s, break type %d, ubrk_previous expected %d, got UBRK_DONE\n", 868 testPtr->locale, testPtr->type, testPtr->offsRev[offsindx]); 869 } 870 871 ubrk_close(ubrkiter); 872 } else { 873 log_err_status(status, "FAIL: locale %s, break type %d, ubrk_open status: %s\n", testPtr->locale, testPtr->type, u_errorName(status)); 874 } 875 } 876} 877 878 879static void TestBreakIteratorRefresh(void) { 880 /* 881 * RefreshInput changes out the input of a Break Iterator without 882 * changing anything else in the iterator's state. Used with Java JNI, 883 * when Java moves the underlying string storage. This test 884 * runs a ubrk_next() repeatedly, moving the text in the middle of the sequence. 885 * The right set of boundaries should still be found. 886 */ 887 UChar testStr[] = {0x20, 0x41, 0x20, 0x42, 0x20, 0x43, 0x20, 0x44, 0x0}; /* = " A B C D" */ 888 UChar movedStr[] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0}; 889 UErrorCode status = U_ZERO_ERROR; 890 UBreakIterator *bi; 891 UText ut1 = UTEXT_INITIALIZER; 892 UText ut2 = UTEXT_INITIALIZER; 893 894 bi = ubrk_open(UBRK_LINE, "en_US", NULL, 0, &status); 895 TEST_ASSERT_SUCCESS(status); 896 if (U_FAILURE(status)) { 897 return; 898 } 899 900 utext_openUChars(&ut1, testStr, -1, &status); 901 TEST_ASSERT_SUCCESS(status); 902 ubrk_setUText(bi, &ut1, &status); 903 TEST_ASSERT_SUCCESS(status); 904 905 if (U_SUCCESS(status)) { 906 /* Line boundaries will occur before each letter in the original string */ 907 TEST_ASSERT(1 == ubrk_next(bi)); 908 TEST_ASSERT(3 == ubrk_next(bi)); 909 910 /* Move the string, kill the original string. */ 911 u_strcpy(movedStr, testStr); 912 u_memset(testStr, 0x20, u_strlen(testStr)); 913 utext_openUChars(&ut2, movedStr, -1, &status); 914 TEST_ASSERT_SUCCESS(status); 915 ubrk_refreshUText(bi, &ut2, &status); 916 TEST_ASSERT_SUCCESS(status); 917 918 /* Find the following matches, now working in the moved string. */ 919 TEST_ASSERT(5 == ubrk_next(bi)); 920 TEST_ASSERT(7 == ubrk_next(bi)); 921 TEST_ASSERT(8 == ubrk_next(bi)); 922 TEST_ASSERT(UBRK_DONE == ubrk_next(bi)); 923 TEST_ASSERT_SUCCESS(status); 924 925 utext_close(&ut1); 926 utext_close(&ut2); 927 } 928 ubrk_close(bi); 929} 930 931 932static void TestBug11665(void) { 933 // The problem was with the incorrect breaking of Japanese text beginning 934 // with Katakana characters when no prior Japanese or Chinese text had been 935 // encountered. 936 // 937 // Tested here in cintltst, rather than in intltest, because only cintltst 938 // tests have the ability to reset ICU, which is needed to get the bug 939 // to manifest itself. 940 941 static UChar japaneseText[] = {0x30A2, 0x30EC, 0x30EB, 0x30AE, 0x30FC, 0x6027, 0x7D50, 0x819C, 0x708E}; 942 int32_t boundaries[10] = {0}; 943 UBreakIterator *bi = NULL; 944 int32_t brk; 945 int32_t brkIdx = 0; 946 int32_t totalBreaks = 0; 947 UErrorCode status = U_ZERO_ERROR; 948 949 ctest_resetICU(); 950 bi = ubrk_open(UBRK_WORD, "en_US", japaneseText, UPRV_LENGTHOF(japaneseText), &status); 951 TEST_ASSERT_SUCCESS(status); 952 if (!bi) { 953 return; 954 } 955 for (brk=ubrk_first(bi); brk != UBRK_DONE; brk=ubrk_next(bi)) { 956 boundaries[brkIdx] = brk; 957 if (++brkIdx >= UPRV_LENGTHOF(boundaries) - 1) { 958 break; 959 } 960 } 961 if (brkIdx <= 2 || brkIdx >= UPRV_LENGTHOF(boundaries)) { 962 log_err("%s:%d too few or many breaks found.\n", __FILE__, __LINE__); 963 } else { 964 totalBreaks = brkIdx; 965 brkIdx = 0; 966 for (brk=ubrk_first(bi); brk != UBRK_DONE; brk=ubrk_next(bi)) { 967 if (brk != boundaries[brkIdx]) { 968 log_err("%s:%d Break #%d differs between first and second iteration.\n", __FILE__, __LINE__, brkIdx); 969 break; 970 } 971 if (++brkIdx >= UPRV_LENGTHOF(boundaries) - 1) { 972 log_err("%s:%d Too many breaks.\n", __FILE__, __LINE__); 973 break; 974 } 975 } 976 if (totalBreaks != brkIdx) { 977 log_err("%s:%d Number of breaks differ between first and second iteration.\n", __FILE__, __LINE__); 978 } 979 } 980 ubrk_close(bi); 981} 982 983/* 984 * expOffset is the set of expected offsets, ending with '-1'. 985 * "Expected expOffset -1" means "expected the end of the offsets" 986 */ 987 988static const char testSentenceSuppressionsEn[] = "Mr. Jones comes home. Dr. Smith Ph.D. is out. In the U.S.A. it is hot."; 989static const int32_t testSentSuppFwdOffsetsEn[] = { 22, 26, 46, 70, -1 }; /* With suppressions, currently not handling Dr. */ 990static const int32_t testSentFwdOffsetsEn[] = { 4, 22, 26, 46, 70, -1 }; /* Without suppressions */ 991static const int32_t testSentSuppRevOffsetsEn[] = { 46, 26, 22, 0, -1 }; /* With suppressions, currently not handling Dr. */ 992static const int32_t testSentRevOffsetsEn[] = { 46, 26, 22, 4, 0, -1 }; /* Without suppressions */ 993 994static const char testSentenceSuppressionsDe[] = "Wenn ich schon h\\u00F6re zu Guttenberg kommt evtl. zur\\u00FCck."; 995static const int32_t testSentSuppFwdOffsetsDe[] = { 53, -1 }; /* With suppressions */ 996static const int32_t testSentFwdOffsetsDe[] = { 53, -1 }; /* Without suppressions; no break in evtl. zur due to casing */ 997static const int32_t testSentSuppRevOffsetsDe[] = { 0, -1 }; /* With suppressions */ 998static const int32_t testSentRevOffsetsDe[] = { 0, -1 }; /* Without suppressions */ 999 1000static const char testSentenceSuppressionsEs[] = "Te esperamos todos los miercoles en Bravo 416, Col. El Pueblo a las 7 PM."; 1001static const int32_t testSentSuppFwdOffsetsEs[] = { 73, -1 }; /* With suppressions */ 1002static const int32_t testSentFwdOffsetsEs[] = { 52, 73, -1 }; /* Without suppressions */ 1003static const int32_t testSentSuppRevOffsetsEs[] = { 0, -1 }; /* With suppressions */ 1004static const int32_t testSentRevOffsetsEs[] = { 52, 0, -1 }; /* Without suppressions */ 1005 1006enum { kTextULenMax = 128 }; 1007 1008typedef struct { 1009 const char * locale; 1010 const char * text; 1011 const int32_t * expFwdOffsets; 1012 const int32_t * expRevOffsets; 1013} TestBISuppressionsItem; 1014 1015static const TestBISuppressionsItem testBISuppressionsItems[] = { 1016 { "en@ss=standard", testSentenceSuppressionsEn, testSentSuppFwdOffsetsEn, testSentSuppRevOffsetsEn }, 1017 { "en", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, 1018 { "en_CA", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, 1019 { "en_CA@ss=standard", testSentenceSuppressionsEn, testSentSuppFwdOffsetsEn, testSentSuppRevOffsetsEn }, 1020 { "fr@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, 1021 { "af@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, /* no brkiter data => nosuppressions? */ 1022 { "af_ZA@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, /* no brkiter data => nosuppressions? */ 1023 { "zh@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, /* brkiter data, no suppressions data => no suppressions */ 1024 { "zh_Hant@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, /* brkiter data, no suppressions data => no suppressions */ 1025 { "fi@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, /* brkiter data, no suppressions data => no suppressions */ 1026 { "ja@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, /* brkiter data, no suppressions data => no suppressions */ 1027 { "de@ss=standard", testSentenceSuppressionsDe, testSentSuppFwdOffsetsDe, testSentSuppRevOffsetsDe }, 1028 { "de", testSentenceSuppressionsDe, testSentFwdOffsetsDe, testSentRevOffsetsDe }, 1029 { "es@ss=standard", testSentenceSuppressionsEs, testSentSuppFwdOffsetsEs, testSentSuppRevOffsetsEs }, 1030 { "es", testSentenceSuppressionsEs, testSentFwdOffsetsEs, testSentRevOffsetsEs }, 1031 { NULL, NULL, NULL } 1032}; 1033 1034static void TestBreakIteratorSuppressions(void) { 1035 const TestBISuppressionsItem * itemPtr; 1036 1037 for (itemPtr = testBISuppressionsItems; itemPtr->locale != NULL; itemPtr++) { 1038 UChar textU[kTextULenMax]; 1039 int32_t textULen = u_unescape(itemPtr->text, textU, kTextULenMax); 1040 UErrorCode status = U_ZERO_ERROR; 1041 UBreakIterator *bi = ubrk_open(UBRK_SENTENCE, itemPtr->locale, textU, textULen, &status); 1042 log_verbose("#%d: %s\n", (itemPtr-testBISuppressionsItems), itemPtr->locale); 1043 if (U_SUCCESS(status)) { 1044 int32_t offset, start; 1045 const int32_t * expOffsetPtr; 1046 const int32_t * expOffsetStart; 1047 1048 expOffsetStart = expOffsetPtr = itemPtr->expFwdOffsets; 1049 ubrk_first(bi); 1050 for (; (offset = ubrk_next(bi)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { 1051 if (offset != *expOffsetPtr) { 1052 log_err("FAIL: ubrk_next loc \"%s\", expected %d, got %d\n", itemPtr->locale, *expOffsetPtr, offset); 1053 } 1054 } 1055 if (offset != UBRK_DONE || *expOffsetPtr >= 0) { 1056 log_err("FAIL: ubrk_next loc \"%s\", expected UBRK_DONE & expOffset -1, got %d and %d\n", itemPtr->locale, offset, *expOffsetPtr); 1057 } 1058 1059 expOffsetStart = expOffsetPtr = itemPtr->expFwdOffsets; 1060 start = ubrk_first(bi) + 1; 1061 for (; (offset = ubrk_following(bi, start)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { 1062 if (offset != *expOffsetPtr) { 1063 log_err("FAIL: ubrk_following(%d) loc \"%s\", expected %d, got %d\n", start, itemPtr->locale, *expOffsetPtr, offset); 1064 } 1065 start = *expOffsetPtr + 1; 1066 } 1067 if (offset != UBRK_DONE || *expOffsetPtr >= 0) { 1068 log_err("FAIL: ubrk_following(%d) loc \"%s\", expected UBRK_DONE & expOffset -1, got %d and %d\n", start, itemPtr->locale, offset, *expOffsetPtr); 1069 } 1070 1071 expOffsetStart = expOffsetPtr = itemPtr->expRevOffsets; 1072 offset = ubrk_last(bi); 1073 log_verbose("___ @%d ubrk_last\n", offset); 1074 if(offset == 0) { 1075 log_err("FAIL: ubrk_last loc \"%s\" unexpected %d\n", itemPtr->locale, offset); 1076 } 1077 for (; (offset = ubrk_previous(bi)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { 1078 if (offset != *expOffsetPtr) { 1079 log_err("FAIL: ubrk_previous loc \"%s\", expected %d, got %d\n", itemPtr->locale, *expOffsetPtr, offset); 1080 } else { 1081 log_verbose("[%d] @%d ubrk_previous()\n", (expOffsetPtr - expOffsetStart), offset); 1082 } 1083 } 1084 if (offset != UBRK_DONE || *expOffsetPtr >= 0) { 1085 log_err("FAIL: ubrk_previous loc \"%s\", expected UBRK_DONE & expOffset[%d] -1, got %d and %d\n", itemPtr->locale, 1086 expOffsetPtr - expOffsetStart, 1087 offset, *expOffsetPtr); 1088 } 1089 1090 expOffsetStart = expOffsetPtr = itemPtr->expRevOffsets; 1091 start = ubrk_last(bi) - 1; 1092 for (; (offset = ubrk_preceding(bi, start)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { 1093 if (offset != *expOffsetPtr) { 1094 log_err("FAIL: ubrk_preceding(%d) loc \"%s\", expected %d, got %d\n", start, itemPtr->locale, *expOffsetPtr, offset); 1095 } 1096 start = *expOffsetPtr - 1; 1097 } 1098 if (start >=0 && (offset != UBRK_DONE || *expOffsetPtr >= 0)) { 1099 log_err("FAIL: ubrk_preceding loc(%d) \"%s\", expected UBRK_DONE & expOffset -1, got %d and %d\n", start, itemPtr->locale, offset, *expOffsetPtr); 1100 } 1101 1102 ubrk_close(bi); 1103 } else { 1104 log_data_err("FAIL: ubrk_open(UBRK_SENTENCE, \"%s\", ...) status %s (Are you missing data?)\n", itemPtr->locale, u_errorName(status)); 1105 } 1106 } 1107} 1108 1109 1110#endif /* #if !UCONFIG_NO_BREAK_ITERATION */ 1111