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