/********************************************************************
* COPYRIGHT:
- * Copyright (c) 1997-2003, International Business Machines Corporation and
+ * Copyright (c) 1997-2009, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
// Array of our test objects
-struct
+static struct
{
const char* name;
Locale *locale;
{ "ne", NULL, U_USING_DEFAULT_WARNING, e_Root, { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } }
};
-int32_t bundles_count = sizeof(param) / sizeof(param[0]);
+static const int32_t bundles_count = sizeof(param) / sizeof(param[0]);
//***************************************************************************************
*/
ResourceBundleTest::ResourceBundleTest()
: pass(0),
- fail(0),
- OUT(it_out)
+ fail(0)
{
if (param[5].locale == NULL) {
param[0].locale = new Locale("root");
{
if (exec) logln("TestSuite ResourceBundleTest: ");
switch (index) {
+#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
case 0: name = "TestResourceBundles"; if (exec) TestResourceBundles(); break;
case 1: name = "TestConstruction"; if (exec) TestConstruction(); break;
- case 2: name = "TestExemplar"; if (exec) TestExemplar(); break;
+ case 2: name = "TestGetSize"; if (exec) TestGetSize(); break;
+ case 3: name = "TestGetLocaleByType"; if (exec) TestGetLocaleByType(); break;
+#else
+ case 0: case 1: case 2: case 3: name = "skip"; break;
+#endif
+
+ case 4: name = "TestExemplar"; if (exec) TestExemplar(); break;
default: name = ""; break; //needed to end loop
}
}
void
ResourceBundleTest::TestResourceBundles()
{
+ UErrorCode status = U_ZERO_ERROR;
+
+ loadTestData(status);
+ if(U_FAILURE(status))
+ {
+ dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status)));
+ return;
+ }
+
+ /* Make sure that users using te_IN for the default locale don't get test failures. */
+ Locale originalDefault;
+ if (Locale::getDefault() == Locale("te_IN")) {
+ Locale::setDefault(Locale("en_US"), status);
+ }
+
testTag("only_in_Root", TRUE, FALSE, FALSE);
testTag("only_in_te", FALSE, TRUE, FALSE);
testTag("only_in_te_IN", FALSE, FALSE, TRUE);
testTag("in_te_te_IN", FALSE, TRUE, TRUE);
testTag("nonexistent", FALSE, FALSE, FALSE);
logln("Passed: %d\nFailed: %d", pass, fail);
+
+ /* Restore the default locale for the other tests. */
+ Locale::setDefault(originalDefault, status);
}
void
ResourceBundleTest::TestConstruction()
{
+ UErrorCode err = U_ZERO_ERROR;
+ Locale locale("te", "IN");
+
+ const char* testdatapath=loadTestData(err);
+ if(U_FAILURE(err))
{
- UErrorCode err = U_ZERO_ERROR;
- const char* testdatapath;
- Locale locale("te", "IN");
+ dataerrln("Could not load testdata.dat " + UnicodeString(testdatapath) + ", " + UnicodeString(u_errorName(err)));
+ return;
+ }
- testdatapath=loadTestData(err);
- if(U_FAILURE(err))
- {
- errln("Could not load testdata.dat " + UnicodeString(testdatapath) + ", " + UnicodeString(u_errorName(err)));
- return;
- }
- ResourceBundle test1((UnicodeString)testdatapath, err);
- ResourceBundle test2(testdatapath, locale, err);
- //ResourceBundle test1("c:\\icu\\icu\\source\\test\\testdata\\testdata", err);
- //ResourceBundle test2("c:\\icu\\icu\\source\\test\\testdata\\testdata", locale, err);
+ /* Make sure that users using te_IN for the default locale don't get test failures. */
+ Locale originalDefault;
+ if (Locale::getDefault() == Locale("te_IN")) {
+ Locale::setDefault(Locale("en_US"), err);
+ }
- UnicodeString result1(test1.getStringEx("string_in_Root_te_te_IN", err));
- UnicodeString result2(test2.getStringEx("string_in_Root_te_te_IN", err));
+ ResourceBundle test1((UnicodeString)testdatapath, err);
+ ResourceBundle test2(testdatapath, locale, err);
+ //ResourceBundle test1("c:\\icu\\icu\\source\\test\\testdata\\testdata", err);
+ //ResourceBundle test2("c:\\icu\\icu\\source\\test\\testdata\\testdata", locale, err);
- if (U_FAILURE(err)) {
- errln("Something threw an error in TestConstruction()");
- return;
- }
+ UnicodeString result1(test1.getStringEx("string_in_Root_te_te_IN", err));
+ UnicodeString result2(test2.getStringEx("string_in_Root_te_te_IN", err));
- logln("for string_in_Root_te_te_IN, default.txt had " + result1);
- logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2);
+ if (U_FAILURE(err)) {
+ errln("Something threw an error in TestConstruction()");
+ return;
+ }
- if (result1 != "ROOT" || result2 != "TE_IN")
- errln("Construction test failed; run verbose for more information");
+ logln("for string_in_Root_te_te_IN, default.txt had " + result1);
+ logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2);
- const char* version1;
- const char* version2;
+ if (result1 != "ROOT" || result2 != "TE_IN")
+ errln("Construction test failed; run verbose for more information");
- version1 = test1.getVersionNumber();
- version2 = test2.getVersionNumber();
+ const char* version1;
+ const char* version2;
- char *versionID1 = new char[1+strlen(version1)]; // + 1 for zero byte
- char *versionID2 = new char[1+ strlen(version2)]; // + 1 for zero byte
+ version1 = test1.getVersionNumber();
+ version2 = test2.getVersionNumber();
- strcpy(versionID1, "44.0"); // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed.
+ char *versionID1 = new char[1+strlen(version1)]; // + 1 for zero byte
+ char *versionID2 = new char[1+ strlen(version2)]; // + 1 for zero byte
- strcpy(versionID2, "55.0"); // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed.
+ strcpy(versionID1, "44.0"); // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed.
- logln(UnicodeString("getVersionNumber on default.txt returned ") + version1);
- logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2);
+ strcpy(versionID2, "55.0"); // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed.
- if (strcmp(version1, versionID1) != 0 || strcmp(version2, versionID2) != 0)
- errln("getVersionNumber() failed");
+ logln(UnicodeString("getVersionNumber on default.txt returned ") + version1);
+ logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2);
- delete[] versionID1;
- delete[] versionID2;
- }
+ if (strcmp(version1, versionID1) != 0 || strcmp(version2, versionID2) != 0)
+ errln("getVersionNumber() failed");
+
+ delete[] versionID1;
+ delete[] versionID2;
+
+ /* Restore the default locale for the other tests. */
+ Locale::setDefault(originalDefault, err);
}
//***************************************************************************************
testdatapath=loadTestData(status);
if(U_FAILURE(status))
{
- errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status)));
+ dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status)));
return FALSE;
}
logln("Number of installed locales with exemplar characters that could be tested: %d",num);
}
-
+
+void
+ResourceBundleTest::TestGetSize(void)
+{
+ const struct {
+ const char* key;
+ int32_t size;
+ } test[] = {
+ { "zerotest", 1},
+ { "one", 1},
+ { "importtest", 1},
+ { "integerarray", 1},
+ { "emptyarray", 0},
+ { "emptytable", 0},
+ { "emptystring", 1}, /* empty string is still a string */
+ { "emptyint", 1},
+ { "emptybin", 1},
+ { "testinclude", 1},
+ { "collations", 1}, /* not 2 - there is hidden %%CollationBin */
+ };
+
+ UErrorCode status = U_ZERO_ERROR;
+
+ const char* testdatapath = loadTestData(status);
+ int32_t i = 0, j = 0;
+ int32_t size = 0;
+
+ if(U_FAILURE(status))
+ {
+ dataerrln("Could not load testdata.dat %s\n", u_errorName(status));
+ return;
+ }
+
+ ResourceBundle rb(testdatapath, "testtypes", status);
+ if(U_FAILURE(status))
+ {
+ err("Could not testtypes resource bundle %s\n", u_errorName(status));
+ return;
+ }
+
+ for(i = 0; i < (int32_t)(sizeof(test)/sizeof(test[0])); i++) {
+ ResourceBundle res = rb.get(test[i].key, status);
+ if(U_FAILURE(status))
+ {
+ err("Couldn't find the key %s. Error: %s\n", u_errorName(status));
+ return;
+ }
+ size = res.getSize();
+ if(size != test[i].size) {
+ err("Expected size %i, got size %i for key %s\n", test[i].size, size, test[i].key);
+ for(j = 0; j < size; j++) {
+ ResourceBundle helper = res.get(j, status);
+ err("%s\n", helper.getKey());
+ }
+ }
+ }
+}
+
+void
+ResourceBundleTest::TestGetLocaleByType(void)
+{
+ const struct {
+ const char *requestedLocale;
+ const char *resourceKey;
+ const char *validLocale;
+ const char *actualLocale;
+ } test[] = {
+ { "te_IN_BLAH", "string_only_in_te_IN", "te_IN", "te_IN" },
+ { "te_IN_BLAH", "string_only_in_te", "te_IN", "te" },
+ { "te_IN_BLAH", "string_only_in_Root", "te_IN", "root" },
+ { "te_IN_BLAH_01234567890_01234567890_01234567890_01234567890_01234567890_01234567890", "array_2d_only_in_Root", "te_IN", "root" },
+ { "te_IN_BLAH@currency=euro", "array_2d_only_in_te_IN", "te_IN", "te_IN" },
+ { "te_IN_BLAH@calendar=thai;collation=phonebook", "array_2d_only_in_te", "te_IN", "te" }
+ };
+
+ UErrorCode status = U_ZERO_ERROR;
+
+ const char* testdatapath = loadTestData(status);
+ int32_t i = 0;
+ Locale locale;
+
+ if(U_FAILURE(status))
+ {
+ dataerrln("Could not load testdata.dat %s\n", u_errorName(status));
+ return;
+ }
+
+ for(i = 0; i < (int32_t)(sizeof(test)/sizeof(test[0])); i++) {
+ ResourceBundle rb(testdatapath, test[i].requestedLocale, status);
+ if(U_FAILURE(status))
+ {
+ err("Could not open resource bundle %s (error %s)\n", test[i].requestedLocale, u_errorName(status));
+ status = U_ZERO_ERROR;
+ continue;
+ }
+
+ ResourceBundle res = rb.get(test[i].resourceKey, status);
+ if(U_FAILURE(status))
+ {
+ err("Couldn't find the key %s. Error: %s\n", test[i].resourceKey, u_errorName(status));
+ status = U_ZERO_ERROR;
+ continue;
+ }
+
+ locale = res.getLocale(ULOC_REQUESTED_LOCALE, status);
+ if(locale != Locale::getDefault()) {
+ err("Expected requested locale to be %s. Got %s\n", test[i].requestedLocale, locale.getName());
+ }
+ locale = res.getLocale(ULOC_VALID_LOCALE, status);
+ if(strcmp(locale.getName(), test[i].validLocale) != 0) {
+ err("Expected valid locale to be %s. Got %s\n", test[i].requestedLocale, locale.getName());
+ }
+ locale = res.getLocale(ULOC_ACTUAL_LOCALE, status);
+ if(strcmp(locale.getName(), test[i].actualLocale) != 0) {
+ err("Expected actual locale to be %s. Got %s\n", test[i].requestedLocale, locale.getName());
+ }
+ }
+}
+
//eof