1 /********************************************************************
3 * Copyright (c) 1997-2004, 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
27 #include "unicode/uloc.h"
28 #include "unicode/ubrk.h"
29 #include "unicode/ustring.h"
30 #include "unicode/ucnv.h"
34 #define TEST_ASSET_SUCCESS(status) {if (U_FAILURE(status)) { \
35 log_err("Failure at file %s, line %d, error = %s\n", __FILE__, __LINE__, u_errorName(status));}}
37 #define TEST_ASSERT(expr) {if ((expr)==FALSE) { \
38 log_err("Test Failure at file %s, line %d\n", __FILE__, __LINE__);}}
40 static void TestBreakIteratorSafeClone(void);
41 static void TestBreakIteratorRules(void);
42 static void TestBreakIteratorRuleError(void);
43 static void TestBreakIteratorStatusVec(void);
45 void addBrkIterAPITest(TestNode
** root
);
47 void addBrkIterAPITest(TestNode
** root
)
49 addTest(root
, &TestBreakIteratorCAPI
, "tstxtbd/cbiapts/TestBreakIteratorCAPI");
50 addTest(root
, &TestBreakIteratorSafeClone
, "tstxtbd/cbiapts/TestBreakIteratorSafeClone");
51 addTest(root
, &TestBreakIteratorRules
, "tstxtbd/cbiapts/TestBreakIteratorRules");
52 addTest(root
, &TestBreakIteratorRuleError
, "tstxtbd/cbiapts/TestBreakIteratorRuleError");
53 addTest(root
, &TestBreakIteratorStatusVec
, "tstxtbd/cbiapts/TestBreakIteratorStatusVec");
56 #define CLONETEST_ITERATOR_COUNT 2
59 * Utility function for converting char * to UChar * strings, to
60 * simplify the test code. Converted strings are put in heap allocated
61 * storage. A hook (probably a local in the caller's code) allows all
62 * strings converted with that hook to be freed with a single call.
64 typedef struct StringStruct
{
65 struct StringStruct
*link
;
70 static UChar
* toUChar(const char *src
, void **freeHook
) {
71 /* Structure of the memory that we allocate on the heap */
75 UChar stackBuf
[2000 + sizeof(void *)/sizeof(UChar
)];
79 UErrorCode status
= U_ZERO_ERROR
;
84 cnv
= ucnv_open(NULL
, &status
);
85 if(U_FAILURE(status
) || cnv
== NULL
) {
89 numUChars
= ucnv_toUChars(cnv
,
95 destSize
= (numUChars
+1) * sizeof(UChar
) + sizeof(struct StringStruct
);
96 dest
= (StringStruct
*)malloc(destSize
);
98 if (status
== U_BUFFER_OVERFLOW_ERROR
|| status
== U_STRING_NOT_TERMINATED_WARNING
) {
99 ucnv_toUChars(cnv
, dest
->str
, numUChars
+1, src
, -1, &status
);
100 } else if (status
== U_ZERO_ERROR
) {
101 u_strcpy(dest
->str
, stackBuf
);
108 ucnv_reset(cnv
); /* be good citizens */
114 dest
->link
= (StringStruct
*)(*freeHook
);
119 static void freeToUCharStrings(void **hook
) {
120 StringStruct
*s
= *(StringStruct
**)hook
;
122 StringStruct
*next
= s
->link
;
129 static void TestBreakIteratorCAPI()
131 UErrorCode status
= U_ZERO_ERROR
;
132 UBreakIterator
*word
, *sentence
, *line
, *character
, *b
, *bogus
;
133 int32_t start
,pos
,end
,to
;
139 /* Note: the adjacent "" are concatenating strings, not adding a \" to the
140 string, which is probably what whoever wrote this intended. Don't fix,
141 because it would throw off the hard coded break positions in the following
143 u_uastrcpy(text
, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah");
147 log_verbose("\nTesting BreakIterator open functions\n");
149 /* Use french for fun */
150 word
= ubrk_open(UBRK_WORD
, "en_US", text
, u_strlen(text
), &status
);
151 if(status
== U_FILE_ACCESS_ERROR
) {
152 log_data_err("Check your data - it doesn't seem to be around\n");
154 } else if(U_FAILURE(status
)){
155 log_err("FAIL: Error in ubrk_open() for word breakiterator: %s\n", myErrorName(status
));
158 log_verbose("PASS: Successfully opened word breakiterator\n");
161 sentence
= ubrk_open(UBRK_SENTENCE
, "en_US", text
, u_strlen(text
), &status
);
162 if(U_FAILURE(status
)){
163 log_err("FAIL: Error in ubrk_open() for sentence breakiterator: %s\n", myErrorName(status
));
167 log_verbose("PASS: Successfully opened sentence breakiterator\n");
170 line
= ubrk_open(UBRK_LINE
, "en_US", text
, u_strlen(text
), &status
);
171 if(U_FAILURE(status
)){
172 log_err("FAIL: Error in ubrk_open() for line breakiterator: %s\n", myErrorName(status
));
176 log_verbose("PASS: Successfully opened line breakiterator\n");
179 character
= ubrk_open(UBRK_CHARACTER
, "en_US", text
, u_strlen(text
), &status
);
180 if(U_FAILURE(status
)){
181 log_err("FAIL: Error in ubrk_open() for character breakiterator: %s\n", myErrorName(status
));
185 log_verbose("PASS: Successfully opened character breakiterator\n");
187 /*trying to open an illegal iterator*/
188 bogus
= ubrk_open((UBreakIteratorType
)5, "en_US", text
, u_strlen(text
), &status
);
189 if(U_SUCCESS(status
)){
190 log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_MEMORY_ALLOCATION_ERROR\n");
192 if(U_FAILURE(status
)){
193 if(status
!= U_MEMORY_ALLOCATION_ERROR
){
194 log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_MEMORY_ALLOCATION_ERROR\n Got %s\n", myErrorName(status
));
200 /* ======= Test ubrk_countAvialable() and ubrk_getAvialable() */
202 log_verbose("\nTesting ubrk_countAvailable() and ubrk_getAvailable()\n");
203 count
=ubrk_countAvailable();
204 /* use something sensible w/o hardcoding the count */
206 log_err("FAIL: Error in ubrk_countAvialable() returned %d\n", count
);
209 log_verbose("PASS: ubrk_countAvialable() successful returned %d\n", count
);
213 log_verbose("%s\n", ubrk_getAvailable(i
));
214 if (ubrk_getAvailable(i
) == 0)
215 log_err("No locale for which breakiterator is applicable\n");
217 log_verbose("A locale %s for which breakiterator is applicable\n",ubrk_getAvailable(i
));
220 /*========Test ubrk_first(), ubrk_last()...... and other functions*/
222 log_verbose("\nTesting the functions for word\n");
223 start
= ubrk_first(word
);
225 log_err("error ubrk_start(word) did not return 0\n");
226 log_verbose("first (word = %d\n", (int32_t)start
);
229 log_err("error ubrk_next(word) did not return 4\n");
230 log_verbose("next (word = %d\n", (int32_t)pos
);
231 pos
=ubrk_following(word
, 4);
233 log_err("error ubrl_following(word,4) did not return 6\n");
234 log_verbose("next (word = %d\n", (int32_t)pos
);
237 log_err("error ubrk_last(word) did not return 49\n");
238 log_verbose("last (word = %d\n", (int32_t)end
);
240 pos
=ubrk_previous(word
);
241 log_verbose("%d %d\n", end
, pos
);
243 pos
=ubrk_previous(word
);
244 log_verbose("%d \n", pos
);
246 if (ubrk_isBoundary(word
, 2) != FALSE
) {
247 log_err("error ubrk_isBoundary(word, 2) did not return FALSE\n");
249 pos
=ubrk_current(word
);
251 log_err("error ubrk_current() != 4 after ubrk_isBoundary(word, 2)\n");
253 if (ubrk_isBoundary(word
, 4) != TRUE
) {
254 log_err("error ubrk_isBoundary(word, 4) did not return TRUE\n");
259 log_verbose("\nTesting the functions for character\n");
260 ubrk_first(character
);
261 pos
= ubrk_following(character
, 5);
263 log_err("error ubrk_following(character,5) did not return 6\n");
264 log_verbose("Following (character,5) = %d\n", (int32_t)pos
);
265 pos
=ubrk_following(character
, 18);
267 log_err("error ubrk_following(character,18) did not return 19\n");
268 log_verbose("Followingcharacter,18) = %d\n", (int32_t)pos
);
269 pos
=ubrk_preceding(character
, 22);
271 log_err("error ubrk_preceding(character,22) did not return 21\n");
272 log_verbose("preceding(character,22) = %d\n", (int32_t)pos
);
275 log_verbose("\nTesting the functions for line\n");
276 pos
=ubrk_first(line
);
278 log_err("error ubrk_first(line) returned %d, expected 0\n", (int32_t)pos
);
279 pos
= ubrk_next(line
);
280 pos
=ubrk_following(line
, 18);
282 log_err("error ubrk_following(line) did not return 22\n");
283 log_verbose("following (line) = %d\n", (int32_t)pos
);
286 log_verbose("\nTesting the functions for sentence\n");
287 ubrk_first(sentence
);
288 pos
= ubrk_current(sentence
);
289 log_verbose("Current(sentence) = %d\n", (int32_t)pos
);
290 pos
= ubrk_last(sentence
);
292 log_err("error ubrk_last for sentence did not return 49\n");
293 log_verbose("Last (sentence) = %d\n", (int32_t)pos
);
294 ubrk_first(sentence
);
295 to
= ubrk_following( sentence
, 0 );
296 if (to
== 0) log_err("ubrk_following returned 0\n");
297 to
= ubrk_preceding( sentence
, to
);
298 if (to
!= 0) log_err("ubrk_preceding didn't return 0\n");
299 if (ubrk_first(sentence
)!=ubrk_current(sentence
)) {
300 log_err("error in ubrk_first() or ubrk_current()\n");
305 /*Testing ubrk_open and ubrk_close()*/
306 log_verbose("\nTesting open and close for us locale\n");
307 b
= ubrk_open(UBRK_WORD
, "fr_FR", text
, u_strlen(text
), &status
);
308 if (U_FAILURE(status
)) {
309 log_err("ubrk_open for word returned NULL: %s\n", myErrorName(status
));
314 ubrk_close(sentence
);
316 ubrk_close(character
);
319 static void TestBreakIteratorSafeClone(void)
321 UChar text
[51]; /* Keep this odd to test for 64-bit memory alignment */
322 /* NOTE: This doesn't reliably force mis-alignment of following items. */
323 uint8_t buffer
[CLONETEST_ITERATOR_COUNT
] [U_BRK_SAFECLONE_BUFFERSIZE
];
324 int32_t bufferSize
= U_BRK_SAFECLONE_BUFFERSIZE
;
326 UBreakIterator
* someIterators
[CLONETEST_ITERATOR_COUNT
];
327 UBreakIterator
* someClonedIterators
[CLONETEST_ITERATOR_COUNT
];
329 UBreakIterator
* brk
;
330 UErrorCode status
= U_ZERO_ERROR
;
334 /*Testing ubrk_safeClone */
336 /* Note: the adjacent "" are concatenating strings, not adding a \" to the
337 string, which is probably what whoever wrote this intended. Don't fix,
338 because it would throw off the hard coded break positions in the following
340 u_uastrcpy(text
, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah");
342 /* US & Thai - rule-based & dictionary based */
343 someIterators
[0] = ubrk_open(UBRK_WORD
, "en_US", text
, u_strlen(text
), &status
);
344 if(!someIterators
[0] || U_FAILURE(status
)) {
345 log_data_err("Couldn't open en_US word break iterator - %s\n", u_errorName(status
));
349 someIterators
[1] = ubrk_open(UBRK_WORD
, "th_TH", text
, u_strlen(text
), &status
);
350 if(!someIterators
[1] || U_FAILURE(status
)) {
351 log_data_err("Couldn't open th_TH word break iterator - %s\n", u_errorName(status
));
355 /* test each type of iterator */
356 for (i
= 0; i
< CLONETEST_ITERATOR_COUNT
; i
++)
359 /* Check the various error & informational states */
361 /* Null status - just returns NULL */
362 if (0 != ubrk_safeClone(someIterators
[i
], buffer
[i
], &bufferSize
, 0))
364 log_err("FAIL: Cloned Iterator failed to deal correctly with null status\n");
366 /* error status - should return 0 & keep error the same */
367 status
= U_MEMORY_ALLOCATION_ERROR
;
368 if (0 != ubrk_safeClone(someIterators
[i
], buffer
[i
], &bufferSize
, &status
) || status
!= U_MEMORY_ALLOCATION_ERROR
)
370 log_err("FAIL: Cloned Iterator failed to deal correctly with incoming error status\n");
372 status
= U_ZERO_ERROR
;
374 /* Null buffer size pointer - just returns NULL & set error to U_ILLEGAL_ARGUMENT_ERROR*/
375 if (0 != ubrk_safeClone(someIterators
[i
], buffer
[i
], 0, &status
) || status
!= U_ILLEGAL_ARGUMENT_ERROR
)
377 log_err("FAIL: Cloned Iterator failed to deal correctly with null bufferSize pointer\n");
379 status
= U_ZERO_ERROR
;
381 /* buffer size pointer is 0 - fill in pbufferSize with a size */
383 if (0 != ubrk_safeClone(someIterators
[i
], buffer
[i
], &bufferSize
, &status
) || U_FAILURE(status
) || bufferSize
<= 0)
385 log_err("FAIL: Cloned Iterator failed a sizing request ('preflighting')\n");
387 /* Verify our define is large enough */
388 if (U_BRK_SAFECLONE_BUFFERSIZE
< bufferSize
)
390 log_err("FAIL: Pre-calculated buffer size is too small\n");
392 /* Verify we can use this run-time calculated size */
393 if (0 == (brk
= ubrk_safeClone(someIterators
[i
], buffer
[i
], &bufferSize
, &status
)) || U_FAILURE(status
))
395 log_err("FAIL: Iterator can't be cloned with run-time size\n");
399 /* size one byte too small - should allocate & let us know */
401 if (0 == (brk
= ubrk_safeClone(someIterators
[i
], 0, &bufferSize
, &status
)) || status
!= U_SAFECLONE_ALLOCATED_WARNING
)
403 log_err("FAIL: Cloned Iterator failed to deal correctly with too-small buffer size\n");
407 status
= U_ZERO_ERROR
;
408 bufferSize
= U_BRK_SAFECLONE_BUFFERSIZE
;
410 /* Null buffer pointer - return Iterator & set error to U_SAFECLONE_ALLOCATED_ERROR */
411 if (0 == (brk
= ubrk_safeClone(someIterators
[i
], 0, &bufferSize
, &status
)) || status
!= U_SAFECLONE_ALLOCATED_WARNING
)
413 log_err("FAIL: Cloned Iterator failed to deal correctly with null buffer pointer\n");
417 status
= U_ZERO_ERROR
;
419 /* Mis-aligned buffer pointer. */
421 char stackBuf
[U_BRK_SAFECLONE_BUFFERSIZE
+sizeof(void *)];
425 brk
= ubrk_safeClone(someIterators
[i
], &stackBuf
[1], &bufferSize
, &status
);
426 if (U_FAILURE(status
) || brk
== 0) {
427 log_err("FAIL: Cloned Iterator failed with misaligned buffer pointer\n");
429 if (status
== U_SAFECLONE_ALLOCATED_WARNING
) {
430 log_err("FAIL: Cloned Iterator allocated when using a mis-aligned buffer.\n");
432 offset
= (int32_t)((char *)&p
-(char*)brk
);
436 if (offset
% sizeof(void *) != 0) {
437 log_err("FAIL: Cloned Iterator failed to align correctly with misaligned buffer pointer\n");
444 /* Null Iterator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
445 if (0 != ubrk_safeClone(0, buffer
[i
], &bufferSize
, &status
) || status
!= U_ILLEGAL_ARGUMENT_ERROR
)
447 log_err("FAIL: Cloned Iterator failed to deal correctly with null Iterator pointer\n");
449 status
= U_ZERO_ERROR
;
451 /* Do these cloned Iterators work at all - make a first & next call */
452 bufferSize
= U_BRK_SAFECLONE_BUFFERSIZE
;
453 someClonedIterators
[i
] = ubrk_safeClone(someIterators
[i
], buffer
[i
], &bufferSize
, &status
);
455 start
= ubrk_first(someClonedIterators
[i
]);
457 log_err("error ubrk_start(clone) did not return 0\n");
458 pos
=ubrk_next(someClonedIterators
[i
]);
460 log_err("error ubrk_next(clone) did not return 4\n");
462 ubrk_close(someClonedIterators
[i
]);
463 ubrk_close(someIterators
[i
]);
469 // Open a break iterator from char * rules. Take care of conversion
470 // of the rules and error checking.
472 static UBreakIterator
* testOpenRules(char *rules
) {
473 UErrorCode status
= U_ZERO_ERROR
;
474 UChar
*ruleSourceU
= NULL
;
475 void *strCleanUp
= NULL
;
476 UParseError parseErr
;
479 ruleSourceU
= toUChar(rules
, &strCleanUp
);
481 bi
= ubrk_openRules(ruleSourceU
, -1, /* The rules */
482 NULL
, -1, /* The text to be iterated over. */
485 if (U_FAILURE(status
)) {
486 log_err("FAIL: ubrk_openRules: ICU Error \"%s\"\n", u_errorName(status
));
489 freeToUCharStrings(&strCleanUp
);
495 * TestBreakIteratorRules - Verify that a break iterator can be created from
496 * a set of source rules.
498 static void TestBreakIteratorRules() {
499 /* Rules will keep together any run of letters not including 'a', OR
500 * keep together 'abc', but only when followed by 'def', OTHERWISE
501 * just return one char at a time.
503 char rules
[] = "abc{666}/def;\n [\\p{L} - [a]]* {2}; . {1};";
504 /* 0123456789012345678 */
505 char data
[] = "abcdex abcdefgh-def"; /* the test data string */
506 char breaks
[] = "** ** * ** *"; /* * the expected break positions */
507 char tags
[] = "01 21 6 21 2"; /* expected tag values at break positions */
508 int32_t tagMap
[] = {0, 1, 2, 3, 4, 5, 666};
511 void *freeHook
= NULL
;
512 UErrorCode status
= U_ZERO_ERROR
;
516 UBreakIterator
*bi
= testOpenRules(rules
);
517 if (bi
== NULL
) {return;}
518 uData
= toUChar(data
, &freeHook
);
519 ubrk_setText(bi
, uData
, -1, &status
);
521 pos
= ubrk_first(bi
);
522 for (i
=0; i
<sizeof(breaks
); i
++) {
523 if (pos
== i
&& breaks
[i
] != '*') {
524 log_err("FAIL: unexpected break at position %d found\n", pos
);
527 if (pos
!= i
&& breaks
[i
] == '*') {
528 log_err("FAIL: expected break at position %d not found.\n", i
);
532 int32_t tag
, expectedTag
;
533 tag
= ubrk_getRuleStatus(bi
);
534 expectedTag
= tagMap
[tags
[i
]&0xf];
535 if (tag
!= expectedTag
) {
536 log_err("FAIL: incorrect tag value. Position = %d; expected tag %d, got %d",
537 pos
, expectedTag
, tag
);
544 freeToUCharStrings(&freeHook
);
548 static void TestBreakIteratorRuleError() {
550 * TestBreakIteratorRuleError - Try to create a BI from rules with syntax errors,
551 * check that the error is reported correctly.
553 char rules
[] = " # This is a rule comment on line 1\n"
554 "[:L:]; # this rule is OK.\n"
555 "abcdefg); # Error, mismatched parens\n";
557 void *freeHook
= NULL
;
558 UErrorCode status
= U_ZERO_ERROR
;
559 UParseError parseErr
;
562 uRules
= toUChar(rules
, &freeHook
);
563 bi
= ubrk_openRules(uRules
, -1, /* The rules */
564 NULL
, -1, /* The text to be iterated over. */
566 if (U_SUCCESS(status
)) {
567 log_err("FAIL: construction of break iterator succeeded when it should have failed.\n");
570 if (parseErr
.line
!= 3 || parseErr
.offset
!= 8) {
571 log_err("FAIL: incorrect error position reported. Got line %d, char %d, expected line 3, char 7",
572 parseErr
.line
, parseErr
.offset
);
575 freeToUCharStrings(&freeHook
);
580 * TestsBreakIteratorStatusVals() Test the ubrk_getRuleStatusVec() funciton
582 static void TestBreakIteratorStatusVec() {
583 #define RULE_STRING_LENGTH 200
584 UChar rules
[RULE_STRING_LENGTH
];
586 #define TEST_STRING_LENGTH 25
587 UChar testString
[TEST_STRING_LENGTH
];
588 UBreakIterator
*bi
= NULL
;
592 UErrorCode status
= U_ZERO_ERROR
;
594 u_uastrncpy(rules
, "[A-N]{100}; \n"
599 "!.*;\n", RULE_STRING_LENGTH
);
600 u_uastrncpy(testString
, "ABC", TEST_STRING_LENGTH
);
603 bi
= ubrk_openRules(rules
, -1, testString
, -1, NULL
, &status
);
604 TEST_ASSET_SUCCESS(status
);
605 TEST_ASSERT(bi
!= NULL
);
608 TEST_ASSERT(pos
== 1);
610 memset(vals
, -1, sizeof(vals
));
611 numVals
= ubrk_getRuleStatusVec(bi
, vals
, 10, &status
);
612 TEST_ASSET_SUCCESS(status
);
613 TEST_ASSERT(numVals
== 2);
614 TEST_ASSERT(vals
[0] == 100);
615 TEST_ASSERT(vals
[1] == 300);
616 TEST_ASSERT(vals
[2] == -1);
618 numVals
= ubrk_getRuleStatusVec(bi
, vals
, 0, &status
);
619 TEST_ASSERT(status
== U_BUFFER_OVERFLOW_ERROR
);
620 TEST_ASSERT(numVals
== 2);
626 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */