1/******************************************************************** 2 * COPYRIGHT: 3 * Copyright (c) 2005-2011, International Business Machines Corporation and 4 * others. All Rights Reserved. 5 ********************************************************************/ 6/* 7* File utexttst.c 8* 9* Modification History: 10* 11* Date Name Description 12* 06/13/2005 Andy Heninger Creation 13******************************************************************************* 14*/ 15 16#include "unicode/utypes.h" 17#include "unicode/utext.h" 18#include "unicode/ustring.h" 19#include "cintltst.h" 20#include "memory.h" 21#include "string.h" 22 23 24static void TestAPI(void); 25void addUTextTest(TestNode** root); 26 27 28void 29addUTextTest(TestNode** root) 30{ 31 addTest(root, &TestAPI , "tsutil/UTextTest/TestAPI"); 32} 33 34 35#define TEST_ASSERT(x) \ 36 {if ((x)==FALSE) {log_err("Test failure in file %s at line %d\n", __FILE__, __LINE__);\ 37 gFailed = TRUE;\ 38 }} 39 40 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)); \ 44 gFailed = TRUE;\ 45 }} 46 47 48 49/* 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. 55 */ 56 57 58static void TestAPI(void) { 59 UErrorCode status = U_ZERO_ERROR; 60 UBool gFailed = FALSE; 61 62 /* Open */ 63 { 64 UText utLoc = UTEXT_INITIALIZER; 65 const char * cString = "\x61\x62\x63\x64"; 66 UChar uString[] = {0x41, 0x42, 0x43, 0}; 67 UText *uta; 68 UText *utb; 69 UChar c; 70 71 uta = utext_openUChars(NULL, uString, -1, &status); 72 TEST_SUCCESS(status); 73 c = utext_next32(uta); 74 TEST_ASSERT(c == 0x41); 75 utb = utext_close(uta); 76 TEST_ASSERT(utb == NULL); 77 78 uta = utext_openUTF8(&utLoc, cString, -1, &status); 79 TEST_SUCCESS(status); 80 TEST_ASSERT(uta == &utLoc); 81 82 uta = utext_close(&utLoc); 83 TEST_ASSERT(uta == &utLoc); 84 } 85 86 /* utext_clone() */ 87 { 88 UChar uString[] = {0x41, 0x42, 0x43, 0}; 89 int64_t len; 90 UText *uta; 91 UText *utb; 92 93 status = U_ZERO_ERROR; 94 uta = utext_openUChars(NULL, uString, -1, &status); 95 TEST_SUCCESS(status); 96 utb = utext_clone(NULL, uta, FALSE, FALSE, &status); 97 TEST_SUCCESS(status); 98 TEST_ASSERT(utb != NULL); 99 TEST_ASSERT(utb != uta); 100 len = utext_nativeLength(uta); 101 TEST_ASSERT(len == u_strlen(uString)); 102 utext_close(uta); 103 utext_close(utb); 104 } 105 106 /* basic access functions */ 107 { 108 UChar uString[] = {0x41, 0x42, 0x43, 0}; 109 UText *uta; 110 UChar32 c; 111 int64_t len; 112 UBool b; 113 int64_t i; 114 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); 125 126 c = utext_char32At(uta, 0); 127 TEST_ASSERT(c==uString[0]); 128 129 c = utext_current32(uta); 130 TEST_ASSERT(c==uString[0]); 131 132 c = utext_next32(uta); 133 TEST_ASSERT(c==uString[0]); 134 c = utext_current32(uta); 135 TEST_ASSERT(c==uString[1]); 136 137 c = utext_previous32(uta); 138 TEST_ASSERT(c==uString[0]); 139 c = utext_current32(uta); 140 TEST_ASSERT(c==uString[0]); 141 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); 146 147 c = utext_previous32From(uta, 2); 148 TEST_ASSERT(c==uString[1]); 149 i = utext_getNativeIndex(uta); 150 TEST_ASSERT(i == 1); 151 152 utext_setNativeIndex(uta, 0); 153 b = utext_moveIndex32(uta, 1); 154 TEST_ASSERT(b==TRUE); 155 i = utext_getNativeIndex(uta); 156 TEST_ASSERT(i==1); 157 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)); 162 163 b = utext_moveIndex32(uta, 1); 164 TEST_ASSERT(b==FALSE); 165 i = utext_getNativeIndex(uta); 166 TEST_ASSERT(i==u_strlen(uString)); 167 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]); 173 174 c = UTEXT_PREVIOUS32(uta); 175 TEST_ASSERT(c==uString[0]); 176 c = UTEXT_PREVIOUS32(uta); 177 TEST_ASSERT(c==U_SENTINEL); 178 179 180 utext_close(uta); 181 } 182 183 { 184 /* 185 * UText opened on a NULL string with zero length 186 */ 187 UText *uta; 188 UChar32 c; 189 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); 195 utext_close(uta); 196 197 uta = utext_openUTF8(NULL, NULL, 0, &status); 198 TEST_SUCCESS(status); 199 c = UTEXT_NEXT32(uta); 200 TEST_ASSERT(c == U_SENTINEL); 201 utext_close(uta); 202 } 203 204 205 { 206 /* 207 * extract 208 */ 209 UText *uta; 210 UChar uString[] = {0x41, 0x42, 0x43, 0}; 211 UChar buf[100]; 212 int32_t i; 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; 217 218 status = U_ZERO_ERROR; 219 uta = utext_openUChars(NULL, uString, -1, &status); 220 TEST_SUCCESS(status); 221 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)); 226 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); 233 TEST_ASSERT(i == 0); 234 utext_close(uta); 235 236 /* Test pinning of input bounds */ 237 status = U_ZERO_ERROR; 238 uta = utext_openUChars(NULL, uString2Ptr, -1, &status); 239 TEST_SUCCESS(status); 240 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); 247 TEST_ASSERT(i == 0); 248 utext_close(uta); 249 } 250 251 { 252 /* 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. 256 */ 257 UText *uta; 258 UChar uString[] = {0x41, 0x42, 0x43, 0}; 259 UBool b; 260 261 status = U_ZERO_ERROR; 262 uta = utext_openUChars(NULL, uString, -1, &status); 263 TEST_SUCCESS(status); 264 265 b = utext_isWritable(uta); 266 TEST_ASSERT(b == FALSE); 267 268 b = utext_hasMetaData(uta); 269 TEST_ASSERT(b == FALSE); 270 271 utext_replace(uta, 272 0, 1, /* start, limit */ 273 uString, -1, /* replacement, replacement length */ 274 &status); 275 TEST_ASSERT(status == U_NO_WRITE_PERMISSION); 276 277 278 utext_copy(uta, 279 0, 1, /* start, limit */ 280 2, /* destination index */ 281 FALSE, /* move flag */ 282 &status); 283 TEST_ASSERT(status == U_NO_WRITE_PERMISSION); 284 285 utext_close(uta); 286 } 287 288 289} 290 291