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 ********************************************************************/
9 #include "unicode/utypes.h"
13 #include "unicode/unistr.h"
14 #include "unicode/uniset.h"
15 #include "unicode/resbund.h"
23 //***************************************************************************************
25 static const UChar kErrorUChars
[] = { 0x45, 0x52, 0x52, 0x4f, 0x52, 0 };
26 static const int32_t kErrorLength
= 5;
28 //***************************************************************************************
38 //***************************************************************************************
40 #define CONFIRM_EQ(actual, expected, myAction) UPRV_BLOCK_MACRO_BEGIN { \
41 if ((expected)==(actual)) { \
42 record_pass(myAction); \
44 record_fail(myAction + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of " + (expected) + "\n"); \
46 } UPRV_BLOCK_MACRO_END
47 #define CONFIRM_GE(actual, expected, myAction) UPRV_BLOCK_MACRO_BEGIN { \
48 if ((actual)>=(expected)) { \
49 record_pass(myAction); \
51 record_fail(myAction + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x >= " + (expected) + "\n"); \
53 } UPRV_BLOCK_MACRO_END
54 #define CONFIRM_NE(actual, expected, myAction) UPRV_BLOCK_MACRO_BEGIN { \
55 if ((expected)!=(actual)) { \
56 record_pass(myAction); \
58 record_fail(myAction + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x != " + (expected) + "\n"); \
60 } UPRV_BLOCK_MACRO_END
62 #define CONFIRM_UErrorCode(actual, expected, myAction) UPRV_BLOCK_MACRO_BEGIN { \
63 if ((expected)==(actual)) { \
64 record_pass(myAction); \
66 record_fail(myAction + (UnicodeString)" returned " + u_errorName(actual) + " instead of " + u_errorName(expected) + "\n"); \
68 } UPRV_BLOCK_MACRO_END
70 //***************************************************************************************
73 * Convert an integer, positive or negative, to a character string radix 10.
76 itoa(int32_t i
, char* buf
)
87 // Output digits in reverse order
91 *p
++ = (char)('0' + (i
% 10));
110 //***************************************************************************************
112 // Array of our test objects
118 UErrorCode expected_constructor_status
;
120 UBool like
[e_Where_count
];
121 UBool inherits
[e_Where_count
];
126 // "IN" means inherits
127 // "NE" or "ne" means "does not exist"
129 { "root", NULL
, U_ZERO_ERROR
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} },
130 { "te", NULL
, U_ZERO_ERROR
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
131 { "te_IN", NULL
, U_ZERO_ERROR
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
132 { "te_NE", NULL
, U_USING_FALLBACK_WARNING
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
133 { "te_IN_NE", NULL
, U_USING_FALLBACK_WARNING
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
134 { "ne", NULL
, U_USING_DEFAULT_WARNING
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} }
137 static const int32_t bundles_count
= UPRV_LENGTHOF(param
);
139 //***************************************************************************************
142 * Return a random unsigned long l where 0N <= l <= ULONG_MAX.
148 static UBool initialized
= FALSE
;
151 srand((unsigned)time(NULL
));
154 // Assume rand has at least 12 bits of precision
156 for (uint32_t i
=0; i
<sizeof(l
); ++i
)
157 ((char*)&l
)[i
] = (char)((rand() & 0x0FF0) >> 4);
162 * Return a random double x where 0.0 <= x < 1.0.
167 return (double)(randul() / ULONG_MAX
);
171 * Return a random integer i where 0 <= i < n.
173 int32_t randi(int32_t n
)
175 return (int32_t)(randd() * n
);
178 //***************************************************************************************
181 Don't use more than one of these at a time because of the Locale names
183 ResourceBundleTest::ResourceBundleTest()
187 if (param
[5].locale
== NULL
) {
188 param
[0].locale
= new Locale("root");
189 param
[1].locale
= new Locale("te");
190 param
[2].locale
= new Locale("te", "IN");
191 param
[3].locale
= new Locale("te", "NE");
192 param
[4].locale
= new Locale("te", "IN", "NE");
193 param
[5].locale
= new Locale("ne");
197 ResourceBundleTest::~ResourceBundleTest()
199 if (param
[5].locale
) {
201 for (idx
= 0; idx
< UPRV_LENGTHOF(param
); idx
++) {
202 delete param
[idx
].locale
;
203 param
[idx
].locale
= NULL
;
208 void ResourceBundleTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
210 if (exec
) logln("TestSuite ResourceBundleTest: ");
212 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
213 case 0: name
= "TestResourceBundles"; if (exec
) TestResourceBundles(); break;
214 case 1: name
= "TestConstruction"; if (exec
) TestConstruction(); break;
215 case 2: name
= "TestGetSize"; if (exec
) TestGetSize(); break;
216 case 3: name
= "TestGetLocaleByType"; if (exec
) TestGetLocaleByType(); break;
218 case 0: case 1: case 2: case 3: name
= "skip"; break;
221 case 4: name
= "TestExemplar"; if (exec
) TestExemplar(); break;
222 default: name
= ""; break; //needed to end loop
226 //***************************************************************************************
229 ResourceBundleTest::TestResourceBundles()
231 UErrorCode status
= U_ZERO_ERROR
;
233 loadTestData(status
);
234 if(U_FAILURE(status
))
236 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status
)));
240 /* Make sure that users using te_IN for the default locale don't get test failures. */
241 Locale originalDefault
;
242 if (Locale::getDefault() == Locale("te_IN")) {
243 Locale::setDefault(Locale("en_US"), status
);
246 testTag("only_in_Root", TRUE
, FALSE
, FALSE
);
247 testTag("only_in_te", FALSE
, TRUE
, FALSE
);
248 testTag("only_in_te_IN", FALSE
, FALSE
, TRUE
);
249 testTag("in_Root_te", TRUE
, TRUE
, FALSE
);
250 testTag("in_Root_te_te_IN", TRUE
, TRUE
, TRUE
);
251 testTag("in_Root_te_IN", TRUE
, FALSE
, TRUE
);
252 testTag("in_te_te_IN", FALSE
, TRUE
, TRUE
);
253 testTag("nonexistent", FALSE
, FALSE
, FALSE
);
254 logln("Passed: %d\nFailed: %d", pass
, fail
);
256 /* Restore the default locale for the other tests. */
257 Locale::setDefault(originalDefault
, status
);
261 ResourceBundleTest::TestConstruction()
263 UErrorCode err
= U_ZERO_ERROR
;
264 Locale
locale("te", "IN");
266 const char* testdatapath
=loadTestData(err
);
269 dataerrln("Could not load testdata.dat " + UnicodeString(testdatapath
) + ", " + UnicodeString(u_errorName(err
)));
273 /* Make sure that users using te_IN for the default locale don't get test failures. */
274 Locale originalDefault
;
275 if (Locale::getDefault() == Locale("te_IN")) {
276 Locale::setDefault(Locale("en_US"), err
);
279 ResourceBundle
test1((UnicodeString
)testdatapath
, err
);
280 ResourceBundle
test2(testdatapath
, locale
, err
);
281 //ResourceBundle test1("c:\\icu\\icu\\source\\test\\testdata\\testdata", err);
282 //ResourceBundle test2("c:\\icu\\icu\\source\\test\\testdata\\testdata", locale, err);
284 UnicodeString
result1(test1
.getStringEx("string_in_Root_te_te_IN", err
));
285 UnicodeString
result2(test2
.getStringEx("string_in_Root_te_te_IN", err
));
287 if (U_FAILURE(err
)) {
288 errln("Something threw an error in TestConstruction()");
292 logln("for string_in_Root_te_te_IN, default.txt had " + result1
);
293 logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2
);
295 if (result1
!= "ROOT" || result2
!= "TE_IN")
296 errln("Construction test failed; run verbose for more information");
298 const char* version1
;
299 const char* version2
;
301 version1
= test1
.getVersionNumber();
302 version2
= test2
.getVersionNumber();
304 char *versionID1
= new char[1+strlen(version1
)]; // + 1 for zero byte
305 char *versionID2
= new char[1+ strlen(version2
)]; // + 1 for zero byte
307 strcpy(versionID1
, "44.0"); // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed.
309 strcpy(versionID2
, "55.0"); // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed.
311 logln(UnicodeString("getVersionNumber on default.txt returned ") + version1
);
312 logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2
);
314 if (strcmp(version1
, versionID1
) != 0 || strcmp(version2
, versionID2
) != 0)
315 errln("getVersionNumber() failed");
320 /* Restore the default locale for the other tests. */
321 Locale::setDefault(originalDefault
, err
);
324 //***************************************************************************************
327 ResourceBundleTest::testTag(const char* frag
,
332 int32_t failOrig
= fail
;
334 // Make array from input params
336 UBool is_in
[] = { in_Root
, in_te
, in_te_IN
};
338 const char* NAME
[] = { "ROOT", "TE", "TE_IN" };
340 // Now try to load the desired items
343 UnicodeString action
;
345 int32_t i
,j
,actual_bundle
;
348 UErrorCode status
= U_ZERO_ERROR
;
349 const char* testdatapath
;
350 testdatapath
=loadTestData(status
);
351 if(U_FAILURE(status
))
353 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status
)));
357 for (i
=0; i
<bundles_count
; ++i
)
359 action
= "Constructor for ";
360 action
+= param
[i
].name
;
362 status
= U_ZERO_ERROR
;
363 ResourceBundle
theBundle( testdatapath
, *param
[i
].locale
, status
);
364 //ResourceBundle theBundle( "c:\\icu\\icu\\source\\test\\testdata\\testdata", *param[i].locale, status);
365 CONFIRM_UErrorCode(status
, param
[i
].expected_constructor_status
, action
);
368 actual_bundle
= 0; /* ne -> default */
370 actual_bundle
= 1; /* te_NE -> te */
372 actual_bundle
= 2; /* te_IN_NE -> te_IN */
377 UErrorCode expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
378 for (j
=e_te_IN
; j
>=e_Root
; --j
)
380 if (is_in
[j
] && param
[i
].inherits
[j
])
382 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
383 expected_resource_status
= U_ZERO_ERROR
;
385 expected_resource_status
= U_USING_DEFAULT_WARNING
;
387 expected_resource_status
= U_USING_FALLBACK_WARNING
;
393 UErrorCode expected_status
;
396 for (j
=param
[i
].where
; j
>=0; --j
)
405 //--------------------------------------------------------------------------
408 uprv_strcpy(tag
, "string_");
409 uprv_strcat(tag
, frag
);
411 action
= param
[i
].name
;
412 action
+= ".getString(";
417 status
= U_ZERO_ERROR
;
419 UnicodeString
string(theBundle
.getStringEx(tag
, status
));
421 if(U_FAILURE(status
)) {
422 string
.setTo(TRUE
, kErrorUChars
, kErrorLength
);
425 CONFIRM_UErrorCode(status
, expected_resource_status
, action
);
427 UnicodeString
expected_string(kErrorUChars
);
428 if (U_SUCCESS(status
)) {
429 expected_string
= base
;
432 CONFIRM_EQ(string
, expected_string
, action
);
434 //--------------------------------------------------------------------------
437 uprv_strcpy(tag
, "array_");
438 uprv_strcat(tag
, frag
);
440 action
= param
[i
].name
;
445 status
= U_ZERO_ERROR
;
446 ResourceBundle
arrayBundle(theBundle
.get(tag
, status
));
447 CONFIRM_UErrorCode(status
, expected_resource_status
, action
);
448 int32_t count
= arrayBundle
.getSize();
450 if (U_SUCCESS(status
))
452 CONFIRM_GE(count
, 1, action
);
454 for (j
=0; j
< count
; ++j
)
457 UnicodeString
value(arrayBundle
.getStringEx(j
, status
));
458 expected_string
= base
;
459 expected_string
+= itoa(j
,buf
);
460 CONFIRM_EQ(value
, expected_string
, action
);
463 action
= param
[i
].name
;
464 action
+= ".getStringEx(";
468 for (j
=0; j
<100; ++j
)
470 index
= count
? (randi(count
* 3) - count
) : (randi(200) - 100);
471 status
= U_ZERO_ERROR
;
472 string
= kErrorUChars
;
473 UnicodeString
t(arrayBundle
.getStringEx(index
, status
));
474 expected_status
= (index
>= 0 && index
< count
) ? expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
475 CONFIRM_UErrorCode(status
, expected_status
, action
);
477 if (U_SUCCESS(status
))
480 expected_string
= base
;
481 expected_string
+= itoa(index
,buf
);
485 expected_string
= kErrorUChars
;
487 CONFIRM_EQ(string
, expected_string
, action
);
490 else if (status
!= expected_resource_status
)
492 record_fail("Error getting " + (UnicodeString
)tag
);
493 return (UBool
)(failOrig
!= fail
);
498 return (UBool
)(failOrig
!= fail
);
502 ResourceBundleTest::record_pass(UnicodeString passMessage
)
508 ResourceBundleTest::record_fail(UnicodeString errMessage
)
515 ResourceBundleTest::TestExemplar(){
517 int32_t locCount
= uloc_countAvailable();
520 UErrorCode status
= U_ZERO_ERROR
;
521 for(;locIndex
<locCount
;locIndex
++){
522 const char* locale
= uloc_getAvailable(locIndex
);
523 UResourceBundle
*resb
=ures_open(NULL
,locale
,&status
);
524 if(U_SUCCESS(status
) && status
!=U_USING_FALLBACK_WARNING
&& status
!=U_USING_DEFAULT_WARNING
){
526 const UChar
* strSet
= ures_getStringByKey(resb
,"ExemplarCharacters",&len
,&status
);
527 UnicodeSet
set(strSet
,status
);
528 if(U_FAILURE(status
)){
529 errln("Could not construct UnicodeSet from pattern for ExemplarCharacters in locale : %s. Error: %s",locale
,u_errorName(status
));
536 logln("Number of installed locales with exemplar characters that could be tested: %d",num
);
541 ResourceBundleTest::TestGetSize(void)
550 { "integerarray", 1},
553 { "emptystring", 1}, /* empty string is still a string */
557 { "collations", 1}, /* not 2 - there is hidden %%CollationBin */
560 UErrorCode status
= U_ZERO_ERROR
;
562 const char* testdatapath
= loadTestData(status
);
563 int32_t i
= 0, j
= 0;
566 if(U_FAILURE(status
))
568 dataerrln("Could not load testdata.dat %s\n", u_errorName(status
));
572 ResourceBundle
rb(testdatapath
, "testtypes", status
);
573 if(U_FAILURE(status
))
575 err("Could not testtypes resource bundle %s\n", u_errorName(status
));
579 for(i
= 0; i
< UPRV_LENGTHOF(test
); i
++) {
580 ResourceBundle res
= rb
.get(test
[i
].key
, status
);
581 if(U_FAILURE(status
))
583 err("Couldn't find the key %s. Error: %s\n", u_errorName(status
));
586 size
= res
.getSize();
587 if(size
!= test
[i
].size
) {
588 err("Expected size %i, got size %i for key %s\n", test
[i
].size
, size
, test
[i
].key
);
589 for(j
= 0; j
< size
; j
++) {
590 ResourceBundle helper
= res
.get(j
, status
);
591 err("%s\n", helper
.getKey());
598 ResourceBundleTest::TestGetLocaleByType(void)
601 const char *requestedLocale
;
602 const char *resourceKey
;
603 const char *validLocale
;
604 const char *actualLocale
;
606 { "te_IN_BLAH", "string_only_in_te_IN", "te_IN", "te_IN" },
607 { "te_IN_BLAH", "string_only_in_te", "te_IN", "te" },
608 { "te_IN_BLAH", "string_only_in_Root", "te_IN", "root" },
609 { "te_IN_BLAH_01234567890_01234567890_01234567890_01234567890_01234567890_01234567890", "array_2d_only_in_Root", "te_IN", "root" },
610 { "te_IN_BLAH@currency=euro", "array_2d_only_in_te_IN", "te_IN", "te_IN" },
611 { "te_IN_BLAH@calendar=thai;collation=phonebook", "array_2d_only_in_te", "te_IN", "te" }
614 UErrorCode status
= U_ZERO_ERROR
;
616 const char* testdatapath
= loadTestData(status
);
620 if(U_FAILURE(status
))
622 dataerrln("Could not load testdata.dat %s\n", u_errorName(status
));
626 for(i
= 0; i
< UPRV_LENGTHOF(test
); i
++) {
627 ResourceBundle
rb(testdatapath
, test
[i
].requestedLocale
, status
);
628 if(U_FAILURE(status
))
630 err("Could not open resource bundle %s (error %s)\n", test
[i
].requestedLocale
, u_errorName(status
));
631 status
= U_ZERO_ERROR
;
635 ResourceBundle res
= rb
.get(test
[i
].resourceKey
, status
);
636 if(U_FAILURE(status
))
638 err("Couldn't find the key %s. Error: %s\n", test
[i
].resourceKey
, u_errorName(status
));
639 status
= U_ZERO_ERROR
;
643 locale
= res
.getLocale(ULOC_REQUESTED_LOCALE
, status
);
644 if(U_SUCCESS(status
) && locale
!= Locale::getDefault()) {
645 err("Expected requested locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
.getName());
647 status
= U_ZERO_ERROR
;
648 locale
= res
.getLocale(ULOC_VALID_LOCALE
, status
);
649 if(strcmp(locale
.getName(), test
[i
].validLocale
) != 0) {
650 err("Expected valid locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
.getName());
652 locale
= res
.getLocale(ULOC_ACTUAL_LOCALE
, status
);
653 if(strcmp(locale
.getName(), test
[i
].actualLocale
) != 0) {
654 err("Expected actual locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
.getName());