1 /********************************************************************
3 * Copyright (c) 1997-2016, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /*******************************************************************************
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 ********************************************************************************
19 #include "unicode/utypes.h"
21 #include "unicode/putil.h"
22 #include "unicode/ustring.h"
23 #include "unicode/ucnv.h"
27 #include "unicode/uchar.h"
28 #include "ucol_imp.h" /* for U_ICUDATA_COLL */
29 #include "ubrkimpl.h" /* for U_ICUDATA_BRKITR */
30 #define RESTEST_HEAP_CHECK 0
32 #include "unicode/uloc.h"
33 #include "unicode/ulocdata.h"
36 #include "unicode/ctest.h"
43 /*****************************************************************************/
45 * Return a random unsigned long l where 0N <= l <= ULONG_MAX.
53 static UBool initialized
= FALSE
;
56 srand((unsigned)time(NULL
));
59 /* Assume rand has at least 12 bits of precision */
61 for (i
=0; i
<sizeof(l
); ++i
)
62 ((char*)&l
)[i
] = (char)((rand() & 0x0FF0) >> 4);
67 * Return a random double x where 0.0 <= x < 1.0.
72 return ((double)randul()) / UINT32_MAX
;
76 * Return a random integer i where 0 <= i < n.
78 static int32_t randi(int32_t n
)
80 return (int32_t)(randd() * n
);
82 /***************************************************************************************/
84 * Convert an integer, positive or negative, to a character string radix 10.
87 itoa1(int32_t i
, char* buf
)
97 /* Output digits in reverse order */
100 *p
++ = (char)('0' + (i
% 10));
106 /* Reverse the string */
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'*/};
119 /*****************************************************************************/
128 typedef enum E_Where E_Where
;
129 /*****************************************************************************/
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)); } */
137 CONFIRM_ErrorCode(UErrorCode actual
,UErrorCode expected
)
139 if ((expected
)==(actual
))
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
));
150 /* Array of our test objects */
155 UErrorCode expected_constructor_status
;
157 UBool like
[e_Where_count
];
158 UBool inherits
[e_Where_count
];
162 /* "te" means test */
163 /* "IN" means inherits */
164 /* "NE" or "ne" means "does not exist" */
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
} }
174 static int32_t bundles_count
= UPRV_LENGTHOF(param
);
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 static void TestCLDRVersion(void);
187 /***************************************************************************************/
189 /* Array of our test objects */
191 void addNEWResourceBundleTest(TestNode
** root
)
193 addTest(root
, &TestErrorCodes
, "tsutil/creststn/TestErrorCodes");
194 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
195 addTest(root
, &TestEmptyBundle
, "tsutil/creststn/TestEmptyBundle");
196 addTest(root
, &TestConstruction1
, "tsutil/creststn/TestConstruction1");
197 addTest(root
, &TestResourceBundles
, "tsutil/creststn/TestResourceBundles");
198 addTest(root
, &TestNewTypes
, "tsutil/creststn/TestNewTypes");
199 addTest(root
, &TestEmptyTypes
, "tsutil/creststn/TestEmptyTypes");
200 addTest(root
, &TestBinaryCollationData
, "tsutil/creststn/TestBinaryCollationData");
201 addTest(root
, &TestAPI
, "tsutil/creststn/TestAPI");
202 addTest(root
, &TestErrorConditions
, "tsutil/creststn/TestErrorConditions");
203 addTest(root
, &TestDecodedBundle
, "tsutil/creststn/TestDecodedBundle");
204 addTest(root
, &TestResourceLevelAliasing
, "tsutil/creststn/TestResourceLevelAliasing");
205 addTest(root
, &TestDirectAccess
, "tsutil/creststn/TestDirectAccess");
206 addTest(root
, &TestTicket9804
, "tsutil/creststn/TestTicket9804");
207 addTest(root
, &TestXPath
, "tsutil/creststn/TestXPath");
208 addTest(root
, &TestCLDRStyleAliases
, "tsutil/creststn/TestCLDRStyleAliases");
209 addTest(root
, &TestFallbackCodes
, "tsutil/creststn/TestFallbackCodes");
210 addTest(root
, &TestGetUTF8String
, "tsutil/creststn/TestGetUTF8String");
211 addTest(root
, &TestCLDRVersion
, "tsutil/creststn/TestCLDRVersion");
212 addTest(root
, &TestPreventFallback
, "tsutil/creststn/TestPreventFallback");
214 addTest(root
, &TestFallback
, "tsutil/creststn/TestFallback");
215 addTest(root
, &TestGetVersion
, "tsutil/creststn/TestGetVersion");
216 addTest(root
, &TestGetVersionColl
, "tsutil/creststn/TestGetVersionColl");
217 addTest(root
, &TestAliasConflict
, "tsutil/creststn/TestAliasConflict");
218 addTest(root
, &TestGetKeywordValues
, "tsutil/creststn/TestGetKeywordValues");
219 addTest(root
, &TestGetFunctionalEquivalent
,"tsutil/creststn/TestGetFunctionalEquivalent");
220 addTest(root
, &TestJB3763
, "tsutil/creststn/TestJB3763");
221 addTest(root
, &TestStackReuse
, "tsutil/creststn/TestStackReuse");
225 /***************************************************************************************/
226 static const char* norwayNames
[] = {
236 static const char* norwayLocales
[] = {
246 static void checkStatus(int32_t line
, UErrorCode expected
, UErrorCode status
) {
247 if(U_FAILURE(status
)) {
248 log_data_err("Resource not present, cannot test (%s:%d)\n", __FILE__
, line
);
250 if(status
!= expected
) {
251 log_err_status(status
, "%s:%d: Expected error code %s, got error code %s\n", __FILE__
, line
, u_errorName(expected
), u_errorName(status
));
255 static void TestErrorCodes(void) {
256 UErrorCode status
= U_USING_DEFAULT_WARNING
;
258 UResourceBundle
*r
= NULL
, *r2
= NULL
;
260 /* First check with ICUDATA */
261 /* first bundle should return fallback warning */
262 r
= ures_open(NULL
, "ti_ER_ASSAB", &status
);
263 checkStatus(__LINE__
, U_USING_FALLBACK_WARNING
, status
);
266 /* this bundle should return zero error, so it shouldn't change the status */
267 status
= U_USING_DEFAULT_WARNING
;
268 r
= ures_open(NULL
, "ti_ER", &status
);
269 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
271 /* we look up the resource which is aliased, but it lives in fallback */
273 if(U_SUCCESS(status
) && r
!= NULL
) {
274 status
= U_USING_DEFAULT_WARNING
;
275 r2
= ures_getByKey(r
, "ExemplarCharacters", NULL
, &status
); /* ExemplarCharacters lives in ti */
276 checkStatus(__LINE__
, U_USING_FALLBACK_WARNING
, status
);
280 /* this bundle should return zero error, so it shouldn't change the status */
281 status
= U_USING_DEFAULT_WARNING
;
282 r
= ures_open(U_ICUDATA_REGION
, "ti", &status
);
283 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
286 status
= U_USING_FALLBACK_WARNING
;
287 r
= ures_open(NULL
, "nolocale", &status
);
288 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
292 #if !UCONFIG_NO_COLLATION
293 /** Now, with the collation bundle **/
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
);
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
);
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
);
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
);
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
);
326 status
= U_USING_FALLBACK_WARNING
;
327 r
= ures_open(U_ICUDATA_COLL
, "nolocale", &status
);
328 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
331 #endif /* !UCONFIG_NO_COLLATION */
334 static void TestAliasConflict(void) {
335 UErrorCode status
= U_ZERO_ERROR
;
336 UResourceBundle
*he
= NULL
;
337 UResourceBundle
*iw
= NULL
;
338 UResourceBundle
*norway
= NULL
;
339 const UChar
*result
= NULL
;
343 const char *realName
= NULL
;
345 he
= ures_open(NULL
, "he", &status
);
346 iw
= ures_open(NULL
, "iw", &status
);
347 if(U_FAILURE(status
)) {
348 log_err_status(status
, "Failed to get resource with %s\n", myErrorName(status
));
351 result
= ures_getStringByKey(he
, "ExemplarCharacters", &resultLen
, &status
);
352 if(U_FAILURE(status
) || result
== NULL
) {
353 log_err_status(status
, "Failed to get resource ExemplarCharacters with %s\n", myErrorName(status
));
357 size
= UPRV_LENGTHOF(norwayNames
);
358 for(i
= 0; i
< size
; i
++) {
359 status
= U_ZERO_ERROR
;
360 norway
= ures_open(NULL
, norwayNames
[i
], &status
);
361 if(U_FAILURE(status
)) {
362 log_err_status(status
, "Failed to get resource with %s for %s\n", myErrorName(status
), norwayNames
[i
]);
365 realName
= ures_getLocale(norway
, &status
);
366 log_verbose("ures_getLocale(\"%s\")=%s\n", norwayNames
[i
], realName
);
367 if(realName
== NULL
|| strcmp(norwayLocales
[i
], realName
) != 0) {
368 log_data_err("Wrong locale name for %s, expected %s, got %s\n", norwayNames
[i
], norwayLocales
[i
], realName
);
374 static void TestDecodedBundle(){
376 UErrorCode error
= U_ZERO_ERROR
;
378 UResourceBundle
* resB
;
380 const UChar
* srcFromRes
;
382 static const UChar uSrc
[] = {
383 0x0009,0x092F,0x0941,0x0928,0x0947,0x0938,0x094D,0x0915,0x094B,0x0020,0x002E,0x0915,0x0947,0x0020,0x002E,0x090F,
384 0x0915,0x0020,0x002E,0x0905,0x0927,0x094D,0x092F,0x092F,0x0928,0x0020,0x002E,0x0915,0x0947,0x0020,0x0905,0x0928,
385 0x0941,0x0938,0x093E,0x0930,0x0020,0x0031,0x0039,0x0039,0x0030,0x0020,0x0924,0x0915,0x0020,0x0915,0x0902,0x092A,
386 0x094D,0x092F,0x0942,0x091F,0x0930,0x002D,0x092A,0x094D,0x0930,0x092C,0x0902,0x0927,0x093F,0x0924,0x0020,0x0938,
387 0x0942,0x091A,0x0928,0x093E,0x092A,0x094D,0x0930,0x0923,0x093E,0x0932,0x0940,0x0020,0x002E,0x0915,0x0947,0x0020,
388 0x002E,0x092F,0x094B,0x0917,0x0926,0x093E,0x0928,0x0020,0x002E,0x0915,0x0947,0x0020,0x002E,0x092B,0x0932,0x0938,
389 0x094D,0x0935,0x0930,0x0942,0x092A,0x0020,0x002E,0x0935,0x093F,0x0936,0x094D,0x0935,0x0020,0x002E,0x092E,0x0947,
390 0x0902,0x0020,0x002E,0x0938,0x093E,0x0932,0x093E,0x0928,0x093E,0x0020,0x002E,0x0032,0x0032,0x0030,0x0030,0x0020,
391 0x0905,0x0930,0x092C,0x0020,0x0930,0x0941,0x092A,0x092F,0x0947,0x0020,0x092E,0x0942,0x0932,0x094D,0x092F,0x0915,
392 0x0940,0x0020,0x002E,0x0034,0x0935,0x0938,0x094D,0x0924,0x0941,0x0913,0x0902,0x0020,0x002E,0x0034,0x0915,0x093E,
393 0x0020,0x002E,0x0034,0x0909,0x0924,0x094D,0x092A,0x093E,0x0926,0x0928,0x0020,0x002E,0x0034,0x0939,0x094B,0x0917,
394 0x093E,0x002C,0x0020,0x002E,0x0033,0x091C,0x092C,0x0915,0x093F,0x0020,0x002E,0x0033,0x0915,0x0902,0x092A,0x094D,
395 0x092F,0x0942,0x091F,0x0930,0x0020,0x002E,0x0033,0x0915,0x093E,0x0020,0x002E,0x0033,0x0915,0x0941,0x0932,0x0020,
396 0x002E,0x0033,0x092F,0x094B,0x0917,0x0926,0x093E,0x0928,0x0020,0x002E,0x0033,0x0907,0x0938,0x0938,0x0947,0x0915,
397 0x0939,0x093F,0x0020,0x002E,0x002F,0x091C,0x094D,0x092F,0x093E,0x0926,0x093E,0x0020,0x002E,0x002F,0x0939,0x094B,
398 0x0917,0x093E,0x0964,0x0020,0x002E,0x002F,0x0905,0x0928,0x0941,0x0938,0x0902,0x0927,0x093E,0x0928,0x0020,0x002E,
399 0x002F,0x0915,0x0940,0x0020,0x002E,0x002F,0x091A,0x0930,0x092E,0x0020,0x0938,0x0940,0x092E,0x093E,0x0913,0x0902,
400 0x0020,0x092A,0x0930,0x0020,0x092A,0x0939,0x0941,0x0902,0x091A,0x0928,0x0947,0x0020,0x0915,0x0947,0x0020,0x0932,
401 0x093F,0x090F,0x0020,0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x090F,0x0915,0x0020,0x002E,0x002F,
402 0x0906,0x092E,0x0020,0x002E,0x002F,0x091C,0x0930,0x0942,0x0930,0x0924,0x0020,0x002E,0x002F,0x091C,0x0948,0x0938,
403 0x093E,0x0020,0x092C,0x0928,0x0020,0x0917,0x092F,0x093E,0x0020,0x0939,0x0948,0x0964,0x0020,0x092D,0x093E,0x0930,
404 0x0924,0x0020,0x092E,0x0947,0x0902,0x0020,0x092D,0x0940,0x002C,0x0020,0x0916,0x093E,0x0938,0x0915,0x0930,0x0020,
405 0x092E,0x094C,0x091C,0x0942,0x0926,0x093E,0x0020,0x0938,0x0930,0x0915,0x093E,0x0930,0x0928,0x0947,0x002C,0x0020,
406 0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x0020,0x0915,0x0947,0x0020,0x092A,0x094D,0x0930,0x092F,
407 0x094B,0x0917,0x0020,0x092A,0x0930,0x0020,0x091C,0x092C,0x0930,0x0926,0x0938,0x094D,0x0924,0x0020,0x090F,0x095C,
408 0x0020,0x0932,0x0917,0x093E,0x092F,0x0940,0x0020,0x0939,0x0948,0x002C,0x0020,0x0915,0x093F,0x0902,0x0924,0x0941,
409 0x0020,0x0907,0x0938,0x0915,0x0947,0x0020,0x0938,0x0930,0x092A,0x091F,0x0020,0x0926,0x094C,0x095C,0x0932,0x0917,
410 0x093E,0x0928,0x0947,0x0020,0x002E,0x0032,0x0915,0x0947,0x0020,0x002E,0x0032,0x0932,0x093F,0x090F,0x0020,0x002E,
411 0x0032,0x0915,0x094D,0x092F,0x093E,0x0020,0x002E,0x0032,0x0938,0x092A,0x093E,0x091F,0x0020,0x002E,0x0032,0x0930,
412 0x093E,0x0938,0x094D,0x0924,0x093E,0x0020,0x002E,0x0032,0x0909,0x092A,0x0932,0x092C,0x094D,0x0927,0x0020,0x002E,
413 0x0939,0x0948,0x002C,0x0020,0x002E,0x0905,0x0925,0x0935,0x093E,0x0020,0x002E,0x0935,0x093F,0x0936,0x094D,0x0935,
414 0x0020,0x002E,0x092E,0x0947,0x0902,0x0020,0x002E,0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x0020,
415 0x002E,0x0915,0x0940,0x0938,0x092B,0x0932,0x0924,0x093E,0x0020,0x002E,0x0033,0x0935,0x0020,0x002E,0x0033,0x0935,
416 0x093F,0x092B,0x0932,0x0924,0x093E,0x0020,0x002E,0x0033,0x0938,0x0947,0x0020,0x002E,0x0033,0x0938,0x092C,0x0915,
417 0x0020,0x002E,0x0033,0x0932,0x0947,0x0020,0x002E,0x0033,0x0915,0x0930,0x0020,0x002E,0x0033,0x0915,0x094D,0x092F,
418 0x093E,0x0020,0x002E,0x0033,0x0939,0x092E,0x0020,0x002E,0x0033,0x0907,0x0938,0x0915,0x093E,0x0020,0x002E,0x0033,
419 0x092F,0x0941,0x0915,0x094D,0x0924,0x093F,0x092A,0x0942,0x0930,0x094D,0x0923,0x0020,0x002E,0x0032,0x0935,0x093F,
420 0x0938,0x094D,0x0924,0x093E,0x0930,0x0020,0x0905,0x092A,0x0947,0x0915,0x094D,0x0937,0x093F,0x0924,0x0020,0x0915,
421 0x0930,0x0020,0x0938,0x0915,0x0947,0x0902,0x0917,0x0947,0x0020,0x003F,0x0020,
427 const char *testdatapath
= loadTestData(&error
);
428 resB
= ures_open(testdatapath
, "encoded", &error
);
429 srcFromRes
=tres_getString(resB
,-1,"str",&len
,&error
);
430 if(U_FAILURE(error
)){
431 log_data_err("Could not find encoded.res from test data bundle. Error: %s\n", u_errorName(error
));
435 if(u_strncmp(srcFromRes
,uSrc
,len
)!=0){
436 log_err("Genrb produced res files after decoding failed\n");
439 if(uSrc
[num
]!=srcFromRes
[num
]){
440 log_verbose(" Expected: 0x%04X Got: 0x%04X \n", uSrc
[num
],srcFromRes
[num
]);
444 if (len
!= u_strlen(uSrc
)) {
445 log_err("Genrb produced a string larger than expected\n");
450 static void TestNewTypes() {
451 UResourceBundle
* theBundle
= NULL
;
453 const char* testdatapath
;
454 UErrorCode status
= U_ZERO_ERROR
;
455 UResourceBundle
* res
= NULL
;
456 uint8_t *binResult
= NULL
;
459 int32_t intResult
= 0;
460 uint32_t uintResult
= 0;
461 const UChar
*empty
= NULL
;
462 const UChar
*zeroString
;
463 UChar expected
[] = { 'a','b','c','\0','d','e','f' };
464 const char* expect
="tab:\t cr:\r ff:\f newline:\n backslash:\\\\ quote=\\\' doubleQuote=\\\" singlequoutes=''";
467 testdatapath
=loadTestData(&status
);
469 if(U_FAILURE(status
))
471 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
475 theBundle
= ures_open(testdatapath
, "testtypes", &status
);
477 empty
= tres_getString(theBundle
, -1, "emptystring", &len
, &status
);
478 if(empty
&& (*empty
!= 0 || len
!= 0)) {
479 log_err("Empty string returned invalid value\n");
482 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
484 CONFIRM_INT_NE(theBundle
, NULL
);
486 /* This test reads the string "abc\u0000def" from the bundle */
487 /* if everything is working correctly, the size of this string */
488 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
490 strcpy(action
, "getting and testing of string with embeded zero");
491 res
= ures_getByKey(theBundle
, "zerotest", res
, &status
);
492 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
493 CONFIRM_INT_EQ(ures_getType(res
), URES_STRING
);
494 zeroString
=tres_getString(res
, -1, NULL
, &len
, &status
);
495 if(U_SUCCESS(status
)){
496 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
497 CONFIRM_INT_EQ(len
, 7);
498 CONFIRM_INT_NE(len
, 3);
501 if(zeroString
[i
]!= expected
[i
]){
502 log_verbose("Output did not match Expected: \\u%4X Got: \\u%4X", expected
[i
], zeroString
[i
]);
506 strcpy(action
, "getting and testing of binary type");
507 res
= ures_getByKey(theBundle
, "binarytest", res
, &status
);
508 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
509 CONFIRM_INT_EQ(ures_getType(res
), URES_BINARY
);
510 binResult
=(uint8_t*)ures_getBinary(res
, &len
, &status
);
511 if(U_SUCCESS(status
)){
512 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
513 CONFIRM_INT_EQ(len
, 15);
514 for(i
= 0; i
<15; i
++) {
515 CONFIRM_INT_EQ(binResult
[i
], i
);
519 strcpy(action
, "getting and testing of imported binary type");
520 res
= ures_getByKey(theBundle
, "importtest", res
, &status
);
521 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
522 CONFIRM_INT_EQ(ures_getType(res
), URES_BINARY
);
523 binResult
=(uint8_t*)ures_getBinary(res
, &len
, &status
);
524 if(U_SUCCESS(status
)){
525 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
526 CONFIRM_INT_EQ(len
, 15);
527 for(i
= 0; i
<15; i
++) {
528 CONFIRM_INT_EQ(binResult
[i
], i
);
532 strcpy(action
, "getting and testing of integer types");
533 res
= ures_getByKey(theBundle
, "one", res
, &status
);
534 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
535 CONFIRM_INT_EQ(ures_getType(res
), URES_INT
);
536 intResult
=ures_getInt(res
, &status
);
537 uintResult
= ures_getUInt(res
, &status
);
538 if(U_SUCCESS(status
)){
539 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
540 CONFIRM_INT_EQ(uintResult
, (uint32_t)intResult
);
541 CONFIRM_INT_EQ(intResult
, 1);
544 strcpy(action
, "getting minusone");
545 res
= ures_getByKey(theBundle
, "minusone", res
, &status
);
546 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
547 CONFIRM_INT_EQ(ures_getType(res
), URES_INT
);
548 intResult
=ures_getInt(res
, &status
);
549 uintResult
= ures_getUInt(res
, &status
);
550 if(U_SUCCESS(status
)){
551 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
552 CONFIRM_INT_EQ(uintResult
, 0x0FFFFFFF); /* a 28 bit integer */
553 CONFIRM_INT_EQ(intResult
, -1);
554 CONFIRM_INT_NE(uintResult
, (uint32_t)intResult
);
557 strcpy(action
, "getting plusone");
558 res
= ures_getByKey(theBundle
, "plusone", res
, &status
);
559 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
560 CONFIRM_INT_EQ(ures_getType(res
), URES_INT
);
561 intResult
=ures_getInt(res
, &status
);
562 uintResult
= ures_getUInt(res
, &status
);
563 if(U_SUCCESS(status
)){
564 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
565 CONFIRM_INT_EQ(uintResult
, (uint32_t)intResult
);
566 CONFIRM_INT_EQ(intResult
, 1);
569 res
= ures_getByKey(theBundle
, "onehundredtwentythree", res
, &status
);
570 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
571 CONFIRM_INT_EQ(ures_getType(res
), URES_INT
);
572 intResult
=ures_getInt(res
, &status
);
573 if(U_SUCCESS(status
)){
574 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
575 CONFIRM_INT_EQ(intResult
, 123);
578 /* this tests if escapes are preserved or not */
580 const UChar
* str
= tres_getString(theBundle
,-1,"testescape",&len
,&status
);
581 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
582 if(U_SUCCESS(status
)){
583 u_charsToUChars(expect
,uExpect
,(int32_t)strlen(expect
)+1);
584 if(u_strcmp(uExpect
,str
)){
585 log_err("Did not get the expected string for testescape\n");
589 /* this tests if unescaping works are expected */
592 char pattern
[2048] = "";
594 UChar
* expectedEscaped
;
598 /* This strcpy fixes compiler warnings about long strings */
599 strcpy(pattern
, "[ \\\\u0020 \\\\u00A0 \\\\u1680 \\\\u2000 \\\\u2001 \\\\u2002 \\\\u2003 \\\\u2004 \\\\u2005 \\\\u2006 \\\\u2007 "
600 "\\\\u2008 \\\\u2009 \\\\u200A \\u200B \\\\u202F \\u205F \\\\u3000 \\u0000-\\u001F \\u007F \\u0080-\\u009F "
601 "\\\\u06DD \\\\u070F \\\\u180E \\\\u200C \\\\u200D \\\\u2028 \\\\u2029 \\\\u2060 \\\\u2061 \\\\u2062 \\\\u2063 "
602 "\\\\u206A-\\\\u206F \\\\uFEFF \\\\uFFF9-\\uFFFC \\U0001D173-\\U0001D17A \\U000F0000-\\U000FFFFD "
603 "\\U00100000-\\U0010FFFD \\uFDD0-\\uFDEF \\uFFFE-\\uFFFF \\U0001FFFE-\\U0001FFFF \\U0002FFFE-\\U0002FFFF "
606 "\\U0003FFFE-\\U0003FFFF \\U0004FFFE-\\U0004FFFF \\U0005FFFE-\\U0005FFFF \\U0006FFFE-\\U0006FFFF "
607 "\\U0007FFFE-\\U0007FFFF \\U0008FFFE-\\U0008FFFF \\U0009FFFE-\\U0009FFFF \\U000AFFFE-\\U000AFFFF "
608 "\\U000BFFFE-\\U000BFFFF \\U000CFFFE-\\U000CFFFF \\U000DFFFE-\\U000DFFFF \\U000EFFFE-\\U000EFFFF "
609 "\\U000FFFFE-\\U000FFFFF \\U0010FFFE-\\U0010FFFF \\uD800-\\uDFFF \\\\uFFF9 \\\\uFFFA \\\\uFFFB "
610 "\\uFFFC \\uFFFD \\u2FF0-\\u2FFB \\u0340 \\u0341 \\\\u200E \\\\u200F \\\\u202A \\\\u202B \\\\u202C "
613 "\\\\u202D \\\\u202E \\\\u206A \\\\u206B \\\\u206C \\\\u206D \\\\u206E \\\\u206F \\U000E0001 \\U000E0020-\\U000E007F "
617 patternLen
= (int32_t)uprv_strlen(pattern
);
618 expectedEscaped
= (UChar
*)malloc(U_SIZEOF_UCHAR
* patternLen
);
619 got
= tres_getString(theBundle
,-1,"test_unescaping",&len
,&status
);
620 expectedLen
= u_unescape(pattern
,expectedEscaped
,patternLen
);
621 if(got
==NULL
|| u_strncmp(expectedEscaped
,got
,expectedLen
)!=0 || expectedLen
!= len
){
622 log_err("genrb failed to unescape string\n");
625 for(i
=0;i
<expectedLen
;i
++){
626 if(expectedEscaped
[i
] != got
[i
]){
627 log_verbose("Expected: 0x%04X Got: 0x%04X \n",expectedEscaped
[i
], got
[i
]);
631 free(expectedEscaped
);
632 status
= U_ZERO_ERROR
;
634 /* test for jitterbug#1435 */
636 const UChar
* str
= tres_getString(theBundle
,-1,"test_underscores",&len
,&status
);
637 expect
="test message ....";
638 u_charsToUChars(expect
,uExpect
,(int32_t)strlen(expect
)+1);
639 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
640 if(str
== NULL
|| u_strcmp(uExpect
,str
)){
641 log_err("Did not get the expected string for test_underscores.\n");
644 /* test for jitterbug#2626 */
645 #if !UCONFIG_NO_COLLATION
647 UResourceBundle
* resB
= NULL
;
648 const UChar
* str
= NULL
;
649 int32_t strLength
= 0;
650 const UChar my
[] = {0x0026,0x0027,0x0075,0x0027,0x0020,0x003d,0x0020,0x0027,0xff55,0x0027,0x0000}; /* &'\u0075' = '\uFF55' */
651 status
= U_ZERO_ERROR
;
652 resB
= ures_getByKey(theBundle
, "collations", resB
, &status
);
653 resB
= ures_getByKey(resB
, "standard", resB
, &status
);
654 str
= tres_getString(resB
,-1,"Sequence",&strLength
,&status
);
655 if(!str
|| U_FAILURE(status
)) {
656 log_data_err("Could not load collations from theBundle: %s\n", u_errorName(status
));
657 } else if(u_strcmp(my
,str
) != 0){
658 log_err("Did not get the expected string for escaped \\u0075\n");
664 const char *sourcePath
= ctest_dataSrcDir();
665 int32_t srcPathLen
= (int32_t)strlen(sourcePath
);
666 const char *deltaPath
= ".."U_FILE_SEP_STRING
"test"U_FILE_SEP_STRING
"testdata"U_FILE_SEP_STRING
;
667 int32_t deltaPathLen
= (int32_t)strlen(deltaPath
);
668 char *testDataFileName
= (char *) malloc( srcPathLen
+ deltaPathLen
+ 50 );
669 char *path
= testDataFileName
;
671 strcpy(path
, sourcePath
);
673 strcpy(path
, deltaPath
);
674 path
+= deltaPathLen
;
675 status
= U_ZERO_ERROR
;
678 const UChar
* str
= tres_getString(theBundle
, -1, "testincludeUTF",&strLen
,&status
);
679 strcpy(path
, "riwords.txt");
680 path
[strlen("riwords.txt")]=0;
681 if(U_FAILURE(status
)){
682 log_err("Could not get testincludeUTF resource from testtypes bundle. Error: %s\n",u_errorName(status
));
685 const char* cp
= NULL
;
686 UCHARBUF
* ucbuf
= ucbuf_open(testDataFileName
,&cp
,FALSE
,FALSE
,&status
);
688 if(U_SUCCESS(status
)){
689 const UChar
* buffer
= ucbuf_getBuffer(ucbuf
,&len
,&status
);
690 if(U_SUCCESS(status
)){
691 /* verify the contents */
693 log_err("Did not get the expected len for riwords. Expected: %i , Got: %i\n", len
,strLen
);
695 /* test string termination */
696 if(u_strlen(str
) != strLen
|| str
[strLen
]!= 0 ){
697 log_err("testinclude not null terminated!\n");
699 if(u_strncmp(str
, buffer
,strLen
)!=0){
700 log_err("Did not get the expected string from riwords. Include functionality failed for genrb.\n");
703 log_err("ucbuf failed to open %s. Error: %s\n", testDataFileName
, u_errorName(status
));
708 log_err("Could not get riwords.txt (path : %s). Error: %s\n",testDataFileName
,u_errorName(status
));
712 status
= U_ZERO_ERROR
;
715 const UChar
* str
= tres_getString(theBundle
, -1, "testinclude",&strLen
,&status
);
716 strcpy(path
, "translit_rules.txt");
717 path
[strlen("translit_rules.txt")]=0;
719 if(U_FAILURE(status
)){
720 log_err("Could not get testinclude resource from testtypes bundle. Error: %s\n",u_errorName(status
));
724 UCHARBUF
* ucbuf
= ucbuf_open(testDataFileName
,&cp
,FALSE
,FALSE
,&status
);
726 if(U_SUCCESS(status
)){
727 const UChar
* buffer
= ucbuf_getBuffer(ucbuf
,&len
,&status
);
728 if(U_SUCCESS(status
)){
729 /* verify the contents */
731 log_err("Did not get the expected len for translit_rules. Expected: %i , Got: %i\n", len
,strLen
);
733 if(u_strncmp(str
, buffer
,strLen
)!=0){
734 log_err("Did not get the expected string from translit_rules. Include functionality failed for genrb.\n");
737 log_err("ucbuf failed to open %s. Error: %s\n", testDataFileName
, u_errorName(status
));
741 log_err("Could not get translit_rules.txt (path : %s). Error: %s\n",testDataFileName
,u_errorName(status
));
745 free(testDataFileName
);
748 ures_close(theBundle
);
752 static void TestEmptyTypes() {
753 UResourceBundle
* theBundle
= NULL
;
755 const char* testdatapath
;
756 UErrorCode status
= U_ZERO_ERROR
;
757 UResourceBundle
* res
= NULL
;
758 UResourceBundle
* resArray
= NULL
;
759 const uint8_t *binResult
= NULL
;
761 int32_t intResult
= 0;
762 const UChar
*zeroString
;
763 const int32_t *zeroIntVect
= NULL
;
765 strcpy(action
, "Construction of testtypes bundle");
766 testdatapath
=loadTestData(&status
);
767 if(U_FAILURE(status
))
769 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
773 theBundle
= ures_open(testdatapath
, "testtypes", &status
);
775 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
777 CONFIRM_INT_NE(theBundle
, NULL
);
779 /* This test reads the string "abc\u0000def" from the bundle */
780 /* if everything is working correctly, the size of this string */
781 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
783 status
= U_ZERO_ERROR
;
784 strcpy(action
, "getting and testing of explicit string of zero length string");
785 res
= ures_getByKey(theBundle
, "emptyexplicitstring", res
, &status
);
786 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
787 CONFIRM_INT_EQ(ures_getType(res
), URES_STRING
);
788 zeroString
=tres_getString(res
, -1, NULL
, &len
, &status
);
789 if(U_SUCCESS(status
)){
790 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
791 CONFIRM_INT_EQ(len
, 0);
792 CONFIRM_INT_EQ(u_strlen(zeroString
), 0);
795 log_err("Couldn't get emptyexplicitstring\n");
798 status
= U_ZERO_ERROR
;
799 strcpy(action
, "getting and testing of normal string of zero length string");
800 res
= ures_getByKey(theBundle
, "emptystring", res
, &status
);
801 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
802 CONFIRM_INT_EQ(ures_getType(res
), URES_STRING
);
803 zeroString
=tres_getString(res
, -1, NULL
, &len
, &status
);
804 if(U_SUCCESS(status
)){
805 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
806 CONFIRM_INT_EQ(len
, 0);
807 CONFIRM_INT_EQ(u_strlen(zeroString
), 0);
810 log_err("Couldn't get emptystring\n");
813 status
= U_ZERO_ERROR
;
814 strcpy(action
, "getting and testing of empty int");
815 res
= ures_getByKey(theBundle
, "emptyint", res
, &status
);
816 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
817 CONFIRM_INT_EQ(ures_getType(res
), URES_INT
);
818 intResult
=ures_getInt(res
, &status
);
819 if(U_SUCCESS(status
)){
820 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
821 CONFIRM_INT_EQ(intResult
, 0);
824 log_err("Couldn't get emptystring\n");
827 status
= U_ZERO_ERROR
;
828 strcpy(action
, "getting and testing of zero length intvector");
829 res
= ures_getByKey(theBundle
, "emptyintv", res
, &status
);
830 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
831 CONFIRM_INT_EQ(ures_getType(res
), URES_INT_VECTOR
);
833 if(U_FAILURE(status
)){
834 log_err("Couldn't get emptyintv key %s\n", u_errorName(status
));
837 zeroIntVect
=ures_getIntVector(res
, &len
, &status
);
838 (void)zeroIntVect
; /* Suppress set but not used warning. */
839 if(!U_SUCCESS(status
) || resArray
!= NULL
|| len
!= 0) {
840 log_err("Shouldn't get emptyintv\n");
844 status
= U_ZERO_ERROR
;
845 strcpy(action
, "getting and testing of zero length emptybin");
846 res
= ures_getByKey(theBundle
, "emptybin", res
, &status
);
847 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
848 CONFIRM_INT_EQ(ures_getType(res
), URES_BINARY
);
850 if(U_FAILURE(status
)){
851 log_err("Couldn't get emptybin key %s\n", u_errorName(status
));
854 binResult
=ures_getBinary(res
, &len
, &status
);
855 (void)binResult
; /* Suppress set but not used warning. */
856 if(!U_SUCCESS(status
) || len
!= 0) {
857 log_err("Couldn't get emptybin, or it's not empty\n");
861 status
= U_ZERO_ERROR
;
862 strcpy(action
, "getting and testing of zero length emptyarray");
863 res
= ures_getByKey(theBundle
, "emptyarray", res
, &status
);
864 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
865 CONFIRM_INT_EQ(ures_getType(res
), URES_ARRAY
);
867 if(U_FAILURE(status
)){
868 log_err("Couldn't get emptyarray key %s\n", u_errorName(status
));
871 resArray
=ures_getByIndex(res
, 0, resArray
, &status
);
872 if(U_SUCCESS(status
) || resArray
!= NULL
){
873 log_err("Shouldn't get emptyarray[0]\n");
877 status
= U_ZERO_ERROR
;
878 strcpy(action
, "getting and testing of zero length emptytable");
879 res
= ures_getByKey(theBundle
, "emptytable", res
, &status
);
880 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
881 CONFIRM_INT_EQ(ures_getType(res
), URES_TABLE
);
883 if(U_FAILURE(status
)){
884 log_err("Couldn't get emptytable key %s\n", u_errorName(status
));
887 resArray
=ures_getByIndex(res
, 0, resArray
, &status
);
888 if(U_SUCCESS(status
) || resArray
!= NULL
){
889 log_err("Shouldn't get emptytable[0]\n");
894 ures_close(theBundle
);
897 static void TestEmptyBundle(){
898 UErrorCode status
= U_ZERO_ERROR
;
899 const char* testdatapath
=NULL
;
900 UResourceBundle
*resb
=0, *dResB
=0;
902 testdatapath
=loadTestData(&status
);
903 if(U_FAILURE(status
))
905 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
908 resb
= ures_open(testdatapath
, "testempty", &status
);
910 if(U_SUCCESS(status
)){
911 dResB
= ures_getByKey(resb
,"test",dResB
,&status
);
912 if(status
!= U_MISSING_RESOURCE_ERROR
){
913 log_err("Did not get the expected error from an empty resource bundle. Expected : %s Got: %s\n",
914 u_errorName(U_MISSING_RESOURCE_ERROR
),u_errorName(status
));
921 static void TestBinaryCollationData(){
922 #if !UCONFIG_NO_COLLATION
923 UErrorCode status
=U_ZERO_ERROR
;
924 const char* locale
="te";
925 const char* testdatapath
;
926 UResourceBundle
*teRes
= NULL
;
927 UResourceBundle
*coll
=NULL
;
928 UResourceBundle
*binColl
= NULL
;
929 uint8_t *binResult
= NULL
;
931 const char* action
="testing the binary collaton data";
933 log_verbose("Testing binary collation data resource......\n");
935 testdatapath
=loadTestData(&status
);
936 if(U_FAILURE(status
))
938 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
943 teRes
=ures_open(testdatapath
, locale
, &status
);
944 if(U_FAILURE(status
)){
945 log_err("ERROR: Failed to get resource for \"te\" with %s", myErrorName(status
));
949 coll
= ures_getByKey(teRes
, "collations", coll
, &status
);
950 coll
= ures_getByKey(coll
, "standard", coll
, &status
);
951 if(U_SUCCESS(status
)){
952 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
953 CONFIRM_INT_EQ(ures_getType(coll
), URES_TABLE
);
954 binColl
=ures_getByKey(coll
, "%%CollationBin", binColl
, &status
);
955 if(U_SUCCESS(status
)){
956 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
957 CONFIRM_INT_EQ(ures_getType(binColl
), URES_BINARY
);
958 binResult
=(uint8_t*)ures_getBinary(binColl
, &len
, &status
);
959 (void)binResult
; /* Suppress set but not used warning. */
960 if(U_SUCCESS(status
)){
961 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
962 CONFIRM_INT_GE(len
, 1);
966 log_err("ERROR: ures_getByKey(locale(te), %%CollationBin) failed\n");
970 log_err("ERROR: ures_getByKey(locale(te), collations) failed\n");
979 static void TestAPI() {
980 UErrorCode status
=U_ZERO_ERROR
;
982 const char* key
=NULL
;
983 const UChar
* value
=NULL
;
984 const char* testdatapath
;
985 UChar
* utestdatapath
=NULL
;
986 char convOutput
[256];
987 UChar largeBuffer
[1025];
988 UResourceBundle
*teRes
= NULL
;
989 UResourceBundle
*teFillin
=NULL
;
990 UResourceBundle
*teFillin2
=NULL
;
992 log_verbose("Testing ures_openU()......\n");
994 testdatapath
=loadTestData(&status
);
995 if(U_FAILURE(status
))
997 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
1000 len
=(int32_t)strlen(testdatapath
);
1001 utestdatapath
= (UChar
*) malloc((len
+10)*sizeof(UChar
));
1003 u_charsToUChars(testdatapath
, utestdatapath
, (int32_t)strlen(testdatapath
)+1);
1004 #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) && U_FILE_SEP_CHAR == '\\'
1006 /* Convert all backslashes to forward slashes so that we can make sure that ures_openU
1007 can handle invariant characters. */
1009 while ((backslash
= u_strchr(utestdatapath
, 0x005C))) {
1010 *backslash
= 0x002F;
1015 u_memset(largeBuffer
, 0x0030, UPRV_LENGTHOF(largeBuffer
));
1016 largeBuffer
[UPRV_LENGTHOF(largeBuffer
)-1] = 0;
1018 /*Test ures_openU */
1020 status
= U_ZERO_ERROR
;
1021 ures_close(ures_openU(largeBuffer
, "root", &status
));
1022 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1023 log_err("ERROR: ures_openU() worked when the path is very large. It returned %s\n", myErrorName(status
));
1026 status
= U_ZERO_ERROR
;
1027 ures_close(ures_openU(NULL
, "root", &status
));
1028 if(U_FAILURE(status
)){
1029 log_err_status(status
, "ERROR: ures_openU() failed path = NULL with %s\n", myErrorName(status
));
1032 status
= U_ILLEGAL_ARGUMENT_ERROR
;
1033 if(ures_openU(NULL
, "root", &status
) != NULL
){
1034 log_err("ERROR: ures_openU() worked with error status with %s\n", myErrorName(status
));
1037 status
= U_ZERO_ERROR
;
1038 teRes
=ures_openU(utestdatapath
, "te", &status
);
1039 if(U_FAILURE(status
)){
1040 log_err_status(status
, "ERROR: ures_openU() failed path =%s with %s\n", austrdup(utestdatapath
), myErrorName(status
));
1043 /*Test ures_getLocale() */
1044 log_verbose("Testing ures_getLocale() .....\n");
1045 if(strcmp(ures_getLocale(teRes
, &status
), "te") != 0){
1046 log_err("ERROR: ures_getLocale() failed. Expected = te_TE Got = %s\n", ures_getLocale(teRes
, &status
));
1048 /*Test ures_getNextString() */
1049 teFillin
=ures_getByKey(teRes
, "tagged_array_in_te_te_IN", teFillin
, &status
);
1050 key
=ures_getKey(teFillin
);
1051 value
=(UChar
*)ures_getNextString(teFillin
, &len
, &key
, &status
);
1052 (void)value
; /* Suppress set but not used warning. */
1053 ures_resetIterator(NULL
);
1054 value
=(UChar
*)ures_getNextString(teFillin
, &len
, &key
, &status
);
1055 if(status
!=U_INDEX_OUTOFBOUNDS_ERROR
){
1056 log_err("ERROR: calling getNextString where index out of bounds should return U_INDEX_OUTOFBOUNDS_ERROR, Got : %s\n",
1057 myErrorName(status
));
1059 ures_resetIterator(teRes
);
1060 /*Test ures_getNextResource() where resource is table*/
1061 status
=U_ZERO_ERROR
;
1062 #if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
1063 /* The next key varies depending on the charset. */
1064 teFillin
=ures_getNextResource(teRes
, teFillin
, &status
);
1065 if(U_FAILURE(status
)){
1066 log_err("ERROR: ures_getNextResource() failed \n");
1068 key
=ures_getKey(teFillin
);
1069 /*if(strcmp(key, "%%CollationBin") != 0){*/
1070 /*if(strcmp(key, "array_2d_in_Root_te") != 0){*/ /* added "aliasClient" that goes first */
1071 if(strcmp(key
, "a") != 0){
1072 log_err("ERROR: ures_getNextResource() failed\n");
1076 /*Test ures_getByIndex on string Resource*/
1077 teFillin
=ures_getByKey(teRes
, "string_only_in_te", teFillin
, &status
);
1078 teFillin2
=ures_getByIndex(teFillin
, 0, teFillin2
, &status
);
1079 if(U_FAILURE(status
)){
1080 log_err("ERROR: ures_getByIndex on string resource failed\n");
1082 if(strcmp(u_austrcpy(convOutput
, tres_getString(teFillin2
, -1, NULL
, &len
, &status
)), "TE") != 0){
1083 status
=U_ZERO_ERROR
;
1084 log_err("ERROR: ures_getByIndex on string resource fetched the key=%s, expected \"TE\" \n", austrdup(ures_getString(teFillin2
, &len
, &status
)));
1087 /*ures_close(teRes);*/
1089 /*Test ures_openFillIn*/
1090 log_verbose("Testing ures_openFillIn......\n");
1091 status
=U_ZERO_ERROR
;
1092 ures_openFillIn(teRes
, testdatapath
, "te", &status
);
1093 if(U_FAILURE(status
)){
1094 log_err("ERROR: ures_openFillIn failed\n");
1097 if(strcmp(ures_getLocale(teRes
, &status
), "te") != 0){
1098 log_err("ERROR: ures_openFillIn did not open the ResourceBundle correctly\n");
1100 ures_getByKey(teRes
, "string_only_in_te", teFillin
, &status
);
1101 teFillin2
=ures_getNextResource(teFillin
, teFillin2
, &status
);
1102 if(ures_getType(teFillin2
) != URES_STRING
){
1103 log_err("ERROR: getType for getNextResource after ures_openFillIn failed\n");
1105 teFillin2
=ures_getNextResource(teFillin
, teFillin2
, &status
);
1106 if(status
!=U_INDEX_OUTOFBOUNDS_ERROR
){
1107 log_err("ERROR: calling getNextResource where index out of bounds should return U_INDEX_OUTOFBOUNDS_ERROR, Got : %s\n",
1108 myErrorName(status
));
1111 ures_close(teFillin
);
1112 ures_close(teFillin2
);
1115 /* Test that ures_getLocale() returns the "real" locale ID */
1116 status
=U_ZERO_ERROR
;
1117 teRes
=ures_open(NULL
, "dE_At_NOWHERE_TO_BE_FOUND", &status
);
1118 if(U_FAILURE(status
)) {
1119 log_data_err("unable to open a locale resource bundle from \"dE_At_NOWHERE_TO_BE_FOUND\"(%s)\n", u_errorName(status
));
1121 if(0!=strcmp("de_AT", ures_getLocale(teRes
, &status
))) {
1122 log_data_err("ures_getLocale(\"dE_At_NOWHERE_TO_BE_FOUND\")=%s but must be de_AT\n", ures_getLocale(teRes
, &status
));
1127 /* same test, but with an aliased locale resource bundle */
1128 status
=U_ZERO_ERROR
;
1129 teRes
=ures_open(NULL
, "iW_Il_depRecaTed_HebreW", &status
);
1130 if(U_FAILURE(status
)) {
1131 log_data_err("unable to open a locale resource bundle from \"iW_Il_depRecaTed_HebreW\"(%s)\n", u_errorName(status
));
1133 if(0!=strcmp("he_IL", ures_getLocale(teRes
, &status
))) {
1134 log_data_err("ures_getLocale(\"iW_Il_depRecaTed_HebreW\")=%s but must be he_IL\n", ures_getLocale(teRes
, &status
));
1138 free(utestdatapath
);
1141 static void TestErrorConditions(){
1142 UErrorCode status
=U_ZERO_ERROR
;
1143 const char *key
=NULL
;
1144 const UChar
*value
=NULL
;
1145 const char* testdatapath
;
1146 UChar
* utestdatapath
;
1148 UResourceBundle
*teRes
= NULL
;
1149 UResourceBundle
*coll
=NULL
;
1150 UResourceBundle
*binColl
= NULL
;
1151 UResourceBundle
*teFillin
=NULL
;
1152 UResourceBundle
*teFillin2
=NULL
;
1153 uint8_t *binResult
= NULL
;
1157 testdatapath
= loadTestData(&status
);
1158 if(U_FAILURE(status
))
1160 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
1163 len
= (int32_t)strlen(testdatapath
);
1164 utestdatapath
= (UChar
*) malloc(sizeof(UChar
) *(len
+10));
1165 u_uastrcpy(utestdatapath
, testdatapath
);
1167 /*Test ures_openU with status != U_ZERO_ERROR*/
1168 log_verbose("Testing ures_openU() with status != U_ZERO_ERROR.....\n");
1169 status
=U_ILLEGAL_ARGUMENT_ERROR
;
1170 teRes
=ures_openU(utestdatapath
, "te", &status
);
1171 if(U_FAILURE(status
)){
1172 log_verbose("ures_openU() failed as expected path =%s with status != U_ZERO_ERROR\n", testdatapath
);
1174 log_err("ERROR: ures_openU() is supposed to fail path =%s with status != U_ZERO_ERROR\n", austrdup(utestdatapath
));
1177 /*Test ures_openFillIn with UResourceBundle = NULL*/
1178 log_verbose("Testing ures_openFillIn with UResourceBundle = NULL.....\n");
1179 status
=U_ZERO_ERROR
;
1180 ures_openFillIn(NULL
, testdatapath
, "te", &status
);
1181 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1182 log_err("ERROR: ures_openFillIn with UResourceBundle= NULL should fail. Expected U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1183 myErrorName(status
));
1185 /*Test ures_getLocale() with status != U_ZERO_ERROR*/
1186 status
=U_ZERO_ERROR
;
1187 teRes
=ures_openU(utestdatapath
, "te", &status
);
1188 if(U_FAILURE(status
)){
1189 log_err("ERROR: ures_openU() failed path =%s with %s\n", austrdup(utestdatapath
), myErrorName(status
));
1192 status
=U_ILLEGAL_ARGUMENT_ERROR
;
1193 if(ures_getLocale(teRes
, &status
) != NULL
){
1194 log_err("ERROR: ures_getLocale is supposed to fail with errorCode != U_ZERO_ERROR\n");
1196 /*Test ures_getLocale() with UResourceBundle = NULL*/
1197 status
=U_ZERO_ERROR
;
1198 if(ures_getLocale(NULL
, &status
) != NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1199 log_err("ERROR: ures_getLocale is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1200 myErrorName(status
));
1202 /*Test ures_getSize() with UResourceBundle = NULL */
1203 status
=U_ZERO_ERROR
;
1204 if(ures_getSize(NULL
) != 0){
1205 log_err("ERROR: ures_getSize() should return 0 when UResourceBundle=NULL. Got =%d\n", ures_getSize(NULL
));
1207 /*Test ures_getType() with UResourceBundle = NULL should return URES_NONE==-1*/
1208 status
=U_ZERO_ERROR
;
1209 if(ures_getType(NULL
) != URES_NONE
){
1210 log_err("ERROR: ures_getType() should return URES_NONE when UResourceBundle=NULL. Got =%d\n", ures_getType(NULL
));
1212 /*Test ures_getKey() with UResourceBundle = NULL*/
1213 status
=U_ZERO_ERROR
;
1214 if(ures_getKey(NULL
) != NULL
){
1215 log_err("ERROR: ures_getKey() should return NULL when UResourceBundle=NULL. Got =%d\n", ures_getKey(NULL
));
1217 /*Test ures_hasNext() with UResourceBundle = NULL*/
1218 status
=U_ZERO_ERROR
;
1219 if(ures_hasNext(NULL
) != FALSE
){
1220 log_err("ERROR: ures_hasNext() should return FALSE when UResourceBundle=NULL. Got =%d\n", ures_hasNext(NULL
));
1222 /*Test ures_get() with UResourceBundle = NULL*/
1223 status
=U_ZERO_ERROR
;
1224 if(ures_getStringByKey(NULL
, "string_only_in_te", &resultLen
, &status
) != NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1225 log_err("ERROR: ures_get is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1226 myErrorName(status
));
1228 /*Test ures_getByKey() with UResourceBundle = NULL*/
1229 status
=U_ZERO_ERROR
;
1230 teFillin
=ures_getByKey(NULL
, "string_only_in_te", teFillin
, &status
);
1231 if( teFillin
!= NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1232 log_err("ERROR: ures_getByKey is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1233 myErrorName(status
));
1235 /*Test ures_getByKey() with status != U_ZERO_ERROR*/
1236 teFillin
=ures_getByKey(NULL
, "string_only_in_te", teFillin
, &status
);
1237 if(teFillin
!= NULL
){
1238 log_err("ERROR: ures_getByKey is supposed to fail when errorCode != U_ZERO_ERROR\n");
1240 /*Test ures_getStringByKey() with UResourceBundle = NULL*/
1241 status
=U_ZERO_ERROR
;
1242 if(ures_getStringByKey(NULL
, "string_only_in_te", &len
, &status
) != NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1243 log_err("ERROR: ures_getStringByKey is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1244 myErrorName(status
));
1246 /*Test ures_getStringByKey() with status != U_ZERO_ERROR*/
1247 if(ures_getStringByKey(teRes
, "string_only_in_te", &len
, &status
) != NULL
){
1248 log_err("ERROR: ures_getStringByKey is supposed to fail when status != U_ZERO_ERROR. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1249 myErrorName(status
));
1251 /*Test ures_getString() with UResourceBundle = NULL*/
1252 status
=U_ZERO_ERROR
;
1253 if(ures_getString(NULL
, &len
, &status
) != NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1254 log_err("ERROR: ures_getString is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1255 myErrorName(status
));
1257 /*Test ures_getString() with status != U_ZERO_ERROR*/
1258 if(ures_getString(teRes
, &len
, &status
) != NULL
){
1259 log_err("ERROR: ures_getString is supposed to fail when status != U_ZERO_ERROR. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1260 myErrorName(status
));
1262 /*Test ures_getBinary() with UResourceBundle = NULL*/
1263 status
=U_ZERO_ERROR
;
1264 if(ures_getBinary(NULL
, &len
, &status
) != NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1265 log_err("ERROR: ures_getBinary is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1266 myErrorName(status
));
1268 /*Test ures_getBinary(0 status != U_ILLEGAL_ARGUMENT_ERROR*/
1269 status
=U_ZERO_ERROR
;
1270 coll
= ures_getByKey(teRes
, "collations", coll
, &status
);
1271 coll
= ures_getByKey(teRes
, "standard", coll
, &status
);
1272 binColl
=ures_getByKey(coll
, "%%CollationBin", binColl
, &status
);
1274 status
=U_ILLEGAL_ARGUMENT_ERROR
;
1275 binResult
=(uint8_t*)ures_getBinary(binColl
, &len
, &status
);
1276 if(binResult
!= NULL
){
1277 log_err("ERROR: ures_getBinary() with status != U_ZERO_ERROR is supposed to fail\n");
1280 /*Test ures_getNextResource() with status != U_ZERO_ERROR*/
1281 teFillin
=ures_getNextResource(teRes
, teFillin
, &status
);
1282 if(teFillin
!= NULL
){
1283 log_err("ERROR: ures_getNextResource() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1285 /*Test ures_getNextResource() with UResourceBundle = NULL*/
1286 status
=U_ZERO_ERROR
;
1287 teFillin
=ures_getNextResource(NULL
, teFillin
, &status
);
1288 if(teFillin
!= NULL
|| status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1289 log_err("ERROR: ures_getNextResource() with UResourceBundle = NULL is supposed to fail. Expected : U_IILEGAL_ARGUMENT_ERROR, Got : %s\n",
1290 myErrorName(status
));
1292 /*Test ures_getNextString with errorCode != U_ZERO_ERROR*/
1293 teFillin
=ures_getByKey(teRes
, "tagged_array_in_te_te_IN", teFillin
, &status
);
1294 key
=ures_getKey(teFillin
);
1295 status
= U_ILLEGAL_ARGUMENT_ERROR
;
1296 value
=(UChar
*)ures_getNextString(teFillin
, &len
, &key
, &status
);
1298 log_err("ERROR: ures_getNextString() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1300 /*Test ures_getNextString with UResourceBundle = NULL*/
1301 status
=U_ZERO_ERROR
;
1302 value
=(UChar
*)ures_getNextString(NULL
, &len
, &key
, &status
);
1303 if(value
!= NULL
|| status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1304 log_err("ERROR: ures_getNextString() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1305 myErrorName(status
));
1307 /*Test ures_getByIndex with errorCode != U_ZERO_ERROR*/
1308 status
=U_ZERO_ERROR
;
1309 teFillin
=ures_getByKey(teRes
, "array_only_in_te", teFillin
, &status
);
1310 if(ures_countArrayItems(teRes
, "array_only_in_te", &status
) != 4) {
1311 log_err("ERROR: Wrong number of items in an array!\n");
1313 status
=U_ILLEGAL_ARGUMENT_ERROR
;
1314 teFillin2
=ures_getByIndex(teFillin
, 0, teFillin2
, &status
);
1315 if(teFillin2
!= NULL
){
1316 log_err("ERROR: ures_getByIndex() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1318 /*Test ures_getByIndex with UResourceBundle = NULL */
1319 status
=U_ZERO_ERROR
;
1320 teFillin2
=ures_getByIndex(NULL
, 0, teFillin2
, &status
);
1321 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1322 log_err("ERROR: ures_getByIndex() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1323 myErrorName(status
));
1325 /*Test ures_getStringByIndex with errorCode != U_ZERO_ERROR*/
1326 status
=U_ZERO_ERROR
;
1327 teFillin
=ures_getByKey(teRes
, "array_only_in_te", teFillin
, &status
);
1328 status
=U_ILLEGAL_ARGUMENT_ERROR
;
1329 value
=(UChar
*)ures_getStringByIndex(teFillin
, 0, &len
, &status
);
1331 log_err("ERROR: ures_getSringByIndex() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1333 /*Test ures_getStringByIndex with UResourceBundle = NULL */
1334 status
=U_ZERO_ERROR
;
1335 value
=(UChar
*)ures_getStringByIndex(NULL
, 0, &len
, &status
);
1336 if(value
!= NULL
|| status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1337 log_err("ERROR: ures_getStringByIndex() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1338 myErrorName(status
));
1340 /*Test ures_getStringByIndex with UResourceBundle = NULL */
1341 status
=U_ZERO_ERROR
;
1342 value
=(UChar
*)ures_getStringByIndex(teFillin
, 9999, &len
, &status
);
1343 if(value
!= NULL
|| status
!= U_MISSING_RESOURCE_ERROR
){
1344 log_err("ERROR: ures_getStringByIndex() with index that is too big is supposed to fail\n Expected: U_MISSING_RESOURCE_ERROR, Got: %s\n",
1345 myErrorName(status
));
1347 /*Test ures_getInt() where UResourceBundle = NULL */
1348 status
=U_ZERO_ERROR
;
1349 if(ures_getInt(NULL
, &status
) != -1 && status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1350 log_err("ERROR: ures_getInt() with UResourceBundle = NULL should fail. Expected: U_IILEGAL_ARGUMENT_ERROR, Got: %s\n",
1351 myErrorName(status
));
1353 /*Test ures_getInt() where status != U_ZERO_ERROR */
1354 if(ures_getInt(teRes
, &status
) != -1){
1355 log_err("ERROR: ures_getInt() with errorCode != U_ZERO_ERROR should fail\n");
1358 ures_close(teFillin
);
1359 ures_close(teFillin2
);
1361 ures_close(binColl
);
1363 free(utestdatapath
);
1368 static void TestGetVersion(){
1369 UVersionInfo minVersionArray
= {0x01, 0x00, 0x00, 0x00};
1370 UVersionInfo maxVersionArray
= {0x50, 0xff, 0xcf, 0xcf};
1371 UVersionInfo versionArray
;
1372 UErrorCode status
= U_ZERO_ERROR
;
1373 UResourceBundle
* resB
= NULL
;
1375 int locCount
= uloc_countAvailable();
1376 const char *locName
= "root";
1378 log_verbose("The ures_getVersion tests begin : \n");
1380 for(j
= -1; j
< locCount
; j
++) {
1382 locName
= uloc_getAvailable(j
);
1384 log_verbose("Testing version number for locale %s\n", locName
);
1385 resB
= ures_open(NULL
,locName
, &status
);
1386 if (U_FAILURE(status
)) {
1387 log_err_status(status
, "Resource bundle creation for locale %s failed.: %s\n", locName
, myErrorName(status
));
1391 ures_getVersion(resB
, versionArray
);
1392 for (i
=0; i
<4; ++i
) {
1393 if (versionArray
[i
] < minVersionArray
[i
] ||
1394 versionArray
[i
] > maxVersionArray
[i
])
1396 log_err("Testing ures_getVersion(%-5s) - unexpected result: %d.%d.%d.%d\n",
1397 locName
, versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
1406 static void TestGetVersionColl(){
1407 #if !UCONFIG_NO_COLLATION
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
;
1415 const char *locName
= "root";
1417 const UChar
* rules
=NULL
;
1420 /* test NUL termination of UCARules */
1421 resB
= ures_open(U_ICUDATA_COLL
,locName
, &status
);
1422 rules
= tres_getString(resB
,-1,"UCARules",&len
, &status
);
1423 if(!rules
|| U_FAILURE(status
)) {
1424 log_data_err("Could not load UCARules for locale %s\n", locName
);
1425 status
= U_ZERO_ERROR
;
1426 } else if(u_strlen(rules
) != len
){
1427 log_err("UCARules string not nul terminated! \n");
1431 log_verbose("The ures_getVersion(%s) tests begin : \n", U_ICUDATA_COLL
);
1432 locs
= ures_openAvailableLocales(U_ICUDATA_COLL
, &status
);
1433 if (U_FAILURE(status
)) {
1434 log_err_status(status
, "enumeration of %s failed.: %s\n", U_ICUDATA_COLL
, myErrorName(status
));
1439 log_verbose("Testing version number for locale %s\n", locName
);
1440 resB
= ures_open(U_ICUDATA_COLL
,locName
, &status
);
1441 if (U_FAILURE(status
)) {
1442 log_err("Resource bundle creation for locale %s:%s failed.: %s\n", U_ICUDATA_COLL
, locName
, myErrorName(status
));
1446 ures_getVersion(resB
, versionArray
);
1447 for (i
=0; i
<4; ++i
) {
1448 if (versionArray
[i
] < minVersionArray
[i
] ||
1449 versionArray
[i
] > maxVersionArray
[i
])
1451 log_err("Testing ures_getVersion(%-5s) - unexpected result: %d.%d.%d.%d\n",
1452 locName
, versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
1457 locName
= uenum_next(locs
, &locLen
, &status
);
1458 if(U_FAILURE(status
)) {
1459 log_err("uenum_next(locs) error %s\n", u_errorName(status
));
1462 if(locName
== NULL
) {
1467 #endif /* !UCONFIG_NO_COLLATION */
1470 static void TestResourceBundles()
1472 UErrorCode status
= U_ZERO_ERROR
;
1473 loadTestData(&status
);
1474 if(U_FAILURE(status
)) {
1475 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
1479 testTag("only_in_Root", TRUE
, FALSE
, FALSE
);
1480 testTag("in_Root_te", TRUE
, TRUE
, FALSE
);
1481 testTag("in_Root_te_te_IN", TRUE
, TRUE
, TRUE
);
1482 testTag("in_Root_te_IN", TRUE
, FALSE
, TRUE
);
1483 testTag("only_in_te", FALSE
, TRUE
, FALSE
);
1484 testTag("only_in_te_IN", FALSE
, FALSE
, TRUE
);
1485 testTag("in_te_te_IN", FALSE
, TRUE
, TRUE
);
1486 testTag("nonexistent", FALSE
, FALSE
, FALSE
);
1488 log_verbose("Passed:= %d Failed= %d \n", pass
, fail
);
1493 static void TestConstruction1()
1495 UResourceBundle
*test1
= 0, *test2
= 0,*empty
= 0;
1496 const UChar
*result1
, *result2
;
1497 UErrorCode status
= U_ZERO_ERROR
;
1498 UErrorCode err
= U_ZERO_ERROR
;
1499 const char* locale
="te_IN";
1500 const char* testdatapath
;
1504 UVersionInfo versionInfo
;
1505 char versionString
[256];
1506 char verboseOutput
[256];
1508 U_STRING_DECL(rootVal
, "ROOT", 4);
1509 U_STRING_DECL(te_inVal
, "TE_IN", 5);
1511 U_STRING_INIT(rootVal
, "ROOT", 4);
1512 U_STRING_INIT(te_inVal
, "TE_IN", 5);
1514 testdatapath
=loadTestData(&status
);
1515 if(U_FAILURE(status
))
1517 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
1521 log_verbose("Testing ures_open()......\n");
1523 empty
= ures_open(testdatapath
, "testempty", &status
);
1524 if(empty
== NULL
|| U_FAILURE(status
)) {
1525 log_err("opening empty failed!\n");
1529 test1
=ures_open(testdatapath
, NULL
, &err
);
1533 log_err("construction of NULL did not succeed : %s \n", myErrorName(status
));
1536 test2
=ures_open(testdatapath
, locale
, &err
);
1539 log_err("construction of %s did not succeed : %s \n", locale
, myErrorName(status
));
1542 result1
= tres_getString(test1
, -1, "string_in_Root_te_te_IN", &len1
, &err
);
1543 result2
= tres_getString(test2
, -1, "string_in_Root_te_te_IN", &len2
, &err
);
1544 if (U_FAILURE(err
) || len1
==0 || len2
==0) {
1545 log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(status
));
1548 log_verbose("for string_in_Root_te_te_IN, default.txt had %s\n", u_austrcpy(verboseOutput
, result1
));
1549 log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", u_austrcpy(verboseOutput
, result2
));
1550 if(u_strcmp(result1
, rootVal
) !=0 || u_strcmp(result2
, te_inVal
) !=0 ){
1551 log_err("construction test failed. Run Verbose for more information");
1555 /* Test getVersionNumber*/
1556 log_verbose("Testing version number\n");
1557 log_verbose("for getVersionNumber : %s\n", ures_getVersionNumber(test1
));
1559 log_verbose("Testing version \n");
1560 ures_getVersion(test1
, versionInfo
);
1561 u_versionToString(versionInfo
, versionString
);
1563 log_verbose("for getVersion : %s\n", versionString
);
1565 if(strcmp(versionString
, ures_getVersionNumber(test1
)) != 0) {
1566 log_err("Versions differ: %s vs %s\n", versionString
, ures_getVersionNumber(test1
));
1574 /*****************************************************************************/
1575 /*****************************************************************************/
1577 static UBool
testTag(const char* frag
,
1582 int32_t failNum
= fail
;
1584 /* Make array from input params */
1587 const char *NAME
[] = { "ROOT", "TE", "TE_IN" };
1589 /* Now try to load the desired items */
1590 UResourceBundle
* theBundle
= NULL
;
1593 UErrorCode expected_status
,status
= U_ZERO_ERROR
,expected_resource_status
= U_ZERO_ERROR
;
1595 UChar
* expected_string
= NULL
;
1596 const UChar
* string
= NULL
;
1599 int32_t i
,j
,row
,col
, len
;
1600 int32_t actual_bundle
;
1602 int32_t row_count
=0;
1603 int32_t column_count
=0;
1605 int32_t tag_count
= 0;
1606 const char* testdatapath
;
1607 char verboseOutput
[256];
1608 UResourceBundle
* array
=NULL
;
1609 UResourceBundle
* array2d
=NULL
;
1610 UResourceBundle
* tags
=NULL
;
1611 UResourceBundle
* arrayItem1
=NULL
;
1613 testdatapath
= loadTestData(&status
);
1614 if(U_FAILURE(status
))
1616 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
1622 is_in
[2] = in_te_IN
;
1624 strcpy(item_tag
, "tag");
1626 for (i
=0; i
<bundles_count
; ++i
)
1628 strcpy(action
,"construction for ");
1629 strcat(action
, param
[i
].name
);
1632 status
= U_ZERO_ERROR
;
1634 theBundle
= ures_open(testdatapath
, param
[i
].name
, &status
);
1635 CONFIRM_ErrorCode(status
,param
[i
].expected_constructor_status
);
1638 actual_bundle
= 0; /* ne -> default */
1640 actual_bundle
= 1; /* te_NE -> te */
1642 actual_bundle
= 2; /* te_IN_NE -> te_IN */
1646 expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
1647 for (j
=e_te_IN
; j
>=e_Root
; --j
)
1649 if (is_in
[j
] && param
[i
].inherits
[j
])
1652 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
1653 expected_resource_status
= U_ZERO_ERROR
;
1655 expected_resource_status
= U_USING_DEFAULT_WARNING
;
1657 expected_resource_status
= U_USING_FALLBACK_WARNING
;
1659 log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>. actual_bundle=%s\n",
1664 is_in
[j
]?"Yes":"No",
1666 param
[i
].inherits
[j
]?"Yes":"No",
1667 param
[actual_bundle
].name
);
1673 for (j
=param
[i
].where
; j
>=0; --j
)
1681 base
=(UChar
*)malloc(sizeof(UChar
)*(strlen(NAME
[j
]) + 1));
1682 u_uastrcpy(base
,NAME
[j
]);
1691 base
= (UChar
*) malloc(sizeof(UChar
) * 1);
1696 /*----string---------------------------------------------------------------- */
1698 strcpy(tag
,"string_");
1701 strcpy(action
,param
[i
].name
);
1702 strcat(action
, ".ures_getStringByKey(" );
1704 strcat(action
, ")");
1707 status
= U_ZERO_ERROR
;
1710 string
=tres_getString(theBundle
, -1, tag
, &len
, &status
);
1711 if(U_SUCCESS(status
)) {
1712 expected_string
=(UChar
*)malloc(sizeof(UChar
)*(u_strlen(base
) + 4));
1713 u_strcpy(expected_string
,base
);
1714 CONFIRM_INT_EQ(len
, u_strlen(expected_string
));
1716 expected_string
= (UChar
*)malloc(sizeof(UChar
)*(u_strlen(kERROR
) + 1));
1717 u_strcpy(expected_string
,kERROR
);
1720 log_verbose("%s got %d, expected %d\n", action
, status
, expected_resource_status
);
1722 CONFIRM_ErrorCode(status
, expected_resource_status
);
1723 CONFIRM_EQ(string
, expected_string
);
1727 /*--------------array------------------------------------------------- */
1729 strcpy(tag
,"array_");
1732 strcpy(action
,param
[i
].name
);
1733 strcat(action
, ".ures_getByKey(" );
1735 strcat(action
, ")");
1739 count
= kERROR_COUNT
;
1740 status
= U_ZERO_ERROR
;
1741 array
=ures_getByKey(theBundle
, tag
, array
, &status
);
1742 CONFIRM_ErrorCode(status
,expected_resource_status
);
1743 if (U_SUCCESS(status
)) {
1744 /*confirm the resource type is an array*/
1745 CONFIRM_INT_EQ(ures_getType(array
), URES_ARRAY
);
1746 /*confirm the size*/
1747 count
=ures_getSize(array
);
1748 CONFIRM_INT_GE(count
,1);
1749 for (j
=0; j
<count
; ++j
) {
1751 u_strcpy(expected_string
, base
);
1752 u_uastrcpy(element
, itoa1(j
,buf
));
1753 u_strcat(expected_string
, element
);
1754 arrayItem1
=ures_getNextResource(array
, arrayItem1
, &status
);
1755 if(U_SUCCESS(status
)){
1756 CONFIRM_EQ(tres_getString(arrayItem1
, -1, NULL
, &len
, &status
),expected_string
);
1762 CONFIRM_INT_EQ(count
,kERROR_COUNT
);
1763 CONFIRM_ErrorCode(status
, U_MISSING_RESOURCE_ERROR
);
1764 /*CONFIRM_INT_EQ((int32_t)(unsigned long)array,(int32_t)0);*/
1768 /*--------------arrayItem------------------------------------------------- */
1770 strcpy(tag
,"array_");
1773 strcpy(action
,param
[i
].name
);
1774 strcat(action
, ".ures_getStringByIndex(");
1775 strcat(action
, tag
);
1776 strcat(action
, ")");
1779 for (j
=0; j
<10; ++j
){
1780 idx
= count
? (randi(count
* 3) - count
) : (randi(200) - 100);
1781 status
= U_ZERO_ERROR
;
1783 array
=ures_getByKey(theBundle
, tag
, array
, &status
);
1784 if(!U_FAILURE(status
)){
1786 t
=(UChar
*)ures_getStringByIndex(array
, idx
, &len
, &status
);
1787 if(!U_FAILURE(status
)){
1790 u_strcpy(expected_string
, base
);
1791 u_uastrcpy(element
, itoa1(idx
,buf
));
1792 u_strcat(expected_string
, element
);
1794 u_strcpy(expected_string
, kERROR
);
1798 expected_status
= (idx
>= 0 && idx
< count
) ? expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
1799 CONFIRM_ErrorCode(status
,expected_status
);
1800 CONFIRM_EQ(string
,expected_string
);
1805 /*--------------2dArray------------------------------------------------- */
1807 strcpy(tag
,"array_2d_");
1810 strcpy(action
,param
[i
].name
);
1811 strcat(action
, ".ures_getByKey(" );
1813 strcat(action
, ")");
1817 row_count
= kERROR_COUNT
, column_count
= kERROR_COUNT
;
1818 status
= U_ZERO_ERROR
;
1819 array2d
=ures_getByKey(theBundle
, tag
, array2d
, &status
);
1821 CONFIRM_ErrorCode(status
,expected_resource_status
);
1822 if (U_SUCCESS(status
))
1824 /*confirm the resource type is an 2darray*/
1825 CONFIRM_INT_EQ(ures_getType(array2d
), URES_ARRAY
);
1826 row_count
=ures_getSize(array2d
);
1827 CONFIRM_INT_GE(row_count
,1);
1829 for(row
=0; row
<row_count
; ++row
){
1830 UResourceBundle
*tableRow
=NULL
;
1831 tableRow
=ures_getByIndex(array2d
, row
, tableRow
, &status
);
1832 CONFIRM_ErrorCode(status
, expected_resource_status
);
1833 if(U_SUCCESS(status
)){
1834 /*confirm the resourcetype of each table row is an array*/
1835 CONFIRM_INT_EQ(ures_getType(tableRow
), URES_ARRAY
);
1836 column_count
=ures_getSize(tableRow
);
1837 CONFIRM_INT_GE(column_count
,1);
1839 for (col
=0; j
<column_count
; ++j
) {
1841 u_strcpy(expected_string
, base
);
1842 u_uastrcpy(element
, itoa1(row
, buf
));
1843 u_strcat(expected_string
, element
);
1844 u_uastrcpy(element
, itoa1(col
, buf
));
1845 u_strcat(expected_string
, element
);
1846 arrayItem1
=ures_getNextResource(tableRow
, arrayItem1
, &status
);
1847 if(U_SUCCESS(status
)){
1848 const UChar
*stringValue
=tres_getString(arrayItem1
, -1, NULL
, &len
, &status
);
1849 CONFIRM_EQ(stringValue
, expected_string
);
1853 ures_close(tableRow
);
1856 CONFIRM_INT_EQ(row_count
,kERROR_COUNT
);
1857 CONFIRM_INT_EQ(column_count
,kERROR_COUNT
);
1858 row_count
=column_count
=0;
1862 /*------2dArrayItem-------------------------------------------------------------- */
1864 for (j
=0; j
<10; ++j
)
1866 row
= row_count
? (randi(row_count
* 3) - row_count
) : (randi(200) - 100);
1867 col
= column_count
? (randi(column_count
* 3) - column_count
) : (randi(200) - 100);
1868 status
= U_ZERO_ERROR
;
1871 array2d
=ures_getByKey(theBundle
, tag
, array2d
, &status
);
1872 if(U_SUCCESS(status
)){
1873 UResourceBundle
*tableRow
=NULL
;
1874 tableRow
=ures_getByIndex(array2d
, row
, tableRow
, &status
);
1875 if(U_SUCCESS(status
)) {
1877 t
=(UChar
*)ures_getStringByIndex(tableRow
, col
, &len
, &status
);
1878 if(U_SUCCESS(status
)){
1882 ures_close(tableRow
);
1884 expected_status
= (row
>= 0 && row
< row_count
&& col
>= 0 && col
< column_count
) ?
1885 expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
1886 CONFIRM_ErrorCode(status
,expected_status
);
1888 if (U_SUCCESS(status
)){
1890 u_strcpy(expected_string
, base
);
1891 u_uastrcpy(element
, itoa1(row
, buf
));
1892 u_strcat(expected_string
, element
);
1893 u_uastrcpy(element
, itoa1(col
, buf
));
1894 u_strcat(expected_string
, element
);
1896 u_strcpy(expected_string
,kERROR
);
1898 CONFIRM_EQ(string
,expected_string
);
1903 /*--------------taggedArray----------------------------------------------- */
1904 strcpy(tag
,"tagged_array_");
1907 strcpy(action
,param
[i
].name
);
1908 strcat(action
,".ures_getByKey(");
1909 strcat(action
, tag
);
1913 status
= U_ZERO_ERROR
;
1915 tags
=ures_getByKey(theBundle
, tag
, tags
, &status
);
1916 CONFIRM_ErrorCode(status
, expected_resource_status
);
1917 if (U_SUCCESS(status
)) {
1918 UResType bundleType
=ures_getType(tags
);
1919 CONFIRM_INT_EQ(bundleType
, URES_TABLE
);
1921 tag_count
=ures_getSize(tags
);
1922 CONFIRM_INT_GE((int32_t)tag_count
, (int32_t)0);
1924 for(idx
=0; idx
<tag_count
; idx
++){
1925 UResourceBundle
*tagelement
=NULL
;
1926 const char *key
=NULL
;
1928 tagelement
=ures_getByIndex(tags
, idx
, tagelement
, &status
);
1929 key
=ures_getKey(tagelement
);
1930 value
=(UChar
*)ures_getNextString(tagelement
, &len
, &key
, &status
);
1931 log_verbose("tag = %s, value = %s\n", key
, u_austrcpy(verboseOutput
, value
));
1932 if(strncmp(key
, "tag", 3) == 0 && u_strncmp(value
, base
, u_strlen(base
)) == 0){
1937 ures_close(tagelement
);
1943 /*---------taggedArrayItem----------------------------------------------*/
1945 for (idx
=-20; idx
<20; ++idx
)
1948 status
= U_ZERO_ERROR
;
1950 strcpy(item_tag
, "tag");
1951 strcat(item_tag
, itoa1(idx
,buf
));
1952 tags
=ures_getByKey(theBundle
, tag
, tags
, &status
);
1953 if(U_SUCCESS(status
)){
1954 UResourceBundle
*tagelement
=NULL
;
1956 tagelement
=ures_getByKey(tags
, item_tag
, tagelement
, &status
);
1957 if(!U_FAILURE(status
)){
1958 UResType elementType
=ures_getType(tagelement
);
1959 CONFIRM_INT_EQ(elementType
, URES_STRING
);
1960 if(strcmp(ures_getKey(tagelement
), item_tag
) == 0){
1965 t
=(UChar
*)tres_getString(tagelement
, -1, NULL
, &len
, &status
);
1966 if(!U_FAILURE(status
)){
1971 CONFIRM_ErrorCode(status
,U_MISSING_RESOURCE_ERROR
);
1974 if (status
!= U_MISSING_RESOURCE_ERROR
) {
1976 u_strcpy(expected_string
, base
);
1977 u_uastrcpy(element
, itoa1(idx
,buf
));
1978 u_strcat(expected_string
, element
);
1979 CONFIRM_EQ(string
,expected_string
);
1983 ures_close(tagelement
);
1986 CONFIRM_INT_EQ(count
, tag_count
);
1988 free(expected_string
);
1989 ures_close(theBundle
);
1992 ures_close(array2d
);
1994 ures_close(arrayItem1
);
1996 return (UBool
)(failNum
== fail
);
1999 static void record_pass()
2004 static void record_fail()
2009 static void TestPreventFallback() {
2010 UResourceBundle
* theBundle
= NULL
;
2011 const char* testdatapath
;
2012 UErrorCode status
= U_ZERO_ERROR
;
2013 int32_t unused_len
= 0;
2015 testdatapath
=loadTestData(&status
);
2016 if(U_FAILURE(status
))
2018 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
2022 // In te_IN locale, fallback of string_in_te_no_te_IN_fallback is blocked
2023 // with the three empty-set (U+2205) chars.
2024 theBundle
= ures_open(testdatapath
, "te_IN_NE", &status
);
2025 if(U_FAILURE(status
))
2027 log_data_err("Could not open resource bundle te_IN_NE %s \n",myErrorName(status
));
2031 // Fallback is blocked
2032 ures_getStringByKeyWithFallback(theBundle
, "string_in_te_no_te_IN_fallback", &unused_len
, &status
);
2033 if (status
!= U_MISSING_RESOURCE_ERROR
)
2035 log_err("Expected missing resource error for string_in_te_no_te_IN_fallback.");
2037 status
= U_ZERO_ERROR
;
2039 // This fallback should succeed
2040 ures_getStringByKeyWithFallback(theBundle
, "string_only_in_te", &unused_len
, &status
);
2041 if(U_FAILURE(status
))
2043 log_err("Expected to find string_only_in_te %s \n",myErrorName(status
));
2045 status
= U_ZERO_ERROR
;
2046 ures_close(theBundle
);
2048 // From te locale, we should be able to fetch string_in_te_no_te_IN_fallback.
2049 theBundle
= ures_open(testdatapath
, "te", &status
);
2050 if(U_FAILURE(status
))
2052 log_data_err("Could not open resource bundle te_IN_NE %s \n",myErrorName(status
));
2055 ures_getStringByKeyWithFallback(theBundle
, "string_in_te_no_te_IN_fallback", &unused_len
, &status
);
2056 if(U_FAILURE(status
))
2058 log_err("Expected to find string_in_te_no_te_IN_fallback %s \n",myErrorName(status
));
2060 status
= U_ZERO_ERROR
;
2061 ures_close(theBundle
);
2065 * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR
2069 static void TestFallback()
2071 UErrorCode status
= U_ZERO_ERROR
;
2072 UResourceBundle
*fr_FR
= NULL
;
2073 UResourceBundle
*subResource
= NULL
;
2074 const UChar
*junk
; /* ignored */
2077 log_verbose("Opening fr_FR..");
2078 fr_FR
= ures_open(NULL
, "fr_FR", &status
);
2079 if(U_FAILURE(status
))
2081 log_err_status(status
, "Couldn't open fr_FR - %s\n", u_errorName(status
));
2085 status
= U_ZERO_ERROR
;
2088 /* clear it out.. just do some calls to get the gears turning */
2089 junk
= tres_getString(fr_FR
, -1, "LocaleID", &resultLen
, &status
);
2090 status
= U_ZERO_ERROR
;
2091 junk
= tres_getString(fr_FR
, -1, "LocaleString", &resultLen
, &status
);
2092 status
= U_ZERO_ERROR
;
2093 junk
= tres_getString(fr_FR
, -1, "LocaleID", &resultLen
, &status
);
2094 status
= U_ZERO_ERROR
;
2095 (void)junk
; /* Suppress set but not used warning. */
2097 /* OK first one. This should be a Default value. */
2098 subResource
= ures_getByKey(fr_FR
, "layout", NULL
, &status
);
2099 if(status
!= U_USING_DEFAULT_WARNING
)
2101 log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get layout from fr_FR, got %s\n",
2102 u_errorName(status
));
2105 status
= U_ZERO_ERROR
;
2106 ures_close(subResource
);
2108 /* and this is a Fallback, to fr */
2109 junk
= tres_getString(fr_FR
, -1, "ExemplarCharacters", &resultLen
, &status
);
2110 if(status
!= U_USING_FALLBACK_WARNING
)
2112 log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get ExemplarCharacters from fr_FR, got %d\n",
2116 status
= U_ZERO_ERROR
;
2119 /* Temporary hack err actually should be U_USING_FALLBACK_ERROR */
2120 /* Test Jitterbug 552 fallback mechanism of aliased data */
2122 UErrorCode err
=U_ZERO_ERROR
;
2123 UResourceBundle
* myResB
= ures_open(NULL
,"no_NO_NY",&err
);
2124 UResourceBundle
* resLocID
= ures_getByKey(myResB
, "Version", NULL
, &err
);
2125 UResourceBundle
* tResB
;
2126 UResourceBundle
* zoneResource
;
2127 const UChar
* version
= NULL
;
2128 static const UChar versionStr
[] = { 0x0032, 0x002E, 0x0031, 0x002E, 0x0031, 0x0039, 0x002E, 0x0031, 0x0034, 0x0000}; // 2.1.19.14
2130 if(err
!= U_ZERO_ERROR
){
2131 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
));
2134 version
= tres_getString(resLocID
, -1, NULL
, &resultLen
, &err
);
2135 if(u_strcmp(version
, versionStr
) != 0){
2138 u_austrcpy(x
, versionStr
);
2139 u_austrcpy(g
, version
);
2140 log_data_err("ures_getString(resLocID, &resultLen, &err) returned an unexpected version value. Expected '%s', but got '%s'\n",
2143 zoneResource
= ures_open(U_ICUDATA_ZONE
, "no_NO_NY", &err
);
2144 tResB
= ures_getByKey(zoneResource
, "zoneStrings", NULL
, &err
);
2145 if(err
!= U_USING_FALLBACK_WARNING
){
2146 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
));
2149 ures_close(zoneResource
);
2150 ures_close(resLocID
);
2156 /* static void printUChars(UChar* uchars){
2158 / for(i=0; i<u_strlen(uchars); i++){
2159 / log_err("%04X ", *(uchars+i));
2163 static void TestResourceLevelAliasing(void) {
2164 UErrorCode status
= U_ZERO_ERROR
;
2165 UResourceBundle
*aliasB
= NULL
, *tb
= NULL
;
2166 UResourceBundle
*en
= NULL
, *uk
= NULL
, *testtypes
= NULL
;
2167 const char* testdatapath
= NULL
;
2168 const UChar
*string
= NULL
, *sequence
= NULL
;
2169 /*const uint8_t *binary = NULL, *binSequence = NULL;*/
2170 int32_t strLen
= 0, seqLen
= 0;/*, binLen = 0, binSeqLen = 0;*/
2174 testdatapath
=loadTestData(&status
);
2175 if(U_FAILURE(status
))
2177 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
2181 aliasB
= ures_open(testdatapath
, "testaliases", &status
);
2183 if(U_FAILURE(status
))
2185 log_data_err("Could not load testaliases.res %s \n",myErrorName(status
));
2188 /* this should fail - circular alias */
2189 tb
= ures_getByKey(aliasB
, "aaa", tb
, &status
);
2190 if(status
!= U_TOO_MANY_ALIASES_ERROR
) {
2191 log_err("Failed to detect circular alias\n");
2194 status
= U_ZERO_ERROR
;
2196 tb
= ures_getByKey(aliasB
, "aab", tb
, &status
);
2197 if(status
!= U_TOO_MANY_ALIASES_ERROR
) {
2198 log_err("Failed to detect circular alias\n");
2200 status
= U_ZERO_ERROR
;
2202 if(U_FAILURE(status
) ) {
2203 log_data_err("err loading tb resource\n");
2205 /* testing aliasing to a non existing resource */
2206 tb
= ures_getByKey(aliasB
, "nonexisting", tb
, &status
);
2207 if(status
!= U_MISSING_RESOURCE_ERROR
) {
2208 log_err("Managed to find an alias to non-existing resource\n");
2210 status
= U_ZERO_ERROR
;
2212 /* testing referencing/composed alias */
2213 uk
= ures_findResource("ja/calendar/gregorian/DateTimePatterns/2", uk
, &status
);
2214 if((uk
== NULL
) || U_FAILURE(status
)) {
2215 log_err_status(status
, "Couldn't findResource('ja/calendar/gregorian/DateTimePatterns/2') err %s\n", u_errorName(status
));
2219 sequence
= tres_getString(uk
, -1, NULL
, &seqLen
, &status
);
2221 tb
= ures_getByKey(aliasB
, "referencingalias", tb
, &status
);
2222 string
= tres_getString(tb
, -1, NULL
, &strLen
, &status
);
2224 if(seqLen
!= strLen
|| u_strncmp(sequence
, string
, seqLen
) != 0) {
2225 log_err("Referencing alias didn't get the right string (1)\n");
2228 string
= tres_getString(aliasB
, -1, "referencingalias", &strLen
, &status
);
2229 if(seqLen
!= strLen
|| u_strncmp(sequence
, string
, seqLen
) != 0) {
2230 log_err("Referencing alias didn't get the right string (2)\n");
2233 checkStatus(__LINE__
, U_ZERO_ERROR
, status
);
2234 tb
= ures_getByKey(aliasB
, "DateTimePatterns", tb
, &status
);
2235 checkStatus(__LINE__
, U_ZERO_ERROR
, status
);
2236 tb
= ures_getByIndex(tb
, 2, tb
, &status
);
2237 checkStatus(__LINE__
, U_ZERO_ERROR
, status
);
2238 string
= tres_getString(tb
, -1, NULL
, &strLen
, &status
);
2239 checkStatus(__LINE__
, U_ZERO_ERROR
, status
);
2241 if(U_FAILURE(status
)) {
2242 log_err("%s trying to get string via separate getters\n", u_errorName(status
));
2243 } else if(seqLen
!= strLen
|| u_strncmp(sequence
, string
, seqLen
) != 0) {
2244 log_err("Referencing alias didn't get the right string (3)\n");
2248 testtypes
= ures_open(testdatapath
, "testtypes", &status
);
2249 strcpy(buffer
, "menu/file/open");
2251 uk
= ures_findSubResource(testtypes
, s
, uk
, &status
);
2252 sequence
= tres_getString(uk
, -1, NULL
, &seqLen
, &status
);
2254 tb
= ures_getByKey(aliasB
, "simplealias", tb
, &status
);
2255 string
= tres_getString(tb
, -1, NULL
, &strLen
, &status
);
2257 if(U_FAILURE(status
) || seqLen
!= strLen
|| u_strncmp(sequence
, string
, seqLen
) != 0) {
2258 log_err("Referencing alias didn't get the right string (4)\n");
2261 /* test indexed aliasing */
2263 tb
= ures_getByKey(aliasB
, "zoneTests", tb
, &status
);
2264 tb
= ures_getByKey(tb
, "zoneAlias2", tb
, &status
);
2265 string
= tres_getString(tb
, -1, NULL
, &strLen
, &status
);
2267 en
= ures_findResource("/ICUDATA-zone/en/zoneStrings/3/0", en
, &status
);
2268 sequence
= tres_getString(en
, -1, NULL
, &seqLen
, &status
);
2270 if(U_FAILURE(status
) || seqLen
!= strLen
|| u_strncmp(sequence
, string
, seqLen
) != 0) {
2271 log_err("Referencing alias didn't get the right string (5)\n");
2274 /* test getting aliased string by index */
2276 const char* keys
[] = {
2278 "KeyAlias1PacificStandardTime",
2280 "KeyAlias3LosAngeles"
2283 const char* strings
[] = {
2284 "America/Los_Angeles",
2285 "Pacific Standard Time",
2290 const UChar
* result
;
2291 int32_t uBufferLen
= 0, resultLen
= 0;
2293 const char *key
= NULL
;
2294 tb
= ures_getByKey(aliasB
, "testGetStringByKeyAliasing", tb
, &status
);
2295 if(U_FAILURE(status
)) {
2296 log_err("FAIL: Couldn't get testGetStringByKeyAliasing resource: %s\n", u_errorName(status
));
2298 for(i
= 0; i
< UPRV_LENGTHOF(strings
); i
++) {
2299 result
= tres_getString(tb
, -1, keys
[i
], &resultLen
, &status
);
2300 if(U_FAILURE(status
)){
2301 log_err("(1) Fetching the resource with key %s failed. Error: %s\n", keys
[i
], u_errorName(status
));
2304 uBufferLen
= u_unescape(strings
[i
], uBuffer
, 256);
2305 if(resultLen
!= uBufferLen
|| u_strncmp(result
, uBuffer
, resultLen
) != 0) {
2306 log_err("(1) Didn't get correct string while accessing alias table by key (%s)\n", keys
[i
]);
2309 for(i
= 0; i
< UPRV_LENGTHOF(strings
); i
++) {
2310 result
= tres_getString(tb
, i
, NULL
, &resultLen
, &status
);
2311 if(U_FAILURE(status
)){
2312 log_err("(2) Fetching the resource with key %s failed. Error: %s\n", keys
[i
], u_errorName(status
));
2315 uBufferLen
= u_unescape(strings
[i
], uBuffer
, 256);
2316 if(result
==NULL
|| resultLen
!= uBufferLen
|| u_strncmp(result
, uBuffer
, resultLen
) != 0) {
2317 log_err("(2) Didn't get correct string while accesing alias table by index (%s)\n", strings
[i
]);
2320 for(i
= 0; i
< UPRV_LENGTHOF(strings
); i
++) {
2321 result
= ures_getNextString(tb
, &resultLen
, &key
, &status
);
2322 if(U_FAILURE(status
)){
2323 log_err("(3) Fetching the resource with key %s failed. Error: %s\n", keys
[i
], u_errorName(status
));
2326 uBufferLen
= u_unescape(strings
[i
], uBuffer
, 256);
2327 if(result
==NULL
|| resultLen
!= uBufferLen
|| u_strncmp(result
, uBuffer
, resultLen
) != 0) {
2328 log_err("(3) Didn't get correct string while iterating over alias table (%s)\n", strings
[i
]);
2332 tb
= ures_getByKey(aliasB
, "testGetStringByIndexAliasing", tb
, &status
);
2333 if(U_FAILURE(status
)) {
2334 log_err("FAIL: Couldn't get testGetStringByIndexAliasing resource: %s\n", u_errorName(status
));
2336 for(i
= 0; i
< UPRV_LENGTHOF(strings
); i
++) {
2337 result
= tres_getString(tb
, i
, NULL
, &resultLen
, &status
);
2338 if(U_FAILURE(status
)){
2339 log_err("Fetching the resource with key %s failed. Error: %s\n", keys
[i
], u_errorName(status
));
2342 uBufferLen
= u_unescape(strings
[i
], uBuffer
, 256);
2343 if(result
==NULL
|| resultLen
!= uBufferLen
|| u_strncmp(result
, uBuffer
, resultLen
) != 0) {
2344 log_err("Didn't get correct string while accesing alias by index in an array (%s)\n", strings
[i
]);
2347 for(i
= 0; i
< UPRV_LENGTHOF(strings
); i
++) {
2348 result
= ures_getNextString(tb
, &resultLen
, &key
, &status
);
2349 if(U_FAILURE(status
)){
2350 log_err("Fetching the resource with key %s failed. Error: %s\n", keys
[i
], u_errorName(status
));
2353 uBufferLen
= u_unescape(strings
[i
], uBuffer
, 256);
2354 if(result
==NULL
|| resultLen
!= uBufferLen
|| u_strncmp(result
, uBuffer
, resultLen
) != 0) {
2355 log_err("Didn't get correct string while iterating over aliases in an array (%s)\n", strings
[i
]);
2360 tb
= ures_getByKey(aliasB
, "testAliasToTree", tb
, &status
);
2361 if(U_FAILURE(status
)){
2362 log_err("Fetching the resource with key \"testAliasToTree\" failed. Error: %s\n", u_errorName(status
));
2365 if (strcmp(ures_getKey(tb
), "collations") != 0) {
2366 log_err("ures_getKey(aliasB) unexpectedly returned %s instead of \"collations\"\n", ures_getKey(tb
));
2373 ures_close(testtypes
);
2376 static void TestDirectAccess(void) {
2377 UErrorCode status
= U_ZERO_ERROR
;
2378 UResourceBundle
*t
= NULL
, *t2
= NULL
;
2379 const char* key
= NULL
;
2383 /*const char* testdatapath=loadTestData(&status);
2384 if(U_FAILURE(status)){
2385 log_err("Could not load testdata.dat %s \n",myErrorName(status));
2389 t
= ures_findResource("/testdata/te/zoneStrings/3/2", t
, &status
);
2390 if(U_FAILURE(status
)) {
2391 log_data_err("Couldn't access indexed resource, error %s\n", u_errorName(status
));
2392 status
= U_ZERO_ERROR
;
2394 key
= ures_getKey(t
);
2396 log_err("Got a strange key, expected NULL, got %s\n", key
);
2399 t
= ures_findResource("en/calendar/gregorian/DateTimePatterns/3", t
, &status
);
2400 if(U_FAILURE(status
)) {
2401 log_data_err("Couldn't access indexed resource, error %s\n", u_errorName(status
));
2402 status
= U_ZERO_ERROR
;
2404 key
= ures_getKey(t
);
2406 log_err("Got a strange key, expected NULL, got %s\n", key
);
2410 t
= ures_findResource("ja/ExemplarCharacters", t
, &status
);
2411 if(U_FAILURE(status
)) {
2412 log_data_err("Couldn't access keyed resource, error %s\n", u_errorName(status
));
2413 status
= U_ZERO_ERROR
;
2415 key
= ures_getKey(t
);
2416 if(strcmp(key
, "ExemplarCharacters")!=0) {
2417 log_err("Got a strange key, expected 'ExemplarCharacters', got %s\n", key
);
2421 t2
= ures_open(U_ICUDATA_LANG
, "sr", &status
);
2422 if(U_FAILURE(status
)) {
2423 log_err_status(status
, "Couldn't open 'sr' resource bundle, error %s\n", u_errorName(status
));
2424 log_data_err("No 'sr', no test - you have bigger problems than testing direct access. "
2425 "You probably have no data! Aborting this test\n");
2428 if(U_SUCCESS(status
)) {
2429 strcpy(buffer
, "Languages/hr");
2431 t
= ures_findSubResource(t2
, s
, t
, &status
);
2432 if(U_FAILURE(status
)) {
2433 log_err("Couldn't access keyed resource, error %s\n", u_errorName(status
));
2434 status
= U_ZERO_ERROR
;
2436 key
= ures_getKey(t
);
2437 if(strcmp(key
, "hr")!=0) {
2438 log_err("Got a strange key, expected 'hr', got %s\n", key
);
2443 t
= ures_findResource("root/calendar/islamic-civil/DateTime", t
, &status
);
2444 if(U_SUCCESS(status
)) {
2445 log_data_err("This resource does not exist. How did it get here?\n");
2447 status
= U_ZERO_ERROR
;
2449 /* this one will freeze */
2450 t
= ures_findResource("root/calendar/islamic-civil/eras/abbreviated/0/mikimaus/pera", t
, &status
);
2451 if(U_SUCCESS(status
)) {
2452 log_data_err("Second resource does not exist. How did it get here?\n");
2454 status
= U_ZERO_ERROR
;
2457 t2
= ures_open(NULL
, "he", &status
);
2458 t2
= ures_getByKeyWithFallback(t2
, "calendar", t2
, &status
);
2459 t2
= ures_getByKeyWithFallback(t2
, "islamic-civil", t2
, &status
);
2460 t2
= ures_getByKeyWithFallback(t2
, "DateTime", t2
, &status
);
2461 if(U_SUCCESS(status
)) {
2462 log_err("This resource does not exist. How did it get here?\n");
2464 status
= U_ZERO_ERROR
;
2467 t2
= ures_open(NULL
, "he", &status
);
2469 t2
= ures_getByKeyWithFallback(t2
, "calendar", t2
, &status
);
2470 t2
= ures_getByKeyWithFallback(t2
, "islamic-civil", t2
, &status
);
2471 t2
= ures_getByKeyWithFallback(t2
, "eras", t2
, &status
);
2472 if(U_FAILURE(status
)) {
2473 log_err_status(status
, "Didn't get Eras. I know they are there!\n");
2475 status
= U_ZERO_ERROR
;
2478 t2
= ures_open(NULL
, "root", &status
);
2479 t2
= ures_getByKeyWithFallback(t2
, "calendar", t2
, &status
);
2480 t2
= ures_getByKeyWithFallback(t2
, "islamic-civil", t2
, &status
);
2481 t2
= ures_getByKeyWithFallback(t2
, "DateTime", t2
, &status
);
2482 if(U_SUCCESS(status
)) {
2483 log_err("This resource does not exist. How did it get here?\n");
2485 status
= U_ZERO_ERROR
;
2491 static void TestTicket9804(void) {
2492 UErrorCode status
= U_ZERO_ERROR
;
2493 UResourceBundle
*t
= NULL
;
2494 t
= ures_open(NULL
, "he", &status
);
2495 t
= ures_getByKeyWithFallback(t
, "calendar/islamic-civil/DateTime", t
, &status
);
2496 if(U_SUCCESS(status
)) {
2497 log_err("This resource does not exist. How did it get here?\n");
2499 status
= U_ZERO_ERROR
;
2501 t
= ures_open(NULL
, "he", &status
);
2502 t
= ures_getByKeyWithFallback(t
, "calendar/islamic-civil/eras", t
, &status
);
2503 if(U_FAILURE(status
)) {
2504 log_err_status(status
, "Didn't get Eras. I know they are there!\n");
2506 const char *locale
= ures_getLocaleByType(t
, ULOC_ACTUAL_LOCALE
, &status
);
2507 if (uprv_strcmp("he", locale
) != 0) {
2508 log_err("Eras should be in the 'he' locale, but was in: %s", locale
);
2511 status
= U_ZERO_ERROR
;
2515 static void TestJB3763(void) {
2516 /* Nasty bug prevented using parent as fill-in, since it would
2517 * stomp the path information.
2519 UResourceBundle
*t
= NULL
;
2520 UErrorCode status
= U_ZERO_ERROR
;
2521 t
= ures_open(NULL
, "sr_Latn", &status
);
2522 t
= ures_getByKeyWithFallback(t
, "calendar", t
, &status
);
2523 t
= ures_getByKeyWithFallback(t
, "gregorian", t
, &status
);
2524 t
= ures_getByKeyWithFallback(t
, "AmPmMarkers", t
, &status
);
2525 if(U_FAILURE(status
)) {
2526 log_err_status(status
, "This resource should be available?\n");
2528 status
= U_ZERO_ERROR
;
2534 static void TestGetKeywordValues(void) {
2535 UEnumeration
*kwVals
;
2536 UBool foundStandard
= FALSE
;
2537 UErrorCode status
= U_ZERO_ERROR
;
2539 #if !UCONFIG_NO_COLLATION
2540 kwVals
= ures_getKeywordValues( U_ICUDATA_COLL
, "collations", &status
);
2542 log_verbose("Testing getting collation keyword values:\n");
2544 while((kw
=uenum_next(kwVals
, NULL
, &status
))) {
2545 log_verbose(" %s\n", kw
);
2546 if(!strcmp(kw
,"standard")) {
2547 if(foundStandard
== FALSE
) {
2548 foundStandard
= TRUE
;
2550 log_err("'standard' was found twice in the keyword list.\n");
2554 if(foundStandard
== FALSE
) {
2555 log_err_status(status
, "'standard' was not found in the keyword list.\n");
2557 uenum_close(kwVals
);
2558 if(U_FAILURE(status
)) {
2559 log_err_status(status
, "err %s getting collation values\n", u_errorName(status
));
2561 status
= U_ZERO_ERROR
;
2563 foundStandard
= FALSE
;
2564 kwVals
= ures_getKeywordValues( "ICUDATA", "calendar", &status
);
2566 log_verbose("Testing getting calendar keyword values:\n");
2568 while((kw
=uenum_next(kwVals
, NULL
, &status
))) {
2569 log_verbose(" %s\n", kw
);
2570 if(!strcmp(kw
,"japanese")) {
2571 if(foundStandard
== FALSE
) {
2572 foundStandard
= TRUE
;
2574 log_err("'japanese' was found twice in the calendar keyword list.\n");
2578 if(foundStandard
== FALSE
) {
2579 log_err_status(status
, "'japanese' was not found in the calendar keyword list.\n");
2581 uenum_close(kwVals
);
2582 if(U_FAILURE(status
)) {
2583 log_err_status(status
, "err %s getting calendar values\n", u_errorName(status
));
2587 static void TestGetFunctionalEquivalentOf(const char *path
, const char *resName
, const char *keyword
, UBool truncate
, const char * const testCases
[]) {
2589 for(i
=0;testCases
[i
];i
+=3) {
2590 UBool expectAvail
= (testCases
[i
][0]=='t')?TRUE
:FALSE
;
2591 UBool gotAvail
= FALSE
;
2592 const char *inLocale
= testCases
[i
+1];
2593 const char *expectLocale
= testCases
[i
+2];
2594 char equivLocale
[256];
2596 UErrorCode status
= U_ZERO_ERROR
;
2597 log_verbose("%d: %c %s\texpect %s\n",i
/3, expectAvail
?'t':'f', inLocale
, expectLocale
);
2598 len
= ures_getFunctionalEquivalent(equivLocale
, 255, path
,
2599 resName
, keyword
, inLocale
,
2600 &gotAvail
, truncate
, &status
);
2601 if(U_FAILURE(status
) || (len
<= 0)) {
2602 log_err_status(status
, "FAIL: got len %d, err %s on #%d: %c\t%s\t%s\n",
2603 len
, u_errorName(status
),
2604 i
/3,expectAvail
?'t':'f', inLocale
, expectLocale
);
2606 log_verbose("got: %c %s\n", expectAvail
?'t':'f',equivLocale
);
2608 if((gotAvail
!= expectAvail
) || strcmp(equivLocale
, expectLocale
)) {
2609 log_err("FAIL: got avail=%c, loc=%s but expected #%d: %c\t%s\t-> loc=%s\n",
2610 gotAvail
?'t':'f', equivLocale
,
2612 expectAvail
?'t':'f', inLocale
, expectLocale
);
2619 static void TestGetFunctionalEquivalent(void) {
2620 #if !UCONFIG_NO_COLLATION
2621 static const char * const collCases
[] = {
2622 /* avail locale equiv */
2623 "f", "sv_US_CALIFORNIA", "sv",
2624 "f", "zh_TW@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_TW */
2625 "f", "zh_Hant_TW@collation=stroke", "zh@collation=stroke",
2626 "f", "sv_CN@collation=pinyin", "sv",
2627 "t", "zh@collation=pinyin", "zh",
2628 "f", "zh_CN@collation=pinyin", "zh", /* alias of zh_Hans_CN */
2629 "f", "zh_Hans_CN@collation=pinyin", "zh",
2630 "f", "zh_HK@collation=pinyin", "zh", /* alias of zh_Hant_HK */
2631 "f", "zh_Hant_HK@collation=pinyin", "zh",
2632 "f", "zh_HK@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_HK */
2633 "f", "zh_Hant_HK@collation=stroke", "zh@collation=stroke",
2634 "f", "zh_HK", "zh@collation=stroke", /* alias of zh_Hant_HK */
2635 "f", "zh_Hant_HK", "zh@collation=stroke",
2636 "f", "zh_MO", "zh@collation=stroke", /* alias of zh_Hant_MO */
2637 "f", "zh_Hant_MO", "zh@collation=stroke",
2638 "f", "zh_TW_STROKE", "zh@collation=stroke",
2639 "f", "zh_TW_STROKE@collation=pinyin", "zh",
2640 "f", "sv_CN@calendar=japanese", "sv",
2641 "t", "sv@calendar=japanese", "sv",
2642 "f", "zh_TW@collation=pinyin", "zh", /* alias of zh_Hant_TW */
2643 "f", "zh_Hant_TW@collation=pinyin", "zh",
2644 "f", "zh_CN@collation=stroke", "zh@collation=stroke", /* alias of zh_Hans_CN */
2645 "f", "zh_Hans_CN@collation=stroke", "zh@collation=stroke",
2646 "t", "de@collation=phonebook", "de@collation=phonebook",
2647 "t", "hi@collation=standard", "hi",
2648 "f", "hi_AU@collation=standard;currency=CHF;calendar=buddhist", "hi",
2649 "f", "sv_SE@collation=pinyin", "sv", /* bug 4582 tests */
2650 "f", "sv_SE_BONN@collation=pinyin", "sv",
2652 "f", "nl_NL", "root",
2653 "f", "nl_NL_EEXT", "root",
2654 "t", "nl@collation=stroke", "root",
2655 "f", "nl_NL@collation=stroke", "root",
2656 "f", "nl_NL_EEXT@collation=stroke", "root",
2659 #endif /* !UCONFIG_NO_COLLATION */
2661 static const char *calCases
[] = {
2662 /* avail locale equiv */
2663 "t", "en_US_POSIX", "en@calendar=gregorian",
2664 "f", "ja_JP_TOKYO", "ja@calendar=gregorian",
2665 "f", "ja_JP_TOKYO@calendar=japanese", "ja@calendar=japanese",
2666 "t", "sr@calendar=gregorian", "sr@calendar=gregorian",
2667 "t", "en", "en@calendar=gregorian",
2671 #if !UCONFIG_NO_COLLATION
2672 TestGetFunctionalEquivalentOf(U_ICUDATA_COLL
, "collations", "collation", TRUE
, collCases
);
2674 TestGetFunctionalEquivalentOf("ICUDATA", "calendar", "calendar", FALSE
, calCases
);
2676 #if !UCONFIG_NO_COLLATION
2677 log_verbose("Testing error conditions:\n");
2679 char equivLocale
[256] = "???";
2681 UErrorCode status
= U_ZERO_ERROR
;
2682 UBool gotAvail
= FALSE
;
2684 len
= ures_getFunctionalEquivalent(equivLocale
, 255, U_ICUDATA_COLL
,
2685 "calendar", "calendar", "ar_EG@calendar=islamic",
2686 &gotAvail
, FALSE
, &status
);
2687 (void)len
; /* Suppress set but not used warning. */
2689 if(status
== U_MISSING_RESOURCE_ERROR
) {
2690 log_verbose("PASS: Got expected U_MISSING_RESOURCE_ERROR\n");
2692 log_err("ures_getFunctionalEquivalent returned locale %s, avail %c, err %s, but expected U_MISSING_RESOURCE_ERROR \n",
2693 equivLocale
, gotAvail
?'t':'f', u_errorName(status
));
2699 static void TestXPath(void) {
2700 UErrorCode status
= U_ZERO_ERROR
;
2701 UResourceBundle
*rb
= NULL
, *alias
= NULL
;
2703 const UChar
* result
= NULL
;
2704 const UChar expResult
[] = { 0x0063, 0x006F, 0x0072, 0x0072, 0x0065, 0x0063, 0x0074, 0x0000 }; /* "correct" */
2705 /*const UChar expResult[] = { 0x0074, 0x0065, 0x0069, 0x006E, 0x0064, 0x0065, 0x0073, 0x0074, 0x0000 }; *//*teindest*/
2707 const char *testdatapath
=loadTestData(&status
);
2708 if(U_FAILURE(status
))
2710 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
2714 log_verbose("Testing ures_open()......\n");
2716 rb
= ures_open(testdatapath
, "te_IN", &status
);
2717 if(U_FAILURE(status
)) {
2718 log_err("Could not open te_IN (%s)\n", myErrorName(status
));
2721 alias
= ures_getByKey(rb
, "rootAliasClient", alias
, &status
);
2722 if(U_FAILURE(status
)) {
2723 log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status
));
2728 result
= tres_getString(alias
, -1, NULL
, &len
, &status
);
2729 if(U_FAILURE(status
) || result
== NULL
|| u_strcmp(result
, expResult
)) {
2730 log_err("Couldn't get correct string value (%s)\n", myErrorName(status
));
2733 alias
= ures_getByKey(rb
, "aliasClient", alias
, &status
);
2734 if(U_FAILURE(status
)) {
2735 log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status
));
2740 result
= tres_getString(alias
, -1, NULL
, &len
, &status
);
2741 if(U_FAILURE(status
) || result
== NULL
|| u_strcmp(result
, expResult
)) {
2742 log_err("Couldn't get correct string value (%s)\n", myErrorName(status
));
2745 alias
= ures_getByKey(rb
, "nestedRootAliasClient", alias
, &status
);
2746 if(U_FAILURE(status
)) {
2747 log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status
));
2752 result
= tres_getString(alias
, -1, NULL
, &len
, &status
);
2753 if(U_FAILURE(status
) || result
== NULL
|| u_strcmp(result
, expResult
)) {
2754 log_err("Couldn't get correct string value (%s)\n", myErrorName(status
));
2760 static void TestCLDRStyleAliases(void) {
2761 UErrorCode status
= U_ZERO_ERROR
;
2762 UResourceBundle
*rb
= NULL
, *alias
= NULL
, *a
=NULL
;
2765 const UChar
*result
= NULL
;
2766 UChar expected
[256];
2767 const char *expects
[7] = { "", "a41", "a12", "a03", "ar4" };
2768 const char *testdatapath
=loadTestData(&status
);
2769 if(U_FAILURE(status
)) {
2770 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
2773 log_verbose("Testing CLDR style aliases......\n");
2775 rb
= ures_open(testdatapath
, "te_IN_REVISED", &status
);
2776 if(U_FAILURE(status
)) {
2777 log_err("Could not open te_IN (%s)\n", myErrorName(status
));
2780 alias
= ures_getByKey(rb
, "a", alias
, &status
);
2781 if(U_FAILURE(status
)) {
2782 log_err("Couldn't find the aliased with name \"a\" resource (%s)\n", myErrorName(status
));
2786 for(i
= 1; i
< 5 ; i
++) {
2790 /* instead of sprintf(resource, "a%i", i); */
2791 a
= ures_getByKeyWithFallback(alias
, resource
, a
, &status
);
2792 result
= tres_getString(a
, -1, NULL
, &len
, &status
);
2793 u_charsToUChars(expects
[i
], expected
, strlen(expects
[i
])+1);
2794 if(U_FAILURE(status
) || !result
|| u_strcmp(result
, expected
)) {
2795 log_err("CLDR style aliases failed resource with name \"%s\" resource, exp %s, got %S (%s)\n", resource
, expects
[i
], result
, myErrorName(status
));
2796 status
= U_ZERO_ERROR
;
2805 static void TestFallbackCodes(void) {
2806 UErrorCode status
= U_ZERO_ERROR
;
2807 const char *testdatapath
=loadTestData(&status
);
2809 UResourceBundle
*res
= ures_open(testdatapath
, "te_IN", &status
);
2811 UResourceBundle
*r
= NULL
, *fall
= NULL
;
2813 r
= ures_getByKey(res
, "tagged_array_in_Root_te_te_IN", r
, &status
);
2815 status
= U_ZERO_ERROR
;
2816 fall
= ures_getByKeyWithFallback(r
, "tag2", fall
, &status
);
2818 if(status
!= U_ZERO_ERROR
) {
2819 log_data_err("Expected error code to be U_ZERO_ERROR, got %s\n", u_errorName(status
));
2820 status
= U_ZERO_ERROR
;
2823 fall
= ures_getByKeyWithFallback(r
, "tag7", fall
, &status
);
2825 if(status
!= U_USING_FALLBACK_WARNING
) {
2826 log_data_err("Expected error code to be U_USING_FALLBACK_WARNING, got %s\n", u_errorName(status
));
2828 status
= U_ZERO_ERROR
;
2830 fall
= ures_getByKeyWithFallback(r
, "tag1", fall
, &status
);
2832 if(status
!= U_USING_DEFAULT_WARNING
) {
2833 log_data_err("Expected error code to be U_USING_DEFAULT_WARNING, got %s\n", u_errorName(status
));
2835 status
= U_ZERO_ERROR
;
2842 /* This test will crash if this doesn't work. Results don't need testing. */
2843 static void TestStackReuse(void) {
2844 UResourceBundle table
;
2845 UErrorCode errorCode
= U_ZERO_ERROR
;
2846 UResourceBundle
*rb
= ures_open(NULL
, "en_US", &errorCode
);
2848 if(U_FAILURE(errorCode
)) {
2849 log_data_err("Could not load en_US locale. status=%s\n",myErrorName(errorCode
));
2852 ures_initStackObject(&table
);
2853 ures_getByKeyWithFallback(rb
, "Types", &table
, &errorCode
);
2854 ures_getByKeyWithFallback(&table
, "collation", &table
, &errorCode
);
2859 /* Test ures_getUTF8StringXYZ() --------------------------------------------- */
2862 * Replace most ures_getStringXYZ() with this function which wraps the
2863 * desired call and also calls the UTF-8 variant and checks that it works.
2865 extern const UChar
*
2866 tres_getString(const UResourceBundle
*resB
,
2867 int32_t idx
, const char *key
,
2869 UErrorCode
*status
) {
2875 int32_t length16
, length8
, i16
, i8
;
2878 if(length
== NULL
) {
2882 s16
= ures_getStringByIndex(resB
, idx
, length
, status
);
2883 } else if(key
!= NULL
) {
2884 s16
= ures_getStringByKey(resB
, key
, length
, status
);
2886 s16
= ures_getString(resB
, length
, status
);
2888 if(U_FAILURE(*status
)) {
2893 /* try the UTF-8 variant of ures_getStringXYZ() */
2894 for(forceCopy
= FALSE
; forceCopy
<= TRUE
; ++forceCopy
) {
2896 length8
= (int32_t)sizeof(buffer8
);
2898 s8
= ures_getUTF8StringByIndex(resB
, idx
, p8
, &length8
, forceCopy
, status
);
2899 } else if(key
!= NULL
) {
2900 s8
= ures_getUTF8StringByKey(resB
, key
, p8
, &length8
, forceCopy
, status
);
2902 s8
= ures_getUTF8String(resB
, p8
, &length8
, forceCopy
, status
);
2904 if(*status
== U_INVALID_CHAR_FOUND
) {
2905 /* the UTF-16 string contains an unpaired surrogate, can't test UTF-8 variant */
2908 if(*status
== U_BUFFER_OVERFLOW_ERROR
) {
2909 *status
= U_ZERO_ERROR
;
2910 p8
= (char *)malloc(++length8
);
2915 s8
= ures_getUTF8StringByIndex(resB
, idx
, p8
, &length8
, forceCopy
, status
);
2916 } else if(key
!= NULL
) {
2917 s8
= ures_getUTF8StringByKey(resB
, key
, p8
, &length8
, forceCopy
, status
);
2919 s8
= ures_getUTF8String(resB
, p8
, &length8
, forceCopy
, status
);
2922 if(U_FAILURE(*status
)) {
2923 /* something unexpected happened */
2930 if(forceCopy
&& s8
!= p8
) {
2931 log_err("ures_getUTF8String(%p, %ld, '%s') did not write the string to dest\n",
2932 resB
, (long)idx
, key
);
2935 /* verify NUL-termination */
2936 if((p8
!= buffer8
|| length8
< sizeof(buffer8
)) && s8
[length8
] != 0) {
2937 log_err("ures_getUTF8String(%p, %ld, '%s') did not NUL-terminate\n",
2938 resB
, (long)idx
, key
);
2940 /* verify correct string */
2942 while(i16
< length16
&& i8
< length8
) {
2943 U16_NEXT(s16
, i16
, length16
, c16
);
2944 U8_NEXT(s8
, i8
, length8
, c8
);
2946 log_err("ures_getUTF8String(%p, %ld, '%s') got a bad string, c16=U+%04lx!=U+%04lx=c8 before i16=%ld\n",
2947 resB
, (long)idx
, key
, (long)c16
, (long)c8
, (long)i16
);
2950 /* verify correct length */
2951 if(i16
< length16
) {
2952 log_err("ures_getUTF8String(%p, %ld, '%s') UTF-8 string too short, length8=%ld, length16=%ld\n",
2953 resB
, (long)idx
, key
, (long)length8
, (long)length16
);
2956 log_err("ures_getUTF8String(%p, %ld, '%s') UTF-8 string too long, length8=%ld, length16=%ld\n",
2957 resB
, (long)idx
, key
, (long)length8
, (long)length16
);
2969 * API tests for ures_getUTF8String().
2970 * Most cases are handled by tres_getString(), which leaves argument checking
2971 * to be tested here.
2972 * Since the variants share most of their implementation, we only need to test
2974 * We also need not test for checking arguments which will be checked by the
2975 * UTF-16 ures_getStringXYZ() that are called internally.
2978 TestGetUTF8String() {
2979 UResourceBundle
*res
;
2980 const char *testdatapath
;
2986 status
= U_ZERO_ERROR
;
2987 testdatapath
= loadTestData(&status
);
2988 if(U_FAILURE(status
)) {
2989 log_data_err("Could not load testdata.dat - %s\n", u_errorName(status
));
2993 res
= ures_open(testdatapath
, "", &status
);
2994 if(U_FAILURE(status
)) {
2995 log_err("Unable to ures_open(testdata, \"\") - %s\n", u_errorName(status
));
3000 status
= U_ZERO_ERROR
;
3001 length8
= (int32_t)sizeof(buffer8
);
3002 s8
= ures_getUTF8StringByKey(res
, "string_only_in_Root", buffer8
, &length8
, FALSE
, &status
);
3003 (void)s8
; /* Suppress set but not used warning. */
3004 if(status
!= U_ZERO_ERROR
) {
3005 log_err("ures_getUTF8StringByKey(testdata/root string) malfunctioned - %s\n", u_errorName(status
));
3008 /* negative capacity */
3009 status
= U_ZERO_ERROR
;
3011 s8
= ures_getUTF8StringByKey(res
, "string_only_in_Root", buffer8
, &length8
, FALSE
, &status
);
3012 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
3013 log_err("ures_getUTF8StringByKey(capacity<0) malfunctioned - %s\n", u_errorName(status
));
3016 /* capacity>0 but dest=NULL */
3017 status
= U_ZERO_ERROR
;
3018 length8
= (int32_t)sizeof(buffer8
);
3019 s8
= ures_getUTF8StringByKey(res
, "string_only_in_Root", NULL
, &length8
, FALSE
, &status
);
3020 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
3021 log_err("ures_getUTF8StringByKey(dest=NULL capacity>0) malfunctioned - %s\n", u_errorName(status
));
3027 static void TestCLDRVersion(void) {
3028 UVersionInfo zeroVersion
;
3029 UVersionInfo testExpect
;
3030 UVersionInfo testCurrent
;
3031 UVersionInfo cldrVersion
;
3033 UErrorCode status
= U_ZERO_ERROR
;
3035 /* setup the constant value */
3036 u_versionFromString(zeroVersion
, "0.0.0.0");
3038 /* test CLDR value from API */
3039 ulocdata_getCLDRVersion(cldrVersion
, &status
);
3040 if(U_FAILURE(status
)) {
3041 /* the show is pretty much over at this point */
3042 log_err_status(status
, "FAIL: ulocdata_getCLDRVersion() returned %s\n", u_errorName(status
));
3045 u_versionToString(cldrVersion
, tmp
);
3046 log_info("ulocdata_getCLDRVersion() returned: '%s'\n", tmp
);
3050 /* setup from resource bundle */
3052 UResourceBundle
*res
;
3053 const char *testdatapath
;
3055 status
= U_ZERO_ERROR
;
3056 testdatapath
= loadTestData(&status
);
3057 if(U_FAILURE(status
)) {
3058 log_data_err("Could not load testdata.dat - %s\n", u_errorName(status
));
3062 res
= ures_openDirect(testdatapath
, "root", &status
);
3063 if(U_FAILURE(status
)) {
3064 log_err("Unable to ures_open(testdata, \"\") - %s\n", u_errorName(status
3068 ures_getVersionByKey(res
, "ExpectCLDRVersionAtLeast", testExpect
, &status
);
3069 ures_getVersionByKey(res
, "CurrentCLDRVersion", testCurrent
, &status
);
3071 if(U_FAILURE(status
)) {
3072 log_err("Unable to get test data for CLDR version - %s\n", u_errorName(status
));
3075 if(U_FAILURE(status
)) return;
3078 u_versionToString(testExpect
,tmp
);
3079 log_verbose("(data) ExpectCLDRVersionAtLeast { %s }\n", tmp
);
3080 if(memcmp(cldrVersion
, testExpect
, sizeof(UVersionInfo
)) < 0) {
3081 log_data_err("CLDR version is too old, expect at least %s.", tmp
);
3083 u_versionToString(testCurrent
,tmp
);
3084 log_verbose("(data) CurrentCLDRVersion { %s }\n", tmp
);
3085 switch(memcmp(cldrVersion
, testCurrent
, sizeof(UVersionInfo
))) {
3086 case 0: break; /* OK- current. */
3087 case -1: log_info("CLDR version is behind 'current' (for testdata/root.txt) %s. Some things may fail.\n", tmp
); break;
3088 case 1: log_info("CLDR version is ahead of 'current' (for testdata/root.txt) %s. Some things may fail.\n", tmp
); break;