X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/b75a7d8f3b4adbae880cab104ce2c6a50eee4db2..5ea0322b6ab2af986e4c764284141380031dd014:/icuSources/test/cintltst/cbiapts.c?ds=sidebyside diff --git a/icuSources/test/cintltst/cbiapts.c b/icuSources/test/cintltst/cbiapts.c index ecf20e85..39393679 100644 --- a/icuSources/test/cintltst/cbiapts.c +++ b/icuSources/test/cintltst/cbiapts.c @@ -1,6 +1,6 @@ /******************************************************************** - * COPYRIGHT: - * Copyright (c) 1997-2003, International Business Machines Corporation and + * COPYRIGHT: + * Copyright (c) 1997-2016, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /******************************************************************************** @@ -8,7 +8,7 @@ * File CBIAPTS.C * * Modification History: -* Name Description +* Name Description * Madhu Katragadda Creation *********************************************************************************/ /*C API TEST FOR BREAKITERATOR */ @@ -23,25 +23,50 @@ #if !UCONFIG_NO_BREAK_ITERATION #include +#include #include "unicode/uloc.h" #include "unicode/ubrk.h" #include "unicode/ustring.h" #include "unicode/ucnv.h" +#include "unicode/utext.h" #include "cintltst.h" #include "cbiapts.h" +#include "cmemory.h" +#define TEST_ASSERT_SUCCESS(status) {if (U_FAILURE(status)) { \ +log_data_err("Failure at file %s, line %d, error = %s (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status));}} + +#define TEST_ASSERT(expr) {if ((expr)==FALSE) { \ +log_data_err("Test Failure at file %s, line %d (Are you missing data?)\n", __FILE__, __LINE__);}} + +#if !UCONFIG_NO_FILE_IO static void TestBreakIteratorSafeClone(void); +#endif static void TestBreakIteratorRules(void); static void TestBreakIteratorRuleError(void); +static void TestBreakIteratorStatusVec(void); +static void TestBreakIteratorUText(void); +static void TestBreakIteratorTailoring(void); +static void TestBreakIteratorRefresh(void); +static void TestBug11665(void); +static void TestBreakIteratorSuppressions(void); void addBrkIterAPITest(TestNode** root); void addBrkIterAPITest(TestNode** root) { +#if !UCONFIG_NO_FILE_IO addTest(root, &TestBreakIteratorCAPI, "tstxtbd/cbiapts/TestBreakIteratorCAPI"); addTest(root, &TestBreakIteratorSafeClone, "tstxtbd/cbiapts/TestBreakIteratorSafeClone"); + addTest(root, &TestBreakIteratorUText, "tstxtbd/cbiapts/TestBreakIteratorUText"); +#endif addTest(root, &TestBreakIteratorRules, "tstxtbd/cbiapts/TestBreakIteratorRules"); addTest(root, &TestBreakIteratorRuleError, "tstxtbd/cbiapts/TestBreakIteratorRuleError"); + addTest(root, &TestBreakIteratorStatusVec, "tstxtbd/cbiapts/TestBreakIteratorStatusVec"); + addTest(root, &TestBreakIteratorTailoring, "tstxtbd/cbiapts/TestBreakIteratorTailoring"); + addTest(root, &TestBreakIteratorRefresh, "tstxtbd/cbiapts/TestBreakIteratorRefresh"); + addTest(root, &TestBug11665, "tstxtbd/cbiapts/TestBug11665"); + addTest(root, &TestBreakIteratorSuppressions, "tstxtbd/cbiapts/TestBreakIteratorSuppressions"); } #define CLONETEST_ITERATOR_COUNT 2 @@ -72,7 +97,7 @@ static UChar* toUChar(const char *src, void **freeHook) { return NULL; }; - cnv = ucnv_open(NULL, &status); + cnv = ucnv_open(NULL, &status); if(U_FAILURE(status) || cnv == NULL) { return NULL; } @@ -101,7 +126,7 @@ static UChar* toUChar(const char *src, void **freeHook) { if (dest == NULL) { return NULL; } - + dest->link = (StringStruct*)(*freeHook); *freeHook = dest; return dest->str; @@ -117,6 +142,7 @@ static void freeToUCharStrings(void **hook) { } +#if !UCONFIG_NO_FILE_IO static void TestBreakIteratorCAPI() { UErrorCode status = U_ZERO_ERROR; @@ -136,50 +162,56 @@ static void TestBreakIteratorCAPI() /*test ubrk_open()*/ log_verbose("\nTesting BreakIterator open functions\n"); - + /* Use french for fun */ word = ubrk_open(UBRK_WORD, "en_US", text, u_strlen(text), &status); if(status == U_FILE_ACCESS_ERROR) { - log_data_err("Check your data - it doesn't seem to be around\n"); - return; + log_data_err("Check your data - it doesn't seem to be around\n"); + return; } else if(U_FAILURE(status)){ - log_err("FAIL: Error in ubrk_open() for word breakiterator: %s\n", myErrorName(status)); + log_err_status(status, "FAIL: Error in ubrk_open() for word breakiterator: %s\n", myErrorName(status)); } else{ log_verbose("PASS: Successfully opened word breakiterator\n"); } - + sentence = ubrk_open(UBRK_SENTENCE, "en_US", text, u_strlen(text), &status); if(U_FAILURE(status)){ - log_err("FAIL: Error in ubrk_open() for sentence breakiterator: %s\n", myErrorName(status)); + log_err_status(status, "FAIL: Error in ubrk_open() for sentence breakiterator: %s\n", myErrorName(status)); + return; } else{ log_verbose("PASS: Successfully opened sentence breakiterator\n"); } - + line = ubrk_open(UBRK_LINE, "en_US", text, u_strlen(text), &status); if(U_FAILURE(status)){ log_err("FAIL: Error in ubrk_open() for line breakiterator: %s\n", myErrorName(status)); + return; } else{ log_verbose("PASS: Successfully opened line breakiterator\n"); } - + character = ubrk_open(UBRK_CHARACTER, "en_US", text, u_strlen(text), &status); if(U_FAILURE(status)){ log_err("FAIL: Error in ubrk_open() for character breakiterator: %s\n", myErrorName(status)); + return; } else{ log_verbose("PASS: Successfully opened character breakiterator\n"); } /*trying to open an illegal iterator*/ bogus = ubrk_open((UBreakIteratorType)5, "en_US", text, u_strlen(text), &status); + if(bogus != NULL) { + log_err("FAIL: expected NULL from opening an invalid break iterator.\n"); + } if(U_SUCCESS(status)){ - log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_MEMORY_ALLOCATION_ERROR\n"); + log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_ILLEGAL_ARGUMENT_ERROR\n"); } if(U_FAILURE(status)){ - if(status != U_MEMORY_ALLOCATION_ERROR){ - log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_MEMORY_ALLOCATION_ERROR\n Got %s\n", myErrorName(status)); + if(status != U_ILLEGAL_ARGUMENT_ERROR){ + log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_ILLEGAL_ARGUMENT_ERROR\n Got %s\n", myErrorName(status)); } } status=U_ZERO_ERROR; @@ -198,10 +230,10 @@ static void TestBreakIteratorCAPI() } for(i=0;i 1) { + --bufferSize; + } + if (NULL == (brk = ubrk_safeClone(someIterators[i], NULL, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_WARNING) { log_err("FAIL: Cloned Iterator failed to deal correctly with too-small buffer size\n"); } @@ -396,7 +459,7 @@ static void TestBreakIteratorSafeClone(void) bufferSize = U_BRK_SAFECLONE_BUFFERSIZE; /* Null buffer pointer - return Iterator & set error to U_SAFECLONE_ALLOCATED_ERROR */ - if (0 == (brk = ubrk_safeClone(someIterators[i], 0, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_WARNING) + if (NULL == (brk = ubrk_safeClone(someIterators[i], NULL, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_WARNING) { log_err("FAIL: Cloned Iterator failed to deal correctly with null buffer pointer\n"); } @@ -407,22 +470,13 @@ static void TestBreakIteratorSafeClone(void) /* Mis-aligned buffer pointer. */ { char stackBuf[U_BRK_SAFECLONE_BUFFERSIZE+sizeof(void *)]; - void *p; - int32_t offset; brk = ubrk_safeClone(someIterators[i], &stackBuf[1], &bufferSize, &status); - if (U_FAILURE(status) || brk == 0) { + if (U_FAILURE(status) || brk == NULL) { log_err("FAIL: Cloned Iterator failed with misaligned buffer pointer\n"); } if (status == U_SAFECLONE_ALLOCATED_WARNING) { - log_err("FAIL: Cloned Iterator allocated when using a mis-aligned buffer.\n"); - } - offset = (int32_t)((char *)&p-(char*)brk); - if (offset < 0) { - offset = -offset; - } - if (offset % sizeof(void *) != 0) { - log_err("FAIL: Cloned Iterator failed to align correctly with misaligned buffer pointer\n"); + log_verbose("Cloned Iterator allocated when using a mis-aligned buffer.\n"); } if (brk) ubrk_close(brk); @@ -430,7 +484,7 @@ static void TestBreakIteratorSafeClone(void) /* Null Iterator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */ - if (0 != ubrk_safeClone(0, buffer[i], &bufferSize, &status) || status != U_ILLEGAL_ARGUMENT_ERROR) + if (NULL != ubrk_safeClone(NULL, buffer[i], &bufferSize, &status) || status != U_ILLEGAL_ARGUMENT_ERROR) { log_err("FAIL: Cloned Iterator failed to deal correctly with null Iterator pointer\n"); } @@ -451,6 +505,7 @@ static void TestBreakIteratorSafeClone(void) ubrk_close(someIterators[i]); } } +#endif /* @@ -469,9 +524,9 @@ static UBreakIterator * testOpenRules(char *rules) { bi = ubrk_openRules(ruleSourceU, -1, /* The rules */ NULL, -1, /* The text to be iterated over. */ &parseErr, &status); - + if (U_FAILURE(status)) { - log_err("FAIL: ubrk_openRules: ICU Error \"%s\"\n", u_errorName(status)); + log_data_err("FAIL: ubrk_openRules: ICU Error \"%s\" (Are you missing data?)\n", u_errorName(status)); bi = 0; }; freeToUCharStrings(&strCleanUp); @@ -488,7 +543,7 @@ static void TestBreakIteratorRules() { * keep together 'abc', but only when followed by 'def', OTHERWISE * just return one char at a time. */ - char rules[] = "abc{666}/def;\n [\\p{L} - [a]]* {2}; . {1};"; + char rules[] = "abc/def{666};\n [\\p{L} - [a]]* {2}; . {1};"; /* 0123456789012345678 */ char data[] = "abcdex abcdefgh-def"; /* the test data string */ char breaks[] = "** ** * ** *"; /* * the expected break positions */ @@ -556,11 +611,507 @@ static void TestBreakIteratorRuleError() { ubrk_close(bi); } else { if (parseErr.line != 3 || parseErr.offset != 8) { - log_err("FAIL: incorrect error position reported. Got line %d, char %d, expected line 3, char 7", + log_data_err("FAIL: incorrect error position reported. Got line %d, char %d, expected line 3, char 7 (Are you missing data?)\n", parseErr.line, parseErr.offset); } } freeToUCharStrings(&freeHook); } + +/* +* TestsBreakIteratorStatusVals() Test the ubrk_getRuleStatusVec() funciton +*/ +static void TestBreakIteratorStatusVec() { + #define RULE_STRING_LENGTH 200 + UChar rules[RULE_STRING_LENGTH]; + + #define TEST_STRING_LENGTH 25 + UChar testString[TEST_STRING_LENGTH]; + UBreakIterator *bi = NULL; + int32_t pos = 0; + int32_t vals[10]; + int32_t numVals; + UErrorCode status = U_ZERO_ERROR; + + u_uastrncpy(rules, "[A-N]{100}; \n" + "[a-w]{200}; \n" + "[\\p{L}]{300}; \n" + "[\\p{N}]{400}; \n" + "[0-5]{500}; \n" + "!.*;\n", RULE_STRING_LENGTH); + u_uastrncpy(testString, "ABC", TEST_STRING_LENGTH); + + + bi = ubrk_openRules(rules, -1, testString, -1, NULL, &status); + TEST_ASSERT_SUCCESS(status); + TEST_ASSERT(bi != NULL); + + /* The TEST_ASSERT above should change too... */ + if (bi != NULL) { + pos = ubrk_next(bi); + TEST_ASSERT(pos == 1); + + memset(vals, -1, sizeof(vals)); + numVals = ubrk_getRuleStatusVec(bi, vals, 10, &status); + TEST_ASSERT_SUCCESS(status); + TEST_ASSERT(numVals == 2); + TEST_ASSERT(vals[0] == 100); + TEST_ASSERT(vals[1] == 300); + TEST_ASSERT(vals[2] == -1); + + numVals = ubrk_getRuleStatusVec(bi, vals, 0, &status); + TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR); + TEST_ASSERT(numVals == 2); + } + + ubrk_close(bi); +} + + +/* + * static void TestBreakIteratorUText(void); + * + * Test that ubrk_setUText() is present and works for a simple case. + */ +static void TestBreakIteratorUText(void) { + 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 */ + /* 0 1 2 34567890 */ + + UErrorCode status = U_ZERO_ERROR; + UBreakIterator *bi = NULL; + int32_t pos = 0; + + + UText *ut = utext_openUTF8(NULL, UTF8Str, -1, &status); + TEST_ASSERT_SUCCESS(status); + + bi = ubrk_open(UBRK_WORD, "en_US", NULL, 0, &status); + if (U_FAILURE(status)) { + log_err_status(status, "Failure at file %s, line %d, error = %s\n", __FILE__, __LINE__, u_errorName(status)); + return; + } + + ubrk_setUText(bi, ut, &status); + if (U_FAILURE(status)) { + log_err("Failure at file %s, line %d, error = %s\n", __FILE__, __LINE__, u_errorName(status)); + return; + } + + pos = ubrk_first(bi); + TEST_ASSERT(pos == 0); + + pos = ubrk_next(bi); + TEST_ASSERT(pos == 4); + + pos = ubrk_next(bi); + TEST_ASSERT(pos == 5); + + pos = ubrk_next(bi); + TEST_ASSERT(pos == 10); + + pos = ubrk_next(bi); + TEST_ASSERT(pos == UBRK_DONE); + ubrk_close(bi); + utext_close(ut); +} + +/* + * static void TestBreakIteratorTailoring(void); + * + * Test break iterator tailorings from CLDR data. + */ + +/* Thai/Lao grapheme break tailoring */ +static const UChar thTest[] = { 0x0020, 0x0E40, 0x0E01, 0x0020, + 0x0E01, 0x0E30, 0x0020, 0x0E01, 0x0E33, 0x0020, 0 }; +/*in Unicode 6.1 en should behave just like th for this*/ +/*static const int32_t thTestOffs_enFwd[] = { 1, 3, 4, 6, 7, 9, 10 };*/ +static const int32_t thTestOffs_thFwd[] = { 1, 2, 3, 4, 5, 6, 7, 9, 10 }; +/*static const int32_t thTestOffs_enRev[] = { 9, 7, 6, 4, 3, 1, 0 };*/ +static const int32_t thTestOffs_thRev[] = { 9, 7, 6, 5, 4, 3, 2, 1, 0 }; + +/* Hebrew line break tailoring, for cldrbug 3028 */ +static const UChar heTest[] = { 0x0020, 0x002D, 0x0031, 0x0032, 0x0020, + 0x0061, 0x002D, 0x006B, 0x0020, + 0x0061, 0x0300, 0x2010, 0x006B, 0x0020, + 0x05DE, 0x05D4, 0x002D, 0x0069, 0x0020, + 0x05D1, 0x05BC, 0x2010, 0x0047, 0x0020, 0 }; +/*in Unicode 6.1 en should behave just like he for this*/ +/*static const int32_t heTestOffs_enFwd[] = { 1, 5, 7, 9, 12, 14, 17, 19, 22, 24 };*/ +static const int32_t heTestOffs_heFwd[] = { 1, 5, 7, 9, 12, 14, 19, 24 }; +/*static const int32_t heTestOffs_enRev[] = { 22, 19, 17, 14, 12, 9, 7, 5, 1, 0 };*/ +static const int32_t heTestOffs_heRev[] = { 19, 14, 12, 9, 7, 5, 1, 0 }; + +/* Finnish line break tailoring, for cldrbug 3029 */ +static const UChar fiTest[] = { /* 00 */ 0x0020, 0x002D, 0x0031, 0x0032, 0x0020, + /* 05 */ 0x0061, 0x002D, 0x006B, 0x0020, + /* 09 */ 0x0061, 0x0300, 0x2010, 0x006B, 0x0020, + /* 14 */ 0x0061, 0x0020, 0x002D, 0x006B, 0x0020, + /* 19 */ 0x0061, 0x0300, 0x0020, 0x2010, 0x006B, 0x0020, 0 }; +static const int32_t fiTestOffs_enFwd[] = { 1, 5, 7, 9, 12, 14, 16, 17, 19, 22, 23, 25 }; +static const int32_t fiTestOffs_fiFwd[] = { 1, 5, 7, 9, 12, 14, 16, 19, 22, 25 }; +static const int32_t fiTestOffs_enRev[] = { 23, 22, 19, 17, 16, 14, 12, 9, 7, 5, 1, 0 }; +static const int32_t fiTestOffs_fiRev[] = { 22, 19, 16, 14, 12, 9, 7, 5, 1, 0 }; + +/* Khmer dictionary-based work break, for ICU ticket #8329 */ +static const UChar kmTest[] = { /* 00 */ 0x179F, 0x17BC, 0x1798, 0x1785, 0x17C6, 0x178E, 0x17B6, 0x1799, 0x1796, 0x17C1, + /* 10 */ 0x179B, 0x1794, 0x1793, 0x17D2, 0x178F, 0x17B7, 0x1785, 0x178A, 0x17BE, 0x1798, + /* 20 */ 0x17D2, 0x1794, 0x17B8, 0x17A2, 0x1792, 0x17B7, 0x179F, 0x17D2, 0x178B, 0x17B6, + /* 30 */ 0x1793, 0x17A2, 0x179A, 0x1796, 0x17D2, 0x179A, 0x17C7, 0x1782, 0x17BB, 0x178E, + /* 40 */ 0x178A, 0x179B, 0x17CB, 0x1796, 0x17D2, 0x179A, 0x17C7, 0x17A2, 0x1784, 0x17D2, + /* 50 */ 0x1782, 0 }; +static const int32_t kmTestOffs_kmFwd[] = { 3, /*8,*/ 11, 17, 23, 31, /*33,*/ 40, 43, 51 }; /* TODO: Investigate failure to break at offset 8 */ +static const int32_t kmTestOffs_kmRev[] = { 43, 40, /*33,*/ 31, 23, 17, 11, /*8,*/ 3, 0 }; + +typedef struct { + const char * locale; + UBreakIteratorType type; + const UChar * test; + const int32_t * offsFwd; + const int32_t * offsRev; + int32_t numOffsets; +} RBBITailoringTest; + +static const RBBITailoringTest tailoringTests[] = { + { "en", UBRK_CHARACTER, thTest, thTestOffs_thFwd, thTestOffs_thRev, UPRV_LENGTHOF(thTestOffs_thFwd) }, + { "en_US_POSIX", UBRK_CHARACTER, thTest, thTestOffs_thFwd, thTestOffs_thRev, UPRV_LENGTHOF(thTestOffs_thFwd) }, + { "en", UBRK_LINE, heTest, heTestOffs_heFwd, heTestOffs_heRev, UPRV_LENGTHOF(heTestOffs_heFwd) }, + { "he", UBRK_LINE, heTest, heTestOffs_heFwd, heTestOffs_heRev, UPRV_LENGTHOF(heTestOffs_heFwd) }, + { "en", UBRK_LINE, fiTest, fiTestOffs_enFwd, fiTestOffs_enRev, UPRV_LENGTHOF(fiTestOffs_enFwd) }, + { "fi", UBRK_LINE, fiTest, fiTestOffs_fiFwd, fiTestOffs_fiRev, UPRV_LENGTHOF(fiTestOffs_fiFwd) }, + { "km", UBRK_WORD, kmTest, kmTestOffs_kmFwd, kmTestOffs_kmRev, UPRV_LENGTHOF(kmTestOffs_kmFwd) }, + { NULL, 0, NULL, NULL, NULL, 0 }, +}; + +static void TestBreakIteratorTailoring(void) { + const RBBITailoringTest * testPtr; + for (testPtr = tailoringTests; testPtr->locale != NULL; ++testPtr) { + UErrorCode status = U_ZERO_ERROR; + UBreakIterator* ubrkiter = ubrk_open(testPtr->type, testPtr->locale, testPtr->test, -1, &status); + if ( U_SUCCESS(status) ) { + int32_t offset, offsindx; + UBool foundError; + + foundError = FALSE; + for (offsindx = 0; (offset = ubrk_next(ubrkiter)) != UBRK_DONE; ++offsindx) { + if (!foundError && offsindx >= testPtr->numOffsets) { + log_err("FAIL: locale %s, break type %d, ubrk_next expected UBRK_DONE, got %d\n", + testPtr->locale, testPtr->type, offset); + foundError = TRUE; + } else if (!foundError && offset != testPtr->offsFwd[offsindx]) { + log_err("FAIL: locale %s, break type %d, ubrk_next expected %d, got %d\n", + testPtr->locale, testPtr->type, testPtr->offsFwd[offsindx], offset); + foundError = TRUE; + } + } + if (!foundError && offsindx < testPtr->numOffsets) { + log_err("FAIL: locale %s, break type %d, ubrk_next expected %d, got UBRK_DONE\n", + testPtr->locale, testPtr->type, testPtr->offsFwd[offsindx]); + } + + foundError = FALSE; + for (offsindx = 0; (offset = ubrk_previous(ubrkiter)) != UBRK_DONE; ++offsindx) { + if (!foundError && offsindx >= testPtr->numOffsets) { + log_err("FAIL: locale %s, break type %d, ubrk_previous expected UBRK_DONE, got %d\n", + testPtr->locale, testPtr->type, offset); + foundError = TRUE; + } else if (!foundError && offset != testPtr->offsRev[offsindx]) { + log_err("FAIL: locale %s, break type %d, ubrk_previous expected %d, got %d\n", + testPtr->locale, testPtr->type, testPtr->offsRev[offsindx], offset); + foundError = TRUE; + } + } + if (!foundError && offsindx < testPtr->numOffsets) { + log_err("FAIL: locale %s, break type %d, ubrk_previous expected %d, got UBRK_DONE\n", + testPtr->locale, testPtr->type, testPtr->offsRev[offsindx]); + } + + ubrk_close(ubrkiter); + } else { + log_err_status(status, "FAIL: locale %s, break type %d, ubrk_open status: %s\n", testPtr->locale, testPtr->type, u_errorName(status)); + } + } +} + + +static void TestBreakIteratorRefresh(void) { + /* + * RefreshInput changes out the input of a Break Iterator without + * changing anything else in the iterator's state. Used with Java JNI, + * when Java moves the underlying string storage. This test + * runs a ubrk_next() repeatedly, moving the text in the middle of the sequence. + * The right set of boundaries should still be found. + */ + UChar testStr[] = {0x20, 0x41, 0x20, 0x42, 0x20, 0x43, 0x20, 0x44, 0x0}; /* = " A B C D" */ + UChar movedStr[] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0}; + UErrorCode status = U_ZERO_ERROR; + UBreakIterator *bi; + UText ut1 = UTEXT_INITIALIZER; + UText ut2 = UTEXT_INITIALIZER; + + bi = ubrk_open(UBRK_LINE, "en_US", NULL, 0, &status); + TEST_ASSERT_SUCCESS(status); + if (U_FAILURE(status)) { + return; + } + + utext_openUChars(&ut1, testStr, -1, &status); + TEST_ASSERT_SUCCESS(status); + ubrk_setUText(bi, &ut1, &status); + TEST_ASSERT_SUCCESS(status); + + if (U_SUCCESS(status)) { + /* Line boundaries will occur before each letter in the original string */ + TEST_ASSERT(1 == ubrk_next(bi)); + TEST_ASSERT(3 == ubrk_next(bi)); + + /* Move the string, kill the original string. */ + u_strcpy(movedStr, testStr); + u_memset(testStr, 0x20, u_strlen(testStr)); + utext_openUChars(&ut2, movedStr, -1, &status); + TEST_ASSERT_SUCCESS(status); + ubrk_refreshUText(bi, &ut2, &status); + TEST_ASSERT_SUCCESS(status); + + /* Find the following matches, now working in the moved string. */ + TEST_ASSERT(5 == ubrk_next(bi)); + TEST_ASSERT(7 == ubrk_next(bi)); + TEST_ASSERT(8 == ubrk_next(bi)); + TEST_ASSERT(UBRK_DONE == ubrk_next(bi)); + TEST_ASSERT_SUCCESS(status); + + utext_close(&ut1); + utext_close(&ut2); + } + ubrk_close(bi); +} + + +static void TestBug11665(void) { + // The problem was with the incorrect breaking of Japanese text beginning + // with Katakana characters when no prior Japanese or Chinese text had been + // encountered. + // + // Tested here in cintltst, rather than in intltest, because only cintltst + // tests have the ability to reset ICU, which is needed to get the bug + // to manifest itself. + + static UChar japaneseText[] = {0x30A2, 0x30EC, 0x30EB, 0x30AE, 0x30FC, 0x6027, 0x7D50, 0x819C, 0x708E}; + int32_t boundaries[10] = {0}; + UBreakIterator *bi = NULL; + int32_t brk; + int32_t brkIdx = 0; + int32_t totalBreaks = 0; + UErrorCode status = U_ZERO_ERROR; + + ctest_resetICU(); + bi = ubrk_open(UBRK_WORD, "en_US", japaneseText, UPRV_LENGTHOF(japaneseText), &status); + TEST_ASSERT_SUCCESS(status); + if (!bi) { + return; + } + for (brk=ubrk_first(bi); brk != UBRK_DONE; brk=ubrk_next(bi)) { + boundaries[brkIdx] = brk; + if (++brkIdx >= UPRV_LENGTHOF(boundaries) - 1) { + break; + } + } + if (brkIdx <= 2 || brkIdx >= UPRV_LENGTHOF(boundaries)) { + log_err("%s:%d too few or many breaks found.\n", __FILE__, __LINE__); + } else { + totalBreaks = brkIdx; + brkIdx = 0; + for (brk=ubrk_first(bi); brk != UBRK_DONE; brk=ubrk_next(bi)) { + if (brk != boundaries[brkIdx]) { + log_err("%s:%d Break #%d differs between first and second iteration.\n", __FILE__, __LINE__, brkIdx); + break; + } + if (++brkIdx >= UPRV_LENGTHOF(boundaries) - 1) { + log_err("%s:%d Too many breaks.\n", __FILE__, __LINE__); + break; + } + } + if (totalBreaks != brkIdx) { + log_err("%s:%d Number of breaks differ between first and second iteration.\n", __FILE__, __LINE__); + } + } + ubrk_close(bi); +} + +/* + * expOffset is the set of expected offsets, ending with '-1'. + * "Expected expOffset -1" means "expected the end of the offsets" + */ + +static const char testSentenceSuppressionsEn[] = "Mr. Jones comes home. Dr. Smith Ph.D. is out. In the U.S.A. it is hot."; +static const int32_t testSentSuppFwdOffsetsEn[] = { 22, 46, 70, -1 }; /* With suppressions */ +static const int32_t testSentFwdOffsetsEn[] = { 4, 22, 26, 46, 70, -1 }; /* Without suppressions */ +static const int32_t testSentSuppRevOffsetsEn[] = { 46, 22, 0, -1 }; /* With suppressions */ +static const int32_t testSentRevOffsetsEn[] = { 46, 26, 22, 4, 0, -1 }; /* Without suppressions */ + +static const char testSentenceSuppressionsDe[] = "Wenn ich schon h\\u00F6re zu Guttenberg kommt evtl. zur\\u00FCck."; +// "Wenn ich schon höre zu Guttenberg kommt evtl. zurück." +static const int32_t testSentSuppFwdOffsetsDe[] = { 53, -1 }; /* With suppressions */ +static const int32_t testSentFwdOffsetsDe[] = { 53, -1 }; /* Without suppressions; no break in evtl. zur due to casing */ +static const int32_t testSentSuppRevOffsetsDe[] = { 0, -1 }; /* With suppressions */ +static const int32_t testSentRevOffsetsDe[] = { 0, -1 }; /* Without suppressions */ + +static const char testSentenceSuppressionsEs[] = "Te esperamos todos los miercoles en Bravo 416, Col. El Pueblo a las 7 PM."; +static const int32_t testSentSuppFwdOffsetsEs[] = { 73, -1 }; /* With suppressions */ +static const int32_t testSentFwdOffsetsEs[] = { 52, 73, -1 }; /* Without suppressions */ +static const int32_t testSentSuppRevOffsetsEs[] = { 0, -1 }; /* With suppressions */ +static const int32_t testSentRevOffsetsEs[] = { 52, 0, -1 }; /* Without suppressions */ + +static const char testSentenceSuppressionsE1[] = "Add or detract. The world will little note."; +static const char testSentenceSuppressionsE1u[] = "ADD OR DETRACT. THE WORLD WILL LITTLE NOTE."; +static const int32_t testSentFwdOffsetsE1[] = { 16, 43, -1 }; /* Suppressions and case should make no difference */ +static const int32_t testSentRevOffsetsE1[] = { 16, 0, -1 }; /* Suppressions and case should make no difference */ + +static const char testSentenceSuppressionsE2[] = "Coming up, the sprints at NCAA. Are you watching?"; +static const char testSentenceSuppressionsE2u[] = "COMING UP, THE SPRINTS AT NCAA. ARE YOU WATCHING?"; +static const int32_t testSentFwdOffsetsE2[] = { 32, 49, -1 }; /* Suppressions and case should make no difference */ +static const int32_t testSentRevOffsetsE2[] = { 32, 0, -1 }; /* Suppressions and case should make no difference */ + +static const char testSentenceSuppressionsFr[] = "Tr\\u00E8s bonne prise de parole de M. Junod, municipal \\u00E0 la culture de Lausanne."; +// "Très bonne prise de parole de M. Junod, municipal à la culture de Lausanne." +static const int32_t testSentFwdOffsetsFr[] = { 33, 75, -1 }; /* Without suppressions */ +static const int32_t testSentSuppFwdOffsetsFr[] = { 75, -1 }; /* With suppressions */ +static const int32_t testSentRevOffsetsFr[] = { 33, 0, -1 }; /* Without suppressions */ +static const int32_t testSentSuppRevOffsetsFr[] = { 0, -1 }; /* With suppressions */ + +static const char testSentenceSuppressionsE3[] = "G8 countries e.g. U.K., Japan. Sanctions i.e. restrictions. Test E. Xx G. Xx I. Xx."; +static const char testSentenceSuppressionsE3u[] = "G8 COUNTRIES E.G. U.K., JAPAN. SANCTIONS I.E. RESTRICTIONS. TEST E. XX G. XX I. XX."; +static const int32_t testSentSuppFwdOffsetsE3[] = { 31, 60, 83, -1 }; /* With suppressions */ +static const int32_t testSentSuppRevOffsetsE3[] = { 60, 31, 0, -1 }; /* With suppressions */ +static const int32_t testSentFwdOffsetsE3[] = { 18, 31, 60, 68, 74, 80, 83, -1 }; /* Without suppressions */ +static const int32_t testSentRevOffsetsE3[] = { 80, 74, 68, 60, 31, 18, 0, -1 }; /* Without suppressions */ +static const int32_t testSentFwdOffsetsE3u[] = { 18, 31, 46, 60, 68, 74, 80, 83, -1 }; /* Without suppressions */ +static const int32_t testSentRevOffsetsE3u[] = { 80, 74, 68, 60, 46, 31, 18, 0, -1 }; /* Without suppressions */ + +enum { kTextULenMax = 128, kTextBLenMax = 192 }; + +typedef struct { + const char * locale; + const char * text; + const int32_t * expFwdOffsets; + const int32_t * expRevOffsets; +} TestBISuppressionsItem; + +static const TestBISuppressionsItem testBISuppressionsItems[] = { + { "en@ss=standard", testSentenceSuppressionsEn, testSentSuppFwdOffsetsEn, testSentSuppRevOffsetsEn }, + { "en", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, + { "fr@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, + { "af@ss=standard", testSentenceSuppressionsEn, testSentSuppFwdOffsetsEn, testSentSuppRevOffsetsEn }, /* no brkiter data => en suppressions? */ + { "zh@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, /* brkiter data, no suppressions data => no suppressions */ + { "zh_Hant@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, /* brkiter data, no suppressions data => no suppressions */ + { "fi@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, /* brkiter data, no suppressions data => no suppressions */ + { "ja@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, /* brkiter data, no suppressions data => no suppressions */ + { "de@ss=standard", testSentenceSuppressionsDe, testSentSuppFwdOffsetsDe, testSentSuppRevOffsetsDe }, + { "de", testSentenceSuppressionsDe, testSentFwdOffsetsDe, testSentRevOffsetsDe }, + { "es@ss=standard", testSentenceSuppressionsEs, testSentSuppFwdOffsetsEs, testSentSuppRevOffsetsEs }, + { "es", testSentenceSuppressionsEs, testSentFwdOffsetsEs, testSentRevOffsetsEs }, + { "en", testSentenceSuppressionsE1, testSentFwdOffsetsE1, testSentRevOffsetsE1 }, + { "en@ss=standard", testSentenceSuppressionsE1, testSentFwdOffsetsE1, testSentRevOffsetsE1 }, + { "en", testSentenceSuppressionsE1u, testSentFwdOffsetsE1, testSentRevOffsetsE1 }, + { "en@ss=standard", testSentenceSuppressionsE1u, testSentFwdOffsetsE1, testSentRevOffsetsE1 }, + { "en", testSentenceSuppressionsE2, testSentFwdOffsetsE2, testSentRevOffsetsE2 }, + { "en@ss=standard", testSentenceSuppressionsE2, testSentFwdOffsetsE2, testSentRevOffsetsE2 }, + { "en", testSentenceSuppressionsE2u, testSentFwdOffsetsE2, testSentRevOffsetsE2 }, + { "en@ss=standard", testSentenceSuppressionsE2u, testSentFwdOffsetsE2, testSentRevOffsetsE2 }, + { "fr", testSentenceSuppressionsFr, testSentFwdOffsetsFr, testSentRevOffsetsFr }, + { "fr@ss=standard", testSentenceSuppressionsFr, testSentSuppFwdOffsetsFr, testSentSuppRevOffsetsFr }, + { "en@ss=standard", testSentenceSuppressionsE3, testSentSuppFwdOffsetsE3, testSentSuppRevOffsetsE3 }, + { "en", testSentenceSuppressionsE3, testSentFwdOffsetsE3, testSentRevOffsetsE3 }, + { "en@ss=standard", testSentenceSuppressionsE3u, testSentSuppFwdOffsetsE3, testSentSuppRevOffsetsE3 }, + { "en", testSentenceSuppressionsE3u, testSentFwdOffsetsE3u, testSentRevOffsetsE3u }, + { NULL, NULL, NULL } +}; + +static void TestBreakIteratorSuppressions(void) { + const TestBISuppressionsItem * itemPtr; + + for (itemPtr = testBISuppressionsItems; itemPtr->locale != NULL; itemPtr++) { + UChar textU[kTextULenMax + 1]; + char textB[kTextBLenMax]; + int32_t textULen = u_unescape(itemPtr->text, textU, kTextULenMax); + textU[kTextULenMax] = 0; // ensure zero termination + UErrorCode status = U_ZERO_ERROR; + UBreakIterator *bi = ubrk_open(UBRK_SENTENCE, itemPtr->locale, textU, textULen, &status); + log_verbose("#%d: %s\n", (itemPtr-testBISuppressionsItems), itemPtr->locale); + if (U_SUCCESS(status)) { + int32_t offset, start; + const int32_t * expOffsetPtr; + const int32_t * expOffsetStart; + u_austrcpy(textB, textU); + + expOffsetStart = expOffsetPtr = itemPtr->expFwdOffsets; + ubrk_first(bi); + for (; (offset = ubrk_next(bi)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { + if (offset != *expOffsetPtr) { + log_err("FAIL: ubrk_next loc \"%s\", expected %d, got %d, text \"%s\"\n", + itemPtr->locale, *expOffsetPtr, offset, textB); + } + } + if (offset != UBRK_DONE || *expOffsetPtr >= 0) { + log_err("FAIL: ubrk_next loc \"%s\", expected UBRK_DONE & expOffset -1, got %d and %d, text \"%s\"\n", + itemPtr->locale, offset, *expOffsetPtr, textB); + } + + expOffsetStart = expOffsetPtr = itemPtr->expFwdOffsets; + start = ubrk_first(bi) + 1; + for (; (offset = ubrk_following(bi, start)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { + if (offset != *expOffsetPtr) { + log_err("FAIL: ubrk_following(%d) loc \"%s\", expected %d, got %d, text \"%s\"\n", + start, itemPtr->locale, *expOffsetPtr, offset, textB); + } + start = *expOffsetPtr + 1; + } + if (offset != UBRK_DONE || *expOffsetPtr >= 0) { + log_err("FAIL: ubrk_following(%d) loc \"%s\", expected UBRK_DONE & expOffset -1, got %d and %d, text \"%s\"\n", + start, itemPtr->locale, offset, *expOffsetPtr, textB); + } + + expOffsetStart = expOffsetPtr = itemPtr->expRevOffsets; + offset = ubrk_last(bi); + log_verbose("___ @%d ubrk_last\n", offset); + if(offset == 0) { + log_err("FAIL: ubrk_last loc \"%s\" unexpected %d\n", itemPtr->locale, offset); + } + for (; (offset = ubrk_previous(bi)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { + if (offset != *expOffsetPtr) { + log_err("FAIL: ubrk_previous loc \"%s\", expected %d, got %d, text \"%s\"\n", + itemPtr->locale, *expOffsetPtr, offset, textB); + } else { + log_verbose("[%d] @%d ubrk_previous()\n", (expOffsetPtr - expOffsetStart), offset); + } + } + if (offset != UBRK_DONE || *expOffsetPtr >= 0) { + log_err("FAIL: ubrk_previous loc \"%s\", expected UBRK_DONE & expOffset[%d] -1, got %d and %d, text \"%s\"\n", + itemPtr->locale, expOffsetPtr - expOffsetStart, offset, *expOffsetPtr, textB); + } + + expOffsetStart = expOffsetPtr = itemPtr->expRevOffsets; + start = ubrk_last(bi) - 1; + for (; (offset = ubrk_preceding(bi, start)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { + if (offset != *expOffsetPtr) { + log_err("FAIL: ubrk_preceding(%d) loc \"%s\", expected %d, got %d, text \"%s\"\n", + start, itemPtr->locale, *expOffsetPtr, offset, textB); + } + start = *expOffsetPtr - 1; + } + if (start >=0 && (offset != UBRK_DONE || *expOffsetPtr >= 0)) { + log_err("FAIL: ubrk_preceding loc(%d) \"%s\", expected UBRK_DONE & expOffset -1, got %d and %d, text \"%s\"\n", + start, itemPtr->locale, offset, *expOffsetPtr, textB); + } + + ubrk_close(bi); + } else { + log_data_err("FAIL: ubrk_open(UBRK_SENTENCE, \"%s\", ...) status %s (Are you missing data?)\n", + itemPtr->locale, u_errorName(status)); + } + } +} + + #endif /* #if !UCONFIG_NO_BREAK_ITERATION */