1
/********************************************************************
3
* Copyright (c) 1997-2001, International Business Machines Corporation and
4
* others. All Rights Reserved.
5
********************************************************************/
6
/********************************************************************************
10
* Modification History:
12
* Madhu Katragadda Creation
13
*********************************************************************************/
14
/*C API TEST FOR BREAKITERATOR */
16
* This is an API test. It doesn't test very many cases, and doesn't
17
* try to test the full functionality. It just calls each function in the class and
18
* verifies that it works on a basic level.
20
#include "unicode/uloc.h"
21
#include "unicode/ubrk.h"
22
#include "unicode/utypes.h"
23
#include "unicode/ustring.h"
27
void addBrkIterAPITest(TestNode** root);
29
void addBrkIterAPITest(TestNode** root)
31
addTest(root, &TestBreakIteratorCAPI, "tstxtbd/cbiapts/TestBreakIteratorCAPI");
35
#define CLONETEST_ITERATOR_COUNT 2
37
static void TestBreakIteratorCAPI()
39
UErrorCode status = U_ZERO_ERROR;
40
UBreakIterator *word, *sentence, *line, *character, *b, *bogus;
41
int32_t start,pos,end,to;
45
UBreakIterator * someIterators [CLONETEST_ITERATOR_COUNT];
46
UBreakIterator * someClonedIterators [CLONETEST_ITERATOR_COUNT];
48
UChar text[51]; /* Keep this odd to test for 64-bit memory alignment */
49
/* NOTE: This doesn't reliably force mis-alignment of following items. */
50
uint8_t buffer [CLONETEST_ITERATOR_COUNT] [U_BRK_SAFECLONE_BUFFERSIZE];
51
int32_t bufferSize = U_BRK_SAFECLONE_BUFFERSIZE;
53
/* Note: the adjacent "" are concatenating strings, not adding a \" to the
54
string, which is probably what whoever wrote this intended. Don't fix,
55
because it would throw off the hard coded break positions in the following
57
u_uastrcpy(text, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah");
61
log_verbose("\nTesting BreakIterator open functions\n");
63
/* Use french for fun */
64
word = ubrk_open(UBRK_WORD, "en_US", text, u_strlen(text), &status);
65
if(U_FAILURE(status)){
66
log_err("FAIL: Error in ubrk_open() for word breakiterator: %s\n", myErrorName(status));
69
log_verbose("PASS: Successfully opened word breakiterator\n");
72
sentence = ubrk_open(UBRK_SENTENCE, "en_US", text, u_strlen(text), &status);
73
if(U_FAILURE(status)){
74
log_err("FAIL: Error in ubrk_open() for sentence breakiterator: %s\n", myErrorName(status));
77
log_verbose("PASS: Successfully opened sentence breakiterator\n");
80
line = ubrk_open(UBRK_LINE, "en_US", text, u_strlen(text), &status);
81
if(U_FAILURE(status)){
82
log_err("FAIL: Error in ubrk_open() for line breakiterator: %s\n", myErrorName(status));
85
log_verbose("PASS: Successfully opened line breakiterator\n");
88
character = ubrk_open(UBRK_CHARACTER, "en_US", text, u_strlen(text), &status);
89
if(U_FAILURE(status)){
90
log_err("FAIL: Error in ubrk_open() for character breakiterator: %s\n", myErrorName(status));
93
log_verbose("PASS: Successfully opened character breakiterator\n");
95
/*trying to open an illegal iterator*/
96
bogus = ubrk_open((UBreakIteratorType)5, "en_US", text, u_strlen(text), &status);
97
if(U_SUCCESS(status)){
98
log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_MEMORY_ALLOCATION_ERROR\n");
100
if(U_FAILURE(status)){
101
if(status != U_MEMORY_ALLOCATION_ERROR){
102
log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_MEMORY_ALLOCATION_ERROR\n Got %s\n", myErrorName(status));
108
/* ======= Test ubrk_countAvialable() and ubrk_getAvialable() */
110
log_verbose("\nTesting ubrk_countAvailable() and ubrk_getAvailable()\n");
111
count=ubrk_countAvailable();
112
/* use something sensible w/o hardcoding the count */
114
log_err("FAIL: Error in ubrk_countAvialable() returned %d\n", count);
117
log_verbose("PASS: ubrk_countAvialable() successful returned %d\n", count);
121
log_verbose("%s\n", ubrk_getAvailable(i));
122
if (ubrk_getAvailable(i) == 0)
123
log_err("No locale for which breakiterator is applicable\n");
125
log_verbose("A locale %s for which breakiterator is applicable\n",ubrk_getAvailable(i));
128
/*========Test ubrk_first(), ubrk_last()...... and other functions*/
130
log_verbose("\nTesting the functions for word\n");
131
start = ubrk_first(word);
133
log_err("error ubrk_start(word) did not return 0\n");
134
log_verbose("first (word = %d\n", (int32_t)start);
137
log_err("error ubrk_next(word) did not return 4\n");
138
log_verbose("next (word = %d\n", (int32_t)pos);
139
pos=ubrk_following(word, 4);
141
log_err("error ubrl_following(word,4) did not return 6\n");
142
log_verbose("next (word = %d\n", (int32_t)pos);
145
log_err("error ubrk_last(word) did not return 49\n");
146
log_verbose("last (word = %d\n", (int32_t)end);
148
pos=ubrk_previous(word);
149
log_verbose("%d %d\n", end, pos);
151
pos=ubrk_previous(word);
152
log_verbose("%d \n", pos);
154
if (ubrk_isBoundary(word, 2) != FALSE) {
155
log_err("error ubrk_isBoundary(word, 2) did not return FALSE\n");
157
pos=ubrk_current(word);
159
log_err("error ubrk_current() != 4 after ubrk_isBoundary(word, 2)\n");
161
if (ubrk_isBoundary(word, 4) != TRUE) {
162
log_err("error ubrk_isBoundary(word, 4) did not return TRUE\n");
167
log_verbose("\nTesting the functions for character\n");
168
ubrk_first(character);
169
pos = ubrk_following(character, 5);
171
log_err("error ubrk_following(character,5) did not return 6\n");
172
log_verbose("Following (character,5) = %d\n", (int32_t)pos);
173
pos=ubrk_following(character, 18);
175
log_err("error ubrk_following(character,18) did not return 19\n");
176
log_verbose("Followingcharacter,18) = %d\n", (int32_t)pos);
177
pos=ubrk_preceding(character, 22);
179
log_err("error ubrk_preceding(character,22) did not return 21\n");
180
log_verbose("preceding(character,22) = %d\n", (int32_t)pos);
183
log_verbose("\nTesting the functions for line\n");
184
pos=ubrk_first(line);
186
log_err("error ubrk_first(line) returned %d, expected 0\n", (int32_t)pos);
187
pos = ubrk_next(line);
188
pos=ubrk_following(line, 18);
190
log_err("error ubrk_following(line) did not return 22\n");
191
log_verbose("following (line) = %d\n", (int32_t)pos);
194
log_verbose("\nTesting the functions for sentence\n");
195
ubrk_first(sentence);
196
pos = ubrk_current(sentence);
197
log_verbose("Current(sentence) = %d\n", (int32_t)pos);
198
pos = ubrk_last(sentence);
200
log_err("error ubrk_last for sentence did not return 49\n");
201
log_verbose("Last (sentence) = %d\n", (int32_t)pos);
202
ubrk_first(sentence);
203
to = ubrk_following( sentence, 0 );
204
if (to == 0) log_err("ubrk_following returned 0\n");
205
to = ubrk_preceding( sentence, to );
206
if (to != 0) log_err("ubrk_preceding didn't return 0\n");
207
if (ubrk_first(sentence)!=ubrk_current(sentence)) {
208
log_err("error in ubrk_first() or ubrk_current()\n");
213
/*Testing ubrk_open and ubrk_close()*/
214
log_verbose("\nTesting open and close for us locale\n");
215
b = ubrk_open(UBRK_WORD, "fr_FR", text, u_strlen(text), &status);
216
if (U_FAILURE(status)) {
217
log_err("ubrk_open for word returned NULL: %s\n", myErrorName(status));
222
ubrk_close(sentence);
224
ubrk_close(character);
226
/*Testing ubrk_safeClone */
228
/* US & Thai - rule-based & dictionary based */
229
someIterators[0] = ubrk_open(UBRK_WORD, "en_US", text, u_strlen(text), &status);
230
someIterators[1] = ubrk_open(UBRK_WORD, "th_TH", text, u_strlen(text), &status);
232
/* test each type of iterator */
233
for (i = 0; i < CLONETEST_ITERATOR_COUNT; i++)
236
/* Check the various error & informational states */
238
/* Null status - just returns NULL */
239
if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, 0))
241
log_err("FAIL: Cloned Iterator failed to deal correctly with null status\n");
243
/* error status - should return 0 & keep error the same */
244
status = U_MEMORY_ALLOCATION_ERROR;
245
if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status) || status != U_MEMORY_ALLOCATION_ERROR)
247
log_err("FAIL: Cloned Iterator failed to deal correctly with incoming error status\n");
249
status = U_ZERO_ERROR;
251
/* Null buffer size pointer - just returns NULL & set error to U_ILLEGAL_ARGUMENT_ERROR*/
252
if (0 != ubrk_safeClone(someIterators[i], buffer[i], 0, &status) || status != U_ILLEGAL_ARGUMENT_ERROR)
254
log_err("FAIL: Cloned Iterator failed to deal correctly with null bufferSize pointer\n");
256
status = U_ZERO_ERROR;
258
/* buffer size pointer is 0 - fill in pbufferSize with a size */
260
if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status) || U_FAILURE(status) || bufferSize <= 0)
262
log_err("FAIL: Cloned Iterator failed a sizing request ('preflighting')\n");
264
/* Verify our define is large enough */
265
if (U_BRK_SAFECLONE_BUFFERSIZE < bufferSize)
267
log_err("FAIL: Pre-calculated buffer size is too small\n");
269
/* Verify we can use this run-time calculated size */
270
if (0 == (brk = ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status)) || U_FAILURE(status))
272
log_err("FAIL: Iterator can't be cloned with run-time size\n");
274
if (brk) ubrk_close(brk);
275
/* size one byte too small - should allocate & let us know */
277
if (0 == (brk = ubrk_safeClone(someIterators[i], 0, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_ERROR)
279
log_err("FAIL: Cloned Iterator failed to deal correctly with too-small buffer size\n");
281
if (brk) ubrk_close(brk);
282
status = U_ZERO_ERROR;
283
bufferSize = U_BRK_SAFECLONE_BUFFERSIZE;
285
/* Null buffer pointer - return Iterator & set error to U_SAFECLONE_ALLOCATED_ERROR */
286
if (0 == (brk = ubrk_safeClone(someIterators[i], 0, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_ERROR)
288
log_err("FAIL: Cloned Iterator failed to deal correctly with null buffer pointer\n");
290
if (brk) ubrk_close(brk);
291
status = U_ZERO_ERROR;
293
/* Mis-aligned buffer pointer. */
295
char stackBuf[U_BRK_SAFECLONE_BUFFERSIZE+sizeof(void *)];
299
brk = ubrk_safeClone(someIterators[i], &stackBuf[1], &bufferSize, &status);
300
if (U_FAILURE(status) || brk == 0) {
301
log_err("FAIL: Cloned Iterator failed with misaligned buffer pointer\n");
303
if (status == U_SAFECLONE_ALLOCATED_ERROR) {
304
log_err("FAIL: Cloned Iterator allocated when using a mis-aligned buffer.\n");
306
offset = (char *)&p-(char*)brk;
310
if (offset % sizeof(void *) != 0) {
311
log_err("FAIL: Cloned Iterator failed to align correctly with misaligned buffer pointer\n");
313
if (brk) ubrk_close(brk);
317
/* Null Iterator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
318
if (0 != ubrk_safeClone(0, buffer[i], &bufferSize, &status) || status != U_ILLEGAL_ARGUMENT_ERROR)
320
log_err("FAIL: Cloned Iterator failed to deal correctly with null Iterator pointer\n");
322
status = U_ZERO_ERROR;
324
/* Do these cloned Iterators work at all - make a first & next call */
325
bufferSize = U_BRK_SAFECLONE_BUFFERSIZE;
326
someClonedIterators[i] = ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status);
328
start = ubrk_first(someClonedIterators[i]);
330
log_err("error ubrk_start(clone) did not return 0\n");
331
pos=ubrk_next(someClonedIterators[i]);
333
log_err("error ubrk_next(clone) did not return 4\n");
335
ubrk_close(someClonedIterators[i]);
336
ubrk_close(someIterators[i]);