~ubuntu-branches/ubuntu/gutsy/icu/gutsy-updates

« back to all changes in this revision

Viewing changes to source/test/cintltst/cbiapts.c

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2005-11-19 11:29:31 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20051119112931-vcizkrp10tli4enw
Tags: 3.4-3
Explicitly build with g++ 3.4.  The current ICU fails its test suite
with 4.0 but not with 3.4.  Future versions should work properly with
4.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/********************************************************************
2
 
 * COPYRIGHT: 
3
 
 * Copyright (c) 1997-2001, 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
 
#include "unicode/uloc.h"
21
 
#include "unicode/ubrk.h"
22
 
#include "unicode/utypes.h"
23
 
#include "unicode/ustring.h"
24
 
#include "cintltst.h"
25
 
#include "cbiapts.h"
26
 
 
27
 
void addBrkIterAPITest(TestNode** root);
28
 
 
29
 
void addBrkIterAPITest(TestNode** root)
30
 
{
31
 
    addTest(root, &TestBreakIteratorCAPI, "tstxtbd/cbiapts/TestBreakIteratorCAPI");
32
 
 
33
 
}
34
 
 
35
 
#define CLONETEST_ITERATOR_COUNT 2
36
 
 
37
 
static void TestBreakIteratorCAPI()
38
 
{
39
 
    UErrorCode status = U_ZERO_ERROR;
40
 
    UBreakIterator *word, *sentence, *line, *character, *b, *bogus;
41
 
    int32_t start,pos,end,to;
42
 
    int32_t i;
43
 
    int32_t count = 0;
44
 
 
45
 
    UBreakIterator * someIterators [CLONETEST_ITERATOR_COUNT];
46
 
    UBreakIterator * someClonedIterators [CLONETEST_ITERATOR_COUNT];
47
 
    UBreakIterator * brk;
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;
52
 
 
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
56
 
       tests. */
57
 
    u_uastrcpy(text, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah");
58
 
 
59
 
 
60
 
/*test ubrk_open()*/
61
 
    log_verbose("\nTesting BreakIterator open functions\n");
62
 
                                            
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));
67
 
    }
68
 
    else{
69
 
        log_verbose("PASS: Successfully opened  word breakiterator\n");
70
 
    }
71
 
    
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));
75
 
    }
76
 
    else{
77
 
        log_verbose("PASS: Successfully opened  sentence breakiterator\n");
78
 
    }
79
 
    
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));
83
 
    }
84
 
    else{
85
 
        log_verbose("PASS: Successfully opened  line breakiterator\n");
86
 
    }
87
 
    
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));
91
 
    }
92
 
    else{
93
 
        log_verbose("PASS: Successfully opened  character breakiterator\n");
94
 
    }
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");
99
 
    }
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));
103
 
        }
104
 
    }
105
 
    status=U_ZERO_ERROR;
106
 
 
107
 
 
108
 
/* ======= Test ubrk_countAvialable() and ubrk_getAvialable() */
109
 
 
110
 
    log_verbose("\nTesting ubrk_countAvailable() and ubrk_getAvailable()\n");
111
 
    count=ubrk_countAvailable();
112
 
    /* use something sensible w/o hardcoding the count */
113
 
    if(count < 0){
114
 
        log_err("FAIL: Error in ubrk_countAvialable() returned %d\n", count);
115
 
    }
116
 
    else{
117
 
        log_verbose("PASS: ubrk_countAvialable() successful returned %d\n", count);
118
 
    }
119
 
    for(i=0;i<count;i++)
120
 
    {
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");
124
 
        else 
125
 
            log_verbose("A locale %s for which breakiterator is applicable\n",ubrk_getAvailable(i));
126
 
    }
127
 
 
128
 
/*========Test ubrk_first(), ubrk_last()...... and other functions*/
129
 
 
130
 
    log_verbose("\nTesting the functions for word\n");
131
 
    start = ubrk_first(word);
132
 
    if(start!=0)
133
 
        log_err("error ubrk_start(word) did not return 0\n");
134
 
    log_verbose("first (word = %d\n", (int32_t)start);
135
 
       pos=ubrk_next(word);
136
 
    if(pos!=4)
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);
140
 
    if(pos!=5)
141
 
        log_err("error ubrl_following(word,4) did not return 6\n");
142
 
    log_verbose("next (word = %d\n", (int32_t)pos);
143
 
    end=ubrk_last(word);
144
 
    if(end!=49)
145
 
        log_err("error ubrk_last(word) did not return 49\n");
146
 
    log_verbose("last (word = %d\n", (int32_t)end);
147
 
    
148
 
    pos=ubrk_previous(word);
149
 
    log_verbose("%d   %d\n", end, pos);
150
 
     
151
 
    pos=ubrk_previous(word);
152
 
    log_verbose("%d \n", pos);
153
 
 
154
 
    if (ubrk_isBoundary(word, 2) != FALSE) {
155
 
        log_err("error ubrk_isBoundary(word, 2) did not return FALSE\n");
156
 
    }
157
 
    pos=ubrk_current(word);
158
 
    if (pos != 4) {
159
 
        log_err("error ubrk_current() != 4 after ubrk_isBoundary(word, 2)\n");
160
 
    }
161
 
    if (ubrk_isBoundary(word, 4) != TRUE) {
162
 
        log_err("error ubrk_isBoundary(word, 4) did not return TRUE\n");
163
 
    }
164
 
 
165
 
 
166
 
    
167
 
    log_verbose("\nTesting the functions for character\n");
168
 
    ubrk_first(character);
169
 
    pos = ubrk_following(character, 5);
170
 
    if(pos!=6)
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);
174
 
    if(pos!=19)
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);
178
 
    if(pos!=21)
179
 
       log_err("error ubrk_preceding(character,22) did not return 21\n");
180
 
    log_verbose("preceding(character,22) = %d\n", (int32_t)pos);
181
 
    
182
 
 
183
 
    log_verbose("\nTesting the functions for line\n");
184
 
    pos=ubrk_first(line);
185
 
    if(pos != 0)
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);
189
 
    if(pos!=22)
190
 
        log_err("error ubrk_following(line) did not return 22\n");
191
 
    log_verbose("following (line) = %d\n", (int32_t)pos);
192
 
 
193
 
    
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);
199
 
    if(pos!=49)
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");
209
 
    }
210
 
    
211
 
 
212
 
    /*---- */
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));
218
 
    }
219
 
    ubrk_close(b);
220
 
 
221
 
    ubrk_close(word);
222
 
    ubrk_close(sentence);
223
 
    ubrk_close(line);
224
 
    ubrk_close(character);
225
 
 
226
 
    /*Testing ubrk_safeClone */
227
 
 
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);
231
 
 
232
 
    /* test each type of iterator */
233
 
    for (i = 0; i < CLONETEST_ITERATOR_COUNT; i++)
234
 
    {
235
 
 
236
 
        /* Check the various error & informational states */
237
 
 
238
 
        /* Null status - just returns NULL */
239
 
        if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, 0))
240
 
        {
241
 
            log_err("FAIL: Cloned Iterator failed to deal correctly with null status\n");
242
 
        }
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)
246
 
        {
247
 
            log_err("FAIL: Cloned Iterator failed to deal correctly with incoming error status\n");
248
 
        }
249
 
        status = U_ZERO_ERROR;
250
 
 
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)
253
 
        {
254
 
            log_err("FAIL: Cloned Iterator failed to deal correctly with null bufferSize pointer\n");
255
 
        }
256
 
        status = U_ZERO_ERROR;
257
 
    
258
 
        /* buffer size pointer is 0 - fill in pbufferSize with a size */
259
 
        bufferSize = 0;
260
 
        if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status) || U_FAILURE(status) || bufferSize <= 0)
261
 
        {
262
 
            log_err("FAIL: Cloned Iterator failed a sizing request ('preflighting')\n");
263
 
        }
264
 
        /* Verify our define is large enough  */
265
 
        if (U_BRK_SAFECLONE_BUFFERSIZE < bufferSize)
266
 
        {
267
 
            log_err("FAIL: Pre-calculated buffer size is too small\n");
268
 
        }
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))
271
 
        {
272
 
            log_err("FAIL: Iterator can't be cloned with run-time size\n");
273
 
        }
274
 
        if (brk) ubrk_close(brk);
275
 
        /* size one byte too small - should allocate & let us know */
276
 
        --bufferSize;
277
 
        if (0 == (brk = ubrk_safeClone(someIterators[i], 0, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_ERROR)
278
 
        {
279
 
            log_err("FAIL: Cloned Iterator failed to deal correctly with too-small buffer size\n");
280
 
        }
281
 
        if (brk) ubrk_close(brk);
282
 
        status = U_ZERO_ERROR;
283
 
        bufferSize = U_BRK_SAFECLONE_BUFFERSIZE;
284
 
 
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)
287
 
        {
288
 
            log_err("FAIL: Cloned Iterator failed to deal correctly with null buffer pointer\n");
289
 
        }
290
 
        if (brk) ubrk_close(brk);
291
 
        status = U_ZERO_ERROR;
292
 
 
293
 
        /* Mis-aligned buffer pointer. */
294
 
        {
295
 
            char  stackBuf[U_BRK_SAFECLONE_BUFFERSIZE+sizeof(void *)];
296
 
            void  *p;
297
 
            int    offset;
298
 
 
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");
302
 
            }
303
 
            if (status == U_SAFECLONE_ALLOCATED_ERROR) {
304
 
                log_err("FAIL: Cloned Iterator allocated when using a mis-aligned buffer.\n");
305
 
            }
306
 
            offset = (char *)&p-(char*)brk;
307
 
            if (offset < 0) {
308
 
                offset = -offset;
309
 
            }
310
 
            if (offset % sizeof(void *) != 0) {
311
 
                log_err("FAIL: Cloned Iterator failed to align correctly with misaligned buffer pointer\n");
312
 
            }
313
 
            if (brk) ubrk_close(brk);
314
 
        }
315
 
 
316
 
 
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)
319
 
        {
320
 
            log_err("FAIL: Cloned Iterator failed to deal correctly with null Iterator pointer\n");
321
 
        }
322
 
        status = U_ZERO_ERROR;
323
 
 
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);
327
 
 
328
 
        start = ubrk_first(someClonedIterators[i]);
329
 
        if(start!=0)
330
 
            log_err("error ubrk_start(clone) did not return 0\n");
331
 
        pos=ubrk_next(someClonedIterators[i]);
332
 
        if(pos!=4)
333
 
            log_err("error ubrk_next(clone) did not return 4\n");
334
 
 
335
 
        ubrk_close(someClonedIterators[i]);
336
 
        ubrk_close(someIterators[i]);
337
 
    }
338
 
}