]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/utexttst.c
1 // © 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) UPRV_BLOCK_MACRO_BEGIN { \
39 log_err("Test failure in file %s at line %d\n", __FILE__, __LINE__); \
42 } UPRV_BLOCK_MACRO_END
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)); \
51 } UPRV_BLOCK_MACRO_END
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.
64 static void TestAPI(void) {
65 UErrorCode status
= U_ZERO_ERROR
;
66 UBool gFailed
= FALSE
;
67 (void)gFailed
; /* Suppress set but not used warning. */
71 UText utLoc
= UTEXT_INITIALIZER
;
72 const char * cString
= "\x61\x62\x63\x64";
73 UChar uString
[] = {0x41, 0x42, 0x43, 0};
78 uta
= utext_openUChars(NULL
, uString
, -1, &status
);
80 c
= utext_next32(uta
);
81 TEST_ASSERT(c
== 0x41);
82 utb
= utext_close(uta
);
83 TEST_ASSERT(utb
== NULL
);
85 uta
= utext_openUTF8(&utLoc
, cString
, -1, &status
);
87 TEST_ASSERT(uta
== &utLoc
);
89 uta
= utext_close(&utLoc
);
90 TEST_ASSERT(uta
== &utLoc
);
95 UChar uString
[] = {0x41, 0x42, 0x43, 0};
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
));
113 /* basic access functions */
115 UChar uString
[] = {0x41, 0x42, 0x43, 0};
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
);
133 c
= utext_char32At(uta
, 0);
134 TEST_ASSERT(c
==uString
[0]);
136 c
= utext_current32(uta
);
137 TEST_ASSERT(c
==uString
[0]);
139 c
= utext_next32(uta
);
140 TEST_ASSERT(c
==uString
[0]);
141 c
= utext_current32(uta
);
142 TEST_ASSERT(c
==uString
[1]);
144 c
= utext_previous32(uta
);
145 TEST_ASSERT(c
==uString
[0]);
146 c
= utext_current32(uta
);
147 TEST_ASSERT(c
==uString
[0]);
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
);
154 c
= utext_previous32From(uta
, 2);
155 TEST_ASSERT(c
==uString
[1]);
156 i
= utext_getNativeIndex(uta
);
159 utext_setNativeIndex(uta
, 0);
160 b
= utext_moveIndex32(uta
, 1);
161 TEST_ASSERT(b
==TRUE
);
162 i
= utext_getNativeIndex(uta
);
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
));
170 b
= utext_moveIndex32(uta
, 1);
171 TEST_ASSERT(b
==FALSE
);
172 i
= utext_getNativeIndex(uta
);
173 TEST_ASSERT(i
==u_strlen(uString
));
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]);
181 c
= UTEXT_PREVIOUS32(uta
);
182 TEST_ASSERT(c
==uString
[0]);
183 c
= UTEXT_PREVIOUS32(uta
);
184 TEST_ASSERT(c
==U_SENTINEL
);
192 * UText opened on a NULL string with zero length
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
);
204 uta
= utext_openUTF8(NULL
, NULL
, 0, &status
);
205 TEST_SUCCESS(status
);
206 c
= UTEXT_NEXT32(uta
);
207 TEST_ASSERT(c
== U_SENTINEL
);
217 UChar uString
[] = {0x41, 0x42, 0x43, 0};
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;
225 status
= U_ZERO_ERROR
;
226 uta
= utext_openUChars(NULL
, uString
, -1, &status
);
227 TEST_SUCCESS(status
);
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
));
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
);
243 /* Test pinning of input bounds */
244 status
= U_ZERO_ERROR
;
245 uta
= utext_openUChars(NULL
, uString2Ptr
, -1, &status
);
246 TEST_SUCCESS(status
);
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
);
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.
265 UChar uString
[] = {0x41, 0x42, 0x43, 0};
268 status
= U_ZERO_ERROR
;
269 uta
= utext_openUChars(NULL
, uString
, -1, &status
);
270 TEST_SUCCESS(status
);
272 b
= utext_isWritable(uta
);
273 TEST_ASSERT(b
== FALSE
);
275 b
= utext_hasMetaData(uta
);
276 TEST_ASSERT(b
== FALSE
);
279 0, 1, /* start, limit */
280 uString
, -1, /* replacement, replacement length */
282 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
286 0, 1, /* start, limit */
287 2, /* destination index */
288 FALSE
, /* move flag */
290 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);