]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/utexttst.c
1 /********************************************************************
3 * Copyright (c) 2005-2006, 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
);
188 UChar uString
[] = {0x41, 0x42, 0x43, 0};
192 status
= U_ZERO_ERROR
;
193 uta
= utext_openUChars(NULL
, uString
, -1, &status
);
194 TEST_SUCCESS(status
);
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
));
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
);
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.
218 UChar uString
[] = {0x41, 0x42, 0x43, 0};
221 status
= U_ZERO_ERROR
;
222 uta
= utext_openUChars(NULL
, uString
, -1, &status
);
223 TEST_SUCCESS(status
);
225 b
= utext_isWritable(uta
);
226 TEST_ASSERT(b
== FALSE
);
228 b
= utext_hasMetaData(uta
);
229 TEST_ASSERT(b
== FALSE
);
232 0, 1, /* start, limit */
233 uString
, -1, /* replacement, replacement length */
235 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
239 0, 1, /* start, limit */
240 2, /* destination index */
241 FALSE
, /* move flag */
243 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);