1 /********************************************************************
3 * Copyright (c) 1997-2014, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/utypes.h"
10 #include "unicode/unistr.h"
11 #include "unicode/uniset.h"
12 #include "unicode/resbund.h"
20 //***************************************************************************************
22 static const UChar kErrorUChars
[] = { 0x45, 0x52, 0x52, 0x4f, 0x52, 0 };
23 static const int32_t kErrorLength
= 5;
25 //***************************************************************************************
35 //***************************************************************************************
37 #define CONFIRM_EQ(actual, expected, myAction) if ((expected)==(actual)) { record_pass(myAction); } else { record_fail(myAction + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of " + (expected) + "\n");}
38 #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");}
39 #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");}
41 #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"); }
43 //***************************************************************************************
46 * Convert an integer, positive or negative, to a character string radix 10.
49 itoa(int32_t i
, char* buf
)
60 // Output digits in reverse order
64 *p
++ = (char)('0' + (i
% 10));
83 //***************************************************************************************
85 // Array of our test objects
91 UErrorCode expected_constructor_status
;
93 UBool like
[e_Where_count
];
94 UBool inherits
[e_Where_count
];
99 // "IN" means inherits
100 // "NE" or "ne" means "does not exist"
102 { "root", NULL
, U_ZERO_ERROR
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} },
103 { "te", NULL
, U_ZERO_ERROR
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
104 { "te_IN", NULL
, U_ZERO_ERROR
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
105 { "te_NE", NULL
, U_USING_FALLBACK_WARNING
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
106 { "te_IN_NE", NULL
, U_USING_FALLBACK_WARNING
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
107 { "ne", NULL
, U_USING_DEFAULT_WARNING
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} }
110 static const int32_t bundles_count
= sizeof(param
) / sizeof(param
[0]);
112 //***************************************************************************************
115 * Return a random unsigned long l where 0N <= l <= ULONG_MAX.
121 static UBool initialized
= FALSE
;
124 srand((unsigned)time(NULL
));
127 // Assume rand has at least 12 bits of precision
129 for (uint32_t i
=0; i
<sizeof(l
); ++i
)
130 ((char*)&l
)[i
] = (char)((rand() & 0x0FF0) >> 4);
135 * Return a random double x where 0.0 <= x < 1.0.
140 return (double)(randul() / ULONG_MAX
);
144 * Return a random integer i where 0 <= i < n.
146 int32_t randi(int32_t n
)
148 return (int32_t)(randd() * n
);
151 //***************************************************************************************
154 Don't use more than one of these at a time because of the Locale names
156 ResourceBundleTest::ResourceBundleTest()
160 if (param
[5].locale
== NULL
) {
161 param
[0].locale
= new Locale("root");
162 param
[1].locale
= new Locale("te");
163 param
[2].locale
= new Locale("te", "IN");
164 param
[3].locale
= new Locale("te", "NE");
165 param
[4].locale
= new Locale("te", "IN", "NE");
166 param
[5].locale
= new Locale("ne");
170 ResourceBundleTest::~ResourceBundleTest()
172 if (param
[5].locale
) {
174 for (idx
= 0; idx
< (int)(sizeof(param
)/sizeof(param
[0])); idx
++) {
175 delete param
[idx
].locale
;
176 param
[idx
].locale
= NULL
;
181 void ResourceBundleTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
183 if (exec
) logln("TestSuite ResourceBundleTest: ");
185 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
186 case 0: name
= "TestResourceBundles"; if (exec
) TestResourceBundles(); break;
187 case 1: name
= "TestConstruction"; if (exec
) TestConstruction(); break;
188 case 2: name
= "TestGetSize"; if (exec
) TestGetSize(); break;
189 case 3: name
= "TestGetLocaleByType"; if (exec
) TestGetLocaleByType(); break;
191 case 0: case 1: case 2: case 3: name
= "skip"; break;
194 case 4: name
= "TestExemplar"; if (exec
) TestExemplar(); break;
195 default: name
= ""; break; //needed to end loop
199 //***************************************************************************************
202 ResourceBundleTest::TestResourceBundles()
204 UErrorCode status
= U_ZERO_ERROR
;
206 loadTestData(status
);
207 if(U_FAILURE(status
))
209 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status
)));
213 /* Make sure that users using te_IN for the default locale don't get test failures. */
214 Locale originalDefault
;
215 if (Locale::getDefault() == Locale("te_IN")) {
216 Locale::setDefault(Locale("en_US"), status
);
219 testTag("only_in_Root", TRUE
, FALSE
, FALSE
);
220 testTag("only_in_te", FALSE
, TRUE
, FALSE
);
221 testTag("only_in_te_IN", FALSE
, FALSE
, TRUE
);
222 testTag("in_Root_te", TRUE
, TRUE
, FALSE
);
223 testTag("in_Root_te_te_IN", TRUE
, TRUE
, TRUE
);
224 testTag("in_Root_te_IN", TRUE
, FALSE
, TRUE
);
225 testTag("in_te_te_IN", FALSE
, TRUE
, TRUE
);
226 testTag("nonexistent", FALSE
, FALSE
, FALSE
);
227 logln("Passed: %d\nFailed: %d", pass
, fail
);
229 /* Restore the default locale for the other tests. */
230 Locale::setDefault(originalDefault
, status
);
234 ResourceBundleTest::TestConstruction()
236 UErrorCode err
= U_ZERO_ERROR
;
237 Locale
locale("te", "IN");
239 const char* testdatapath
=loadTestData(err
);
242 dataerrln("Could not load testdata.dat " + UnicodeString(testdatapath
) + ", " + UnicodeString(u_errorName(err
)));
246 /* Make sure that users using te_IN for the default locale don't get test failures. */
247 Locale originalDefault
;
248 if (Locale::getDefault() == Locale("te_IN")) {
249 Locale::setDefault(Locale("en_US"), err
);
252 ResourceBundle
test1((UnicodeString
)testdatapath
, err
);
253 ResourceBundle
test2(testdatapath
, locale
, err
);
254 //ResourceBundle test1("c:\\icu\\icu\\source\\test\\testdata\\testdata", err);
255 //ResourceBundle test2("c:\\icu\\icu\\source\\test\\testdata\\testdata", locale, err);
257 UnicodeString
result1(test1
.getStringEx("string_in_Root_te_te_IN", err
));
258 UnicodeString
result2(test2
.getStringEx("string_in_Root_te_te_IN", err
));
260 if (U_FAILURE(err
)) {
261 errln("Something threw an error in TestConstruction()");
265 logln("for string_in_Root_te_te_IN, default.txt had " + result1
);
266 logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2
);
268 if (result1
!= "ROOT" || result2
!= "TE_IN")
269 errln("Construction test failed; run verbose for more information");
271 const char* version1
;
272 const char* version2
;
274 version1
= test1
.getVersionNumber();
275 version2
= test2
.getVersionNumber();
277 char *versionID1
= new char[1+strlen(version1
)]; // + 1 for zero byte
278 char *versionID2
= new char[1+ strlen(version2
)]; // + 1 for zero byte
280 strcpy(versionID1
, "44.0"); // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed.
282 strcpy(versionID2
, "55.0"); // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed.
284 logln(UnicodeString("getVersionNumber on default.txt returned ") + version1
);
285 logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2
);
287 if (strcmp(version1
, versionID1
) != 0 || strcmp(version2
, versionID2
) != 0)
288 errln("getVersionNumber() failed");
293 /* Restore the default locale for the other tests. */
294 Locale::setDefault(originalDefault
, err
);
297 //***************************************************************************************
300 ResourceBundleTest::testTag(const char* frag
,
305 int32_t failOrig
= fail
;
307 // Make array from input params
309 UBool is_in
[] = { in_Root
, in_te
, in_te_IN
};
311 const char* NAME
[] = { "ROOT", "TE", "TE_IN" };
313 // Now try to load the desired items
316 UnicodeString action
;
318 int32_t i
,j
,actual_bundle
;
321 UErrorCode status
= U_ZERO_ERROR
;
322 const char* testdatapath
;
323 testdatapath
=loadTestData(status
);
324 if(U_FAILURE(status
))
326 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status
)));
330 for (i
=0; i
<bundles_count
; ++i
)
332 action
= "Constructor for ";
333 action
+= param
[i
].name
;
335 status
= U_ZERO_ERROR
;
336 ResourceBundle
theBundle( testdatapath
, *param
[i
].locale
, status
);
337 //ResourceBundle theBundle( "c:\\icu\\icu\\source\\test\\testdata\\testdata", *param[i].locale, status);
338 CONFIRM_UErrorCode(status
, param
[i
].expected_constructor_status
, action
);
341 actual_bundle
= 0; /* ne -> default */
343 actual_bundle
= 1; /* te_NE -> te */
345 actual_bundle
= 2; /* te_IN_NE -> te_IN */
350 UErrorCode expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
351 for (j
=e_te_IN
; j
>=e_Root
; --j
)
353 if (is_in
[j
] && param
[i
].inherits
[j
])
355 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
356 expected_resource_status
= U_ZERO_ERROR
;
358 expected_resource_status
= U_USING_DEFAULT_WARNING
;
360 expected_resource_status
= U_USING_FALLBACK_WARNING
;
366 UErrorCode expected_status
;
369 for (j
=param
[i
].where
; j
>=0; --j
)
378 //--------------------------------------------------------------------------
381 uprv_strcpy(tag
, "string_");
382 uprv_strcat(tag
, frag
);
384 action
= param
[i
].name
;
385 action
+= ".getString(";
390 status
= U_ZERO_ERROR
;
392 UnicodeString
string(theBundle
.getStringEx(tag
, status
));
394 if(U_FAILURE(status
)) {
395 string
.setTo(TRUE
, kErrorUChars
, kErrorLength
);
398 CONFIRM_UErrorCode(status
, expected_resource_status
, action
);
400 UnicodeString
expected_string(kErrorUChars
);
401 if (U_SUCCESS(status
)) {
402 expected_string
= base
;
405 CONFIRM_EQ(string
, expected_string
, action
);
407 //--------------------------------------------------------------------------
410 uprv_strcpy(tag
, "array_");
411 uprv_strcat(tag
, frag
);
413 action
= param
[i
].name
;
418 status
= U_ZERO_ERROR
;
419 ResourceBundle
arrayBundle(theBundle
.get(tag
, status
));
420 CONFIRM_UErrorCode(status
, expected_resource_status
, action
);
421 int32_t count
= arrayBundle
.getSize();
423 if (U_SUCCESS(status
))
425 CONFIRM_GE(count
, 1, action
);
427 for (j
=0; j
< count
; ++j
)
430 UnicodeString
value(arrayBundle
.getStringEx(j
, status
));
431 expected_string
= base
;
432 expected_string
+= itoa(j
,buf
);
433 CONFIRM_EQ(value
, expected_string
, action
);
436 action
= param
[i
].name
;
437 action
+= ".getStringEx(";
441 for (j
=0; j
<100; ++j
)
443 index
= count
? (randi(count
* 3) - count
) : (randi(200) - 100);
444 status
= U_ZERO_ERROR
;
445 string
= kErrorUChars
;
446 UnicodeString
t(arrayBundle
.getStringEx(index
, status
));
447 expected_status
= (index
>= 0 && index
< count
) ? expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
448 CONFIRM_UErrorCode(status
, expected_status
, action
);
450 if (U_SUCCESS(status
))
453 expected_string
= base
;
454 expected_string
+= itoa(index
,buf
);
458 expected_string
= kErrorUChars
;
460 CONFIRM_EQ(string
, expected_string
, action
);
463 else if (status
!= expected_resource_status
)
465 record_fail("Error getting " + (UnicodeString
)tag
);
466 return (UBool
)(failOrig
!= fail
);
471 return (UBool
)(failOrig
!= fail
);
475 ResourceBundleTest::record_pass(UnicodeString passMessage
)
481 ResourceBundleTest::record_fail(UnicodeString errMessage
)
488 ResourceBundleTest::TestExemplar(){
490 int32_t locCount
= uloc_countAvailable();
493 UErrorCode status
= U_ZERO_ERROR
;
494 for(;locIndex
<locCount
;locIndex
++){
495 const char* locale
= uloc_getAvailable(locIndex
);
496 UResourceBundle
*resb
=ures_open(NULL
,locale
,&status
);
497 if(U_SUCCESS(status
) && status
!=U_USING_FALLBACK_WARNING
&& status
!=U_USING_DEFAULT_WARNING
){
499 const UChar
* strSet
= ures_getStringByKey(resb
,"ExemplarCharacters",&len
,&status
);
500 UnicodeSet
set(strSet
,status
);
501 if(U_FAILURE(status
)){
502 errln("Could not construct UnicodeSet from pattern for ExemplarCharacters in locale : %s. Error: %s",locale
,u_errorName(status
));
509 logln("Number of installed locales with exemplar characters that could be tested: %d",num
);
514 ResourceBundleTest::TestGetSize(void)
523 { "integerarray", 1},
526 { "emptystring", 1}, /* empty string is still a string */
530 { "collations", 1}, /* not 2 - there is hidden %%CollationBin */
533 UErrorCode status
= U_ZERO_ERROR
;
535 const char* testdatapath
= loadTestData(status
);
536 int32_t i
= 0, j
= 0;
539 if(U_FAILURE(status
))
541 dataerrln("Could not load testdata.dat %s\n", u_errorName(status
));
545 ResourceBundle
rb(testdatapath
, "testtypes", status
);
546 if(U_FAILURE(status
))
548 err("Could not testtypes resource bundle %s\n", u_errorName(status
));
552 for(i
= 0; i
< (int32_t)(sizeof(test
)/sizeof(test
[0])); i
++) {
553 ResourceBundle res
= rb
.get(test
[i
].key
, status
);
554 if(U_FAILURE(status
))
556 err("Couldn't find the key %s. Error: %s\n", u_errorName(status
));
559 size
= res
.getSize();
560 if(size
!= test
[i
].size
) {
561 err("Expected size %i, got size %i for key %s\n", test
[i
].size
, size
, test
[i
].key
);
562 for(j
= 0; j
< size
; j
++) {
563 ResourceBundle helper
= res
.get(j
, status
);
564 err("%s\n", helper
.getKey());
571 ResourceBundleTest::TestGetLocaleByType(void)
574 const char *requestedLocale
;
575 const char *resourceKey
;
576 const char *validLocale
;
577 const char *actualLocale
;
579 { "te_IN_BLAH", "string_only_in_te_IN", "te_IN", "te_IN" },
580 { "te_IN_BLAH", "string_only_in_te", "te_IN", "te" },
581 { "te_IN_BLAH", "string_only_in_Root", "te_IN", "root" },
582 { "te_IN_BLAH_01234567890_01234567890_01234567890_01234567890_01234567890_01234567890", "array_2d_only_in_Root", "te_IN", "root" },
583 { "te_IN_BLAH@currency=euro", "array_2d_only_in_te_IN", "te_IN", "te_IN" },
584 { "te_IN_BLAH@calendar=thai;collation=phonebook", "array_2d_only_in_te", "te_IN", "te" }
587 UErrorCode status
= U_ZERO_ERROR
;
589 const char* testdatapath
= loadTestData(status
);
593 if(U_FAILURE(status
))
595 dataerrln("Could not load testdata.dat %s\n", u_errorName(status
));
599 for(i
= 0; i
< (int32_t)(sizeof(test
)/sizeof(test
[0])); i
++) {
600 ResourceBundle
rb(testdatapath
, test
[i
].requestedLocale
, status
);
601 if(U_FAILURE(status
))
603 err("Could not open resource bundle %s (error %s)\n", test
[i
].requestedLocale
, u_errorName(status
));
604 status
= U_ZERO_ERROR
;
608 ResourceBundle res
= rb
.get(test
[i
].resourceKey
, status
);
609 if(U_FAILURE(status
))
611 err("Couldn't find the key %s. Error: %s\n", test
[i
].resourceKey
, u_errorName(status
));
612 status
= U_ZERO_ERROR
;
616 locale
= res
.getLocale(ULOC_REQUESTED_LOCALE
, status
);
617 if(U_SUCCESS(status
) && locale
!= Locale::getDefault()) {
618 err("Expected requested locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
.getName());
620 status
= U_ZERO_ERROR
;
621 locale
= res
.getLocale(ULOC_VALID_LOCALE
, status
);
622 if(strcmp(locale
.getName(), test
[i
].validLocale
) != 0) {
623 err("Expected valid locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
.getName());
625 locale
= res
.getLocale(ULOC_ACTUAL_LOCALE
, status
);
626 if(strcmp(locale
.getName(), test
[i
].actualLocale
) != 0) {
627 err("Expected actual locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
.getName());