• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4  * COPYRIGHT:
5  * Copyright (c) 2005-2013, International Business Machines Corporation and
6  * others. All Rights Reserved.
7  ********************************************************************/
8 /*
9 * File utexttst.c
10 *
11 * Modification History:
12 *
13 *   Date          Name               Description
14 *   06/13/2005    Andy Heninger      Creation
15 *******************************************************************************
16 */
17 
18 #include "unicode/utypes.h"
19 #include "unicode/utext.h"
20 #include "unicode/ustring.h"
21 #include "cintltst.h"
22 #include "memory.h"
23 #include "string.h"
24 
25 
26 static void TestAPI(void);
27 void addUTextTest(TestNode** root);
28 
29 
30 void
addUTextTest(TestNode ** root)31 addUTextTest(TestNode** root)
32 {
33   addTest(root, &TestAPI           ,    "tsutil/UTextTest/TestAPI");
34 }
35 
36 
37 #define TEST_ASSERT(x) UPRV_BLOCK_MACRO_BEGIN { \
38     if ((x)==FALSE) { \
39         log_err("Test failure in file %s at line %d\n", __FILE__, __LINE__); \
40         gFailed = TRUE; \
41     } \
42 } UPRV_BLOCK_MACRO_END
43 
44 
45 #define TEST_SUCCESS(status) UPRV_BLOCK_MACRO_BEGIN { \
46     if (U_FAILURE(status)) { \
47         log_err("Test failure in file %s at line %d. Error = \"%s\"\n", \
48                 __FILE__, __LINE__, u_errorName(status)); \
49         gFailed = TRUE; \
50    } \
51 } UPRV_BLOCK_MACRO_END
52 
53 
54 
55 /*
56  *  TestAPI   verify that the UText API is accessible from C programs.
57  *            This is not intended to be a complete test of the API functionality.  That is
58  *            in the C++ intltest program.
59  *            This test is intended to check that everything can be accessed and built in
60  *            a pure C enviornment.
61  */
62 
63 
TestAPI(void)64 static void TestAPI(void) {
65     UErrorCode      status = U_ZERO_ERROR;
66     UBool           gFailed = FALSE;
67     (void)gFailed;   /* Suppress set but not used warning. */
68 
69     /* Open    */
70     {
71         UText           utLoc = UTEXT_INITIALIZER;
72         const char *    cString = "\x61\x62\x63\x64";
73         UChar           uString[]  = {0x41, 0x42, 0x43, 0};
74         UText          *uta;
75         UText          *utb;
76         UChar           c;
77 
78         uta = utext_openUChars(NULL, uString, -1, &status);
79         TEST_SUCCESS(status);
80         c = utext_next32(uta);
81         TEST_ASSERT(c == 0x41);
82         utb = utext_close(uta);
83         TEST_ASSERT(utb == NULL);
84 
85         uta = utext_openUTF8(&utLoc, cString, -1, &status);
86         TEST_SUCCESS(status);
87         TEST_ASSERT(uta == &utLoc);
88 
89         uta = utext_close(&utLoc);
90         TEST_ASSERT(uta == &utLoc);
91     }
92 
93     /* utext_clone()  */
94     {
95         UChar   uString[]  = {0x41, 0x42, 0x43, 0};
96         int64_t len;
97         UText   *uta;
98         UText   *utb;
99 
100         status = U_ZERO_ERROR;
101         uta = utext_openUChars(NULL, uString, -1, &status);
102         TEST_SUCCESS(status);
103         utb = utext_clone(NULL, uta, FALSE, FALSE, &status);
104         TEST_SUCCESS(status);
105         TEST_ASSERT(utb != NULL);
106         TEST_ASSERT(utb != uta);
107         len = utext_nativeLength(uta);
108         TEST_ASSERT(len == u_strlen(uString));
109         utext_close(uta);
110         utext_close(utb);
111     }
112 
113     /* basic access functions  */
114     {
115         UChar     uString[]  = {0x41, 0x42, 0x43, 0};
116         UText     *uta;
117         UChar32   c;
118         int64_t   len;
119         UBool     b;
120         int64_t   i;
121 
122         status = U_ZERO_ERROR;
123         uta = utext_openUChars(NULL, uString, -1, &status);
124         TEST_ASSERT(uta!=NULL);
125         TEST_SUCCESS(status);
126         b = utext_isLengthExpensive(uta);
127         TEST_ASSERT(b==TRUE);
128         len = utext_nativeLength(uta);
129         TEST_ASSERT(len == u_strlen(uString));
130         b = utext_isLengthExpensive(uta);
131         TEST_ASSERT(b==FALSE);
132 
133         c = utext_char32At(uta, 0);
134         TEST_ASSERT(c==uString[0]);
135 
136         c = utext_current32(uta);
137         TEST_ASSERT(c==uString[0]);
138 
139         c = utext_next32(uta);
140         TEST_ASSERT(c==uString[0]);
141         c = utext_current32(uta);
142         TEST_ASSERT(c==uString[1]);
143 
144         c = utext_previous32(uta);
145         TEST_ASSERT(c==uString[0]);
146         c = utext_current32(uta);
147         TEST_ASSERT(c==uString[0]);
148 
149         c = utext_next32From(uta, 1);
150         TEST_ASSERT(c==uString[1]);
151         c = utext_next32From(uta, u_strlen(uString));
152         TEST_ASSERT(c==U_SENTINEL);
153 
154         c = utext_previous32From(uta, 2);
155         TEST_ASSERT(c==uString[1]);
156         i = utext_getNativeIndex(uta);
157         TEST_ASSERT(i == 1);
158 
159         utext_setNativeIndex(uta, 0);
160         b = utext_moveIndex32(uta, 1);
161         TEST_ASSERT(b==TRUE);
162         i = utext_getNativeIndex(uta);
163         TEST_ASSERT(i==1);
164 
165         b = utext_moveIndex32(uta, u_strlen(uString)-1);
166         TEST_ASSERT(b==TRUE);
167         i = utext_getNativeIndex(uta);
168         TEST_ASSERT(i==u_strlen(uString));
169 
170         b = utext_moveIndex32(uta, 1);
171         TEST_ASSERT(b==FALSE);
172         i = utext_getNativeIndex(uta);
173         TEST_ASSERT(i==u_strlen(uString));
174 
175         utext_setNativeIndex(uta, 0);
176         c = UTEXT_NEXT32(uta);
177         TEST_ASSERT(c==uString[0]);
178         c = utext_current32(uta);
179         TEST_ASSERT(c==uString[1]);
180 
181         c = UTEXT_PREVIOUS32(uta);
182         TEST_ASSERT(c==uString[0]);
183         c = UTEXT_PREVIOUS32(uta);
184         TEST_ASSERT(c==U_SENTINEL);
185 
186 
187         utext_close(uta);
188     }
189 
190     {
191         /*
192          * UText opened on a NULL string with zero length
193          */
194         UText    *uta;
195         UChar32   c;
196 
197         status = U_ZERO_ERROR;
198         uta = utext_openUChars(NULL, NULL, 0, &status);
199         TEST_SUCCESS(status);
200         c = UTEXT_NEXT32(uta);
201         TEST_ASSERT(c == U_SENTINEL);
202         utext_close(uta);
203 
204         uta = utext_openUTF8(NULL, NULL, 0, &status);
205         TEST_SUCCESS(status);
206         c = UTEXT_NEXT32(uta);
207         TEST_ASSERT(c == U_SENTINEL);
208         utext_close(uta);
209     }
210 
211 
212     {
213         /*
214          * extract
215          */
216         UText     *uta;
217         UChar     uString[]  = {0x41, 0x42, 0x43, 0};
218         UChar     buf[100];
219         int32_t   i;
220         /* Test pinning of input bounds */
221         UChar     uString2[]  = {0x41, 0x42, 0x43, 0x44, 0x45,
222                                  0x46, 0x47, 0x48, 0x49, 0x4A, 0};
223         UChar *   uString2Ptr = uString2 + 5;
224 
225         status = U_ZERO_ERROR;
226         uta = utext_openUChars(NULL, uString, -1, &status);
227         TEST_SUCCESS(status);
228 
229         status = U_ZERO_ERROR;
230         i = utext_extract(uta, 0, 100, NULL, 0, &status);
231         TEST_ASSERT(status==U_BUFFER_OVERFLOW_ERROR);
232         TEST_ASSERT(i == u_strlen(uString));
233 
234         status = U_ZERO_ERROR;
235         memset(buf, 0, sizeof(buf));
236         i = utext_extract(uta, 0, 100, buf, 100, &status);
237         TEST_SUCCESS(status);
238         TEST_ASSERT(i == u_strlen(uString));
239         i = u_strcmp(uString, buf);
240         TEST_ASSERT(i == 0);
241         utext_close(uta);
242 
243         /* Test pinning of input bounds */
244         status = U_ZERO_ERROR;
245         uta = utext_openUChars(NULL, uString2Ptr, -1, &status);
246         TEST_SUCCESS(status);
247 
248         status = U_ZERO_ERROR;
249         memset(buf, 0, sizeof(buf));
250         i = utext_extract(uta, -3, 20, buf, 100, &status);
251         TEST_SUCCESS(status);
252         TEST_ASSERT(i == u_strlen(uString2Ptr));
253         i = u_strcmp(uString2Ptr, buf);
254         TEST_ASSERT(i == 0);
255         utext_close(uta);
256     }
257 
258     {
259         /*
260          *  Copy, Replace, isWritable
261          *    Can't create an editable UText from plain C, so all we
262          *    can easily do is check that errors returned.
263          */
264         UText     *uta;
265         UChar     uString[]  = {0x41, 0x42, 0x43, 0};
266         UBool     b;
267 
268         status = U_ZERO_ERROR;
269         uta = utext_openUChars(NULL, uString, -1, &status);
270         TEST_SUCCESS(status);
271 
272         b = utext_isWritable(uta);
273         TEST_ASSERT(b == FALSE);
274 
275         b = utext_hasMetaData(uta);
276         TEST_ASSERT(b == FALSE);
277 
278         utext_replace(uta,
279                       0, 1,     /* start, limit */
280                       uString, -1,  /* replacement, replacement length */
281                       &status);
282         TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
283 
284 
285         utext_copy(uta,
286                    0, 1,         /* start, limit      */
287                    2,            /* destination index */
288                    FALSE,        /* move flag         */
289                    &status);
290         TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
291 
292         utext_close(uta);
293     }
294 
295 
296 }
297 
298