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