• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)) { \
39 log_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) { \
42 log_data_err("Test Failure at file %s, line %d (Are you missing data?)\n", __FILE__, __LINE__);}}
43 
44 #if !UCONFIG_NO_FILE_IO
45 static void TestBreakIteratorSafeClone(void);
46 #endif
47 static void TestBreakIteratorRules(void);
48 static void TestBreakIteratorRuleError(void);
49 static void TestBreakIteratorStatusVec(void);
50 static void TestBreakIteratorUText(void);
51 static void TestBreakIteratorTailoring(void);
52 static void TestBreakIteratorRefresh(void);
53 static void TestBug11665(void);
54 static void TestBreakIteratorSuppressions(void);
55 
56 void addBrkIterAPITest(TestNode** root);
57 
addBrkIterAPITest(TestNode ** root)58 void 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  */
84 typedef struct StringStruct {
85         struct StringStruct   *link;
86         UChar                 str[1];
87     } StringStruct;
88 
89 
toUChar(const char * src,void ** freeHook)90 static 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 
freeToUCharStrings(void ** hook)139 static 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
TestBreakIteratorCAPI()150 static 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     pos = 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     pos = 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 
TestBreakIteratorSafeClone(void)370 static 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 */
testOpenRules(char * rules)519 static 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  */
TestBreakIteratorRules()545 static 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 
TestBreakIteratorRuleError()632 static 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 */
TestBreakIteratorStatusVec()666 static 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  */
TestBreakIteratorUText(void)718 static 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 */
767 static 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 };*/
771 static 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 };*/
773 static const int32_t thTestOffs_thRev[] = {  9,      7,  6,  5,  4,  3,  2,  1,  0 };
774 
775 /* Hebrew line break tailoring, for cldrbug 3028 */
776 static 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 };*/
783 static 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 };*/
785 static const int32_t heTestOffs_heRev[] = {     19,     14, 12,  9,  7,  5,  1,  0 };
786 
787 /* Finnish line break tailoring, for cldrbug 3029.
788  * As of ICU 63, Finnish tailoring moved to root, Finnish and English should be the same. */
789 static const UChar fiTest[] = { /* 00 */ 0x0020, 0x002D, 0x0031, 0x0032, 0x0020,
790                                 /* 05 */ 0x0061, 0x002D, 0x006B, 0x0020,
791                                 /* 09 */ 0x0061, 0x0300, 0x2010, 0x006B, 0x0020,
792                                 /* 14 */ 0x0061, 0x0020, 0x002D, 0x006B, 0x0020,
793                                 /* 19 */ 0x0061, 0x0300, 0x0020, 0x2010, 0x006B, 0x0020, 0 };
794 //static const int32_t fiTestOffs_enFwd[] =  {  1,  5,  7,  9, 12, 14, 16, 17, 19, 22, 23, 25 };
795 static const int32_t fiTestOffs_enFwd[] =  {  1,  5,  7,  9, 12, 14, 16,     19, 22,     25 };
796 static const int32_t fiTestOffs_fiFwd[] =  {  1,  5,  7,  9, 12, 14, 16,     19, 22,     25 };
797 //static const int32_t fiTestOffs_enRev[] =  { 23, 22, 19, 17, 16, 14, 12,  9,  7,  5,  1,  0 };
798 static const int32_t fiTestOffs_enRev[] =  {     22, 19,     16, 14, 12,  9,  7,  5,  1,  0 };
799 static const int32_t fiTestOffs_fiRev[] =  {     22, 19,     16, 14, 12,  9,  7,  5,  1,  0 };
800 
801 /* Khmer dictionary-based work break, for ICU ticket #8329 */
802 static const UChar kmTest[] = { /* 00 */ 0x179F, 0x17BC, 0x1798, 0x1785, 0x17C6, 0x178E, 0x17B6, 0x1799, 0x1796, 0x17C1,
803                                 /* 10 */ 0x179B, 0x1794, 0x1793, 0x17D2, 0x178F, 0x17B7, 0x1785, 0x178A, 0x17BE, 0x1798,
804                                 /* 20 */ 0x17D2, 0x1794, 0x17B8, 0x17A2, 0x1792, 0x17B7, 0x179F, 0x17D2, 0x178B, 0x17B6,
805                                 /* 30 */ 0x1793, 0x17A2, 0x179A, 0x1796, 0x17D2, 0x179A, 0x17C7, 0x1782, 0x17BB, 0x178E,
806                                 /* 40 */ 0x178A, 0x179B, 0x17CB, 0x1796, 0x17D2, 0x179A, 0x17C7, 0x17A2, 0x1784, 0x17D2,
807                                 /* 50 */ 0x1782, 0 };
808 static const int32_t kmTestOffs_kmFwd[] =  {  3, /*8,*/ 11, 17, 23, 31, /*33,*/  40,  43, 51 }; /* TODO: Investigate failure to break at offset 8 */
809 static const int32_t kmTestOffs_kmRev[] =  { 43,  40,   /*33,*/ 31, 23, 17, 11, /*8,*/ 3,  0 };
810 
811 typedef struct {
812     const char * locale;
813     UBreakIteratorType type;
814     const UChar * test;
815     const int32_t * offsFwd;
816     const int32_t * offsRev;
817     int32_t numOffsets;
818 } RBBITailoringTest;
819 
820 static const RBBITailoringTest tailoringTests[] = {
821     { "en", UBRK_CHARACTER, thTest, thTestOffs_thFwd, thTestOffs_thRev, UPRV_LENGTHOF(thTestOffs_thFwd) },
822     { "en_US_POSIX", UBRK_CHARACTER, thTest, thTestOffs_thFwd, thTestOffs_thRev, UPRV_LENGTHOF(thTestOffs_thFwd) },
823     { "en", UBRK_LINE,      heTest, heTestOffs_heFwd, heTestOffs_heRev, UPRV_LENGTHOF(heTestOffs_heFwd) },
824     { "he", UBRK_LINE,      heTest, heTestOffs_heFwd, heTestOffs_heRev, UPRV_LENGTHOF(heTestOffs_heFwd) },
825     { "en", UBRK_LINE,      fiTest, fiTestOffs_enFwd, fiTestOffs_enRev, UPRV_LENGTHOF(fiTestOffs_enFwd) },
826     { "fi", UBRK_LINE,      fiTest, fiTestOffs_fiFwd, fiTestOffs_fiRev, UPRV_LENGTHOF(fiTestOffs_fiFwd) },
827     { "km", UBRK_WORD,      kmTest, kmTestOffs_kmFwd, kmTestOffs_kmRev, UPRV_LENGTHOF(kmTestOffs_kmFwd) },
828     { NULL, 0, NULL, NULL, NULL, 0 },
829 };
830 
TestBreakIteratorTailoring(void)831 static void TestBreakIteratorTailoring(void) {
832     const RBBITailoringTest * testPtr;
833     for (testPtr = tailoringTests; testPtr->locale != NULL; ++testPtr) {
834         UErrorCode status = U_ZERO_ERROR;
835         UBreakIterator* ubrkiter = ubrk_open(testPtr->type, testPtr->locale, testPtr->test, -1, &status);
836         if ( U_SUCCESS(status) ) {
837             int32_t offset, offsindx;
838             UBool foundError;
839 
840             foundError = FALSE;
841             for (offsindx = 0; (offset = ubrk_next(ubrkiter)) != UBRK_DONE; ++offsindx) {
842                 if (!foundError && offsindx >= testPtr->numOffsets) {
843                     log_err("FAIL: locale %s, break type %d, ubrk_next expected UBRK_DONE, got %d\n",
844                             testPtr->locale, testPtr->type, offset);
845                     foundError = TRUE;
846                 } else if (!foundError && offset != testPtr->offsFwd[offsindx]) {
847                     log_err("FAIL: locale %s, break type %d, ubrk_next expected %d, got %d\n",
848                             testPtr->locale, testPtr->type, testPtr->offsFwd[offsindx], offset);
849                     foundError = TRUE;
850                 }
851             }
852             if (!foundError && offsindx < testPtr->numOffsets) {
853                 log_err("FAIL: locale %s, break type %d, ubrk_next expected %d, got UBRK_DONE\n",
854                         testPtr->locale, testPtr->type, testPtr->offsFwd[offsindx]);
855             }
856 
857             foundError = FALSE;
858             for (offsindx = 0; (offset = ubrk_previous(ubrkiter)) != UBRK_DONE; ++offsindx) {
859                 if (!foundError && offsindx >= testPtr->numOffsets) {
860                     log_err("FAIL: locale %s, break type %d, ubrk_previous expected UBRK_DONE, got %d\n",
861                             testPtr->locale, testPtr->type, offset);
862                     foundError = TRUE;
863                 } else if (!foundError && offset != testPtr->offsRev[offsindx]) {
864                     log_err("FAIL: locale %s, break type %d, ubrk_previous expected %d, got %d\n",
865                             testPtr->locale, testPtr->type, testPtr->offsRev[offsindx], offset);
866                     foundError = TRUE;
867                 }
868             }
869             if (!foundError && offsindx < testPtr->numOffsets) {
870                 log_err("FAIL: locale %s, break type %d, ubrk_previous expected %d, got UBRK_DONE\n",
871                         testPtr->locale, testPtr->type, testPtr->offsRev[offsindx]);
872             }
873 
874             ubrk_close(ubrkiter);
875         } else {
876             log_err_status(status, "FAIL: locale %s, break type %d, ubrk_open status: %s\n", testPtr->locale, testPtr->type, u_errorName(status));
877         }
878     }
879 }
880 
881 
TestBreakIteratorRefresh(void)882 static void TestBreakIteratorRefresh(void) {
883     /*
884      *  RefreshInput changes out the input of a Break Iterator without
885      *    changing anything else in the iterator's state.  Used with Java JNI,
886      *    when Java moves the underlying string storage.   This test
887      *    runs a ubrk_next() repeatedly, moving the text in the middle of the sequence.
888      *    The right set of boundaries should still be found.
889      */
890     UChar testStr[]  = {0x20, 0x41, 0x20, 0x42, 0x20, 0x43, 0x20, 0x44, 0x0};  /* = " A B C D"  */
891     UChar movedStr[] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,  0};
892     UErrorCode status = U_ZERO_ERROR;
893     UBreakIterator *bi;
894     UText ut1 = UTEXT_INITIALIZER;
895     UText ut2 = UTEXT_INITIALIZER;
896 
897     bi = ubrk_open(UBRK_LINE, "en_US", NULL, 0, &status);
898     TEST_ASSERT_SUCCESS(status);
899     if (U_FAILURE(status)) {
900         return;
901     }
902 
903     utext_openUChars(&ut1, testStr, -1, &status);
904     TEST_ASSERT_SUCCESS(status);
905     ubrk_setUText(bi, &ut1, &status);
906     TEST_ASSERT_SUCCESS(status);
907 
908     if (U_SUCCESS(status)) {
909         /* Line boundaries will occur before each letter in the original string */
910         TEST_ASSERT(1 == ubrk_next(bi));
911         TEST_ASSERT(3 == ubrk_next(bi));
912 
913         /* Move the string, kill the original string.  */
914         u_strcpy(movedStr, testStr);
915         u_memset(testStr, 0x20, u_strlen(testStr));
916         utext_openUChars(&ut2, movedStr, -1, &status);
917         TEST_ASSERT_SUCCESS(status);
918         ubrk_refreshUText(bi, &ut2, &status);
919         TEST_ASSERT_SUCCESS(status);
920 
921         /* Find the following matches, now working in the moved string. */
922         TEST_ASSERT(5 == ubrk_next(bi));
923         TEST_ASSERT(7 == ubrk_next(bi));
924         TEST_ASSERT(8 == ubrk_next(bi));
925         TEST_ASSERT(UBRK_DONE == ubrk_next(bi));
926         TEST_ASSERT_SUCCESS(status);
927 
928         utext_close(&ut1);
929         utext_close(&ut2);
930     }
931     ubrk_close(bi);
932 }
933 
934 
TestBug11665(void)935 static void TestBug11665(void) {
936     // The problem was with the incorrect breaking of Japanese text beginning
937     // with Katakana characters when no prior Japanese or Chinese text had been
938     // encountered.
939     //
940     // Tested here in cintltst, rather than in intltest, because only cintltst
941     // tests have the ability to reset ICU, which is needed to get the bug
942     // to manifest itself.
943 
944     static UChar japaneseText[] = {0x30A2, 0x30EC, 0x30EB, 0x30AE, 0x30FC, 0x6027, 0x7D50, 0x819C, 0x708E};
945     int32_t boundaries[10] = {0};
946     UBreakIterator *bi = NULL;
947     int32_t brk;
948     int32_t brkIdx = 0;
949     int32_t totalBreaks = 0;
950     UErrorCode status = U_ZERO_ERROR;
951 
952     ctest_resetICU();
953     bi = ubrk_open(UBRK_WORD, "en_US", japaneseText, UPRV_LENGTHOF(japaneseText), &status);
954     TEST_ASSERT_SUCCESS(status);
955     if (!bi) {
956         return;
957     }
958     for (brk=ubrk_first(bi); brk != UBRK_DONE; brk=ubrk_next(bi)) {
959         boundaries[brkIdx] = brk;
960         if (++brkIdx >= UPRV_LENGTHOF(boundaries) - 1) {
961             break;
962         }
963     }
964     if (brkIdx <= 2 || brkIdx >= UPRV_LENGTHOF(boundaries)) {
965         log_err("%s:%d too few or many breaks found.\n", __FILE__, __LINE__);
966     } else {
967         totalBreaks = brkIdx;
968         brkIdx = 0;
969         for (brk=ubrk_first(bi); brk != UBRK_DONE; brk=ubrk_next(bi)) {
970             if (brk != boundaries[brkIdx]) {
971                 log_err("%s:%d Break #%d differs between first and second iteration.\n", __FILE__, __LINE__, brkIdx);
972                 break;
973             }
974             if (++brkIdx >= UPRV_LENGTHOF(boundaries) - 1) {
975                 log_err("%s:%d Too many breaks.\n", __FILE__, __LINE__);
976                 break;
977             }
978         }
979         if (totalBreaks != brkIdx) {
980             log_err("%s:%d Number of breaks differ between first and second iteration.\n", __FILE__, __LINE__);
981         }
982     }
983     ubrk_close(bi);
984 }
985 
986 /*
987  * expOffset is the set of expected offsets, ending with '-1'.
988  * "Expected expOffset -1" means "expected the end of the offsets"
989  */
990 
991 static const char testSentenceSuppressionsEn[]  = "Mr. Jones comes home. Dr. Smith Ph.D. is out. In the U.S.A. it is hot.";
992 static const int32_t testSentSuppFwdOffsetsEn[] = { 22, 26, 46, 70, -1 };     /* With suppressions, currently not handling Dr. */
993 static const int32_t testSentFwdOffsetsEn[]     = {  4, 22, 26, 46, 70, -1 }; /* Without suppressions */
994 static const int32_t testSentSuppRevOffsetsEn[] = { 46, 26, 22,  0, -1 };     /* With suppressions, currently not handling Dr.  */
995 static const int32_t testSentRevOffsetsEn[]     = { 46, 26, 22,  4,  0, -1 }; /* Without suppressions */
996 
997 static const char testSentenceSuppressionsDe[]  = "Wenn ich schon h\\u00F6re zu Guttenberg kommt evtl. zur\\u00FCck.";
998 static const int32_t testSentSuppFwdOffsetsDe[] = { 53, -1 };       /* With suppressions */
999 static const int32_t testSentFwdOffsetsDe[]     = { 53, -1 };       /* Without suppressions; no break in evtl. zur due to casing */
1000 static const int32_t testSentSuppRevOffsetsDe[] = {  0, -1 };       /* With suppressions */
1001 static const int32_t testSentRevOffsetsDe[]     = {  0, -1 };       /* Without suppressions */
1002 
1003 static const char testSentenceSuppressionsEs[]  = "Te esperamos todos los miercoles en Bravo 416, Col. El Pueblo a las 7 PM.";
1004 static const int32_t testSentSuppFwdOffsetsEs[] = { 73, -1 };       /* With suppressions */
1005 static const int32_t testSentFwdOffsetsEs[]     = { 52, 73, -1 };   /* Without suppressions */
1006 static const int32_t testSentSuppRevOffsetsEs[] = {  0, -1 };       /* With suppressions */
1007 static const int32_t testSentRevOffsetsEs[]     = { 52,  0, -1 };   /* Without suppressions */
1008 
1009 enum { kTextULenMax = 128 };
1010 
1011 typedef struct {
1012     const char * locale;
1013     const char * text;
1014     const int32_t * expFwdOffsets;
1015     const int32_t * expRevOffsets;
1016 } TestBISuppressionsItem;
1017 
1018 static const TestBISuppressionsItem testBISuppressionsItems[] = {
1019     { "en@ss=standard", testSentenceSuppressionsEn, testSentSuppFwdOffsetsEn, testSentSuppRevOffsetsEn },
1020     { "en",             testSentenceSuppressionsEn, testSentFwdOffsetsEn,     testSentRevOffsetsEn     },
1021     { "en_CA",             testSentenceSuppressionsEn, testSentFwdOffsetsEn,     testSentRevOffsetsEn     },
1022     { "en_CA@ss=standard", testSentenceSuppressionsEn, testSentSuppFwdOffsetsEn, testSentSuppRevOffsetsEn },
1023     { "fr@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn,     testSentRevOffsetsEn     },
1024     { "af@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn,     testSentRevOffsetsEn     }, /* no brkiter data => nosuppressions? */
1025     { "af_ZA@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn,     testSentRevOffsetsEn     }, /* no brkiter data => nosuppressions? */
1026     { "zh@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn,     testSentRevOffsetsEn     }, /* brkiter data, no suppressions data => no suppressions */
1027     { "zh_Hant@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn    }, /* brkiter data, no suppressions data => no suppressions */
1028     { "fi@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn,     testSentRevOffsetsEn     }, /* brkiter data, no suppressions data => no suppressions */
1029     { "ja@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn,     testSentRevOffsetsEn     }, /* brkiter data, no suppressions data => no suppressions */
1030     { "de@ss=standard", testSentenceSuppressionsDe, testSentSuppFwdOffsetsDe, testSentSuppRevOffsetsDe },
1031     { "de",             testSentenceSuppressionsDe, testSentFwdOffsetsDe,     testSentRevOffsetsDe     },
1032     { "es@ss=standard", testSentenceSuppressionsEs, testSentSuppFwdOffsetsEs, testSentSuppRevOffsetsEs },
1033     { "es",             testSentenceSuppressionsEs, testSentFwdOffsetsEs,     testSentRevOffsetsEs     },
1034     { NULL, NULL, NULL }
1035 };
1036 
TestBreakIteratorSuppressions(void)1037 static void TestBreakIteratorSuppressions(void) {
1038     const TestBISuppressionsItem * itemPtr;
1039 
1040     for (itemPtr = testBISuppressionsItems; itemPtr->locale != NULL; itemPtr++) {
1041         UChar textU[kTextULenMax];
1042         int32_t textULen = u_unescape(itemPtr->text, textU, kTextULenMax);
1043         UErrorCode status = U_ZERO_ERROR;
1044         UBreakIterator *bi = ubrk_open(UBRK_SENTENCE, itemPtr->locale, textU, textULen, &status);
1045         log_verbose("#%d: %s\n", (itemPtr-testBISuppressionsItems), itemPtr->locale);
1046         if (U_SUCCESS(status)) {
1047             int32_t offset, start;
1048             const int32_t * expOffsetPtr;
1049             const int32_t * expOffsetStart;
1050 
1051             expOffsetStart = expOffsetPtr = itemPtr->expFwdOffsets;
1052             ubrk_first(bi);
1053             for (; (offset = ubrk_next(bi)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) {
1054                 if (offset != *expOffsetPtr) {
1055                     log_err("FAIL: ubrk_next loc \"%s\", expected %d, got %d\n", itemPtr->locale, *expOffsetPtr, offset);
1056                 }
1057             }
1058             if (offset != UBRK_DONE || *expOffsetPtr >= 0) {
1059                 log_err("FAIL: ubrk_next loc \"%s\", expected UBRK_DONE & expOffset -1, got %d and %d\n", itemPtr->locale, offset, *expOffsetPtr);
1060             }
1061 
1062             expOffsetStart = expOffsetPtr = itemPtr->expFwdOffsets;
1063             start = ubrk_first(bi) + 1;
1064             for (; (offset = ubrk_following(bi, start)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) {
1065                 if (offset != *expOffsetPtr) {
1066                     log_err("FAIL: ubrk_following(%d) loc \"%s\", expected %d, got %d\n", start, itemPtr->locale, *expOffsetPtr, offset);
1067                 }
1068                 start = *expOffsetPtr + 1;
1069             }
1070             if (offset != UBRK_DONE || *expOffsetPtr >= 0) {
1071                 log_err("FAIL: ubrk_following(%d) loc \"%s\", expected UBRK_DONE & expOffset -1, got %d and %d\n", start, itemPtr->locale, offset, *expOffsetPtr);
1072             }
1073 
1074             expOffsetStart = expOffsetPtr = itemPtr->expRevOffsets;
1075             offset = ubrk_last(bi);
1076             log_verbose("___ @%d ubrk_last\n", offset);
1077             if(offset == 0) {
1078               log_err("FAIL: ubrk_last loc \"%s\" unexpected %d\n", itemPtr->locale, offset);
1079             }
1080             for (; (offset = ubrk_previous(bi)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) {
1081                 if (offset != *expOffsetPtr) {
1082                     log_err("FAIL: ubrk_previous loc \"%s\", expected %d, got %d\n", itemPtr->locale, *expOffsetPtr, offset);
1083                 } else {
1084                     log_verbose("[%d] @%d ubrk_previous()\n", (expOffsetPtr - expOffsetStart), offset);
1085                 }
1086             }
1087             if (offset != UBRK_DONE || *expOffsetPtr >= 0) {
1088                 log_err("FAIL: ubrk_previous loc \"%s\", expected UBRK_DONE & expOffset[%d] -1, got %d and %d\n", itemPtr->locale,
1089                         expOffsetPtr - expOffsetStart,
1090                         offset, *expOffsetPtr);
1091             }
1092 
1093             expOffsetStart = expOffsetPtr = itemPtr->expRevOffsets;
1094             start = ubrk_last(bi) - 1;
1095             for (; (offset = ubrk_preceding(bi, start)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) {
1096                 if (offset != *expOffsetPtr) {
1097                     log_err("FAIL: ubrk_preceding(%d) loc \"%s\", expected %d, got %d\n", start, itemPtr->locale, *expOffsetPtr, offset);
1098                 }
1099                 start = *expOffsetPtr - 1;
1100             }
1101             if (start >=0 && (offset != UBRK_DONE || *expOffsetPtr >= 0)) {
1102                 log_err("FAIL: ubrk_preceding loc(%d) \"%s\", expected UBRK_DONE & expOffset -1, got %d and %d\n", start, itemPtr->locale, offset, *expOffsetPtr);
1103             }
1104 
1105             ubrk_close(bi);
1106         } else {
1107             log_data_err("FAIL: ubrk_open(UBRK_SENTENCE, \"%s\", ...) status %s (Are you missing data?)\n", itemPtr->locale, u_errorName(status));
1108         }
1109     }
1110 }
1111 
1112 
1113 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */
1114