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