]>
Commit | Line | Data |
---|---|---|
b75a7d8f | 1 | /******************************************************************** |
2ca993e8 A |
2 | * COPYRIGHT: |
3 | * Copyright (c) 1997-2016, International Business Machines Corporation and | |
b75a7d8f A |
4 | * others. All Rights Reserved. |
5 | ********************************************************************/ | |
6 | /******************************************************************************** | |
7 | * | |
8 | * File CBIAPTS.C | |
9 | * | |
10 | * Modification History: | |
2ca993e8 | 11 | * Name Description |
b75a7d8f A |
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" | |
73c04bcf | 31 | #include "unicode/utext.h" |
b75a7d8f A |
32 | #include "cintltst.h" |
33 | #include "cbiapts.h" | |
2ca993e8 | 34 | #include "cmemory.h" |
b75a7d8f | 35 | |
73c04bcf | 36 | #define TEST_ASSERT_SUCCESS(status) {if (U_FAILURE(status)) { \ |
729e4ab9 | 37 | log_data_err("Failure at file %s, line %d, error = %s (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status));}} |
374ca955 A |
38 | |
39 | #define TEST_ASSERT(expr) {if ((expr)==FALSE) { \ | |
729e4ab9 | 40 | log_data_err("Test Failure at file %s, line %d (Are you missing data?)\n", __FILE__, __LINE__);}} |
374ca955 | 41 | |
51004dcb | 42 | #if !UCONFIG_NO_FILE_IO |
b75a7d8f | 43 | static void TestBreakIteratorSafeClone(void); |
51004dcb | 44 | #endif |
b75a7d8f A |
45 | static void TestBreakIteratorRules(void); |
46 | static void TestBreakIteratorRuleError(void); | |
374ca955 | 47 | static void TestBreakIteratorStatusVec(void); |
73c04bcf | 48 | static void TestBreakIteratorUText(void); |
729e4ab9 | 49 | static void TestBreakIteratorTailoring(void); |
4388f060 | 50 | static void TestBreakIteratorRefresh(void); |
2ca993e8 A |
51 | static void TestBug11665(void); |
52 | static void TestBreakIteratorSuppressions(void); | |
b75a7d8f A |
53 | |
54 | void addBrkIterAPITest(TestNode** root); | |
55 | ||
56 | void addBrkIterAPITest(TestNode** root) | |
57 | { | |
729e4ab9 | 58 | #if !UCONFIG_NO_FILE_IO |
b75a7d8f A |
59 | addTest(root, &TestBreakIteratorCAPI, "tstxtbd/cbiapts/TestBreakIteratorCAPI"); |
60 | addTest(root, &TestBreakIteratorSafeClone, "tstxtbd/cbiapts/TestBreakIteratorSafeClone"); | |
729e4ab9 A |
61 | addTest(root, &TestBreakIteratorUText, "tstxtbd/cbiapts/TestBreakIteratorUText"); |
62 | #endif | |
b75a7d8f A |
63 | addTest(root, &TestBreakIteratorRules, "tstxtbd/cbiapts/TestBreakIteratorRules"); |
64 | addTest(root, &TestBreakIteratorRuleError, "tstxtbd/cbiapts/TestBreakIteratorRuleError"); | |
374ca955 | 65 | addTest(root, &TestBreakIteratorStatusVec, "tstxtbd/cbiapts/TestBreakIteratorStatusVec"); |
729e4ab9 | 66 | addTest(root, &TestBreakIteratorTailoring, "tstxtbd/cbiapts/TestBreakIteratorTailoring"); |
4388f060 | 67 | addTest(root, &TestBreakIteratorRefresh, "tstxtbd/cbiapts/TestBreakIteratorRefresh"); |
2ca993e8 | 68 | addTest(root, &TestBug11665, "tstxtbd/cbiapts/TestBug11665"); |
b331163b | 69 | addTest(root, &TestBreakIteratorSuppressions, "tstxtbd/cbiapts/TestBreakIteratorSuppressions"); |
b75a7d8f A |
70 | } |
71 | ||
72 | #define CLONETEST_ITERATOR_COUNT 2 | |
73 | ||
74 | /* | |
75 | * Utility function for converting char * to UChar * strings, to | |
76 | * simplify the test code. Converted strings are put in heap allocated | |
77 | * storage. A hook (probably a local in the caller's code) allows all | |
78 | * strings converted with that hook to be freed with a single call. | |
79 | */ | |
80 | typedef struct StringStruct { | |
81 | struct StringStruct *link; | |
82 | UChar str[1]; | |
83 | } StringStruct; | |
84 | ||
85 | ||
86 | static UChar* toUChar(const char *src, void **freeHook) { | |
87 | /* Structure of the memory that we allocate on the heap */ | |
88 | ||
89 | int32_t numUChars; | |
90 | int32_t destSize; | |
91 | UChar stackBuf[2000 + sizeof(void *)/sizeof(UChar)]; | |
92 | StringStruct *dest; | |
93 | UConverter *cnv; | |
94 | ||
95 | UErrorCode status = U_ZERO_ERROR; | |
96 | if (src == NULL) { | |
97 | return NULL; | |
98 | }; | |
99 | ||
374ca955 | 100 | cnv = ucnv_open(NULL, &status); |
b75a7d8f A |
101 | if(U_FAILURE(status) || cnv == NULL) { |
102 | return NULL; | |
103 | } | |
104 | ucnv_reset(cnv); | |
105 | numUChars = ucnv_toUChars(cnv, | |
106 | stackBuf, | |
107 | 2000, | |
108 | src, -1, | |
109 | &status); | |
110 | ||
111 | destSize = (numUChars+1) * sizeof(UChar) + sizeof(struct StringStruct); | |
112 | dest = (StringStruct *)malloc(destSize); | |
113 | if (dest != NULL) { | |
114 | if (status == U_BUFFER_OVERFLOW_ERROR || status == U_STRING_NOT_TERMINATED_WARNING) { | |
115 | ucnv_toUChars(cnv, dest->str, numUChars+1, src, -1, &status); | |
116 | } else if (status == U_ZERO_ERROR) { | |
117 | u_strcpy(dest->str, stackBuf); | |
118 | } else { | |
119 | free(dest); | |
120 | dest = NULL; | |
121 | } | |
122 | } | |
123 | ||
124 | ucnv_reset(cnv); /* be good citizens */ | |
125 | ucnv_close(cnv); | |
126 | if (dest == NULL) { | |
127 | return NULL; | |
128 | } | |
2ca993e8 | 129 | |
b75a7d8f A |
130 | dest->link = (StringStruct*)(*freeHook); |
131 | *freeHook = dest; | |
132 | return dest->str; | |
133 | } | |
134 | ||
135 | static void freeToUCharStrings(void **hook) { | |
136 | StringStruct *s = *(StringStruct **)hook; | |
137 | while (s != NULL) { | |
138 | StringStruct *next = s->link; | |
139 | free(s); | |
140 | s = next; | |
141 | } | |
142 | } | |
143 | ||
144 | ||
51004dcb | 145 | #if !UCONFIG_NO_FILE_IO |
b75a7d8f A |
146 | static void TestBreakIteratorCAPI() |
147 | { | |
148 | UErrorCode status = U_ZERO_ERROR; | |
149 | UBreakIterator *word, *sentence, *line, *character, *b, *bogus; | |
150 | int32_t start,pos,end,to; | |
151 | int32_t i; | |
152 | int32_t count = 0; | |
153 | ||
154 | UChar text[50]; | |
155 | ||
156 | /* Note: the adjacent "" are concatenating strings, not adding a \" to the | |
157 | string, which is probably what whoever wrote this intended. Don't fix, | |
158 | because it would throw off the hard coded break positions in the following | |
159 | tests. */ | |
160 | u_uastrcpy(text, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah"); | |
161 | ||
162 | ||
163 | /*test ubrk_open()*/ | |
164 | log_verbose("\nTesting BreakIterator open functions\n"); | |
2ca993e8 | 165 | |
b75a7d8f A |
166 | /* Use french for fun */ |
167 | word = ubrk_open(UBRK_WORD, "en_US", text, u_strlen(text), &status); | |
168 | if(status == U_FILE_ACCESS_ERROR) { | |
374ca955 A |
169 | log_data_err("Check your data - it doesn't seem to be around\n"); |
170 | return; | |
b75a7d8f | 171 | } else if(U_FAILURE(status)){ |
729e4ab9 | 172 | log_err_status(status, "FAIL: Error in ubrk_open() for word breakiterator: %s\n", myErrorName(status)); |
b75a7d8f A |
173 | } |
174 | else{ | |
175 | log_verbose("PASS: Successfully opened word breakiterator\n"); | |
176 | } | |
2ca993e8 | 177 | |
b75a7d8f A |
178 | sentence = ubrk_open(UBRK_SENTENCE, "en_US", text, u_strlen(text), &status); |
179 | if(U_FAILURE(status)){ | |
729e4ab9 | 180 | log_err_status(status, "FAIL: Error in ubrk_open() for sentence breakiterator: %s\n", myErrorName(status)); |
374ca955 | 181 | return; |
b75a7d8f A |
182 | } |
183 | else{ | |
184 | log_verbose("PASS: Successfully opened sentence breakiterator\n"); | |
185 | } | |
2ca993e8 | 186 | |
b75a7d8f A |
187 | line = ubrk_open(UBRK_LINE, "en_US", text, u_strlen(text), &status); |
188 | if(U_FAILURE(status)){ | |
189 | log_err("FAIL: Error in ubrk_open() for line breakiterator: %s\n", myErrorName(status)); | |
374ca955 | 190 | return; |
b75a7d8f A |
191 | } |
192 | else{ | |
193 | log_verbose("PASS: Successfully opened line breakiterator\n"); | |
194 | } | |
2ca993e8 | 195 | |
b75a7d8f A |
196 | character = ubrk_open(UBRK_CHARACTER, "en_US", text, u_strlen(text), &status); |
197 | if(U_FAILURE(status)){ | |
198 | log_err("FAIL: Error in ubrk_open() for character breakiterator: %s\n", myErrorName(status)); | |
374ca955 | 199 | return; |
b75a7d8f A |
200 | } |
201 | else{ | |
202 | log_verbose("PASS: Successfully opened character breakiterator\n"); | |
203 | } | |
204 | /*trying to open an illegal iterator*/ | |
205 | bogus = ubrk_open((UBreakIteratorType)5, "en_US", text, u_strlen(text), &status); | |
57a6839d A |
206 | if(bogus != NULL) { |
207 | log_err("FAIL: expected NULL from opening an invalid break iterator.\n"); | |
208 | } | |
b75a7d8f | 209 | if(U_SUCCESS(status)){ |
73c04bcf | 210 | log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_ILLEGAL_ARGUMENT_ERROR\n"); |
b75a7d8f A |
211 | } |
212 | if(U_FAILURE(status)){ | |
73c04bcf A |
213 | if(status != U_ILLEGAL_ARGUMENT_ERROR){ |
214 | log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_ILLEGAL_ARGUMENT_ERROR\n Got %s\n", myErrorName(status)); | |
b75a7d8f A |
215 | } |
216 | } | |
217 | status=U_ZERO_ERROR; | |
218 | ||
219 | ||
220 | /* ======= Test ubrk_countAvialable() and ubrk_getAvialable() */ | |
221 | ||
222 | log_verbose("\nTesting ubrk_countAvailable() and ubrk_getAvailable()\n"); | |
223 | count=ubrk_countAvailable(); | |
224 | /* use something sensible w/o hardcoding the count */ | |
225 | if(count < 0){ | |
226 | log_err("FAIL: Error in ubrk_countAvialable() returned %d\n", count); | |
227 | } | |
228 | else{ | |
229 | log_verbose("PASS: ubrk_countAvialable() successful returned %d\n", count); | |
230 | } | |
231 | for(i=0;i<count;i++) | |
232 | { | |
2ca993e8 | 233 | log_verbose("%s\n", ubrk_getAvailable(i)); |
b75a7d8f A |
234 | if (ubrk_getAvailable(i) == 0) |
235 | log_err("No locale for which breakiterator is applicable\n"); | |
2ca993e8 | 236 | else |
b75a7d8f A |
237 | log_verbose("A locale %s for which breakiterator is applicable\n",ubrk_getAvailable(i)); |
238 | } | |
239 | ||
240 | /*========Test ubrk_first(), ubrk_last()...... and other functions*/ | |
241 | ||
242 | log_verbose("\nTesting the functions for word\n"); | |
243 | start = ubrk_first(word); | |
244 | if(start!=0) | |
245 | log_err("error ubrk_start(word) did not return 0\n"); | |
246 | log_verbose("first (word = %d\n", (int32_t)start); | |
247 | pos=ubrk_next(word); | |
248 | if(pos!=4) | |
249 | log_err("error ubrk_next(word) did not return 4\n"); | |
250 | log_verbose("next (word = %d\n", (int32_t)pos); | |
251 | pos=ubrk_following(word, 4); | |
252 | if(pos!=5) | |
253 | log_err("error ubrl_following(word,4) did not return 6\n"); | |
254 | log_verbose("next (word = %d\n", (int32_t)pos); | |
255 | end=ubrk_last(word); | |
256 | if(end!=49) | |
257 | log_err("error ubrk_last(word) did not return 49\n"); | |
258 | log_verbose("last (word = %d\n", (int32_t)end); | |
2ca993e8 | 259 | |
b75a7d8f A |
260 | pos=ubrk_previous(word); |
261 | log_verbose("%d %d\n", end, pos); | |
2ca993e8 | 262 | |
b75a7d8f A |
263 | pos=ubrk_previous(word); |
264 | log_verbose("%d \n", pos); | |
265 | ||
266 | if (ubrk_isBoundary(word, 2) != FALSE) { | |
267 | log_err("error ubrk_isBoundary(word, 2) did not return FALSE\n"); | |
268 | } | |
269 | pos=ubrk_current(word); | |
270 | if (pos != 4) { | |
271 | log_err("error ubrk_current() != 4 after ubrk_isBoundary(word, 2)\n"); | |
272 | } | |
273 | if (ubrk_isBoundary(word, 4) != TRUE) { | |
274 | log_err("error ubrk_isBoundary(word, 4) did not return TRUE\n"); | |
275 | } | |
276 | ||
277 | ||
2ca993e8 | 278 | |
b75a7d8f A |
279 | log_verbose("\nTesting the functions for character\n"); |
280 | ubrk_first(character); | |
281 | pos = ubrk_following(character, 5); | |
282 | if(pos!=6) | |
283 | log_err("error ubrk_following(character,5) did not return 6\n"); | |
284 | log_verbose("Following (character,5) = %d\n", (int32_t)pos); | |
285 | pos=ubrk_following(character, 18); | |
286 | if(pos!=19) | |
287 | log_err("error ubrk_following(character,18) did not return 19\n"); | |
288 | log_verbose("Followingcharacter,18) = %d\n", (int32_t)pos); | |
289 | pos=ubrk_preceding(character, 22); | |
290 | if(pos!=21) | |
291 | log_err("error ubrk_preceding(character,22) did not return 21\n"); | |
292 | log_verbose("preceding(character,22) = %d\n", (int32_t)pos); | |
2ca993e8 | 293 | |
b75a7d8f A |
294 | |
295 | log_verbose("\nTesting the functions for line\n"); | |
296 | pos=ubrk_first(line); | |
297 | if(pos != 0) | |
298 | log_err("error ubrk_first(line) returned %d, expected 0\n", (int32_t)pos); | |
299 | pos = ubrk_next(line); | |
300 | pos=ubrk_following(line, 18); | |
301 | if(pos!=22) | |
302 | log_err("error ubrk_following(line) did not return 22\n"); | |
303 | log_verbose("following (line) = %d\n", (int32_t)pos); | |
304 | ||
2ca993e8 | 305 | |
b75a7d8f A |
306 | log_verbose("\nTesting the functions for sentence\n"); |
307 | ubrk_first(sentence); | |
308 | pos = ubrk_current(sentence); | |
309 | log_verbose("Current(sentence) = %d\n", (int32_t)pos); | |
310 | pos = ubrk_last(sentence); | |
311 | if(pos!=49) | |
312 | log_err("error ubrk_last for sentence did not return 49\n"); | |
313 | log_verbose("Last (sentence) = %d\n", (int32_t)pos); | |
314 | ubrk_first(sentence); | |
315 | to = ubrk_following( sentence, 0 ); | |
316 | if (to == 0) log_err("ubrk_following returned 0\n"); | |
317 | to = ubrk_preceding( sentence, to ); | |
318 | if (to != 0) log_err("ubrk_preceding didn't return 0\n"); | |
319 | if (ubrk_first(sentence)!=ubrk_current(sentence)) { | |
320 | log_err("error in ubrk_first() or ubrk_current()\n"); | |
321 | } | |
2ca993e8 A |
322 | |
323 | ||
b75a7d8f | 324 | /*---- */ |
73c04bcf | 325 | /*Testing ubrk_open and ubrk_close()*/ |
b75a7d8f A |
326 | log_verbose("\nTesting open and close for us locale\n"); |
327 | b = ubrk_open(UBRK_WORD, "fr_FR", text, u_strlen(text), &status); | |
328 | if (U_FAILURE(status)) { | |
329 | log_err("ubrk_open for word returned NULL: %s\n", myErrorName(status)); | |
330 | } | |
331 | ubrk_close(b); | |
332 | ||
73c04bcf A |
333 | /* Test setText and setUText */ |
334 | { | |
335 | UChar s1[] = {0x41, 0x42, 0x20, 0}; | |
336 | UChar s2[] = {0x41, 0x42, 0x43, 0x44, 0x45, 0}; | |
337 | UText *ut = NULL; | |
338 | UBreakIterator *bb; | |
339 | int j; | |
340 | ||
341 | log_verbose("\nTesting ubrk_setText() and ubrk_setUText()\n"); | |
342 | status = U_ZERO_ERROR; | |
343 | bb = ubrk_open(UBRK_WORD, "en_US", NULL, 0, &status); | |
344 | TEST_ASSERT_SUCCESS(status); | |
345 | ubrk_setText(bb, s1, -1, &status); | |
346 | TEST_ASSERT_SUCCESS(status); | |
347 | ubrk_first(bb); | |
348 | j = ubrk_next(bb); | |
349 | TEST_ASSERT(j == 2); | |
350 | ut = utext_openUChars(ut, s2, -1, &status); | |
351 | ubrk_setUText(bb, ut, &status); | |
352 | TEST_ASSERT_SUCCESS(status); | |
353 | j = ubrk_next(bb); | |
354 | TEST_ASSERT(j == 5); | |
355 | ||
356 | ubrk_close(bb); | |
357 | utext_close(ut); | |
358 | } | |
359 | ||
b75a7d8f A |
360 | ubrk_close(word); |
361 | ubrk_close(sentence); | |
362 | ubrk_close(line); | |
363 | ubrk_close(character); | |
364 | } | |
365 | ||
366 | static void TestBreakIteratorSafeClone(void) | |
367 | { | |
368 | UChar text[51]; /* Keep this odd to test for 64-bit memory alignment */ | |
2ca993e8 | 369 | /* NOTE: This doesn't reliably force mis-alignment of following items. */ |
b75a7d8f A |
370 | uint8_t buffer [CLONETEST_ITERATOR_COUNT] [U_BRK_SAFECLONE_BUFFERSIZE]; |
371 | int32_t bufferSize = U_BRK_SAFECLONE_BUFFERSIZE; | |
372 | ||
373 | UBreakIterator * someIterators [CLONETEST_ITERATOR_COUNT]; | |
374 | UBreakIterator * someClonedIterators [CLONETEST_ITERATOR_COUNT]; | |
375 | ||
376 | UBreakIterator * brk; | |
377 | UErrorCode status = U_ZERO_ERROR; | |
378 | int32_t start,pos; | |
379 | int32_t i; | |
380 | ||
381 | /*Testing ubrk_safeClone */ | |
382 | ||
383 | /* Note: the adjacent "" are concatenating strings, not adding a \" to the | |
384 | string, which is probably what whoever wrote this intended. Don't fix, | |
385 | because it would throw off the hard coded break positions in the following | |
386 | tests. */ | |
387 | u_uastrcpy(text, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah"); | |
388 | ||
389 | /* US & Thai - rule-based & dictionary based */ | |
390 | someIterators[0] = ubrk_open(UBRK_WORD, "en_US", text, u_strlen(text), &status); | |
391 | if(!someIterators[0] || U_FAILURE(status)) { | |
392 | log_data_err("Couldn't open en_US word break iterator - %s\n", u_errorName(status)); | |
393 | return; | |
394 | } | |
395 | ||
396 | someIterators[1] = ubrk_open(UBRK_WORD, "th_TH", text, u_strlen(text), &status); | |
397 | if(!someIterators[1] || U_FAILURE(status)) { | |
398 | log_data_err("Couldn't open th_TH word break iterator - %s\n", u_errorName(status)); | |
399 | return; | |
400 | } | |
401 | ||
402 | /* test each type of iterator */ | |
403 | for (i = 0; i < CLONETEST_ITERATOR_COUNT; i++) | |
404 | { | |
405 | ||
406 | /* Check the various error & informational states */ | |
407 | ||
408 | /* Null status - just returns NULL */ | |
57a6839d | 409 | if (NULL != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, NULL)) |
b75a7d8f A |
410 | { |
411 | log_err("FAIL: Cloned Iterator failed to deal correctly with null status\n"); | |
412 | } | |
413 | /* error status - should return 0 & keep error the same */ | |
414 | status = U_MEMORY_ALLOCATION_ERROR; | |
57a6839d | 415 | if (NULL != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status) || status != U_MEMORY_ALLOCATION_ERROR) |
b75a7d8f A |
416 | { |
417 | log_err("FAIL: Cloned Iterator failed to deal correctly with incoming error status\n"); | |
418 | } | |
419 | status = U_ZERO_ERROR; | |
420 | ||
57a6839d A |
421 | /* Null buffer size pointer is ok */ |
422 | if (NULL == (brk = ubrk_safeClone(someIterators[i], buffer[i], NULL, &status)) || U_FAILURE(status)) | |
b75a7d8f A |
423 | { |
424 | log_err("FAIL: Cloned Iterator failed to deal correctly with null bufferSize pointer\n"); | |
425 | } | |
57a6839d | 426 | ubrk_close(brk); |
b75a7d8f | 427 | status = U_ZERO_ERROR; |
57a6839d | 428 | |
b75a7d8f A |
429 | /* buffer size pointer is 0 - fill in pbufferSize with a size */ |
430 | bufferSize = 0; | |
57a6839d A |
431 | if (NULL != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status) || |
432 | U_FAILURE(status) || bufferSize <= 0) | |
b75a7d8f A |
433 | { |
434 | log_err("FAIL: Cloned Iterator failed a sizing request ('preflighting')\n"); | |
435 | } | |
436 | /* Verify our define is large enough */ | |
437 | if (U_BRK_SAFECLONE_BUFFERSIZE < bufferSize) | |
438 | { | |
4388f060 | 439 | log_err("FAIL: Pre-calculated buffer size is too small - %d but needed %d\n", U_BRK_SAFECLONE_BUFFERSIZE, bufferSize); |
b75a7d8f A |
440 | } |
441 | /* Verify we can use this run-time calculated size */ | |
57a6839d | 442 | if (NULL == (brk = ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status)) || U_FAILURE(status)) |
b75a7d8f A |
443 | { |
444 | log_err("FAIL: Iterator can't be cloned with run-time size\n"); | |
445 | } | |
446 | if (brk) | |
447 | ubrk_close(brk); | |
448 | /* size one byte too small - should allocate & let us know */ | |
57a6839d A |
449 | if (bufferSize > 1) { |
450 | --bufferSize; | |
451 | } | |
452 | if (NULL == (brk = ubrk_safeClone(someIterators[i], NULL, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_WARNING) | |
b75a7d8f A |
453 | { |
454 | log_err("FAIL: Cloned Iterator failed to deal correctly with too-small buffer size\n"); | |
455 | } | |
456 | if (brk) | |
457 | ubrk_close(brk); | |
458 | status = U_ZERO_ERROR; | |
459 | bufferSize = U_BRK_SAFECLONE_BUFFERSIZE; | |
460 | ||
461 | /* Null buffer pointer - return Iterator & set error to U_SAFECLONE_ALLOCATED_ERROR */ | |
57a6839d | 462 | if (NULL == (brk = ubrk_safeClone(someIterators[i], NULL, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_WARNING) |
b75a7d8f A |
463 | { |
464 | log_err("FAIL: Cloned Iterator failed to deal correctly with null buffer pointer\n"); | |
465 | } | |
466 | if (brk) | |
467 | ubrk_close(brk); | |
468 | status = U_ZERO_ERROR; | |
469 | ||
470 | /* Mis-aligned buffer pointer. */ | |
471 | { | |
472 | char stackBuf[U_BRK_SAFECLONE_BUFFERSIZE+sizeof(void *)]; | |
b75a7d8f A |
473 | |
474 | brk = ubrk_safeClone(someIterators[i], &stackBuf[1], &bufferSize, &status); | |
57a6839d | 475 | if (U_FAILURE(status) || brk == NULL) { |
b75a7d8f A |
476 | log_err("FAIL: Cloned Iterator failed with misaligned buffer pointer\n"); |
477 | } | |
478 | if (status == U_SAFECLONE_ALLOCATED_WARNING) { | |
57a6839d | 479 | log_verbose("Cloned Iterator allocated when using a mis-aligned buffer.\n"); |
b75a7d8f A |
480 | } |
481 | if (brk) | |
482 | ubrk_close(brk); | |
483 | } | |
484 | ||
485 | ||
486 | /* Null Iterator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */ | |
57a6839d | 487 | if (NULL != ubrk_safeClone(NULL, buffer[i], &bufferSize, &status) || status != U_ILLEGAL_ARGUMENT_ERROR) |
b75a7d8f A |
488 | { |
489 | log_err("FAIL: Cloned Iterator failed to deal correctly with null Iterator pointer\n"); | |
490 | } | |
491 | status = U_ZERO_ERROR; | |
492 | ||
493 | /* Do these cloned Iterators work at all - make a first & next call */ | |
494 | bufferSize = U_BRK_SAFECLONE_BUFFERSIZE; | |
495 | someClonedIterators[i] = ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status); | |
496 | ||
497 | start = ubrk_first(someClonedIterators[i]); | |
498 | if(start!=0) | |
499 | log_err("error ubrk_start(clone) did not return 0\n"); | |
500 | pos=ubrk_next(someClonedIterators[i]); | |
501 | if(pos!=4) | |
502 | log_err("error ubrk_next(clone) did not return 4\n"); | |
503 | ||
504 | ubrk_close(someClonedIterators[i]); | |
505 | ubrk_close(someIterators[i]); | |
506 | } | |
507 | } | |
51004dcb | 508 | #endif |
b75a7d8f A |
509 | |
510 | ||
511 | /* | |
512 | // Open a break iterator from char * rules. Take care of conversion | |
513 | // of the rules and error checking. | |
514 | */ | |
515 | static UBreakIterator * testOpenRules(char *rules) { | |
516 | UErrorCode status = U_ZERO_ERROR; | |
517 | UChar *ruleSourceU = NULL; | |
518 | void *strCleanUp = NULL; | |
519 | UParseError parseErr; | |
520 | UBreakIterator *bi; | |
521 | ||
522 | ruleSourceU = toUChar(rules, &strCleanUp); | |
523 | ||
524 | bi = ubrk_openRules(ruleSourceU, -1, /* The rules */ | |
525 | NULL, -1, /* The text to be iterated over. */ | |
526 | &parseErr, &status); | |
2ca993e8 | 527 | |
b75a7d8f | 528 | if (U_FAILURE(status)) { |
729e4ab9 | 529 | log_data_err("FAIL: ubrk_openRules: ICU Error \"%s\" (Are you missing data?)\n", u_errorName(status)); |
b75a7d8f A |
530 | bi = 0; |
531 | }; | |
532 | freeToUCharStrings(&strCleanUp); | |
533 | return bi; | |
534 | ||
535 | } | |
536 | ||
537 | /* | |
538 | * TestBreakIteratorRules - Verify that a break iterator can be created from | |
539 | * a set of source rules. | |
540 | */ | |
541 | static void TestBreakIteratorRules() { | |
542 | /* Rules will keep together any run of letters not including 'a', OR | |
543 | * keep together 'abc', but only when followed by 'def', OTHERWISE | |
544 | * just return one char at a time. | |
545 | */ | |
2ca993e8 | 546 | char rules[] = "abc/def{666};\n [\\p{L} - [a]]* {2}; . {1};"; |
b75a7d8f A |
547 | /* 0123456789012345678 */ |
548 | char data[] = "abcdex abcdefgh-def"; /* the test data string */ | |
549 | char breaks[] = "** ** * ** *"; /* * the expected break positions */ | |
550 | char tags[] = "01 21 6 21 2"; /* expected tag values at break positions */ | |
551 | int32_t tagMap[] = {0, 1, 2, 3, 4, 5, 666}; | |
552 | ||
553 | UChar *uData; | |
554 | void *freeHook = NULL; | |
555 | UErrorCode status = U_ZERO_ERROR; | |
556 | int32_t pos; | |
557 | int i; | |
558 | ||
559 | UBreakIterator *bi = testOpenRules(rules); | |
560 | if (bi == NULL) {return;} | |
561 | uData = toUChar(data, &freeHook); | |
562 | ubrk_setText(bi, uData, -1, &status); | |
563 | ||
564 | pos = ubrk_first(bi); | |
565 | for (i=0; i<sizeof(breaks); i++) { | |
566 | if (pos == i && breaks[i] != '*') { | |
567 | log_err("FAIL: unexpected break at position %d found\n", pos); | |
568 | break; | |
569 | } | |
570 | if (pos != i && breaks[i] == '*') { | |
571 | log_err("FAIL: expected break at position %d not found.\n", i); | |
572 | break; | |
573 | } | |
574 | if (pos == i) { | |
575 | int32_t tag, expectedTag; | |
576 | tag = ubrk_getRuleStatus(bi); | |
577 | expectedTag = tagMap[tags[i]&0xf]; | |
578 | if (tag != expectedTag) { | |
579 | log_err("FAIL: incorrect tag value. Position = %d; expected tag %d, got %d", | |
580 | pos, expectedTag, tag); | |
581 | break; | |
582 | } | |
583 | pos = ubrk_next(bi); | |
584 | } | |
585 | } | |
586 | ||
587 | freeToUCharStrings(&freeHook); | |
588 | ubrk_close(bi); | |
589 | } | |
590 | ||
591 | static void TestBreakIteratorRuleError() { | |
592 | /* | |
593 | * TestBreakIteratorRuleError - Try to create a BI from rules with syntax errors, | |
594 | * check that the error is reported correctly. | |
595 | */ | |
596 | char rules[] = " # This is a rule comment on line 1\n" | |
597 | "[:L:]; # this rule is OK.\n" | |
598 | "abcdefg); # Error, mismatched parens\n"; | |
599 | UChar *uRules; | |
600 | void *freeHook = NULL; | |
601 | UErrorCode status = U_ZERO_ERROR; | |
602 | UParseError parseErr; | |
603 | UBreakIterator *bi; | |
604 | ||
605 | uRules = toUChar(rules, &freeHook); | |
606 | bi = ubrk_openRules(uRules, -1, /* The rules */ | |
607 | NULL, -1, /* The text to be iterated over. */ | |
608 | &parseErr, &status); | |
609 | if (U_SUCCESS(status)) { | |
610 | log_err("FAIL: construction of break iterator succeeded when it should have failed.\n"); | |
611 | ubrk_close(bi); | |
612 | } else { | |
613 | if (parseErr.line != 3 || parseErr.offset != 8) { | |
729e4ab9 | 614 | log_data_err("FAIL: incorrect error position reported. Got line %d, char %d, expected line 3, char 7 (Are you missing data?)\n", |
b75a7d8f A |
615 | parseErr.line, parseErr.offset); |
616 | } | |
617 | } | |
618 | freeToUCharStrings(&freeHook); | |
619 | } | |
620 | ||
374ca955 A |
621 | |
622 | /* | |
623 | * TestsBreakIteratorStatusVals() Test the ubrk_getRuleStatusVec() funciton | |
624 | */ | |
625 | static void TestBreakIteratorStatusVec() { | |
626 | #define RULE_STRING_LENGTH 200 | |
627 | UChar rules[RULE_STRING_LENGTH]; | |
628 | ||
629 | #define TEST_STRING_LENGTH 25 | |
630 | UChar testString[TEST_STRING_LENGTH]; | |
631 | UBreakIterator *bi = NULL; | |
632 | int32_t pos = 0; | |
633 | int32_t vals[10]; | |
634 | int32_t numVals; | |
635 | UErrorCode status = U_ZERO_ERROR; | |
636 | ||
637 | u_uastrncpy(rules, "[A-N]{100}; \n" | |
638 | "[a-w]{200}; \n" | |
639 | "[\\p{L}]{300}; \n" | |
640 | "[\\p{N}]{400}; \n" | |
641 | "[0-5]{500}; \n" | |
642 | "!.*;\n", RULE_STRING_LENGTH); | |
643 | u_uastrncpy(testString, "ABC", TEST_STRING_LENGTH); | |
644 | ||
645 | ||
646 | bi = ubrk_openRules(rules, -1, testString, -1, NULL, &status); | |
73c04bcf | 647 | TEST_ASSERT_SUCCESS(status); |
374ca955 A |
648 | TEST_ASSERT(bi != NULL); |
649 | ||
73c04bcf A |
650 | /* The TEST_ASSERT above should change too... */ |
651 | if (bi != NULL) { | |
652 | pos = ubrk_next(bi); | |
653 | TEST_ASSERT(pos == 1); | |
654 | ||
655 | memset(vals, -1, sizeof(vals)); | |
656 | numVals = ubrk_getRuleStatusVec(bi, vals, 10, &status); | |
657 | TEST_ASSERT_SUCCESS(status); | |
658 | TEST_ASSERT(numVals == 2); | |
659 | TEST_ASSERT(vals[0] == 100); | |
660 | TEST_ASSERT(vals[1] == 300); | |
661 | TEST_ASSERT(vals[2] == -1); | |
662 | ||
663 | numVals = ubrk_getRuleStatusVec(bi, vals, 0, &status); | |
664 | TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR); | |
665 | TEST_ASSERT(numVals == 2); | |
666 | } | |
667 | ||
668 | ubrk_close(bi); | |
669 | } | |
670 | ||
671 | ||
672 | /* | |
673 | * static void TestBreakIteratorUText(void); | |
674 | * | |
675 | * Test that ubrk_setUText() is present and works for a simple case. | |
676 | */ | |
677 | static void TestBreakIteratorUText(void) { | |
678 | 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 */ | |
679 | /* 0 1 2 34567890 */ | |
680 | ||
681 | UErrorCode status = U_ZERO_ERROR; | |
682 | UBreakIterator *bi = NULL; | |
683 | int32_t pos = 0; | |
684 | ||
685 | ||
686 | UText *ut = utext_openUTF8(NULL, UTF8Str, -1, &status); | |
687 | TEST_ASSERT_SUCCESS(status); | |
688 | ||
689 | bi = ubrk_open(UBRK_WORD, "en_US", NULL, 0, &status); | |
690 | if (U_FAILURE(status)) { | |
729e4ab9 | 691 | log_err_status(status, "Failure at file %s, line %d, error = %s\n", __FILE__, __LINE__, u_errorName(status)); |
73c04bcf A |
692 | return; |
693 | } | |
694 | ||
695 | ubrk_setUText(bi, ut, &status); | |
696 | if (U_FAILURE(status)) { | |
697 | log_err("Failure at file %s, line %d, error = %s\n", __FILE__, __LINE__, u_errorName(status)); | |
698 | return; | |
699 | } | |
700 | ||
701 | pos = ubrk_first(bi); | |
702 | TEST_ASSERT(pos == 0); | |
703 | ||
374ca955 | 704 | pos = ubrk_next(bi); |
73c04bcf | 705 | TEST_ASSERT(pos == 4); |
374ca955 | 706 | |
73c04bcf A |
707 | pos = ubrk_next(bi); |
708 | TEST_ASSERT(pos == 5); | |
374ca955 | 709 | |
73c04bcf A |
710 | pos = ubrk_next(bi); |
711 | TEST_ASSERT(pos == 10); | |
374ca955 | 712 | |
73c04bcf A |
713 | pos = ubrk_next(bi); |
714 | TEST_ASSERT(pos == UBRK_DONE); | |
374ca955 | 715 | ubrk_close(bi); |
73c04bcf | 716 | utext_close(ut); |
374ca955 A |
717 | } |
718 | ||
729e4ab9 A |
719 | /* |
720 | * static void TestBreakIteratorTailoring(void); | |
721 | * | |
722 | * Test break iterator tailorings from CLDR data. | |
723 | */ | |
724 | ||
725 | /* Thai/Lao grapheme break tailoring */ | |
726 | static const UChar thTest[] = { 0x0020, 0x0E40, 0x0E01, 0x0020, | |
727 | 0x0E01, 0x0E30, 0x0020, 0x0E01, 0x0E33, 0x0020, 0 }; | |
4388f060 A |
728 | /*in Unicode 6.1 en should behave just like th for this*/ |
729 | /*static const int32_t thTestOffs_enFwd[] = { 1, 3, 4, 6, 7, 9, 10 };*/ | |
729e4ab9 | 730 | static const int32_t thTestOffs_thFwd[] = { 1, 2, 3, 4, 5, 6, 7, 9, 10 }; |
4388f060 | 731 | /*static const int32_t thTestOffs_enRev[] = { 9, 7, 6, 4, 3, 1, 0 };*/ |
729e4ab9 A |
732 | static const int32_t thTestOffs_thRev[] = { 9, 7, 6, 5, 4, 3, 2, 1, 0 }; |
733 | ||
734 | /* Hebrew line break tailoring, for cldrbug 3028 */ | |
735 | static const UChar heTest[] = { 0x0020, 0x002D, 0x0031, 0x0032, 0x0020, | |
736 | 0x0061, 0x002D, 0x006B, 0x0020, | |
737 | 0x0061, 0x0300, 0x2010, 0x006B, 0x0020, | |
738 | 0x05DE, 0x05D4, 0x002D, 0x0069, 0x0020, | |
739 | 0x05D1, 0x05BC, 0x2010, 0x0047, 0x0020, 0 }; | |
4388f060 A |
740 | /*in Unicode 6.1 en should behave just like he for this*/ |
741 | /*static const int32_t heTestOffs_enFwd[] = { 1, 5, 7, 9, 12, 14, 17, 19, 22, 24 };*/ | |
729e4ab9 | 742 | static const int32_t heTestOffs_heFwd[] = { 1, 5, 7, 9, 12, 14, 19, 24 }; |
4388f060 | 743 | /*static const int32_t heTestOffs_enRev[] = { 22, 19, 17, 14, 12, 9, 7, 5, 1, 0 };*/ |
729e4ab9 A |
744 | static const int32_t heTestOffs_heRev[] = { 19, 14, 12, 9, 7, 5, 1, 0 }; |
745 | ||
746 | /* Finnish line break tailoring, for cldrbug 3029 */ | |
747 | static const UChar fiTest[] = { /* 00 */ 0x0020, 0x002D, 0x0031, 0x0032, 0x0020, | |
748 | /* 05 */ 0x0061, 0x002D, 0x006B, 0x0020, | |
749 | /* 09 */ 0x0061, 0x0300, 0x2010, 0x006B, 0x0020, | |
750 | /* 14 */ 0x0061, 0x0020, 0x002D, 0x006B, 0x0020, | |
751 | /* 19 */ 0x0061, 0x0300, 0x0020, 0x2010, 0x006B, 0x0020, 0 }; | |
752 | static const int32_t fiTestOffs_enFwd[] = { 1, 5, 7, 9, 12, 14, 16, 17, 19, 22, 23, 25 }; | |
753 | static const int32_t fiTestOffs_fiFwd[] = { 1, 5, 7, 9, 12, 14, 16, 19, 22, 25 }; | |
754 | static const int32_t fiTestOffs_enRev[] = { 23, 22, 19, 17, 16, 14, 12, 9, 7, 5, 1, 0 }; | |
755 | static const int32_t fiTestOffs_fiRev[] = { 22, 19, 16, 14, 12, 9, 7, 5, 1, 0 }; | |
756 | ||
4388f060 A |
757 | /* Khmer dictionary-based work break, for ICU ticket #8329 */ |
758 | static const UChar kmTest[] = { /* 00 */ 0x179F, 0x17BC, 0x1798, 0x1785, 0x17C6, 0x178E, 0x17B6, 0x1799, 0x1796, 0x17C1, | |
759 | /* 10 */ 0x179B, 0x1794, 0x1793, 0x17D2, 0x178F, 0x17B7, 0x1785, 0x178A, 0x17BE, 0x1798, | |
760 | /* 20 */ 0x17D2, 0x1794, 0x17B8, 0x17A2, 0x1792, 0x17B7, 0x179F, 0x17D2, 0x178B, 0x17B6, | |
761 | /* 30 */ 0x1793, 0x17A2, 0x179A, 0x1796, 0x17D2, 0x179A, 0x17C7, 0x1782, 0x17BB, 0x178E, | |
762 | /* 40 */ 0x178A, 0x179B, 0x17CB, 0x1796, 0x17D2, 0x179A, 0x17C7, 0x17A2, 0x1784, 0x17D2, | |
763 | /* 50 */ 0x1782, 0 }; | |
764 | static const int32_t kmTestOffs_kmFwd[] = { 3, /*8,*/ 11, 17, 23, 31, /*33,*/ 40, 43, 51 }; /* TODO: Investigate failure to break at offset 8 */ | |
765 | static const int32_t kmTestOffs_kmRev[] = { 43, 40, /*33,*/ 31, 23, 17, 11, /*8,*/ 3, 0 }; | |
766 | ||
729e4ab9 A |
767 | typedef struct { |
768 | const char * locale; | |
769 | UBreakIteratorType type; | |
770 | const UChar * test; | |
771 | const int32_t * offsFwd; | |
772 | const int32_t * offsRev; | |
773 | int32_t numOffsets; | |
774 | } RBBITailoringTest; | |
775 | ||
776 | static const RBBITailoringTest tailoringTests[] = { | |
2ca993e8 A |
777 | { "en", UBRK_CHARACTER, thTest, thTestOffs_thFwd, thTestOffs_thRev, UPRV_LENGTHOF(thTestOffs_thFwd) }, |
778 | { "en_US_POSIX", UBRK_CHARACTER, thTest, thTestOffs_thFwd, thTestOffs_thRev, UPRV_LENGTHOF(thTestOffs_thFwd) }, | |
779 | { "en", UBRK_LINE, heTest, heTestOffs_heFwd, heTestOffs_heRev, UPRV_LENGTHOF(heTestOffs_heFwd) }, | |
780 | { "he", UBRK_LINE, heTest, heTestOffs_heFwd, heTestOffs_heRev, UPRV_LENGTHOF(heTestOffs_heFwd) }, | |
781 | { "en", UBRK_LINE, fiTest, fiTestOffs_enFwd, fiTestOffs_enRev, UPRV_LENGTHOF(fiTestOffs_enFwd) }, | |
782 | { "fi", UBRK_LINE, fiTest, fiTestOffs_fiFwd, fiTestOffs_fiRev, UPRV_LENGTHOF(fiTestOffs_fiFwd) }, | |
783 | { "km", UBRK_WORD, kmTest, kmTestOffs_kmFwd, kmTestOffs_kmRev, UPRV_LENGTHOF(kmTestOffs_kmFwd) }, | |
729e4ab9 A |
784 | { NULL, 0, NULL, NULL, NULL, 0 }, |
785 | }; | |
786 | ||
787 | static void TestBreakIteratorTailoring(void) { | |
788 | const RBBITailoringTest * testPtr; | |
789 | for (testPtr = tailoringTests; testPtr->locale != NULL; ++testPtr) { | |
790 | UErrorCode status = U_ZERO_ERROR; | |
791 | UBreakIterator* ubrkiter = ubrk_open(testPtr->type, testPtr->locale, testPtr->test, -1, &status); | |
792 | if ( U_SUCCESS(status) ) { | |
793 | int32_t offset, offsindx; | |
794 | UBool foundError; | |
795 | ||
796 | foundError = FALSE; | |
797 | for (offsindx = 0; (offset = ubrk_next(ubrkiter)) != UBRK_DONE; ++offsindx) { | |
798 | if (!foundError && offsindx >= testPtr->numOffsets) { | |
799 | log_err("FAIL: locale %s, break type %d, ubrk_next expected UBRK_DONE, got %d\n", | |
800 | testPtr->locale, testPtr->type, offset); | |
801 | foundError = TRUE; | |
802 | } else if (!foundError && offset != testPtr->offsFwd[offsindx]) { | |
803 | log_err("FAIL: locale %s, break type %d, ubrk_next expected %d, got %d\n", | |
804 | testPtr->locale, testPtr->type, testPtr->offsFwd[offsindx], offset); | |
805 | foundError = TRUE; | |
806 | } | |
807 | } | |
808 | if (!foundError && offsindx < testPtr->numOffsets) { | |
809 | log_err("FAIL: locale %s, break type %d, ubrk_next expected %d, got UBRK_DONE\n", | |
2ca993e8 | 810 | testPtr->locale, testPtr->type, testPtr->offsFwd[offsindx]); |
729e4ab9 A |
811 | } |
812 | ||
813 | foundError = FALSE; | |
814 | for (offsindx = 0; (offset = ubrk_previous(ubrkiter)) != UBRK_DONE; ++offsindx) { | |
815 | if (!foundError && offsindx >= testPtr->numOffsets) { | |
816 | log_err("FAIL: locale %s, break type %d, ubrk_previous expected UBRK_DONE, got %d\n", | |
817 | testPtr->locale, testPtr->type, offset); | |
818 | foundError = TRUE; | |
819 | } else if (!foundError && offset != testPtr->offsRev[offsindx]) { | |
820 | log_err("FAIL: locale %s, break type %d, ubrk_previous expected %d, got %d\n", | |
821 | testPtr->locale, testPtr->type, testPtr->offsRev[offsindx], offset); | |
822 | foundError = TRUE; | |
823 | } | |
824 | } | |
825 | if (!foundError && offsindx < testPtr->numOffsets) { | |
826 | log_err("FAIL: locale %s, break type %d, ubrk_previous expected %d, got UBRK_DONE\n", | |
2ca993e8 | 827 | testPtr->locale, testPtr->type, testPtr->offsRev[offsindx]); |
729e4ab9 | 828 | } |
374ca955 | 829 | |
729e4ab9 A |
830 | ubrk_close(ubrkiter); |
831 | } else { | |
832 | log_err_status(status, "FAIL: locale %s, break type %d, ubrk_open status: %s\n", testPtr->locale, testPtr->type, u_errorName(status)); | |
833 | } | |
834 | } | |
835 | } | |
73c04bcf | 836 | |
4388f060 A |
837 | |
838 | static void TestBreakIteratorRefresh(void) { | |
839 | /* | |
840 | * RefreshInput changes out the input of a Break Iterator without | |
841 | * changing anything else in the iterator's state. Used with Java JNI, | |
842 | * when Java moves the underlying string storage. This test | |
843 | * runs a ubrk_next() repeatedly, moving the text in the middle of the sequence. | |
844 | * The right set of boundaries should still be found. | |
845 | */ | |
846 | UChar testStr[] = {0x20, 0x41, 0x20, 0x42, 0x20, 0x43, 0x20, 0x44, 0x0}; /* = " A B C D" */ | |
847 | UChar movedStr[] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0}; | |
848 | UErrorCode status = U_ZERO_ERROR; | |
849 | UBreakIterator *bi; | |
850 | UText ut1 = UTEXT_INITIALIZER; | |
851 | UText ut2 = UTEXT_INITIALIZER; | |
2ca993e8 | 852 | |
4388f060 A |
853 | bi = ubrk_open(UBRK_LINE, "en_US", NULL, 0, &status); |
854 | TEST_ASSERT_SUCCESS(status); | |
2ca993e8 A |
855 | if (U_FAILURE(status)) { |
856 | return; | |
857 | } | |
4388f060 A |
858 | |
859 | utext_openUChars(&ut1, testStr, -1, &status); | |
860 | TEST_ASSERT_SUCCESS(status); | |
861 | ubrk_setUText(bi, &ut1, &status); | |
862 | TEST_ASSERT_SUCCESS(status); | |
863 | ||
864 | if (U_SUCCESS(status)) { | |
865 | /* Line boundaries will occur before each letter in the original string */ | |
866 | TEST_ASSERT(1 == ubrk_next(bi)); | |
867 | TEST_ASSERT(3 == ubrk_next(bi)); | |
868 | ||
869 | /* Move the string, kill the original string. */ | |
870 | u_strcpy(movedStr, testStr); | |
871 | u_memset(testStr, 0x20, u_strlen(testStr)); | |
872 | utext_openUChars(&ut2, movedStr, -1, &status); | |
873 | TEST_ASSERT_SUCCESS(status); | |
874 | ubrk_refreshUText(bi, &ut2, &status); | |
875 | TEST_ASSERT_SUCCESS(status); | |
2ca993e8 | 876 | |
4388f060 A |
877 | /* Find the following matches, now working in the moved string. */ |
878 | TEST_ASSERT(5 == ubrk_next(bi)); | |
879 | TEST_ASSERT(7 == ubrk_next(bi)); | |
880 | TEST_ASSERT(8 == ubrk_next(bi)); | |
881 | TEST_ASSERT(UBRK_DONE == ubrk_next(bi)); | |
882 | TEST_ASSERT_SUCCESS(status); | |
883 | ||
884 | utext_close(&ut1); | |
885 | utext_close(&ut2); | |
886 | } | |
887 | ubrk_close(bi); | |
888 | } | |
889 | ||
2ca993e8 A |
890 | |
891 | static void TestBug11665(void) { | |
892 | // The problem was with the incorrect breaking of Japanese text beginning | |
893 | // with Katakana characters when no prior Japanese or Chinese text had been | |
894 | // encountered. | |
895 | // | |
896 | // Tested here in cintltst, rather than in intltest, because only cintltst | |
897 | // tests have the ability to reset ICU, which is needed to get the bug | |
898 | // to manifest itself. | |
899 | ||
900 | static UChar japaneseText[] = {0x30A2, 0x30EC, 0x30EB, 0x30AE, 0x30FC, 0x6027, 0x7D50, 0x819C, 0x708E}; | |
901 | int32_t boundaries[10] = {0}; | |
902 | UBreakIterator *bi = NULL; | |
903 | int32_t brk; | |
904 | int32_t brkIdx = 0; | |
905 | int32_t totalBreaks = 0; | |
906 | UErrorCode status = U_ZERO_ERROR; | |
907 | ||
908 | ctest_resetICU(); | |
909 | bi = ubrk_open(UBRK_WORD, "en_US", japaneseText, UPRV_LENGTHOF(japaneseText), &status); | |
910 | TEST_ASSERT_SUCCESS(status); | |
911 | if (!bi) { | |
912 | return; | |
913 | } | |
914 | for (brk=ubrk_first(bi); brk != UBRK_DONE; brk=ubrk_next(bi)) { | |
915 | boundaries[brkIdx] = brk; | |
916 | if (++brkIdx >= UPRV_LENGTHOF(boundaries) - 1) { | |
917 | break; | |
918 | } | |
919 | } | |
920 | if (brkIdx <= 2 || brkIdx >= UPRV_LENGTHOF(boundaries)) { | |
921 | log_err("%s:%d too few or many breaks found.\n", __FILE__, __LINE__); | |
922 | } else { | |
923 | totalBreaks = brkIdx; | |
924 | brkIdx = 0; | |
925 | for (brk=ubrk_first(bi); brk != UBRK_DONE; brk=ubrk_next(bi)) { | |
926 | if (brk != boundaries[brkIdx]) { | |
927 | log_err("%s:%d Break #%d differs between first and second iteration.\n", __FILE__, __LINE__, brkIdx); | |
928 | break; | |
929 | } | |
930 | if (++brkIdx >= UPRV_LENGTHOF(boundaries) - 1) { | |
931 | log_err("%s:%d Too many breaks.\n", __FILE__, __LINE__); | |
932 | break; | |
933 | } | |
934 | } | |
935 | if (totalBreaks != brkIdx) { | |
936 | log_err("%s:%d Number of breaks differ between first and second iteration.\n", __FILE__, __LINE__); | |
937 | } | |
938 | } | |
939 | ubrk_close(bi); | |
940 | } | |
941 | ||
942 | /* | |
943 | * expOffset is the set of expected offsets, ending with '-1'. | |
944 | * "Expected expOffset -1" means "expected the end of the offsets" | |
945 | */ | |
946 | ||
b331163b | 947 | static const char testSentenceSuppressionsEn[] = "Mr. Jones comes home. Dr. Smith Ph.D. is out. In the U.S.A. it is hot."; |
2ca993e8 A |
948 | static const int32_t testSentSuppFwdOffsetsEn[] = { 22, 46, 70, -1 }; /* With suppressions */ |
949 | static const int32_t testSentFwdOffsetsEn[] = { 4, 22, 26, 46, 70, -1 }; /* Without suppressions */ | |
950 | static const int32_t testSentSuppRevOffsetsEn[] = { 46, 22, 0, -1 }; /* With suppressions */ | |
951 | static const int32_t testSentRevOffsetsEn[] = { 46, 26, 22, 4, 0, -1 }; /* Without suppressions */ | |
b331163b A |
952 | |
953 | static const char testSentenceSuppressionsDe[] = "Wenn ich schon h\\u00F6re zu Guttenberg kommt evtl. zur\\u00FCck."; | |
954 | // "Wenn ich schon höre zu Guttenberg kommt evtl. zurück." | |
955 | static const int32_t testSentSuppFwdOffsetsDe[] = { 53, -1 }; /* With suppressions */ | |
956 | static const int32_t testSentFwdOffsetsDe[] = { 53, -1 }; /* Without suppressions; no break in evtl. zur due to casing */ | |
957 | static const int32_t testSentSuppRevOffsetsDe[] = { 0, -1 }; /* With suppressions */ | |
958 | static const int32_t testSentRevOffsetsDe[] = { 0, -1 }; /* Without suppressions */ | |
959 | ||
960 | static const char testSentenceSuppressionsEs[] = "Te esperamos todos los miercoles en Bravo 416, Col. El Pueblo a las 7 PM."; | |
961 | static const int32_t testSentSuppFwdOffsetsEs[] = { 73, -1 }; /* With suppressions */ | |
962 | static const int32_t testSentFwdOffsetsEs[] = { 52, 73, -1 }; /* Without suppressions */ | |
963 | static const int32_t testSentSuppRevOffsetsEs[] = { 0, -1 }; /* With suppressions */ | |
964 | static const int32_t testSentRevOffsetsEs[] = { 52, 0, -1 }; /* Without suppressions */ | |
965 | ||
2ca993e8 A |
966 | static const char testSentenceSuppressionsE1[] = "Add or detract. The world will little note."; |
967 | static const char testSentenceSuppressionsE1u[] = "ADD OR DETRACT. THE WORLD WILL LITTLE NOTE."; | |
968 | static const int32_t testSentFwdOffsetsE1[] = { 16, 43, -1 }; /* Suppressions and case should make no difference */ | |
969 | static const int32_t testSentRevOffsetsE1[] = { 16, 0, -1 }; /* Suppressions and case should make no difference */ | |
970 | ||
971 | static const char testSentenceSuppressionsE2[] = "Coming up, the sprints at NCAA. Are you watching?"; | |
972 | static const char testSentenceSuppressionsE2u[] = "COMING UP, THE SPRINTS AT NCAA. ARE YOU WATCHING?"; | |
973 | static const int32_t testSentFwdOffsetsE2[] = { 32, 49, -1 }; /* Suppressions and case should make no difference */ | |
974 | static const int32_t testSentRevOffsetsE2[] = { 32, 0, -1 }; /* Suppressions and case should make no difference */ | |
975 | ||
976 | static const char testSentenceSuppressionsFr[] = "Tr\\u00E8s bonne prise de parole de M. Junod, municipal \\u00E0 la culture de Lausanne."; | |
977 | // "Très bonne prise de parole de M. Junod, municipal à la culture de Lausanne." | |
978 | static const int32_t testSentFwdOffsetsFr[] = { 33, 75, -1 }; /* Without suppressions */ | |
979 | static const int32_t testSentSuppFwdOffsetsFr[] = { 75, -1 }; /* With suppressions */ | |
980 | static const int32_t testSentRevOffsetsFr[] = { 33, 0, -1 }; /* Without suppressions */ | |
981 | static const int32_t testSentSuppRevOffsetsFr[] = { 0, -1 }; /* With suppressions */ | |
982 | ||
983 | static const char testSentenceSuppressionsE3[] = "G8 countries e.g. U.K., Japan. Sanctions i.e. restrictions. Test E. Xx G. Xx I. Xx."; | |
984 | static const char testSentenceSuppressionsE3u[] = "G8 COUNTRIES E.G. U.K., JAPAN. SANCTIONS I.E. RESTRICTIONS. TEST E. XX G. XX I. XX."; | |
985 | static const int32_t testSentSuppFwdOffsetsE3[] = { 31, 60, 83, -1 }; /* With suppressions */ | |
986 | static const int32_t testSentSuppRevOffsetsE3[] = { 60, 31, 0, -1 }; /* With suppressions */ | |
987 | static const int32_t testSentFwdOffsetsE3[] = { 18, 31, 60, 68, 74, 80, 83, -1 }; /* Without suppressions */ | |
988 | static const int32_t testSentRevOffsetsE3[] = { 80, 74, 68, 60, 31, 18, 0, -1 }; /* Without suppressions */ | |
989 | static const int32_t testSentFwdOffsetsE3u[] = { 18, 31, 46, 60, 68, 74, 80, 83, -1 }; /* Without suppressions */ | |
990 | static const int32_t testSentRevOffsetsE3u[] = { 80, 74, 68, 60, 46, 31, 18, 0, -1 }; /* Without suppressions */ | |
991 | ||
992 | enum { kTextULenMax = 128, kTextBLenMax = 192 }; | |
b331163b A |
993 | |
994 | typedef struct { | |
995 | const char * locale; | |
996 | const char * text; | |
997 | const int32_t * expFwdOffsets; | |
998 | const int32_t * expRevOffsets; | |
999 | } TestBISuppressionsItem; | |
1000 | ||
1001 | static const TestBISuppressionsItem testBISuppressionsItems[] = { | |
1002 | { "en@ss=standard", testSentenceSuppressionsEn, testSentSuppFwdOffsetsEn, testSentSuppRevOffsetsEn }, | |
1003 | { "en", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, | |
1004 | { "fr@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, | |
1005 | { "af@ss=standard", testSentenceSuppressionsEn, testSentSuppFwdOffsetsEn, testSentSuppRevOffsetsEn }, /* no brkiter data => en suppressions? */ | |
1006 | { "zh@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, /* brkiter data, no suppressions data => no suppressions */ | |
1007 | { "zh_Hant@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, /* brkiter data, no suppressions data => no suppressions */ | |
1008 | { "fi@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, /* brkiter data, no suppressions data => no suppressions */ | |
1009 | { "ja@ss=standard", testSentenceSuppressionsEn, testSentFwdOffsetsEn, testSentRevOffsetsEn }, /* brkiter data, no suppressions data => no suppressions */ | |
1010 | { "de@ss=standard", testSentenceSuppressionsDe, testSentSuppFwdOffsetsDe, testSentSuppRevOffsetsDe }, | |
1011 | { "de", testSentenceSuppressionsDe, testSentFwdOffsetsDe, testSentRevOffsetsDe }, | |
1012 | { "es@ss=standard", testSentenceSuppressionsEs, testSentSuppFwdOffsetsEs, testSentSuppRevOffsetsEs }, | |
1013 | { "es", testSentenceSuppressionsEs, testSentFwdOffsetsEs, testSentRevOffsetsEs }, | |
2ca993e8 A |
1014 | { "en", testSentenceSuppressionsE1, testSentFwdOffsetsE1, testSentRevOffsetsE1 }, |
1015 | { "en@ss=standard", testSentenceSuppressionsE1, testSentFwdOffsetsE1, testSentRevOffsetsE1 }, | |
1016 | { "en", testSentenceSuppressionsE1u, testSentFwdOffsetsE1, testSentRevOffsetsE1 }, | |
1017 | { "en@ss=standard", testSentenceSuppressionsE1u, testSentFwdOffsetsE1, testSentRevOffsetsE1 }, | |
1018 | { "en", testSentenceSuppressionsE2, testSentFwdOffsetsE2, testSentRevOffsetsE2 }, | |
1019 | { "en@ss=standard", testSentenceSuppressionsE2, testSentFwdOffsetsE2, testSentRevOffsetsE2 }, | |
1020 | { "en", testSentenceSuppressionsE2u, testSentFwdOffsetsE2, testSentRevOffsetsE2 }, | |
1021 | { "en@ss=standard", testSentenceSuppressionsE2u, testSentFwdOffsetsE2, testSentRevOffsetsE2 }, | |
1022 | { "fr", testSentenceSuppressionsFr, testSentFwdOffsetsFr, testSentRevOffsetsFr }, | |
1023 | { "fr@ss=standard", testSentenceSuppressionsFr, testSentSuppFwdOffsetsFr, testSentSuppRevOffsetsFr }, | |
1024 | { "en@ss=standard", testSentenceSuppressionsE3, testSentSuppFwdOffsetsE3, testSentSuppRevOffsetsE3 }, | |
1025 | { "en", testSentenceSuppressionsE3, testSentFwdOffsetsE3, testSentRevOffsetsE3 }, | |
1026 | { "en@ss=standard", testSentenceSuppressionsE3u, testSentSuppFwdOffsetsE3, testSentSuppRevOffsetsE3 }, | |
1027 | { "en", testSentenceSuppressionsE3u, testSentFwdOffsetsE3u, testSentRevOffsetsE3u }, | |
b331163b A |
1028 | { NULL, NULL, NULL } |
1029 | }; | |
1030 | ||
1031 | static void TestBreakIteratorSuppressions(void) { | |
1032 | const TestBISuppressionsItem * itemPtr; | |
2ca993e8 | 1033 | |
b331163b | 1034 | for (itemPtr = testBISuppressionsItems; itemPtr->locale != NULL; itemPtr++) { |
2ca993e8 A |
1035 | UChar textU[kTextULenMax + 1]; |
1036 | char textB[kTextBLenMax]; | |
b331163b | 1037 | int32_t textULen = u_unescape(itemPtr->text, textU, kTextULenMax); |
2ca993e8 | 1038 | textU[kTextULenMax] = 0; // ensure zero termination |
b331163b A |
1039 | UErrorCode status = U_ZERO_ERROR; |
1040 | UBreakIterator *bi = ubrk_open(UBRK_SENTENCE, itemPtr->locale, textU, textULen, &status); | |
2ca993e8 | 1041 | log_verbose("#%d: %s\n", (itemPtr-testBISuppressionsItems), itemPtr->locale); |
b331163b A |
1042 | if (U_SUCCESS(status)) { |
1043 | int32_t offset, start; | |
1044 | const int32_t * expOffsetPtr; | |
2ca993e8 A |
1045 | const int32_t * expOffsetStart; |
1046 | u_austrcpy(textB, textU); | |
b331163b | 1047 | |
2ca993e8 | 1048 | expOffsetStart = expOffsetPtr = itemPtr->expFwdOffsets; |
b331163b A |
1049 | ubrk_first(bi); |
1050 | for (; (offset = ubrk_next(bi)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { | |
1051 | if (offset != *expOffsetPtr) { | |
2ca993e8 A |
1052 | log_err("FAIL: ubrk_next loc \"%s\", expected %d, got %d, text \"%s\"\n", |
1053 | itemPtr->locale, *expOffsetPtr, offset, textB); | |
b331163b A |
1054 | } |
1055 | } | |
1056 | if (offset != UBRK_DONE || *expOffsetPtr >= 0) { | |
2ca993e8 A |
1057 | log_err("FAIL: ubrk_next loc \"%s\", expected UBRK_DONE & expOffset -1, got %d and %d, text \"%s\"\n", |
1058 | itemPtr->locale, offset, *expOffsetPtr, textB); | |
b331163b A |
1059 | } |
1060 | ||
2ca993e8 | 1061 | expOffsetStart = expOffsetPtr = itemPtr->expFwdOffsets; |
b331163b A |
1062 | start = ubrk_first(bi) + 1; |
1063 | for (; (offset = ubrk_following(bi, start)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { | |
1064 | if (offset != *expOffsetPtr) { | |
2ca993e8 A |
1065 | log_err("FAIL: ubrk_following(%d) loc \"%s\", expected %d, got %d, text \"%s\"\n", |
1066 | start, itemPtr->locale, *expOffsetPtr, offset, textB); | |
b331163b A |
1067 | } |
1068 | start = *expOffsetPtr + 1; | |
1069 | } | |
1070 | if (offset != UBRK_DONE || *expOffsetPtr >= 0) { | |
2ca993e8 A |
1071 | log_err("FAIL: ubrk_following(%d) loc \"%s\", expected UBRK_DONE & expOffset -1, got %d and %d, text \"%s\"\n", |
1072 | start, itemPtr->locale, offset, *expOffsetPtr, textB); | |
b331163b A |
1073 | } |
1074 | ||
2ca993e8 A |
1075 | expOffsetStart = expOffsetPtr = itemPtr->expRevOffsets; |
1076 | offset = ubrk_last(bi); | |
1077 | log_verbose("___ @%d ubrk_last\n", offset); | |
1078 | if(offset == 0) { | |
1079 | log_err("FAIL: ubrk_last loc \"%s\" unexpected %d\n", itemPtr->locale, offset); | |
1080 | } | |
b331163b A |
1081 | for (; (offset = ubrk_previous(bi)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { |
1082 | if (offset != *expOffsetPtr) { | |
2ca993e8 A |
1083 | log_err("FAIL: ubrk_previous loc \"%s\", expected %d, got %d, text \"%s\"\n", |
1084 | itemPtr->locale, *expOffsetPtr, offset, textB); | |
1085 | } else { | |
1086 | log_verbose("[%d] @%d ubrk_previous()\n", (expOffsetPtr - expOffsetStart), offset); | |
b331163b A |
1087 | } |
1088 | } | |
1089 | if (offset != UBRK_DONE || *expOffsetPtr >= 0) { | |
2ca993e8 A |
1090 | log_err("FAIL: ubrk_previous loc \"%s\", expected UBRK_DONE & expOffset[%d] -1, got %d and %d, text \"%s\"\n", |
1091 | itemPtr->locale, expOffsetPtr - expOffsetStart, offset, *expOffsetPtr, textB); | |
b331163b A |
1092 | } |
1093 | ||
2ca993e8 | 1094 | expOffsetStart = expOffsetPtr = itemPtr->expRevOffsets; |
b331163b A |
1095 | start = ubrk_last(bi) - 1; |
1096 | for (; (offset = ubrk_preceding(bi, start)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { | |
1097 | if (offset != *expOffsetPtr) { | |
2ca993e8 A |
1098 | log_err("FAIL: ubrk_preceding(%d) loc \"%s\", expected %d, got %d, text \"%s\"\n", |
1099 | start, itemPtr->locale, *expOffsetPtr, offset, textB); | |
b331163b A |
1100 | } |
1101 | start = *expOffsetPtr - 1; | |
1102 | } | |
1103 | if (start >=0 && (offset != UBRK_DONE || *expOffsetPtr >= 0)) { | |
2ca993e8 A |
1104 | log_err("FAIL: ubrk_preceding loc(%d) \"%s\", expected UBRK_DONE & expOffset -1, got %d and %d, text \"%s\"\n", |
1105 | start, itemPtr->locale, offset, *expOffsetPtr, textB); | |
b331163b A |
1106 | } |
1107 | ||
1108 | ubrk_close(bi); | |
1109 | } else { | |
2ca993e8 A |
1110 | log_data_err("FAIL: ubrk_open(UBRK_SENTENCE, \"%s\", ...) status %s (Are you missing data?)\n", |
1111 | itemPtr->locale, u_errorName(status)); | |
b331163b A |
1112 | } |
1113 | } | |
1114 | } | |
1115 | ||
2ca993e8 | 1116 | |
b75a7d8f | 1117 | #endif /* #if !UCONFIG_NO_BREAK_ITERATION */ |