]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/cbiapts.c
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cbiapts.c
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2003, 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 "unicode/uloc.h"
27#include "unicode/ubrk.h"
28#include "unicode/ustring.h"
29#include "unicode/ucnv.h"
30#include "cintltst.h"
31#include "cbiapts.h"
32
33static void TestBreakIteratorSafeClone(void);
34static void TestBreakIteratorRules(void);
35static void TestBreakIteratorRuleError(void);
36
37void addBrkIterAPITest(TestNode** root);
38
39void addBrkIterAPITest(TestNode** root)
40{
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");
45}
46
47#define CLONETEST_ITERATOR_COUNT 2
48
49/*
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.
54 */
55typedef struct StringStruct {
56 struct StringStruct *link;
57 UChar str[1];
58 } StringStruct;
59
60
61static UChar* toUChar(const char *src, void **freeHook) {
62 /* Structure of the memory that we allocate on the heap */
63
64 int32_t numUChars;
65 int32_t destSize;
66 UChar stackBuf[2000 + sizeof(void *)/sizeof(UChar)];
67 StringStruct *dest;
68 UConverter *cnv;
69
70 UErrorCode status = U_ZERO_ERROR;
71 if (src == NULL) {
72 return NULL;
73 };
74
75 cnv = ucnv_open(NULL, &status);
76 if(U_FAILURE(status) || cnv == NULL) {
77 return NULL;
78 }
79 ucnv_reset(cnv);
80 numUChars = ucnv_toUChars(cnv,
81 stackBuf,
82 2000,
83 src, -1,
84 &status);
85
86 destSize = (numUChars+1) * sizeof(UChar) + sizeof(struct StringStruct);
87 dest = (StringStruct *)malloc(destSize);
88 if (dest != NULL) {
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);
93 } else {
94 free(dest);
95 dest = NULL;
96 }
97 }
98
99 ucnv_reset(cnv); /* be good citizens */
100 ucnv_close(cnv);
101 if (dest == NULL) {
102 return NULL;
103 }
104
105 dest->link = (StringStruct*)(*freeHook);
106 *freeHook = dest;
107 return dest->str;
108}
109
110static void freeToUCharStrings(void **hook) {
111 StringStruct *s = *(StringStruct **)hook;
112 while (s != NULL) {
113 StringStruct *next = s->link;
114 free(s);
115 s = next;
116 }
117}
118
119
120static void TestBreakIteratorCAPI()
121{
122 UErrorCode status = U_ZERO_ERROR;
123 UBreakIterator *word, *sentence, *line, *character, *b, *bogus;
124 int32_t start,pos,end,to;
125 int32_t i;
126 int32_t count = 0;
127
128 UChar text[50];
129
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
133 tests. */
134 u_uastrcpy(text, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah");
135
136
137/*test ubrk_open()*/
138 log_verbose("\nTesting BreakIterator open functions\n");
139
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");
144 return;
145 } else if(U_FAILURE(status)){
146 log_err("FAIL: Error in ubrk_open() for word breakiterator: %s\n", myErrorName(status));
147 }
148 else{
149 log_verbose("PASS: Successfully opened word breakiterator\n");
150 }
151
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));
155 }
156 else{
157 log_verbose("PASS: Successfully opened sentence breakiterator\n");
158 }
159
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));
163 }
164 else{
165 log_verbose("PASS: Successfully opened line breakiterator\n");
166 }
167
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));
171 }
172 else{
173 log_verbose("PASS: Successfully opened character breakiterator\n");
174 }
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");
179 }
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));
183 }
184 }
185 status=U_ZERO_ERROR;
186
187
188/* ======= Test ubrk_countAvialable() and ubrk_getAvialable() */
189
190 log_verbose("\nTesting ubrk_countAvailable() and ubrk_getAvailable()\n");
191 count=ubrk_countAvailable();
192 /* use something sensible w/o hardcoding the count */
193 if(count < 0){
194 log_err("FAIL: Error in ubrk_countAvialable() returned %d\n", count);
195 }
196 else{
197 log_verbose("PASS: ubrk_countAvialable() successful returned %d\n", count);
198 }
199 for(i=0;i<count;i++)
200 {
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");
204 else
205 log_verbose("A locale %s for which breakiterator is applicable\n",ubrk_getAvailable(i));
206 }
207
208/*========Test ubrk_first(), ubrk_last()...... and other functions*/
209
210 log_verbose("\nTesting the functions for word\n");
211 start = ubrk_first(word);
212 if(start!=0)
213 log_err("error ubrk_start(word) did not return 0\n");
214 log_verbose("first (word = %d\n", (int32_t)start);
215 pos=ubrk_next(word);
216 if(pos!=4)
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);
220 if(pos!=5)
221 log_err("error ubrl_following(word,4) did not return 6\n");
222 log_verbose("next (word = %d\n", (int32_t)pos);
223 end=ubrk_last(word);
224 if(end!=49)
225 log_err("error ubrk_last(word) did not return 49\n");
226 log_verbose("last (word = %d\n", (int32_t)end);
227
228 pos=ubrk_previous(word);
229 log_verbose("%d %d\n", end, pos);
230
231 pos=ubrk_previous(word);
232 log_verbose("%d \n", pos);
233
234 if (ubrk_isBoundary(word, 2) != FALSE) {
235 log_err("error ubrk_isBoundary(word, 2) did not return FALSE\n");
236 }
237 pos=ubrk_current(word);
238 if (pos != 4) {
239 log_err("error ubrk_current() != 4 after ubrk_isBoundary(word, 2)\n");
240 }
241 if (ubrk_isBoundary(word, 4) != TRUE) {
242 log_err("error ubrk_isBoundary(word, 4) did not return TRUE\n");
243 }
244
245
246
247 log_verbose("\nTesting the functions for character\n");
248 ubrk_first(character);
249 pos = ubrk_following(character, 5);
250 if(pos!=6)
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);
254 if(pos!=19)
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);
258 if(pos!=21)
259 log_err("error ubrk_preceding(character,22) did not return 21\n");
260 log_verbose("preceding(character,22) = %d\n", (int32_t)pos);
261
262
263 log_verbose("\nTesting the functions for line\n");
264 pos=ubrk_first(line);
265 if(pos != 0)
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);
269 if(pos!=22)
270 log_err("error ubrk_following(line) did not return 22\n");
271 log_verbose("following (line) = %d\n", (int32_t)pos);
272
273
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);
279 if(pos!=49)
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");
289 }
290
291
292 /*---- */
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));
298 }
299 ubrk_close(b);
300
301 ubrk_close(word);
302 ubrk_close(sentence);
303 ubrk_close(line);
304 ubrk_close(character);
305}
306
307static void TestBreakIteratorSafeClone(void)
308{
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;
313
314 UBreakIterator * someIterators [CLONETEST_ITERATOR_COUNT];
315 UBreakIterator * someClonedIterators [CLONETEST_ITERATOR_COUNT];
316
317 UBreakIterator * brk;
318 UErrorCode status = U_ZERO_ERROR;
319 int32_t start,pos;
320 int32_t i;
321
322 /*Testing ubrk_safeClone */
323
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
327 tests. */
328 u_uastrcpy(text, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah");
329
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));
334 return;
335 }
336
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));
340 return;
341 }
342
343 /* test each type of iterator */
344 for (i = 0; i < CLONETEST_ITERATOR_COUNT; i++)
345 {
346
347 /* Check the various error & informational states */
348
349 /* Null status - just returns NULL */
350 if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, 0))
351 {
352 log_err("FAIL: Cloned Iterator failed to deal correctly with null status\n");
353 }
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)
357 {
358 log_err("FAIL: Cloned Iterator failed to deal correctly with incoming error status\n");
359 }
360 status = U_ZERO_ERROR;
361
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)
364 {
365 log_err("FAIL: Cloned Iterator failed to deal correctly with null bufferSize pointer\n");
366 }
367 status = U_ZERO_ERROR;
368
369 /* buffer size pointer is 0 - fill in pbufferSize with a size */
370 bufferSize = 0;
371 if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status) || U_FAILURE(status) || bufferSize <= 0)
372 {
373 log_err("FAIL: Cloned Iterator failed a sizing request ('preflighting')\n");
374 }
375 /* Verify our define is large enough */
376 if (U_BRK_SAFECLONE_BUFFERSIZE < bufferSize)
377 {
378 log_err("FAIL: Pre-calculated buffer size is too small\n");
379 }
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))
382 {
383 log_err("FAIL: Iterator can't be cloned with run-time size\n");
384 }
385 if (brk)
386 ubrk_close(brk);
387 /* size one byte too small - should allocate & let us know */
388 --bufferSize;
389 if (0 == (brk = ubrk_safeClone(someIterators[i], 0, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_WARNING)
390 {
391 log_err("FAIL: Cloned Iterator failed to deal correctly with too-small buffer size\n");
392 }
393 if (brk)
394 ubrk_close(brk);
395 status = U_ZERO_ERROR;
396 bufferSize = U_BRK_SAFECLONE_BUFFERSIZE;
397
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)
400 {
401 log_err("FAIL: Cloned Iterator failed to deal correctly with null buffer pointer\n");
402 }
403 if (brk)
404 ubrk_close(brk);
405 status = U_ZERO_ERROR;
406
407 /* Mis-aligned buffer pointer. */
408 {
409 char stackBuf[U_BRK_SAFECLONE_BUFFERSIZE+sizeof(void *)];
410 void *p;
411 int32_t offset;
412
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");
416 }
417 if (status == U_SAFECLONE_ALLOCATED_WARNING) {
418 log_err("FAIL: Cloned Iterator allocated when using a mis-aligned buffer.\n");
419 }
420 offset = (int32_t)((char *)&p-(char*)brk);
421 if (offset < 0) {
422 offset = -offset;
423 }
424 if (offset % sizeof(void *) != 0) {
425 log_err("FAIL: Cloned Iterator failed to align correctly with misaligned buffer pointer\n");
426 }
427 if (brk)
428 ubrk_close(brk);
429 }
430
431
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)
434 {
435 log_err("FAIL: Cloned Iterator failed to deal correctly with null Iterator pointer\n");
436 }
437 status = U_ZERO_ERROR;
438
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);
442
443 start = ubrk_first(someClonedIterators[i]);
444 if(start!=0)
445 log_err("error ubrk_start(clone) did not return 0\n");
446 pos=ubrk_next(someClonedIterators[i]);
447 if(pos!=4)
448 log_err("error ubrk_next(clone) did not return 4\n");
449
450 ubrk_close(someClonedIterators[i]);
451 ubrk_close(someIterators[i]);
452 }
453}
454
455
456/*
457// Open a break iterator from char * rules. Take care of conversion
458// of the rules and error checking.
459*/
460static UBreakIterator * testOpenRules(char *rules) {
461 UErrorCode status = U_ZERO_ERROR;
462 UChar *ruleSourceU = NULL;
463 void *strCleanUp = NULL;
464 UParseError parseErr;
465 UBreakIterator *bi;
466
467 ruleSourceU = toUChar(rules, &strCleanUp);
468
469 bi = ubrk_openRules(ruleSourceU, -1, /* The rules */
470 NULL, -1, /* The text to be iterated over. */
471 &parseErr, &status);
472
473 if (U_FAILURE(status)) {
474 log_err("FAIL: ubrk_openRules: ICU Error \"%s\"\n", u_errorName(status));
475 bi = 0;
476 };
477 freeToUCharStrings(&strCleanUp);
478 return bi;
479
480}
481
482/*
483 * TestBreakIteratorRules - Verify that a break iterator can be created from
484 * a set of source rules.
485 */
486static 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.
490 */
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};
497
498 UChar *uData;
499 void *freeHook = NULL;
500 UErrorCode status = U_ZERO_ERROR;
501 int32_t pos;
502 int i;
503
504 UBreakIterator *bi = testOpenRules(rules);
505 if (bi == NULL) {return;}
506 uData = toUChar(data, &freeHook);
507 ubrk_setText(bi, uData, -1, &status);
508
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);
513 break;
514 }
515 if (pos != i && breaks[i] == '*') {
516 log_err("FAIL: expected break at position %d not found.\n", i);
517 break;
518 }
519 if (pos == 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);
526 break;
527 }
528 pos = ubrk_next(bi);
529 }
530 }
531
532 freeToUCharStrings(&freeHook);
533 ubrk_close(bi);
534}
535
536static void TestBreakIteratorRuleError() {
537/*
538 * TestBreakIteratorRuleError - Try to create a BI from rules with syntax errors,
539 * check that the error is reported correctly.
540 */
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";
544 UChar *uRules;
545 void *freeHook = NULL;
546 UErrorCode status = U_ZERO_ERROR;
547 UParseError parseErr;
548 UBreakIterator *bi;
549
550 uRules = toUChar(rules, &freeHook);
551 bi = ubrk_openRules(uRules, -1, /* The rules */
552 NULL, -1, /* The text to be iterated over. */
553 &parseErr, &status);
554 if (U_SUCCESS(status)) {
555 log_err("FAIL: construction of break iterator succeeded when it should have failed.\n");
556 ubrk_close(bi);
557 } else {
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);
561 }
562 }
563 freeToUCharStrings(&freeHook);
564}
565
566#endif /* #if !UCONFIG_NO_BREAK_ITERATION */