1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 2005-2013, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
11 * Modification History:
13 * Date Name Description
14 * 06/13/2005 Andy Heninger Creation
15 *******************************************************************************
18 #include "unicode/utypes.h"
19 #include "unicode/utext.h"
20 #include "unicode/ustring.h"
26 static void TestAPI(void);
27 void addUTextTest(TestNode** root);
31 addUTextTest(TestNode** root)
33 addTest(root, &TestAPI , "tsutil/UTextTest/TestAPI");
37 #define TEST_ASSERT(x) \
38 {if ((x)==FALSE) {log_err("Test failure in file %s at line %d\n", __FILE__, __LINE__);\
43 #define TEST_SUCCESS(status) \
44 {if (U_FAILURE(status)) {log_err("Test failure in file %s at line %d. Error = \"%s\"\n", \
45 __FILE__, __LINE__, u_errorName(status)); \
52 * TestAPI verify that the UText API is accessible from C programs.
53 * This is not intended to be a complete test of the API functionality. That is
54 * in the C++ intltest program.
55 * This test is intended to check that everything can be accessed and built in
56 * a pure C enviornment.
60 static void TestAPI(void) {
61 UErrorCode status = U_ZERO_ERROR;
62 UBool gFailed = FALSE;
63 (void)gFailed; /* Suppress set but not used warning. */
67 UText utLoc = UTEXT_INITIALIZER;
68 const char * cString = "\x61\x62\x63\x64";
69 UChar uString[] = {0x41, 0x42, 0x43, 0};
74 uta = utext_openUChars(NULL, uString, -1, &status);
76 c = utext_next32(uta);
77 TEST_ASSERT(c == 0x41);
78 utb = utext_close(uta);
79 TEST_ASSERT(utb == NULL);
81 uta = utext_openUTF8(&utLoc, cString, -1, &status);
83 TEST_ASSERT(uta == &utLoc);
85 uta = utext_close(&utLoc);
86 TEST_ASSERT(uta == &utLoc);
91 UChar uString[] = {0x41, 0x42, 0x43, 0};
96 status = U_ZERO_ERROR;
97 uta = utext_openUChars(NULL, uString, -1, &status);
99 utb = utext_clone(NULL, uta, FALSE, FALSE, &status);
100 TEST_SUCCESS(status);
101 TEST_ASSERT(utb != NULL);
102 TEST_ASSERT(utb != uta);
103 len = utext_nativeLength(uta);
104 TEST_ASSERT(len == u_strlen(uString));
109 /* basic access functions */
111 UChar uString[] = {0x41, 0x42, 0x43, 0};
118 status = U_ZERO_ERROR;
119 uta = utext_openUChars(NULL, uString, -1, &status);
120 TEST_ASSERT(uta!=NULL);
121 TEST_SUCCESS(status);
122 b = utext_isLengthExpensive(uta);
123 TEST_ASSERT(b==TRUE);
124 len = utext_nativeLength(uta);
125 TEST_ASSERT(len == u_strlen(uString));
126 b = utext_isLengthExpensive(uta);
127 TEST_ASSERT(b==FALSE);
129 c = utext_char32At(uta, 0);
130 TEST_ASSERT(c==uString[0]);
132 c = utext_current32(uta);
133 TEST_ASSERT(c==uString[0]);
135 c = utext_next32(uta);
136 TEST_ASSERT(c==uString[0]);
137 c = utext_current32(uta);
138 TEST_ASSERT(c==uString[1]);
140 c = utext_previous32(uta);
141 TEST_ASSERT(c==uString[0]);
142 c = utext_current32(uta);
143 TEST_ASSERT(c==uString[0]);
145 c = utext_next32From(uta, 1);
146 TEST_ASSERT(c==uString[1]);
147 c = utext_next32From(uta, u_strlen(uString));
148 TEST_ASSERT(c==U_SENTINEL);
150 c = utext_previous32From(uta, 2);
151 TEST_ASSERT(c==uString[1]);
152 i = utext_getNativeIndex(uta);
155 utext_setNativeIndex(uta, 0);
156 b = utext_moveIndex32(uta, 1);
157 TEST_ASSERT(b==TRUE);
158 i = utext_getNativeIndex(uta);
161 b = utext_moveIndex32(uta, u_strlen(uString)-1);
162 TEST_ASSERT(b==TRUE);
163 i = utext_getNativeIndex(uta);
164 TEST_ASSERT(i==u_strlen(uString));
166 b = utext_moveIndex32(uta, 1);
167 TEST_ASSERT(b==FALSE);
168 i = utext_getNativeIndex(uta);
169 TEST_ASSERT(i==u_strlen(uString));
171 utext_setNativeIndex(uta, 0);
172 c = UTEXT_NEXT32(uta);
173 TEST_ASSERT(c==uString[0]);
174 c = utext_current32(uta);
175 TEST_ASSERT(c==uString[1]);
177 c = UTEXT_PREVIOUS32(uta);
178 TEST_ASSERT(c==uString[0]);
179 c = UTEXT_PREVIOUS32(uta);
180 TEST_ASSERT(c==U_SENTINEL);
188 * UText opened on a NULL string with zero length
193 status = U_ZERO_ERROR;
194 uta = utext_openUChars(NULL, NULL, 0, &status);
195 TEST_SUCCESS(status);
196 c = UTEXT_NEXT32(uta);
197 TEST_ASSERT(c == U_SENTINEL);
200 uta = utext_openUTF8(NULL, NULL, 0, &status);
201 TEST_SUCCESS(status);
202 c = UTEXT_NEXT32(uta);
203 TEST_ASSERT(c == U_SENTINEL);
213 UChar uString[] = {0x41, 0x42, 0x43, 0};
216 /* Test pinning of input bounds */
217 UChar uString2[] = {0x41, 0x42, 0x43, 0x44, 0x45,
218 0x46, 0x47, 0x48, 0x49, 0x4A, 0};
219 UChar * uString2Ptr = uString2 + 5;
221 status = U_ZERO_ERROR;
222 uta = utext_openUChars(NULL, uString, -1, &status);
223 TEST_SUCCESS(status);
225 status = U_ZERO_ERROR;
226 i = utext_extract(uta, 0, 100, NULL, 0, &status);
227 TEST_ASSERT(status==U_BUFFER_OVERFLOW_ERROR);
228 TEST_ASSERT(i == u_strlen(uString));
230 status = U_ZERO_ERROR;
231 memset(buf, 0, sizeof(buf));
232 i = utext_extract(uta, 0, 100, buf, 100, &status);
233 TEST_SUCCESS(status);
234 TEST_ASSERT(i == u_strlen(uString));
235 i = u_strcmp(uString, buf);
239 /* Test pinning of input bounds */
240 status = U_ZERO_ERROR;
241 uta = utext_openUChars(NULL, uString2Ptr, -1, &status);
242 TEST_SUCCESS(status);
244 status = U_ZERO_ERROR;
245 memset(buf, 0, sizeof(buf));
246 i = utext_extract(uta, -3, 20, buf, 100, &status);
247 TEST_SUCCESS(status);
248 TEST_ASSERT(i == u_strlen(uString2Ptr));
249 i = u_strcmp(uString2Ptr, buf);
256 * Copy, Replace, isWritable
257 * Can't create an editable UText from plain C, so all we
258 * can easily do is check that errors returned.
261 UChar uString[] = {0x41, 0x42, 0x43, 0};
264 status = U_ZERO_ERROR;
265 uta = utext_openUChars(NULL, uString, -1, &status);
266 TEST_SUCCESS(status);
268 b = utext_isWritable(uta);
269 TEST_ASSERT(b == FALSE);
271 b = utext_hasMetaData(uta);
272 TEST_ASSERT(b == FALSE);
275 0, 1, /* start, limit */
276 uString, -1, /* replacement, replacement length */
278 TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
282 0, 1, /* start, limit */
283 2, /* destination index */
284 FALSE, /* move flag */
286 TEST_ASSERT(status == U_NO_WRITE_PERMISSION);