]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/utexttst.c
1 /********************************************************************
3 * Copyright (c) 2005-2013, 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
;
61 (void)gFailed
; /* Suppress set but not used warning. */
65 UText utLoc
= UTEXT_INITIALIZER
;
66 const char * cString
= "\x61\x62\x63\x64";
67 UChar uString
[] = {0x41, 0x42, 0x43, 0};
72 uta
= utext_openUChars(NULL
, uString
, -1, &status
);
74 c
= utext_next32(uta
);
75 TEST_ASSERT(c
== 0x41);
76 utb
= utext_close(uta
);
77 TEST_ASSERT(utb
== NULL
);
79 uta
= utext_openUTF8(&utLoc
, cString
, -1, &status
);
81 TEST_ASSERT(uta
== &utLoc
);
83 uta
= utext_close(&utLoc
);
84 TEST_ASSERT(uta
== &utLoc
);
89 UChar uString
[] = {0x41, 0x42, 0x43, 0};
94 status
= U_ZERO_ERROR
;
95 uta
= utext_openUChars(NULL
, uString
, -1, &status
);
97 utb
= utext_clone(NULL
, uta
, FALSE
, FALSE
, &status
);
99 TEST_ASSERT(utb
!= NULL
);
100 TEST_ASSERT(utb
!= uta
);
101 len
= utext_nativeLength(uta
);
102 TEST_ASSERT(len
== u_strlen(uString
));
107 /* basic access functions */
109 UChar uString
[] = {0x41, 0x42, 0x43, 0};
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
);
127 c
= utext_char32At(uta
, 0);
128 TEST_ASSERT(c
==uString
[0]);
130 c
= utext_current32(uta
);
131 TEST_ASSERT(c
==uString
[0]);
133 c
= utext_next32(uta
);
134 TEST_ASSERT(c
==uString
[0]);
135 c
= utext_current32(uta
);
136 TEST_ASSERT(c
==uString
[1]);
138 c
= utext_previous32(uta
);
139 TEST_ASSERT(c
==uString
[0]);
140 c
= utext_current32(uta
);
141 TEST_ASSERT(c
==uString
[0]);
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
);
148 c
= utext_previous32From(uta
, 2);
149 TEST_ASSERT(c
==uString
[1]);
150 i
= utext_getNativeIndex(uta
);
153 utext_setNativeIndex(uta
, 0);
154 b
= utext_moveIndex32(uta
, 1);
155 TEST_ASSERT(b
==TRUE
);
156 i
= utext_getNativeIndex(uta
);
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
));
164 b
= utext_moveIndex32(uta
, 1);
165 TEST_ASSERT(b
==FALSE
);
166 i
= utext_getNativeIndex(uta
);
167 TEST_ASSERT(i
==u_strlen(uString
));
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]);
175 c
= UTEXT_PREVIOUS32(uta
);
176 TEST_ASSERT(c
==uString
[0]);
177 c
= UTEXT_PREVIOUS32(uta
);
178 TEST_ASSERT(c
==U_SENTINEL
);
186 * UText opened on a NULL string with zero length
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
);
198 uta
= utext_openUTF8(NULL
, NULL
, 0, &status
);
199 TEST_SUCCESS(status
);
200 c
= UTEXT_NEXT32(uta
);
201 TEST_ASSERT(c
== U_SENTINEL
);
211 UChar uString
[] = {0x41, 0x42, 0x43, 0};
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;
219 status
= U_ZERO_ERROR
;
220 uta
= utext_openUChars(NULL
, uString
, -1, &status
);
221 TEST_SUCCESS(status
);
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
));
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
);
237 /* Test pinning of input bounds */
238 status
= U_ZERO_ERROR
;
239 uta
= utext_openUChars(NULL
, uString2Ptr
, -1, &status
);
240 TEST_SUCCESS(status
);
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
);
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.
259 UChar uString
[] = {0x41, 0x42, 0x43, 0};
262 status
= U_ZERO_ERROR
;
263 uta
= utext_openUChars(NULL
, uString
, -1, &status
);
264 TEST_SUCCESS(status
);
266 b
= utext_isWritable(uta
);
267 TEST_ASSERT(b
== FALSE
);
269 b
= utext_hasMetaData(uta
);
270 TEST_ASSERT(b
== FALSE
);
273 0, 1, /* start, limit */
274 uString
, -1, /* replacement, replacement length */
276 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
280 0, 1, /* start, limit */
281 2, /* destination index */
282 FALSE
, /* move flag */
284 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);