]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /******************************************************************** |
2 | * COPYRIGHT: | |
374ca955 | 3 | * Copyright (c) 1997-2004, International Business Machines Corporation and |
b75a7d8f A |
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> | |
374ca955 | 26 | #include <string.h> |
b75a7d8f A |
27 | #include "unicode/uloc.h" |
28 | #include "unicode/ubrk.h" | |
29 | #include "unicode/ustring.h" | |
30 | #include "unicode/ucnv.h" | |
31 | #include "cintltst.h" | |
32 | #include "cbiapts.h" | |
33 | ||
374ca955 A |
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));}} | |
36 | ||
37 | #define TEST_ASSERT(expr) {if ((expr)==FALSE) { \ | |
38 | log_err("Test Failure at file %s, line %d\n", __FILE__, __LINE__);}} | |
39 | ||
b75a7d8f A |
40 | static void TestBreakIteratorSafeClone(void); |
41 | static void TestBreakIteratorRules(void); | |
42 | static void TestBreakIteratorRuleError(void); | |
374ca955 | 43 | static void TestBreakIteratorStatusVec(void); |
b75a7d8f A |
44 | |
45 | void addBrkIterAPITest(TestNode** root); | |
46 | ||
47 | void addBrkIterAPITest(TestNode** root) | |
48 | { | |
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"); | |
374ca955 | 53 | addTest(root, &TestBreakIteratorStatusVec, "tstxtbd/cbiapts/TestBreakIteratorStatusVec"); |
b75a7d8f A |
54 | } |
55 | ||
56 | #define CLONETEST_ITERATOR_COUNT 2 | |
57 | ||
58 | /* | |
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. | |
63 | */ | |
64 | typedef struct StringStruct { | |
65 | struct StringStruct *link; | |
66 | UChar str[1]; | |
67 | } StringStruct; | |
68 | ||
69 | ||
70 | static UChar* toUChar(const char *src, void **freeHook) { | |
71 | /* Structure of the memory that we allocate on the heap */ | |
72 | ||
73 | int32_t numUChars; | |
74 | int32_t destSize; | |
75 | UChar stackBuf[2000 + sizeof(void *)/sizeof(UChar)]; | |
76 | StringStruct *dest; | |
77 | UConverter *cnv; | |
78 | ||
79 | UErrorCode status = U_ZERO_ERROR; | |
80 | if (src == NULL) { | |
81 | return NULL; | |
82 | }; | |
83 | ||
374ca955 | 84 | cnv = ucnv_open(NULL, &status); |
b75a7d8f A |
85 | if(U_FAILURE(status) || cnv == NULL) { |
86 | return NULL; | |
87 | } | |
88 | ucnv_reset(cnv); | |
89 | numUChars = ucnv_toUChars(cnv, | |
90 | stackBuf, | |
91 | 2000, | |
92 | src, -1, | |
93 | &status); | |
94 | ||
95 | destSize = (numUChars+1) * sizeof(UChar) + sizeof(struct StringStruct); | |
96 | dest = (StringStruct *)malloc(destSize); | |
97 | if (dest != NULL) { | |
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); | |
102 | } else { | |
103 | free(dest); | |
104 | dest = NULL; | |
105 | } | |
106 | } | |
107 | ||
108 | ucnv_reset(cnv); /* be good citizens */ | |
109 | ucnv_close(cnv); | |
110 | if (dest == NULL) { | |
111 | return NULL; | |
112 | } | |
113 | ||
114 | dest->link = (StringStruct*)(*freeHook); | |
115 | *freeHook = dest; | |
116 | return dest->str; | |
117 | } | |
118 | ||
119 | static void freeToUCharStrings(void **hook) { | |
120 | StringStruct *s = *(StringStruct **)hook; | |
121 | while (s != NULL) { | |
122 | StringStruct *next = s->link; | |
123 | free(s); | |
124 | s = next; | |
125 | } | |
126 | } | |
127 | ||
128 | ||
129 | static void TestBreakIteratorCAPI() | |
130 | { | |
131 | UErrorCode status = U_ZERO_ERROR; | |
132 | UBreakIterator *word, *sentence, *line, *character, *b, *bogus; | |
133 | int32_t start,pos,end,to; | |
134 | int32_t i; | |
135 | int32_t count = 0; | |
136 | ||
137 | UChar text[50]; | |
138 | ||
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 | |
142 | tests. */ | |
143 | u_uastrcpy(text, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah"); | |
144 | ||
145 | ||
146 | /*test ubrk_open()*/ | |
147 | log_verbose("\nTesting BreakIterator open functions\n"); | |
148 | ||
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) { | |
374ca955 A |
152 | log_data_err("Check your data - it doesn't seem to be around\n"); |
153 | return; | |
b75a7d8f A |
154 | } else if(U_FAILURE(status)){ |
155 | log_err("FAIL: Error in ubrk_open() for word breakiterator: %s\n", myErrorName(status)); | |
156 | } | |
157 | else{ | |
158 | log_verbose("PASS: Successfully opened word breakiterator\n"); | |
159 | } | |
160 | ||
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)); | |
374ca955 | 164 | return; |
b75a7d8f A |
165 | } |
166 | else{ | |
167 | log_verbose("PASS: Successfully opened sentence breakiterator\n"); | |
168 | } | |
169 | ||
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)); | |
374ca955 | 173 | return; |
b75a7d8f A |
174 | } |
175 | else{ | |
176 | log_verbose("PASS: Successfully opened line breakiterator\n"); | |
177 | } | |
178 | ||
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)); | |
374ca955 | 182 | return; |
b75a7d8f A |
183 | } |
184 | else{ | |
185 | log_verbose("PASS: Successfully opened character breakiterator\n"); | |
186 | } | |
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"); | |
191 | } | |
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)); | |
195 | } | |
196 | } | |
197 | status=U_ZERO_ERROR; | |
198 | ||
199 | ||
200 | /* ======= Test ubrk_countAvialable() and ubrk_getAvialable() */ | |
201 | ||
202 | log_verbose("\nTesting ubrk_countAvailable() and ubrk_getAvailable()\n"); | |
203 | count=ubrk_countAvailable(); | |
204 | /* use something sensible w/o hardcoding the count */ | |
205 | if(count < 0){ | |
206 | log_err("FAIL: Error in ubrk_countAvialable() returned %d\n", count); | |
207 | } | |
208 | else{ | |
209 | log_verbose("PASS: ubrk_countAvialable() successful returned %d\n", count); | |
210 | } | |
211 | for(i=0;i<count;i++) | |
212 | { | |
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"); | |
216 | else | |
217 | log_verbose("A locale %s for which breakiterator is applicable\n",ubrk_getAvailable(i)); | |
218 | } | |
219 | ||
220 | /*========Test ubrk_first(), ubrk_last()...... and other functions*/ | |
221 | ||
222 | log_verbose("\nTesting the functions for word\n"); | |
223 | start = ubrk_first(word); | |
224 | if(start!=0) | |
225 | log_err("error ubrk_start(word) did not return 0\n"); | |
226 | log_verbose("first (word = %d\n", (int32_t)start); | |
227 | pos=ubrk_next(word); | |
228 | if(pos!=4) | |
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); | |
232 | if(pos!=5) | |
233 | log_err("error ubrl_following(word,4) did not return 6\n"); | |
234 | log_verbose("next (word = %d\n", (int32_t)pos); | |
235 | end=ubrk_last(word); | |
236 | if(end!=49) | |
237 | log_err("error ubrk_last(word) did not return 49\n"); | |
238 | log_verbose("last (word = %d\n", (int32_t)end); | |
239 | ||
240 | pos=ubrk_previous(word); | |
241 | log_verbose("%d %d\n", end, pos); | |
242 | ||
243 | pos=ubrk_previous(word); | |
244 | log_verbose("%d \n", pos); | |
245 | ||
246 | if (ubrk_isBoundary(word, 2) != FALSE) { | |
247 | log_err("error ubrk_isBoundary(word, 2) did not return FALSE\n"); | |
248 | } | |
249 | pos=ubrk_current(word); | |
250 | if (pos != 4) { | |
251 | log_err("error ubrk_current() != 4 after ubrk_isBoundary(word, 2)\n"); | |
252 | } | |
253 | if (ubrk_isBoundary(word, 4) != TRUE) { | |
254 | log_err("error ubrk_isBoundary(word, 4) did not return TRUE\n"); | |
255 | } | |
256 | ||
257 | ||
258 | ||
259 | log_verbose("\nTesting the functions for character\n"); | |
260 | ubrk_first(character); | |
261 | pos = ubrk_following(character, 5); | |
262 | if(pos!=6) | |
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); | |
266 | if(pos!=19) | |
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); | |
270 | if(pos!=21) | |
271 | log_err("error ubrk_preceding(character,22) did not return 21\n"); | |
272 | log_verbose("preceding(character,22) = %d\n", (int32_t)pos); | |
273 | ||
274 | ||
275 | log_verbose("\nTesting the functions for line\n"); | |
276 | pos=ubrk_first(line); | |
277 | if(pos != 0) | |
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); | |
281 | if(pos!=22) | |
282 | log_err("error ubrk_following(line) did not return 22\n"); | |
283 | log_verbose("following (line) = %d\n", (int32_t)pos); | |
284 | ||
285 | ||
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); | |
291 | if(pos!=49) | |
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"); | |
301 | } | |
302 | ||
303 | ||
304 | /*---- */ | |
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)); | |
310 | } | |
311 | ubrk_close(b); | |
312 | ||
313 | ubrk_close(word); | |
314 | ubrk_close(sentence); | |
315 | ubrk_close(line); | |
316 | ubrk_close(character); | |
317 | } | |
318 | ||
319 | static void TestBreakIteratorSafeClone(void) | |
320 | { | |
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; | |
325 | ||
326 | UBreakIterator * someIterators [CLONETEST_ITERATOR_COUNT]; | |
327 | UBreakIterator * someClonedIterators [CLONETEST_ITERATOR_COUNT]; | |
328 | ||
329 | UBreakIterator * brk; | |
330 | UErrorCode status = U_ZERO_ERROR; | |
331 | int32_t start,pos; | |
332 | int32_t i; | |
333 | ||
334 | /*Testing ubrk_safeClone */ | |
335 | ||
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 | |
339 | tests. */ | |
340 | u_uastrcpy(text, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah"); | |
341 | ||
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)); | |
346 | return; | |
347 | } | |
348 | ||
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)); | |
352 | return; | |
353 | } | |
354 | ||
355 | /* test each type of iterator */ | |
356 | for (i = 0; i < CLONETEST_ITERATOR_COUNT; i++) | |
357 | { | |
358 | ||
359 | /* Check the various error & informational states */ | |
360 | ||
361 | /* Null status - just returns NULL */ | |
362 | if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, 0)) | |
363 | { | |
364 | log_err("FAIL: Cloned Iterator failed to deal correctly with null status\n"); | |
365 | } | |
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) | |
369 | { | |
370 | log_err("FAIL: Cloned Iterator failed to deal correctly with incoming error status\n"); | |
371 | } | |
372 | status = U_ZERO_ERROR; | |
373 | ||
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) | |
376 | { | |
377 | log_err("FAIL: Cloned Iterator failed to deal correctly with null bufferSize pointer\n"); | |
378 | } | |
379 | status = U_ZERO_ERROR; | |
380 | ||
381 | /* buffer size pointer is 0 - fill in pbufferSize with a size */ | |
382 | bufferSize = 0; | |
383 | if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status) || U_FAILURE(status) || bufferSize <= 0) | |
384 | { | |
385 | log_err("FAIL: Cloned Iterator failed a sizing request ('preflighting')\n"); | |
386 | } | |
387 | /* Verify our define is large enough */ | |
388 | if (U_BRK_SAFECLONE_BUFFERSIZE < bufferSize) | |
389 | { | |
390 | log_err("FAIL: Pre-calculated buffer size is too small\n"); | |
391 | } | |
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)) | |
394 | { | |
395 | log_err("FAIL: Iterator can't be cloned with run-time size\n"); | |
396 | } | |
397 | if (brk) | |
398 | ubrk_close(brk); | |
399 | /* size one byte too small - should allocate & let us know */ | |
400 | --bufferSize; | |
401 | if (0 == (brk = ubrk_safeClone(someIterators[i], 0, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_WARNING) | |
402 | { | |
403 | log_err("FAIL: Cloned Iterator failed to deal correctly with too-small buffer size\n"); | |
404 | } | |
405 | if (brk) | |
406 | ubrk_close(brk); | |
407 | status = U_ZERO_ERROR; | |
408 | bufferSize = U_BRK_SAFECLONE_BUFFERSIZE; | |
409 | ||
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) | |
412 | { | |
413 | log_err("FAIL: Cloned Iterator failed to deal correctly with null buffer pointer\n"); | |
414 | } | |
415 | if (brk) | |
416 | ubrk_close(brk); | |
417 | status = U_ZERO_ERROR; | |
418 | ||
419 | /* Mis-aligned buffer pointer. */ | |
420 | { | |
421 | char stackBuf[U_BRK_SAFECLONE_BUFFERSIZE+sizeof(void *)]; | |
422 | void *p; | |
423 | int32_t offset; | |
424 | ||
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"); | |
428 | } | |
429 | if (status == U_SAFECLONE_ALLOCATED_WARNING) { | |
430 | log_err("FAIL: Cloned Iterator allocated when using a mis-aligned buffer.\n"); | |
431 | } | |
432 | offset = (int32_t)((char *)&p-(char*)brk); | |
433 | if (offset < 0) { | |
434 | offset = -offset; | |
435 | } | |
436 | if (offset % sizeof(void *) != 0) { | |
437 | log_err("FAIL: Cloned Iterator failed to align correctly with misaligned buffer pointer\n"); | |
438 | } | |
439 | if (brk) | |
440 | ubrk_close(brk); | |
441 | } | |
442 | ||
443 | ||
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) | |
446 | { | |
447 | log_err("FAIL: Cloned Iterator failed to deal correctly with null Iterator pointer\n"); | |
448 | } | |
449 | status = U_ZERO_ERROR; | |
450 | ||
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); | |
454 | ||
455 | start = ubrk_first(someClonedIterators[i]); | |
456 | if(start!=0) | |
457 | log_err("error ubrk_start(clone) did not return 0\n"); | |
458 | pos=ubrk_next(someClonedIterators[i]); | |
459 | if(pos!=4) | |
460 | log_err("error ubrk_next(clone) did not return 4\n"); | |
461 | ||
462 | ubrk_close(someClonedIterators[i]); | |
463 | ubrk_close(someIterators[i]); | |
464 | } | |
465 | } | |
466 | ||
467 | ||
468 | /* | |
469 | // Open a break iterator from char * rules. Take care of conversion | |
470 | // of the rules and error checking. | |
471 | */ | |
472 | static UBreakIterator * testOpenRules(char *rules) { | |
473 | UErrorCode status = U_ZERO_ERROR; | |
474 | UChar *ruleSourceU = NULL; | |
475 | void *strCleanUp = NULL; | |
476 | UParseError parseErr; | |
477 | UBreakIterator *bi; | |
478 | ||
479 | ruleSourceU = toUChar(rules, &strCleanUp); | |
480 | ||
481 | bi = ubrk_openRules(ruleSourceU, -1, /* The rules */ | |
482 | NULL, -1, /* The text to be iterated over. */ | |
483 | &parseErr, &status); | |
484 | ||
485 | if (U_FAILURE(status)) { | |
486 | log_err("FAIL: ubrk_openRules: ICU Error \"%s\"\n", u_errorName(status)); | |
487 | bi = 0; | |
488 | }; | |
489 | freeToUCharStrings(&strCleanUp); | |
490 | return bi; | |
491 | ||
492 | } | |
493 | ||
494 | /* | |
495 | * TestBreakIteratorRules - Verify that a break iterator can be created from | |
496 | * a set of source rules. | |
497 | */ | |
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. | |
502 | */ | |
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}; | |
509 | ||
510 | UChar *uData; | |
511 | void *freeHook = NULL; | |
512 | UErrorCode status = U_ZERO_ERROR; | |
513 | int32_t pos; | |
514 | int i; | |
515 | ||
516 | UBreakIterator *bi = testOpenRules(rules); | |
517 | if (bi == NULL) {return;} | |
518 | uData = toUChar(data, &freeHook); | |
519 | ubrk_setText(bi, uData, -1, &status); | |
520 | ||
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); | |
525 | break; | |
526 | } | |
527 | if (pos != i && breaks[i] == '*') { | |
528 | log_err("FAIL: expected break at position %d not found.\n", i); | |
529 | break; | |
530 | } | |
531 | if (pos == 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); | |
538 | break; | |
539 | } | |
540 | pos = ubrk_next(bi); | |
541 | } | |
542 | } | |
543 | ||
544 | freeToUCharStrings(&freeHook); | |
545 | ubrk_close(bi); | |
546 | } | |
547 | ||
548 | static void TestBreakIteratorRuleError() { | |
549 | /* | |
550 | * TestBreakIteratorRuleError - Try to create a BI from rules with syntax errors, | |
551 | * check that the error is reported correctly. | |
552 | */ | |
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"; | |
556 | UChar *uRules; | |
557 | void *freeHook = NULL; | |
558 | UErrorCode status = U_ZERO_ERROR; | |
559 | UParseError parseErr; | |
560 | UBreakIterator *bi; | |
561 | ||
562 | uRules = toUChar(rules, &freeHook); | |
563 | bi = ubrk_openRules(uRules, -1, /* The rules */ | |
564 | NULL, -1, /* The text to be iterated over. */ | |
565 | &parseErr, &status); | |
566 | if (U_SUCCESS(status)) { | |
567 | log_err("FAIL: construction of break iterator succeeded when it should have failed.\n"); | |
568 | ubrk_close(bi); | |
569 | } else { | |
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); | |
573 | } | |
574 | } | |
575 | freeToUCharStrings(&freeHook); | |
576 | } | |
577 | ||
374ca955 A |
578 | |
579 | /* | |
580 | * TestsBreakIteratorStatusVals() Test the ubrk_getRuleStatusVec() funciton | |
581 | */ | |
582 | static void TestBreakIteratorStatusVec() { | |
583 | #define RULE_STRING_LENGTH 200 | |
584 | UChar rules[RULE_STRING_LENGTH]; | |
585 | ||
586 | #define TEST_STRING_LENGTH 25 | |
587 | UChar testString[TEST_STRING_LENGTH]; | |
588 | UBreakIterator *bi = NULL; | |
589 | int32_t pos = 0; | |
590 | int32_t vals[10]; | |
591 | int32_t numVals; | |
592 | UErrorCode status = U_ZERO_ERROR; | |
593 | ||
594 | u_uastrncpy(rules, "[A-N]{100}; \n" | |
595 | "[a-w]{200}; \n" | |
596 | "[\\p{L}]{300}; \n" | |
597 | "[\\p{N}]{400}; \n" | |
598 | "[0-5]{500}; \n" | |
599 | "!.*;\n", RULE_STRING_LENGTH); | |
600 | u_uastrncpy(testString, "ABC", TEST_STRING_LENGTH); | |
601 | ||
602 | ||
603 | bi = ubrk_openRules(rules, -1, testString, -1, NULL, &status); | |
604 | TEST_ASSET_SUCCESS(status); | |
605 | TEST_ASSERT(bi != NULL); | |
606 | ||
607 | pos = ubrk_next(bi); | |
608 | TEST_ASSERT(pos == 1); | |
609 | ||
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); | |
617 | ||
618 | numVals = ubrk_getRuleStatusVec(bi, vals, 0, &status); | |
619 | TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR); | |
620 | TEST_ASSERT(numVals == 2); | |
621 | ||
622 | ubrk_close(bi); | |
623 | } | |
624 | ||
625 | ||
b75a7d8f | 626 | #endif /* #if !UCONFIG_NO_BREAK_ITERATION */ |