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