1 /******************************************************************** 2 * COPYRIGHT: 3 * Copyright (c) 1997-2009, International Business Machines Corporation and 4 * others. All Rights Reserved. 5 ********************************************************************/ 6 /******************************************************************************* 7 * 8 * File CRESTST.C 9 * 10 * Modification History: 11 * Name Date Description 12 * Madhu Katragadda 05/09/2000 Ported Tests for New ResourceBundle API 13 * Madhu Katragadda 05/24/2000 Added new tests to test RES_BINARY for collationElements 14 ******************************************************************************** 15 */ 16 17 18 #include <time.h> 19 #include "unicode/utypes.h" 20 #include "cintltst.h" 21 #include "unicode/putil.h" 22 #include "unicode/ustring.h" 23 #include "unicode/ucnv.h" 24 #include "string.h" 25 #include "cstring.h" 26 #include "unicode/uchar.h" 27 #include "ucol_imp.h" /* for U_ICUDATA_COLL */ 28 #include "ubrkimpl.h" /* for U_ICUDATA_BRKITR */ 29 #define RESTEST_HEAP_CHECK 0 30 31 #include "unicode/uloc.h" 32 #include "unicode/ulocdata.h" 33 #include "uresimp.h" 34 #include "creststn.h" 35 #include "unicode/ctest.h" 36 #include "ucbuf.h" 37 #include "ureslocs.h" 38 39 static int32_t pass; 40 static int32_t fail; 41 42 /*****************************************************************************/ 43 /** 44 * Return a random unsigned long l where 0N <= l <= ULONG_MAX. 45 */ 46 47 static uint32_t 48 randul() 49 { 50 uint32_t l=0; 51 int32_t i; 52 static UBool initialized = FALSE; 53 if (!initialized) 54 { 55 srand((unsigned)time(NULL)); 56 initialized = TRUE; 57 } 58 /* Assume rand has at least 12 bits of precision */ 59 60 for (i=0; i<sizeof(l); ++i) 61 ((char*)&l)[i] = (char)((rand() & 0x0FF0) >> 4); 62 return l; 63 } 64 65 /** 66 * Return a random double x where 0.0 <= x < 1.0. 67 */ 68 static double 69 randd() 70 { 71 return ((double)randul()) / UINT32_MAX; 72 } 73 74 /** 75 * Return a random integer i where 0 <= i < n. 76 */ 77 static int32_t randi(int32_t n) 78 { 79 return (int32_t)(randd() * n); 80 } 81 /***************************************************************************************/ 82 /** 83 * Convert an integer, positive or negative, to a character string radix 10. 84 */ 85 static char* 86 itoa1(int32_t i, char* buf) 87 { 88 char *p = 0; 89 char* result = buf; 90 /* Handle negative */ 91 if(i < 0) { 92 *buf++ = '-'; 93 i = -i; 94 } 95 96 /* Output digits in reverse order */ 97 p = buf; 98 do { 99 *p++ = (char)('0' + (i % 10)); 100 i /= 10; 101 } 102 while(i); 103 *p-- = 0; 104 105 /* Reverse the string */ 106 while(buf < p) { 107 char c = *buf; 108 *buf++ = *p; 109 *p-- = c; 110 } 111 112 return result; 113 } 114 static const int32_t kERROR_COUNT = -1234567; 115 static const UChar kERROR[] = { 0x0045 /*E*/, 0x0052 /*'R'*/, 0x0052 /*'R'*/, 116 0x004F /*'O'*/, 0x0052/*'R'*/, 0x0000 /*'\0'*/}; 117 118 /*****************************************************************************/ 119 120 enum E_Where 121 { 122 e_Root, 123 e_te, 124 e_te_IN, 125 e_Where_count 126 }; 127 typedef enum E_Where E_Where; 128 /*****************************************************************************/ 129 130 #define CONFIRM_EQ(actual,expected) if (u_strcmp(expected,actual)==0){ record_pass(); } else { record_fail(); log_err("%s returned %s instead of %s\n", action, austrdup(actual), austrdup(expected)); } 131 #define CONFIRM_INT_EQ(actual,expected) if ((expected)==(actual)) { record_pass(); } else { record_fail(); log_err("%s returned %d instead of %d\n", action, actual, expected); } 132 #define CONFIRM_INT_GE(actual,expected) if ((actual)>=(expected)) { record_pass(); } else { record_fail(); log_err("%s returned %d instead of x >= %d\n", action, actual, expected); } 133 #define CONFIRM_INT_NE(actual,expected) if ((expected)!=(actual)) { record_pass(); } else { record_fail(); log_err("%s returned %d instead of x != %d\n", action, actual, expected); } 134 /*#define CONFIRM_ErrorCode(actual,expected) if ((expected)==(actual)) { record_pass(); } else { record_fail(); log_err("%s returned %s instead of %s\n", action, myErrorName(actual), myErrorName(expected)); } */ 135 static void 136 CONFIRM_ErrorCode(UErrorCode actual,UErrorCode expected) 137 { 138 if ((expected)==(actual)) 139 { 140 record_pass(); 141 } else { 142 record_fail(); 143 /*log_err("%s returned %s instead of %s\n", action, myErrorName(actual), myErrorName(expected)); */ 144 log_err("returned %s instead of %s\n", myErrorName(actual), myErrorName(expected)); 145 } 146 } 147 148 149 /* Array of our test objects */ 150 151 static struct 152 { 153 const char* name; 154 UErrorCode expected_constructor_status; 155 E_Where where; 156 UBool like[e_Where_count]; 157 UBool inherits[e_Where_count]; 158 } 159 param[] = 160 { 161 /* "te" means test */ 162 /* "IN" means inherits */ 163 /* "NE" or "ne" means "does not exist" */ 164 165 { "root", U_ZERO_ERROR, e_Root, { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } }, 166 { "te", U_ZERO_ERROR, e_te, { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } }, 167 { "te_IN", U_ZERO_ERROR, e_te_IN, { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } }, 168 { "te_NE", U_USING_FALLBACK_WARNING, e_te, { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } }, 169 { "te_IN_NE", U_USING_FALLBACK_WARNING, e_te_IN, { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } }, 170 { "ne", U_USING_DEFAULT_WARNING, e_Root, { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } } 171 }; 172 173 static int32_t bundles_count = sizeof(param) / sizeof(param[0]); 174 175 176 177 /*static void printUChars(UChar*);*/ 178 static void TestDecodedBundle(void); 179 static void TestGetKeywordValues(void); 180 static void TestGetFunctionalEquivalent(void); 181 static void TestCLDRStyleAliases(void); 182 static void TestFallbackCodes(void); 183 static void TestGetUTF8String(void); 184 static void TestCLDRVersion(void); 185 186 /***************************************************************************************/ 187 188 /* Array of our test objects */ 189 190 void addNEWResourceBundleTest(TestNode** root) 191 { 192 addTest(root, &TestErrorCodes, "tsutil/creststn/TestErrorCodes"); 193 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION 194 addTest(root, &TestEmptyBundle, "tsutil/creststn/TestEmptyBundle"); 195 addTest(root, &TestConstruction1, "tsutil/creststn/TestConstruction1"); 196 addTest(root, &TestResourceBundles, "tsutil/creststn/TestResourceBundles"); 197 addTest(root, &TestNewTypes, "tsutil/creststn/TestNewTypes"); 198 addTest(root, &TestEmptyTypes, "tsutil/creststn/TestEmptyTypes"); 199 addTest(root, &TestBinaryCollationData, "tsutil/creststn/TestBinaryCollationData"); 200 addTest(root, &TestAPI, "tsutil/creststn/TestAPI"); 201 addTest(root, &TestErrorConditions, "tsutil/creststn/TestErrorConditions"); 202 addTest(root, &TestDecodedBundle, "tsutil/creststn/TestDecodedBundle"); 203 addTest(root, &TestResourceLevelAliasing, "tsutil/creststn/TestResourceLevelAliasing"); 204 addTest(root, &TestDirectAccess, "tsutil/creststn/TestDirectAccess"); 205 addTest(root, &TestXPath, "tsutil/creststn/TestXPath"); 206 addTest(root, &TestCLDRStyleAliases, "tsutil/creststn/TestCLDRStyleAliases"); 207 addTest(root, &TestFallbackCodes, "tsutil/creststn/TestFallbackCodes"); 208 addTest(root, &TestGetUTF8String, "tsutil/creststn/TestGetUTF8String"); 209 addTest(root, &TestCLDRVersion, "tsutil/creststn/TestCLDRVersion"); 210 #endif 211 addTest(root, &TestFallback, "tsutil/creststn/TestFallback"); 212 addTest(root, &TestGetVersion, "tsutil/creststn/TestGetVersion"); 213 addTest(root, &TestGetVersionColl, "tsutil/creststn/TestGetVersionColl"); 214 addTest(root, &TestAliasConflict, "tsutil/creststn/TestAliasConflict"); 215 addTest(root, &TestGetKeywordValues, "tsutil/creststn/TestGetKeywordValues"); 216 addTest(root, &TestGetFunctionalEquivalent,"tsutil/creststn/TestGetFunctionalEquivalent"); 217 addTest(root, &TestJB3763, "tsutil/creststn/TestJB3763"); 218 addTest(root, &TestStackReuse, "tsutil/creststn/TestStackReuse"); 219 } 220 221 222 /***************************************************************************************/ 223 static const char* norwayNames[] = { 224 "no_NO_NY", 225 "no_NO", 226 "no", 227 "nn_NO", 228 "nn", 229 "nb_NO", 230 "nb" 231 }; 232 233 static const char* norwayLocales[] = { 234 "nn_NO", 235 "nb_NO", 236 "nb", 237 "nn_NO", 238 "nn", 239 "nb_NO", 240 "nb" 241 }; 242 243 static void checkStatus(int32_t line, UErrorCode expected, UErrorCode status) { 244 if(U_FAILURE(status)) { 245 log_data_err("Resource not present, cannot test (%s:%d)\n", __FILE__, line); 246 } 247 if(status != expected) { 248 log_err_status(status, "%s:%d: Expected error code %s, got error code %s\n", __FILE__, line, u_errorName(expected), u_errorName(status)); 249 } 250 } 251 252 static void TestErrorCodes(void) { 253 UErrorCode status = U_USING_DEFAULT_WARNING; 254 255 UResourceBundle *r = NULL, *r2 = NULL; 256 257 /* First check with ICUDATA */ 258 /* first bundle should return fallback warning */ 259 r = ures_open(NULL, "ti_ER_ASSAB", &status); 260 checkStatus(__LINE__, U_USING_FALLBACK_WARNING, status); 261 ures_close(r); 262 263 /* this bundle should return zero error, so it shouldn't change the status */ 264 status = U_USING_DEFAULT_WARNING; 265 r = ures_open(NULL, "ti_ER", &status); 266 checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status); 267 268 /* we look up the resource which is aliased, but it lives in fallback */ 269 270 if(U_SUCCESS(status) && r != NULL) { 271 status = U_USING_DEFAULT_WARNING; 272 r2 = ures_getByKey(r, "LocaleScript", NULL, &status); /* LocaleScript lives in ti */ 273 checkStatus(__LINE__, U_USING_FALLBACK_WARNING, status); 274 } 275 ures_close(r); 276 277 /* this bundle should return zero error, so it shouldn't change the status */ 278 status = U_USING_DEFAULT_WARNING; 279 r = ures_open(U_ICUDATA_REGION, "ti", &status); 280 checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status); 281 282 /* we look up the resource which is aliased and at our level */ 283 if(U_SUCCESS(status) && r != NULL) { 284 status = U_USING_DEFAULT_WARNING; 285 r2 = ures_getByKey(r, "Countries", r2, &status); 286 checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status); 287 } 288 ures_close(r); 289 290 status = U_USING_FALLBACK_WARNING; 291 r = ures_open(NULL, "nolocale", &status); 292 checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status); 293 ures_close(r); 294 ures_close(r2); 295 296 /** Now, with the collation bundle **/ 297 298 /* first bundle should return fallback warning */ 299 r = ures_open(U_ICUDATA_COLL, "sr_YU_VOJVODINA", &status); 300 checkStatus(__LINE__, U_USING_FALLBACK_WARNING, status); 301 ures_close(r); 302 303 /* this bundle should return zero error, so it shouldn't change the status */ 304 status = U_USING_FALLBACK_WARNING; 305 r = ures_open(U_ICUDATA_COLL, "sr", &status); 306 checkStatus(__LINE__, U_USING_FALLBACK_WARNING, status); 307 308 /* we look up the resource which is aliased */ 309 if(U_SUCCESS(status) && r != NULL) { 310 status = U_USING_DEFAULT_WARNING; 311 r2 = ures_getByKey(r, "collations", NULL, &status); 312 checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status); 313 } 314 ures_close(r); 315 316 /* this bundle should return zero error, so it shouldn't change the status */ 317 status = U_USING_DEFAULT_WARNING; 318 r = ures_open(U_ICUDATA_COLL, "sr", &status); 319 checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status); 320 321 /* we look up the resource which is aliased and at our level */ 322 if(U_SUCCESS(status) && r != NULL) { 323 status = U_USING_DEFAULT_WARNING; 324 r2 = ures_getByKey(r, "collations", r2, &status); 325 checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status); 326 } 327 ures_close(r); 328 329 status = U_USING_FALLBACK_WARNING; 330 r = ures_open(U_ICUDATA_COLL, "nolocale", &status); 331 checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status); 332 ures_close(r); 333 ures_close(r2); 334 } 335 336 static void TestAliasConflict(void) { 337 UErrorCode status = U_ZERO_ERROR; 338 UResourceBundle *he = NULL; 339 UResourceBundle *iw = NULL; 340 UResourceBundle *norway = NULL; 341 const UChar *result = NULL; 342 int32_t resultLen; 343 uint32_t size = 0; 344 uint32_t i = 0; 345 const char *realName = NULL; 346 347 he = ures_open(NULL, "he", &status); 348 iw = ures_open(NULL, "iw", &status); 349 if(U_FAILURE(status)) { 350 log_err_status(status, "Failed to get resource with %s\n", myErrorName(status)); 351 } 352 ures_close(iw); 353 result = ures_getStringByKey(he, "ExemplarCharacters", &resultLen, &status); 354 if(U_FAILURE(status) || result == NULL) { 355 log_err_status(status, "Failed to get resource ExemplarCharacters with %s\n", myErrorName(status)); 356 } 357 ures_close(he); 358 359 size = sizeof(norwayNames)/sizeof(norwayNames[0]); 360 for(i = 0; i < size; i++) { 361 status = U_ZERO_ERROR; 362 norway = ures_open(NULL, norwayNames[i], &status); 363 if(U_FAILURE(status)) { 364 log_err_status(status, "Failed to get resource with %s for %s\n", myErrorName(status), norwayNames[i]); 365 continue; 366 } 367 realName = ures_getLocale(norway, &status); 368 log_verbose("ures_getLocale(\"%s\")=%s\n", norwayNames[i], realName); 369 if(realName == NULL || strcmp(norwayLocales[i], realName) != 0) { 370 log_data_err("Wrong locale name for %s, expected %s, got %s\n", norwayNames[i], norwayLocales[i], realName); 371 } 372 ures_close(norway); 373 } 374 } 375 376 static void TestDecodedBundle(){ 377 378 UErrorCode error = U_ZERO_ERROR; 379 380 UResourceBundle* resB; 381 382 const UChar* srcFromRes; 383 int32_t len; 384 static const UChar uSrc[] = { 385 0x0009,0x092F,0x0941,0x0928,0x0947,0x0938,0x094D,0x0915,0x094B,0x0020,0x002E,0x0915,0x0947,0x0020,0x002E,0x090F, 386 0x0915,0x0020,0x002E,0x0905,0x0927,0x094D,0x092F,0x092F,0x0928,0x0020,0x002E,0x0915,0x0947,0x0020,0x0905,0x0928, 387 0x0941,0x0938,0x093E,0x0930,0x0020,0x0031,0x0039,0x0039,0x0030,0x0020,0x0924,0x0915,0x0020,0x0915,0x0902,0x092A, 388 0x094D,0x092F,0x0942,0x091F,0x0930,0x002D,0x092A,0x094D,0x0930,0x092C,0x0902,0x0927,0x093F,0x0924,0x0020,0x0938, 389 0x0942,0x091A,0x0928,0x093E,0x092A,0x094D,0x0930,0x0923,0x093E,0x0932,0x0940,0x0020,0x002E,0x0915,0x0947,0x0020, 390 0x002E,0x092F,0x094B,0x0917,0x0926,0x093E,0x0928,0x0020,0x002E,0x0915,0x0947,0x0020,0x002E,0x092B,0x0932,0x0938, 391 0x094D,0x0935,0x0930,0x0942,0x092A,0x0020,0x002E,0x0935,0x093F,0x0936,0x094D,0x0935,0x0020,0x002E,0x092E,0x0947, 392 0x0902,0x0020,0x002E,0x0938,0x093E,0x0932,0x093E,0x0928,0x093E,0x0020,0x002E,0x0032,0x0032,0x0030,0x0030,0x0020, 393 0x0905,0x0930,0x092C,0x0020,0x0930,0x0941,0x092A,0x092F,0x0947,0x0020,0x092E,0x0942,0x0932,0x094D,0x092F,0x0915, 394 0x0940,0x0020,0x002E,0x0034,0x0935,0x0938,0x094D,0x0924,0x0941,0x0913,0x0902,0x0020,0x002E,0x0034,0x0915,0x093E, 395 0x0020,0x002E,0x0034,0x0909,0x0924,0x094D,0x092A,0x093E,0x0926,0x0928,0x0020,0x002E,0x0034,0x0939,0x094B,0x0917, 396 0x093E,0x002C,0x0020,0x002E,0x0033,0x091C,0x092C,0x0915,0x093F,0x0020,0x002E,0x0033,0x0915,0x0902,0x092A,0x094D, 397 0x092F,0x0942,0x091F,0x0930,0x0020,0x002E,0x0033,0x0915,0x093E,0x0020,0x002E,0x0033,0x0915,0x0941,0x0932,0x0020, 398 0x002E,0x0033,0x092F,0x094B,0x0917,0x0926,0x093E,0x0928,0x0020,0x002E,0x0033,0x0907,0x0938,0x0938,0x0947,0x0915, 399 0x0939,0x093F,0x0020,0x002E,0x002F,0x091C,0x094D,0x092F,0x093E,0x0926,0x093E,0x0020,0x002E,0x002F,0x0939,0x094B, 400 0x0917,0x093E,0x0964,0x0020,0x002E,0x002F,0x0905,0x0928,0x0941,0x0938,0x0902,0x0927,0x093E,0x0928,0x0020,0x002E, 401 0x002F,0x0915,0x0940,0x0020,0x002E,0x002F,0x091A,0x0930,0x092E,0x0020,0x0938,0x0940,0x092E,0x093E,0x0913,0x0902, 402 0x0020,0x092A,0x0930,0x0020,0x092A,0x0939,0x0941,0x0902,0x091A,0x0928,0x0947,0x0020,0x0915,0x0947,0x0020,0x0932, 403 0x093F,0x090F,0x0020,0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x090F,0x0915,0x0020,0x002E,0x002F, 404 0x0906,0x092E,0x0020,0x002E,0x002F,0x091C,0x0930,0x0942,0x0930,0x0924,0x0020,0x002E,0x002F,0x091C,0x0948,0x0938, 405 0x093E,0x0020,0x092C,0x0928,0x0020,0x0917,0x092F,0x093E,0x0020,0x0939,0x0948,0x0964,0x0020,0x092D,0x093E,0x0930, 406 0x0924,0x0020,0x092E,0x0947,0x0902,0x0020,0x092D,0x0940,0x002C,0x0020,0x0916,0x093E,0x0938,0x0915,0x0930,0x0020, 407 0x092E,0x094C,0x091C,0x0942,0x0926,0x093E,0x0020,0x0938,0x0930,0x0915,0x093E,0x0930,0x0928,0x0947,0x002C,0x0020, 408 0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x0020,0x0915,0x0947,0x0020,0x092A,0x094D,0x0930,0x092F, 409 0x094B,0x0917,0x0020,0x092A,0x0930,0x0020,0x091C,0x092C,0x0930,0x0926,0x0938,0x094D,0x0924,0x0020,0x090F,0x095C, 410 0x0020,0x0932,0x0917,0x093E,0x092F,0x0940,0x0020,0x0939,0x0948,0x002C,0x0020,0x0915,0x093F,0x0902,0x0924,0x0941, 411 0x0020,0x0907,0x0938,0x0915,0x0947,0x0020,0x0938,0x0930,0x092A,0x091F,0x0020,0x0926,0x094C,0x095C,0x0932,0x0917, 412 0x093E,0x0928,0x0947,0x0020,0x002E,0x0032,0x0915,0x0947,0x0020,0x002E,0x0032,0x0932,0x093F,0x090F,0x0020,0x002E, 413 0x0032,0x0915,0x094D,0x092F,0x093E,0x0020,0x002E,0x0032,0x0938,0x092A,0x093E,0x091F,0x0020,0x002E,0x0032,0x0930, 414 0x093E,0x0938,0x094D,0x0924,0x093E,0x0020,0x002E,0x0032,0x0909,0x092A,0x0932,0x092C,0x094D,0x0927,0x0020,0x002E, 415 0x0939,0x0948,0x002C,0x0020,0x002E,0x0905,0x0925,0x0935,0x093E,0x0020,0x002E,0x0935,0x093F,0x0936,0x094D,0x0935, 416 0x0020,0x002E,0x092E,0x0947,0x0902,0x0020,0x002E,0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x0020, 417 0x002E,0x0915,0x0940,0x0938,0x092B,0x0932,0x0924,0x093E,0x0020,0x002E,0x0033,0x0935,0x0020,0x002E,0x0033,0x0935, 418 0x093F,0x092B,0x0932,0x0924,0x093E,0x0020,0x002E,0x0033,0x0938,0x0947,0x0020,0x002E,0x0033,0x0938,0x092C,0x0915, 419 0x0020,0x002E,0x0033,0x0932,0x0947,0x0020,0x002E,0x0033,0x0915,0x0930,0x0020,0x002E,0x0033,0x0915,0x094D,0x092F, 420 0x093E,0x0020,0x002E,0x0033,0x0939,0x092E,0x0020,0x002E,0x0033,0x0907,0x0938,0x0915,0x093E,0x0020,0x002E,0x0033, 421 0x092F,0x0941,0x0915,0x094D,0x0924,0x093F,0x092A,0x0942,0x0930,0x094D,0x0923,0x0020,0x002E,0x0032,0x0935,0x093F, 422 0x0938,0x094D,0x0924,0x093E,0x0930,0x0020,0x0905,0x092A,0x0947,0x0915,0x094D,0x0937,0x093F,0x0924,0x0020,0x0915, 423 0x0930,0x0020,0x0938,0x0915,0x0947,0x0902,0x0917,0x0947,0x0020,0x003F,0x0020, 424 0 425 }; 426 427 /* pre-flight */ 428 int32_t num =0; 429 const char *testdatapath = loadTestData(&error); 430 resB = ures_open(testdatapath, "iscii", &error); 431 srcFromRes=tres_getString(resB,-1,"str",&len,&error); 432 if(U_FAILURE(error)){ 433 #if UCONFIG_NO_LEGACY_CONVERSION 434 log_info("Couldn't load iscii.bin from test data bundle, (because UCONFIG_NO_LEGACY_CONVERSION is turned on)\n"); 435 #else 436 log_data_err("Could not find iscii.bin from test data bundle. Error: %s\n", u_errorName(error)); 437 #endif 438 ures_close(resB); 439 return; 440 } 441 if(u_strncmp(srcFromRes,uSrc,len)!=0){ 442 log_err("Genrb produced res files after decoding failed\n"); 443 } 444 while(num<len){ 445 if(uSrc[num]!=srcFromRes[num]){ 446 log_verbose(" Expected: 0x%04X Got: 0x%04X \n", uSrc[num],srcFromRes[num]); 447 } 448 num++; 449 } 450 if (len != u_strlen(uSrc)) { 451 log_err("Genrb produced a string larger than expected\n"); 452 } 453 ures_close(resB); 454 } 455 456 static void TestNewTypes() { 457 UResourceBundle* theBundle = NULL; 458 char action[256]; 459 const char* testdatapath; 460 UErrorCode status = U_ZERO_ERROR; 461 UResourceBundle* res = NULL; 462 uint8_t *binResult = NULL; 463 int32_t len = 0; 464 int32_t i = 0; 465 int32_t intResult = 0; 466 uint32_t uintResult = 0; 467 const UChar *empty = NULL; 468 const UChar *zeroString; 469 UChar expected[] = { 'a','b','c','\0','d','e','f' }; 470 const char* expect ="tab:\t cr:\r ff:\f newline:\n backslash:\\\\ quote=\\\' doubleQuote=\\\" singlequoutes=''"; 471 UChar uExpect[200]; 472 473 testdatapath=loadTestData(&status); 474 475 if(U_FAILURE(status)) 476 { 477 log_data_err("Could not load testdata.dat %s \n",myErrorName(status)); 478 return; 479 } 480 481 theBundle = ures_open(testdatapath, "testtypes", &status); 482 483 empty = tres_getString(theBundle, -1, "emptystring", &len, &status); 484 if(empty && (*empty != 0 || len != 0)) { 485 log_err("Empty string returned invalid value\n"); 486 } 487 488 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 489 490 CONFIRM_INT_NE(theBundle, NULL); 491 492 /* This test reads the string "abc\u0000def" from the bundle */ 493 /* if everything is working correctly, the size of this string */ 494 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/ 495 496 strcpy(action, "getting and testing of string with embeded zero"); 497 res = ures_getByKey(theBundle, "zerotest", res, &status); 498 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 499 CONFIRM_INT_EQ(ures_getType(res), URES_STRING); 500 zeroString=tres_getString(res, -1, NULL, &len, &status); 501 if(U_SUCCESS(status)){ 502 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 503 CONFIRM_INT_EQ(len, 7); 504 CONFIRM_INT_NE(len, 3); 505 } 506 for(i=0;i<len;i++){ 507 if(zeroString[i]!= expected[i]){ 508 log_verbose("Output did not match Expected: \\u%4X Got: \\u%4X", expected[i], zeroString[i]); 509 } 510 } 511 512 strcpy(action, "getting and testing of binary type"); 513 res = ures_getByKey(theBundle, "binarytest", res, &status); 514 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 515 CONFIRM_INT_EQ(ures_getType(res), URES_BINARY); 516 binResult=(uint8_t*)ures_getBinary(res, &len, &status); 517 if(U_SUCCESS(status)){ 518 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 519 CONFIRM_INT_EQ(len, 15); 520 for(i = 0; i<15; i++) { 521 CONFIRM_INT_EQ(binResult[i], i); 522 } 523 } 524 525 strcpy(action, "getting and testing of imported binary type"); 526 res = ures_getByKey(theBundle, "importtest", res, &status); 527 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 528 CONFIRM_INT_EQ(ures_getType(res), URES_BINARY); 529 binResult=(uint8_t*)ures_getBinary(res, &len, &status); 530 if(U_SUCCESS(status)){ 531 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 532 CONFIRM_INT_EQ(len, 15); 533 for(i = 0; i<15; i++) { 534 CONFIRM_INT_EQ(binResult[i], i); 535 } 536 } 537 538 strcpy(action, "getting and testing of integer types"); 539 res = ures_getByKey(theBundle, "one", res, &status); 540 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 541 CONFIRM_INT_EQ(ures_getType(res), URES_INT); 542 intResult=ures_getInt(res, &status); 543 uintResult = ures_getUInt(res, &status); 544 if(U_SUCCESS(status)){ 545 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 546 CONFIRM_INT_EQ(uintResult, (uint32_t)intResult); 547 CONFIRM_INT_EQ(intResult, 1); 548 } 549 550 strcpy(action, "getting minusone"); 551 res = ures_getByKey(theBundle, "minusone", res, &status); 552 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 553 CONFIRM_INT_EQ(ures_getType(res), URES_INT); 554 intResult=ures_getInt(res, &status); 555 uintResult = ures_getUInt(res, &status); 556 if(U_SUCCESS(status)){ 557 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 558 CONFIRM_INT_EQ(uintResult, 0x0FFFFFFF); /* a 28 bit integer */ 559 CONFIRM_INT_EQ(intResult, -1); 560 CONFIRM_INT_NE(uintResult, (uint32_t)intResult); 561 } 562 563 strcpy(action, "getting plusone"); 564 res = ures_getByKey(theBundle, "plusone", res, &status); 565 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 566 CONFIRM_INT_EQ(ures_getType(res), URES_INT); 567 intResult=ures_getInt(res, &status); 568 uintResult = ures_getUInt(res, &status); 569 if(U_SUCCESS(status)){ 570 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 571 CONFIRM_INT_EQ(uintResult, (uint32_t)intResult); 572 CONFIRM_INT_EQ(intResult, 1); 573 } 574 575 res = ures_getByKey(theBundle, "onehundredtwentythree", res, &status); 576 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 577 CONFIRM_INT_EQ(ures_getType(res), URES_INT); 578 intResult=ures_getInt(res, &status); 579 if(U_SUCCESS(status)){ 580 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 581 CONFIRM_INT_EQ(intResult, 123); 582 } 583 584 /* this tests if escapes are preserved or not */ 585 { 586 const UChar* str = tres_getString(theBundle,-1,"testescape",&len,&status); 587 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 588 if(U_SUCCESS(status)){ 589 u_charsToUChars(expect,uExpect,(int32_t)strlen(expect)+1); 590 if(u_strcmp(uExpect,str)){ 591 log_err("Did not get the expected string for testescape\n"); 592 } 593 } 594 } 595 /* this tests if unescaping works are expected */ 596 len=0; 597 { 598 char pattern[2048] = ""; 599 int32_t patternLen; 600 UChar* expectedEscaped; 601 const UChar* got; 602 int32_t expectedLen; 603 604 /* This strcpy fixes compiler warnings about long strings */ 605 strcpy(pattern, "[ \\\\u0020 \\\\u00A0 \\\\u1680 \\\\u2000 \\\\u2001 \\\\u2002 \\\\u2003 \\\\u2004 \\\\u2005 \\\\u2006 \\\\u2007 " 606 "\\\\u2008 \\\\u2009 \\\\u200A \\u200B \\\\u202F \\u205F \\\\u3000 \\u0000-\\u001F \\u007F \\u0080-\\u009F " 607 "\\\\u06DD \\\\u070F \\\\u180E \\\\u200C \\\\u200D \\\\u2028 \\\\u2029 \\\\u2060 \\\\u2061 \\\\u2062 \\\\u2063 " 608 "\\\\u206A-\\\\u206F \\\\uFEFF \\\\uFFF9-\\uFFFC \\U0001D173-\\U0001D17A \\U000F0000-\\U000FFFFD " 609 "\\U00100000-\\U0010FFFD \\uFDD0-\\uFDEF \\uFFFE-\\uFFFF \\U0001FFFE-\\U0001FFFF \\U0002FFFE-\\U0002FFFF " 610 ); 611 strcat(pattern, 612 "\\U0003FFFE-\\U0003FFFF \\U0004FFFE-\\U0004FFFF \\U0005FFFE-\\U0005FFFF \\U0006FFFE-\\U0006FFFF " 613 "\\U0007FFFE-\\U0007FFFF \\U0008FFFE-\\U0008FFFF \\U0009FFFE-\\U0009FFFF \\U000AFFFE-\\U000AFFFF " 614 "\\U000BFFFE-\\U000BFFFF \\U000CFFFE-\\U000CFFFF \\U000DFFFE-\\U000DFFFF \\U000EFFFE-\\U000EFFFF " 615 "\\U000FFFFE-\\U000FFFFF \\U0010FFFE-\\U0010FFFF \\uD800-\\uDFFF \\\\uFFF9 \\\\uFFFA \\\\uFFFB " 616 "\\uFFFC \\uFFFD \\u2FF0-\\u2FFB \\u0340 \\u0341 \\\\u200E \\\\u200F \\\\u202A \\\\u202B \\\\u202C " 617 ); 618 strcat(pattern, 619 "\\\\u202D \\\\u202E \\\\u206A \\\\u206B \\\\u206C \\\\u206D \\\\u206E \\\\u206F \\U000E0001 \\U000E0020-\\U000E007F " 620 "]" 621 ); 622 623 patternLen = (int32_t)uprv_strlen(pattern); 624 expectedEscaped = (UChar*)malloc(U_SIZEOF_UCHAR * patternLen); 625 got = tres_getString(theBundle,-1,"test_unescaping",&len,&status); 626 expectedLen = u_unescape(pattern,expectedEscaped,patternLen); 627 if(got==NULL || u_strncmp(expectedEscaped,got,expectedLen)!=0 || expectedLen != len){ 628 log_err("genrb failed to unescape string\n"); 629 } 630 if(got != NULL){ 631 for(i=0;i<expectedLen;i++){ 632 if(expectedEscaped[i] != got[i]){ 633 log_verbose("Expected: 0x%04X Got: 0x%04X \n",expectedEscaped[i], got[i]); 634 } 635 } 636 } 637 free(expectedEscaped); 638 status = U_ZERO_ERROR; 639 } 640 /* test for jitterbug#1435 */ 641 { 642 const UChar* str = tres_getString(theBundle,-1,"test_underscores",&len,&status); 643 expect ="test message ...."; 644 u_charsToUChars(expect,uExpect,(int32_t)strlen(expect)+1); 645 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 646 if(str == NULL || u_strcmp(uExpect,str)){ 647 log_err("Did not get the expected string for test_underscores.\n"); 648 } 649 } 650 /* test for jitterbug#2626 */ 651 #if !UCONFIG_NO_COLLATION 652 { 653 UResourceBundle* resB = NULL; 654 const UChar* str = NULL; 655 int32_t strLength = 0; 656 const UChar my[] = {0x0026,0x0027,0x0075,0x0027,0x0020,0x003d,0x0020,0x0027,0xff55,0x0027,0x0000}; /* &'\u0075' = '\uFF55' */ 657 status = U_ZERO_ERROR; 658 resB = ures_getByKey(theBundle, "collations", resB, &status); 659 resB = ures_getByKey(resB, "standard", resB, &status); 660 str = tres_getString(resB,-1,"Sequence",&strLength,&status); 661 if(!str || U_FAILURE(status)) { 662 log_data_err("Could not load collations from theBundle: %s\n", u_errorName(status)); 663 } else if(u_strcmp(my,str) != 0){ 664 log_err("Did not get the expected string for escaped \\u0075\n"); 665 } 666 ures_close(resB); 667 } 668 #endif 669 { 670 const char *sourcePath = ctest_dataSrcDir(); 671 int32_t srcPathLen = (int32_t)strlen(sourcePath); 672 const char *deltaPath = ".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING; 673 int32_t deltaPathLen = (int32_t)strlen(deltaPath); 674 char *testDataFileName = (char *) malloc( srcPathLen+ deltaPathLen + 50 ); 675 char *path = testDataFileName; 676 677 strcpy(path, sourcePath); 678 path += srcPathLen; 679 strcpy(path, deltaPath); 680 path += deltaPathLen; 681 status = U_ZERO_ERROR; 682 { 683 int32_t strLen =0; 684 const UChar* str = tres_getString(theBundle, -1, "testincludeUTF",&strLen,&status); 685 strcpy(path, "riwords.txt"); 686 path[strlen("riwords.txt")]=0; 687 if(U_FAILURE(status)){ 688 log_err("Could not get testincludeUTF resource from testtypes bundle. Error: %s\n",u_errorName(status)); 689 }else{ 690 /* open the file */ 691 const char* cp = NULL; 692 UCHARBUF* ucbuf = ucbuf_open(testDataFileName,&cp,FALSE,FALSE,&status); 693 len = 0; 694 if(U_SUCCESS(status)){ 695 const UChar* buffer = ucbuf_getBuffer(ucbuf,&len,&status); 696 if(U_SUCCESS(status)){ 697 /* verify the contents */ 698 if(strLen != len ){ 699 log_err("Did not get the expected len for riwords. Expected: %i , Got: %i\n", len ,strLen); 700 } 701 /* test string termination */ 702 if(u_strlen(str) != strLen || str[strLen]!= 0 ){ 703 log_err("testinclude not null terminated!\n"); 704 } 705 if(u_strncmp(str, buffer,strLen)!=0){ 706 log_err("Did not get the expected string from riwords. Include functionality failed for genrb.\n"); 707 } 708 }else{ 709 log_err("ucbuf failed to open %s. Error: %s\n", testDataFileName, u_errorName(status)); 710 } 711 712 ucbuf_close(ucbuf); 713 }else{ 714 log_err("Could not get riwords.txt (path : %s). Error: %s\n",testDataFileName,u_errorName(status)); 715 } 716 } 717 } 718 status = U_ZERO_ERROR; 719 { 720 int32_t strLen =0; 721 const UChar* str = tres_getString(theBundle, -1, "testinclude",&strLen,&status); 722 strcpy(path, "translit_rules.txt"); 723 path[strlen("translit_rules.txt")]=0; 724 725 if(U_FAILURE(status)){ 726 log_err("Could not get testinclude resource from testtypes bundle. Error: %s\n",u_errorName(status)); 727 }else{ 728 /* open the file */ 729 const char* cp=NULL; 730 UCHARBUF* ucbuf = ucbuf_open(testDataFileName,&cp,FALSE,FALSE,&status); 731 len = 0; 732 if(U_SUCCESS(status)){ 733 const UChar* buffer = ucbuf_getBuffer(ucbuf,&len,&status); 734 if(U_SUCCESS(status)){ 735 /* verify the contents */ 736 if(strLen != len ){ 737 log_err("Did not get the expected len for translit_rules. Expected: %i , Got: %i\n", len ,strLen); 738 } 739 if(u_strncmp(str, buffer,strLen)!=0){ 740 log_err("Did not get the expected string from translit_rules. Include functionality failed for genrb.\n"); 741 } 742 }else{ 743 log_err("ucbuf failed to open %s. Error: %s\n", testDataFileName, u_errorName(status)); 744 } 745 ucbuf_close(ucbuf); 746 }else{ 747 log_err("Could not get translit_rules.txt (path : %s). Error: %s\n",testDataFileName,u_errorName(status)); 748 } 749 } 750 } 751 free(testDataFileName); 752 } 753 ures_close(res); 754 ures_close(theBundle); 755 756 } 757 758 static void TestEmptyTypes() { 759 UResourceBundle* theBundle = NULL; 760 char action[256]; 761 const char* testdatapath; 762 UErrorCode status = U_ZERO_ERROR; 763 UResourceBundle* res = NULL; 764 UResourceBundle* resArray = NULL; 765 const uint8_t *binResult = NULL; 766 int32_t len = 0; 767 int32_t intResult = 0; 768 const UChar *zeroString; 769 const int32_t *zeroIntVect; 770 771 strcpy(action, "Construction of testtypes bundle"); 772 testdatapath=loadTestData(&status); 773 if(U_FAILURE(status)) 774 { 775 log_data_err("Could not load testdata.dat %s \n",myErrorName(status)); 776 return; 777 } 778 779 theBundle = ures_open(testdatapath, "testtypes", &status); 780 781 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 782 783 CONFIRM_INT_NE(theBundle, NULL); 784 785 /* This test reads the string "abc\u0000def" from the bundle */ 786 /* if everything is working correctly, the size of this string */ 787 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/ 788 789 status = U_ZERO_ERROR; 790 strcpy(action, "getting and testing of explicit string of zero length string"); 791 res = ures_getByKey(theBundle, "emptyexplicitstring", res, &status); 792 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 793 CONFIRM_INT_EQ(ures_getType(res), URES_STRING); 794 zeroString=tres_getString(res, -1, NULL, &len, &status); 795 if(U_SUCCESS(status)){ 796 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 797 CONFIRM_INT_EQ(len, 0); 798 CONFIRM_INT_EQ(u_strlen(zeroString), 0); 799 } 800 else { 801 log_err("Couldn't get emptyexplicitstring\n"); 802 } 803 804 status = U_ZERO_ERROR; 805 strcpy(action, "getting and testing of normal string of zero length string"); 806 res = ures_getByKey(theBundle, "emptystring", res, &status); 807 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 808 CONFIRM_INT_EQ(ures_getType(res), URES_STRING); 809 zeroString=tres_getString(res, -1, NULL, &len, &status); 810 if(U_SUCCESS(status)){ 811 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 812 CONFIRM_INT_EQ(len, 0); 813 CONFIRM_INT_EQ(u_strlen(zeroString), 0); 814 } 815 else { 816 log_err("Couldn't get emptystring\n"); 817 } 818 819 status = U_ZERO_ERROR; 820 strcpy(action, "getting and testing of empty int"); 821 res = ures_getByKey(theBundle, "emptyint", res, &status); 822 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 823 CONFIRM_INT_EQ(ures_getType(res), URES_INT); 824 intResult=ures_getInt(res, &status); 825 if(U_SUCCESS(status)){ 826 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 827 CONFIRM_INT_EQ(intResult, 0); 828 } 829 else { 830 log_err("Couldn't get emptystring\n"); 831 } 832 833 status = U_ZERO_ERROR; 834 strcpy(action, "getting and testing of zero length intvector"); 835 res = ures_getByKey(theBundle, "emptyintv", res, &status); 836 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 837 CONFIRM_INT_EQ(ures_getType(res), URES_INT_VECTOR); 838 839 if(U_FAILURE(status)){ 840 log_err("Couldn't get emptyintv key %s\n", u_errorName(status)); 841 } 842 else { 843 zeroIntVect=ures_getIntVector(res, &len, &status); 844 if(!U_SUCCESS(status) || resArray != NULL || len != 0) { 845 log_err("Shouldn't get emptyintv\n"); 846 } 847 } 848 849 status = U_ZERO_ERROR; 850 strcpy(action, "getting and testing of zero length emptybin"); 851 res = ures_getByKey(theBundle, "emptybin", res, &status); 852 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 853 CONFIRM_INT_EQ(ures_getType(res), URES_BINARY); 854 855 if(U_FAILURE(status)){ 856 log_err("Couldn't get emptybin key %s\n", u_errorName(status)); 857 } 858 else { 859 binResult=ures_getBinary(res, &len, &status); 860 if(!U_SUCCESS(status) || len != 0) { 861 log_err("Couldn't get emptybin, or it's not empty\n"); 862 } 863 } 864 865 status = U_ZERO_ERROR; 866 strcpy(action, "getting and testing of zero length emptyarray"); 867 res = ures_getByKey(theBundle, "emptyarray", res, &status); 868 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 869 CONFIRM_INT_EQ(ures_getType(res), URES_ARRAY); 870 871 if(U_FAILURE(status)){ 872 log_err("Couldn't get emptyarray key %s\n", u_errorName(status)); 873 } 874 else { 875 resArray=ures_getByIndex(res, 0, resArray, &status); 876 if(U_SUCCESS(status) || resArray != NULL){ 877 log_err("Shouldn't get emptyarray[0]\n"); 878 } 879 } 880 881 status = U_ZERO_ERROR; 882 strcpy(action, "getting and testing of zero length emptytable"); 883 res = ures_getByKey(theBundle, "emptytable", res, &status); 884 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 885 CONFIRM_INT_EQ(ures_getType(res), URES_TABLE); 886 887 if(U_FAILURE(status)){ 888 log_err("Couldn't get emptytable key %s\n", u_errorName(status)); 889 } 890 else { 891 resArray=ures_getByIndex(res, 0, resArray, &status); 892 if(U_SUCCESS(status) || resArray != NULL){ 893 log_err("Shouldn't get emptytable[0]\n"); 894 } 895 } 896 897 ures_close(res); 898 ures_close(theBundle); 899 } 900 901 static void TestEmptyBundle(){ 902 UErrorCode status = U_ZERO_ERROR; 903 const char* testdatapath=NULL; 904 UResourceBundle *resb=0, *dResB=0; 905 906 testdatapath=loadTestData(&status); 907 if(U_FAILURE(status)) 908 { 909 log_data_err("Could not load testdata.dat %s \n",myErrorName(status)); 910 return; 911 } 912 resb = ures_open(testdatapath, "testempty", &status); 913 914 if(U_SUCCESS(status)){ 915 dResB = ures_getByKey(resb,"test",dResB,&status); 916 if(status!= U_MISSING_RESOURCE_ERROR){ 917 log_err("Did not get the expected error from an empty resource bundle. Expected : %s Got: %s\n", 918 u_errorName(U_MISSING_RESOURCE_ERROR),u_errorName(status)); 919 } 920 } 921 ures_close(dResB); 922 ures_close(resb); 923 } 924 925 static void TestBinaryCollationData(){ 926 UErrorCode status=U_ZERO_ERROR; 927 const char* locale="te"; 928 #if !UCONFIG_NO_COLLATION 929 const char* testdatapath; 930 #endif 931 UResourceBundle *teRes = NULL; 932 UResourceBundle *coll=NULL; 933 UResourceBundle *binColl = NULL; 934 uint8_t *binResult = NULL; 935 int32_t len=0; 936 const char* action="testing the binary collaton data"; 937 938 #if !UCONFIG_NO_COLLATION 939 log_verbose("Testing binary collation data resource......\n"); 940 941 testdatapath=loadTestData(&status); 942 if(U_FAILURE(status)) 943 { 944 log_data_err("Could not load testdata.dat %s \n",myErrorName(status)); 945 return; 946 } 947 948 949 teRes=ures_open(testdatapath, locale, &status); 950 if(U_FAILURE(status)){ 951 log_err("ERROR: Failed to get resource for \"te\" with %s", myErrorName(status)); 952 return; 953 } 954 status=U_ZERO_ERROR; 955 coll = ures_getByKey(teRes, "collations", coll, &status); 956 coll = ures_getByKey(coll, "standard", coll, &status); 957 if(U_SUCCESS(status)){ 958 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 959 CONFIRM_INT_EQ(ures_getType(coll), URES_TABLE); 960 binColl=ures_getByKey(coll, "%%CollationBin", binColl, &status); 961 if(U_SUCCESS(status)){ 962 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 963 CONFIRM_INT_EQ(ures_getType(binColl), URES_BINARY); 964 binResult=(uint8_t*)ures_getBinary(binColl, &len, &status); 965 if(U_SUCCESS(status)){ 966 CONFIRM_ErrorCode(status, U_ZERO_ERROR); 967 CONFIRM_INT_GE(len, 1); 968 } 969 970 }else{ 971 log_err("ERROR: ures_getByKey(locale(te), %%CollationBin) failed\n"); 972 } 973 } 974 else{ 975 log_err("ERROR: ures_getByKey(locale(te), collations) failed\n"); 976 return; 977 } 978 ures_close(binColl); 979 ures_close(coll); 980 ures_close(teRes); 981 #endif 982 } 983 984 static void TestAPI() { 985 UErrorCode status=U_ZERO_ERROR; 986 int32_t len=0; 987 const char* key=NULL; 988 const UChar* value=NULL; 989 const char* testdatapath; 990 UChar* utestdatapath=NULL; 991 char convOutput[256]; 992 UChar largeBuffer[1025]; 993 UResourceBundle *teRes = NULL; 994 UResourceBundle *teFillin=NULL; 995 UResourceBundle *teFillin2=NULL; 996 997 log_verbose("Testing ures_openU()......\n"); 998 999 testdatapath=loadTestData(&status); 1000 if(U_FAILURE(status)) 1001 { 1002 log_data_err("Could not load testdata.dat %s \n",myErrorName(status)); 1003 return; 1004 } 1005 len =(int32_t)strlen(testdatapath); 1006 utestdatapath = (UChar*) malloc((len+10)*sizeof(UChar)); 1007 1008 u_charsToUChars(testdatapath, utestdatapath, (int32_t)strlen(testdatapath)+1); 1009 #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) && U_FILE_SEP_CHAR == '\\' 1010 { 1011 /* Convert all backslashes to forward slashes so that we can make sure that ures_openU 1012 can handle invariant characters. */ 1013 UChar *backslash; 1014 while ((backslash = u_strchr(utestdatapath, 0x005C))) { 1015 *backslash = 0x002F; 1016 } 1017 } 1018 #endif 1019 1020 u_memset(largeBuffer, 0x0030, sizeof(largeBuffer)/sizeof(largeBuffer[0])); 1021 largeBuffer[sizeof(largeBuffer)/sizeof(largeBuffer[0])-1] = 0; 1022 1023 /*Test ures_openU */ 1024 1025 status = U_ZERO_ERROR; 1026 ures_close(ures_openU(largeBuffer, "root", &status)); 1027 if(status != U_ILLEGAL_ARGUMENT_ERROR){ 1028 log_err("ERROR: ures_openU() worked when the path is very large. It returned %s\n", myErrorName(status)); 1029 } 1030 1031 status = U_ZERO_ERROR; 1032 ures_close(ures_openU(NULL, "root", &status)); 1033 if(U_FAILURE(status)){ 1034 log_err_status(status, "ERROR: ures_openU() failed path = NULL with %s\n", myErrorName(status)); 1035 } 1036 1037 status = U_ILLEGAL_ARGUMENT_ERROR; 1038 if(ures_openU(NULL, "root", &status) != NULL){ 1039 log_err("ERROR: ures_openU() worked with error status with %s\n", myErrorName(status)); 1040 } 1041 1042 status = U_ZERO_ERROR; 1043 teRes=ures_openU(utestdatapath, "te", &status); 1044 if(U_FAILURE(status)){ 1045 log_err_status(status, "ERROR: ures_openU() failed path =%s with %s\n", austrdup(utestdatapath), myErrorName(status)); 1046 return; 1047 } 1048 /*Test ures_getLocale() */ 1049 log_verbose("Testing ures_getLocale() .....\n"); 1050 if(strcmp(ures_getLocale(teRes, &status), "te") != 0){ 1051 log_err("ERROR: ures_getLocale() failed. Expected = te_TE Got = %s\n", ures_getLocale(teRes, &status)); 1052 } 1053 /*Test ures_getNextString() */ 1054 teFillin=ures_getByKey(teRes, "tagged_array_in_te_te_IN", teFillin, &status); 1055 key=ures_getKey(teFillin); 1056 value=(UChar*)ures_getNextString(teFillin, &len, &key, &status); 1057 ures_resetIterator(NULL); 1058 value=(UChar*)ures_getNextString(teFillin, &len, &key, &status); 1059 if(status !=U_INDEX_OUTOFBOUNDS_ERROR){ 1060 log_err("ERROR: calling getNextString where index out of bounds should return U_INDEX_OUTOFBOUNDS_ERROR, Got : %s\n", 1061 myErrorName(status)); 1062 } 1063 ures_resetIterator(teRes); 1064 /*Test ures_getNextResource() where resource is table*/ 1065 status=U_ZERO_ERROR; 1066 #if (U_CHARSET_FAMILY == U_ASCII_FAMILY) 1067 /* The next key varies depending on the charset. */ 1068 teFillin=ures_getNextResource(teRes, teFillin, &status); 1069 if(U_FAILURE(status)){ 1070 log_err("ERROR: ures_getNextResource() failed \n"); 1071 } 1072 key=ures_getKey(teFillin); 1073 /*if(strcmp(key, "%%CollationBin") != 0){*/ 1074 /*if(strcmp(key, "array_2d_in_Root_te") != 0){*/ /* added "aliasClient" that goes first */ 1075 if(strcmp(key, "a") != 0){ 1076 log_err("ERROR: ures_getNextResource() failed\n"); 1077 } 1078 #endif 1079 1080 /*Test ures_getByIndex on string Resource*/ 1081 teFillin=ures_getByKey(teRes, "string_only_in_te", teFillin, &status); 1082 teFillin2=ures_getByIndex(teFillin, 0, teFillin2, &status); 1083 if(U_FAILURE(status)){ 1084 log_err("ERROR: ures_getByIndex on string resource failed\n"); 1085 } 1086 if(strcmp(u_austrcpy(convOutput, tres_getString(teFillin2, -1, NULL, &len, &status)), "TE") != 0){ 1087 status=U_ZERO_ERROR; 1088 log_err("ERROR: ures_getByIndex on string resource fetched the key=%s, expected \"TE\" \n", austrdup(ures_getString(teFillin2, &len, &status))); 1089 } 1090 1091 /*ures_close(teRes);*/ 1092 1093 /*Test ures_openFillIn*/ 1094 log_verbose("Testing ures_openFillIn......\n"); 1095 status=U_ZERO_ERROR; 1096 ures_openFillIn(teRes, testdatapath, "te", &status); 1097 if(U_FAILURE(status)){ 1098 log_err("ERROR: ures_openFillIn failed\n"); 1099 return; 1100 } 1101 if(strcmp(ures_getLocale(teRes, &status), "te") != 0){ 1102 log_err("ERROR: ures_openFillIn did not open the ResourceBundle correctly\n"); 1103 } 1104 ures_getByKey(teRes, "string_only_in_te", teFillin, &status); 1105 teFillin2=ures_getNextResource(teFillin, teFillin2, &status); 1106 if(ures_getType(teFillin2) != URES_STRING){ 1107 log_err("ERROR: getType for getNextResource after ures_openFillIn failed\n"); 1108 } 1109 teFillin2=ures_getNextResource(teFillin, teFillin2, &status); 1110 if(status !=U_INDEX_OUTOFBOUNDS_ERROR){ 1111 log_err("ERROR: calling getNextResource where index out of bounds should return U_INDEX_OUTOFBOUNDS_ERROR, Got : %s\n", 1112 myErrorName(status)); 1113 } 1114 1115 ures_close(teFillin); 1116 ures_close(teFillin2); 1117 ures_close(teRes); 1118 1119 /* Test that ures_getLocale() returns the "real" locale ID */ 1120 status=U_ZERO_ERROR; 1121 teRes=ures_open(NULL, "dE_At_NOWHERE_TO_BE_FOUND", &status); 1122 if(U_FAILURE(status)) { 1123 log_data_err("unable to open a locale resource bundle from \"dE_At_NOWHERE_TO_BE_FOUND\"(%s)\n", u_errorName(status)); 1124 } else { 1125 if(0!=strcmp("de_AT", ures_getLocale(teRes, &status))) { 1126 log_data_err("ures_getLocale(\"dE_At_NOWHERE_TO_BE_FOUND\")=%s but must be de_AT\n", ures_getLocale(teRes, &status)); 1127 } 1128 ures_close(teRes); 1129 } 1130 1131 /* same test, but with an aliased locale resource bundle */ 1132 status=U_ZERO_ERROR; 1133 teRes=ures_open(NULL, "iW_Il_depRecaTed_HebreW", &status); 1134 if(U_FAILURE(status)) { 1135 log_data_err("unable to open a locale resource bundle from \"iW_Il_depRecaTed_HebreW\"(%s)\n", u_errorName(status)); 1136 } else { 1137 if(0!=strcmp("he_IL", ures_getLocale(teRes, &status))) { 1138 log_data_err("ures_getLocale(\"iW_Il_depRecaTed_HebreW\")=%s but must be he_IL\n", ures_getLocale(teRes, &status)); 1139 } 1140 ures_close(teRes); 1141 } 1142 free(utestdatapath); 1143 } 1144 1145 static void TestErrorConditions(){ 1146 UErrorCode status=U_ZERO_ERROR; 1147 const char *key=NULL; 1148 const UChar *value=NULL; 1149 const char* testdatapath; 1150 UChar* utestdatapath; 1151 int32_t len=0; 1152 UResourceBundle *teRes = NULL; 1153 UResourceBundle *coll=NULL; 1154 UResourceBundle *binColl = NULL; 1155 UResourceBundle *teFillin=NULL; 1156 UResourceBundle *teFillin2=NULL; 1157 uint8_t *binResult = NULL; 1158 int32_t resultLen; 1159 1160 1161 testdatapath = loadTestData(&status); 1162 if(U_FAILURE(status)) 1163 { 1164 log_data_err("Could not load testdata.dat %s \n",myErrorName(status)); 1165 return; 1166 } 1167 len = (int32_t)strlen(testdatapath); 1168 utestdatapath = (UChar*) malloc(sizeof(UChar) *(len+10)); 1169 u_uastrcpy(utestdatapath, testdatapath); 1170 1171 /*Test ures_openU with status != U_ZERO_ERROR*/ 1172 log_verbose("Testing ures_openU() with status != U_ZERO_ERROR.....\n"); 1173 status=U_ILLEGAL_ARGUMENT_ERROR; 1174 teRes=ures_openU(utestdatapath, "te", &status); 1175 if(U_FAILURE(status)){ 1176 log_verbose("ures_openU() failed as expected path =%s with status != U_ZERO_ERROR\n", testdatapath); 1177 }else{ 1178 log_err("ERROR: ures_openU() is supposed to fail path =%s with status != U_ZERO_ERROR\n", austrdup(utestdatapath)); 1179 ures_close(teRes); 1180 } 1181 /*Test ures_openFillIn with UResourceBundle = NULL*/ 1182 log_verbose("Testing ures_openFillIn with UResourceBundle = NULL.....\n"); 1183 status=U_ZERO_ERROR; 1184 ures_openFillIn(NULL, testdatapath, "te", &status); 1185 if(status != U_ILLEGAL_ARGUMENT_ERROR){ 1186 log_err("ERROR: ures_openFillIn with UResourceBundle= NULL should fail. Expected U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n", 1187 myErrorName(status)); 1188 } 1189 /*Test ures_getLocale() with status != U_ZERO_ERROR*/ 1190 status=U_ZERO_ERROR; 1191 teRes=ures_openU(utestdatapath, "te", &status); 1192 if(U_FAILURE(status)){ 1193 log_err("ERROR: ures_openU() failed path =%s with %s\n", austrdup(utestdatapath), myErrorName(status)); 1194 return; 1195 } 1196 status=U_ILLEGAL_ARGUMENT_ERROR; 1197 if(ures_getLocale(teRes, &status) != NULL){ 1198 log_err("ERROR: ures_getLocale is supposed to fail with errorCode != U_ZERO_ERROR\n"); 1199 } 1200 /*Test ures_getLocale() with UResourceBundle = NULL*/ 1201 status=U_ZERO_ERROR; 1202 if(ures_getLocale(NULL, &status) != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){ 1203 log_err("ERROR: ures_getLocale is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n", 1204 myErrorName(status)); 1205 } 1206 /*Test ures_getSize() with UResourceBundle = NULL */ 1207 status=U_ZERO_ERROR; 1208 if(ures_getSize(NULL) != 0){ 1209 log_err("ERROR: ures_getSize() should return 0 when UResourceBundle=NULL. Got =%d\n", ures_getSize(NULL)); 1210 } 1211 /*Test ures_getType() with UResourceBundle = NULL should return URES_NONE==-1*/ 1212 status=U_ZERO_ERROR; 1213 if(ures_getType(NULL) != URES_NONE){ 1214 log_err("ERROR: ures_getType() should return URES_NONE when UResourceBundle=NULL. Got =%d\n", ures_getType(NULL)); 1215 } 1216 /*Test ures_getKey() with UResourceBundle = NULL*/ 1217 status=U_ZERO_ERROR; 1218 if(ures_getKey(NULL) != NULL){ 1219 log_err("ERROR: ures_getKey() should return NULL when UResourceBundle=NULL. Got =%d\n", ures_getKey(NULL)); 1220 } 1221 /*Test ures_hasNext() with UResourceBundle = NULL*/ 1222 status=U_ZERO_ERROR; 1223 if(ures_hasNext(NULL) != FALSE){ 1224 log_err("ERROR: ures_hasNext() should return FALSE when UResourceBundle=NULL. Got =%d\n", ures_hasNext(NULL)); 1225 } 1226 /*Test ures_get() with UResourceBundle = NULL*/ 1227 status=U_ZERO_ERROR; 1228 if(ures_getStringByKey(NULL, "string_only_in_te", &resultLen, &status) != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){ 1229 log_err("ERROR: ures_get is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n", 1230 myErrorName(status)); 1231 } 1232 /*Test ures_getByKey() with UResourceBundle = NULL*/ 1233 status=U_ZERO_ERROR; 1234 teFillin=ures_getByKey(NULL, "string_only_in_te", teFillin, &status); 1235 if( teFillin != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){ 1236 log_err("ERROR: ures_getByKey is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n", 1237 myErrorName(status)); 1238 } 1239 /*Test ures_getByKey() with status != U_ZERO_ERROR*/ 1240 teFillin=ures_getByKey(NULL, "string_only_in_te", teFillin, &status); 1241 if(teFillin != NULL ){ 1242 log_err("ERROR: ures_getByKey is supposed to fail when errorCode != U_ZERO_ERROR\n"); 1243 } 1244 /*Test ures_getStringByKey() with UResourceBundle = NULL*/ 1245 status=U_ZERO_ERROR; 1246 if(ures_getStringByKey(NULL, "string_only_in_te", &len, &status) != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){ 1247 log_err("ERROR: ures_getStringByKey is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n", 1248 myErrorName(status)); 1249 } 1250 /*Test ures_getStringByKey() with status != U_ZERO_ERROR*/ 1251 if(ures_getStringByKey(teRes, "string_only_in_te", &len, &status) != NULL){ 1252 log_err("ERROR: ures_getStringByKey is supposed to fail when status != U_ZERO_ERROR. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n", 1253 myErrorName(status)); 1254 } 1255 /*Test ures_getString() with UResourceBundle = NULL*/ 1256 status=U_ZERO_ERROR; 1257 if(ures_getString(NULL, &len, &status) != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){ 1258 log_err("ERROR: ures_getString is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n", 1259 myErrorName(status)); 1260 } 1261 /*Test ures_getString() with status != U_ZERO_ERROR*/ 1262 if(ures_getString(teRes, &len, &status) != NULL){ 1263 log_err("ERROR: ures_getString is supposed to fail when status != U_ZERO_ERROR. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n", 1264 myErrorName(status)); 1265 } 1266 /*Test ures_getBinary() with UResourceBundle = NULL*/ 1267 status=U_ZERO_ERROR; 1268 if(ures_getBinary(NULL, &len, &status) != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){ 1269 log_err("ERROR: ures_getBinary is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n", 1270 myErrorName(status)); 1271 } 1272 /*Test ures_getBinary(0 status != U_ILLEGAL_ARGUMENT_ERROR*/ 1273 status=U_ZERO_ERROR; 1274 coll = ures_getByKey(teRes, "collations", coll, &status); 1275 coll = ures_getByKey(teRes, "standard", coll, &status); 1276 binColl=ures_getByKey(coll, "%%CollationBin", binColl, &status); 1277 1278 status=U_ILLEGAL_ARGUMENT_ERROR; 1279 binResult=(uint8_t*)ures_getBinary(binColl, &len, &status); 1280 if(binResult != NULL){ 1281 log_err("ERROR: ures_getBinary() with status != U_ZERO_ERROR is supposed to fail\n"); 1282 } 1283 1284 /*Test ures_getNextResource() with status != U_ZERO_ERROR*/ 1285 teFillin=ures_getNextResource(teRes, teFillin, &status); 1286 if(teFillin != NULL){ 1287 log_err("ERROR: ures_getNextResource() with errorCode != U_ZERO_ERROR is supposed to fail\n"); 1288 } 1289 /*Test ures_getNextResource() with UResourceBundle = NULL*/ 1290 status=U_ZERO_ERROR; 1291 teFillin=ures_getNextResource(NULL, teFillin, &status); 1292 if(teFillin != NULL || status != U_ILLEGAL_ARGUMENT_ERROR){ 1293 log_err("ERROR: ures_getNextResource() with UResourceBundle = NULL is supposed to fail. Expected : U_IILEGAL_ARGUMENT_ERROR, Got : %s\n", 1294 myErrorName(status)); 1295 } 1296 /*Test ures_getNextString with errorCode != U_ZERO_ERROR*/ 1297 teFillin=ures_getByKey(teRes, "tagged_array_in_te_te_IN", teFillin, &status); 1298 key=ures_getKey(teFillin); 1299 status = U_ILLEGAL_ARGUMENT_ERROR; 1300 value=(UChar*)ures_getNextString(teFillin, &len, &key, &status); 1301 if(value != NULL){ 1302 log_err("ERROR: ures_getNextString() with errorCode != U_ZERO_ERROR is supposed to fail\n"); 1303 } 1304 /*Test ures_getNextString with UResourceBundle = NULL*/ 1305 status=U_ZERO_ERROR; 1306 value=(UChar*)ures_getNextString(NULL, &len, &key, &status); 1307 if(value != NULL || status != U_ILLEGAL_ARGUMENT_ERROR){ 1308 log_err("ERROR: ures_getNextString() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n", 1309 myErrorName(status)); 1310 } 1311 /*Test ures_getByIndex with errorCode != U_ZERO_ERROR*/ 1312 status=U_ZERO_ERROR; 1313 teFillin=ures_getByKey(teRes, "array_only_in_te", teFillin, &status); 1314 if(ures_countArrayItems(teRes, "array_only_in_te", &status) != 4) { 1315 log_err("ERROR: Wrong number of items in an array!\n"); 1316 } 1317 status=U_ILLEGAL_ARGUMENT_ERROR; 1318 teFillin2=ures_getByIndex(teFillin, 0, teFillin2, &status); 1319 if(teFillin2 != NULL){ 1320 log_err("ERROR: ures_getByIndex() with errorCode != U_ZERO_ERROR is supposed to fail\n"); 1321 } 1322 /*Test ures_getByIndex with UResourceBundle = NULL */ 1323 status=U_ZERO_ERROR; 1324 teFillin2=ures_getByIndex(NULL, 0, teFillin2, &status); 1325 if(status != U_ILLEGAL_ARGUMENT_ERROR){ 1326 log_err("ERROR: ures_getByIndex() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n", 1327 myErrorName(status)); 1328 } 1329 /*Test ures_getStringByIndex with errorCode != U_ZERO_ERROR*/ 1330 status=U_ZERO_ERROR; 1331 teFillin=ures_getByKey(teRes, "array_only_in_te", teFillin, &status); 1332 status=U_ILLEGAL_ARGUMENT_ERROR; 1333 value=(UChar*)ures_getStringByIndex(teFillin, 0, &len, &status); 1334 if( value != NULL){ 1335 log_err("ERROR: ures_getSringByIndex() with errorCode != U_ZERO_ERROR is supposed to fail\n"); 1336 } 1337 /*Test ures_getStringByIndex with UResourceBundle = NULL */ 1338 status=U_ZERO_ERROR; 1339 value=(UChar*)ures_getStringByIndex(NULL, 0, &len, &status); 1340 if(value != NULL || status != U_ILLEGAL_ARGUMENT_ERROR){ 1341 log_err("ERROR: ures_getStringByIndex() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n", 1342 myErrorName(status)); 1343 } 1344 /*Test ures_getStringByIndex with UResourceBundle = NULL */ 1345 status=U_ZERO_ERROR; 1346 value=(UChar*)ures_getStringByIndex(teFillin, 9999, &len, &status); 1347 if(value != NULL || status != U_MISSING_RESOURCE_ERROR){ 1348 log_err("ERROR: ures_getStringByIndex() with index that is too big is supposed to fail\n Expected: U_MISSING_RESOURCE_ERROR, Got: %s\n", 1349 myErrorName(status)); 1350 } 1351 /*Test ures_getInt() where UResourceBundle = NULL */ 1352 status=U_ZERO_ERROR; 1353 if(ures_getInt(NULL, &status) != -1 && status != U_ILLEGAL_ARGUMENT_ERROR){ 1354 log_err("ERROR: ures_getInt() with UResourceBundle = NULL should fail. Expected: U_IILEGAL_ARGUMENT_ERROR, Got: %s\n", 1355 myErrorName(status)); 1356 } 1357 /*Test ures_getInt() where status != U_ZERO_ERROR */ 1358 if(ures_getInt(teRes, &status) != -1){ 1359 log_err("ERROR: ures_getInt() with errorCode != U_ZERO_ERROR should fail\n"); 1360 } 1361 1362 ures_close(teFillin); 1363 ures_close(teFillin2); 1364 ures_close(coll); 1365 ures_close(binColl); 1366 ures_close(teRes); 1367 free(utestdatapath); 1368 1369 1370 } 1371 1372 static void TestGetVersion(){ 1373 UVersionInfo minVersionArray = {0x01, 0x00, 0x00, 0x00}; 1374 UVersionInfo maxVersionArray = {0x50, 0xff, 0xcf, 0xcf}; 1375 UVersionInfo versionArray; 1376 UErrorCode status= U_ZERO_ERROR; 1377 UResourceBundle* resB = NULL; 1378 int i=0, j = 0; 1379 int locCount = uloc_countAvailable(); 1380 const char *locName = "root"; 1381 1382 log_verbose("The ures_getVersion tests begin : \n"); 1383 1384 for(j = -1; j < locCount; j++) { 1385 if(j >= 0) { 1386 locName = uloc_getAvailable(j); 1387 } 1388 log_verbose("Testing version number for locale %s\n", locName); 1389 resB = ures_open(NULL,locName, &status); 1390 if (U_FAILURE(status)) { 1391 log_err_status(status, "Resource bundle creation for locale %s failed.: %s\n", locName, myErrorName(status)); 1392 ures_close(resB); 1393 return; 1394 } 1395 ures_getVersion(resB, versionArray); 1396 for (i=0; i<4; ++i) { 1397 if (versionArray[i] < minVersionArray[i] || 1398 versionArray[i] > maxVersionArray[i]) 1399 { 1400 log_err("Testing ures_getVersion(%-5s) - unexpected result: %d.%d.%d.%d\n", 1401 locName, versionArray[0], versionArray[1], versionArray[2], versionArray[3]); 1402 break; 1403 } 1404 } 1405 ures_close(resB); 1406 } 1407 } 1408 1409 1410 static void TestGetVersionColl(){ 1411 UVersionInfo minVersionArray = {0x00, 0x00, 0x00, 0x00}; 1412 UVersionInfo maxVersionArray = {0x50, 0x80, 0xcf, 0xcf}; 1413 UVersionInfo versionArray; 1414 UErrorCode status= U_ZERO_ERROR; 1415 UResourceBundle* resB = NULL; 1416 UEnumeration *locs= NULL; 1417 int i=0; 1418 const char *locName = "root"; 1419 int32_t locLen; 1420 const UChar* rules =NULL; 1421 int32_t len = 0; 1422 1423 log_verbose("The ures_getVersion(%s) tests begin : \n", U_ICUDATA_COLL); 1424 locs = ures_openAvailableLocales(U_ICUDATA_COLL, &status); 1425 if (U_FAILURE(status)) { 1426 log_err_status(status, "enumeration of %s failed.: %s\n", U_ICUDATA_COLL, myErrorName(status)); 1427 return; 1428 } 1429 1430 do{ 1431 log_verbose("Testing version number for locale %s\n", locName); 1432 resB = ures_open(U_ICUDATA_COLL,locName, &status); 1433 if (U_FAILURE(status)) { 1434 log_err("Resource bundle creation for locale %s:%s failed.: %s\n", U_ICUDATA_COLL, locName, myErrorName(status)); 1435 ures_close(resB); 1436 return; 1437 } 1438 /* test NUL termination of UCARules */ 1439 rules = tres_getString(resB,-1,"UCARules",&len, &status); 1440 if(!rules || U_FAILURE(status)) { 1441 /* BEGIN android-changd 1442 To save space, Android does not include the collation tailoring rules. 1443 Skip the error log. 1444 log_data_err("Could not load UCARules for locale %s\n", locName); 1445 */ 1446 status = U_ZERO_ERROR; 1447 /* END android-changed */ 1448 continue; 1449 } 1450 if(u_strlen(rules) != len){ 1451 log_err("UCARules string not nul terminated! \n"); 1452 } 1453 ures_getVersion(resB, versionArray); 1454 for (i=0; i<4; ++i) { 1455 if (versionArray[i] < minVersionArray[i] || 1456 versionArray[i] > maxVersionArray[i]) 1457 { 1458 log_err("Testing ures_getVersion(%-5s) - unexpected result: %d.%d.%d.%d\n", 1459 locName, versionArray[0], versionArray[1], versionArray[2], versionArray[3]); 1460 break; 1461 } 1462 } 1463 ures_close(resB); 1464 } while((locName = uenum_next(locs,&locLen,&status))&&U_SUCCESS(status)); 1465 1466 if(U_FAILURE(status)) { 1467 log_err("Err %s testing Collation locales.\n", u_errorName(status)); 1468 } 1469 uenum_close(locs); 1470 } 1471 1472 static void TestResourceBundles() 1473 { 1474 UErrorCode status = U_ZERO_ERROR; 1475 loadTestData(&status); 1476 if(U_FAILURE(status)) { 1477 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status)); 1478 return; 1479 } 1480 1481 testTag("only_in_Root", TRUE, FALSE, FALSE); 1482 testTag("in_Root_te", TRUE, TRUE, FALSE); 1483 testTag("in_Root_te_te_IN", TRUE, TRUE, TRUE); 1484 testTag("in_Root_te_IN", TRUE, FALSE, TRUE); 1485 testTag("only_in_te", FALSE, TRUE, FALSE); 1486 testTag("only_in_te_IN", FALSE, FALSE, TRUE); 1487 testTag("in_te_te_IN", FALSE, TRUE, TRUE); 1488 testTag("nonexistent", FALSE, FALSE, FALSE); 1489 1490 log_verbose("Passed:= %d Failed= %d \n", pass, fail); 1491 1492 } 1493 1494 1495 static void TestConstruction1() 1496 { 1497 UResourceBundle *test1 = 0, *test2 = 0,*empty = 0; 1498 const UChar *result1, *result2; 1499 UErrorCode status= U_ZERO_ERROR; 1500 UErrorCode err = U_ZERO_ERROR; 1501 const char* locale="te_IN"; 1502 const char* testdatapath; 1503 1504 int32_t len1=0; 1505 int32_t len2=0; 1506 UVersionInfo versionInfo; 1507 char versionString[256]; 1508 char verboseOutput[256]; 1509 1510 U_STRING_DECL(rootVal, "ROOT", 4); 1511 U_STRING_DECL(te_inVal, "TE_IN", 5); 1512 1513 U_STRING_INIT(rootVal, "ROOT", 4); 1514 U_STRING_INIT(te_inVal, "TE_IN", 5); 1515 1516 testdatapath=loadTestData(&status); 1517 if(U_FAILURE(status)) 1518 { 1519 log_data_err("Could not load testdata.dat %s \n",myErrorName(status)); 1520 return; 1521 } 1522 1523 log_verbose("Testing ures_open()......\n"); 1524 1525 empty = ures_open(testdatapath, "testempty", &status); 1526 if(empty == NULL || U_FAILURE(status)) { 1527 log_err("opening empty failed!\n"); 1528 } 1529 ures_close(empty); 1530 1531 test1=ures_open(testdatapath, NULL, &err); 1532 1533 if(U_FAILURE(err)) 1534 { 1535 log_err("construction of NULL did not succeed : %s \n", myErrorName(status)); 1536 return; 1537 } 1538 test2=ures_open(testdatapath, locale, &err); 1539 if(U_FAILURE(err)) 1540 { 1541 log_err("construction of %s did not succeed : %s \n", locale, myErrorName(status)); 1542 return; 1543 } 1544 result1= tres_getString(test1, -1, "string_in_Root_te_te_IN", &len1, &err); 1545 result2= tres_getString(test2, -1, "string_in_Root_te_te_IN", &len2, &err); 1546 if (U_FAILURE(err) || len1==0 || len2==0) { 1547 log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(status)); 1548 return; 1549 } 1550 log_verbose("for string_in_Root_te_te_IN, default.txt had %s\n", u_austrcpy(verboseOutput, result1)); 1551 log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", u_austrcpy(verboseOutput, result2)); 1552 if(u_strcmp(result1, rootVal) !=0 || u_strcmp(result2, te_inVal) !=0 ){ 1553 log_err("construction test failed. Run Verbose for more information"); 1554 } 1555 1556 1557 /* Test getVersionNumber*/ 1558 log_verbose("Testing version number\n"); 1559 log_verbose("for getVersionNumber : %s\n", ures_getVersionNumber(test1)); 1560 1561 log_verbose("Testing version \n"); 1562 ures_getVersion(test1, versionInfo); 1563 u_versionToString(versionInfo, versionString); 1564 1565 log_verbose("for getVersion : %s\n", versionString); 1566 1567 if(strcmp(versionString, ures_getVersionNumber(test1)) != 0) { 1568 log_err("Versions differ: %s vs %s\n", versionString, ures_getVersionNumber(test1)); 1569 } 1570 1571 ures_close(test1); 1572 ures_close(test2); 1573 1574 } 1575 1576 /*****************************************************************************/ 1577 /*****************************************************************************/ 1578 1579 static UBool testTag(const char* frag, 1580 UBool in_Root, 1581 UBool in_te, 1582 UBool in_te_IN) 1583 { 1584 int32_t failNum = fail; 1585 1586 /* Make array from input params */ 1587 1588 UBool is_in[3]; 1589 const char *NAME[] = { "ROOT", "TE", "TE_IN" }; 1590 1591 /* Now try to load the desired items */ 1592 UResourceBundle* theBundle = NULL; 1593 char tag[99]; 1594 char action[256]; 1595 UErrorCode expected_status,status = U_ZERO_ERROR,expected_resource_status = U_ZERO_ERROR; 1596 UChar* base = NULL; 1597 UChar* expected_string = NULL; 1598 const UChar* string = NULL; 1599 char buf[5]; 1600 char item_tag[10]; 1601 int32_t i,j,row,col, len; 1602 int32_t actual_bundle; 1603 int32_t count = 0; 1604 int32_t row_count=0; 1605 int32_t column_count=0; 1606 int32_t index = 0; 1607 int32_t tag_count= 0; 1608 const char* testdatapath; 1609 char verboseOutput[256]; 1610 UResourceBundle* array=NULL; 1611 UResourceBundle* array2d=NULL; 1612 UResourceBundle* tags=NULL; 1613 UResourceBundle* arrayItem1=NULL; 1614 1615 testdatapath = loadTestData(&status); 1616 if(U_FAILURE(status)) 1617 { 1618 log_data_err("Could not load testdata.dat %s \n",myErrorName(status)); 1619 return FALSE; 1620 } 1621 1622 is_in[0] = in_Root; 1623 is_in[1] = in_te; 1624 is_in[2] = in_te_IN; 1625 1626 strcpy(item_tag, "tag"); 1627 1628 for (i=0; i<bundles_count; ++i) 1629 { 1630 strcpy(action,"construction for "); 1631 strcat(action, param[i].name); 1632 1633 1634 status = U_ZERO_ERROR; 1635 1636 theBundle = ures_open(testdatapath, param[i].name, &status); 1637 CONFIRM_ErrorCode(status,param[i].expected_constructor_status); 1638 1639 if(i == 5) 1640 actual_bundle = 0; /* ne -> default */ 1641 else if(i == 3) 1642 actual_bundle = 1; /* te_NE -> te */ 1643 else if(i == 4) 1644 actual_bundle = 2; /* te_IN_NE -> te_IN */ 1645 else 1646 actual_bundle = i; 1647 1648 expected_resource_status = U_MISSING_RESOURCE_ERROR; 1649 for (j=e_te_IN; j>=e_Root; --j) 1650 { 1651 if (is_in[j] && param[i].inherits[j]) 1652 { 1653 1654 if(j == actual_bundle) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */ 1655 expected_resource_status = U_ZERO_ERROR; 1656 else if(j == 0) 1657 expected_resource_status = U_USING_DEFAULT_WARNING; 1658 else 1659 expected_resource_status = U_USING_FALLBACK_WARNING; 1660 1661 log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>. actual_bundle=%s\n", 1662 param[i].name, 1663 i, 1664 frag, 1665 j, 1666 is_in[j]?"Yes":"No", 1667 j, 1668 param[i].inherits[j]?"Yes":"No", 1669 param[actual_bundle].name); 1670 1671 break; 1672 } 1673 } 1674 1675 for (j=param[i].where; j>=0; --j) 1676 { 1677 if (is_in[j]) 1678 { 1679 if(base != NULL) { 1680 free(base); 1681 base = NULL; 1682 } 1683 base=(UChar*)malloc(sizeof(UChar)*(strlen(NAME[j]) + 1)); 1684 u_uastrcpy(base,NAME[j]); 1685 1686 break; 1687 } 1688 else { 1689 if(base != NULL) { 1690 free(base); 1691 base = NULL; 1692 } 1693 base = (UChar*) malloc(sizeof(UChar) * 1); 1694 *base = 0x0000; 1695 } 1696 } 1697 1698 /*----string---------------------------------------------------------------- */ 1699 1700 strcpy(tag,"string_"); 1701 strcat(tag,frag); 1702 1703 strcpy(action,param[i].name); 1704 strcat(action, ".ures_getStringByKey(" ); 1705 strcat(action,tag); 1706 strcat(action, ")"); 1707 1708 1709 status = U_ZERO_ERROR; 1710 len=0; 1711 1712 string=tres_getString(theBundle, -1, tag, &len, &status); 1713 if(U_SUCCESS(status)) { 1714 expected_string=(UChar*)malloc(sizeof(UChar)*(u_strlen(base) + 4)); 1715 u_strcpy(expected_string,base); 1716 CONFIRM_INT_EQ(len, u_strlen(expected_string)); 1717 }else{ 1718 expected_string = (UChar*)malloc(sizeof(UChar)*(u_strlen(kERROR) + 1)); 1719 u_strcpy(expected_string,kERROR); 1720 string=kERROR; 1721 } 1722 log_verbose("%s got %d, expected %d\n", action, status, expected_resource_status); 1723 1724 CONFIRM_ErrorCode(status, expected_resource_status); 1725 CONFIRM_EQ(string, expected_string); 1726 1727 1728 1729 /*--------------array------------------------------------------------- */ 1730 1731 strcpy(tag,"array_"); 1732 strcat(tag,frag); 1733 1734 strcpy(action,param[i].name); 1735 strcat(action, ".ures_getByKey(" ); 1736 strcat(action,tag); 1737 strcat(action, ")"); 1738 1739 len=0; 1740 1741 count = kERROR_COUNT; 1742 status = U_ZERO_ERROR; 1743 array=ures_getByKey(theBundle, tag, array, &status); 1744 CONFIRM_ErrorCode(status,expected_resource_status); 1745 if (U_SUCCESS(status)) { 1746 /*confirm the resource type is an array*/ 1747 CONFIRM_INT_EQ(ures_getType(array), URES_ARRAY); 1748 /*confirm the size*/ 1749 count=ures_getSize(array); 1750 CONFIRM_INT_GE(count,1); 1751 for (j=0; j<count; ++j) { 1752 UChar element[3]; 1753 u_strcpy(expected_string, base); 1754 u_uastrcpy(element, itoa1(j,buf)); 1755 u_strcat(expected_string, element); 1756 arrayItem1=ures_getNextResource(array, arrayItem1, &status); 1757 if(U_SUCCESS(status)){ 1758 CONFIRM_EQ(tres_getString(arrayItem1, -1, NULL, &len, &status),expected_string); 1759 } 1760 } 1761 1762 } 1763 else { 1764 CONFIRM_INT_EQ(count,kERROR_COUNT); 1765 CONFIRM_ErrorCode(status, U_MISSING_RESOURCE_ERROR); 1766 /*CONFIRM_INT_EQ((int32_t)(unsigned long)array,(int32_t)0);*/ 1767 count = 0; 1768 } 1769 1770 /*--------------arrayItem------------------------------------------------- */ 1771 1772 strcpy(tag,"array_"); 1773 strcat(tag,frag); 1774 1775 strcpy(action,param[i].name); 1776 strcat(action, ".ures_getStringByIndex("); 1777 strcat(action, tag); 1778 strcat(action, ")"); 1779 1780 1781 for (j=0; j<10; ++j){ 1782 index = count ? (randi(count * 3) - count) : (randi(200) - 100); 1783 status = U_ZERO_ERROR; 1784 string=kERROR; 1785 array=ures_getByKey(theBundle, tag, array, &status); 1786 if(!U_FAILURE(status)){ 1787 UChar *t=NULL; 1788 t=(UChar*)ures_getStringByIndex(array, index, &len, &status); 1789 if(!U_FAILURE(status)){ 1790 UChar element[3]; 1791 string=t; 1792 u_strcpy(expected_string, base); 1793 u_uastrcpy(element, itoa1(index,buf)); 1794 u_strcat(expected_string, element); 1795 } else { 1796 u_strcpy(expected_string, kERROR); 1797 } 1798 1799 } 1800 expected_status = (index >= 0 && index < count) ? expected_resource_status : U_MISSING_RESOURCE_ERROR; 1801 CONFIRM_ErrorCode(status,expected_status); 1802 CONFIRM_EQ(string,expected_string); 1803 1804 } 1805 1806 1807 /*--------------2dArray------------------------------------------------- */ 1808 1809 strcpy(tag,"array_2d_"); 1810 strcat(tag,frag); 1811 1812 strcpy(action,param[i].name); 1813 strcat(action, ".ures_getByKey(" ); 1814 strcat(action,tag); 1815 strcat(action, ")"); 1816 1817 1818 1819 row_count = kERROR_COUNT, column_count = kERROR_COUNT; 1820 status = U_ZERO_ERROR; 1821 array2d=ures_getByKey(theBundle, tag, array2d, &status); 1822 1823 CONFIRM_ErrorCode(status,expected_resource_status); 1824 if (U_SUCCESS(status)) 1825 { 1826 /*confirm the resource type is an 2darray*/ 1827 CONFIRM_INT_EQ(ures_getType(array2d), URES_ARRAY); 1828 row_count=ures_getSize(array2d); 1829 CONFIRM_INT_GE(row_count,1); 1830 1831 for(row=0; row<row_count; ++row){ 1832 UResourceBundle *tableRow=NULL; 1833 tableRow=ures_getByIndex(array2d, row, tableRow, &status); 1834 CONFIRM_ErrorCode(status, expected_resource_status); 1835 if(U_SUCCESS(status)){ 1836 /*confirm the resourcetype of each table row is an array*/ 1837 CONFIRM_INT_EQ(ures_getType(tableRow), URES_ARRAY); 1838 column_count=ures_getSize(tableRow); 1839 CONFIRM_INT_GE(column_count,1); 1840 1841 for (col=0; j<column_count; ++j) { 1842 UChar element[3]; 1843 u_strcpy(expected_string, base); 1844 u_uastrcpy(element, itoa1(row, buf)); 1845 u_strcat(expected_string, element); 1846 u_uastrcpy(element, itoa1(col, buf)); 1847 u_strcat(expected_string, element); 1848 arrayItem1=ures_getNextResource(tableRow, arrayItem1, &status); 1849 if(U_SUCCESS(status)){ 1850 const UChar *stringValue=tres_getString(arrayItem1, -1, NULL, &len, &status); 1851 CONFIRM_EQ(stringValue, expected_string); 1852 } 1853 } 1854 } 1855 ures_close(tableRow); 1856 } 1857 }else{ 1858 CONFIRM_INT_EQ(row_count,kERROR_COUNT); 1859 CONFIRM_INT_EQ(column_count,kERROR_COUNT); 1860 row_count=column_count=0; 1861 } 1862 1863 1864 /*------2dArrayItem-------------------------------------------------------------- */ 1865 /* 2dArrayItem*/ 1866 for (j=0; j<10; ++j) 1867 { 1868 row = row_count ? (randi(row_count * 3) - row_count) : (randi(200) - 100); 1869 col = column_count ? (randi(column_count * 3) - column_count) : (randi(200) - 100); 1870 status = U_ZERO_ERROR; 1871 string = kERROR; 1872 len=0; 1873 array2d=ures_getByKey(theBundle, tag, array2d, &status); 1874 if(U_SUCCESS(status)){ 1875 UResourceBundle *tableRow=NULL; 1876 tableRow=ures_getByIndex(array2d, row, tableRow, &status); 1877 if(U_SUCCESS(status)) { 1878 UChar *t=NULL; 1879 t=(UChar*)ures_getStringByIndex(tableRow, col, &len, &status); 1880 if(U_SUCCESS(status)){ 1881 string=t; 1882 } 1883 } 1884 ures_close(tableRow); 1885 } 1886 expected_status = (row >= 0 && row < row_count && col >= 0 && col < column_count) ? 1887 expected_resource_status: U_MISSING_RESOURCE_ERROR; 1888 CONFIRM_ErrorCode(status,expected_status); 1889 1890 if (U_SUCCESS(status)){ 1891 UChar element[3]; 1892 u_strcpy(expected_string, base); 1893 u_uastrcpy(element, itoa1(row, buf)); 1894 u_strcat(expected_string, element); 1895 u_uastrcpy(element, itoa1(col, buf)); 1896 u_strcat(expected_string, element); 1897 } else { 1898 u_strcpy(expected_string,kERROR); 1899 } 1900 CONFIRM_EQ(string,expected_string); 1901 1902 } 1903 1904 1905 /*--------------taggedArray----------------------------------------------- */ 1906 strcpy(tag,"tagged_array_"); 1907 strcat(tag,frag); 1908 1909 strcpy(action,param[i].name); 1910 strcat(action,".ures_getByKey("); 1911 strcat(action, tag); 1912 strcat(action,")"); 1913 1914 1915 status = U_ZERO_ERROR; 1916 tag_count=0; 1917 tags=ures_getByKey(theBundle, tag, tags, &status); 1918 CONFIRM_ErrorCode(status, expected_resource_status); 1919 if (U_SUCCESS(status)) { 1920 UResType bundleType=ures_getType(tags); 1921 CONFIRM_INT_EQ(bundleType, URES_TABLE); 1922 1923 tag_count=ures_getSize(tags); 1924 CONFIRM_INT_GE((int32_t)tag_count, (int32_t)0); 1925 1926 for(index=0; index <tag_count; index++){ 1927 UResourceBundle *tagelement=NULL; 1928 const char *key=NULL; 1929 UChar* value=NULL; 1930 tagelement=ures_getByIndex(tags, index, tagelement, &status); 1931 key=ures_getKey(tagelement); 1932 value=(UChar*)ures_getNextString(tagelement, &len, &key, &status); 1933 log_verbose("tag = %s, value = %s\n", key, u_austrcpy(verboseOutput, value)); 1934 if(strncmp(key, "tag", 3) == 0 && u_strncmp(value, base, u_strlen(base)) == 0){ 1935 record_pass(); 1936 }else{ 1937 record_fail(); 1938 } 1939 ures_close(tagelement); 1940 } 1941 }else{ 1942 tag_count=0; 1943 } 1944 1945 /*---------taggedArrayItem----------------------------------------------*/ 1946 count = 0; 1947 for (index=-20; index<20; ++index) 1948 { 1949 1950 status = U_ZERO_ERROR; 1951 string = kERROR; 1952 strcpy(item_tag, "tag"); 1953 strcat(item_tag, itoa1(index,buf)); 1954 tags=ures_getByKey(theBundle, tag, tags, &status); 1955 if(U_SUCCESS(status)){ 1956 UResourceBundle *tagelement=NULL; 1957 UChar *t=NULL; 1958 tagelement=ures_getByKey(tags, item_tag, tagelement, &status); 1959 if(!U_FAILURE(status)){ 1960 UResType elementType=ures_getType(tagelement); 1961 CONFIRM_INT_EQ(elementType, URES_STRING); 1962 if(strcmp(ures_getKey(tagelement), item_tag) == 0){ 1963 record_pass(); 1964 }else{ 1965 record_fail(); 1966 } 1967 t=(UChar*)tres_getString(tagelement, -1, NULL, &len, &status); 1968 if(!U_FAILURE(status)){ 1969 string=t; 1970 } 1971 } 1972 if (index < 0) { 1973 CONFIRM_ErrorCode(status,U_MISSING_RESOURCE_ERROR); 1974 } 1975 else{ 1976 if (status != U_MISSING_RESOURCE_ERROR) { 1977 UChar element[3]; 1978 u_strcpy(expected_string, base); 1979 u_uastrcpy(element, itoa1(index,buf)); 1980 u_strcat(expected_string, element); 1981 CONFIRM_EQ(string,expected_string); 1982 count++; 1983 } 1984 } 1985 ures_close(tagelement); 1986 } 1987 } 1988 CONFIRM_INT_EQ(count, tag_count); 1989 1990 free(expected_string); 1991 ures_close(theBundle); 1992 } 1993 ures_close(array); 1994 ures_close(array2d); 1995 ures_close(tags); 1996 ures_close(arrayItem1); 1997 free(base); 1998 return (UBool)(failNum == fail); 1999 } 2000 2001 static void record_pass() 2002 { 2003 ++pass; 2004 } 2005 2006 static void record_fail() 2007 { 2008 ++fail; 2009 } 2010 2011 /** 2012 * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR 2013 * are set correctly 2014 */ 2015 2016 static void TestFallback() 2017 { 2018 UErrorCode status = U_ZERO_ERROR; 2019 UResourceBundle *fr_FR = NULL; 2020 UResourceBundle *subResource = NULL; 2021 const UChar *junk; /* ignored */ 2022 int32_t resultLen; 2023 2024 log_verbose("Opening fr_FR.."); 2025 fr_FR = ures_open(NULL, "fr_FR", &status); 2026 if(U_FAILURE(status)) 2027 { 2028 log_err_status(status, "Couldn't open fr_FR - %s\n", u_errorName(status)); 2029 return; 2030 } 2031 2032 status = U_ZERO_ERROR; 2033 2034 2035 /* clear it out.. just do some calls to get the gears turning */ 2036 junk = tres_getString(fr_FR, -1, "LocaleID", &resultLen, &status); 2037 status = U_ZERO_ERROR; 2038 junk = tres_getString(fr_FR, -1, "LocaleString", &resultLen, &status); 2039 status = U_ZERO_ERROR; 2040 junk = tres_getString(fr_FR, -1, "LocaleID", &resultLen, &status); 2041 status = U_ZERO_ERROR; 2042 2043 /* OK first one. This should be a Default value. */ 2044 subResource = ures_getByKey(fr_FR, "MeasurementSystem", NULL, &status); 2045 if(status != U_USING_DEFAULT_WARNING) 2046 { 2047 log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get CurrencyMap from fr_FR, got %s\n", 2048 u_errorName(status)); 2049 } 2050 2051 status = U_ZERO_ERROR; 2052 ures_close(subResource); 2053 2054 /* and this is a Fallback, to fr */ 2055 junk = tres_getString(fr_FR, -1, "ExemplarCharacters", &resultLen, &status); 2056 if(status != U_USING_FALLBACK_WARNING) 2057 { 2058 log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get ExemplarCharacters from fr_FR, got %d\n", 2059 status); 2060 } 2061 2062 status = U_ZERO_ERROR; 2063 2064 ures_close(fr_FR); 2065 /* Temporary hack err actually should be U_USING_FALLBACK_ERROR */ 2066 /* Test Jitterbug 552 fallback mechanism of aliased data */ 2067 { 2068 UErrorCode err =U_ZERO_ERROR; 2069 UResourceBundle* myResB = ures_open(NULL,"no_NO_NY",&err); 2070 UResourceBundle* resLocID = ures_getByKey(myResB, "Version", NULL, &err); 2071 UResourceBundle* tResB; 2072 UResourceBundle* zoneResource; 2073 const UChar* version = NULL; 2074 static const UChar versionStr[] = { 0x0032, 0x002E, 0x0030, 0x002E, 0x0034, 0x0031, 0x002E, 0x0032, 0x0033, 0x0000}; 2075 2076 if(err != U_ZERO_ERROR){ 2077 log_data_err("Expected U_ZERO_ERROR when trying to test no_NO_NY aliased to nn_NO for Version err=%s\n",u_errorName(err)); 2078 return; 2079 } 2080 version = tres_getString(resLocID, -1, NULL, &resultLen, &err); 2081 if(u_strcmp(version, versionStr) != 0){ 2082 char x[100]; 2083 char g[100]; 2084 u_austrcpy(x, versionStr); 2085 u_austrcpy(g, version); 2086 log_data_err("ures_getString(resLocID, &resultLen, &err) returned an unexpected version value. Expected '%s', but got '%s'\n", 2087 x, g); 2088 } 2089 zoneResource = ures_open(U_ICUDATA_ZONE, "no_NO_NY", &err); 2090 tResB = ures_getByKey(zoneResource, "zoneStrings", NULL, &err); 2091 if(err != U_USING_FALLBACK_WARNING){ 2092 log_err("Expected U_USING_FALLBACK_ERROR when trying to test no_NO_NY aliased with nn_NO_NY for zoneStrings err=%s\n",u_errorName(err)); 2093 } 2094 ures_close(tResB); 2095 ures_close(zoneResource); 2096 ures_close(resLocID); 2097 ures_close(myResB); 2098 } 2099 2100 } 2101 2102 /* static void printUChars(UChar* uchars){ 2103 / int16_t i=0; 2104 / for(i=0; i<u_strlen(uchars); i++){ 2105 / log_err("%04X ", *(uchars+i)); 2106 / } 2107 / } */ 2108 2109 static void TestResourceLevelAliasing(void) { 2110 UErrorCode status = U_ZERO_ERROR; 2111 UResourceBundle *aliasB = NULL, *tb = NULL; 2112 UResourceBundle *en = NULL, *uk = NULL, *testtypes = NULL; 2113 const char* testdatapath = NULL; 2114 const UChar *string = NULL, *sequence = NULL; 2115 /*const uint8_t *binary = NULL, *binSequence = NULL;*/ 2116 int32_t strLen = 0, seqLen = 0;/*, binLen = 0, binSeqLen = 0;*/ 2117 char buffer[100]; 2118 char *s; 2119 2120 testdatapath=loadTestData(&status); 2121 if(U_FAILURE(status)) 2122 { 2123 log_data_err("Could not load testdata.dat %s \n",myErrorName(status)); 2124 return; 2125 } 2126 2127 aliasB = ures_open(testdatapath, "testaliases", &status); 2128 2129 if(U_FAILURE(status)) 2130 { 2131 log_data_err("Could not load testaliases.res %s \n",myErrorName(status)); 2132 return; 2133 } 2134 /* this should fail - circular alias */ 2135 tb = ures_getByKey(aliasB, "aaa", tb, &status); 2136 if(status != U_TOO_MANY_ALIASES_ERROR) { 2137 log_err("Failed to detect circular alias\n"); 2138 } 2139 else { 2140 status = U_ZERO_ERROR; 2141 } 2142 tb = ures_getByKey(aliasB, "aab", tb, &status); 2143 if(status != U_TOO_MANY_ALIASES_ERROR) { 2144 log_err("Failed to detect circular alias\n"); 2145 } else { 2146 status = U_ZERO_ERROR; 2147 } 2148 if(U_FAILURE(status) ) { 2149 log_data_err("err loading tb resource\n"); 2150 } else { 2151 /* testing aliasing to a non existing resource */ 2152 tb = ures_getByKey(aliasB, "nonexisting", tb, &status); 2153 if(status != U_MISSING_RESOURCE_ERROR) { 2154 log_err("Managed to find an alias to non-existing resource\n"); 2155 } else { 2156 status = U_ZERO_ERROR; 2157 } 2158 /* testing referencing/composed alias */ 2159 uk = ures_findResource("ja/LocaleScript/2", uk, &status); 2160 if((uk == NULL) || U_FAILURE(status)) { 2161 log_err_status(status, "Couldn't findResource('ja/LocaleScript/2') err %s\n", u_errorName(status)); 2162 goto cleanup; 2163 } 2164 2165 sequence = tres_getString(uk, -1, NULL, &seqLen, &status); 2166 2167 tb = ures_getByKey(aliasB, "referencingalias", tb, &status); 2168 string = tres_getString(tb, -1, NULL, &strLen, &status); 2169 2170 if(seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) { 2171 log_err("Referencing alias didn't get the right string (1)\n"); 2172 } 2173 2174 string = tres_getString(aliasB, -1, "referencingalias", &strLen, &status); 2175 if(seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) { 2176 log_err("Referencing alias didn't get the right string (2)\n"); 2177 } 2178 2179 checkStatus(__LINE__, U_ZERO_ERROR, status); 2180 tb = ures_getByKey(aliasB, "LocaleScript", tb, &status); 2181 checkStatus(__LINE__, U_ZERO_ERROR, status); 2182 tb = ures_getByIndex(tb, 2, tb, &status); 2183 checkStatus(__LINE__, U_ZERO_ERROR, status); 2184 string = tres_getString(tb, -1, NULL, &strLen, &status); 2185 checkStatus(__LINE__, U_ZERO_ERROR, status); 2186 2187 if(U_FAILURE(status)) { 2188 log_err("%s trying to get string via separate getters\n", u_errorName(status)); 2189 } else if(seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) { 2190 log_err("Referencing alias didn't get the right string (3)\n"); 2191 } 2192 2193 2194 { 2195 UResourceBundle* ja = ures_open(U_ICUDATA_BRKITR,"ja", &status); 2196 const UChar *got = NULL, *exp=NULL; 2197 int32_t gotLen = 0, expLen=0; 2198 ja = ures_getByKey(ja, "boundaries", ja, &status); 2199 exp = tres_getString(ja, -1, "word", &expLen, &status); 2200 2201 tb = ures_getByKey(aliasB, "boundaries", tb, &status); 2202 got = tres_getString(tb, -1, "word", &gotLen, &status); 2203 2204 if(U_FAILURE(status)) { 2205 log_err("%s trying to read str boundaries\n", u_errorName(status)); 2206 } else if(gotLen != expLen || u_strncmp(exp, got, gotLen) != 0) { 2207 log_err("Referencing alias didn't get the right data\n"); 2208 } 2209 ures_close(ja); 2210 status = U_ZERO_ERROR; 2211 } 2212 /* simple alias */ 2213 testtypes = ures_open(testdatapath, "testtypes", &status); 2214 strcpy(buffer, "menu/file/open"); 2215 s = buffer; 2216 uk = ures_findSubResource(testtypes, s, uk, &status); 2217 sequence = tres_getString(uk, -1, NULL, &seqLen, &status); 2218 2219 tb = ures_getByKey(aliasB, "simplealias", tb, &status); 2220 string = tres_getString(tb, -1, NULL, &strLen, &status); 2221 2222 if(U_FAILURE(status) || seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) { 2223 log_err("Referencing alias didn't get the right string (4)\n"); 2224 } 2225 2226 /* test indexed aliasing */ 2227 2228 tb = ures_getByKey(aliasB, "zoneTests", tb, &status); 2229 tb = ures_getByKey(tb, "zoneAlias2", tb, &status); 2230 string = tres_getString(tb, -1, NULL, &strLen, &status); 2231 2232 en = ures_findResource("/ICUDATA-zone/en/zoneStrings/3/0", en, &status); 2233 sequence = tres_getString(en, -1, NULL, &seqLen, &status); 2234 2235 if(U_FAILURE(status) || seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) { 2236 log_err("Referencing alias didn't get the right string (5)\n"); 2237 } 2238 } 2239 /* test getting aliased string by index */ 2240 { 2241 const char* keys[] = { 2242 "KeyAlias0PST", 2243 "KeyAlias1PacificStandardTime", 2244 "KeyAlias2PDT", 2245 "KeyAlias3LosAngeles" 2246 }; 2247 2248 const char* strings[] = { 2249 "America/Los_Angeles", 2250 "Pacific Standard Time", 2251 "PDT", 2252 "Los Angeles", 2253 }; 2254 UChar uBuffer[256]; 2255 const UChar* result; 2256 int32_t uBufferLen = 0, resultLen = 0; 2257 int32_t i = 0; 2258 const char *key = NULL; 2259 tb = ures_getByKey(aliasB, "testGetStringByKeyAliasing", tb, &status); 2260 if(U_FAILURE(status)) { 2261 log_err("FAIL: Couldn't get testGetStringByKeyAliasing resource: %s\n", u_errorName(status)); 2262 } else { 2263 for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) { 2264 result = tres_getString(tb, -1, keys[i], &resultLen, &status); 2265 if(U_FAILURE(status)){ 2266 log_err("(1) Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status)); 2267 continue; 2268 } 2269 uBufferLen = u_unescape(strings[i], uBuffer, 256); 2270 if(resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) { 2271 log_err("(1) Didn't get correct string while accessing alias table by key (%s)\n", keys[i]); 2272 } 2273 } 2274 for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) { 2275 result = tres_getString(tb, i, NULL, &resultLen, &status); 2276 if(U_FAILURE(status)){ 2277 log_err("(2) Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status)); 2278 continue; 2279 } 2280 uBufferLen = u_unescape(strings[i], uBuffer, 256); 2281 if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) { 2282 log_err("(2) Didn't get correct string while accesing alias table by index (%s)\n", strings[i]); 2283 } 2284 } 2285 for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) { 2286 result = ures_getNextString(tb, &resultLen, &key, &status); 2287 if(U_FAILURE(status)){ 2288 log_err("(3) Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status)); 2289 continue; 2290 } 2291 uBufferLen = u_unescape(strings[i], uBuffer, 256); 2292 if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) { 2293 log_err("(3) Didn't get correct string while iterating over alias table (%s)\n", strings[i]); 2294 } 2295 } 2296 } 2297 tb = ures_getByKey(aliasB, "testGetStringByIndexAliasing", tb, &status); 2298 if(U_FAILURE(status)) { 2299 log_err("FAIL: Couldn't get testGetStringByIndexAliasing resource: %s\n", u_errorName(status)); 2300 } else { 2301 for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) { 2302 result = tres_getString(tb, i, NULL, &resultLen, &status); 2303 if(U_FAILURE(status)){ 2304 log_err("Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status)); 2305 continue; 2306 } 2307 uBufferLen = u_unescape(strings[i], uBuffer, 256); 2308 if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) { 2309 log_err("Didn't get correct string while accesing alias by index in an array (%s)\n", strings[i]); 2310 } 2311 } 2312 for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) { 2313 result = ures_getNextString(tb, &resultLen, &key, &status); 2314 if(U_FAILURE(status)){ 2315 log_err("Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status)); 2316 continue; 2317 } 2318 uBufferLen = u_unescape(strings[i], uBuffer, 256); 2319 if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) { 2320 log_err("Didn't get correct string while iterating over aliases in an array (%s)\n", strings[i]); 2321 } 2322 } 2323 } 2324 } 2325 tb = ures_getByKey(aliasB, "testAliasToTree", tb, &status); 2326 if(U_FAILURE(status)){ 2327 log_err("Fetching the resource with key \"testAliasToTree\" failed. Error: %s\n", u_errorName(status)); 2328 goto cleanup; 2329 } 2330 if (strcmp(ures_getKey(tb), "collations") != 0) { 2331 log_err("ures_getKey(aliasB) unexpectedly returned %s instead of \"collations\"\n", ures_getKey(tb)); 2332 } 2333 cleanup: 2334 ures_close(aliasB); 2335 ures_close(tb); 2336 ures_close(en); 2337 ures_close(uk); 2338 ures_close(testtypes); 2339 } 2340 2341 static void TestDirectAccess(void) { 2342 UErrorCode status = U_ZERO_ERROR; 2343 UResourceBundle *t = NULL, *t2 = NULL; 2344 const char* key = NULL; 2345 2346 char buffer[100]; 2347 char *s; 2348 /*const char* testdatapath=loadTestData(&status); 2349 if(U_FAILURE(status)){ 2350 log_err("Could not load testdata.dat %s \n",myErrorName(status)); 2351 return; 2352 }*/ 2353 2354 t = ures_findResource("/testdata/te/zoneStrings/3/2", t, &status); 2355 if(U_FAILURE(status)) { 2356 log_data_err("Couldn't access indexed resource, error %s\n", u_errorName(status)); 2357 status = U_ZERO_ERROR; 2358 } else { 2359 key = ures_getKey(t); 2360 if(key != NULL) { 2361 log_err("Got a strange key, expected NULL, got %s\n", key); 2362 } 2363 } 2364 t = ures_findResource("en/calendar/gregorian/DateTimePatterns/3", t, &status); 2365 if(U_FAILURE(status)) { 2366 log_data_err("Couldn't access indexed resource, error %s\n", u_errorName(status)); 2367 status = U_ZERO_ERROR; 2368 } else { 2369 key = ures_getKey(t); 2370 if(key != NULL) { 2371 log_err("Got a strange key, expected NULL, got %s\n", key); 2372 } 2373 } 2374 2375 t = ures_findResource("ja/LocaleScript", t, &status); 2376 if(U_FAILURE(status)) { 2377 log_data_err("Couldn't access keyed resource, error %s\n", u_errorName(status)); 2378 status = U_ZERO_ERROR; 2379 } else { 2380 key = ures_getKey(t); 2381 if(strcmp(key, "LocaleScript")!=0) { 2382 log_err("Got a strange key, expected 'LocaleScript', got %s\n", key); 2383 } 2384 } 2385 2386 t2 = ures_open(U_ICUDATA_LANG, "sr", &status); 2387 if(U_FAILURE(status)) { 2388 log_err_status(status, "Couldn't open 'sr' resource bundle, error %s\n", u_errorName(status)); 2389 log_data_err("No 'sr', no test - you have bigger problems than testing direct access. " 2390 "You probably have no data! Aborting this test\n"); 2391 } 2392 2393 if(U_SUCCESS(status)) { 2394 strcpy(buffer, "Languages/hr"); 2395 s = buffer; 2396 t = ures_findSubResource(t2, s, t, &status); 2397 if(U_FAILURE(status)) { 2398 log_err("Couldn't access keyed resource, error %s\n", u_errorName(status)); 2399 status = U_ZERO_ERROR; 2400 } else { 2401 key = ures_getKey(t); 2402 if(strcmp(key, "hr")!=0) { 2403 log_err("Got a strange key, expected 'hr', got %s\n", key); 2404 } 2405 } 2406 } 2407 2408 t = ures_findResource("root/calendar/islamic-civil/DateTime", t, &status); 2409 if(U_SUCCESS(status)) { 2410 log_data_err("This resource does not exist. How did it get here?\n"); 2411 } 2412 status = U_ZERO_ERROR; 2413 2414 /* this one will freeze */ 2415 t = ures_findResource("root/calendar/islamic-civil/eras/abbreviated/0/mikimaus/pera", t, &status); 2416 if(U_SUCCESS(status)) { 2417 log_data_err("Second resource does not exist. How did it get here?\n"); 2418 } 2419 status = U_ZERO_ERROR; 2420 2421 ures_close(t2); 2422 t2 = ures_open(NULL, "he", &status); 2423 t2 = ures_getByKeyWithFallback(t2, "calendar", t2, &status); 2424 t2 = ures_getByKeyWithFallback(t2, "islamic-civil", t2, &status); 2425 t2 = ures_getByKeyWithFallback(t2, "DateTime", t2, &status); 2426 if(U_SUCCESS(status)) { 2427 log_err("This resource does not exist. How did it get here?\n"); 2428 } 2429 status = U_ZERO_ERROR; 2430 2431 ures_close(t2); 2432 t2 = ures_open(NULL, "he", &status); 2433 /* George's fix */ 2434 t2 = ures_getByKeyWithFallback(t2, "calendar", t2, &status); 2435 t2 = ures_getByKeyWithFallback(t2, "islamic-civil", t2, &status); 2436 t2 = ures_getByKeyWithFallback(t2, "eras", t2, &status); 2437 if(U_FAILURE(status)) { 2438 log_err_status(status, "Didn't get Eras. I know they are there!\n"); 2439 } 2440 status = U_ZERO_ERROR; 2441 2442 ures_close(t2); 2443 t2 = ures_open(NULL, "root", &status); 2444 t2 = ures_getByKeyWithFallback(t2, "calendar", t2, &status); 2445 t2 = ures_getByKeyWithFallback(t2, "islamic-civil", t2, &status); 2446 t2 = ures_getByKeyWithFallback(t2, "DateTime", t2, &status); 2447 if(U_SUCCESS(status)) { 2448 log_err("This resource does not exist. How did it get here?\n"); 2449 } 2450 status = U_ZERO_ERROR; 2451 2452 ures_close(t2); 2453 ures_close(t); 2454 } 2455 2456 static void TestJB3763(void) { 2457 /* Nasty bug prevented using parent as fill-in, since it would 2458 * stomp the path information. 2459 */ 2460 UResourceBundle *t = NULL; 2461 UErrorCode status = U_ZERO_ERROR; 2462 t = ures_open(NULL, "sr_Latn", &status); 2463 t = ures_getByKeyWithFallback(t, "calendar", t, &status); 2464 t = ures_getByKeyWithFallback(t, "gregorian", t, &status); 2465 t = ures_getByKeyWithFallback(t, "AmPmMarkers", t, &status); 2466 if(U_FAILURE(status)) { 2467 log_err_status(status, "This resource should be available?\n"); 2468 } 2469 status = U_ZERO_ERROR; 2470 2471 ures_close(t); 2472 2473 } 2474 2475 static void TestGetKeywordValues(void) { 2476 UEnumeration *kwVals; 2477 UBool foundStandard = FALSE; 2478 UErrorCode status = U_ZERO_ERROR; 2479 const char *kw; 2480 #if !UCONFIG_NO_COLLATION 2481 kwVals = ures_getKeywordValues( U_ICUDATA_COLL, "collations", &status); 2482 2483 log_verbose("Testing getting collation keyword values:\n"); 2484 2485 while((kw=uenum_next(kwVals, NULL, &status))) { 2486 log_verbose(" %s\n", kw); 2487 if(!strcmp(kw,"standard")) { 2488 if(foundStandard == FALSE) { 2489 foundStandard = TRUE; 2490 } else { 2491 log_err("'standard' was found twice in the keyword list.\n"); 2492 } 2493 } 2494 } 2495 if(foundStandard == FALSE) { 2496 log_err_status(status, "'standard' was not found in the keyword list.\n"); 2497 } 2498 uenum_close(kwVals); 2499 if(U_FAILURE(status)) { 2500 log_err_status(status, "err %s getting collation values\n", u_errorName(status)); 2501 } 2502 status = U_ZERO_ERROR; 2503 #endif 2504 foundStandard = FALSE; 2505 kwVals = ures_getKeywordValues( "ICUDATA", "calendar", &status); 2506 2507 log_verbose("Testing getting calendar keyword values:\n"); 2508 2509 while((kw=uenum_next(kwVals, NULL, &status))) { 2510 log_verbose(" %s\n", kw); 2511 if(!strcmp(kw,"japanese")) { 2512 if(foundStandard == FALSE) { 2513 foundStandard = TRUE; 2514 } else { 2515 log_err("'japanese' was found twice in the calendar keyword list.\n"); 2516 } 2517 } 2518 } 2519 if(foundStandard == FALSE) { 2520 log_err_status(status, "'japanese' was not found in the calendar keyword list.\n"); 2521 } 2522 uenum_close(kwVals); 2523 if(U_FAILURE(status)) { 2524 log_err_status(status, "err %s getting calendar values\n", u_errorName(status)); 2525 } 2526 } 2527 2528 static void TestGetFunctionalEquivalentOf(const char *path, const char *resName, const char *keyword, UBool truncate, const char * const testCases[]) { 2529 int32_t i; 2530 for(i=0;testCases[i];i+=3) { 2531 UBool expectAvail = (testCases[i][0]=='t')?TRUE:FALSE; 2532 UBool gotAvail = FALSE; 2533 const char *inLocale = testCases[i+1]; 2534 const char *expectLocale = testCases[i+2]; 2535 char equivLocale[256]; 2536 int32_t len; 2537 UErrorCode status = U_ZERO_ERROR; 2538 log_verbose("%d: %c %s\texpect %s\n",i/3, expectAvail?'t':'f', inLocale, expectLocale); 2539 len = ures_getFunctionalEquivalent(equivLocale, 255, path, 2540 resName, keyword, inLocale, 2541 &gotAvail, truncate, &status); 2542 if(U_FAILURE(status) || (len <= 0)) { 2543 log_err_status(status, "FAIL: got len %d, err %s on #%d: %c\t%s\t%s\n", 2544 len, u_errorName(status), 2545 i/3,expectAvail?'t':'f', inLocale, expectLocale); 2546 } else { 2547 log_verbose("got: %c %s\n", expectAvail?'t':'f',equivLocale); 2548 2549 if((gotAvail != expectAvail) || strcmp(equivLocale, expectLocale)) { 2550 log_err("FAIL: got avail=%c, loc=%s but expected #%d: %c\t%s\t-> loc=%s\n", 2551 gotAvail?'t':'f', equivLocale, 2552 i/3, 2553 expectAvail?'t':'f', inLocale, expectLocale); 2554 2555 } 2556 } 2557 } 2558 } 2559 2560 static void TestGetFunctionalEquivalent(void) { 2561 2562 /* BEGIN android-changed 2563 To save space, Android does not build full CJK collation tables. 2564 We skip the tests for big5han, gb2312* and unihan. */ 2565 static const char * const collCases[] = { 2566 /* avail locale equiv */ 2567 "f", "de_US_CALIFORNIA", "de", 2568 "f", "zh_TW@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_TW */ 2569 "t", "zh_Hant_TW@collation=stroke", "zh@collation=stroke", 2570 "f", "de_CN@collation=pinyin", "de", 2571 "t", "zh@collation=pinyin", "zh", 2572 "f", "zh_CN@collation=pinyin", "zh", /* alias of zh_Hans_CN */ 2573 "t", "zh_Hans_CN@collation=pinyin", "zh", 2574 "f", "zh_HK@collation=pinyin", "zh", /* alias of zh_Hant_HK */ 2575 "t", "zh_Hant_HK@collation=pinyin", "zh", 2576 "f", "zh_HK@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_HK */ 2577 "t", "zh_Hant_HK@collation=stroke", "zh@collation=stroke", 2578 "f", "zh_HK", "zh@collation=stroke", /* alias of zh_Hant_HK */ 2579 "t", "zh_Hant_HK", "zh@collation=stroke", 2580 "f", "zh_MO", "zh@collation=stroke", /* alias of zh_Hant_MO */ 2581 "t", "zh_Hant_MO", "zh@collation=stroke", 2582 "f", "zh_TW_STROKE", "zh@collation=stroke", 2583 /* "f", "zh_TW_STROKE@collation=big5han", "zh@collation=big5han", */ 2584 "f", "de_CN@calendar=japanese", "de", 2585 "t", "de@calendar=japanese", "de", 2586 /* "f", "zh_TW@collation=big5han", "zh@collation=big5han", */ /* alias of zh_Hant_TW */ 2587 /* "t", "zh_Hant_TW@collation=big5han", "zh@collation=big5han", */ 2588 /* "f", "zh_TW@collation=gb2312han", "zh@collation=gb2312han", */ /* alias of zh_Hant_TW */ 2589 /* "t", "zh_Hant_TW@collation=gb2312han", "zh@collation=gb2312han", */ 2590 /* "f", "zh_CN@collation=big5han", "zh@collation=big5han", *//* alias of zh_Hans_CN */ 2591 /* "t", "zh_Hans_CN@collation=big5han", "zh@collation=big5han", */ 2592 /* "f", "zh_CN@collation=gb2312han", "zh@collation=gb2312han", */ /* alias of zh_Hans_CN */ 2593 /* "t", "zh_Hans_CN@collation=gb2312han", "zh@collation=gb2312han", */ 2594 /* "t", "zh@collation=big5han", "zh@collation=big5han", */ 2595 /* "t", "zh@collation=gb2312han", "zh@collation=gb2312han", */ 2596 "t", "hi_IN@collation=direct", "hi@collation=direct", 2597 "t", "hi@collation=standard", "hi", 2598 "t", "hi@collation=direct", "hi@collation=direct", 2599 "f", "hi_AU@collation=direct;currency=CHF;calendar=buddhist", "hi@collation=direct", 2600 "f", "hi_AU@collation=standard;currency=CHF;calendar=buddhist", "hi", 2601 "t", "de_DE@collation=pinyin", "de", /* bug 4582 tests */ 2602 "f", "de_DE_BONN@collation=pinyin", "de", 2603 "t", "nl", "root", 2604 "t", "nl_NL", "root", 2605 "f", "nl_NL_EEXT", "root", 2606 "t", "nl@collation=stroke", "root", 2607 "t", "nl_NL@collation=stroke", "root", 2608 "f", "nl_NL_EEXT@collation=stroke", "root", 2609 NULL 2610 }; 2611 /* END android-changed */ 2612 2613 static const char *calCases[] = { 2614 /* avail locale equiv */ 2615 "t", "en_US_POSIX", "en_US@calendar=gregorian", 2616 "f", "ja_JP_TOKYO", "ja_JP@calendar=gregorian", 2617 "f", "ja_JP_TOKYO@calendar=japanese", "ja@calendar=japanese", 2618 "t", "sr@calendar=gregorian", "sr@calendar=gregorian", 2619 "t", "en", "en@calendar=gregorian", 2620 NULL 2621 }; 2622 2623 #if !UCONFIG_NO_COLLATION 2624 TestGetFunctionalEquivalentOf(U_ICUDATA_COLL, "collations", "collation", TRUE, collCases); 2625 #endif 2626 TestGetFunctionalEquivalentOf("ICUDATA", "calendar", "calendar", FALSE, calCases); 2627 2628 #if !UCONFIG_NO_COLLATION 2629 log_verbose("Testing error conditions:\n"); 2630 { 2631 char equivLocale[256] = "???"; 2632 int32_t len; 2633 UErrorCode status = U_ZERO_ERROR; 2634 UBool gotAvail = FALSE; 2635 2636 len = ures_getFunctionalEquivalent(equivLocale, 255, U_ICUDATA_COLL, 2637 "calendar", "calendar", "ar_EG@calendar=islamic", 2638 &gotAvail, FALSE, &status); 2639 2640 if(status == U_MISSING_RESOURCE_ERROR) { 2641 log_verbose("PASS: Got expected U_MISSING_RESOURCE_ERROR\n"); 2642 } else { 2643 log_err("ures_getFunctionalEquivalent returned locale %s, avail %c, err %s, but expected U_MISSING_RESOURCE_ERROR \n", 2644 equivLocale, gotAvail?'t':'f', u_errorName(status)); 2645 } 2646 } 2647 #endif 2648 } 2649 2650 static void TestXPath(void) { 2651 UErrorCode status = U_ZERO_ERROR; 2652 UResourceBundle *rb = NULL, *alias = NULL; 2653 int32_t len = 0; 2654 const UChar* result = NULL; 2655 const UChar expResult[] = { 0x0063, 0x006F, 0x0072, 0x0072, 0x0065, 0x0063, 0x0074, 0x0000 }; /* "correct" */ 2656 /*const UChar expResult[] = { 0x0074, 0x0065, 0x0069, 0x006E, 0x0064, 0x0065, 0x0073, 0x0074, 0x0000 }; *//*teindest*/ 2657 2658 const char *testdatapath=loadTestData(&status); 2659 if(U_FAILURE(status)) 2660 { 2661 log_data_err("Could not load testdata.dat %s \n",myErrorName(status)); 2662 return; 2663 } 2664 2665 log_verbose("Testing ures_open()......\n"); 2666 2667 rb = ures_open(testdatapath, "te_IN", &status); 2668 if(U_FAILURE(status)) { 2669 log_err("Could not open te_IN (%s)\n", myErrorName(status)); 2670 return; 2671 } 2672 alias = ures_getByKey(rb, "rootAliasClient", alias, &status); 2673 if(U_FAILURE(status)) { 2674 log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status)); 2675 ures_close(rb); 2676 return; 2677 } 2678 2679 result = tres_getString(alias, -1, NULL, &len, &status); 2680 if(U_FAILURE(status) || result == NULL || u_strcmp(result, expResult)) { 2681 log_err("Couldn't get correct string value (%s)\n", myErrorName(status)); 2682 } 2683 2684 alias = ures_getByKey(rb, "aliasClient", alias, &status); 2685 if(U_FAILURE(status)) { 2686 log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status)); 2687 ures_close(rb); 2688 return; 2689 } 2690 2691 result = tres_getString(alias, -1, NULL, &len, &status); 2692 if(U_FAILURE(status) || result == NULL || u_strcmp(result, expResult)) { 2693 log_err("Couldn't get correct string value (%s)\n", myErrorName(status)); 2694 } 2695 2696 alias = ures_getByKey(rb, "nestedRootAliasClient", alias, &status); 2697 if(U_FAILURE(status)) { 2698 log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status)); 2699 ures_close(rb); 2700 return; 2701 } 2702 2703 result = tres_getString(alias, -1, NULL, &len, &status); 2704 if(U_FAILURE(status) || result == NULL || u_strcmp(result, expResult)) { 2705 log_err("Couldn't get correct string value (%s)\n", myErrorName(status)); 2706 } 2707 2708 ures_close(alias); 2709 ures_close(rb); 2710 } 2711 static void TestCLDRStyleAliases(void) { 2712 UErrorCode status = U_ZERO_ERROR; 2713 UResourceBundle *rb = NULL, *alias = NULL, *a=NULL; 2714 int32_t i, len; 2715 char resource[256]; 2716 const UChar *result = NULL; 2717 UChar expected[256]; 2718 const char *expects[7] = { "", "a41", "a12", "a03", "ar4" }; 2719 const char *testdatapath=loadTestData(&status); 2720 if(U_FAILURE(status)) { 2721 log_data_err("Could not load testdata.dat %s \n",myErrorName(status)); 2722 return; 2723 } 2724 log_verbose("Testing CLDR style aliases......\n"); 2725 2726 rb = ures_open(testdatapath, "te_IN_REVISED", &status); 2727 if(U_FAILURE(status)) { 2728 log_err("Could not open te_IN (%s)\n", myErrorName(status)); 2729 return; 2730 } 2731 alias = ures_getByKey(rb, "a", alias, &status); 2732 if(U_FAILURE(status)) { 2733 log_err("Couldn't find the aliased with name \"a\" resource (%s)\n", myErrorName(status)); 2734 ures_close(rb); 2735 return; 2736 } 2737 for(i = 1; i < 5 ; i++) { 2738 resource[0]='a'; 2739 resource[1]='0'+i; 2740 resource[2]=0; 2741 /* instead of sprintf(resource, "a%i", i); */ 2742 a = ures_getByKeyWithFallback(alias, resource, a, &status); 2743 result = tres_getString(a, -1, NULL, &len, &status); 2744 u_charsToUChars(expects[i], expected, strlen(expects[i])+1); 2745 if(U_FAILURE(status) || !result || u_strcmp(result, expected)) { 2746 log_err("CLDR style aliases failed resource with name \"%s\" resource, exp %s, got %S (%s)\n", resource, expects[i], result, myErrorName(status)); 2747 status = U_ZERO_ERROR; 2748 } 2749 } 2750 2751 ures_close(a); 2752 ures_close(alias); 2753 ures_close(rb); 2754 } 2755 2756 static void TestFallbackCodes(void) { 2757 UErrorCode status = U_ZERO_ERROR; 2758 const char *testdatapath=loadTestData(&status); 2759 2760 UResourceBundle *res = ures_open(testdatapath, "te_IN", &status); 2761 2762 UResourceBundle *r = NULL, *fall = NULL; 2763 2764 r = ures_getByKey(res, "tagged_array_in_Root_te_te_IN", r, &status); 2765 2766 status = U_ZERO_ERROR; 2767 fall = ures_getByKeyWithFallback(r, "tag2", fall, &status); 2768 2769 if(status != U_ZERO_ERROR) { 2770 log_data_err("Expected error code to be U_ZERO_ERROR, got %s\n", u_errorName(status)); 2771 status = U_ZERO_ERROR; 2772 } 2773 2774 fall = ures_getByKeyWithFallback(r, "tag7", fall, &status); 2775 2776 if(status != U_USING_FALLBACK_WARNING) { 2777 log_data_err("Expected error code to be U_USING_FALLBACK_WARNING, got %s\n", u_errorName(status)); 2778 } 2779 status = U_ZERO_ERROR; 2780 2781 fall = ures_getByKeyWithFallback(r, "tag1", fall, &status); 2782 2783 if(status != U_USING_DEFAULT_WARNING) { 2784 log_data_err("Expected error code to be U_USING_DEFAULT_WARNING, got %s\n", u_errorName(status)); 2785 } 2786 status = U_ZERO_ERROR; 2787 2788 ures_close(fall); 2789 ures_close(r); 2790 ures_close(res); 2791 } 2792 2793 /* This test will crash if this doesn't work. Results don't need testing. */ 2794 static void TestStackReuse(void) { 2795 UResourceBundle table; 2796 UErrorCode errorCode = U_ZERO_ERROR; 2797 UResourceBundle *rb = ures_open(NULL, "en_US", &errorCode); 2798 2799 if(U_FAILURE(errorCode)) { 2800 log_data_err("Could not load en_US locale. status=%s\n",myErrorName(errorCode)); 2801 return; 2802 } 2803 ures_initStackObject(&table); 2804 ures_getByKeyWithFallback(rb, "Types", &table, &errorCode); 2805 ures_getByKeyWithFallback(&table, "collation", &table, &errorCode); 2806 ures_close(rb); 2807 ures_close(&table); 2808 } 2809 2810 /* Test ures_getUTF8StringXYZ() --------------------------------------------- */ 2811 2812 /* 2813 * Replace most ures_getStringXYZ() with this function which wraps the 2814 * desired call and also calls the UTF-8 variant and checks that it works. 2815 */ 2816 extern const UChar * 2817 tres_getString(const UResourceBundle *resB, 2818 int32_t index, const char *key, 2819 int32_t *length, 2820 UErrorCode *status) { 2821 char buffer8[16]; 2822 char *p8; 2823 const UChar *s16; 2824 const char *s8; 2825 UChar32 c16, c8; 2826 int32_t length16, length8, i16, i8; 2827 UBool forceCopy; 2828 2829 if(length == NULL) { 2830 length = &length16; 2831 } 2832 if(index >= 0) { 2833 s16 = ures_getStringByIndex(resB, index, length, status); 2834 } else if(key != NULL) { 2835 s16 = ures_getStringByKey(resB, key, length, status); 2836 } else { 2837 s16 = ures_getString(resB, length, status); 2838 } 2839 if(U_FAILURE(*status)) { 2840 return s16; 2841 } 2842 length16 = *length; 2843 2844 /* try the UTF-8 variant of ures_getStringXYZ() */ 2845 for(forceCopy = FALSE; forceCopy <= TRUE; ++forceCopy) { 2846 p8 = buffer8; 2847 length8 = (int32_t)sizeof(buffer8); 2848 if(index >= 0) { 2849 s8 = ures_getUTF8StringByIndex(resB, index, p8, &length8, forceCopy, status); 2850 } else if(key != NULL) { 2851 s8 = ures_getUTF8StringByKey(resB, key, p8, &length8, forceCopy, status); 2852 } else { 2853 s8 = ures_getUTF8String(resB, p8, &length8, forceCopy, status); 2854 } 2855 if(*status == U_INVALID_CHAR_FOUND) { 2856 /* the UTF-16 string contains an unpaired surrogate, can't test UTF-8 variant */ 2857 return s16; 2858 } 2859 if(*status == U_BUFFER_OVERFLOW_ERROR) { 2860 *status = U_ZERO_ERROR; 2861 p8 = (char *)malloc(++length8); 2862 if(p8 == NULL) { 2863 return s16; 2864 } 2865 if(index >= 0) { 2866 s8 = ures_getUTF8StringByIndex(resB, index, p8, &length8, forceCopy, status); 2867 } else if(key != NULL) { 2868 s8 = ures_getUTF8StringByKey(resB, key, p8, &length8, forceCopy, status); 2869 } else { 2870 s8 = ures_getUTF8String(resB, p8, &length8, forceCopy, status); 2871 } 2872 } 2873 if(U_FAILURE(*status)) { 2874 /* something unexpected happened */ 2875 if(p8 != buffer8) { 2876 free(p8); 2877 } 2878 return s16; 2879 } 2880 2881 if(forceCopy && s8 != p8) { 2882 log_err("ures_getUTF8String(%p, %ld, '%s') did not write the string to dest\n", 2883 resB, (long)index, key); 2884 } 2885 2886 /* verify NUL-termination */ 2887 if((p8 != buffer8 || length8 < sizeof(buffer8)) && s8[length8] != 0) { 2888 log_err("ures_getUTF8String(%p, %ld, '%s') did not NUL-terminate\n", 2889 resB, (long)index, key); 2890 } 2891 /* verify correct string */ 2892 i16 = i8 = 0; 2893 while(i16 < length16 && i8 < length8) { 2894 U16_NEXT(s16, i16, length16, c16); 2895 U8_NEXT(s8, i8, length8, c8); 2896 if(c16 != c8) { 2897 log_err("ures_getUTF8String(%p, %ld, '%s') got a bad string, c16=U+%04lx!=U+%04lx=c8 before i16=%ld\n", 2898 resB, (long)index, key, (long)c16, (long)c8, (long)i16); 2899 } 2900 } 2901 /* verify correct length */ 2902 if(i16 < length16) { 2903 log_err("ures_getUTF8String(%p, %ld, '%s') UTF-8 string too short, length8=%ld, length16=%ld\n", 2904 resB, (long)index, key, (long)length8, (long)length16); 2905 } 2906 if(i8 < length8) { 2907 log_err("ures_getUTF8String(%p, %ld, '%s') UTF-8 string too long, length8=%ld, length16=%ld\n", 2908 resB, (long)index, key, (long)length8, (long)length16); 2909 } 2910 2911 /* clean up */ 2912 if(p8 != buffer8) { 2913 free(p8); 2914 } 2915 } 2916 return s16; 2917 } 2918 2919 /* 2920 * API tests for ures_getUTF8String(). 2921 * Most cases are handled by tres_getString(), which leaves argument checking 2922 * to be tested here. 2923 * Since the variants share most of their implementation, we only need to test 2924 * one of them. 2925 * We also need not test for checking arguments which will be checked by the 2926 * UTF-16 ures_getStringXYZ() that are called internally. 2927 */ 2928 static void 2929 TestGetUTF8String() { 2930 UResourceBundle *res; 2931 const char *testdatapath; 2932 char buffer8[16]; 2933 const char *s8; 2934 int32_t length8; 2935 UErrorCode status; 2936 2937 status = U_ZERO_ERROR; 2938 testdatapath = loadTestData(&status); 2939 if(U_FAILURE(status)) { 2940 log_data_err("Could not load testdata.dat - %s\n", u_errorName(status)); 2941 return; 2942 } 2943 2944 res = ures_open(testdatapath, "", &status); 2945 if(U_FAILURE(status)) { 2946 log_err("Unable to ures_open(testdata, \"\") - %s\n", u_errorName(status)); 2947 return; 2948 } 2949 2950 /* one good call */ 2951 status = U_ZERO_ERROR; 2952 length8 = (int32_t)sizeof(buffer8); 2953 s8 = ures_getUTF8StringByKey(res, "string_only_in_Root", buffer8, &length8, FALSE, &status); 2954 if(status != U_ZERO_ERROR) { 2955 log_err("ures_getUTF8StringByKey(testdata/root string) malfunctioned - %s\n", u_errorName(status)); 2956 } 2957 2958 /* negative capacity */ 2959 status = U_ZERO_ERROR; 2960 length8 = -1; 2961 s8 = ures_getUTF8StringByKey(res, "string_only_in_Root", buffer8, &length8, FALSE, &status); 2962 if(status != U_ILLEGAL_ARGUMENT_ERROR) { 2963 log_err("ures_getUTF8StringByKey(capacity<0) malfunctioned - %s\n", u_errorName(status)); 2964 } 2965 2966 /* capacity>0 but dest=NULL */ 2967 status = U_ZERO_ERROR; 2968 length8 = (int32_t)sizeof(buffer8); 2969 s8 = ures_getUTF8StringByKey(res, "string_only_in_Root", NULL, &length8, FALSE, &status); 2970 if(status != U_ILLEGAL_ARGUMENT_ERROR) { 2971 log_err("ures_getUTF8StringByKey(dest=NULL capacity>0) malfunctioned - %s\n", u_errorName(status)); 2972 } 2973 2974 ures_close(res); 2975 } 2976 2977 static void TestCLDRVersion(void) { 2978 UVersionInfo zeroVersion; 2979 UVersionInfo testExpect; 2980 UVersionInfo testCurrent; 2981 UVersionInfo cldrVersion; 2982 char tmp[200]; 2983 UErrorCode status = U_ZERO_ERROR; 2984 2985 /* setup the constant value */ 2986 u_versionFromString(zeroVersion, "0.0.0.0"); 2987 2988 /* test CLDR value from API */ 2989 ulocdata_getCLDRVersion(cldrVersion, &status); 2990 if(U_FAILURE(status)) { 2991 /* the show is pretty much over at this point */ 2992 log_err_status(status, "FAIL: ulocdata_getCLDRVersion() returned %s\n", u_errorName(status)); 2993 return; 2994 } else { 2995 u_versionToString(cldrVersion, tmp); 2996 log_info("ulocdata_getCLDRVersion() returned: '%s'\n", tmp); 2997 } 2998 2999 3000 /* setup from resource bundle */ 3001 { 3002 UResourceBundle *res; 3003 const char *testdatapath; 3004 3005 status = U_ZERO_ERROR; 3006 testdatapath = loadTestData(&status); 3007 if(U_FAILURE(status)) { 3008 log_data_err("Could not load testdata.dat - %s\n", u_errorName(status)); 3009 return; 3010 } 3011 3012 res = ures_openDirect(testdatapath, "root", &status); 3013 if(U_FAILURE(status)) { 3014 log_err("Unable to ures_open(testdata, \"\") - %s\n", u_errorName(status 3015 )); 3016 return; 3017 } 3018 ures_getVersionByKey(res, "ExpectCLDRVersionAtLeast", testExpect, &status); 3019 ures_getVersionByKey(res, "CurrentCLDRVersion", testCurrent, &status); 3020 ures_close(res); 3021 if(U_FAILURE(status)) { 3022 log_err("Unable to get test data for CLDR version - %s\n", u_errorName(status)); 3023 } 3024 } 3025 if(U_FAILURE(status)) return; 3026 3027 3028 u_versionToString(testExpect,tmp); 3029 log_verbose("(data) ExpectCLDRVersionAtLeast { %s }\n", tmp); 3030 if(memcmp(cldrVersion, testExpect, sizeof(UVersionInfo)) < 0) { 3031 log_data_err("CLDR version is too old, expect at least %s.", tmp); 3032 } 3033 u_versionToString(testCurrent,tmp); 3034 log_verbose("(data) CurrentCLDRVersion { %s }\n", tmp); 3035 switch(memcmp(cldrVersion, testCurrent, sizeof(UVersionInfo))) { 3036 case 0: break; /* OK- current. */ 3037 case -1: log_info("CLDR version is behind 'current' (for testdata/root.txt) %s. Some things may fail.\n", tmp); break; 3038 case 1: log_info("CLDR version is ahead of 'current' (for testdata/root.txt) %s. Some things may fail.\n", tmp); break; 3039 } 3040 3041 } 3042