1 /********************************************************************
3 * Copyright (c) 1997-2003, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
10 * Modification History:
12 * Madhu Katragadda Creation
13 *********************************************************************************/
14 /*C API TEST FOR BREAKITERATOR */
16 * This is an API test. It doesn't test very many cases, and doesn't
17 * try to test the full functionality. It just calls each function in the class and
18 * verifies that it works on a basic level.
21 #include "unicode/utypes.h"
23 #if !UCONFIG_NO_BREAK_ITERATION
26 #include "unicode/uloc.h"
27 #include "unicode/ubrk.h"
28 #include "unicode/ustring.h"
29 #include "unicode/ucnv.h"
33 static void TestBreakIteratorSafeClone(void);
34 static void TestBreakIteratorRules(void);
35 static void TestBreakIteratorRuleError(void);
37 void addBrkIterAPITest(TestNode
** root
);
39 void addBrkIterAPITest(TestNode
** root
)
41 addTest(root
, &TestBreakIteratorCAPI
, "tstxtbd/cbiapts/TestBreakIteratorCAPI");
42 addTest(root
, &TestBreakIteratorSafeClone
, "tstxtbd/cbiapts/TestBreakIteratorSafeClone");
43 addTest(root
, &TestBreakIteratorRules
, "tstxtbd/cbiapts/TestBreakIteratorRules");
44 addTest(root
, &TestBreakIteratorRuleError
, "tstxtbd/cbiapts/TestBreakIteratorRuleError");
47 #define CLONETEST_ITERATOR_COUNT 2
50 * Utility function for converting char * to UChar * strings, to
51 * simplify the test code. Converted strings are put in heap allocated
52 * storage. A hook (probably a local in the caller's code) allows all
53 * strings converted with that hook to be freed with a single call.
55 typedef struct StringStruct
{
56 struct StringStruct
*link
;
61 static UChar
* toUChar(const char *src
, void **freeHook
) {
62 /* Structure of the memory that we allocate on the heap */
66 UChar stackBuf
[2000 + sizeof(void *)/sizeof(UChar
)];
70 UErrorCode status
= U_ZERO_ERROR
;
75 cnv
= ucnv_open(NULL
, &status
);
76 if(U_FAILURE(status
) || cnv
== NULL
) {
80 numUChars
= ucnv_toUChars(cnv
,
86 destSize
= (numUChars
+1) * sizeof(UChar
) + sizeof(struct StringStruct
);
87 dest
= (StringStruct
*)malloc(destSize
);
89 if (status
== U_BUFFER_OVERFLOW_ERROR
|| status
== U_STRING_NOT_TERMINATED_WARNING
) {
90 ucnv_toUChars(cnv
, dest
->str
, numUChars
+1, src
, -1, &status
);
91 } else if (status
== U_ZERO_ERROR
) {
92 u_strcpy(dest
->str
, stackBuf
);
99 ucnv_reset(cnv
); /* be good citizens */
105 dest
->link
= (StringStruct
*)(*freeHook
);
110 static void freeToUCharStrings(void **hook
) {
111 StringStruct
*s
= *(StringStruct
**)hook
;
113 StringStruct
*next
= s
->link
;
120 static void TestBreakIteratorCAPI()
122 UErrorCode status
= U_ZERO_ERROR
;
123 UBreakIterator
*word
, *sentence
, *line
, *character
, *b
, *bogus
;
124 int32_t start
,pos
,end
,to
;
130 /* Note: the adjacent "" are concatenating strings, not adding a \" to the
131 string, which is probably what whoever wrote this intended. Don't fix,
132 because it would throw off the hard coded break positions in the following
134 u_uastrcpy(text
, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah");
138 log_verbose("\nTesting BreakIterator open functions\n");
140 /* Use french for fun */
141 word
= ubrk_open(UBRK_WORD
, "en_US", text
, u_strlen(text
), &status
);
142 if(status
== U_FILE_ACCESS_ERROR
) {
143 log_data_err("Check your data - it doesn't seem to be around\n");
145 } else if(U_FAILURE(status
)){
146 log_err("FAIL: Error in ubrk_open() for word breakiterator: %s\n", myErrorName(status
));
149 log_verbose("PASS: Successfully opened word breakiterator\n");
152 sentence
= ubrk_open(UBRK_SENTENCE
, "en_US", text
, u_strlen(text
), &status
);
153 if(U_FAILURE(status
)){
154 log_err("FAIL: Error in ubrk_open() for sentence breakiterator: %s\n", myErrorName(status
));
157 log_verbose("PASS: Successfully opened sentence breakiterator\n");
160 line
= ubrk_open(UBRK_LINE
, "en_US", text
, u_strlen(text
), &status
);
161 if(U_FAILURE(status
)){
162 log_err("FAIL: Error in ubrk_open() for line breakiterator: %s\n", myErrorName(status
));
165 log_verbose("PASS: Successfully opened line breakiterator\n");
168 character
= ubrk_open(UBRK_CHARACTER
, "en_US", text
, u_strlen(text
), &status
);
169 if(U_FAILURE(status
)){
170 log_err("FAIL: Error in ubrk_open() for character breakiterator: %s\n", myErrorName(status
));
173 log_verbose("PASS: Successfully opened character breakiterator\n");
175 /*trying to open an illegal iterator*/
176 bogus
= ubrk_open((UBreakIteratorType
)5, "en_US", text
, u_strlen(text
), &status
);
177 if(U_SUCCESS(status
)){
178 log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_MEMORY_ALLOCATION_ERROR\n");
180 if(U_FAILURE(status
)){
181 if(status
!= U_MEMORY_ALLOCATION_ERROR
){
182 log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_MEMORY_ALLOCATION_ERROR\n Got %s\n", myErrorName(status
));
188 /* ======= Test ubrk_countAvialable() and ubrk_getAvialable() */
190 log_verbose("\nTesting ubrk_countAvailable() and ubrk_getAvailable()\n");
191 count
=ubrk_countAvailable();
192 /* use something sensible w/o hardcoding the count */
194 log_err("FAIL: Error in ubrk_countAvialable() returned %d\n", count
);
197 log_verbose("PASS: ubrk_countAvialable() successful returned %d\n", count
);
201 log_verbose("%s\n", ubrk_getAvailable(i
));
202 if (ubrk_getAvailable(i
) == 0)
203 log_err("No locale for which breakiterator is applicable\n");
205 log_verbose("A locale %s for which breakiterator is applicable\n",ubrk_getAvailable(i
));
208 /*========Test ubrk_first(), ubrk_last()...... and other functions*/
210 log_verbose("\nTesting the functions for word\n");
211 start
= ubrk_first(word
);
213 log_err("error ubrk_start(word) did not return 0\n");
214 log_verbose("first (word = %d\n", (int32_t)start
);
217 log_err("error ubrk_next(word) did not return 4\n");
218 log_verbose("next (word = %d\n", (int32_t)pos
);
219 pos
=ubrk_following(word
, 4);
221 log_err("error ubrl_following(word,4) did not return 6\n");
222 log_verbose("next (word = %d\n", (int32_t)pos
);
225 log_err("error ubrk_last(word) did not return 49\n");
226 log_verbose("last (word = %d\n", (int32_t)end
);
228 pos
=ubrk_previous(word
);
229 log_verbose("%d %d\n", end
, pos
);
231 pos
=ubrk_previous(word
);
232 log_verbose("%d \n", pos
);
234 if (ubrk_isBoundary(word
, 2) != FALSE
) {
235 log_err("error ubrk_isBoundary(word, 2) did not return FALSE\n");
237 pos
=ubrk_current(word
);
239 log_err("error ubrk_current() != 4 after ubrk_isBoundary(word, 2)\n");
241 if (ubrk_isBoundary(word
, 4) != TRUE
) {
242 log_err("error ubrk_isBoundary(word, 4) did not return TRUE\n");
247 log_verbose("\nTesting the functions for character\n");
248 ubrk_first(character
);
249 pos
= ubrk_following(character
, 5);
251 log_err("error ubrk_following(character,5) did not return 6\n");
252 log_verbose("Following (character,5) = %d\n", (int32_t)pos
);
253 pos
=ubrk_following(character
, 18);
255 log_err("error ubrk_following(character,18) did not return 19\n");
256 log_verbose("Followingcharacter,18) = %d\n", (int32_t)pos
);
257 pos
=ubrk_preceding(character
, 22);
259 log_err("error ubrk_preceding(character,22) did not return 21\n");
260 log_verbose("preceding(character,22) = %d\n", (int32_t)pos
);
263 log_verbose("\nTesting the functions for line\n");
264 pos
=ubrk_first(line
);
266 log_err("error ubrk_first(line) returned %d, expected 0\n", (int32_t)pos
);
267 pos
= ubrk_next(line
);
268 pos
=ubrk_following(line
, 18);
270 log_err("error ubrk_following(line) did not return 22\n");
271 log_verbose("following (line) = %d\n", (int32_t)pos
);
274 log_verbose("\nTesting the functions for sentence\n");
275 ubrk_first(sentence
);
276 pos
= ubrk_current(sentence
);
277 log_verbose("Current(sentence) = %d\n", (int32_t)pos
);
278 pos
= ubrk_last(sentence
);
280 log_err("error ubrk_last for sentence did not return 49\n");
281 log_verbose("Last (sentence) = %d\n", (int32_t)pos
);
282 ubrk_first(sentence
);
283 to
= ubrk_following( sentence
, 0 );
284 if (to
== 0) log_err("ubrk_following returned 0\n");
285 to
= ubrk_preceding( sentence
, to
);
286 if (to
!= 0) log_err("ubrk_preceding didn't return 0\n");
287 if (ubrk_first(sentence
)!=ubrk_current(sentence
)) {
288 log_err("error in ubrk_first() or ubrk_current()\n");
293 /*Testing ubrk_open and ubrk_close()*/
294 log_verbose("\nTesting open and close for us locale\n");
295 b
= ubrk_open(UBRK_WORD
, "fr_FR", text
, u_strlen(text
), &status
);
296 if (U_FAILURE(status
)) {
297 log_err("ubrk_open for word returned NULL: %s\n", myErrorName(status
));
302 ubrk_close(sentence
);
304 ubrk_close(character
);
307 static void TestBreakIteratorSafeClone(void)
309 UChar text
[51]; /* Keep this odd to test for 64-bit memory alignment */
310 /* NOTE: This doesn't reliably force mis-alignment of following items. */
311 uint8_t buffer
[CLONETEST_ITERATOR_COUNT
] [U_BRK_SAFECLONE_BUFFERSIZE
];
312 int32_t bufferSize
= U_BRK_SAFECLONE_BUFFERSIZE
;
314 UBreakIterator
* someIterators
[CLONETEST_ITERATOR_COUNT
];
315 UBreakIterator
* someClonedIterators
[CLONETEST_ITERATOR_COUNT
];
317 UBreakIterator
* brk
;
318 UErrorCode status
= U_ZERO_ERROR
;
322 /*Testing ubrk_safeClone */
324 /* Note: the adjacent "" are concatenating strings, not adding a \" to the
325 string, which is probably what whoever wrote this intended. Don't fix,
326 because it would throw off the hard coded break positions in the following
328 u_uastrcpy(text
, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah");
330 /* US & Thai - rule-based & dictionary based */
331 someIterators
[0] = ubrk_open(UBRK_WORD
, "en_US", text
, u_strlen(text
), &status
);
332 if(!someIterators
[0] || U_FAILURE(status
)) {
333 log_data_err("Couldn't open en_US word break iterator - %s\n", u_errorName(status
));
337 someIterators
[1] = ubrk_open(UBRK_WORD
, "th_TH", text
, u_strlen(text
), &status
);
338 if(!someIterators
[1] || U_FAILURE(status
)) {
339 log_data_err("Couldn't open th_TH word break iterator - %s\n", u_errorName(status
));
343 /* test each type of iterator */
344 for (i
= 0; i
< CLONETEST_ITERATOR_COUNT
; i
++)
347 /* Check the various error & informational states */
349 /* Null status - just returns NULL */
350 if (0 != ubrk_safeClone(someIterators
[i
], buffer
[i
], &bufferSize
, 0))
352 log_err("FAIL: Cloned Iterator failed to deal correctly with null status\n");
354 /* error status - should return 0 & keep error the same */
355 status
= U_MEMORY_ALLOCATION_ERROR
;
356 if (0 != ubrk_safeClone(someIterators
[i
], buffer
[i
], &bufferSize
, &status
) || status
!= U_MEMORY_ALLOCATION_ERROR
)
358 log_err("FAIL: Cloned Iterator failed to deal correctly with incoming error status\n");
360 status
= U_ZERO_ERROR
;
362 /* Null buffer size pointer - just returns NULL & set error to U_ILLEGAL_ARGUMENT_ERROR*/
363 if (0 != ubrk_safeClone(someIterators
[i
], buffer
[i
], 0, &status
) || status
!= U_ILLEGAL_ARGUMENT_ERROR
)
365 log_err("FAIL: Cloned Iterator failed to deal correctly with null bufferSize pointer\n");
367 status
= U_ZERO_ERROR
;
369 /* buffer size pointer is 0 - fill in pbufferSize with a size */
371 if (0 != ubrk_safeClone(someIterators
[i
], buffer
[i
], &bufferSize
, &status
) || U_FAILURE(status
) || bufferSize
<= 0)
373 log_err("FAIL: Cloned Iterator failed a sizing request ('preflighting')\n");
375 /* Verify our define is large enough */
376 if (U_BRK_SAFECLONE_BUFFERSIZE
< bufferSize
)
378 log_err("FAIL: Pre-calculated buffer size is too small\n");
380 /* Verify we can use this run-time calculated size */
381 if (0 == (brk
= ubrk_safeClone(someIterators
[i
], buffer
[i
], &bufferSize
, &status
)) || U_FAILURE(status
))
383 log_err("FAIL: Iterator can't be cloned with run-time size\n");
387 /* size one byte too small - should allocate & let us know */
389 if (0 == (brk
= ubrk_safeClone(someIterators
[i
], 0, &bufferSize
, &status
)) || status
!= U_SAFECLONE_ALLOCATED_WARNING
)
391 log_err("FAIL: Cloned Iterator failed to deal correctly with too-small buffer size\n");
395 status
= U_ZERO_ERROR
;
396 bufferSize
= U_BRK_SAFECLONE_BUFFERSIZE
;
398 /* Null buffer pointer - return Iterator & set error to U_SAFECLONE_ALLOCATED_ERROR */
399 if (0 == (brk
= ubrk_safeClone(someIterators
[i
], 0, &bufferSize
, &status
)) || status
!= U_SAFECLONE_ALLOCATED_WARNING
)
401 log_err("FAIL: Cloned Iterator failed to deal correctly with null buffer pointer\n");
405 status
= U_ZERO_ERROR
;
407 /* Mis-aligned buffer pointer. */
409 char stackBuf
[U_BRK_SAFECLONE_BUFFERSIZE
+sizeof(void *)];
413 brk
= ubrk_safeClone(someIterators
[i
], &stackBuf
[1], &bufferSize
, &status
);
414 if (U_FAILURE(status
) || brk
== 0) {
415 log_err("FAIL: Cloned Iterator failed with misaligned buffer pointer\n");
417 if (status
== U_SAFECLONE_ALLOCATED_WARNING
) {
418 log_err("FAIL: Cloned Iterator allocated when using a mis-aligned buffer.\n");
420 offset
= (int32_t)((char *)&p
-(char*)brk
);
424 if (offset
% sizeof(void *) != 0) {
425 log_err("FAIL: Cloned Iterator failed to align correctly with misaligned buffer pointer\n");
432 /* Null Iterator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
433 if (0 != ubrk_safeClone(0, buffer
[i
], &bufferSize
, &status
) || status
!= U_ILLEGAL_ARGUMENT_ERROR
)
435 log_err("FAIL: Cloned Iterator failed to deal correctly with null Iterator pointer\n");
437 status
= U_ZERO_ERROR
;
439 /* Do these cloned Iterators work at all - make a first & next call */
440 bufferSize
= U_BRK_SAFECLONE_BUFFERSIZE
;
441 someClonedIterators
[i
] = ubrk_safeClone(someIterators
[i
], buffer
[i
], &bufferSize
, &status
);
443 start
= ubrk_first(someClonedIterators
[i
]);
445 log_err("error ubrk_start(clone) did not return 0\n");
446 pos
=ubrk_next(someClonedIterators
[i
]);
448 log_err("error ubrk_next(clone) did not return 4\n");
450 ubrk_close(someClonedIterators
[i
]);
451 ubrk_close(someIterators
[i
]);
457 // Open a break iterator from char * rules. Take care of conversion
458 // of the rules and error checking.
460 static UBreakIterator
* testOpenRules(char *rules
) {
461 UErrorCode status
= U_ZERO_ERROR
;
462 UChar
*ruleSourceU
= NULL
;
463 void *strCleanUp
= NULL
;
464 UParseError parseErr
;
467 ruleSourceU
= toUChar(rules
, &strCleanUp
);
469 bi
= ubrk_openRules(ruleSourceU
, -1, /* The rules */
470 NULL
, -1, /* The text to be iterated over. */
473 if (U_FAILURE(status
)) {
474 log_err("FAIL: ubrk_openRules: ICU Error \"%s\"\n", u_errorName(status
));
477 freeToUCharStrings(&strCleanUp
);
483 * TestBreakIteratorRules - Verify that a break iterator can be created from
484 * a set of source rules.
486 static void TestBreakIteratorRules() {
487 /* Rules will keep together any run of letters not including 'a', OR
488 * keep together 'abc', but only when followed by 'def', OTHERWISE
489 * just return one char at a time.
491 char rules
[] = "abc{666}/def;\n [\\p{L} - [a]]* {2}; . {1};";
492 /* 0123456789012345678 */
493 char data
[] = "abcdex abcdefgh-def"; /* the test data string */
494 char breaks
[] = "** ** * ** *"; /* * the expected break positions */
495 char tags
[] = "01 21 6 21 2"; /* expected tag values at break positions */
496 int32_t tagMap
[] = {0, 1, 2, 3, 4, 5, 666};
499 void *freeHook
= NULL
;
500 UErrorCode status
= U_ZERO_ERROR
;
504 UBreakIterator
*bi
= testOpenRules(rules
);
505 if (bi
== NULL
) {return;}
506 uData
= toUChar(data
, &freeHook
);
507 ubrk_setText(bi
, uData
, -1, &status
);
509 pos
= ubrk_first(bi
);
510 for (i
=0; i
<sizeof(breaks
); i
++) {
511 if (pos
== i
&& breaks
[i
] != '*') {
512 log_err("FAIL: unexpected break at position %d found\n", pos
);
515 if (pos
!= i
&& breaks
[i
] == '*') {
516 log_err("FAIL: expected break at position %d not found.\n", i
);
520 int32_t tag
, expectedTag
;
521 tag
= ubrk_getRuleStatus(bi
);
522 expectedTag
= tagMap
[tags
[i
]&0xf];
523 if (tag
!= expectedTag
) {
524 log_err("FAIL: incorrect tag value. Position = %d; expected tag %d, got %d",
525 pos
, expectedTag
, tag
);
532 freeToUCharStrings(&freeHook
);
536 static void TestBreakIteratorRuleError() {
538 * TestBreakIteratorRuleError - Try to create a BI from rules with syntax errors,
539 * check that the error is reported correctly.
541 char rules
[] = " # This is a rule comment on line 1\n"
542 "[:L:]; # this rule is OK.\n"
543 "abcdefg); # Error, mismatched parens\n";
545 void *freeHook
= NULL
;
546 UErrorCode status
= U_ZERO_ERROR
;
547 UParseError parseErr
;
550 uRules
= toUChar(rules
, &freeHook
);
551 bi
= ubrk_openRules(uRules
, -1, /* The rules */
552 NULL
, -1, /* The text to be iterated over. */
554 if (U_SUCCESS(status
)) {
555 log_err("FAIL: construction of break iterator succeeded when it should have failed.\n");
558 if (parseErr
.line
!= 3 || parseErr
.offset
!= 8) {
559 log_err("FAIL: incorrect error position reported. Got line %d, char %d, expected line 3, char 7",
560 parseErr
.line
, parseErr
.offset
);
563 freeToUCharStrings(&freeHook
);
566 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */