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