]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/utexttst.c
1 /********************************************************************
3 * Copyright (c) 2005-2011, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
9 * Modification History:
11 * Date Name Description
12 * 06/13/2005 Andy Heninger Creation
13 *******************************************************************************
16 #include "unicode/utypes.h"
17 #include "unicode/utext.h"
18 #include "unicode/ustring.h"
24 static void TestAPI(void);
25 void addUTextTest(TestNode
** root
);
29 addUTextTest(TestNode
** root
)
31 addTest(root
, &TestAPI
, "tsutil/UTextTest/TestAPI");
35 #define TEST_ASSERT(x) \
36 {if ((x)==FALSE) {log_err("Test failure in file %s at line %d\n", __FILE__, __LINE__);\
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)); \
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.
58 static void TestAPI(void) {
59 UErrorCode status
= U_ZERO_ERROR
;
60 UBool gFailed
= FALSE
;
64 UText utLoc
= UTEXT_INITIALIZER
;
65 const char * cString
= "\x61\x62\x63\x64";
66 UChar uString
[] = {0x41, 0x42, 0x43, 0};
71 uta
= utext_openUChars(NULL
, uString
, -1, &status
);
73 c
= utext_next32(uta
);
74 TEST_ASSERT(c
== 0x41);
75 utb
= utext_close(uta
);
76 TEST_ASSERT(utb
== NULL
);
78 uta
= utext_openUTF8(&utLoc
, cString
, -1, &status
);
80 TEST_ASSERT(uta
== &utLoc
);
82 uta
= utext_close(&utLoc
);
83 TEST_ASSERT(uta
== &utLoc
);
88 UChar uString
[] = {0x41, 0x42, 0x43, 0};
93 status
= U_ZERO_ERROR
;
94 uta
= utext_openUChars(NULL
, uString
, -1, &status
);
96 utb
= utext_clone(NULL
, uta
, FALSE
, FALSE
, &status
);
98 TEST_ASSERT(utb
!= NULL
);
99 TEST_ASSERT(utb
!= uta
);
100 len
= utext_nativeLength(uta
);
101 TEST_ASSERT(len
== u_strlen(uString
));
106 /* basic access functions */
108 UChar uString
[] = {0x41, 0x42, 0x43, 0};
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
);
126 c
= utext_char32At(uta
, 0);
127 TEST_ASSERT(c
==uString
[0]);
129 c
= utext_current32(uta
);
130 TEST_ASSERT(c
==uString
[0]);
132 c
= utext_next32(uta
);
133 TEST_ASSERT(c
==uString
[0]);
134 c
= utext_current32(uta
);
135 TEST_ASSERT(c
==uString
[1]);
137 c
= utext_previous32(uta
);
138 TEST_ASSERT(c
==uString
[0]);
139 c
= utext_current32(uta
);
140 TEST_ASSERT(c
==uString
[0]);
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
);
147 c
= utext_previous32From(uta
, 2);
148 TEST_ASSERT(c
==uString
[1]);
149 i
= utext_getNativeIndex(uta
);
152 utext_setNativeIndex(uta
, 0);
153 b
= utext_moveIndex32(uta
, 1);
154 TEST_ASSERT(b
==TRUE
);
155 i
= utext_getNativeIndex(uta
);
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
));
163 b
= utext_moveIndex32(uta
, 1);
164 TEST_ASSERT(b
==FALSE
);
165 i
= utext_getNativeIndex(uta
);
166 TEST_ASSERT(i
==u_strlen(uString
));
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]);
174 c
= UTEXT_PREVIOUS32(uta
);
175 TEST_ASSERT(c
==uString
[0]);
176 c
= UTEXT_PREVIOUS32(uta
);
177 TEST_ASSERT(c
==U_SENTINEL
);
185 * UText opened on a NULL string with zero length
190 status
= U_ZERO_ERROR
;
191 uta
= utext_openUChars(NULL
, NULL
, 0, &status
);
192 TEST_SUCCESS(status
);
193 c
= UTEXT_NEXT32(uta
);
194 TEST_ASSERT(c
== U_SENTINEL
);
197 uta
= utext_openUTF8(NULL
, NULL
, 0, &status
);
198 TEST_SUCCESS(status
);
199 c
= UTEXT_NEXT32(uta
);
200 TEST_ASSERT(c
== U_SENTINEL
);
210 UChar uString
[] = {0x41, 0x42, 0x43, 0};
213 /* Test pinning of input bounds */
214 UChar uString2
[] = {0x41, 0x42, 0x43, 0x44, 0x45,
215 0x46, 0x47, 0x48, 0x49, 0x4A, 0};
216 UChar
* uString2Ptr
= uString2
+ 5;
218 status
= U_ZERO_ERROR
;
219 uta
= utext_openUChars(NULL
, uString
, -1, &status
);
220 TEST_SUCCESS(status
);
222 status
= U_ZERO_ERROR
;
223 i
= utext_extract(uta
, 0, 100, NULL
, 0, &status
);
224 TEST_ASSERT(status
==U_BUFFER_OVERFLOW_ERROR
);
225 TEST_ASSERT(i
== u_strlen(uString
));
227 status
= U_ZERO_ERROR
;
228 memset(buf
, 0, sizeof(buf
));
229 i
= utext_extract(uta
, 0, 100, buf
, 100, &status
);
230 TEST_SUCCESS(status
);
231 TEST_ASSERT(i
== u_strlen(uString
));
232 i
= u_strcmp(uString
, buf
);
236 /* Test pinning of input bounds */
237 status
= U_ZERO_ERROR
;
238 uta
= utext_openUChars(NULL
, uString2Ptr
, -1, &status
);
239 TEST_SUCCESS(status
);
241 status
= U_ZERO_ERROR
;
242 memset(buf
, 0, sizeof(buf
));
243 i
= utext_extract(uta
, -3, 20, buf
, 100, &status
);
244 TEST_SUCCESS(status
);
245 TEST_ASSERT(i
== u_strlen(uString2Ptr
));
246 i
= u_strcmp(uString2Ptr
, buf
);
253 * Copy, Replace, isWritable
254 * Can't create an editable UText from plain C, so all we
255 * can easily do is check that errors returned.
258 UChar uString
[] = {0x41, 0x42, 0x43, 0};
261 status
= U_ZERO_ERROR
;
262 uta
= utext_openUChars(NULL
, uString
, -1, &status
);
263 TEST_SUCCESS(status
);
265 b
= utext_isWritable(uta
);
266 TEST_ASSERT(b
== FALSE
);
268 b
= utext_hasMetaData(uta
);
269 TEST_ASSERT(b
== FALSE
);
272 0, 1, /* start, limit */
273 uString
, -1, /* replacement, replacement length */
275 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
279 0, 1, /* start, limit */
280 2, /* destination index */
281 FALSE
, /* move flag */
283 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);