1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8 /*******************************************************************************
12 * Modification History:
13 * Name Date Description
14 * Madhu Katragadda 05/09/2000 Ported Tests for New ResourceBundle API
15 * Madhu Katragadda 05/24/2000 Added new tests to test RES_BINARY for collationElements
16 ********************************************************************************
21 #include "unicode/utypes.h"
23 #include "unicode/putil.h"
24 #include "unicode/ustring.h"
25 #include "unicode/ucnv.h"
26 #include "unicode/utf8.h"
27 #include "unicode/utf16.h"
31 #include "unicode/uchar.h"
32 #include "ucol_imp.h" /* for U_ICUDATA_COLL */
33 #include "ubrkimpl.h" /* for U_ICUDATA_BRKITR */
34 #define RESTEST_HEAP_CHECK 0
36 #include "unicode/uloc.h"
37 #include "unicode/ulocdata.h"
40 #include "unicode/ctest.h"
47 /*****************************************************************************/
49 * Return a random unsigned long l where 0N <= l <= ULONG_MAX.
57 static UBool initialized
= FALSE
;
60 srand((unsigned)time(NULL
));
63 /* Assume rand has at least 12 bits of precision */
65 for (i
=0; i
<(int32_t)sizeof(l
); ++i
)
66 ((char*)&l
)[i
] = (char)((rand() & 0x0FF0) >> 4);
71 * Return a random double x where 0.0 <= x < 1.0.
76 return ((double)randul()) / UINT32_MAX
;
80 * Return a random integer i where 0 <= i < n.
82 static int32_t randi(int32_t n
)
84 return (int32_t)(randd() * n
);
86 /***************************************************************************************/
88 * Convert an integer, positive or negative, to a character string radix 10.
91 itoa1(int32_t i
, char* buf
)
101 /* Output digits in reverse order */
104 *p
++ = (char)('0' + (i
% 10));
110 /* Reverse the string */
119 static const int32_t kERROR_COUNT
= -1234567;
120 static const UChar kERROR
[] = { 0x0045 /*E*/, 0x0052 /*'R'*/, 0x0052 /*'R'*/,
121 0x004F /*'O'*/, 0x0052/*'R'*/, 0x0000 /*'\0'*/};
123 /*****************************************************************************/
132 typedef enum E_Where E_Where
;
133 /*****************************************************************************/
135 #define CONFIRM_EQ(actual,expected) UPRV_BLOCK_MACRO_BEGIN { \
136 if (u_strcmp(expected,actual)==0) { \
140 log_err("%s returned %s instead of %s\n", action, austrdup(actual), austrdup(expected)); \
142 } UPRV_BLOCK_MACRO_END
143 #define CONFIRM_INT_EQ(actual,expected) UPRV_BLOCK_MACRO_BEGIN { \
144 if ((expected)==(actual)) { \
148 log_err("%s returned %d instead of %d\n", action, actual, expected); \
150 } UPRV_BLOCK_MACRO_END
151 #define CONFIRM_INT_GE(actual,expected) UPRV_BLOCK_MACRO_BEGIN { \
152 if ((actual)>=(expected)) { \
156 log_err("%s returned %d instead of x >= %d\n", action, actual, expected); \
158 } UPRV_BLOCK_MACRO_END
159 #define CONFIRM_INT_NE(actual,expected) UPRV_BLOCK_MACRO_BEGIN { \
160 if ((expected)!=(actual)) { \
164 log_err("%s returned %d instead of x != %d\n", action, actual, expected); \
166 } UPRV_BLOCK_MACRO_END
167 /*#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)); } */
169 CONFIRM_ErrorCode(UErrorCode actual
,UErrorCode expected
)
171 if ((expected
)==(actual
))
176 /*log_err("%s returned %s instead of %s\n", action, myErrorName(actual), myErrorName(expected)); */
177 log_err("returned %s instead of %s\n", myErrorName(actual
), myErrorName(expected
));
182 /* Array of our test objects */
187 UErrorCode expected_constructor_status
;
189 UBool like
[e_Where_count
];
190 UBool inherits
[e_Where_count
];
194 /* "te" means test */
195 /* "IN" means inherits */
196 /* "NE" or "ne" means "does not exist" */
198 { "root", U_ZERO_ERROR
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} },
199 { "te", U_ZERO_ERROR
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
200 { "te_IN", U_ZERO_ERROR
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
201 { "te_NE", U_USING_FALLBACK_WARNING
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
202 { "te_IN_NE", U_USING_FALLBACK_WARNING
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
203 { "ne", U_USING_DEFAULT_WARNING
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} }
206 static int32_t bundles_count
= UPRV_LENGTHOF(param
);
210 /*static void printUChars(UChar*);*/
211 static void TestDecodedBundle(void);
212 static void TestGetKeywordValues(void);
213 static void TestGetFunctionalEquivalent(void);
214 static void TestCLDRStyleAliases(void);
215 static void TestFallbackCodes(void);
216 static void TestGetUTF8String(void);
217 static void TestCLDRVersion(void);
219 /***************************************************************************************/
221 /* Array of our test objects */
223 void addNEWResourceBundleTest(TestNode
** root
)
225 addTest(root
, &TestErrorCodes
, "tsutil/creststn/TestErrorCodes");
226 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
227 addTest(root
, &TestEmptyBundle
, "tsutil/creststn/TestEmptyBundle");
228 addTest(root
, &TestConstruction1
, "tsutil/creststn/TestConstruction1");
229 addTest(root
, &TestResourceBundles
, "tsutil/creststn/TestResourceBundles");
230 addTest(root
, &TestNewTypes
, "tsutil/creststn/TestNewTypes");
231 addTest(root
, &TestEmptyTypes
, "tsutil/creststn/TestEmptyTypes");
232 addTest(root
, &TestBinaryCollationData
, "tsutil/creststn/TestBinaryCollationData");
233 addTest(root
, &TestAPI
, "tsutil/creststn/TestAPI");
234 addTest(root
, &TestErrorConditions
, "tsutil/creststn/TestErrorConditions");
235 addTest(root
, &TestDecodedBundle
, "tsutil/creststn/TestDecodedBundle");
236 addTest(root
, &TestResourceLevelAliasing
, "tsutil/creststn/TestResourceLevelAliasing");
237 addTest(root
, &TestDirectAccess
, "tsutil/creststn/TestDirectAccess");
238 addTest(root
, &TestTicket9804
, "tsutil/creststn/TestTicket9804");
239 addTest(root
, &TestXPath
, "tsutil/creststn/TestXPath");
240 addTest(root
, &TestCLDRStyleAliases
, "tsutil/creststn/TestCLDRStyleAliases");
241 addTest(root
, &TestFallbackCodes
, "tsutil/creststn/TestFallbackCodes");
242 addTest(root
, &TestGetUTF8String
, "tsutil/creststn/TestGetUTF8String");
243 addTest(root
, &TestCLDRVersion
, "tsutil/creststn/TestCLDRVersion");
244 addTest(root
, &TestPreventFallback
, "tsutil/creststn/TestPreventFallback");
246 addTest(root
, &TestFallback
, "tsutil/creststn/TestFallback");
247 addTest(root
, &TestGetVersion
, "tsutil/creststn/TestGetVersion");
248 addTest(root
, &TestGetVersionColl
, "tsutil/creststn/TestGetVersionColl");
249 addTest(root
, &TestAliasConflict
, "tsutil/creststn/TestAliasConflict");
250 addTest(root
, &TestGetKeywordValues
, "tsutil/creststn/TestGetKeywordValues");
251 addTest(root
, &TestGetFunctionalEquivalent
,"tsutil/creststn/TestGetFunctionalEquivalent");
252 addTest(root
, &TestJB3763
, "tsutil/creststn/TestJB3763");
253 addTest(root
, &TestStackReuse
, "tsutil/creststn/TestStackReuse");
257 /***************************************************************************************/
258 static const char* norwayNames
[] = {
268 static const char* norwayLocales
[] = {
278 static void checkStatus(int32_t line
, UErrorCode expected
, UErrorCode status
) {
279 if(U_FAILURE(status
)) {
280 log_data_err("Resource not present, cannot test (%s:%d)\n", __FILE__
, line
);
282 if(status
!= expected
) {
283 log_err_status(status
, "%s:%d: Expected error code %s, got error code %s\n", __FILE__
, line
, u_errorName(expected
), u_errorName(status
));
287 static void TestErrorCodes(void) {
288 UErrorCode status
= U_USING_DEFAULT_WARNING
;
290 UResourceBundle
*r
= NULL
, *r2
= NULL
;
292 /* First check with ICUDATA */
293 /* first bundle should return fallback warning */
294 r
= ures_open(NULL
, "ti_ER_ASSAB", &status
);
295 checkStatus(__LINE__
, U_USING_FALLBACK_WARNING
, status
);
298 /* this bundle should return zero error, so it shouldn't change the status */
299 status
= U_USING_DEFAULT_WARNING
;
300 r
= ures_open(NULL
, "ti_ER", &status
);
301 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
303 /* we look up the resource which is aliased, but it lives in fallback */
305 if(U_SUCCESS(status
) && r
!= NULL
) {
306 status
= U_USING_DEFAULT_WARNING
;
307 r2
= ures_getByKey(r
, "ExemplarCharacters", NULL
, &status
); /* ExemplarCharacters lives in ti */
308 checkStatus(__LINE__
, U_USING_FALLBACK_WARNING
, status
);
312 /* this bundle should return zero error, so it shouldn't change the status */
313 status
= U_USING_DEFAULT_WARNING
;
314 r
= ures_open(U_ICUDATA_REGION
, "ti", &status
);
315 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
318 status
= U_USING_FALLBACK_WARNING
;
319 r
= ures_open(NULL
, "nolocale", &status
);
320 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
324 #if !UCONFIG_NO_COLLATION
325 /** Now, with the collation bundle **/
327 /* first bundle should return fallback warning */
328 r
= ures_open(U_ICUDATA_COLL
, "sr_YU_VOJVODINA", &status
);
329 checkStatus(__LINE__
, U_USING_FALLBACK_WARNING
, status
);
332 /* this bundle should return zero error, so it shouldn't change the status */
333 status
= U_USING_FALLBACK_WARNING
;
334 r
= ures_open(U_ICUDATA_COLL
, "sr", &status
);
335 checkStatus(__LINE__
, U_USING_FALLBACK_WARNING
, status
);
337 /* we look up the resource which is aliased */
338 if(U_SUCCESS(status
) && r
!= NULL
) {
339 status
= U_USING_DEFAULT_WARNING
;
340 r2
= ures_getByKey(r
, "collations", NULL
, &status
);
341 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
345 /* this bundle should return zero error, so it shouldn't change the status */
346 status
= U_USING_DEFAULT_WARNING
;
347 r
= ures_open(U_ICUDATA_COLL
, "sr", &status
);
348 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
350 /* we look up the resource which is aliased and at our level */
351 if(U_SUCCESS(status
) && r
!= NULL
) {
352 status
= U_USING_DEFAULT_WARNING
;
353 r2
= ures_getByKey(r
, "collations", r2
, &status
);
354 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
358 status
= U_USING_FALLBACK_WARNING
;
359 r
= ures_open(U_ICUDATA_COLL
, "nolocale", &status
);
360 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
363 #endif /* !UCONFIG_NO_COLLATION */
366 static void TestAliasConflict(void) {
367 UErrorCode status
= U_ZERO_ERROR
;
368 UResourceBundle
*he
= NULL
;
369 UResourceBundle
*iw
= NULL
;
370 UResourceBundle
*norway
= NULL
;
371 const UChar
*result
= NULL
;
375 const char *realName
= NULL
;
377 he
= ures_open(NULL
, "he", &status
);
378 iw
= ures_open(NULL
, "iw", &status
);
379 if(U_FAILURE(status
)) {
380 log_err_status(status
, "Failed to get resource with %s\n", myErrorName(status
));
383 result
= ures_getStringByKey(he
, "ExemplarCharacters", &resultLen
, &status
);
384 if(U_FAILURE(status
) || result
== NULL
) {
385 log_err_status(status
, "Failed to get resource ExemplarCharacters with %s\n", myErrorName(status
));
389 size
= UPRV_LENGTHOF(norwayNames
);
390 for(i
= 0; i
< size
; i
++) {
391 status
= U_ZERO_ERROR
;
392 norway
= ures_open(NULL
, norwayNames
[i
], &status
);
393 if(U_FAILURE(status
)) {
394 log_err_status(status
, "Failed to get resource with %s for %s\n", myErrorName(status
), norwayNames
[i
]);
397 realName
= ures_getLocale(norway
, &status
);
398 log_verbose("ures_getLocale(\"%s\")=%s\n", norwayNames
[i
], realName
);
399 if(realName
== NULL
|| strcmp(norwayLocales
[i
], realName
) != 0) {
400 log_data_err("Wrong locale name for %s, expected %s, got %s\n", norwayNames
[i
], norwayLocales
[i
], realName
);
406 static void TestDecodedBundle(){
408 UErrorCode error
= U_ZERO_ERROR
;
410 UResourceBundle
* resB
;
412 const UChar
* srcFromRes
;
414 static const UChar uSrc
[] = {
415 0x0009,0x092F,0x0941,0x0928,0x0947,0x0938,0x094D,0x0915,0x094B,0x0020,0x002E,0x0915,0x0947,0x0020,0x002E,0x090F,
416 0x0915,0x0020,0x002E,0x0905,0x0927,0x094D,0x092F,0x092F,0x0928,0x0020,0x002E,0x0915,0x0947,0x0020,0x0905,0x0928,
417 0x0941,0x0938,0x093E,0x0930,0x0020,0x0031,0x0039,0x0039,0x0030,0x0020,0x0924,0x0915,0x0020,0x0915,0x0902,0x092A,
418 0x094D,0x092F,0x0942,0x091F,0x0930,0x002D,0x092A,0x094D,0x0930,0x092C,0x0902,0x0927,0x093F,0x0924,0x0020,0x0938,
419 0x0942,0x091A,0x0928,0x093E,0x092A,0x094D,0x0930,0x0923,0x093E,0x0932,0x0940,0x0020,0x002E,0x0915,0x0947,0x0020,
420 0x002E,0x092F,0x094B,0x0917,0x0926,0x093E,0x0928,0x0020,0x002E,0x0915,0x0947,0x0020,0x002E,0x092B,0x0932,0x0938,
421 0x094D,0x0935,0x0930,0x0942,0x092A,0x0020,0x002E,0x0935,0x093F,0x0936,0x094D,0x0935,0x0020,0x002E,0x092E,0x0947,
422 0x0902,0x0020,0x002E,0x0938,0x093E,0x0932,0x093E,0x0928,0x093E,0x0020,0x002E,0x0032,0x0032,0x0030,0x0030,0x0020,
423 0x0905,0x0930,0x092C,0x0020,0x0930,0x0941,0x092A,0x092F,0x0947,0x0020,0x092E,0x0942,0x0932,0x094D,0x092F,0x0915,
424 0x0940,0x0020,0x002E,0x0034,0x0935,0x0938,0x094D,0x0924,0x0941,0x0913,0x0902,0x0020,0x002E,0x0034,0x0915,0x093E,
425 0x0020,0x002E,0x0034,0x0909,0x0924,0x094D,0x092A,0x093E,0x0926,0x0928,0x0020,0x002E,0x0034,0x0939,0x094B,0x0917,
426 0x093E,0x002C,0x0020,0x002E,0x0033,0x091C,0x092C,0x0915,0x093F,0x0020,0x002E,0x0033,0x0915,0x0902,0x092A,0x094D,
427 0x092F,0x0942,0x091F,0x0930,0x0020,0x002E,0x0033,0x0915,0x093E,0x0020,0x002E,0x0033,0x0915,0x0941,0x0932,0x0020,
428 0x002E,0x0033,0x092F,0x094B,0x0917,0x0926,0x093E,0x0928,0x0020,0x002E,0x0033,0x0907,0x0938,0x0938,0x0947,0x0915,
429 0x0939,0x093F,0x0020,0x002E,0x002F,0x091C,0x094D,0x092F,0x093E,0x0926,0x093E,0x0020,0x002E,0x002F,0x0939,0x094B,
430 0x0917,0x093E,0x0964,0x0020,0x002E,0x002F,0x0905,0x0928,0x0941,0x0938,0x0902,0x0927,0x093E,0x0928,0x0020,0x002E,
431 0x002F,0x0915,0x0940,0x0020,0x002E,0x002F,0x091A,0x0930,0x092E,0x0020,0x0938,0x0940,0x092E,0x093E,0x0913,0x0902,
432 0x0020,0x092A,0x0930,0x0020,0x092A,0x0939,0x0941,0x0902,0x091A,0x0928,0x0947,0x0020,0x0915,0x0947,0x0020,0x0932,
433 0x093F,0x090F,0x0020,0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x090F,0x0915,0x0020,0x002E,0x002F,
434 0x0906,0x092E,0x0020,0x002E,0x002F,0x091C,0x0930,0x0942,0x0930,0x0924,0x0020,0x002E,0x002F,0x091C,0x0948,0x0938,
435 0x093E,0x0020,0x092C,0x0928,0x0020,0x0917,0x092F,0x093E,0x0020,0x0939,0x0948,0x0964,0x0020,0x092D,0x093E,0x0930,
436 0x0924,0x0020,0x092E,0x0947,0x0902,0x0020,0x092D,0x0940,0x002C,0x0020,0x0916,0x093E,0x0938,0x0915,0x0930,0x0020,
437 0x092E,0x094C,0x091C,0x0942,0x0926,0x093E,0x0020,0x0938,0x0930,0x0915,0x093E,0x0930,0x0928,0x0947,0x002C,0x0020,
438 0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x0020,0x0915,0x0947,0x0020,0x092A,0x094D,0x0930,0x092F,
439 0x094B,0x0917,0x0020,0x092A,0x0930,0x0020,0x091C,0x092C,0x0930,0x0926,0x0938,0x094D,0x0924,0x0020,0x090F,0x095C,
440 0x0020,0x0932,0x0917,0x093E,0x092F,0x0940,0x0020,0x0939,0x0948,0x002C,0x0020,0x0915,0x093F,0x0902,0x0924,0x0941,
441 0x0020,0x0907,0x0938,0x0915,0x0947,0x0020,0x0938,0x0930,0x092A,0x091F,0x0020,0x0926,0x094C,0x095C,0x0932,0x0917,
442 0x093E,0x0928,0x0947,0x0020,0x002E,0x0032,0x0915,0x0947,0x0020,0x002E,0x0032,0x0932,0x093F,0x090F,0x0020,0x002E,
443 0x0032,0x0915,0x094D,0x092F,0x093E,0x0020,0x002E,0x0032,0x0938,0x092A,0x093E,0x091F,0x0020,0x002E,0x0032,0x0930,
444 0x093E,0x0938,0x094D,0x0924,0x093E,0x0020,0x002E,0x0032,0x0909,0x092A,0x0932,0x092C,0x094D,0x0927,0x0020,0x002E,
445 0x0939,0x0948,0x002C,0x0020,0x002E,0x0905,0x0925,0x0935,0x093E,0x0020,0x002E,0x0935,0x093F,0x0936,0x094D,0x0935,
446 0x0020,0x002E,0x092E,0x0947,0x0902,0x0020,0x002E,0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x0020,
447 0x002E,0x0915,0x0940,0x0938,0x092B,0x0932,0x0924,0x093E,0x0020,0x002E,0x0033,0x0935,0x0020,0x002E,0x0033,0x0935,
448 0x093F,0x092B,0x0932,0x0924,0x093E,0x0020,0x002E,0x0033,0x0938,0x0947,0x0020,0x002E,0x0033,0x0938,0x092C,0x0915,
449 0x0020,0x002E,0x0033,0x0932,0x0947,0x0020,0x002E,0x0033,0x0915,0x0930,0x0020,0x002E,0x0033,0x0915,0x094D,0x092F,
450 0x093E,0x0020,0x002E,0x0033,0x0939,0x092E,0x0020,0x002E,0x0033,0x0907,0x0938,0x0915,0x093E,0x0020,0x002E,0x0033,
451 0x092F,0x0941,0x0915,0x094D,0x0924,0x093F,0x092A,0x0942,0x0930,0x094D,0x0923,0x0020,0x002E,0x0032,0x0935,0x093F,
452 0x0938,0x094D,0x0924,0x093E,0x0930,0x0020,0x0905,0x092A,0x0947,0x0915,0x094D,0x0937,0x093F,0x0924,0x0020,0x0915,
453 0x0930,0x0020,0x0938,0x0915,0x0947,0x0902,0x0917,0x0947,0x0020,0x003F,0x0020,
459 const char *testdatapath
= loadTestData(&error
);
460 resB
= ures_open(testdatapath
, "encoded", &error
);
461 srcFromRes
=tres_getString(resB
,-1,"str",&len
,&error
);
462 if(U_FAILURE(error
)){
463 log_data_err("Could not find encoded.res from test data bundle. Error: %s\n", u_errorName(error
));
467 if(u_strncmp(srcFromRes
,uSrc
,len
)!=0){
468 log_err("Genrb produced res files after decoding failed\n");
471 if(uSrc
[num
]!=srcFromRes
[num
]){
472 log_verbose(" Expected: 0x%04X Got: 0x%04X \n", uSrc
[num
],srcFromRes
[num
]);
476 if (len
!= u_strlen(uSrc
)) {
477 log_err("Genrb produced a string larger than expected\n");
482 static void TestNewTypes() {
483 UResourceBundle
* theBundle
= NULL
;
485 const char* testdatapath
;
486 UErrorCode status
= U_ZERO_ERROR
;
487 UResourceBundle
* res
= NULL
;
488 uint8_t *binResult
= NULL
;
491 int32_t intResult
= 0;
492 uint32_t uintResult
= 0;
493 const UChar
*empty
= NULL
;
494 const UChar
*zeroString
;
495 UChar expected
[] = { 'a','b','c','\0','d','e','f' };
496 const char* expect
="tab:\t cr:\r ff:\f newline:\n backslash:\\\\ quote=\\\' doubleQuote=\\\" singlequoutes=''";
499 testdatapath
=loadTestData(&status
);
501 if(U_FAILURE(status
))
503 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
507 theBundle
= ures_open(testdatapath
, "testtypes", &status
);
509 empty
= tres_getString(theBundle
, -1, "emptystring", &len
, &status
);
510 if(empty
&& (*empty
!= 0 || len
!= 0)) {
511 log_err("Empty string returned invalid value\n");
514 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
516 CONFIRM_INT_NE(theBundle
, NULL
);
518 /* This test reads the string "abc\u0000def" from the bundle */
519 /* if everything is working correctly, the size of this string */
520 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
522 strcpy(action
, "getting and testing of string with embeded zero");
523 res
= ures_getByKey(theBundle
, "zerotest", res
, &status
);
524 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
525 CONFIRM_INT_EQ(ures_getType(res
), URES_STRING
);
526 zeroString
=tres_getString(res
, -1, NULL
, &len
, &status
);
527 if(U_SUCCESS(status
)){
528 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
529 CONFIRM_INT_EQ(len
, 7);
530 CONFIRM_INT_NE(len
, 3);
533 if(zeroString
[i
]!= expected
[i
]){
534 log_verbose("Output did not match Expected: \\u%4X Got: \\u%4X", expected
[i
], zeroString
[i
]);
538 strcpy(action
, "getting and testing of binary type");
539 res
= ures_getByKey(theBundle
, "binarytest", res
, &status
);
540 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
541 CONFIRM_INT_EQ(ures_getType(res
), URES_BINARY
);
542 binResult
=(uint8_t*)ures_getBinary(res
, &len
, &status
);
543 if(U_SUCCESS(status
)){
544 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
545 CONFIRM_INT_EQ(len
, 15);
546 for(i
= 0; i
<15; i
++) {
547 CONFIRM_INT_EQ(binResult
[i
], i
);
551 strcpy(action
, "getting and testing of imported binary type");
552 res
= ures_getByKey(theBundle
, "importtest", res
, &status
);
553 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
554 CONFIRM_INT_EQ(ures_getType(res
), URES_BINARY
);
555 binResult
=(uint8_t*)ures_getBinary(res
, &len
, &status
);
556 if(U_SUCCESS(status
)){
557 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
558 CONFIRM_INT_EQ(len
, 15);
559 for(i
= 0; i
<15; i
++) {
560 CONFIRM_INT_EQ(binResult
[i
], i
);
564 strcpy(action
, "getting and testing of integer types");
565 res
= ures_getByKey(theBundle
, "one", res
, &status
);
566 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
567 CONFIRM_INT_EQ(ures_getType(res
), URES_INT
);
568 intResult
=ures_getInt(res
, &status
);
569 uintResult
= ures_getUInt(res
, &status
);
570 if(U_SUCCESS(status
)){
571 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
572 CONFIRM_INT_EQ(uintResult
, (uint32_t)intResult
);
573 CONFIRM_INT_EQ(intResult
, 1);
576 strcpy(action
, "getting minusone");
577 res
= ures_getByKey(theBundle
, "minusone", res
, &status
);
578 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
579 CONFIRM_INT_EQ(ures_getType(res
), URES_INT
);
580 intResult
=ures_getInt(res
, &status
);
581 uintResult
= ures_getUInt(res
, &status
);
582 if(U_SUCCESS(status
)){
583 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
584 CONFIRM_INT_EQ(uintResult
, 0x0FFFFFFF); /* a 28 bit integer */
585 CONFIRM_INT_EQ(intResult
, -1);
586 CONFIRM_INT_NE(uintResult
, (uint32_t)intResult
);
589 strcpy(action
, "getting plusone");
590 res
= ures_getByKey(theBundle
, "plusone", res
, &status
);
591 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
592 CONFIRM_INT_EQ(ures_getType(res
), URES_INT
);
593 intResult
=ures_getInt(res
, &status
);
594 uintResult
= ures_getUInt(res
, &status
);
595 if(U_SUCCESS(status
)){
596 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
597 CONFIRM_INT_EQ(uintResult
, (uint32_t)intResult
);
598 CONFIRM_INT_EQ(intResult
, 1);
601 res
= ures_getByKey(theBundle
, "onehundredtwentythree", res
, &status
);
602 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
603 CONFIRM_INT_EQ(ures_getType(res
), URES_INT
);
604 intResult
=ures_getInt(res
, &status
);
605 if(U_SUCCESS(status
)){
606 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
607 CONFIRM_INT_EQ(intResult
, 123);
610 /* this tests if escapes are preserved or not */
612 const UChar
* str
= tres_getString(theBundle
,-1,"testescape",&len
,&status
);
613 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
614 if(U_SUCCESS(status
)){
615 u_charsToUChars(expect
,uExpect
,(int32_t)strlen(expect
)+1);
616 if(u_strcmp(uExpect
,str
)){
617 log_err("Did not get the expected string for testescape\n");
621 /* this tests if unescaping works are expected */
624 char pattern
[2048] = "";
626 UChar
* expectedEscaped
;
630 /* This strcpy fixes compiler warnings about long strings */
631 strcpy(pattern
, "[ \\\\u0020 \\\\u00A0 \\\\u1680 \\\\u2000 \\\\u2001 \\\\u2002 \\\\u2003 \\\\u2004 \\\\u2005 \\\\u2006 \\\\u2007 "
632 "\\\\u2008 \\\\u2009 \\\\u200A \\u200B \\\\u202F \\u205F \\\\u3000 \\u0000-\\u001F \\u007F \\u0080-\\u009F "
633 "\\\\u06DD \\\\u070F \\\\u180E \\\\u200C \\\\u200D \\\\u2028 \\\\u2029 \\\\u2060 \\\\u2061 \\\\u2062 \\\\u2063 "
634 "\\\\u206A-\\\\u206F \\\\uFEFF \\\\uFFF9-\\uFFFC \\U0001D173-\\U0001D17A \\U000F0000-\\U000FFFFD "
635 "\\U00100000-\\U0010FFFD \\uFDD0-\\uFDEF \\uFFFE-\\uFFFF \\U0001FFFE-\\U0001FFFF \\U0002FFFE-\\U0002FFFF "
638 "\\U0003FFFE-\\U0003FFFF \\U0004FFFE-\\U0004FFFF \\U0005FFFE-\\U0005FFFF \\U0006FFFE-\\U0006FFFF "
639 "\\U0007FFFE-\\U0007FFFF \\U0008FFFE-\\U0008FFFF \\U0009FFFE-\\U0009FFFF \\U000AFFFE-\\U000AFFFF "
640 "\\U000BFFFE-\\U000BFFFF \\U000CFFFE-\\U000CFFFF \\U000DFFFE-\\U000DFFFF \\U000EFFFE-\\U000EFFFF "
641 "\\U000FFFFE-\\U000FFFFF \\U0010FFFE-\\U0010FFFF \\uD800-\\uDFFF \\\\uFFF9 \\\\uFFFA \\\\uFFFB "
642 "\\uFFFC \\uFFFD \\u2FF0-\\u2FFB \\u0340 \\u0341 \\\\u200E \\\\u200F \\\\u202A \\\\u202B \\\\u202C "
645 "\\\\u202D \\\\u202E \\\\u206A \\\\u206B \\\\u206C \\\\u206D \\\\u206E \\\\u206F \\U000E0001 \\U000E0020-\\U000E007F "
649 patternLen
= (int32_t)uprv_strlen(pattern
);
650 expectedEscaped
= (UChar
*)malloc(U_SIZEOF_UCHAR
* patternLen
);
651 got
= tres_getString(theBundle
,-1,"test_unescaping",&len
,&status
);
652 expectedLen
= u_unescape(pattern
,expectedEscaped
,patternLen
);
653 if(got
==NULL
|| u_strncmp(expectedEscaped
,got
,expectedLen
)!=0 || expectedLen
!= len
){
654 log_err("genrb failed to unescape string\n");
657 for(i
=0;i
<expectedLen
;i
++){
658 if(expectedEscaped
[i
] != got
[i
]){
659 log_verbose("Expected: 0x%04X Got: 0x%04X \n",expectedEscaped
[i
], got
[i
]);
663 free(expectedEscaped
);
664 status
= U_ZERO_ERROR
;
666 /* test for jitterbug#1435 */
668 const UChar
* str
= tres_getString(theBundle
,-1,"test_underscores",&len
,&status
);
669 expect
="test message ....";
670 u_charsToUChars(expect
,uExpect
,(int32_t)strlen(expect
)+1);
671 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
672 if(str
== NULL
|| u_strcmp(uExpect
,str
)){
673 log_err("Did not get the expected string for test_underscores.\n");
676 /* test for jitterbug#2626 */
677 #if !UCONFIG_NO_COLLATION
679 UResourceBundle
* resB
= NULL
;
680 const UChar
* str
= NULL
;
681 int32_t strLength
= 0;
682 const UChar my
[] = {0x0026,0x0027,0x0075,0x0027,0x0020,0x003d,0x0020,0x0027,0xff55,0x0027,0x0000}; /* &'\u0075' = '\uFF55' */
683 status
= U_ZERO_ERROR
;
684 resB
= ures_getByKey(theBundle
, "collations", resB
, &status
);
685 resB
= ures_getByKey(resB
, "standard", resB
, &status
);
686 str
= tres_getString(resB
,-1,"Sequence",&strLength
,&status
);
687 if(!str
|| U_FAILURE(status
)) {
688 log_data_err("Could not load collations from theBundle: %s\n", u_errorName(status
));
689 } else if(u_strcmp(my
,str
) != 0){
690 log_err("Did not get the expected string for escaped \\u0075\n");
696 const char *sourcePath
= ctest_dataSrcDir();
697 int32_t srcPathLen
= (int32_t)strlen(sourcePath
);
698 const char *deltaPath
= ".."U_FILE_SEP_STRING
"test"U_FILE_SEP_STRING
"testdata"U_FILE_SEP_STRING
;
699 int32_t deltaPathLen
= (int32_t)strlen(deltaPath
);
700 char *testDataFileName
= (char *) malloc( srcPathLen
+ deltaPathLen
+ 50 );
701 char *path
= testDataFileName
;
703 strcpy(path
, sourcePath
);
705 strcpy(path
, deltaPath
);
706 path
+= deltaPathLen
;
707 status
= U_ZERO_ERROR
;
710 const UChar
* str
= tres_getString(theBundle
, -1, "testincludeUTF",&strLen
,&status
);
711 strcpy(path
, "riwords.txt");
712 path
[strlen("riwords.txt")]=0;
713 if(U_FAILURE(status
)){
714 log_err("Could not get testincludeUTF resource from testtypes bundle. Error: %s\n",u_errorName(status
));
717 const char* cp
= NULL
;
718 UCHARBUF
* ucbuf
= ucbuf_open(testDataFileName
,&cp
,FALSE
,FALSE
,&status
);
720 if(U_SUCCESS(status
)){
721 const UChar
* buffer
= ucbuf_getBuffer(ucbuf
,&len
,&status
);
722 if(U_SUCCESS(status
)){
723 /* verify the contents */
725 log_err("Did not get the expected len for riwords. Expected: %i , Got: %i\n", len
,strLen
);
727 /* test string termination */
728 if(u_strlen(str
) != strLen
|| str
[strLen
]!= 0 ){
729 log_err("testinclude not null terminated!\n");
731 if(u_strncmp(str
, buffer
,strLen
)!=0){
732 log_err("Did not get the expected string from riwords. Include functionality failed for genrb.\n");
735 log_err("ucbuf failed to open %s. Error: %s\n", testDataFileName
, u_errorName(status
));
740 log_err("Could not get riwords.txt (path : %s). Error: %s\n",testDataFileName
,u_errorName(status
));
744 status
= U_ZERO_ERROR
;
747 const UChar
* str
= tres_getString(theBundle
, -1, "testinclude",&strLen
,&status
);
748 strcpy(path
, "translit_rules.txt");
749 path
[strlen("translit_rules.txt")]=0;
751 if(U_FAILURE(status
)){
752 log_err("Could not get testinclude resource from testtypes bundle. Error: %s\n",u_errorName(status
));
756 UCHARBUF
* ucbuf
= ucbuf_open(testDataFileName
,&cp
,FALSE
,FALSE
,&status
);
758 if(U_SUCCESS(status
)){
759 const UChar
* buffer
= ucbuf_getBuffer(ucbuf
,&len
,&status
);
760 if(U_SUCCESS(status
)){
761 /* verify the contents */
763 log_err("Did not get the expected len for translit_rules. Expected: %i , Got: %i\n", len
,strLen
);
765 if(u_strncmp(str
, buffer
,strLen
)!=0){
766 log_err("Did not get the expected string from translit_rules. Include functionality failed for genrb.\n");
769 log_err("ucbuf failed to open %s. Error: %s\n", testDataFileName
, u_errorName(status
));
773 log_err("Could not get translit_rules.txt (path : %s). Error: %s\n",testDataFileName
,u_errorName(status
));
777 free(testDataFileName
);
780 ures_close(theBundle
);
784 static void TestEmptyTypes() {
785 UResourceBundle
* theBundle
= NULL
;
787 const char* testdatapath
;
788 UErrorCode status
= U_ZERO_ERROR
;
789 UResourceBundle
* res
= NULL
;
790 UResourceBundle
* resArray
= NULL
;
791 const uint8_t *binResult
= NULL
;
793 int32_t intResult
= 0;
794 const UChar
*zeroString
;
795 const int32_t *zeroIntVect
= NULL
;
797 strcpy(action
, "Construction of testtypes bundle");
798 testdatapath
=loadTestData(&status
);
799 if(U_FAILURE(status
))
801 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
805 theBundle
= ures_open(testdatapath
, "testtypes", &status
);
807 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
809 CONFIRM_INT_NE(theBundle
, NULL
);
811 /* This test reads the string "abc\u0000def" from the bundle */
812 /* if everything is working correctly, the size of this string */
813 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
815 status
= U_ZERO_ERROR
;
816 strcpy(action
, "getting and testing of explicit string of zero length string");
817 res
= ures_getByKey(theBundle
, "emptyexplicitstring", res
, &status
);
818 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
819 CONFIRM_INT_EQ(ures_getType(res
), URES_STRING
);
820 zeroString
=tres_getString(res
, -1, NULL
, &len
, &status
);
821 if(U_SUCCESS(status
)){
822 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
823 CONFIRM_INT_EQ(len
, 0);
824 CONFIRM_INT_EQ(u_strlen(zeroString
), 0);
827 log_err("Couldn't get emptyexplicitstring\n");
830 status
= U_ZERO_ERROR
;
831 strcpy(action
, "getting and testing of normal string of zero length string");
832 res
= ures_getByKey(theBundle
, "emptystring", res
, &status
);
833 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
834 CONFIRM_INT_EQ(ures_getType(res
), URES_STRING
);
835 zeroString
=tres_getString(res
, -1, NULL
, &len
, &status
);
836 if(U_SUCCESS(status
)){
837 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
838 CONFIRM_INT_EQ(len
, 0);
839 CONFIRM_INT_EQ(u_strlen(zeroString
), 0);
842 log_err("Couldn't get emptystring\n");
845 status
= U_ZERO_ERROR
;
846 strcpy(action
, "getting and testing of empty int");
847 res
= ures_getByKey(theBundle
, "emptyint", res
, &status
);
848 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
849 CONFIRM_INT_EQ(ures_getType(res
), URES_INT
);
850 intResult
=ures_getInt(res
, &status
);
851 if(U_SUCCESS(status
)){
852 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
853 CONFIRM_INT_EQ(intResult
, 0);
856 log_err("Couldn't get emptystring\n");
859 status
= U_ZERO_ERROR
;
860 strcpy(action
, "getting and testing of zero length intvector");
861 res
= ures_getByKey(theBundle
, "emptyintv", res
, &status
);
862 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
863 CONFIRM_INT_EQ(ures_getType(res
), URES_INT_VECTOR
);
865 if(U_FAILURE(status
)){
866 log_err("Couldn't get emptyintv key %s\n", u_errorName(status
));
869 zeroIntVect
=ures_getIntVector(res
, &len
, &status
);
870 (void)zeroIntVect
; /* Suppress set but not used warning. */
871 if(!U_SUCCESS(status
) || resArray
!= NULL
|| len
!= 0) {
872 log_err("Shouldn't get emptyintv\n");
876 status
= U_ZERO_ERROR
;
877 strcpy(action
, "getting and testing of zero length emptybin");
878 res
= ures_getByKey(theBundle
, "emptybin", res
, &status
);
879 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
880 CONFIRM_INT_EQ(ures_getType(res
), URES_BINARY
);
882 if(U_FAILURE(status
)){
883 log_err("Couldn't get emptybin key %s\n", u_errorName(status
));
886 binResult
=ures_getBinary(res
, &len
, &status
);
887 (void)binResult
; /* Suppress set but not used warning. */
888 if(!U_SUCCESS(status
) || len
!= 0) {
889 log_err("Couldn't get emptybin, or it's not empty\n");
893 status
= U_ZERO_ERROR
;
894 strcpy(action
, "getting and testing of zero length emptyarray");
895 res
= ures_getByKey(theBundle
, "emptyarray", res
, &status
);
896 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
897 CONFIRM_INT_EQ(ures_getType(res
), URES_ARRAY
);
899 if(U_FAILURE(status
)){
900 log_err("Couldn't get emptyarray key %s\n", u_errorName(status
));
903 resArray
=ures_getByIndex(res
, 0, resArray
, &status
);
904 if(U_SUCCESS(status
) || resArray
!= NULL
){
905 log_err("Shouldn't get emptyarray[0]\n");
909 status
= U_ZERO_ERROR
;
910 strcpy(action
, "getting and testing of zero length emptytable");
911 res
= ures_getByKey(theBundle
, "emptytable", res
, &status
);
912 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
913 CONFIRM_INT_EQ(ures_getType(res
), URES_TABLE
);
915 if(U_FAILURE(status
)){
916 log_err("Couldn't get emptytable key %s\n", u_errorName(status
));
919 resArray
=ures_getByIndex(res
, 0, resArray
, &status
);
920 if(U_SUCCESS(status
) || resArray
!= NULL
){
921 log_err("Shouldn't get emptytable[0]\n");
926 ures_close(theBundle
);
929 static void TestEmptyBundle(){
930 UErrorCode status
= U_ZERO_ERROR
;
931 const char* testdatapath
=NULL
;
932 UResourceBundle
*resb
=0, *dResB
=0;
934 testdatapath
=loadTestData(&status
);
935 if(U_FAILURE(status
))
937 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
940 resb
= ures_open(testdatapath
, "testempty", &status
);
942 if(U_SUCCESS(status
)){
943 dResB
= ures_getByKey(resb
,"test",dResB
,&status
);
944 if(status
!= U_MISSING_RESOURCE_ERROR
){
945 log_err("Did not get the expected error from an empty resource bundle. Expected : %s Got: %s\n",
946 u_errorName(U_MISSING_RESOURCE_ERROR
),u_errorName(status
));
953 static void TestBinaryCollationData(){
954 #if !UCONFIG_NO_COLLATION
955 UErrorCode status
=U_ZERO_ERROR
;
956 const char* locale
="te";
957 const char* testdatapath
;
958 UResourceBundle
*teRes
= NULL
;
959 UResourceBundle
*coll
=NULL
;
960 UResourceBundle
*binColl
= NULL
;
961 uint8_t *binResult
= NULL
;
963 const char* action
="testing the binary collaton data";
965 log_verbose("Testing binary collation data resource......\n");
967 testdatapath
=loadTestData(&status
);
968 if(U_FAILURE(status
))
970 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
975 teRes
=ures_open(testdatapath
, locale
, &status
);
976 if(U_FAILURE(status
)){
977 log_err("ERROR: Failed to get resource for \"te\" with %s", myErrorName(status
));
981 coll
= ures_getByKey(teRes
, "collations", coll
, &status
);
982 coll
= ures_getByKey(coll
, "standard", coll
, &status
);
983 if(U_SUCCESS(status
)){
984 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
985 CONFIRM_INT_EQ(ures_getType(coll
), URES_TABLE
);
986 binColl
=ures_getByKey(coll
, "%%CollationBin", binColl
, &status
);
987 if(U_SUCCESS(status
)){
988 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
989 CONFIRM_INT_EQ(ures_getType(binColl
), URES_BINARY
);
990 binResult
=(uint8_t*)ures_getBinary(binColl
, &len
, &status
);
991 (void)binResult
; /* Suppress set but not used warning. */
992 if(U_SUCCESS(status
)){
993 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
994 CONFIRM_INT_GE(len
, 1);
998 log_err("ERROR: ures_getByKey(locale(te), %%CollationBin) failed\n");
1002 log_err("ERROR: ures_getByKey(locale(te), collations) failed\n");
1005 ures_close(binColl
);
1011 static void TestAPI() {
1012 UErrorCode status
=U_ZERO_ERROR
;
1014 const char* key
=NULL
;
1015 const UChar
* value
=NULL
;
1016 const char* testdatapath
;
1017 UChar
* utestdatapath
=NULL
;
1018 char convOutput
[256];
1019 UChar largeBuffer
[1025];
1020 UResourceBundle
*teRes
= NULL
;
1021 UResourceBundle
*teFillin
=NULL
;
1022 UResourceBundle
*teFillin2
=NULL
;
1024 log_verbose("Testing ures_openU()......\n");
1026 testdatapath
=loadTestData(&status
);
1027 if(U_FAILURE(status
))
1029 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
1032 len
=(int32_t)strlen(testdatapath
);
1033 utestdatapath
= (UChar
*) malloc((len
+10)*sizeof(UChar
));
1035 u_charsToUChars(testdatapath
, utestdatapath
, (int32_t)strlen(testdatapath
)+1);
1036 #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) && U_FILE_SEP_CHAR == '\\'
1038 /* Convert all backslashes to forward slashes so that we can make sure that ures_openU
1039 can handle invariant characters. */
1041 while ((backslash
= u_strchr(utestdatapath
, 0x005C))) {
1042 *backslash
= 0x002F;
1047 u_memset(largeBuffer
, 0x0030, UPRV_LENGTHOF(largeBuffer
));
1048 largeBuffer
[UPRV_LENGTHOF(largeBuffer
)-1] = 0;
1050 /*Test ures_openU */
1052 status
= U_ZERO_ERROR
;
1053 ures_close(ures_openU(largeBuffer
, "root", &status
));
1054 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1055 log_err("ERROR: ures_openU() worked when the path is very large. It returned %s\n", myErrorName(status
));
1058 status
= U_ZERO_ERROR
;
1059 ures_close(ures_openU(NULL
, "root", &status
));
1060 if(U_FAILURE(status
)){
1061 log_err_status(status
, "ERROR: ures_openU() failed path = NULL with %s\n", myErrorName(status
));
1064 status
= U_ILLEGAL_ARGUMENT_ERROR
;
1065 if(ures_openU(NULL
, "root", &status
) != NULL
){
1066 log_err("ERROR: ures_openU() worked with error status with %s\n", myErrorName(status
));
1069 status
= U_ZERO_ERROR
;
1070 teRes
=ures_openU(utestdatapath
, "te", &status
);
1071 if(U_FAILURE(status
)){
1072 log_err_status(status
, "ERROR: ures_openU() failed path =%s with %s\n", austrdup(utestdatapath
), myErrorName(status
));
1075 /*Test ures_getLocale() */
1076 log_verbose("Testing ures_getLocale() .....\n");
1077 if(strcmp(ures_getLocale(teRes
, &status
), "te") != 0){
1078 log_err("ERROR: ures_getLocale() failed. Expected = te_TE Got = %s\n", ures_getLocale(teRes
, &status
));
1080 /*Test ures_getNextString() */
1081 teFillin
=ures_getByKey(teRes
, "tagged_array_in_te_te_IN", teFillin
, &status
);
1082 key
=ures_getKey(teFillin
);
1083 value
=(UChar
*)ures_getNextString(teFillin
, &len
, &key
, &status
);
1084 (void)value
; /* Suppress set but not used warning. */
1085 ures_resetIterator(NULL
);
1086 value
=(UChar
*)ures_getNextString(teFillin
, &len
, &key
, &status
);
1087 if(status
!=U_INDEX_OUTOFBOUNDS_ERROR
){
1088 log_err("ERROR: calling getNextString where index out of bounds should return U_INDEX_OUTOFBOUNDS_ERROR, Got : %s\n",
1089 myErrorName(status
));
1091 ures_resetIterator(teRes
);
1092 /*Test ures_getNextResource() where resource is table*/
1093 status
=U_ZERO_ERROR
;
1094 #if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
1095 /* The next key varies depending on the charset. */
1096 teFillin
=ures_getNextResource(teRes
, teFillin
, &status
);
1097 if(U_FAILURE(status
)){
1098 log_err("ERROR: ures_getNextResource() failed \n");
1100 key
=ures_getKey(teFillin
);
1101 /*if(strcmp(key, "%%CollationBin") != 0){*/
1102 /*if(strcmp(key, "array_2d_in_Root_te") != 0){*/ /* added "aliasClient" that goes first */
1103 if(strcmp(key
, "a") != 0){
1104 log_err("ERROR: ures_getNextResource() failed\n");
1108 /*Test ures_getByIndex on string Resource*/
1109 teFillin
=ures_getByKey(teRes
, "string_only_in_te", teFillin
, &status
);
1110 teFillin2
=ures_getByIndex(teFillin
, 0, teFillin2
, &status
);
1111 if(U_FAILURE(status
)){
1112 log_err("ERROR: ures_getByIndex on string resource failed\n");
1114 if(strcmp(u_austrcpy(convOutput
, tres_getString(teFillin2
, -1, NULL
, &len
, &status
)), "TE") != 0){
1115 status
=U_ZERO_ERROR
;
1116 log_err("ERROR: ures_getByIndex on string resource fetched the key=%s, expected \"TE\" \n", austrdup(ures_getString(teFillin2
, &len
, &status
)));
1119 /*ures_close(teRes);*/
1121 /*Test ures_openFillIn*/
1122 log_verbose("Testing ures_openFillIn......\n");
1123 status
=U_ZERO_ERROR
;
1124 ures_openFillIn(teRes
, testdatapath
, "te", &status
);
1125 if(U_FAILURE(status
)){
1126 log_err("ERROR: ures_openFillIn failed\n");
1129 if(strcmp(ures_getLocale(teRes
, &status
), "te") != 0){
1130 log_err("ERROR: ures_openFillIn did not open the ResourceBundle correctly\n");
1132 ures_getByKey(teRes
, "string_only_in_te", teFillin
, &status
);
1133 teFillin2
=ures_getNextResource(teFillin
, teFillin2
, &status
);
1134 if(ures_getType(teFillin2
) != URES_STRING
){
1135 log_err("ERROR: getType for getNextResource after ures_openFillIn failed\n");
1137 teFillin2
=ures_getNextResource(teFillin
, teFillin2
, &status
);
1138 if(status
!=U_INDEX_OUTOFBOUNDS_ERROR
){
1139 log_err("ERROR: calling getNextResource where index out of bounds should return U_INDEX_OUTOFBOUNDS_ERROR, Got : %s\n",
1140 myErrorName(status
));
1143 ures_close(teFillin
);
1144 ures_close(teFillin2
);
1147 /* Test that ures_getLocale() returns the "real" locale ID */
1148 status
=U_ZERO_ERROR
;
1149 teRes
=ures_open(NULL
, "dE_At_NOWHERE_TO_BE_FOUND", &status
);
1150 if(U_FAILURE(status
)) {
1151 log_data_err("unable to open a locale resource bundle from \"dE_At_NOWHERE_TO_BE_FOUND\"(%s)\n", u_errorName(status
));
1153 if(0!=strcmp("de_AT", ures_getLocale(teRes
, &status
))) {
1154 log_data_err("ures_getLocale(\"dE_At_NOWHERE_TO_BE_FOUND\")=%s but must be de_AT\n", ures_getLocale(teRes
, &status
));
1159 /* same test, but with an aliased locale resource bundle */
1160 status
=U_ZERO_ERROR
;
1161 teRes
=ures_open(NULL
, "iW_Il_depRecaTed_HebreW", &status
);
1162 if(U_FAILURE(status
)) {
1163 log_data_err("unable to open a locale resource bundle from \"iW_Il_depRecaTed_HebreW\"(%s)\n", u_errorName(status
));
1165 if(0!=strcmp("he_IL", ures_getLocale(teRes
, &status
))) {
1166 log_data_err("ures_getLocale(\"iW_Il_depRecaTed_HebreW\")=%s but must be he_IL\n", ures_getLocale(teRes
, &status
));
1170 free(utestdatapath
);
1173 static void TestErrorConditions(){
1174 UErrorCode status
=U_ZERO_ERROR
;
1175 const char *key
=NULL
;
1176 const UChar
*value
=NULL
;
1177 const char* testdatapath
;
1178 UChar
* utestdatapath
;
1180 UResourceBundle
*teRes
= NULL
;
1181 UResourceBundle
*coll
=NULL
;
1182 UResourceBundle
*binColl
= NULL
;
1183 UResourceBundle
*teFillin
=NULL
;
1184 UResourceBundle
*teFillin2
=NULL
;
1185 uint8_t *binResult
= NULL
;
1189 testdatapath
= loadTestData(&status
);
1190 if(U_FAILURE(status
))
1192 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
1195 len
= (int32_t)strlen(testdatapath
);
1196 utestdatapath
= (UChar
*) malloc(sizeof(UChar
) *(len
+10));
1197 u_uastrcpy(utestdatapath
, testdatapath
);
1199 /*Test ures_openU with status != U_ZERO_ERROR*/
1200 log_verbose("Testing ures_openU() with status != U_ZERO_ERROR.....\n");
1201 status
=U_ILLEGAL_ARGUMENT_ERROR
;
1202 teRes
=ures_openU(utestdatapath
, "te", &status
);
1203 if(U_FAILURE(status
)){
1204 log_verbose("ures_openU() failed as expected path =%s with status != U_ZERO_ERROR\n", testdatapath
);
1206 log_err("ERROR: ures_openU() is supposed to fail path =%s with status != U_ZERO_ERROR\n", austrdup(utestdatapath
));
1209 /*Test ures_openFillIn fails when input UResourceBundle parameter is NULL*/
1210 log_verbose("Testing ures_openFillIn with UResourceBundle = NULL.....\n");
1211 status
=U_ZERO_ERROR
;
1212 ures_openFillIn(NULL
, testdatapath
, "te", &status
);
1213 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1214 log_err("ERROR: ures_openFillIn with UResourceBundle= NULL should fail. Expected U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1215 myErrorName(status
));
1217 /*Test ures_openDirectFillIn fails when input UResourceBundle parameter is NULL*/
1218 log_verbose("Testing ures_openDirectFillIn with UResourceBundle = NULL.....\n");
1219 status
=U_ZERO_ERROR
;
1220 ures_openDirectFillIn(NULL
, testdatapath
, "te", &status
);
1221 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1222 log_err("ERROR: ures_openDirectFillIn with UResourceBundle= NULL should fail. Expected U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1223 myErrorName(status
));
1225 /*Test ures_getLocale() with status != U_ZERO_ERROR*/
1226 status
=U_ZERO_ERROR
;
1227 teRes
=ures_openU(utestdatapath
, "te", &status
);
1228 if(U_FAILURE(status
)){
1229 log_err("ERROR: ures_openU() failed path =%s with %s\n", austrdup(utestdatapath
), myErrorName(status
));
1232 status
=U_ILLEGAL_ARGUMENT_ERROR
;
1233 if(ures_getLocale(teRes
, &status
) != NULL
){
1234 log_err("ERROR: ures_getLocale is supposed to fail with errorCode != U_ZERO_ERROR\n");
1236 /*Test ures_getLocale() with UResourceBundle = NULL*/
1237 status
=U_ZERO_ERROR
;
1238 if(ures_getLocale(NULL
, &status
) != NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1239 log_err("ERROR: ures_getLocale is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1240 myErrorName(status
));
1242 /*Test ures_getSize() with UResourceBundle = NULL */
1243 status
=U_ZERO_ERROR
;
1244 if(ures_getSize(NULL
) != 0){
1245 log_err("ERROR: ures_getSize() should return 0 when UResourceBundle=NULL. Got =%d\n", ures_getSize(NULL
));
1247 /*Test ures_getType() with UResourceBundle = NULL should return URES_NONE==-1*/
1248 status
=U_ZERO_ERROR
;
1249 if(ures_getType(NULL
) != URES_NONE
){
1250 log_err("ERROR: ures_getType() should return URES_NONE when UResourceBundle=NULL. Got =%d\n", ures_getType(NULL
));
1252 /*Test ures_getKey() with UResourceBundle = NULL*/
1253 status
=U_ZERO_ERROR
;
1254 if(ures_getKey(NULL
) != NULL
){
1255 log_err("ERROR: ures_getKey() should return NULL when UResourceBundle=NULL. Got =%d\n", ures_getKey(NULL
));
1257 /*Test ures_hasNext() with UResourceBundle = NULL*/
1258 status
=U_ZERO_ERROR
;
1259 if(ures_hasNext(NULL
) != FALSE
){
1260 log_err("ERROR: ures_hasNext() should return FALSE when UResourceBundle=NULL. Got =%d\n", ures_hasNext(NULL
));
1262 /*Test ures_get() with UResourceBundle = NULL*/
1263 status
=U_ZERO_ERROR
;
1264 if(ures_getStringByKey(NULL
, "string_only_in_te", &resultLen
, &status
) != NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1265 log_err("ERROR: ures_get is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1266 myErrorName(status
));
1268 /*Test ures_getByKey() with UResourceBundle = NULL*/
1269 status
=U_ZERO_ERROR
;
1270 teFillin
=ures_getByKey(NULL
, "string_only_in_te", teFillin
, &status
);
1271 if( teFillin
!= NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1272 log_err("ERROR: ures_getByKey is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1273 myErrorName(status
));
1275 /*Test ures_getByKey() with status != U_ZERO_ERROR*/
1276 teFillin
=ures_getByKey(NULL
, "string_only_in_te", teFillin
, &status
);
1277 if(teFillin
!= NULL
){
1278 log_err("ERROR: ures_getByKey is supposed to fail when errorCode != U_ZERO_ERROR\n");
1280 /*Test ures_getStringByKey() with UResourceBundle = NULL*/
1281 status
=U_ZERO_ERROR
;
1282 if(ures_getStringByKey(NULL
, "string_only_in_te", &len
, &status
) != NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1283 log_err("ERROR: ures_getStringByKey is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1284 myErrorName(status
));
1286 /*Test ures_getStringByKey() with status != U_ZERO_ERROR*/
1287 if(ures_getStringByKey(teRes
, "string_only_in_te", &len
, &status
) != NULL
){
1288 log_err("ERROR: ures_getStringByKey is supposed to fail when status != U_ZERO_ERROR. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1289 myErrorName(status
));
1291 /*Test ures_getString() with UResourceBundle = NULL*/
1292 status
=U_ZERO_ERROR
;
1293 if(ures_getString(NULL
, &len
, &status
) != NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1294 log_err("ERROR: ures_getString is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1295 myErrorName(status
));
1297 /*Test ures_getString() with status != U_ZERO_ERROR*/
1298 if(ures_getString(teRes
, &len
, &status
) != NULL
){
1299 log_err("ERROR: ures_getString is supposed to fail when status != U_ZERO_ERROR. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1300 myErrorName(status
));
1302 /*Test ures_getBinary() with UResourceBundle = NULL*/
1303 status
=U_ZERO_ERROR
;
1304 if(ures_getBinary(NULL
, &len
, &status
) != NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1305 log_err("ERROR: ures_getBinary is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1306 myErrorName(status
));
1308 /*Test ures_getBinary(0 status != U_ILLEGAL_ARGUMENT_ERROR*/
1309 status
=U_ZERO_ERROR
;
1310 coll
= ures_getByKey(teRes
, "collations", coll
, &status
);
1311 coll
= ures_getByKey(teRes
, "standard", coll
, &status
);
1312 binColl
=ures_getByKey(coll
, "%%CollationBin", binColl
, &status
);
1314 status
=U_ILLEGAL_ARGUMENT_ERROR
;
1315 binResult
=(uint8_t*)ures_getBinary(binColl
, &len
, &status
);
1316 if(binResult
!= NULL
){
1317 log_err("ERROR: ures_getBinary() with status != U_ZERO_ERROR is supposed to fail\n");
1320 /*Test ures_getNextResource() with status != U_ZERO_ERROR*/
1321 teFillin
=ures_getNextResource(teRes
, teFillin
, &status
);
1322 if(teFillin
!= NULL
){
1323 log_err("ERROR: ures_getNextResource() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1325 /*Test ures_getNextResource() with UResourceBundle = NULL*/
1326 status
=U_ZERO_ERROR
;
1327 teFillin
=ures_getNextResource(NULL
, teFillin
, &status
);
1328 if(teFillin
!= NULL
|| status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1329 log_err("ERROR: ures_getNextResource() with UResourceBundle = NULL is supposed to fail. Expected : U_IILEGAL_ARGUMENT_ERROR, Got : %s\n",
1330 myErrorName(status
));
1332 /*Test ures_getNextString with errorCode != U_ZERO_ERROR*/
1333 teFillin
=ures_getByKey(teRes
, "tagged_array_in_te_te_IN", teFillin
, &status
);
1334 key
=ures_getKey(teFillin
);
1335 status
= U_ILLEGAL_ARGUMENT_ERROR
;
1336 value
=(UChar
*)ures_getNextString(teFillin
, &len
, &key
, &status
);
1338 log_err("ERROR: ures_getNextString() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1340 /*Test ures_getNextString with UResourceBundle = NULL*/
1341 status
=U_ZERO_ERROR
;
1342 value
=(UChar
*)ures_getNextString(NULL
, &len
, &key
, &status
);
1343 if(value
!= NULL
|| status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1344 log_err("ERROR: ures_getNextString() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1345 myErrorName(status
));
1347 /*Test ures_getByIndex with errorCode != U_ZERO_ERROR*/
1348 status
=U_ZERO_ERROR
;
1349 teFillin
=ures_getByKey(teRes
, "array_only_in_te", teFillin
, &status
);
1350 if(ures_countArrayItems(teRes
, "array_only_in_te", &status
) != 4) {
1351 log_err("ERROR: Wrong number of items in an array!\n");
1353 status
=U_ILLEGAL_ARGUMENT_ERROR
;
1354 teFillin2
=ures_getByIndex(teFillin
, 0, teFillin2
, &status
);
1355 if(teFillin2
!= NULL
){
1356 log_err("ERROR: ures_getByIndex() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1358 /*Test ures_getByIndex with UResourceBundle = NULL */
1359 status
=U_ZERO_ERROR
;
1360 teFillin2
=ures_getByIndex(NULL
, 0, teFillin2
, &status
);
1361 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1362 log_err("ERROR: ures_getByIndex() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1363 myErrorName(status
));
1365 /*Test ures_getStringByIndex with errorCode != U_ZERO_ERROR*/
1366 status
=U_ZERO_ERROR
;
1367 teFillin
=ures_getByKey(teRes
, "array_only_in_te", teFillin
, &status
);
1368 status
=U_ILLEGAL_ARGUMENT_ERROR
;
1369 value
=(UChar
*)ures_getStringByIndex(teFillin
, 0, &len
, &status
);
1371 log_err("ERROR: ures_getSringByIndex() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1373 /*Test ures_getStringByIndex with UResourceBundle = NULL */
1374 status
=U_ZERO_ERROR
;
1375 value
=(UChar
*)ures_getStringByIndex(NULL
, 0, &len
, &status
);
1376 if(value
!= NULL
|| status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1377 log_err("ERROR: ures_getStringByIndex() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1378 myErrorName(status
));
1380 /*Test ures_getStringByIndex with UResourceBundle = NULL */
1381 status
=U_ZERO_ERROR
;
1382 value
=(UChar
*)ures_getStringByIndex(teFillin
, 9999, &len
, &status
);
1383 if(value
!= NULL
|| status
!= U_MISSING_RESOURCE_ERROR
){
1384 log_err("ERROR: ures_getStringByIndex() with index that is too big is supposed to fail\n Expected: U_MISSING_RESOURCE_ERROR, Got: %s\n",
1385 myErrorName(status
));
1387 /*Test ures_getInt() where UResourceBundle = NULL */
1388 status
=U_ZERO_ERROR
;
1389 if(ures_getInt(NULL
, &status
) != -1 && status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1390 log_err("ERROR: ures_getInt() with UResourceBundle = NULL should fail. Expected: U_IILEGAL_ARGUMENT_ERROR, Got: %s\n",
1391 myErrorName(status
));
1393 /*Test ures_getInt() where status != U_ZERO_ERROR */
1394 if(ures_getInt(teRes
, &status
) != -1){
1395 log_err("ERROR: ures_getInt() with errorCode != U_ZERO_ERROR should fail\n");
1398 ures_close(teFillin
);
1399 ures_close(teFillin2
);
1401 ures_close(binColl
);
1403 free(utestdatapath
);
1408 static void TestGetVersion(){
1409 UVersionInfo minVersionArray
= {0x01, 0x00, 0x00, 0x00};
1410 UVersionInfo maxVersionArray
= {0x50, 0xff, 0xcf, 0xcf};
1411 UVersionInfo versionArray
;
1412 UErrorCode status
= U_ZERO_ERROR
;
1413 UResourceBundle
* resB
= NULL
;
1415 int locCount
= uloc_countAvailable();
1416 const char *locName
= "root";
1418 log_verbose("The ures_getVersion tests begin : \n");
1420 for(j
= -1; j
< locCount
; j
++) {
1422 locName
= uloc_getAvailable(j
);
1424 log_verbose("Testing version number for locale %s\n", locName
);
1425 resB
= ures_open(NULL
,locName
, &status
);
1426 if (U_FAILURE(status
)) {
1427 log_err_status(status
, "Resource bundle creation for locale %s failed.: %s\n", locName
, myErrorName(status
));
1431 ures_getVersion(resB
, versionArray
);
1432 for (i
=0; i
<4; ++i
) {
1433 if (versionArray
[i
] < minVersionArray
[i
] ||
1434 versionArray
[i
] > maxVersionArray
[i
])
1436 log_err("Testing ures_getVersion(%-5s) - unexpected result: %d.%d.%d.%d\n",
1437 locName
, versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
1446 static void TestGetVersionColl(){
1447 #if !UCONFIG_NO_COLLATION
1448 UVersionInfo minVersionArray
= {0x00, 0x00, 0x00, 0x00};
1449 UVersionInfo maxVersionArray
= {0x50, 0x80, 0xcf, 0xcf};
1450 UVersionInfo versionArray
;
1451 UErrorCode status
= U_ZERO_ERROR
;
1452 UResourceBundle
* resB
= NULL
;
1453 UEnumeration
*locs
= NULL
;
1455 const char *locName
= "root";
1457 const UChar
* rules
=NULL
;
1460 /* test NUL termination of UCARules */
1461 resB
= ures_open(U_ICUDATA_COLL
,locName
, &status
);
1462 rules
= tres_getString(resB
,-1,"UCARules",&len
, &status
);
1463 if(!rules
|| U_FAILURE(status
)) {
1464 log_data_err("Could not load UCARules for locale %s\n", locName
);
1465 status
= U_ZERO_ERROR
;
1466 } else if(u_strlen(rules
) != len
){
1467 log_err("UCARules string not nul terminated! \n");
1471 log_verbose("The ures_getVersion(%s) tests begin : \n", U_ICUDATA_COLL
);
1472 locs
= ures_openAvailableLocales(U_ICUDATA_COLL
, &status
);
1473 if (U_FAILURE(status
)) {
1474 log_err_status(status
, "enumeration of %s failed.: %s\n", U_ICUDATA_COLL
, myErrorName(status
));
1479 log_verbose("Testing version number for locale %s\n", locName
);
1480 resB
= ures_open(U_ICUDATA_COLL
,locName
, &status
);
1481 if (U_FAILURE(status
)) {
1482 log_err("Resource bundle creation for locale %s:%s failed.: %s\n", U_ICUDATA_COLL
, locName
, myErrorName(status
));
1486 ures_getVersion(resB
, versionArray
);
1487 for (i
=0; i
<4; ++i
) {
1488 if (versionArray
[i
] < minVersionArray
[i
] ||
1489 versionArray
[i
] > maxVersionArray
[i
])
1491 log_err("Testing ures_getVersion(%-5s) - unexpected result: %d.%d.%d.%d\n",
1492 locName
, versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
1497 locName
= uenum_next(locs
, &locLen
, &status
);
1498 if(U_FAILURE(status
)) {
1499 log_err("uenum_next(locs) error %s\n", u_errorName(status
));
1502 if(locName
== NULL
) {
1507 #endif /* !UCONFIG_NO_COLLATION */
1510 static void TestResourceBundles()
1512 UErrorCode status
= U_ZERO_ERROR
;
1513 loadTestData(&status
);
1514 if(U_FAILURE(status
)) {
1515 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
1519 testTag("only_in_Root", TRUE
, FALSE
, FALSE
);
1520 testTag("in_Root_te", TRUE
, TRUE
, FALSE
);
1521 testTag("in_Root_te_te_IN", TRUE
, TRUE
, TRUE
);
1522 testTag("in_Root_te_IN", TRUE
, FALSE
, TRUE
);
1523 testTag("only_in_te", FALSE
, TRUE
, FALSE
);
1524 testTag("only_in_te_IN", FALSE
, FALSE
, TRUE
);
1525 testTag("in_te_te_IN", FALSE
, TRUE
, TRUE
);
1526 testTag("nonexistent", FALSE
, FALSE
, FALSE
);
1528 log_verbose("Passed:= %d Failed= %d \n", pass
, fail
);
1533 static void TestConstruction1()
1535 UResourceBundle
*test1
= 0, *test2
= 0,*empty
= 0;
1536 const UChar
*result1
, *result2
;
1537 UErrorCode status
= U_ZERO_ERROR
;
1538 UErrorCode err
= U_ZERO_ERROR
;
1539 const char* locale
="te_IN";
1540 const char* testdatapath
;
1544 UVersionInfo versionInfo
;
1545 char versionString
[256];
1546 char verboseOutput
[256];
1548 U_STRING_DECL(rootVal
, "ROOT", 4);
1549 U_STRING_DECL(te_inVal
, "TE_IN", 5);
1551 U_STRING_INIT(rootVal
, "ROOT", 4);
1552 U_STRING_INIT(te_inVal
, "TE_IN", 5);
1554 testdatapath
=loadTestData(&status
);
1555 if(U_FAILURE(status
))
1557 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
1561 log_verbose("Testing ures_open()......\n");
1563 empty
= ures_open(testdatapath
, "testempty", &status
);
1564 if(empty
== NULL
|| U_FAILURE(status
)) {
1565 log_err("opening empty failed!\n");
1569 test1
=ures_open(testdatapath
, NULL
, &err
);
1573 log_err("construction of NULL did not succeed : %s \n", myErrorName(status
));
1576 test2
=ures_open(testdatapath
, locale
, &err
);
1579 log_err("construction of %s did not succeed : %s \n", locale
, myErrorName(status
));
1582 result1
= tres_getString(test1
, -1, "string_in_Root_te_te_IN", &len1
, &err
);
1583 result2
= tres_getString(test2
, -1, "string_in_Root_te_te_IN", &len2
, &err
);
1584 if (U_FAILURE(err
) || len1
==0 || len2
==0) {
1585 log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(status
));
1588 log_verbose("for string_in_Root_te_te_IN, default.txt had %s\n", u_austrcpy(verboseOutput
, result1
));
1589 log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", u_austrcpy(verboseOutput
, result2
));
1590 if(u_strcmp(result1
, rootVal
) !=0 || u_strcmp(result2
, te_inVal
) !=0 ){
1591 log_err("construction test failed. Run Verbose for more information");
1595 /* Test getVersionNumber*/
1596 log_verbose("Testing version number\n");
1597 log_verbose("for getVersionNumber : %s\n", ures_getVersionNumber(test1
));
1599 log_verbose("Testing version \n");
1600 ures_getVersion(test1
, versionInfo
);
1601 u_versionToString(versionInfo
, versionString
);
1603 log_verbose("for getVersion : %s\n", versionString
);
1605 if(strcmp(versionString
, ures_getVersionNumber(test1
)) != 0) {
1606 log_err("Versions differ: %s vs %s\n", versionString
, ures_getVersionNumber(test1
));
1614 /*****************************************************************************/
1615 /*****************************************************************************/
1617 static UBool
testTag(const char* frag
,
1622 int32_t failNum
= fail
;
1624 /* Make array from input params */
1627 const char *NAME
[] = { "ROOT", "TE", "TE_IN" };
1629 /* Now try to load the desired items */
1630 UResourceBundle
* theBundle
= NULL
;
1633 UErrorCode expected_status
,status
= U_ZERO_ERROR
,expected_resource_status
= U_ZERO_ERROR
;
1635 UChar
* expected_string
= NULL
;
1636 const UChar
* string
= NULL
;
1639 int32_t i
,j
,row
,col
, len
;
1640 int32_t actual_bundle
;
1642 int32_t row_count
=0;
1643 int32_t column_count
=0;
1645 int32_t tag_count
= 0;
1646 const char* testdatapath
;
1647 char verboseOutput
[256];
1648 UResourceBundle
* array
=NULL
;
1649 UResourceBundle
* array2d
=NULL
;
1650 UResourceBundle
* tags
=NULL
;
1651 UResourceBundle
* arrayItem1
=NULL
;
1653 testdatapath
= loadTestData(&status
);
1654 if(U_FAILURE(status
))
1656 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
1662 is_in
[2] = in_te_IN
;
1664 strcpy(item_tag
, "tag");
1666 for (i
=0; i
<bundles_count
; ++i
)
1668 strcpy(action
,"construction for ");
1669 strcat(action
, param
[i
].name
);
1672 status
= U_ZERO_ERROR
;
1674 theBundle
= ures_open(testdatapath
, param
[i
].name
, &status
);
1675 CONFIRM_ErrorCode(status
,param
[i
].expected_constructor_status
);
1678 actual_bundle
= 0; /* ne -> default */
1680 actual_bundle
= 1; /* te_NE -> te */
1682 actual_bundle
= 2; /* te_IN_NE -> te_IN */
1686 expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
1687 for (j
=e_te_IN
; j
>=e_Root
; --j
)
1689 if (is_in
[j
] && param
[i
].inherits
[j
])
1692 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
1693 expected_resource_status
= U_ZERO_ERROR
;
1695 expected_resource_status
= U_USING_DEFAULT_WARNING
;
1697 expected_resource_status
= U_USING_FALLBACK_WARNING
;
1699 log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>. actual_bundle=%s\n",
1704 is_in
[j
]?"Yes":"No",
1706 param
[i
].inherits
[j
]?"Yes":"No",
1707 param
[actual_bundle
].name
);
1713 for (j
=param
[i
].where
; j
>=0; --j
)
1721 base
=(UChar
*)malloc(sizeof(UChar
)*(strlen(NAME
[j
]) + 1));
1722 u_uastrcpy(base
,NAME
[j
]);
1731 base
= (UChar
*) malloc(sizeof(UChar
) * 1);
1736 /*----string---------------------------------------------------------------- */
1738 strcpy(tag
,"string_");
1741 strcpy(action
,param
[i
].name
);
1742 strcat(action
, ".ures_getStringByKey(" );
1744 strcat(action
, ")");
1747 status
= U_ZERO_ERROR
;
1750 string
=tres_getString(theBundle
, -1, tag
, &len
, &status
);
1751 if(U_SUCCESS(status
)) {
1752 expected_string
=(UChar
*)malloc(sizeof(UChar
)*(u_strlen(base
) + 4));
1753 u_strcpy(expected_string
,base
);
1754 CONFIRM_INT_EQ(len
, u_strlen(expected_string
));
1756 expected_string
= (UChar
*)malloc(sizeof(UChar
)*(u_strlen(kERROR
) + 1));
1757 u_strcpy(expected_string
,kERROR
);
1760 log_verbose("%s got %d, expected %d\n", action
, status
, expected_resource_status
);
1762 CONFIRM_ErrorCode(status
, expected_resource_status
);
1763 CONFIRM_EQ(string
, expected_string
);
1767 /*--------------array------------------------------------------------- */
1769 strcpy(tag
,"array_");
1772 strcpy(action
,param
[i
].name
);
1773 strcat(action
, ".ures_getByKey(" );
1775 strcat(action
, ")");
1779 count
= kERROR_COUNT
;
1780 status
= U_ZERO_ERROR
;
1781 array
=ures_getByKey(theBundle
, tag
, array
, &status
);
1782 CONFIRM_ErrorCode(status
,expected_resource_status
);
1783 if (U_SUCCESS(status
)) {
1784 /*confirm the resource type is an array*/
1785 CONFIRM_INT_EQ(ures_getType(array
), URES_ARRAY
);
1786 /*confirm the size*/
1787 count
=ures_getSize(array
);
1788 CONFIRM_INT_GE(count
,1);
1789 for (j
=0; j
<count
; ++j
) {
1791 u_strcpy(expected_string
, base
);
1792 u_uastrcpy(element
, itoa1(j
,buf
));
1793 u_strcat(expected_string
, element
);
1794 arrayItem1
=ures_getNextResource(array
, arrayItem1
, &status
);
1795 if(U_SUCCESS(status
)){
1796 CONFIRM_EQ(tres_getString(arrayItem1
, -1, NULL
, &len
, &status
),expected_string
);
1802 CONFIRM_INT_EQ(count
,kERROR_COUNT
);
1803 CONFIRM_ErrorCode(status
, U_MISSING_RESOURCE_ERROR
);
1804 /*CONFIRM_INT_EQ((int32_t)(unsigned long)array,(int32_t)0);*/
1808 /*--------------arrayItem------------------------------------------------- */
1810 strcpy(tag
,"array_");
1813 strcpy(action
,param
[i
].name
);
1814 strcat(action
, ".ures_getStringByIndex(");
1815 strcat(action
, tag
);
1816 strcat(action
, ")");
1819 for (j
=0; j
<10; ++j
){
1820 idx
= count
? (randi(count
* 3) - count
) : (randi(200) - 100);
1821 status
= U_ZERO_ERROR
;
1823 array
=ures_getByKey(theBundle
, tag
, array
, &status
);
1824 if(!U_FAILURE(status
)){
1826 t
=(UChar
*)ures_getStringByIndex(array
, idx
, &len
, &status
);
1827 if(!U_FAILURE(status
)){
1830 u_strcpy(expected_string
, base
);
1831 u_uastrcpy(element
, itoa1(idx
,buf
));
1832 u_strcat(expected_string
, element
);
1834 u_strcpy(expected_string
, kERROR
);
1838 expected_status
= (idx
>= 0 && idx
< count
) ? expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
1839 CONFIRM_ErrorCode(status
,expected_status
);
1840 CONFIRM_EQ(string
,expected_string
);
1845 /*--------------2dArray------------------------------------------------- */
1847 strcpy(tag
,"array_2d_");
1850 strcpy(action
,param
[i
].name
);
1851 strcat(action
, ".ures_getByKey(" );
1853 strcat(action
, ")");
1857 row_count
= kERROR_COUNT
, column_count
= kERROR_COUNT
;
1858 status
= U_ZERO_ERROR
;
1859 array2d
=ures_getByKey(theBundle
, tag
, array2d
, &status
);
1861 CONFIRM_ErrorCode(status
,expected_resource_status
);
1862 if (U_SUCCESS(status
))
1864 /*confirm the resource type is an 2darray*/
1865 CONFIRM_INT_EQ(ures_getType(array2d
), URES_ARRAY
);
1866 row_count
=ures_getSize(array2d
);
1867 CONFIRM_INT_GE(row_count
,1);
1869 for(row
=0; row
<row_count
; ++row
){
1870 UResourceBundle
*tableRow
=NULL
;
1871 tableRow
=ures_getByIndex(array2d
, row
, tableRow
, &status
);
1872 CONFIRM_ErrorCode(status
, expected_resource_status
);
1873 if(U_SUCCESS(status
)){
1874 /*confirm the resourcetype of each table row is an array*/
1875 CONFIRM_INT_EQ(ures_getType(tableRow
), URES_ARRAY
);
1876 column_count
=ures_getSize(tableRow
);
1877 CONFIRM_INT_GE(column_count
,1);
1879 for (col
=0; j
<column_count
; ++j
) {
1881 u_strcpy(expected_string
, base
);
1882 u_uastrcpy(element
, itoa1(row
, buf
));
1883 u_strcat(expected_string
, element
);
1884 u_uastrcpy(element
, itoa1(col
, buf
));
1885 u_strcat(expected_string
, element
);
1886 arrayItem1
=ures_getNextResource(tableRow
, arrayItem1
, &status
);
1887 if(U_SUCCESS(status
)){
1888 const UChar
*stringValue
=tres_getString(arrayItem1
, -1, NULL
, &len
, &status
);
1889 CONFIRM_EQ(stringValue
, expected_string
);
1893 ures_close(tableRow
);
1896 CONFIRM_INT_EQ(row_count
,kERROR_COUNT
);
1897 CONFIRM_INT_EQ(column_count
,kERROR_COUNT
);
1898 row_count
=column_count
=0;
1902 /*------2dArrayItem-------------------------------------------------------------- */
1904 for (j
=0; j
<10; ++j
)
1906 row
= row_count
? (randi(row_count
* 3) - row_count
) : (randi(200) - 100);
1907 col
= column_count
? (randi(column_count
* 3) - column_count
) : (randi(200) - 100);
1908 status
= U_ZERO_ERROR
;
1911 array2d
=ures_getByKey(theBundle
, tag
, array2d
, &status
);
1912 if(U_SUCCESS(status
)){
1913 UResourceBundle
*tableRow
=NULL
;
1914 tableRow
=ures_getByIndex(array2d
, row
, tableRow
, &status
);
1915 if(U_SUCCESS(status
)) {
1917 t
=(UChar
*)ures_getStringByIndex(tableRow
, col
, &len
, &status
);
1918 if(U_SUCCESS(status
)){
1922 ures_close(tableRow
);
1924 expected_status
= (row
>= 0 && row
< row_count
&& col
>= 0 && col
< column_count
) ?
1925 expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
1926 CONFIRM_ErrorCode(status
,expected_status
);
1928 if (U_SUCCESS(status
)){
1930 u_strcpy(expected_string
, base
);
1931 u_uastrcpy(element
, itoa1(row
, buf
));
1932 u_strcat(expected_string
, element
);
1933 u_uastrcpy(element
, itoa1(col
, buf
));
1934 u_strcat(expected_string
, element
);
1936 u_strcpy(expected_string
,kERROR
);
1938 CONFIRM_EQ(string
,expected_string
);
1943 /*--------------taggedArray----------------------------------------------- */
1944 strcpy(tag
,"tagged_array_");
1947 strcpy(action
,param
[i
].name
);
1948 strcat(action
,".ures_getByKey(");
1949 strcat(action
, tag
);
1953 status
= U_ZERO_ERROR
;
1955 tags
=ures_getByKey(theBundle
, tag
, tags
, &status
);
1956 CONFIRM_ErrorCode(status
, expected_resource_status
);
1957 if (U_SUCCESS(status
)) {
1958 UResType bundleType
=ures_getType(tags
);
1959 CONFIRM_INT_EQ(bundleType
, URES_TABLE
);
1961 tag_count
=ures_getSize(tags
);
1962 CONFIRM_INT_GE((int32_t)tag_count
, (int32_t)0);
1964 for(idx
=0; idx
<tag_count
; idx
++){
1965 UResourceBundle
*tagelement
=NULL
;
1966 const char *key
=NULL
;
1968 tagelement
=ures_getByIndex(tags
, idx
, tagelement
, &status
);
1969 key
=ures_getKey(tagelement
);
1970 value
=(UChar
*)ures_getNextString(tagelement
, &len
, &key
, &status
);
1971 log_verbose("tag = %s, value = %s\n", key
, u_austrcpy(verboseOutput
, value
));
1972 if(strncmp(key
, "tag", 3) == 0 && u_strncmp(value
, base
, u_strlen(base
)) == 0){
1977 ures_close(tagelement
);
1983 /*---------taggedArrayItem----------------------------------------------*/
1985 for (idx
=-20; idx
<20; ++idx
)
1988 status
= U_ZERO_ERROR
;
1990 strcpy(item_tag
, "tag");
1991 strcat(item_tag
, itoa1(idx
,buf
));
1992 tags
=ures_getByKey(theBundle
, tag
, tags
, &status
);
1993 if(U_SUCCESS(status
)){
1994 UResourceBundle
*tagelement
=NULL
;
1996 tagelement
=ures_getByKey(tags
, item_tag
, tagelement
, &status
);
1997 if(!U_FAILURE(status
)){
1998 UResType elementType
=ures_getType(tagelement
);
1999 CONFIRM_INT_EQ(elementType
, URES_STRING
);
2000 if(strcmp(ures_getKey(tagelement
), item_tag
) == 0){
2005 t
=(UChar
*)tres_getString(tagelement
, -1, NULL
, &len
, &status
);
2006 if(!U_FAILURE(status
)){
2011 CONFIRM_ErrorCode(status
,U_MISSING_RESOURCE_ERROR
);
2014 if (status
!= U_MISSING_RESOURCE_ERROR
) {
2016 u_strcpy(expected_string
, base
);
2017 u_uastrcpy(element
, itoa1(idx
,buf
));
2018 u_strcat(expected_string
, element
);
2019 CONFIRM_EQ(string
,expected_string
);
2023 ures_close(tagelement
);
2026 CONFIRM_INT_EQ(count
, tag_count
);
2028 free(expected_string
);
2029 ures_close(theBundle
);
2032 ures_close(array2d
);
2034 ures_close(arrayItem1
);
2036 return (UBool
)(failNum
== fail
);
2039 static void record_pass()
2044 static void record_fail()
2049 static void TestPreventFallback() {
2050 UResourceBundle
* theBundle
= NULL
;
2051 const char* testdatapath
;
2052 UErrorCode status
= U_ZERO_ERROR
;
2053 int32_t unused_len
= 0;
2055 testdatapath
=loadTestData(&status
);
2056 if(U_FAILURE(status
))
2058 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
2062 // In te_IN locale, fallback of string_in_te_no_te_IN_fallback is blocked
2063 // with the three empty-set (U+2205) chars.
2064 theBundle
= ures_open(testdatapath
, "te_IN_NE", &status
);
2065 if(U_FAILURE(status
))
2067 log_data_err("Could not open resource bundle te_IN_NE %s \n",myErrorName(status
));
2071 // Fallback is blocked
2072 ures_getStringByKeyWithFallback(theBundle
, "string_in_te_no_te_IN_fallback", &unused_len
, &status
);
2073 if (status
!= U_MISSING_RESOURCE_ERROR
)
2075 log_err("Expected missing resource error for string_in_te_no_te_IN_fallback.");
2077 status
= U_ZERO_ERROR
;
2079 // This fallback should succeed
2080 ures_getStringByKeyWithFallback(theBundle
, "string_only_in_te", &unused_len
, &status
);
2081 if(U_FAILURE(status
))
2083 log_err("Expected to find string_only_in_te %s \n",myErrorName(status
));
2085 status
= U_ZERO_ERROR
;
2086 ures_close(theBundle
);
2088 // From te locale, we should be able to fetch string_in_te_no_te_IN_fallback.
2089 theBundle
= ures_open(testdatapath
, "te", &status
);
2090 if(U_FAILURE(status
))
2092 log_data_err("Could not open resource bundle te_IN_NE %s \n",myErrorName(status
));
2095 ures_getStringByKeyWithFallback(theBundle
, "string_in_te_no_te_IN_fallback", &unused_len
, &status
);
2096 if(U_FAILURE(status
))
2098 log_err("Expected to find string_in_te_no_te_IN_fallback %s \n",myErrorName(status
));
2100 status
= U_ZERO_ERROR
;
2101 ures_close(theBundle
);
2105 * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR
2109 static void TestFallback()
2111 UErrorCode status
= U_ZERO_ERROR
;
2112 UResourceBundle
*fr_FR
= NULL
;
2113 UResourceBundle
*subResource
= NULL
;
2114 const UChar
*junk
; /* ignored */
2117 log_verbose("Opening fr_FR..");
2118 fr_FR
= ures_open(NULL
, "fr_FR", &status
);
2119 if(U_FAILURE(status
))
2121 log_err_status(status
, "Couldn't open fr_FR - %s\n", u_errorName(status
));
2125 status
= U_ZERO_ERROR
;
2128 /* clear it out.. just do some calls to get the gears turning */
2129 junk
= tres_getString(fr_FR
, -1, "LocaleID", &resultLen
, &status
);
2130 status
= U_ZERO_ERROR
;
2131 junk
= tres_getString(fr_FR
, -1, "LocaleString", &resultLen
, &status
);
2132 status
= U_ZERO_ERROR
;
2133 junk
= tres_getString(fr_FR
, -1, "LocaleID", &resultLen
, &status
);
2134 status
= U_ZERO_ERROR
;
2135 (void)junk
; /* Suppress set but not used warning. */
2137 /* OK first one. This should be a Default value. */
2138 subResource
= ures_getByKey(fr_FR
, "layout", NULL
, &status
);
2139 if(status
!= U_USING_DEFAULT_WARNING
)
2141 log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get layout from fr_FR, got %s\n",
2142 u_errorName(status
));
2145 status
= U_ZERO_ERROR
;
2146 ures_close(subResource
);
2148 /* and this is a Fallback, to fr */
2149 junk
= tres_getString(fr_FR
, -1, "ExemplarCharacters", &resultLen
, &status
);
2150 if(status
!= U_USING_FALLBACK_WARNING
)
2152 log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get ExemplarCharacters from fr_FR, got %d\n",
2156 status
= U_ZERO_ERROR
;
2159 /* Temporary hack err actually should be U_USING_FALLBACK_ERROR */
2160 /* Test Jitterbug 552 fallback mechanism of aliased data */
2162 UErrorCode err
=U_ZERO_ERROR
;
2163 UResourceBundle
* myResB
= ures_open(NULL
,"no_NO_NY",&err
);
2164 UResourceBundle
* resLocID
= ures_getByKey(myResB
, "Version", NULL
, &err
);
2165 UResourceBundle
* tResB
;
2166 UResourceBundle
* zoneResource
;
2167 const UChar
* version
= NULL
;
2168 static const UChar versionStr
[] = u
"36.1"; // 36.1 in nn_NO
2170 if(err
!= U_ZERO_ERROR
){
2171 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
));
2174 version
= tres_getString(resLocID
, -1, NULL
, &resultLen
, &err
);
2175 if(u_strcmp(version
, versionStr
) != 0){
2178 u_austrcpy(x
, versionStr
);
2179 u_austrcpy(g
, version
);
2180 log_data_err("ures_getString(resLocID, &resultLen, &err) returned an unexpected version value. Expected '%s', but got '%s'\n",
2183 zoneResource
= ures_open(U_ICUDATA_ZONE
, "no_NO_NY", &err
);
2184 tResB
= ures_getByKey(zoneResource
, "zoneStrings", NULL
, &err
);
2185 if(err
!= U_USING_FALLBACK_WARNING
){
2186 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
));
2189 ures_close(zoneResource
);
2190 ures_close(resLocID
);
2196 /* static void printUChars(UChar* uchars){
2198 / for(i=0; i<u_strlen(uchars); i++){
2199 / log_err("%04X ", *(uchars+i));
2203 static void TestResourceLevelAliasing(void) {
2204 UErrorCode status
= U_ZERO_ERROR
;
2205 UResourceBundle
*aliasB
= NULL
, *tb
= NULL
;
2206 UResourceBundle
*en
= NULL
, *uk
= NULL
, *testtypes
= NULL
;
2207 const char* testdatapath
= NULL
;
2208 const UChar
*string
= NULL
, *sequence
= NULL
;
2209 /*const uint8_t *binary = NULL, *binSequence = NULL;*/
2210 int32_t strLen
= 0, seqLen
= 0;/*, binLen = 0, binSeqLen = 0;*/
2214 testdatapath
=loadTestData(&status
);
2215 if(U_FAILURE(status
))
2217 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
2221 aliasB
= ures_open(testdatapath
, "testaliases", &status
);
2223 if(U_FAILURE(status
))
2225 log_data_err("Could not load testaliases.res %s \n",myErrorName(status
));
2228 /* this should fail - circular alias */
2229 tb
= ures_getByKey(aliasB
, "aaa", tb
, &status
);
2230 if(status
!= U_TOO_MANY_ALIASES_ERROR
) {
2231 log_err("Failed to detect circular alias\n");
2234 status
= U_ZERO_ERROR
;
2236 tb
= ures_getByKey(aliasB
, "aab", tb
, &status
);
2237 if(status
!= U_TOO_MANY_ALIASES_ERROR
) {
2238 log_err("Failed to detect circular alias\n");
2240 status
= U_ZERO_ERROR
;
2242 if(U_FAILURE(status
) ) {
2243 log_data_err("err loading tb resource\n");
2245 /* testing aliasing to a non existing resource */
2246 tb
= ures_getByKey(aliasB
, "nonexisting", tb
, &status
);
2247 if(status
!= U_MISSING_RESOURCE_ERROR
) {
2248 log_err("Managed to find an alias to non-existing resource\n");
2250 status
= U_ZERO_ERROR
;
2252 /* testing referencing/composed alias */
2253 uk
= ures_findResource("ja/calendar/gregorian/DateTimePatterns/2", uk
, &status
);
2254 if((uk
== NULL
) || U_FAILURE(status
)) {
2255 log_err_status(status
, "Couldn't findResource('ja/calendar/gregorian/DateTimePatterns/2') err %s\n", u_errorName(status
));
2259 sequence
= tres_getString(uk
, -1, NULL
, &seqLen
, &status
);
2261 tb
= ures_getByKey(aliasB
, "referencingalias", tb
, &status
);
2262 string
= tres_getString(tb
, -1, NULL
, &strLen
, &status
);
2264 if(seqLen
!= strLen
|| u_strncmp(sequence
, string
, seqLen
) != 0) {
2265 log_err("Referencing alias didn't get the right string (1)\n");
2268 string
= tres_getString(aliasB
, -1, "referencingalias", &strLen
, &status
);
2269 if(seqLen
!= strLen
|| u_strncmp(sequence
, string
, seqLen
) != 0) {
2270 log_err("Referencing alias didn't get the right string (2)\n");
2273 checkStatus(__LINE__
, U_ZERO_ERROR
, status
);
2274 tb
= ures_getByKey(aliasB
, "DateTimePatterns", tb
, &status
);
2275 checkStatus(__LINE__
, U_ZERO_ERROR
, status
);
2276 tb
= ures_getByIndex(tb
, 2, tb
, &status
);
2277 checkStatus(__LINE__
, U_ZERO_ERROR
, status
);
2278 string
= tres_getString(tb
, -1, NULL
, &strLen
, &status
);
2279 checkStatus(__LINE__
, U_ZERO_ERROR
, status
);
2281 if(U_FAILURE(status
)) {
2282 log_err("%s trying to get string via separate getters\n", u_errorName(status
));
2283 } else if(seqLen
!= strLen
|| u_strncmp(sequence
, string
, seqLen
) != 0) {
2284 log_err("Referencing alias didn't get the right string (3)\n");
2288 testtypes
= ures_open(testdatapath
, "testtypes", &status
);
2289 strcpy(buffer
, "menu/file/open");
2291 uk
= ures_findSubResource(testtypes
, s
, uk
, &status
);
2292 sequence
= tres_getString(uk
, -1, NULL
, &seqLen
, &status
);
2294 tb
= ures_getByKey(aliasB
, "simplealias", tb
, &status
);
2295 string
= tres_getString(tb
, -1, NULL
, &strLen
, &status
);
2297 if(U_FAILURE(status
) || seqLen
!= strLen
|| u_strncmp(sequence
, string
, seqLen
) != 0) {
2298 log_err("Referencing alias didn't get the right string (4)\n");
2301 /* test indexed aliasing */
2303 tb
= ures_getByKey(aliasB
, "zoneTests", tb
, &status
);
2304 tb
= ures_getByKey(tb
, "zoneAlias2", tb
, &status
);
2305 string
= tres_getString(tb
, -1, NULL
, &strLen
, &status
);
2307 en
= ures_findResource("/ICUDATA-zone/en/zoneStrings/3/0", en
, &status
);
2308 sequence
= tres_getString(en
, -1, NULL
, &seqLen
, &status
);
2310 if(U_FAILURE(status
) || seqLen
!= strLen
|| u_strncmp(sequence
, string
, seqLen
) != 0) {
2311 log_err("Referencing alias didn't get the right string (5)\n");
2314 /* test getting aliased string by index */
2316 const char* keys
[] = {
2318 "KeyAlias1PacificStandardTime",
2320 "KeyAlias3LosAngeles"
2323 const char* strings
[] = {
2324 "America/Los_Angeles",
2325 "Pacific Standard Time",
2330 const UChar
* result
;
2331 int32_t uBufferLen
= 0, resultLen
= 0;
2333 const char *key
= NULL
;
2334 tb
= ures_getByKey(aliasB
, "testGetStringByKeyAliasing", tb
, &status
);
2335 if(U_FAILURE(status
)) {
2336 log_err("FAIL: Couldn't get testGetStringByKeyAliasing resource: %s\n", u_errorName(status
));
2338 for(i
= 0; i
< UPRV_LENGTHOF(strings
); i
++) {
2339 result
= tres_getString(tb
, -1, keys
[i
], &resultLen
, &status
);
2340 if(U_FAILURE(status
)){
2341 log_err("(1) Fetching the resource with key %s failed. Error: %s\n", keys
[i
], u_errorName(status
));
2344 uBufferLen
= u_unescape(strings
[i
], uBuffer
, 256);
2345 if(resultLen
!= uBufferLen
|| u_strncmp(result
, uBuffer
, resultLen
) != 0) {
2346 log_err("(1) Didn't get correct string while accessing alias table by key (%s)\n", keys
[i
]);
2349 for(i
= 0; i
< UPRV_LENGTHOF(strings
); i
++) {
2350 result
= tres_getString(tb
, i
, NULL
, &resultLen
, &status
);
2351 if(U_FAILURE(status
)){
2352 log_err("(2) Fetching the resource with key %s failed. Error: %s\n", keys
[i
], u_errorName(status
));
2355 uBufferLen
= u_unescape(strings
[i
], uBuffer
, 256);
2356 if(result
==NULL
|| resultLen
!= uBufferLen
|| u_strncmp(result
, uBuffer
, resultLen
) != 0) {
2357 log_err("(2) Didn't get correct string while accesing alias table by index (%s)\n", strings
[i
]);
2360 for(i
= 0; i
< UPRV_LENGTHOF(strings
); i
++) {
2361 result
= ures_getNextString(tb
, &resultLen
, &key
, &status
);
2362 if(U_FAILURE(status
)){
2363 log_err("(3) Fetching the resource with key %s failed. Error: %s\n", keys
[i
], u_errorName(status
));
2366 uBufferLen
= u_unescape(strings
[i
], uBuffer
, 256);
2367 if(result
==NULL
|| resultLen
!= uBufferLen
|| u_strncmp(result
, uBuffer
, resultLen
) != 0) {
2368 log_err("(3) Didn't get correct string while iterating over alias table (%s)\n", strings
[i
]);
2372 tb
= ures_getByKey(aliasB
, "testGetStringByIndexAliasing", tb
, &status
);
2373 if(U_FAILURE(status
)) {
2374 log_err("FAIL: Couldn't get testGetStringByIndexAliasing resource: %s\n", u_errorName(status
));
2376 for(i
= 0; i
< UPRV_LENGTHOF(strings
); i
++) {
2377 result
= tres_getString(tb
, i
, NULL
, &resultLen
, &status
);
2378 if(U_FAILURE(status
)){
2379 log_err("Fetching the resource with key %s failed. Error: %s\n", keys
[i
], u_errorName(status
));
2382 uBufferLen
= u_unescape(strings
[i
], uBuffer
, 256);
2383 if(result
==NULL
|| resultLen
!= uBufferLen
|| u_strncmp(result
, uBuffer
, resultLen
) != 0) {
2384 log_err("Didn't get correct string while accesing alias by index in an array (%s)\n", strings
[i
]);
2387 for(i
= 0; i
< UPRV_LENGTHOF(strings
); i
++) {
2388 result
= ures_getNextString(tb
, &resultLen
, &key
, &status
);
2389 if(U_FAILURE(status
)){
2390 log_err("Fetching the resource with key %s failed. Error: %s\n", keys
[i
], u_errorName(status
));
2393 uBufferLen
= u_unescape(strings
[i
], uBuffer
, 256);
2394 if(result
==NULL
|| resultLen
!= uBufferLen
|| u_strncmp(result
, uBuffer
, resultLen
) != 0) {
2395 log_err("Didn't get correct string while iterating over aliases in an array (%s)\n", strings
[i
]);
2400 tb
= ures_getByKey(aliasB
, "testAliasToTree", tb
, &status
);
2401 if(U_FAILURE(status
)){
2402 log_err("Fetching the resource with key \"testAliasToTree\" failed. Error: %s\n", u_errorName(status
));
2405 if (strcmp(ures_getKey(tb
), "collations") != 0) {
2406 log_err("ures_getKey(aliasB) unexpectedly returned %s instead of \"collations\"\n", ures_getKey(tb
));
2413 ures_close(testtypes
);
2416 static void TestDirectAccess(void) {
2417 UErrorCode status
= U_ZERO_ERROR
;
2418 UResourceBundle
*t
= NULL
, *t2
= NULL
;
2419 const char* key
= NULL
;
2423 /*const char* testdatapath=loadTestData(&status);
2424 if(U_FAILURE(status)){
2425 log_err("Could not load testdata.dat %s \n",myErrorName(status));
2429 t
= ures_findResource("/testdata/te/zoneStrings/3/2", t
, &status
);
2430 if(U_FAILURE(status
)) {
2431 log_data_err("Couldn't access indexed resource, error %s\n", u_errorName(status
));
2432 status
= U_ZERO_ERROR
;
2434 key
= ures_getKey(t
);
2436 log_err("Got a strange key, expected NULL, got %s\n", key
);
2439 t
= ures_findResource("en/calendar/gregorian/DateTimePatterns/3", t
, &status
);
2440 if(U_FAILURE(status
)) {
2441 log_data_err("Couldn't access indexed resource, error %s\n", u_errorName(status
));
2442 status
= U_ZERO_ERROR
;
2444 key
= ures_getKey(t
);
2446 log_err("Got a strange key, expected NULL, got %s\n", key
);
2450 t
= ures_findResource("ja/ExemplarCharacters", t
, &status
);
2451 if(U_FAILURE(status
)) {
2452 log_data_err("Couldn't access keyed resource, error %s\n", u_errorName(status
));
2453 status
= U_ZERO_ERROR
;
2455 key
= ures_getKey(t
);
2456 if(strcmp(key
, "ExemplarCharacters")!=0) {
2457 log_err("Got a strange key, expected 'ExemplarCharacters', got %s\n", key
);
2461 t2
= ures_open(U_ICUDATA_LANG
, "sr", &status
);
2462 if(U_FAILURE(status
)) {
2463 log_err_status(status
, "Couldn't open 'sr' resource bundle, error %s\n", u_errorName(status
));
2464 log_data_err("No 'sr', no test - you have bigger problems than testing direct access. "
2465 "You probably have no data! Aborting this test\n");
2468 if(U_SUCCESS(status
)) {
2469 strcpy(buffer
, "Languages/hr");
2471 t
= ures_findSubResource(t2
, s
, t
, &status
);
2472 if(U_FAILURE(status
)) {
2473 log_err("Couldn't access keyed resource, error %s\n", u_errorName(status
));
2474 status
= U_ZERO_ERROR
;
2476 key
= ures_getKey(t
);
2477 if(strcmp(key
, "hr")!=0) {
2478 log_err("Got a strange key, expected 'hr', got %s\n", key
);
2483 t
= ures_findResource("root/calendar/islamic-civil/DateTime", t
, &status
);
2484 if(U_SUCCESS(status
)) {
2485 log_data_err("This resource does not exist. How did it get here?\n");
2487 status
= U_ZERO_ERROR
;
2489 /* this one will freeze */
2490 t
= ures_findResource("root/calendar/islamic-civil/eras/abbreviated/0/mikimaus/pera", t
, &status
);
2491 if(U_SUCCESS(status
)) {
2492 log_data_err("Second resource does not exist. How did it get here?\n");
2494 status
= U_ZERO_ERROR
;
2497 t2
= ures_open(NULL
, "he", &status
);
2498 t2
= ures_getByKeyWithFallback(t2
, "calendar", t2
, &status
);
2499 t2
= ures_getByKeyWithFallback(t2
, "islamic-civil", t2
, &status
);
2500 t2
= ures_getByKeyWithFallback(t2
, "DateTime", t2
, &status
);
2501 if(U_SUCCESS(status
)) {
2502 log_err("This resource does not exist. How did it get here?\n");
2504 status
= U_ZERO_ERROR
;
2507 t2
= ures_open(NULL
, "he", &status
);
2509 t2
= ures_getByKeyWithFallback(t2
, "calendar", t2
, &status
);
2510 t2
= ures_getByKeyWithFallback(t2
, "islamic-civil", t2
, &status
);
2511 t2
= ures_getByKeyWithFallback(t2
, "eras", t2
, &status
);
2512 if(U_FAILURE(status
)) {
2513 log_err_status(status
, "Didn't get Eras. I know they are there!\n");
2515 status
= U_ZERO_ERROR
;
2518 t2
= ures_open(NULL
, "root", &status
);
2519 t2
= ures_getByKeyWithFallback(t2
, "calendar", t2
, &status
);
2520 t2
= ures_getByKeyWithFallback(t2
, "islamic-civil", t2
, &status
);
2521 t2
= ures_getByKeyWithFallback(t2
, "DateTime", t2
, &status
);
2522 if(U_SUCCESS(status
)) {
2523 log_err("This resource does not exist. How did it get here?\n");
2525 status
= U_ZERO_ERROR
;
2531 static void TestTicket9804(void) {
2532 UErrorCode status
= U_ZERO_ERROR
;
2533 UResourceBundle
*t
= NULL
;
2534 t
= ures_open(NULL
, "he", &status
);
2535 t
= ures_getByKeyWithFallback(t
, "calendar/islamic-civil/DateTime", t
, &status
);
2536 if(U_SUCCESS(status
)) {
2537 log_err("This resource does not exist. How did it get here?\n");
2539 status
= U_ZERO_ERROR
;
2541 t
= ures_open(NULL
, "he", &status
);
2542 t
= ures_getByKeyWithFallback(t
, "calendar/islamic-civil/eras", t
, &status
);
2543 if(U_FAILURE(status
)) {
2544 log_err_status(status
, "Didn't get Eras. I know they are there!\n");
2546 const char *locale
= ures_getLocaleByType(t
, ULOC_ACTUAL_LOCALE
, &status
);
2547 if (uprv_strcmp("he", locale
) != 0) {
2548 log_err("Eras should be in the 'he' locale, but was in: %s", locale
);
2551 status
= U_ZERO_ERROR
;
2555 static void TestJB3763(void) {
2556 /* Nasty bug prevented using parent as fill-in, since it would
2557 * stomp the path information.
2559 UResourceBundle
*t
= NULL
;
2560 UErrorCode status
= U_ZERO_ERROR
;
2561 t
= ures_open(NULL
, "sr_Latn", &status
);
2562 t
= ures_getByKeyWithFallback(t
, "calendar", t
, &status
);
2563 t
= ures_getByKeyWithFallback(t
, "gregorian", t
, &status
);
2564 t
= ures_getByKeyWithFallback(t
, "AmPmMarkers", t
, &status
);
2565 if(U_FAILURE(status
)) {
2566 log_err_status(status
, "This resource should be available?\n");
2568 status
= U_ZERO_ERROR
;
2574 static void TestGetKeywordValues(void) {
2575 UEnumeration
*kwVals
;
2576 UBool foundStandard
= FALSE
;
2577 UErrorCode status
= U_ZERO_ERROR
;
2579 #if !UCONFIG_NO_COLLATION
2580 kwVals
= ures_getKeywordValues( U_ICUDATA_COLL
, "collations", &status
);
2582 log_verbose("Testing getting collation keyword values:\n");
2584 while((kw
=uenum_next(kwVals
, NULL
, &status
))) {
2585 log_verbose(" %s\n", kw
);
2586 if(!strcmp(kw
,"standard")) {
2587 if(foundStandard
== FALSE
) {
2588 foundStandard
= TRUE
;
2590 log_err("'standard' was found twice in the keyword list.\n");
2594 if(foundStandard
== FALSE
) {
2595 log_err_status(status
, "'standard' was not found in the keyword list.\n");
2597 uenum_close(kwVals
);
2598 if(U_FAILURE(status
)) {
2599 log_err_status(status
, "err %s getting collation values\n", u_errorName(status
));
2601 status
= U_ZERO_ERROR
;
2603 foundStandard
= FALSE
;
2604 kwVals
= ures_getKeywordValues( "ICUDATA", "calendar", &status
);
2606 log_verbose("Testing getting calendar keyword values:\n");
2608 while((kw
=uenum_next(kwVals
, NULL
, &status
))) {
2609 log_verbose(" %s\n", kw
);
2610 if(!strcmp(kw
,"japanese")) {
2611 if(foundStandard
== FALSE
) {
2612 foundStandard
= TRUE
;
2614 log_err("'japanese' was found twice in the calendar keyword list.\n");
2618 if(foundStandard
== FALSE
) {
2619 log_err_status(status
, "'japanese' was not found in the calendar keyword list.\n");
2621 uenum_close(kwVals
);
2622 if(U_FAILURE(status
)) {
2623 log_err_status(status
, "err %s getting calendar values\n", u_errorName(status
));
2627 static void TestGetFunctionalEquivalentOf(const char *path
, const char *resName
, const char *keyword
, UBool truncate
, const char * const testCases
[]) {
2629 for(i
=0;testCases
[i
];i
+=3) {
2630 UBool expectAvail
= (testCases
[i
][0]=='t')?TRUE
:FALSE
;
2631 UBool gotAvail
= FALSE
;
2632 const char *inLocale
= testCases
[i
+1];
2633 const char *expectLocale
= testCases
[i
+2];
2634 char equivLocale
[256];
2636 UErrorCode status
= U_ZERO_ERROR
;
2637 log_verbose("%d: %c %s\texpect %s\n",i
/3, expectAvail
?'t':'f', inLocale
, expectLocale
);
2638 len
= ures_getFunctionalEquivalent(equivLocale
, 255, path
,
2639 resName
, keyword
, inLocale
,
2640 &gotAvail
, truncate
, &status
);
2641 if(U_FAILURE(status
) || (len
<= 0)) {
2642 log_err_status(status
, "FAIL: got len %d, err %s on #%d: %c\t%s\t%s\n",
2643 len
, u_errorName(status
),
2644 i
/3,expectAvail
?'t':'f', inLocale
, expectLocale
);
2646 log_verbose("got: %c %s\n", expectAvail
?'t':'f',equivLocale
);
2648 if((gotAvail
!= expectAvail
) || strcmp(equivLocale
, expectLocale
)) {
2649 log_err("FAIL: #%d: %s -> expect avail=%c, loc=%s but get %c, loc=%s\n",
2651 expectAvail
?'t':'f', expectLocale
,
2652 gotAvail
?'t':'f', equivLocale
);
2659 static void TestGetFunctionalEquivalent(void) {
2660 #if !UCONFIG_NO_COLLATION
2661 static const char * const collCases
[] = {
2662 /* avail locale equiv */
2663 /* note: in ICU 64, empty locales are shown as available for collation */
2664 "f", "sv_US_CALIFORNIA", "sv",
2665 "f", "zh_TW@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_TW */
2666 "t", "zh_Hant_TW@collation=stroke", "zh@collation=stroke",
2667 "f", "sv_CN@collation=pinyin", "sv",
2668 "t", "zh@collation=pinyin", "zh",
2669 "f", "zh_CN@collation=pinyin", "zh", /* alias of zh_Hans_CN */
2670 "t", "zh_Hans_CN@collation=pinyin", "zh",
2671 "f", "zh_HK@collation=pinyin", "zh", /* alias of zh_Hant_HK */
2672 "t", "zh_Hant_HK@collation=pinyin", "zh",
2673 "f", "zh_HK@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_HK */
2674 "t", "zh_Hant_HK@collation=stroke", "zh@collation=stroke",
2675 "f", "zh_HK", "zh@collation=stroke", /* alias of zh_Hant_HK */
2676 "t", "zh_Hant_HK", "zh@collation=stroke",
2677 "f", "zh_MO", "zh@collation=stroke", /* alias of zh_Hant_MO */
2678 "t", "zh_Hant_MO", "zh@collation=stroke",
2679 "f", "zh_TW_STROKE", "zh@collation=stroke",
2680 "f", "zh_TW_STROKE@collation=pinyin", "zh",
2681 "f", "sv_CN@calendar=japanese", "sv",
2682 "t", "sv@calendar=japanese", "sv",
2683 "f", "zh_TW@collation=pinyin", "zh", /* alias of zh_Hant_TW */
2684 "t", "zh_Hant_TW@collation=pinyin", "zh",
2685 "f", "zh_CN@collation=stroke", "zh@collation=stroke", /* alias of zh_Hans_CN */
2686 "t", "zh_Hans_CN@collation=stroke", "zh@collation=stroke",
2687 "t", "de@collation=phonebook", "de@collation=phonebook",
2688 "t", "hi@collation=standard", "hi",
2689 "f", "hi_AU@collation=standard;currency=CHF;calendar=buddhist", "hi",
2690 "f", "sv_SE@collation=pinyin", "sv", /* bug 4582 tests */
2691 "f", "sv_SE_BONN@collation=pinyin", "sv",
2693 "f", "nl_NL", "root",
2694 "f", "nl_NL_EEXT", "root",
2695 "t", "nl@collation=stroke", "root",
2696 "f", "nl_NL@collation=stroke", "root",
2697 "f", "nl_NL_EEXT@collation=stroke", "root",
2698 /* Additions to test aliased locales */
2699 "f", "yue_HK", "zh@collation=stroke",
2700 "f", "yue_Hant", "zh@collation=stroke",
2701 "f", "yue_Hant_HK", "zh@collation=stroke",
2702 "f", "yue@collation=stroke", "zh@collation=stroke",
2703 "f", "yue@collation=pinyin", "zh",
2704 "f", "yue_CN", "zh",
2705 "f", "yue_Hans", "zh",
2706 "f", "yue_Hans_CN", "zh",
2707 "f", "yue_Hans@collation=pinyin", "zh",
2708 "f", "yue_Hans@collation=stroke", "zh@collation=stroke",
2709 "f", "mo", "mo", /* ? */
2710 "f", "no", "no", /* ? */
2711 "f", "ars", "ars", /* ? */
2712 "f", "wuu", "wuu", /* ? */
2713 /* Additions to test locales without resources */
2714 "f", "en_CN", "root",
2715 "f", "zh_Hant_CN", "zh@collation=stroke",
2716 "f", "zh_Hant_US", "zh@collation=stroke",
2717 "f", "zh_Hans_US", "zh",
2718 "f", "yue_TW", "zh@collation=stroke",
2719 "f", "yue_US", "zh@collation=stroke",
2724 #endif /* !UCONFIG_NO_COLLATION */
2726 static const char *calCases
[] = {
2727 /* avail locale equiv */
2728 "t", "en_US_POSIX", "en@calendar=gregorian",
2729 "f", "ja_JP_TOKYO", "ja@calendar=gregorian",
2730 "f", "ja_JP_TOKYO@calendar=japanese", "ja@calendar=japanese",
2731 "t", "sr@calendar=gregorian", "sr@calendar=gregorian",
2732 "t", "en", "en@calendar=gregorian",
2736 #if !UCONFIG_NO_COLLATION
2737 TestGetFunctionalEquivalentOf(U_ICUDATA_COLL
, "collations", "collation", TRUE
, collCases
);
2739 TestGetFunctionalEquivalentOf("ICUDATA", "calendar", "calendar", FALSE
, calCases
);
2741 #if !UCONFIG_NO_COLLATION
2742 log_verbose("Testing error conditions:\n");
2744 char equivLocale
[256] = "???";
2746 UErrorCode status
= U_ZERO_ERROR
;
2747 UBool gotAvail
= FALSE
;
2749 len
= ures_getFunctionalEquivalent(equivLocale
, 255, U_ICUDATA_COLL
,
2750 "calendar", "calendar", "ar_EG@calendar=islamic",
2751 &gotAvail
, FALSE
, &status
);
2752 (void)len
; /* Suppress set but not used warning. */
2754 if(status
== U_MISSING_RESOURCE_ERROR
) {
2755 log_verbose("PASS: Got expected U_MISSING_RESOURCE_ERROR\n");
2757 log_err("ures_getFunctionalEquivalent returned locale %s, avail %c, err %s, but expected U_MISSING_RESOURCE_ERROR \n",
2758 equivLocale
, gotAvail
?'t':'f', u_errorName(status
));
2764 static void TestXPath(void) {
2765 UErrorCode status
= U_ZERO_ERROR
;
2766 UResourceBundle
*rb
= NULL
, *alias
= NULL
;
2768 const UChar
* result
= NULL
;
2769 const UChar expResult
[] = { 0x0063, 0x006F, 0x0072, 0x0072, 0x0065, 0x0063, 0x0074, 0x0000 }; /* "correct" */
2770 /*const UChar expResult[] = { 0x0074, 0x0065, 0x0069, 0x006E, 0x0064, 0x0065, 0x0073, 0x0074, 0x0000 }; *//*teindest*/
2772 const char *testdatapath
=loadTestData(&status
);
2773 if(U_FAILURE(status
))
2775 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
2779 log_verbose("Testing ures_open()......\n");
2781 rb
= ures_open(testdatapath
, "te_IN", &status
);
2782 if(U_FAILURE(status
)) {
2783 log_err("Could not open te_IN (%s)\n", myErrorName(status
));
2786 alias
= ures_getByKey(rb
, "rootAliasClient", alias
, &status
);
2787 if(U_FAILURE(status
)) {
2788 log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status
));
2793 result
= tres_getString(alias
, -1, NULL
, &len
, &status
);
2794 if(U_FAILURE(status
) || result
== NULL
|| u_strcmp(result
, expResult
)) {
2795 log_err("Couldn't get correct string value (%s)\n", myErrorName(status
));
2798 alias
= ures_getByKey(rb
, "aliasClient", alias
, &status
);
2799 if(U_FAILURE(status
)) {
2800 log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status
));
2805 result
= tres_getString(alias
, -1, NULL
, &len
, &status
);
2806 if(U_FAILURE(status
) || result
== NULL
|| u_strcmp(result
, expResult
)) {
2807 log_err("Couldn't get correct string value (%s)\n", myErrorName(status
));
2810 alias
= ures_getByKey(rb
, "nestedRootAliasClient", alias
, &status
);
2811 if(U_FAILURE(status
)) {
2812 log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status
));
2817 result
= tres_getString(alias
, -1, NULL
, &len
, &status
);
2818 if(U_FAILURE(status
) || result
== NULL
|| u_strcmp(result
, expResult
)) {
2819 log_err("Couldn't get correct string value (%s)\n", myErrorName(status
));
2825 static void TestCLDRStyleAliases(void) {
2826 UErrorCode status
= U_ZERO_ERROR
;
2827 UResourceBundle
*rb
= NULL
, *alias
= NULL
, *a
=NULL
;
2830 const UChar
*result
= NULL
;
2831 UChar expected
[256];
2832 const char *expects
[7] = { "", "a41", "a12", "a03", "ar4" };
2833 const char *testdatapath
=loadTestData(&status
);
2834 if(U_FAILURE(status
)) {
2835 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
2838 log_verbose("Testing CLDR style aliases......\n");
2840 rb
= ures_open(testdatapath
, "te_IN_REVISED", &status
);
2841 if(U_FAILURE(status
)) {
2842 log_err("Could not open te_IN (%s)\n", myErrorName(status
));
2845 alias
= ures_getByKey(rb
, "a", alias
, &status
);
2846 if(U_FAILURE(status
)) {
2847 log_err("Couldn't find the aliased with name \"a\" resource (%s)\n", myErrorName(status
));
2851 for(i
= 1; i
< 5 ; i
++) {
2855 /* instead of sprintf(resource, "a%i", i); */
2856 a
= ures_getByKeyWithFallback(alias
, resource
, a
, &status
);
2857 result
= tres_getString(a
, -1, NULL
, &len
, &status
);
2858 u_charsToUChars(expects
[i
], expected
, (int32_t)strlen(expects
[i
])+1);
2859 if(U_FAILURE(status
) || !result
|| u_strcmp(result
, expected
)) {
2860 log_err("CLDR style aliases failed resource with name \"%s\" resource, exp %s, got %S (%s)\n", resource
, expects
[i
], result
, myErrorName(status
));
2861 status
= U_ZERO_ERROR
;
2870 static void TestFallbackCodes(void) {
2871 UErrorCode status
= U_ZERO_ERROR
;
2872 const char *testdatapath
=loadTestData(&status
);
2874 UResourceBundle
*res
= ures_open(testdatapath
, "te_IN", &status
);
2876 UResourceBundle
*r
= NULL
, *fall
= NULL
;
2878 r
= ures_getByKey(res
, "tagged_array_in_Root_te_te_IN", r
, &status
);
2880 status
= U_ZERO_ERROR
;
2881 fall
= ures_getByKeyWithFallback(r
, "tag2", fall
, &status
);
2883 if(status
!= U_ZERO_ERROR
) {
2884 log_data_err("Expected error code to be U_ZERO_ERROR, got %s\n", u_errorName(status
));
2885 status
= U_ZERO_ERROR
;
2888 fall
= ures_getByKeyWithFallback(r
, "tag7", fall
, &status
);
2890 if(status
!= U_USING_FALLBACK_WARNING
) {
2891 log_data_err("Expected error code to be U_USING_FALLBACK_WARNING, got %s\n", u_errorName(status
));
2893 status
= U_ZERO_ERROR
;
2895 fall
= ures_getByKeyWithFallback(r
, "tag1", fall
, &status
);
2897 if(status
!= U_USING_DEFAULT_WARNING
) {
2898 log_data_err("Expected error code to be U_USING_DEFAULT_WARNING, got %s\n", u_errorName(status
));
2900 status
= U_ZERO_ERROR
;
2907 /* This test will crash if this doesn't work. Results don't need testing. */
2908 static void TestStackReuse(void) {
2909 UResourceBundle table
;
2910 UErrorCode errorCode
= U_ZERO_ERROR
;
2911 UResourceBundle
*rb
= ures_open(NULL
, "en_US", &errorCode
);
2913 if(U_FAILURE(errorCode
)) {
2914 log_data_err("Could not load en_US locale. status=%s\n",myErrorName(errorCode
));
2917 ures_initStackObject(&table
);
2918 ures_getByKeyWithFallback(rb
, "Types", &table
, &errorCode
);
2919 ures_getByKeyWithFallback(&table
, "collation", &table
, &errorCode
);
2924 /* Test ures_getUTF8StringXYZ() --------------------------------------------- */
2927 * Replace most ures_getStringXYZ() with this function which wraps the
2928 * desired call and also calls the UTF-8 variant and checks that it works.
2930 extern const UChar
*
2931 tres_getString(const UResourceBundle
*resB
,
2932 int32_t idx
, const char *key
,
2934 UErrorCode
*status
) {
2940 int32_t length16
, length8
, i16
, i8
;
2943 if(length
== NULL
) {
2947 s16
= ures_getStringByIndex(resB
, idx
, length
, status
);
2948 } else if(key
!= NULL
) {
2949 s16
= ures_getStringByKey(resB
, key
, length
, status
);
2951 s16
= ures_getString(resB
, length
, status
);
2953 if(U_FAILURE(*status
)) {
2958 /* try the UTF-8 variant of ures_getStringXYZ() */
2959 for(forceCopy
= FALSE
; forceCopy
<= TRUE
; ++forceCopy
) {
2961 length8
= (int32_t)sizeof(buffer8
);
2963 s8
= ures_getUTF8StringByIndex(resB
, idx
, p8
, &length8
, forceCopy
, status
);
2964 } else if(key
!= NULL
) {
2965 s8
= ures_getUTF8StringByKey(resB
, key
, p8
, &length8
, forceCopy
, status
);
2967 s8
= ures_getUTF8String(resB
, p8
, &length8
, forceCopy
, status
);
2969 if(*status
== U_INVALID_CHAR_FOUND
) {
2970 /* the UTF-16 string contains an unpaired surrogate, can't test UTF-8 variant */
2973 if(*status
== U_BUFFER_OVERFLOW_ERROR
) {
2974 *status
= U_ZERO_ERROR
;
2975 p8
= (char *)malloc(++length8
);
2980 s8
= ures_getUTF8StringByIndex(resB
, idx
, p8
, &length8
, forceCopy
, status
);
2981 } else if(key
!= NULL
) {
2982 s8
= ures_getUTF8StringByKey(resB
, key
, p8
, &length8
, forceCopy
, status
);
2984 s8
= ures_getUTF8String(resB
, p8
, &length8
, forceCopy
, status
);
2987 if(U_FAILURE(*status
)) {
2988 /* something unexpected happened */
2995 if(forceCopy
&& s8
!= p8
) {
2996 log_err("ures_getUTF8String(%p, %ld, '%s') did not write the string to dest\n",
2997 resB
, (long)idx
, key
);
3000 /* verify NUL-termination */
3001 if((p8
!= buffer8
|| length8
< (int32_t)sizeof(buffer8
)) && s8
[length8
] != 0) {
3002 log_err("ures_getUTF8String(%p, %ld, '%s') did not NUL-terminate\n",
3003 resB
, (long)idx
, key
);
3005 /* verify correct string */
3007 while(i16
< length16
&& i8
< length8
) {
3008 U16_NEXT(s16
, i16
, length16
, c16
);
3009 U8_NEXT(s8
, i8
, length8
, c8
);
3011 log_err("ures_getUTF8String(%p, %ld, '%s') got a bad string, c16=U+%04lx!=U+%04lx=c8 before i16=%ld\n",
3012 resB
, (long)idx
, key
, (long)c16
, (long)c8
, (long)i16
);
3015 /* verify correct length */
3016 if(i16
< length16
) {
3017 log_err("ures_getUTF8String(%p, %ld, '%s') UTF-8 string too short, length8=%ld, length16=%ld\n",
3018 resB
, (long)idx
, key
, (long)length8
, (long)length16
);
3021 log_err("ures_getUTF8String(%p, %ld, '%s') UTF-8 string too long, length8=%ld, length16=%ld\n",
3022 resB
, (long)idx
, key
, (long)length8
, (long)length16
);
3034 * API tests for ures_getUTF8String().
3035 * Most cases are handled by tres_getString(), which leaves argument checking
3036 * to be tested here.
3037 * Since the variants share most of their implementation, we only need to test
3039 * We also need not test for checking arguments which will be checked by the
3040 * UTF-16 ures_getStringXYZ() that are called internally.
3043 TestGetUTF8String() {
3044 UResourceBundle
*res
;
3045 const char *testdatapath
;
3051 status
= U_ZERO_ERROR
;
3052 testdatapath
= loadTestData(&status
);
3053 if(U_FAILURE(status
)) {
3054 log_data_err("Could not load testdata.dat - %s\n", u_errorName(status
));
3058 res
= ures_open(testdatapath
, "", &status
);
3059 if(U_FAILURE(status
)) {
3060 log_err("Unable to ures_open(testdata, \"\") - %s\n", u_errorName(status
));
3065 status
= U_ZERO_ERROR
;
3066 length8
= (int32_t)sizeof(buffer8
);
3067 s8
= ures_getUTF8StringByKey(res
, "string_only_in_Root", buffer8
, &length8
, FALSE
, &status
);
3068 (void)s8
; /* Suppress set but not used warning. */
3069 if(status
!= U_ZERO_ERROR
) {
3070 log_err("ures_getUTF8StringByKey(testdata/root string) malfunctioned - %s\n", u_errorName(status
));
3073 /* negative capacity */
3074 status
= U_ZERO_ERROR
;
3076 s8
= ures_getUTF8StringByKey(res
, "string_only_in_Root", buffer8
, &length8
, FALSE
, &status
);
3077 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
3078 log_err("ures_getUTF8StringByKey(capacity<0) malfunctioned - %s\n", u_errorName(status
));
3081 /* capacity>0 but dest=NULL */
3082 status
= U_ZERO_ERROR
;
3083 length8
= (int32_t)sizeof(buffer8
);
3084 s8
= ures_getUTF8StringByKey(res
, "string_only_in_Root", NULL
, &length8
, FALSE
, &status
);
3085 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
3086 log_err("ures_getUTF8StringByKey(dest=NULL capacity>0) malfunctioned - %s\n", u_errorName(status
));
3092 static void TestCLDRVersion(void) {
3093 UVersionInfo zeroVersion
;
3094 UVersionInfo testExpect
;
3095 UVersionInfo testCurrent
;
3096 UVersionInfo cldrVersion
;
3098 UErrorCode status
= U_ZERO_ERROR
;
3100 /* setup the constant value */
3101 u_versionFromString(zeroVersion
, "0.0.0.0");
3103 /* test CLDR value from API */
3104 ulocdata_getCLDRVersion(cldrVersion
, &status
);
3105 if(U_FAILURE(status
)) {
3106 /* the show is pretty much over at this point */
3107 log_err_status(status
, "FAIL: ulocdata_getCLDRVersion() returned %s\n", u_errorName(status
));
3110 u_versionToString(cldrVersion
, tmp
);
3111 log_info("ulocdata_getCLDRVersion() returned: '%s'\n", tmp
);
3115 /* setup from resource bundle */
3117 UResourceBundle
*res
;
3118 const char *testdatapath
;
3120 status
= U_ZERO_ERROR
;
3121 testdatapath
= loadTestData(&status
);
3122 if(U_FAILURE(status
)) {
3123 log_data_err("Could not load testdata.dat - %s\n", u_errorName(status
));
3127 res
= ures_openDirect(testdatapath
, "root", &status
);
3128 if(U_FAILURE(status
)) {
3129 log_err("Unable to ures_open(testdata, \"\") - %s\n", u_errorName(status
3133 ures_getVersionByKey(res
, "ExpectCLDRVersionAtLeast", testExpect
, &status
);
3134 ures_getVersionByKey(res
, "CurrentCLDRVersion", testCurrent
, &status
);
3136 if(U_FAILURE(status
)) {
3137 log_err("Unable to get test data for CLDR version - %s\n", u_errorName(status
));
3140 if(U_FAILURE(status
)) return;
3143 u_versionToString(testExpect
,tmp
);
3144 log_verbose("(data) ExpectCLDRVersionAtLeast { %s }\n", tmp
);
3145 if(memcmp(cldrVersion
, testExpect
, sizeof(UVersionInfo
)) < 0) {
3146 log_data_err("CLDR version is too old, expect at least %s.", tmp
);
3148 u_versionToString(testCurrent
,tmp
);
3149 log_verbose("(data) CurrentCLDRVersion { %s }\n", tmp
);
3150 switch(memcmp(cldrVersion
, testCurrent
, sizeof(UVersionInfo
))) {
3151 case 0: break; /* OK- current. */
3152 case -1: log_info("CLDR version is behind 'current' (for testdata/root.txt) %s. Some things may fail.\n", tmp
); break;
3153 case 1: log_info("CLDR version is ahead of 'current' (for testdata/root.txt) %s. Some things may fail.\n", tmp
); break;