]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/utexttst.c
ICU-400.37.tar.gz
[apple/icu.git] / icuSources / test / cintltst / utexttst.c
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2005-2006, International Business Machines Corporation and
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;
61
62 /* Open */
63 {
64 UText utLoc = UTEXT_INITIALIZER;
65 const char * cString = "\x61\x62\x63\x64";
66 UChar uString[] = {0x41, 0x42, 0x43, 0};
67 UText *uta;
68 UText *utb;
69 UChar c;
70
71 uta = utext_openUChars(NULL, uString, -1, &status);
72 TEST_SUCCESS(status);
73 c = utext_next32(uta);
74 TEST_ASSERT(c == 0x41);
75 utb = utext_close(uta);
76 TEST_ASSERT(utb == NULL);
77
78 uta = utext_openUTF8(&utLoc, cString, -1, &status);
79 TEST_SUCCESS(status);
80 TEST_ASSERT(uta == &utLoc);
81
82 uta = utext_close(&utLoc);
83 TEST_ASSERT(uta == &utLoc);
84 }
85
86 /* utext_clone() */
87 {
88 UChar uString[] = {0x41, 0x42, 0x43, 0};
89 int64_t len;
90 UText *uta;
91 UText *utb;
92
93 status = U_ZERO_ERROR;
94 uta = utext_openUChars(NULL, uString, -1, &status);
95 TEST_SUCCESS(status);
96 utb = utext_clone(NULL, uta, FALSE, FALSE, &status);
97 TEST_SUCCESS(status);
98 TEST_ASSERT(utb != NULL);
99 TEST_ASSERT(utb != uta);
100 len = utext_nativeLength(uta);
101 TEST_ASSERT(len == u_strlen(uString));
102 utext_close(uta);
103 utext_close(utb);
104 }
105
106 /* basic access functions */
107 {
108 UChar uString[] = {0x41, 0x42, 0x43, 0};
109 UText *uta;
110 UChar32 c;
111 int64_t len;
112 UBool b;
113 int64_t i;
114
115 status = U_ZERO_ERROR;
116 uta = utext_openUChars(NULL, uString, -1, &status);
117 TEST_ASSERT(uta!=NULL);
118 TEST_SUCCESS(status);
119 b = utext_isLengthExpensive(uta);
120 TEST_ASSERT(b==TRUE);
121 len = utext_nativeLength(uta);
122 TEST_ASSERT(len == u_strlen(uString));
123 b = utext_isLengthExpensive(uta);
124 TEST_ASSERT(b==FALSE);
125
126 c = utext_char32At(uta, 0);
127 TEST_ASSERT(c==uString[0]);
128
129 c = utext_current32(uta);
130 TEST_ASSERT(c==uString[0]);
131
132 c = utext_next32(uta);
133 TEST_ASSERT(c==uString[0]);
134 c = utext_current32(uta);
135 TEST_ASSERT(c==uString[1]);
136
137 c = utext_previous32(uta);
138 TEST_ASSERT(c==uString[0]);
139 c = utext_current32(uta);
140 TEST_ASSERT(c==uString[0]);
141
142 c = utext_next32From(uta, 1);
143 TEST_ASSERT(c==uString[1]);
144 c = utext_next32From(uta, u_strlen(uString));
145 TEST_ASSERT(c==U_SENTINEL);
146
147 c = utext_previous32From(uta, 2);
148 TEST_ASSERT(c==uString[1]);
149 i = utext_getNativeIndex(uta);
150 TEST_ASSERT(i == 1);
151
152 utext_setNativeIndex(uta, 0);
153 b = utext_moveIndex32(uta, 1);
154 TEST_ASSERT(b==TRUE);
155 i = utext_getNativeIndex(uta);
156 TEST_ASSERT(i==1);
157
158 b = utext_moveIndex32(uta, u_strlen(uString)-1);
159 TEST_ASSERT(b==TRUE);
160 i = utext_getNativeIndex(uta);
161 TEST_ASSERT(i==u_strlen(uString));
162
163 b = utext_moveIndex32(uta, 1);
164 TEST_ASSERT(b==FALSE);
165 i = utext_getNativeIndex(uta);
166 TEST_ASSERT(i==u_strlen(uString));
167
168 utext_setNativeIndex(uta, 0);
169 c = UTEXT_NEXT32(uta);
170 TEST_ASSERT(c==uString[0]);
171 c = utext_current32(uta);
172 TEST_ASSERT(c==uString[1]);
173
174 c = UTEXT_PREVIOUS32(uta);
175 TEST_ASSERT(c==uString[0]);
176 c = UTEXT_PREVIOUS32(uta);
177 TEST_ASSERT(c==U_SENTINEL);
178
179
180 utext_close(uta);
181 }
182
183 {
184 /*
185 * extract
186 */
187 UText *uta;
188 UChar uString[] = {0x41, 0x42, 0x43, 0};
189 UChar buf[100];
190 int32_t i;
191
192 status = U_ZERO_ERROR;
193 uta = utext_openUChars(NULL, uString, -1, &status);
194 TEST_SUCCESS(status);
195
196 status = U_ZERO_ERROR;
197 i = utext_extract(uta, 0, 100, NULL, 0, &status);
198 TEST_ASSERT(status==U_BUFFER_OVERFLOW_ERROR);
199 TEST_ASSERT(i == u_strlen(uString));
200
201 status = U_ZERO_ERROR;
202 memset(buf, 0, sizeof(buf));
203 i = utext_extract(uta, 0, 100, buf, 100, &status);
204 TEST_SUCCESS(status);
205 TEST_ASSERT(i == u_strlen(uString));
206 i = u_strcmp(uString, buf);
207 TEST_ASSERT(i == 0);
208 utext_close(uta);
209 }
210
211 {
212 /*
213 * Copy, Replace, isWritable
214 * Can't create an editable UText from plain C, so all we
215 * can easily do is check that errors returned.
216 */
217 UText *uta;
218 UChar uString[] = {0x41, 0x42, 0x43, 0};
219 UBool b;
220
221 status = U_ZERO_ERROR;
222 uta = utext_openUChars(NULL, uString, -1, &status);
223 TEST_SUCCESS(status);
224
225 b = utext_isWritable(uta);
226 TEST_ASSERT(b == FALSE);
227
228 b = utext_hasMetaData(uta);
229 TEST_ASSERT(b == FALSE);
230
231 utext_replace(uta,
232 0, 1, /* start, limit */
233 uString, -1, /* replacement, replacement length */
234 &status);
235 TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
236
237
238 utext_copy(uta,
239 0, 1, /* start, limit */
240 2, /* destination index */
241 FALSE, /* move flag */
242 &status);
243 TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
244
245 utext_close(uta);
246 }
247
248
249 }
250