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) if ((expected)==(actual)) { record_pass(myAction); } else { record_fail(myAction + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of " + (expected) + "\n");}
41 #define CONFIRM_GE(actual, expected, myAction) if ((actual)>=(expected)) { record_pass(myAction); } else { record_fail(myAction + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x >= " + (expected) + "\n");}
42 #define CONFIRM_NE(actual, expected, myAction) if ((expected)!=(actual)) { record_pass(myAction); } else { record_fail(myAction + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x != " + (expected) + "\n");}
44 #define CONFIRM_UErrorCode(actual, expected, myAction) if ((expected)==(actual)) { record_pass(myAction); } else { record_fail(myAction + (UnicodeString)" returned " + u_errorName(actual) + " instead of " + u_errorName(expected) + "\n"); }
46 //***************************************************************************************
49 * Convert an integer, positive or negative, to a character string radix 10.
52 itoa(int32_t i
, char* buf
)
63 // Output digits in reverse order
67 *p
++ = (char)('0' + (i
% 10));
86 //***************************************************************************************
88 // Array of our test objects
94 UErrorCode expected_constructor_status
;
96 UBool like
[e_Where_count
];
97 UBool inherits
[e_Where_count
];
102 // "IN" means inherits
103 // "NE" or "ne" means "does not exist"
105 { "root", NULL
, U_ZERO_ERROR
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} },
106 { "te", NULL
, U_ZERO_ERROR
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
107 { "te_IN", NULL
, U_ZERO_ERROR
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
108 { "te_NE", NULL
, U_USING_FALLBACK_WARNING
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
109 { "te_IN_NE", NULL
, U_USING_FALLBACK_WARNING
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
110 { "ne", NULL
, U_USING_DEFAULT_WARNING
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} }
113 static const int32_t bundles_count
= UPRV_LENGTHOF(param
);
115 //***************************************************************************************
118 * Return a random unsigned long l where 0N <= l <= ULONG_MAX.
124 static UBool initialized
= FALSE
;
127 srand((unsigned)time(NULL
));
130 // Assume rand has at least 12 bits of precision
132 for (uint32_t i
=0; i
<sizeof(l
); ++i
)
133 ((char*)&l
)[i
] = (char)((rand() & 0x0FF0) >> 4);
138 * Return a random double x where 0.0 <= x < 1.0.
143 return (double)(randul() / ULONG_MAX
);
147 * Return a random integer i where 0 <= i < n.
149 int32_t randi(int32_t n
)
151 return (int32_t)(randd() * n
);
154 //***************************************************************************************
157 Don't use more than one of these at a time because of the Locale names
159 ResourceBundleTest::ResourceBundleTest()
163 if (param
[5].locale
== NULL
) {
164 param
[0].locale
= new Locale("root");
165 param
[1].locale
= new Locale("te");
166 param
[2].locale
= new Locale("te", "IN");
167 param
[3].locale
= new Locale("te", "NE");
168 param
[4].locale
= new Locale("te", "IN", "NE");
169 param
[5].locale
= new Locale("ne");
173 ResourceBundleTest::~ResourceBundleTest()
175 if (param
[5].locale
) {
177 for (idx
= 0; idx
< UPRV_LENGTHOF(param
); idx
++) {
178 delete param
[idx
].locale
;
179 param
[idx
].locale
= NULL
;
184 void ResourceBundleTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
186 if (exec
) logln("TestSuite ResourceBundleTest: ");
188 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
189 case 0: name
= "TestResourceBundles"; if (exec
) TestResourceBundles(); break;
190 case 1: name
= "TestConstruction"; if (exec
) TestConstruction(); break;
191 case 2: name
= "TestGetSize"; if (exec
) TestGetSize(); break;
192 case 3: name
= "TestGetLocaleByType"; if (exec
) TestGetLocaleByType(); break;
194 case 0: case 1: case 2: case 3: name
= "skip"; break;
197 case 4: name
= "TestExemplar"; if (exec
) TestExemplar(); break;
198 default: name
= ""; break; //needed to end loop
202 //***************************************************************************************
205 ResourceBundleTest::TestResourceBundles()
207 UErrorCode status
= U_ZERO_ERROR
;
209 loadTestData(status
);
210 if(U_FAILURE(status
))
212 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status
)));
216 /* Make sure that users using te_IN for the default locale don't get test failures. */
217 Locale originalDefault
;
218 if (Locale::getDefault() == Locale("te_IN")) {
219 Locale::setDefault(Locale("en_US"), status
);
222 testTag("only_in_Root", TRUE
, FALSE
, FALSE
);
223 testTag("only_in_te", FALSE
, TRUE
, FALSE
);
224 testTag("only_in_te_IN", FALSE
, FALSE
, TRUE
);
225 testTag("in_Root_te", TRUE
, TRUE
, FALSE
);
226 testTag("in_Root_te_te_IN", TRUE
, TRUE
, TRUE
);
227 testTag("in_Root_te_IN", TRUE
, FALSE
, TRUE
);
228 testTag("in_te_te_IN", FALSE
, TRUE
, TRUE
);
229 testTag("nonexistent", FALSE
, FALSE
, FALSE
);
230 logln("Passed: %d\nFailed: %d", pass
, fail
);
232 /* Restore the default locale for the other tests. */
233 Locale::setDefault(originalDefault
, status
);
237 ResourceBundleTest::TestConstruction()
239 UErrorCode err
= U_ZERO_ERROR
;
240 Locale
locale("te", "IN");
242 const char* testdatapath
=loadTestData(err
);
245 dataerrln("Could not load testdata.dat " + UnicodeString(testdatapath
) + ", " + UnicodeString(u_errorName(err
)));
249 /* Make sure that users using te_IN for the default locale don't get test failures. */
250 Locale originalDefault
;
251 if (Locale::getDefault() == Locale("te_IN")) {
252 Locale::setDefault(Locale("en_US"), err
);
255 ResourceBundle
test1((UnicodeString
)testdatapath
, err
);
256 ResourceBundle
test2(testdatapath
, locale
, err
);
257 //ResourceBundle test1("c:\\icu\\icu\\source\\test\\testdata\\testdata", err);
258 //ResourceBundle test2("c:\\icu\\icu\\source\\test\\testdata\\testdata", locale, err);
260 UnicodeString
result1(test1
.getStringEx("string_in_Root_te_te_IN", err
));
261 UnicodeString
result2(test2
.getStringEx("string_in_Root_te_te_IN", err
));
263 if (U_FAILURE(err
)) {
264 errln("Something threw an error in TestConstruction()");
268 logln("for string_in_Root_te_te_IN, default.txt had " + result1
);
269 logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2
);
271 if (result1
!= "ROOT" || result2
!= "TE_IN")
272 errln("Construction test failed; run verbose for more information");
274 const char* version1
;
275 const char* version2
;
277 version1
= test1
.getVersionNumber();
278 version2
= test2
.getVersionNumber();
280 char *versionID1
= new char[1+strlen(version1
)]; // + 1 for zero byte
281 char *versionID2
= new char[1+ strlen(version2
)]; // + 1 for zero byte
283 strcpy(versionID1
, "44.0"); // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed.
285 strcpy(versionID2
, "55.0"); // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed.
287 logln(UnicodeString("getVersionNumber on default.txt returned ") + version1
);
288 logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2
);
290 if (strcmp(version1
, versionID1
) != 0 || strcmp(version2
, versionID2
) != 0)
291 errln("getVersionNumber() failed");
296 /* Restore the default locale for the other tests. */
297 Locale::setDefault(originalDefault
, err
);
300 //***************************************************************************************
303 ResourceBundleTest::testTag(const char* frag
,
308 int32_t failOrig
= fail
;
310 // Make array from input params
312 UBool is_in
[] = { in_Root
, in_te
, in_te_IN
};
314 const char* NAME
[] = { "ROOT", "TE", "TE_IN" };
316 // Now try to load the desired items
319 UnicodeString action
;
321 int32_t i
,j
,actual_bundle
;
324 UErrorCode status
= U_ZERO_ERROR
;
325 const char* testdatapath
;
326 testdatapath
=loadTestData(status
);
327 if(U_FAILURE(status
))
329 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status
)));
333 for (i
=0; i
<bundles_count
; ++i
)
335 action
= "Constructor for ";
336 action
+= param
[i
].name
;
338 status
= U_ZERO_ERROR
;
339 ResourceBundle
theBundle( testdatapath
, *param
[i
].locale
, status
);
340 //ResourceBundle theBundle( "c:\\icu\\icu\\source\\test\\testdata\\testdata", *param[i].locale, status);
341 CONFIRM_UErrorCode(status
, param
[i
].expected_constructor_status
, action
);
344 actual_bundle
= 0; /* ne -> default */
346 actual_bundle
= 1; /* te_NE -> te */
348 actual_bundle
= 2; /* te_IN_NE -> te_IN */
353 UErrorCode expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
354 for (j
=e_te_IN
; j
>=e_Root
; --j
)
356 if (is_in
[j
] && param
[i
].inherits
[j
])
358 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
359 expected_resource_status
= U_ZERO_ERROR
;
361 expected_resource_status
= U_USING_DEFAULT_WARNING
;
363 expected_resource_status
= U_USING_FALLBACK_WARNING
;
369 UErrorCode expected_status
;
372 for (j
=param
[i
].where
; j
>=0; --j
)
381 //--------------------------------------------------------------------------
384 uprv_strcpy(tag
, "string_");
385 uprv_strcat(tag
, frag
);
387 action
= param
[i
].name
;
388 action
+= ".getString(";
393 status
= U_ZERO_ERROR
;
395 UnicodeString
string(theBundle
.getStringEx(tag
, status
));
397 if(U_FAILURE(status
)) {
398 string
.setTo(TRUE
, kErrorUChars
, kErrorLength
);
401 CONFIRM_UErrorCode(status
, expected_resource_status
, action
);
403 UnicodeString
expected_string(kErrorUChars
);
404 if (U_SUCCESS(status
)) {
405 expected_string
= base
;
408 CONFIRM_EQ(string
, expected_string
, action
);
410 //--------------------------------------------------------------------------
413 uprv_strcpy(tag
, "array_");
414 uprv_strcat(tag
, frag
);
416 action
= param
[i
].name
;
421 status
= U_ZERO_ERROR
;
422 ResourceBundle
arrayBundle(theBundle
.get(tag
, status
));
423 CONFIRM_UErrorCode(status
, expected_resource_status
, action
);
424 int32_t count
= arrayBundle
.getSize();
426 if (U_SUCCESS(status
))
428 CONFIRM_GE(count
, 1, action
);
430 for (j
=0; j
< count
; ++j
)
433 UnicodeString
value(arrayBundle
.getStringEx(j
, status
));
434 expected_string
= base
;
435 expected_string
+= itoa(j
,buf
);
436 CONFIRM_EQ(value
, expected_string
, action
);
439 action
= param
[i
].name
;
440 action
+= ".getStringEx(";
444 for (j
=0; j
<100; ++j
)
446 index
= count
? (randi(count
* 3) - count
) : (randi(200) - 100);
447 status
= U_ZERO_ERROR
;
448 string
= kErrorUChars
;
449 UnicodeString
t(arrayBundle
.getStringEx(index
, status
));
450 expected_status
= (index
>= 0 && index
< count
) ? expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
451 CONFIRM_UErrorCode(status
, expected_status
, action
);
453 if (U_SUCCESS(status
))
456 expected_string
= base
;
457 expected_string
+= itoa(index
,buf
);
461 expected_string
= kErrorUChars
;
463 CONFIRM_EQ(string
, expected_string
, action
);
466 else if (status
!= expected_resource_status
)
468 record_fail("Error getting " + (UnicodeString
)tag
);
469 return (UBool
)(failOrig
!= fail
);
474 return (UBool
)(failOrig
!= fail
);
478 ResourceBundleTest::record_pass(UnicodeString passMessage
)
484 ResourceBundleTest::record_fail(UnicodeString errMessage
)
491 ResourceBundleTest::TestExemplar(){
493 int32_t locCount
= uloc_countAvailable();
496 UErrorCode status
= U_ZERO_ERROR
;
497 for(;locIndex
<locCount
;locIndex
++){
498 const char* locale
= uloc_getAvailable(locIndex
);
499 UResourceBundle
*resb
=ures_open(NULL
,locale
,&status
);
500 if(U_SUCCESS(status
) && status
!=U_USING_FALLBACK_WARNING
&& status
!=U_USING_DEFAULT_WARNING
){
502 const UChar
* strSet
= ures_getStringByKey(resb
,"ExemplarCharacters",&len
,&status
);
503 UnicodeSet
set(strSet
,status
);
504 if(U_FAILURE(status
)){
505 errln("Could not construct UnicodeSet from pattern for ExemplarCharacters in locale : %s. Error: %s",locale
,u_errorName(status
));
512 logln("Number of installed locales with exemplar characters that could be tested: %d",num
);
517 ResourceBundleTest::TestGetSize(void)
526 { "integerarray", 1},
529 { "emptystring", 1}, /* empty string is still a string */
533 { "collations", 1}, /* not 2 - there is hidden %%CollationBin */
536 UErrorCode status
= U_ZERO_ERROR
;
538 const char* testdatapath
= loadTestData(status
);
539 int32_t i
= 0, j
= 0;
542 if(U_FAILURE(status
))
544 dataerrln("Could not load testdata.dat %s\n", u_errorName(status
));
548 ResourceBundle
rb(testdatapath
, "testtypes", status
);
549 if(U_FAILURE(status
))
551 err("Could not testtypes resource bundle %s\n", u_errorName(status
));
555 for(i
= 0; i
< UPRV_LENGTHOF(test
); i
++) {
556 ResourceBundle res
= rb
.get(test
[i
].key
, status
);
557 if(U_FAILURE(status
))
559 err("Couldn't find the key %s. Error: %s\n", u_errorName(status
));
562 size
= res
.getSize();
563 if(size
!= test
[i
].size
) {
564 err("Expected size %i, got size %i for key %s\n", test
[i
].size
, size
, test
[i
].key
);
565 for(j
= 0; j
< size
; j
++) {
566 ResourceBundle helper
= res
.get(j
, status
);
567 err("%s\n", helper
.getKey());
574 ResourceBundleTest::TestGetLocaleByType(void)
577 const char *requestedLocale
;
578 const char *resourceKey
;
579 const char *validLocale
;
580 const char *actualLocale
;
582 { "te_IN_BLAH", "string_only_in_te_IN", "te_IN", "te_IN" },
583 { "te_IN_BLAH", "string_only_in_te", "te_IN", "te" },
584 { "te_IN_BLAH", "string_only_in_Root", "te_IN", "root" },
585 { "te_IN_BLAH_01234567890_01234567890_01234567890_01234567890_01234567890_01234567890", "array_2d_only_in_Root", "te_IN", "root" },
586 { "te_IN_BLAH@currency=euro", "array_2d_only_in_te_IN", "te_IN", "te_IN" },
587 { "te_IN_BLAH@calendar=thai;collation=phonebook", "array_2d_only_in_te", "te_IN", "te" }
590 UErrorCode status
= U_ZERO_ERROR
;
592 const char* testdatapath
= loadTestData(status
);
596 if(U_FAILURE(status
))
598 dataerrln("Could not load testdata.dat %s\n", u_errorName(status
));
602 for(i
= 0; i
< UPRV_LENGTHOF(test
); i
++) {
603 ResourceBundle
rb(testdatapath
, test
[i
].requestedLocale
, status
);
604 if(U_FAILURE(status
))
606 err("Could not open resource bundle %s (error %s)\n", test
[i
].requestedLocale
, u_errorName(status
));
607 status
= U_ZERO_ERROR
;
611 ResourceBundle res
= rb
.get(test
[i
].resourceKey
, status
);
612 if(U_FAILURE(status
))
614 err("Couldn't find the key %s. Error: %s\n", test
[i
].resourceKey
, u_errorName(status
));
615 status
= U_ZERO_ERROR
;
619 locale
= res
.getLocale(ULOC_REQUESTED_LOCALE
, status
);
620 if(U_SUCCESS(status
) && locale
!= Locale::getDefault()) {
621 err("Expected requested locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
.getName());
623 status
= U_ZERO_ERROR
;
624 locale
= res
.getLocale(ULOC_VALID_LOCALE
, status
);
625 if(strcmp(locale
.getName(), test
[i
].validLocale
) != 0) {
626 err("Expected valid locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
.getName());
628 locale
= res
.getLocale(ULOC_ACTUAL_LOCALE
, status
);
629 if(strcmp(locale
.getName(), test
[i
].actualLocale
) != 0) {
630 err("Expected actual locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
.getName());