]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/cbiapts.c
ICU-491.11.1.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cbiapts.c
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2011, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
7 *
8 * File CBIAPTS.C
9 *
10 * Modification History:
11 * Name Description
12 * Madhu Katragadda Creation
13 *********************************************************************************/
14 /*C API TEST FOR BREAKITERATOR */
15 /**
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.
19 **/
20
21 #include "unicode/utypes.h"
22
23 #if !UCONFIG_NO_BREAK_ITERATION
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include "unicode/uloc.h"
28 #include "unicode/ubrk.h"
29 #include "unicode/ustring.h"
30 #include "unicode/ucnv.h"
31 #include "unicode/utext.h"
32 #include "cintltst.h"
33 #include "cbiapts.h"
34
35 #define TEST_ASSERT_SUCCESS(status) {if (U_FAILURE(status)) { \
36 log_data_err("Failure at file %s, line %d, error = %s (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status));}}
37
38 #define TEST_ASSERT(expr) {if ((expr)==FALSE) { \
39 log_data_err("Test Failure at file %s, line %d (Are you missing data?)\n", __FILE__, __LINE__);}}
40
41 static void TestBreakIteratorSafeClone(void);
42 static void TestBreakIteratorRules(void);
43 static void TestBreakIteratorRuleError(void);
44 static void TestBreakIteratorStatusVec(void);
45 static void TestBreakIteratorUText(void);
46 static void TestBreakIteratorTailoring(void);
47 static void TestBreakIteratorRefresh(void);
48
49 void addBrkIterAPITest(TestNode** root);
50
51 void addBrkIterAPITest(TestNode** root)
52 {
53 #if !UCONFIG_NO_FILE_IO
54 addTest(root, &TestBreakIteratorCAPI, "tstxtbd/cbiapts/TestBreakIteratorCAPI");
55 addTest(root, &TestBreakIteratorSafeClone, "tstxtbd/cbiapts/TestBreakIteratorSafeClone");
56 addTest(root, &TestBreakIteratorUText, "tstxtbd/cbiapts/TestBreakIteratorUText");
57 #endif
58 addTest(root, &TestBreakIteratorRules, "tstxtbd/cbiapts/TestBreakIteratorRules");
59 addTest(root, &TestBreakIteratorRuleError, "tstxtbd/cbiapts/TestBreakIteratorRuleError");
60 addTest(root, &TestBreakIteratorStatusVec, "tstxtbd/cbiapts/TestBreakIteratorStatusVec");
61 addTest(root, &TestBreakIteratorTailoring, "tstxtbd/cbiapts/TestBreakIteratorTailoring");
62 addTest(root, &TestBreakIteratorRefresh, "tstxtbd/cbiapts/TestBreakIteratorRefresh");
63 }
64
65 #define CLONETEST_ITERATOR_COUNT 2
66
67 /*
68 * Utility function for converting char * to UChar * strings, to
69 * simplify the test code. Converted strings are put in heap allocated
70 * storage. A hook (probably a local in the caller's code) allows all
71 * strings converted with that hook to be freed with a single call.
72 */
73 typedef struct StringStruct {
74 struct StringStruct *link;
75 UChar str[1];
76 } StringStruct;
77
78
79 static UChar* toUChar(const char *src, void **freeHook) {
80 /* Structure of the memory that we allocate on the heap */
81
82 int32_t numUChars;
83 int32_t destSize;
84 UChar stackBuf[2000 + sizeof(void *)/sizeof(UChar)];
85 StringStruct *dest;
86 UConverter *cnv;
87
88 UErrorCode status = U_ZERO_ERROR;
89 if (src == NULL) {
90 return NULL;
91 };
92
93 cnv = ucnv_open(NULL, &status);
94 if(U_FAILURE(status) || cnv == NULL) {
95 return NULL;
96 }
97 ucnv_reset(cnv);
98 numUChars = ucnv_toUChars(cnv,
99 stackBuf,
100 2000,
101 src, -1,
102 &status);
103
104 destSize = (numUChars+1) * sizeof(UChar) + sizeof(struct StringStruct);
105 dest = (StringStruct *)malloc(destSize);
106 if (dest != NULL) {
107 if (status == U_BUFFER_OVERFLOW_ERROR || status == U_STRING_NOT_TERMINATED_WARNING) {
108 ucnv_toUChars(cnv, dest->str, numUChars+1, src, -1, &status);
109 } else if (status == U_ZERO_ERROR) {
110 u_strcpy(dest->str, stackBuf);
111 } else {
112 free(dest);
113 dest = NULL;
114 }
115 }
116
117 ucnv_reset(cnv); /* be good citizens */
118 ucnv_close(cnv);
119 if (dest == NULL) {
120 return NULL;
121 }
122
123 dest->link = (StringStruct*)(*freeHook);
124 *freeHook = dest;
125 return dest->str;
126 }
127
128 static void freeToUCharStrings(void **hook) {
129 StringStruct *s = *(StringStruct **)hook;
130 while (s != NULL) {
131 StringStruct *next = s->link;
132 free(s);
133 s = next;
134 }
135 }
136
137
138 static void TestBreakIteratorCAPI()
139 {
140 UErrorCode status = U_ZERO_ERROR;
141 UBreakIterator *word, *sentence, *line, *character, *b, *bogus;
142 int32_t start,pos,end,to;
143 int32_t i;
144 int32_t count = 0;
145
146 UChar text[50];
147
148 /* Note: the adjacent "" are concatenating strings, not adding a \" to the
149 string, which is probably what whoever wrote this intended. Don't fix,
150 because it would throw off the hard coded break positions in the following
151 tests. */
152 u_uastrcpy(text, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah");
153
154
155 /*test ubrk_open()*/
156 log_verbose("\nTesting BreakIterator open functions\n");
157
158 /* Use french for fun */
159 word = ubrk_open(UBRK_WORD, "en_US", text, u_strlen(text), &status);
160 if(status == U_FILE_ACCESS_ERROR) {
161 log_data_err("Check your data - it doesn't seem to be around\n");
162 return;
163 } else if(U_FAILURE(status)){
164 log_err_status(status, "FAIL: Error in ubrk_open() for word breakiterator: %s\n", myErrorName(status));
165 }
166 else{
167 log_verbose("PASS: Successfully opened word breakiterator\n");
168 }
169
170 sentence = ubrk_open(UBRK_SENTENCE, "en_US", text, u_strlen(text), &status);
171 if(U_FAILURE(status)){
172 log_err_status(status, "FAIL: Error in ubrk_open() for sentence breakiterator: %s\n", myErrorName(status));
173 return;
174 }
175 else{
176 log_verbose("PASS: Successfully opened sentence breakiterator\n");
177 }
178
179 line = ubrk_open(UBRK_LINE, "en_US", text, u_strlen(text), &status);
180 if(U_FAILURE(status)){
181 log_err("FAIL: Error in ubrk_open() for line breakiterator: %s\n", myErrorName(status));
182 return;
183 }
184 else{
185 log_verbose("PASS: Successfully opened line breakiterator\n");
186 }
187
188 character = ubrk_open(UBRK_CHARACTER, "en_US", text, u_strlen(text), &status);
189 if(U_FAILURE(status)){
190 log_err("FAIL: Error in ubrk_open() for character breakiterator: %s\n", myErrorName(status));
191 return;
192 }
193 else{
194 log_verbose("PASS: Successfully opened character breakiterator\n");
195 }
196 /*trying to open an illegal iterator*/
197 bogus = ubrk_open((UBreakIteratorType)5, "en_US", text, u_strlen(text), &status);
198 if(U_SUCCESS(status)){
199 log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_ILLEGAL_ARGUMENT_ERROR\n");
200 }
201 if(U_FAILURE(status)){
202 if(status != U_ILLEGAL_ARGUMENT_ERROR){
203 log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_ILLEGAL_ARGUMENT_ERROR\n Got %s\n", myErrorName(status));
204 }
205 }
206 status=U_ZERO_ERROR;
207
208
209 /* ======= Test ubrk_countAvialable() and ubrk_getAvialable() */
210
211 log_verbose("\nTesting ubrk_countAvailable() and ubrk_getAvailable()\n");
212 count=ubrk_countAvailable();
213 /* use something sensible w/o hardcoding the count */
214 if(count < 0){
215 log_err("FAIL: Error in ubrk_countAvialable() returned %d\n", count);
216 }
217 else{
218 log_verbose("PASS: ubrk_countAvialable() successful returned %d\n", count);
219 }
220 for(i=0;i<count;i++)
221 {
222 log_verbose("%s\n", ubrk_getAvailable(i));
223 if (ubrk_getAvailable(i) == 0)
224 log_err("No locale for which breakiterator is applicable\n");
225 else
226 log_verbose("A locale %s for which breakiterator is applicable\n",ubrk_getAvailable(i));
227 }
228
229 /*========Test ubrk_first(), ubrk_last()...... and other functions*/
230
231 log_verbose("\nTesting the functions for word\n");
232 start = ubrk_first(word);
233 if(start!=0)
234 log_err("error ubrk_start(word) did not return 0\n");
235 log_verbose("first (word = %d\n", (int32_t)start);
236 pos=ubrk_next(word);
237 if(pos!=4)
238 log_err("error ubrk_next(word) did not return 4\n");
239 log_verbose("next (word = %d\n", (int32_t)pos);
240 pos=ubrk_following(word, 4);
241 if(pos!=5)
242 log_err("error ubrl_following(word,4) did not return 6\n");
243 log_verbose("next (word = %d\n", (int32_t)pos);
244 end=ubrk_last(word);
245 if(end!=49)
246 log_err("error ubrk_last(word) did not return 49\n");
247 log_verbose("last (word = %d\n", (int32_t)end);
248
249 pos=ubrk_previous(word);
250 log_verbose("%d %d\n", end, pos);
251
252 pos=ubrk_previous(word);
253 log_verbose("%d \n", pos);
254
255 if (ubrk_isBoundary(word, 2) != FALSE) {
256 log_err("error ubrk_isBoundary(word, 2) did not return FALSE\n");
257 }
258 pos=ubrk_current(word);
259 if (pos != 4) {
260 log_err("error ubrk_current() != 4 after ubrk_isBoundary(word, 2)\n");
261 }
262 if (ubrk_isBoundary(word, 4) != TRUE) {
263 log_err("error ubrk_isBoundary(word, 4) did not return TRUE\n");
264 }
265
266
267
268 log_verbose("\nTesting the functions for character\n");
269 ubrk_first(character);
270 pos = ubrk_following(character, 5);
271 if(pos!=6)
272 log_err("error ubrk_following(character,5) did not return 6\n");
273 log_verbose("Following (character,5) = %d\n", (int32_t)pos);
274 pos=ubrk_following(character, 18);
275 if(pos!=19)
276 log_err("error ubrk_following(character,18) did not return 19\n");
277 log_verbose("Followingcharacter,18) = %d\n", (int32_t)pos);
278 pos=ubrk_preceding(character, 22);
279 if(pos!=21)
280 log_err("error ubrk_preceding(character,22) did not return 21\n");
281 log_verbose("preceding(character,22) = %d\n", (int32_t)pos);
282
283
284 log_verbose("\nTesting the functions for line\n");
285 pos=ubrk_first(line);
286 if(pos != 0)
287 log_err("error ubrk_first(line) returned %d, expected 0\n", (int32_t)pos);
288 pos = ubrk_next(line);
289 pos=ubrk_following(line, 18);
290 if(pos!=22)
291 log_err("error ubrk_following(line) did not return 22\n");
292 log_verbose("following (line) = %d\n", (int32_t)pos);
293
294
295 log_verbose("\nTesting the functions for sentence\n");
296 ubrk_first(sentence);
297 pos = ubrk_current(sentence);
298 log_verbose("Current(sentence) = %d\n", (int32_t)pos);
299 pos = ubrk_last(sentence);
300 if(pos!=49)
301 log_err("error ubrk_last for sentence did not return 49\n");
302 log_verbose("Last (sentence) = %d\n", (int32_t)pos);
303 ubrk_first(sentence);
304 to = ubrk_following( sentence, 0 );
305 if (to == 0) log_err("ubrk_following returned 0\n");
306 to = ubrk_preceding( sentence, to );
307 if (to != 0) log_err("ubrk_preceding didn't return 0\n");
308 if (ubrk_first(sentence)!=ubrk_current(sentence)) {
309 log_err("error in ubrk_first() or ubrk_current()\n");
310 }
311
312
313 /*---- */
314 /*Testing ubrk_open and ubrk_close()*/
315 log_verbose("\nTesting open and close for us locale\n");
316 b = ubrk_open(UBRK_WORD, "fr_FR", text, u_strlen(text), &status);
317 if (U_FAILURE(status)) {
318 log_err("ubrk_open for word returned NULL: %s\n", myErrorName(status));
319 }
320 ubrk_close(b);
321
322 /* Test setText and setUText */
323 {
324 UChar s1[] = {0x41, 0x42, 0x20, 0};
325 UChar s2[] = {0x41, 0x42, 0x43, 0x44, 0x45, 0};
326 UText *ut = NULL;
327 UBreakIterator *bb;
328 int j;
329
330 log_verbose("\nTesting ubrk_setText() and ubrk_setUText()\n");
331 status = U_ZERO_ERROR;
332 bb = ubrk_open(UBRK_WORD, "en_US", NULL, 0, &status);
333 TEST_ASSERT_SUCCESS(status);
334 ubrk_setText(bb, s1, -1, &status);
335 TEST_ASSERT_SUCCESS(status);
336 ubrk_first(bb);
337 j = ubrk_next(bb);
338 TEST_ASSERT(j == 2);
339 ut = utext_openUChars(ut, s2, -1, &status);
340 ubrk_setUText(bb, ut, &status);
341 TEST_ASSERT_SUCCESS(status);
342 j = ubrk_next(bb);
343 TEST_ASSERT(j == 5);
344
345 ubrk_close(bb);
346 utext_close(ut);
347 }
348
349 ubrk_close(word);
350 ubrk_close(sentence);
351 ubrk_close(line);
352 ubrk_close(character);
353 }
354
355 static void TestBreakIteratorSafeClone(void)
356 {
357 UChar text[51]; /* Keep this odd to test for 64-bit memory alignment */
358 /* NOTE: This doesn't reliably force mis-alignment of following items. */
359 uint8_t buffer [CLONETEST_ITERATOR_COUNT] [U_BRK_SAFECLONE_BUFFERSIZE];
360 int32_t bufferSize = U_BRK_SAFECLONE_BUFFERSIZE;
361
362 UBreakIterator * someIterators [CLONETEST_ITERATOR_COUNT];
363 UBreakIterator * someClonedIterators [CLONETEST_ITERATOR_COUNT];
364
365 UBreakIterator * brk;
366 UErrorCode status = U_ZERO_ERROR;
367 int32_t start,pos;
368 int32_t i;
369
370 /*Testing ubrk_safeClone */
371
372 /* Note: the adjacent "" are concatenating strings, not adding a \" to the
373 string, which is probably what whoever wrote this intended. Don't fix,
374 because it would throw off the hard coded break positions in the following
375 tests. */
376 u_uastrcpy(text, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah");
377
378 /* US & Thai - rule-based & dictionary based */
379 someIterators[0] = ubrk_open(UBRK_WORD, "en_US", text, u_strlen(text), &status);
380 if(!someIterators[0] || U_FAILURE(status)) {
381 log_data_err("Couldn't open en_US word break iterator - %s\n", u_errorName(status));
382 return;
383 }
384
385 someIterators[1] = ubrk_open(UBRK_WORD, "th_TH", text, u_strlen(text), &status);
386 if(!someIterators[1] || U_FAILURE(status)) {
387 log_data_err("Couldn't open th_TH word break iterator - %s\n", u_errorName(status));
388 return;
389 }
390
391 /* test each type of iterator */
392 for (i = 0; i < CLONETEST_ITERATOR_COUNT; i++)
393 {
394
395 /* Check the various error & informational states */
396
397 /* Null status - just returns NULL */
398 if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, 0))
399 {
400 log_err("FAIL: Cloned Iterator failed to deal correctly with null status\n");
401 }
402 /* error status - should return 0 & keep error the same */
403 status = U_MEMORY_ALLOCATION_ERROR;
404 if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status) || status != U_MEMORY_ALLOCATION_ERROR)
405 {
406 log_err("FAIL: Cloned Iterator failed to deal correctly with incoming error status\n");
407 }
408 status = U_ZERO_ERROR;
409
410 /* Null buffer size pointer - just returns NULL & set error to U_ILLEGAL_ARGUMENT_ERROR*/
411 if (0 != ubrk_safeClone(someIterators[i], buffer[i], 0, &status) || status != U_ILLEGAL_ARGUMENT_ERROR)
412 {
413 log_err("FAIL: Cloned Iterator failed to deal correctly with null bufferSize pointer\n");
414 }
415 status = U_ZERO_ERROR;
416
417 /* buffer size pointer is 0 - fill in pbufferSize with a size */
418 bufferSize = 0;
419 if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status) || U_FAILURE(status) || bufferSize <= 0)
420 {
421 log_err("FAIL: Cloned Iterator failed a sizing request ('preflighting')\n");
422 }
423 /* Verify our define is large enough */
424 if (U_BRK_SAFECLONE_BUFFERSIZE < bufferSize)
425 {
426 log_err("FAIL: Pre-calculated buffer size is too small - %d but needed %d\n", U_BRK_SAFECLONE_BUFFERSIZE, bufferSize);
427 }
428 /* Verify we can use this run-time calculated size */
429 if (0 == (brk = ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status)) || U_FAILURE(status))
430 {
431 log_err("FAIL: Iterator can't be cloned with run-time size\n");
432 }
433 if (brk)
434 ubrk_close(brk);
435 /* size one byte too small - should allocate & let us know */
436 --bufferSize;
437 if (0 == (brk = ubrk_safeClone(someIterators[i], 0, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_WARNING)
438 {
439 log_err("FAIL: Cloned Iterator failed to deal correctly with too-small buffer size\n");
440 }
441 if (brk)
442 ubrk_close(brk);
443 status = U_ZERO_ERROR;
444 bufferSize = U_BRK_SAFECLONE_BUFFERSIZE;
445
446 /* Null buffer pointer - return Iterator & set error to U_SAFECLONE_ALLOCATED_ERROR */
447 if (0 == (brk = ubrk_safeClone(someIterators[i], 0, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_WARNING)
448 {
449 log_err("FAIL: Cloned Iterator failed to deal correctly with null buffer pointer\n");
450 }
451 if (brk)
452 ubrk_close(brk);
453 status = U_ZERO_ERROR;
454
455 /* Mis-aligned buffer pointer. */
456 {
457 char stackBuf[U_BRK_SAFECLONE_BUFFERSIZE+sizeof(void *)];
458 void *p;
459 int32_t offset;
460
461 brk = ubrk_safeClone(someIterators[i], &stackBuf[1], &bufferSize, &status);
462 if (U_FAILURE(status) || brk == 0) {
463 log_err("FAIL: Cloned Iterator failed with misaligned buffer pointer\n");
464 }
465 if (status == U_SAFECLONE_ALLOCATED_WARNING) {
466 log_err("FAIL: Cloned Iterator allocated when using a mis-aligned buffer.\n");
467 }
468 offset = (int32_t)((char *)&p-(char*)brk);
469 if (offset < 0) {
470 offset = -offset;
471 }
472 if (offset % sizeof(void *) != 0) {
473 log_err("FAIL: Cloned Iterator failed to align correctly with misaligned buffer pointer\n");
474 }
475 if (brk)
476 ubrk_close(brk);
477 }
478
479
480 /* Null Iterator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
481 if (0 != ubrk_safeClone(0, buffer[i], &bufferSize, &status) || status != U_ILLEGAL_ARGUMENT_ERROR)
482 {
483 log_err("FAIL: Cloned Iterator failed to deal correctly with null Iterator pointer\n");
484 }
485 status = U_ZERO_ERROR;
486
487 /* Do these cloned Iterators work at all - make a first & next call */
488 bufferSize = U_BRK_SAFECLONE_BUFFERSIZE;
489 someClonedIterators[i] = ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status);
490
491 start = ubrk_first(someClonedIterators[i]);
492 if(start!=0)
493 log_err("error ubrk_start(clone) did not return 0\n");
494 pos=ubrk_next(someClonedIterators[i]);
495 if(pos!=4)
496 log_err("error ubrk_next(clone) did not return 4\n");
497
498 ubrk_close(someClonedIterators[i]);
499 ubrk_close(someIterators[i]);
500 }
501 }
502
503
504 /*
505 // Open a break iterator from char * rules. Take care of conversion
506 // of the rules and error checking.
507 */
508 static UBreakIterator * testOpenRules(char *rules) {
509 UErrorCode status = U_ZERO_ERROR;
510 UChar *ruleSourceU = NULL;
511 void *strCleanUp = NULL;
512 UParseError parseErr;
513 UBreakIterator *bi;
514
515 ruleSourceU = toUChar(rules, &strCleanUp);
516
517 bi = ubrk_openRules(ruleSourceU, -1, /* The rules */
518 NULL, -1, /* The text to be iterated over. */
519 &parseErr, &status);
520
521 if (U_FAILURE(status)) {
522 log_data_err("FAIL: ubrk_openRules: ICU Error \"%s\" (Are you missing data?)\n", u_errorName(status));
523 bi = 0;
524 };
525 freeToUCharStrings(&strCleanUp);
526 return bi;
527
528 }
529
530 /*
531 * TestBreakIteratorRules - Verify that a break iterator can be created from
532 * a set of source rules.
533 */
534 static void TestBreakIteratorRules() {
535 /* Rules will keep together any run of letters not including 'a', OR
536 * keep together 'abc', but only when followed by 'def', OTHERWISE
537 * just return one char at a time.
538 */
539 char rules[] = "abc{666}/def;\n [\\p{L} - [a]]* {2}; . {1};";
540 /* 0123456789012345678 */
541 char data[] = "abcdex abcdefgh-def"; /* the test data string */
542 char breaks[] = "** ** * ** *"; /* * the expected break positions */
543 char tags[] = "01 21 6 21 2"; /* expected tag values at break positions */
544 int32_t tagMap[] = {0, 1, 2, 3, 4, 5, 666};
545
546 UChar *uData;
547 void *freeHook = NULL;
548 UErrorCode status = U_ZERO_ERROR;
549 int32_t pos;
550 int i;
551
552 UBreakIterator *bi = testOpenRules(rules);
553 if (bi == NULL) {return;}
554 uData = toUChar(data, &freeHook);
555 ubrk_setText(bi, uData, -1, &status);
556
557 pos = ubrk_first(bi);
558 for (i=0; i<sizeof(breaks); i++) {
559 if (pos == i && breaks[i] != '*') {
560 log_err("FAIL: unexpected break at position %d found\n", pos);
561 break;
562 }
563 if (pos != i && breaks[i] == '*') {
564 log_err("FAIL: expected break at position %d not found.\n", i);
565 break;
566 }
567 if (pos == i) {
568 int32_t tag, expectedTag;
569 tag = ubrk_getRuleStatus(bi);
570 expectedTag = tagMap[tags[i]&0xf];
571 if (tag != expectedTag) {
572 log_err("FAIL: incorrect tag value. Position = %d; expected tag %d, got %d",
573 pos, expectedTag, tag);
574 break;
575 }
576 pos = ubrk_next(bi);
577 }
578 }
579
580 freeToUCharStrings(&freeHook);
581 ubrk_close(bi);
582 }
583
584 static void TestBreakIteratorRuleError() {
585 /*
586 * TestBreakIteratorRuleError - Try to create a BI from rules with syntax errors,
587 * check that the error is reported correctly.
588 */
589 char rules[] = " # This is a rule comment on line 1\n"
590 "[:L:]; # this rule is OK.\n"
591 "abcdefg); # Error, mismatched parens\n";
592 UChar *uRules;
593 void *freeHook = NULL;
594 UErrorCode status = U_ZERO_ERROR;
595 UParseError parseErr;
596 UBreakIterator *bi;
597
598 uRules = toUChar(rules, &freeHook);
599 bi = ubrk_openRules(uRules, -1, /* The rules */
600 NULL, -1, /* The text to be iterated over. */
601 &parseErr, &status);
602 if (U_SUCCESS(status)) {
603 log_err("FAIL: construction of break iterator succeeded when it should have failed.\n");
604 ubrk_close(bi);
605 } else {
606 if (parseErr.line != 3 || parseErr.offset != 8) {
607 log_data_err("FAIL: incorrect error position reported. Got line %d, char %d, expected line 3, char 7 (Are you missing data?)\n",
608 parseErr.line, parseErr.offset);
609 }
610 }
611 freeToUCharStrings(&freeHook);
612 }
613
614
615 /*
616 * TestsBreakIteratorStatusVals() Test the ubrk_getRuleStatusVec() funciton
617 */
618 static void TestBreakIteratorStatusVec() {
619 #define RULE_STRING_LENGTH 200
620 UChar rules[RULE_STRING_LENGTH];
621
622 #define TEST_STRING_LENGTH 25
623 UChar testString[TEST_STRING_LENGTH];
624 UBreakIterator *bi = NULL;
625 int32_t pos = 0;
626 int32_t vals[10];
627 int32_t numVals;
628 UErrorCode status = U_ZERO_ERROR;
629
630 u_uastrncpy(rules, "[A-N]{100}; \n"
631 "[a-w]{200}; \n"
632 "[\\p{L}]{300}; \n"
633 "[\\p{N}]{400}; \n"
634 "[0-5]{500}; \n"
635 "!.*;\n", RULE_STRING_LENGTH);
636 u_uastrncpy(testString, "ABC", TEST_STRING_LENGTH);
637
638
639 bi = ubrk_openRules(rules, -1, testString, -1, NULL, &status);
640 TEST_ASSERT_SUCCESS(status);
641 TEST_ASSERT(bi != NULL);
642
643 /* The TEST_ASSERT above should change too... */
644 if (bi != NULL) {
645 pos = ubrk_next(bi);
646 TEST_ASSERT(pos == 1);
647
648 memset(vals, -1, sizeof(vals));
649 numVals = ubrk_getRuleStatusVec(bi, vals, 10, &status);
650 TEST_ASSERT_SUCCESS(status);
651 TEST_ASSERT(numVals == 2);
652 TEST_ASSERT(vals[0] == 100);
653 TEST_ASSERT(vals[1] == 300);
654 TEST_ASSERT(vals[2] == -1);
655
656 numVals = ubrk_getRuleStatusVec(bi, vals, 0, &status);
657 TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR);
658 TEST_ASSERT(numVals == 2);
659 }
660
661 ubrk_close(bi);
662 }
663
664
665 /*
666 * static void TestBreakIteratorUText(void);
667 *
668 * Test that ubrk_setUText() is present and works for a simple case.
669 */
670 static void TestBreakIteratorUText(void) {
671 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 */
672 /* 0 1 2 34567890 */
673
674 UErrorCode status = U_ZERO_ERROR;
675 UBreakIterator *bi = NULL;
676 int32_t pos = 0;
677
678
679 UText *ut = utext_openUTF8(NULL, UTF8Str, -1, &status);
680 TEST_ASSERT_SUCCESS(status);
681
682 bi = ubrk_open(UBRK_WORD, "en_US", NULL, 0, &status);
683 if (U_FAILURE(status)) {
684 log_err_status(status, "Failure at file %s, line %d, error = %s\n", __FILE__, __LINE__, u_errorName(status));
685 return;
686 }
687
688 ubrk_setUText(bi, ut, &status);
689 if (U_FAILURE(status)) {
690 log_err("Failure at file %s, line %d, error = %s\n", __FILE__, __LINE__, u_errorName(status));
691 return;
692 }
693
694 pos = ubrk_first(bi);
695 TEST_ASSERT(pos == 0);
696
697 pos = ubrk_next(bi);
698 TEST_ASSERT(pos == 4);
699
700 pos = ubrk_next(bi);
701 TEST_ASSERT(pos == 5);
702
703 pos = ubrk_next(bi);
704 TEST_ASSERT(pos == 10);
705
706 pos = ubrk_next(bi);
707 TEST_ASSERT(pos == UBRK_DONE);
708 ubrk_close(bi);
709 utext_close(ut);
710 }
711
712 /*
713 * static void TestBreakIteratorTailoring(void);
714 *
715 * Test break iterator tailorings from CLDR data.
716 */
717
718 /* Thai/Lao grapheme break tailoring */
719 static const UChar thTest[] = { 0x0020, 0x0E40, 0x0E01, 0x0020,
720 0x0E01, 0x0E30, 0x0020, 0x0E01, 0x0E33, 0x0020, 0 };
721 /*in Unicode 6.1 en should behave just like th for this*/
722 /*static const int32_t thTestOffs_enFwd[] = { 1, 3, 4, 6, 7, 9, 10 };*/
723 static const int32_t thTestOffs_thFwd[] = { 1, 2, 3, 4, 5, 6, 7, 9, 10 };
724 /*static const int32_t thTestOffs_enRev[] = { 9, 7, 6, 4, 3, 1, 0 };*/
725 static const int32_t thTestOffs_thRev[] = { 9, 7, 6, 5, 4, 3, 2, 1, 0 };
726
727 /* Hebrew line break tailoring, for cldrbug 3028 */
728 static const UChar heTest[] = { 0x0020, 0x002D, 0x0031, 0x0032, 0x0020,
729 0x0061, 0x002D, 0x006B, 0x0020,
730 0x0061, 0x0300, 0x2010, 0x006B, 0x0020,
731 0x05DE, 0x05D4, 0x002D, 0x0069, 0x0020,
732 0x05D1, 0x05BC, 0x2010, 0x0047, 0x0020, 0 };
733 /*in Unicode 6.1 en should behave just like he for this*/
734 /*static const int32_t heTestOffs_enFwd[] = { 1, 5, 7, 9, 12, 14, 17, 19, 22, 24 };*/
735 static const int32_t heTestOffs_heFwd[] = { 1, 5, 7, 9, 12, 14, 19, 24 };
736 /*static const int32_t heTestOffs_enRev[] = { 22, 19, 17, 14, 12, 9, 7, 5, 1, 0 };*/
737 static const int32_t heTestOffs_heRev[] = { 19, 14, 12, 9, 7, 5, 1, 0 };
738
739 /* Finnish line break tailoring, for cldrbug 3029 */
740 static const UChar fiTest[] = { /* 00 */ 0x0020, 0x002D, 0x0031, 0x0032, 0x0020,
741 /* 05 */ 0x0061, 0x002D, 0x006B, 0x0020,
742 /* 09 */ 0x0061, 0x0300, 0x2010, 0x006B, 0x0020,
743 /* 14 */ 0x0061, 0x0020, 0x002D, 0x006B, 0x0020,
744 /* 19 */ 0x0061, 0x0300, 0x0020, 0x2010, 0x006B, 0x0020, 0 };
745 static const int32_t fiTestOffs_enFwd[] = { 1, 5, 7, 9, 12, 14, 16, 17, 19, 22, 23, 25 };
746 static const int32_t fiTestOffs_fiFwd[] = { 1, 5, 7, 9, 12, 14, 16, 19, 22, 25 };
747 static const int32_t fiTestOffs_enRev[] = { 23, 22, 19, 17, 16, 14, 12, 9, 7, 5, 1, 0 };
748 static const int32_t fiTestOffs_fiRev[] = { 22, 19, 16, 14, 12, 9, 7, 5, 1, 0 };
749
750 /* Khmer dictionary-based work break, for ICU ticket #8329 */
751 static const UChar kmTest[] = { /* 00 */ 0x179F, 0x17BC, 0x1798, 0x1785, 0x17C6, 0x178E, 0x17B6, 0x1799, 0x1796, 0x17C1,
752 /* 10 */ 0x179B, 0x1794, 0x1793, 0x17D2, 0x178F, 0x17B7, 0x1785, 0x178A, 0x17BE, 0x1798,
753 /* 20 */ 0x17D2, 0x1794, 0x17B8, 0x17A2, 0x1792, 0x17B7, 0x179F, 0x17D2, 0x178B, 0x17B6,
754 /* 30 */ 0x1793, 0x17A2, 0x179A, 0x1796, 0x17D2, 0x179A, 0x17C7, 0x1782, 0x17BB, 0x178E,
755 /* 40 */ 0x178A, 0x179B, 0x17CB, 0x1796, 0x17D2, 0x179A, 0x17C7, 0x17A2, 0x1784, 0x17D2,
756 /* 50 */ 0x1782, 0 };
757 static const int32_t kmTestOffs_kmFwd[] = { 3, /*8,*/ 11, 17, 23, 31, /*33,*/ 40, 43, 51 }; /* TODO: Investigate failure to break at offset 8 */
758 static const int32_t kmTestOffs_kmRev[] = { 43, 40, /*33,*/ 31, 23, 17, 11, /*8,*/ 3, 0 };
759
760 typedef struct {
761 const char * locale;
762 UBreakIteratorType type;
763 const UChar * test;
764 const int32_t * offsFwd;
765 const int32_t * offsRev;
766 int32_t numOffsets;
767 } RBBITailoringTest;
768
769 static const RBBITailoringTest tailoringTests[] = {
770 { "en", UBRK_CHARACTER, thTest, thTestOffs_thFwd, thTestOffs_thRev, sizeof(thTestOffs_thFwd)/sizeof(thTestOffs_thFwd[0]) },
771 { "th", UBRK_CHARACTER, thTest, thTestOffs_thFwd, thTestOffs_thRev, sizeof(thTestOffs_thFwd)/sizeof(thTestOffs_thFwd[0]) },
772 { "en", UBRK_LINE, heTest, heTestOffs_heFwd, heTestOffs_heRev, sizeof(heTestOffs_heFwd)/sizeof(heTestOffs_heFwd[0]) },
773 { "he", UBRK_LINE, heTest, heTestOffs_heFwd, heTestOffs_heRev, sizeof(heTestOffs_heFwd)/sizeof(heTestOffs_heFwd[0]) },
774 { "en", UBRK_LINE, fiTest, fiTestOffs_enFwd, fiTestOffs_enRev, sizeof(fiTestOffs_enFwd)/sizeof(fiTestOffs_enFwd[0]) },
775 { "fi", UBRK_LINE, fiTest, fiTestOffs_fiFwd, fiTestOffs_fiRev, sizeof(fiTestOffs_fiFwd)/sizeof(fiTestOffs_fiFwd[0]) },
776 { "km", UBRK_WORD, kmTest, kmTestOffs_kmFwd, kmTestOffs_kmRev, sizeof(kmTestOffs_kmFwd)/sizeof(kmTestOffs_kmFwd[0]) },
777 { NULL, 0, NULL, NULL, NULL, 0 },
778 };
779
780 static void TestBreakIteratorTailoring(void) {
781 const RBBITailoringTest * testPtr;
782 for (testPtr = tailoringTests; testPtr->locale != NULL; ++testPtr) {
783 UErrorCode status = U_ZERO_ERROR;
784 UBreakIterator* ubrkiter = ubrk_open(testPtr->type, testPtr->locale, testPtr->test, -1, &status);
785 if ( U_SUCCESS(status) ) {
786 int32_t offset, offsindx;
787 UBool foundError;
788
789 foundError = FALSE;
790 for (offsindx = 0; (offset = ubrk_next(ubrkiter)) != UBRK_DONE; ++offsindx) {
791 if (!foundError && offsindx >= testPtr->numOffsets) {
792 log_err("FAIL: locale %s, break type %d, ubrk_next expected UBRK_DONE, got %d\n",
793 testPtr->locale, testPtr->type, offset);
794 foundError = TRUE;
795 } else if (!foundError && offset != testPtr->offsFwd[offsindx]) {
796 log_err("FAIL: locale %s, break type %d, ubrk_next expected %d, got %d\n",
797 testPtr->locale, testPtr->type, testPtr->offsFwd[offsindx], offset);
798 foundError = TRUE;
799 }
800 }
801 if (!foundError && offsindx < testPtr->numOffsets) {
802 log_err("FAIL: locale %s, break type %d, ubrk_next expected %d, got UBRK_DONE\n",
803 testPtr->locale, testPtr->type, testPtr->offsFwd[offsindx]);
804 }
805
806 foundError = FALSE;
807 for (offsindx = 0; (offset = ubrk_previous(ubrkiter)) != UBRK_DONE; ++offsindx) {
808 if (!foundError && offsindx >= testPtr->numOffsets) {
809 log_err("FAIL: locale %s, break type %d, ubrk_previous expected UBRK_DONE, got %d\n",
810 testPtr->locale, testPtr->type, offset);
811 foundError = TRUE;
812 } else if (!foundError && offset != testPtr->offsRev[offsindx]) {
813 log_err("FAIL: locale %s, break type %d, ubrk_previous expected %d, got %d\n",
814 testPtr->locale, testPtr->type, testPtr->offsRev[offsindx], offset);
815 foundError = TRUE;
816 }
817 }
818 if (!foundError && offsindx < testPtr->numOffsets) {
819 log_err("FAIL: locale %s, break type %d, ubrk_previous expected %d, got UBRK_DONE\n",
820 testPtr->locale, testPtr->type, testPtr->offsRev[offsindx]);
821 }
822
823 ubrk_close(ubrkiter);
824 } else {
825 log_err_status(status, "FAIL: locale %s, break type %d, ubrk_open status: %s\n", testPtr->locale, testPtr->type, u_errorName(status));
826 }
827 }
828 }
829
830
831 static void TestBreakIteratorRefresh(void) {
832 /*
833 * RefreshInput changes out the input of a Break Iterator without
834 * changing anything else in the iterator's state. Used with Java JNI,
835 * when Java moves the underlying string storage. This test
836 * runs a ubrk_next() repeatedly, moving the text in the middle of the sequence.
837 * The right set of boundaries should still be found.
838 */
839 UChar testStr[] = {0x20, 0x41, 0x20, 0x42, 0x20, 0x43, 0x20, 0x44, 0x0}; /* = " A B C D" */
840 UChar movedStr[] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0};
841 UErrorCode status = U_ZERO_ERROR;
842 UBreakIterator *bi;
843 UText ut1 = UTEXT_INITIALIZER;
844 UText ut2 = UTEXT_INITIALIZER;
845
846 bi = ubrk_open(UBRK_LINE, "en_US", NULL, 0, &status);
847 TEST_ASSERT_SUCCESS(status);
848
849 utext_openUChars(&ut1, testStr, -1, &status);
850 TEST_ASSERT_SUCCESS(status);
851 ubrk_setUText(bi, &ut1, &status);
852 TEST_ASSERT_SUCCESS(status);
853
854 if (U_SUCCESS(status)) {
855 /* Line boundaries will occur before each letter in the original string */
856 TEST_ASSERT(1 == ubrk_next(bi));
857 TEST_ASSERT(3 == ubrk_next(bi));
858
859 /* Move the string, kill the original string. */
860 u_strcpy(movedStr, testStr);
861 u_memset(testStr, 0x20, u_strlen(testStr));
862 utext_openUChars(&ut2, movedStr, -1, &status);
863 TEST_ASSERT_SUCCESS(status);
864 ubrk_refreshUText(bi, &ut2, &status);
865 TEST_ASSERT_SUCCESS(status);
866
867 /* Find the following matches, now working in the moved string. */
868 TEST_ASSERT(5 == ubrk_next(bi));
869 TEST_ASSERT(7 == ubrk_next(bi));
870 TEST_ASSERT(8 == ubrk_next(bi));
871 TEST_ASSERT(UBRK_DONE == ubrk_next(bi));
872 TEST_ASSERT_SUCCESS(status);
873
874 utext_close(&ut1);
875 utext_close(&ut2);
876 }
877 ubrk_close(bi);
878 }
879
880 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */