1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * Copyright (c) 1997-2016, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 ********************************************************************/
8 #include "unicode/utypes.h"
12 #include "unicode/unistr.h"
13 #include "unicode/resbund.h"
21 //***************************************************************************************
23 static const UChar kErrorUChars
[] = { 0x45, 0x52, 0x52, 0x4f, 0x52, 0 };
24 static const int32_t kErrorLength
= 5;
25 static const int32_t kERROR_COUNT
= -1234567;
27 //***************************************************************************************
37 //***************************************************************************************
39 #define CONFIRM_EQ(actual,expected) if ((expected)==(actual)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of " + (expected)); }
40 #define CONFIRM_GE(actual,expected) if ((actual)>=(expected)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x >= " + (expected)); }
41 #define CONFIRM_NE(actual,expected) if ((expected)!=(actual)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x != " + (expected)); }
43 #define CONFIRM_UErrorCode(actual,expected) if ((expected)==(actual)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (UnicodeString)u_errorName(actual) + (UnicodeString)" instead of " + (UnicodeString)u_errorName(expected)); }
45 //***************************************************************************************
48 * Convert an integer, positive or negative, to a character string radix 10.
51 itoa(int32_t i
, char* buf
)
62 // Output digits in reverse order
66 *p
++ = (char)('0' + (i
% 10));
85 //***************************************************************************************
87 // Array of our test objects
93 UErrorCode expected_constructor_status
;
95 UBool like
[e_Where_count
];
96 UBool inherits
[e_Where_count
];
101 // "IN" means inherits
102 // "NE" or "ne" means "does not exist"
104 { "root", 0, U_ZERO_ERROR
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} },
105 { "te", 0, U_ZERO_ERROR
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
106 { "te_IN", 0, U_ZERO_ERROR
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
107 { "te_NE", 0, U_USING_FALLBACK_WARNING
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
108 { "te_IN_NE", 0, U_USING_FALLBACK_WARNING
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
109 { "ne", 0, U_USING_DEFAULT_WARNING
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} }
112 static int32_t bundles_count
= UPRV_LENGTHOF(param
);
114 //***************************************************************************************
117 * Return a random unsigned long l where 0N <= l <= ULONG_MAX.
123 static UBool initialized
= FALSE
;
126 srand((unsigned)time(NULL
));
129 // Assume rand has at least 12 bits of precision
131 for (uint32_t i
=0; i
<sizeof(l
); ++i
)
132 ((char*)&l
)[i
] = (char)((rand() & 0x0FF0) >> 4);
137 * Return a random double x where 0.0 <= x < 1.0.
142 return (double)(randul() / ULONG_MAX
);
146 * Return a random integer i where 0 <= i < n.
148 static int32_t randi(int32_t n
)
150 return (int32_t)(randd() * n
);
153 //***************************************************************************************
156 Don't use more than one of these at a time because of the Locale names
158 NewResourceBundleTest::NewResourceBundleTest()
162 if (param
[5].locale
== NULL
) {
163 param
[0].locale
= new Locale("root");
164 param
[1].locale
= new Locale("te");
165 param
[2].locale
= new Locale("te", "IN");
166 param
[3].locale
= new Locale("te", "NE");
167 param
[4].locale
= new Locale("te", "IN", "NE");
168 param
[5].locale
= new Locale("ne");
172 NewResourceBundleTest::~NewResourceBundleTest()
174 if (param
[5].locale
) {
176 for (idx
= 0; idx
< UPRV_LENGTHOF(param
); idx
++) {
177 delete param
[idx
].locale
;
178 param
[idx
].locale
= NULL
;
183 void NewResourceBundleTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
185 if (exec
) logln("TestSuite ResourceBundleTest: ");
187 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
188 case 0: name
= "TestResourceBundles"; if (exec
) TestResourceBundles(); break;
189 case 1: name
= "TestConstruction"; if (exec
) TestConstruction(); break;
190 case 2: name
= "TestIteration"; if (exec
) TestIteration(); break;
191 case 3: name
= "TestOtherAPI"; if(exec
) TestOtherAPI(); break;
192 case 4: name
= "TestNewTypes"; if(exec
) TestNewTypes(); break;
194 case 0: case 1: case 2: case 3: case 4: name
= "skip"; break;
197 case 5: name
= "TestGetByFallback"; if(exec
) TestGetByFallback(); break;
198 case 6: name
= "TestFilter"; if(exec
) TestFilter(); break;
200 default: name
= ""; break; //needed to end loop
204 //***************************************************************************************
207 NewResourceBundleTest::TestResourceBundles()
209 UErrorCode status
= U_ZERO_ERROR
;
210 loadTestData(status
);
211 if(U_FAILURE(status
))
213 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status
)));
217 /* Make sure that users using te_IN for the default locale don't get test failures. */
218 Locale originalDefault
;
219 if (Locale::getDefault() == Locale("te_IN")) {
220 Locale::setDefault(Locale("en_US"), status
);
223 testTag("only_in_Root", TRUE
, FALSE
, FALSE
);
224 testTag("only_in_te", FALSE
, TRUE
, FALSE
);
225 testTag("only_in_te_IN", FALSE
, FALSE
, TRUE
);
226 testTag("in_Root_te", TRUE
, TRUE
, FALSE
);
227 testTag("in_Root_te_te_IN", TRUE
, TRUE
, TRUE
);
228 testTag("in_Root_te_IN", TRUE
, FALSE
, TRUE
);
229 testTag("in_te_te_IN", FALSE
, TRUE
, TRUE
);
230 testTag("nonexistent", FALSE
, FALSE
, FALSE
);
231 logln("Passed: %d\nFailed: %d", pass
, fail
);
233 /* Restore the default locale for the other tests. */
234 Locale::setDefault(originalDefault
, status
);
238 NewResourceBundleTest::TestConstruction()
240 UErrorCode err
= U_ZERO_ERROR
;
241 Locale
locale("te", "IN");
243 const char* testdatapath
;
244 testdatapath
=loadTestData(err
);
247 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
251 /* Make sure that users using te_IN for the default locale don't get test failures. */
252 Locale originalDefault
;
253 if (Locale::getDefault() == Locale("te_IN")) {
254 Locale::setDefault(Locale("en_US"), err
);
257 ResourceBundle
test1((UnicodeString
)testdatapath
, err
);
258 ResourceBundle
test2(testdatapath
, locale
, err
);
260 UnicodeString result1
;
261 UnicodeString result2
;
263 result1
= test1
.getStringEx("string_in_Root_te_te_IN", err
);
264 result2
= test2
.getStringEx("string_in_Root_te_te_IN", err
);
265 if (U_FAILURE(err
)) {
266 errln("Something threw an error in TestConstruction()");
270 logln("for string_in_Root_te_te_IN, root.txt had " + result1
);
271 logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2
);
273 if (result1
!= "ROOT" || result2
!= "TE_IN") {
274 errln("Construction test failed; run verbose for more information");
277 const char* version1
;
278 const char* version2
;
280 version1
= test1
.getVersionNumber();
281 version2
= test2
.getVersionNumber();
283 char *versionID1
= new char[1 + strlen(U_ICU_VERSION
) + strlen(version1
)]; // + 1 for zero byte
284 char *versionID2
= new char[1 + strlen(U_ICU_VERSION
) + strlen(version2
)]; // + 1 for zero byte
286 strcpy(versionID1
, "44.0"); // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed.
288 strcpy(versionID2
, "55.0"); // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed.
290 logln(UnicodeString("getVersionNumber on default.txt returned ") + version1
+ UnicodeString(" Expect: " ) + versionID1
);
291 logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2
+ UnicodeString(" Expect: " ) + versionID2
);
293 if (strcmp(version1
, versionID1
) != 0) {
294 errln("getVersionNumber(version1) failed. %s != %s", version1
, versionID1
);
296 if (strcmp(version2
, versionID2
) != 0) {
297 errln("getVersionNumber(version2) failed. %s != %s", version2
, versionID2
);
302 /* Restore the default locale for the other tests. */
303 Locale::setDefault(originalDefault
, err
);
307 NewResourceBundleTest::TestIteration()
309 UErrorCode err
= U_ZERO_ERROR
;
310 const char* testdatapath
;
312 "string_in_Root_te_te_IN", "1",
313 "array_in_Root_te_te_IN", "5",
314 "array_2d_in_Root_te_te_IN", "4",
317 Locale
*locale
=new Locale("te_IN");
319 testdatapath
=loadTestData(err
);
322 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
326 ResourceBundle
test1(testdatapath
, *locale
, err
);
328 errln("Construction failed");
331 int32_t count
, row
=0, col
=0;
333 UnicodeString expected
;
334 UnicodeString
element("TE_IN");
335 UnicodeString action
;
338 for(i
=0; i
<UPRV_LENGTHOF(data
); i
=i
+2){
344 ResourceBundle bundle
= test1
.get(data
[i
], err
);
347 action
+=".getKey()";
349 CONFIRM_EQ((UnicodeString
)bundle
.getKey(), (UnicodeString
)data
[i
]);
353 while(bundle
.hasNext()){
355 action
+=".getNextString(err)";
357 UnicodeString got
=bundle
.getNextString(err
);
360 if(bundle
.getSize() > 1){
361 CONFIRM_EQ(bundle
.getType(), URES_ARRAY
);
362 expected
+=itoa(row
, buf
);
363 ResourceBundle rowbundle
=bundle
.get(row
, err
);
364 if(!U_FAILURE(err
) && rowbundle
.getSize()>1){
366 while(rowbundle
.hasNext()){
368 got
=rowbundle
.getNextString(err
);
370 expected
+=itoa(row
, buf
);
371 expected
+=itoa(col
, buf
);
373 CONFIRM_EQ(got
, expected
);
376 CONFIRM_EQ(col
, rowbundle
.getSize());
380 CONFIRM_EQ(bundle
.getType(), (int32_t)URES_STRING
);
383 CONFIRM_EQ(got
, expected
);
387 action
+=".getSize()";
388 CONFIRM_EQ(bundle
.getSize(), count
);
389 CONFIRM_EQ(count
, atoi(data
[i
+1]));
390 //after reaching the end
392 ResourceBundle errbundle
=bundle
.getNext(err
);
393 action
= "After reaching the end of the Iterator:- ";
395 action
+=".getNext()";
396 CONFIRM_NE(err
, (int32_t)U_ZERO_ERROR
);
397 CONFIRM_EQ(u_errorName(err
), u_errorName(U_INDEX_OUTOFBOUNDS_ERROR
));
400 bundle
.resetIterator();
401 /* The following code is causing a crash
407 errln("ERROR: getNext() throw an error");
414 // TODO: add operator== and != to ResourceBundle
416 equalRB(ResourceBundle
&a
, ResourceBundle
&b
) {
424 a
.getLocale()==b
.getLocale() &&
425 0==strcmp(a
.getName(), b
.getName()) &&
427 a
.getString(status
)==b
.getString(status
) :
429 a
.getInt(status
)==b
.getInt(status
) :
434 NewResourceBundleTest::TestOtherAPI(){
435 UErrorCode err
= U_ZERO_ERROR
;
436 const char* testdatapath
=loadTestData(err
);
437 UnicodeString tDataPathUS
= UnicodeString(testdatapath
, "");
441 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
445 /* Make sure that users using te_IN for the default locale don't get test failures. */
446 Locale originalDefault
;
447 if (Locale::getDefault() == Locale("te_IN")) {
448 Locale::setDefault(Locale("en_US"), err
);
451 Locale
*locale
=new Locale("te_IN");
453 ResourceBundle
test0(tDataPathUS
, *locale
, err
);
455 errln("Construction failed");
459 ResourceBundle
test1(testdatapath
, *locale
, err
);
461 errln("Construction failed");
465 logln("Testing getLocale()\n");
466 if(strcmp(test1
.getLocale().getName(), locale
->getName()) !=0 ){
467 errln("FAIL: ResourceBundle::getLocale() failed\n");
472 logln("Testing ResourceBundle(UErrorCode)\n");
473 ResourceBundle
defaultresource(err
);
474 ResourceBundle
explicitdefaultresource(NULL
, Locale::getDefault(), err
);
476 errcheckln(err
, "Construction of default resourcebundle failed - %s", u_errorName(err
));
479 // You can't compare the default locale to the resolved locale in the
480 // resource bundle due to aliasing, keywords in the default locale
481 // or the chance that the machine running these tests is using a locale
482 // that isn't available in ICU.
483 if(strcmp(defaultresource
.getLocale().getName(), explicitdefaultresource
.getLocale().getName()) != 0){
484 errln("Construction of default resourcebundle didn't take the defaultlocale. Expected %s Got %s err=%s\n",
485 explicitdefaultresource
.getLocale().getName(), defaultresource
.getLocale().getName(), u_errorName(err
));
489 ResourceBundle
copyRes(defaultresource
);
490 if(strcmp(copyRes
.getName(), defaultresource
.getName() ) !=0 ||
491 strcmp(test1
.getName(), defaultresource
.getName() ) ==0 ||
492 strcmp(copyRes
.getLocale().getName(), defaultresource
.getLocale().getName() ) !=0 ||
493 strcmp(test1
.getLocale().getName(), defaultresource
.getLocale().getName() ) ==0 )
495 errln("copy construction failed\n");
498 ResourceBundle defaultSub
= defaultresource
.get((int32_t)0, err
);
499 ResourceBundle
defSubCopy(defaultSub
);
500 if(strcmp(defSubCopy
.getName(), defaultSub
.getName() ) !=0 ||
501 strcmp(defSubCopy
.getLocale().getName(), defaultSub
.getLocale().getName() ) !=0 ){
502 errln("copy construction for subresource failed\n");
507 p
= defaultresource
.clone();
508 if(p
== &defaultresource
|| !equalRB(*p
, defaultresource
)) {
509 errln("ResourceBundle.clone() failed");
513 p
= defaultSub
.clone();
514 if(p
== &defaultSub
|| !equalRB(*p
, defaultSub
)) {
515 errln("2nd ResourceBundle.clone() failed");
520 copyRes
.getVersion(ver
);
522 logln("Version returned: [%d.%d.%d.%d]\n", ver
[0], ver
[1], ver
[2], ver
[3]);
524 logln("Testing C like UnicodeString APIs\n");
526 UResourceBundle
*testCAPI
= NULL
, *bundle
= NULL
, *rowbundle
= NULL
, *temp
= NULL
;
529 "string_in_Root_te_te_IN", "1",
530 "array_in_Root_te_te_IN", "5",
531 "array_2d_in_Root_te_te_IN", "4",
535 testCAPI
= ures_open(testdatapath
, "te_IN", &err
);
542 int32_t count
, row
=0, col
=0;
544 UnicodeString expected
;
545 UnicodeString
element("TE_IN");
546 UnicodeString action
;
549 for(i
=0; i
<UPRV_LENGTHOF(data
); i
=i
+2){
555 bundle
= ures_getByKey(testCAPI
, data
[i
], bundle
, &err
);
557 const char* key
= NULL
;
559 action
+=".getKey()";
561 CONFIRM_EQ((UnicodeString
)ures_getKey(bundle
), (UnicodeString
)data
[i
]);
565 while(ures_hasNext(bundle
)){
567 action
+=".getNextString(err)";
569 UnicodeString got
=ures_getNextUnicodeString(bundle
, &key
, &err
);
572 if(ures_getSize(bundle
) > 1){
573 CONFIRM_EQ(ures_getType(bundle
), URES_ARRAY
);
574 expected
+=itoa(row
, buf
);
575 rowbundle
=ures_getByIndex(bundle
, row
, rowbundle
, &err
);
576 if(!U_FAILURE(err
) && ures_getSize(rowbundle
)>1){
578 while(ures_hasNext(rowbundle
)){
580 got
=ures_getNextUnicodeString(rowbundle
, &key
, &err
);
581 temp
= ures_getByIndex(rowbundle
, col
, temp
, &err
);
582 UnicodeString bla
= ures_getUnicodeString(temp
, &err
);
583 UnicodeString bla2
= ures_getUnicodeStringByIndex(rowbundle
, col
, &err
);
585 expected
+=itoa(row
, buf
);
586 expected
+=itoa(col
, buf
);
588 CONFIRM_EQ(got
, expected
);
589 CONFIRM_EQ(bla
, expected
);
590 CONFIRM_EQ(bla2
, expected
);
593 CONFIRM_EQ(col
, ures_getSize(rowbundle
));
597 CONFIRM_EQ(ures_getType(bundle
), (int32_t)URES_STRING
);
600 CONFIRM_EQ(got
, expected
);
606 // Check that ures_getUnicodeString() & variants return a bogus string if failure.
607 // Same relevant code path whether the failure code is passed in
608 // or comes from a lookup error.
609 UErrorCode failure
= U_INTERNAL_PROGRAM_ERROR
;
610 assertTrue("ures_getUnicodeString(failure).isBogus()",
611 ures_getUnicodeString(testCAPI
, &failure
).isBogus());
612 assertTrue("ures_getNextUnicodeString(failure).isBogus()",
613 ures_getNextUnicodeString(testCAPI
, NULL
, &failure
).isBogus());
614 assertTrue("ures_getUnicodeStringByIndex(failure).isBogus()",
615 ures_getUnicodeStringByIndex(testCAPI
, 999, &failure
).isBogus());
616 assertTrue("ures_getUnicodeStringByKey(failure).isBogus()",
617 ures_getUnicodeStringByKey(testCAPI
, "bogus key", &failure
).isBogus());
620 ures_close(rowbundle
);
622 ures_close(testCAPI
);
624 errln("failed to open a resource bundle\n");
627 /* Restore the default locale for the other tests. */
628 Locale::setDefault(originalDefault
, err
);
634 //***************************************************************************************
637 NewResourceBundleTest::testTag(const char* frag
,
642 int32_t failOrig
= fail
;
644 // Make array from input params
646 UBool is_in
[] = { in_Root
, in_te
, in_te_IN
};
648 const char* NAME
[] = { "ROOT", "TE", "TE_IN" };
650 // Now try to load the desired items
653 UnicodeString action
;
655 int32_t i
,j
,row
,col
, actual_bundle
;
657 const char* testdatapath
;
659 UErrorCode status
= U_ZERO_ERROR
;
660 testdatapath
=loadTestData(status
);
661 if(U_FAILURE(status
))
663 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status
)));
667 for (i
=0; i
<bundles_count
; ++i
)
669 action
= "Constructor for ";
670 action
+= param
[i
].name
;
672 status
= U_ZERO_ERROR
;
673 ResourceBundle
theBundle( testdatapath
, *param
[i
].locale
, status
);
674 //ResourceBundle theBundle( "c:\\icu\\icu\\source\\test\\testdata\\testdata", *param[i].locale, status);
675 CONFIRM_UErrorCode(status
,param
[i
].expected_constructor_status
);
678 actual_bundle
= 0; /* ne -> default */
680 actual_bundle
= 1; /* te_NE -> te */
682 actual_bundle
= 2; /* te_IN_NE -> te_IN */
687 UErrorCode expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
688 for (j
=e_te_IN
; j
>=e_Root
; --j
)
690 if (is_in
[j
] && param
[i
].inherits
[j
])
692 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
693 expected_resource_status
= U_ZERO_ERROR
;
695 expected_resource_status
= U_USING_DEFAULT_WARNING
;
697 expected_resource_status
= U_USING_FALLBACK_WARNING
;
703 UErrorCode expected_status
;
706 for (j
=param
[i
].where
; j
>=0; --j
)
715 //--------------------------------------------------------------------------
718 uprv_strcpy(tag
, "string_");
719 uprv_strcat(tag
, frag
);
721 action
= param
[i
].name
;
722 action
+= ".getStringEx(";
727 status
= U_ZERO_ERROR
;
728 UnicodeString string
= theBundle
.getStringEx(tag
, status
);
729 if(U_FAILURE(status
)) {
730 string
.setTo(TRUE
, kErrorUChars
, kErrorLength
);
733 CONFIRM_UErrorCode(status
, expected_resource_status
);
735 UnicodeString
expected_string(kErrorUChars
);
736 if (U_SUCCESS(status
)) {
737 expected_string
= base
;
740 CONFIRM_EQ(string
, expected_string
);
742 //--------------------------------------------------------------------------
743 // array ResourceBundle using the key
745 uprv_strcpy(tag
, "array_");
746 uprv_strcat(tag
, frag
);
748 action
= param
[i
].name
;
753 int32_t count
= kERROR_COUNT
;
754 status
= U_ZERO_ERROR
;
755 ResourceBundle array
= theBundle
.get(tag
, status
);
756 CONFIRM_UErrorCode(status
,expected_resource_status
);
759 if (U_SUCCESS(status
))
761 //confirm the resource type is an array
762 UResType bundleType
=array
.getType();
763 CONFIRM_EQ(bundleType
, URES_ARRAY
);
765 count
=array
.getSize();
768 for (j
=0; j
<count
; ++j
)
771 expected_string
= base
;
772 expected_string
+= itoa(j
,buf
);
773 CONFIRM_EQ(array
.getNextString(status
),expected_string
);
779 CONFIRM_EQ(count
,kERROR_COUNT
);
780 // CONFIRM_EQ((int32_t)(unsigned long)array,(int32_t)0);
784 //--------------------------------------------------------------------------
785 // arrayItem ResourceBundle using the index
788 for (j
=0; j
<100; ++j
)
790 index
= count
? (randi(count
* 3) - count
) : (randi(200) - 100);
791 status
= U_ZERO_ERROR
;
792 string
= kErrorUChars
;
793 ResourceBundle array
= theBundle
.get(tag
, status
);
794 if(!U_FAILURE(status
)){
795 UnicodeString t
= array
.getStringEx(index
, status
);
796 if(!U_FAILURE(status
)) {
801 expected_status
= (index
>= 0 && index
< count
) ? expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
802 CONFIRM_UErrorCode(status
,expected_status
);
804 if (U_SUCCESS(status
)){
806 expected_string
= base
;
807 expected_string
+= itoa(index
,buf
);
809 expected_string
= kErrorUChars
;
811 CONFIRM_EQ(string
,expected_string
);
815 //--------------------------------------------------------------------------
818 uprv_strcpy(tag
, "array_2d_");
819 uprv_strcat(tag
, frag
);
821 action
= param
[i
].name
;
827 int32_t row_count
= kERROR_COUNT
, column_count
= kERROR_COUNT
;
828 status
= U_ZERO_ERROR
;
829 ResourceBundle array2d
=theBundle
.get(tag
, status
);
831 //const UnicodeString** array2d = theBundle.get2dArray(tag, row_count, column_count, status);
832 CONFIRM_UErrorCode(status
,expected_resource_status
);
834 if (U_SUCCESS(status
))
836 //confirm the resource type is an 2darray
837 UResType bundleType
=array2d
.getType();
838 CONFIRM_EQ(bundleType
, URES_ARRAY
);
840 row_count
=array2d
.getSize();
841 CONFIRM_GE(row_count
,1);
843 for(row
=0; row
<row_count
; ++row
){
844 ResourceBundle tablerow
=array2d
.get(row
, status
);
845 CONFIRM_UErrorCode(status
, expected_resource_status
);
846 if(U_SUCCESS(status
)){
847 //confirm the resourcetype of each table row is an array
848 UResType rowType
=tablerow
.getType();
849 CONFIRM_EQ(rowType
, URES_ARRAY
);
851 column_count
=tablerow
.getSize();
852 CONFIRM_GE(column_count
,1);
854 for (col
=0; j
<column_count
; ++j
) {
856 expected_string
= base
;
857 expected_string
+= itoa(row
,buf
);
858 expected_string
+= itoa(col
,buf
);
859 CONFIRM_EQ(tablerow
.getNextString(status
),expected_string
);
864 CONFIRM_EQ(row_count
,kERROR_COUNT
);
865 CONFIRM_EQ(column_count
,kERROR_COUNT
);
866 row_count
=column_count
=0;
872 //--------------------------------------------------------------------------
874 for (j
=0; j
<200; ++j
)
876 row
= row_count
? (randi(row_count
* 3) - row_count
) : (randi(200) - 100);
877 col
= column_count
? (randi(column_count
* 3) - column_count
) : (randi(200) - 100);
878 status
= U_ZERO_ERROR
;
879 string
= kErrorUChars
;
880 ResourceBundle array2d
=theBundle
.get(tag
, status
);
881 if(U_SUCCESS(status
)){
882 ResourceBundle tablerow
=array2d
.get(row
, status
);
883 if(U_SUCCESS(status
)) {
884 UnicodeString t
=tablerow
.getStringEx(col
, status
);
885 if(U_SUCCESS(status
)){
890 expected_status
= (row
>= 0 && row
< row_count
&& col
>= 0 && col
< column_count
) ?
891 expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
892 CONFIRM_UErrorCode(status
,expected_status
);
894 if (U_SUCCESS(status
)){
896 expected_string
= base
;
897 expected_string
+= itoa(row
,buf
);
898 expected_string
+= itoa(col
,buf
);
900 expected_string
= kErrorUChars
;
902 CONFIRM_EQ(string
,expected_string
);
906 //--------------------------------------------------------------------------
909 uprv_strcpy(tag
, "tagged_array_");
910 uprv_strcat(tag
, frag
);
912 action
= param
[i
].name
;
918 status
= U_ZERO_ERROR
;
920 ResourceBundle tags
=theBundle
.get(tag
, status
);
921 CONFIRM_UErrorCode(status
, expected_resource_status
);
923 if (U_SUCCESS(status
)) {
924 UResType bundleType
=tags
.getType();
925 CONFIRM_EQ(bundleType
, URES_TABLE
);
927 tag_count
=tags
.getSize();
928 CONFIRM_GE((int32_t)tag_count
, (int32_t)0);
930 for(index
=0; index
<tag_count
; index
++){
931 ResourceBundle tagelement
=tags
.get(index
, status
);
932 UnicodeString key
=tagelement
.getKey();
933 UnicodeString value
=tagelement
.getNextString(status
);
934 logln("tag = " + key
+ ", value = " + value
);
935 if(key
.startsWith("tag") && value
.startsWith(base
)){
943 for(index
=0; index
<tag_count
; index
++){
944 ResourceBundle tagelement
=tags
.get(index
, status
);
945 const char *tkey
=NULL
;
946 UnicodeString value
=tagelement
.getNextString(&tkey
, status
);
947 UnicodeString
key(tkey
);
948 logln("tag = " + key
+ ", value = " + value
);
949 if(value
.startsWith(base
)){
963 //--------------------------------------------------------------------------
966 action
= param
[i
].name
;
972 for (index
=-20; index
<20; ++index
)
975 status
= U_ZERO_ERROR
;
976 string
= kErrorUChars
;
978 uprv_strcpy(item_tag
, "tag");
979 uprv_strcat(item_tag
, itoa(index
,buf
));
980 ResourceBundle tags
=theBundle
.get(tag
, status
);
981 if(U_SUCCESS(status
)){
982 ResourceBundle tagelement
=tags
.get(item_tag
, status
);
983 if(!U_FAILURE(status
)){
984 UResType elementType
=tagelement
.getType();
985 CONFIRM_EQ(elementType
, (int32_t)URES_STRING
);
986 const char* key
=tagelement
.getKey();
987 CONFIRM_EQ((UnicodeString
)key
, (UnicodeString
)item_tag
);
988 UnicodeString t
=tagelement
.getString(status
);
989 if(!U_FAILURE(status
)){
994 CONFIRM_UErrorCode(status
,U_MISSING_RESOURCE_ERROR
);
997 if (status
!= U_MISSING_RESOURCE_ERROR
) {
999 expected_string
= base
;
1000 expected_string
+= buf
;
1001 CONFIRM_EQ(string
,expected_string
);
1007 CONFIRM_EQ(count
, tag_count
);
1010 return (UBool
)(failOrig
== fail
);
1014 NewResourceBundleTest::record_pass()
1019 NewResourceBundleTest::record_fail()
1027 NewResourceBundleTest::TestNewTypes() {
1029 const char* testdatapath
;
1030 UErrorCode status
= U_ZERO_ERROR
;
1031 uint8_t *binResult
= NULL
;
1034 int32_t intResult
= 0;
1035 uint32_t uintResult
= 0;
1036 UChar expected
[] = { 'a','b','c','\0','d','e','f' };
1037 const char* expect
="tab:\t cr:\r ff:\f newline:\n backslash:\\\\ quote=\\\' doubleQuote=\\\" singlequoutes=''";
1040 testdatapath
=loadTestData(status
);
1042 if(U_FAILURE(status
))
1044 dataerrln("Could not load testdata.dat %s \n",u_errorName(status
));
1048 ResourceBundle
theBundle(testdatapath
, "testtypes", status
);
1049 ResourceBundle
bundle(testdatapath
, Locale("te_IN"),status
);
1051 UnicodeString emptyStr
= theBundle
.getStringEx("emptystring", status
);
1052 if(emptyStr
.length() != 0) {
1053 logln("Empty string returned invalid value\n");
1056 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1058 /* This test reads the string "abc\u0000def" from the bundle */
1059 /* if everything is working correctly, the size of this string */
1060 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
1062 strcpy(action
, "getting and testing of string with embeded zero");
1063 ResourceBundle res
= theBundle
.get("zerotest", status
);
1064 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1065 CONFIRM_EQ(res
.getType(), URES_STRING
);
1066 UnicodeString zeroString
=res
.getString(status
);
1067 len
= zeroString
.length();
1068 if(U_SUCCESS(status
)){
1069 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1074 if(zeroString
[i
]!= expected
[i
]){
1075 logln("Output didnot match Expected: \\u%4X Got: \\u%4X", expected
[i
], zeroString
[i
]);
1079 strcpy(action
, "getting and testing of binary type");
1080 res
= theBundle
.get("binarytest", status
);
1081 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1082 CONFIRM_EQ(res
.getType(), URES_BINARY
);
1083 binResult
=(uint8_t*)res
.getBinary(len
, status
);
1084 if(U_SUCCESS(status
)){
1085 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1086 CONFIRM_EQ(len
, 15);
1087 for(i
= 0; i
<15; i
++) {
1088 CONFIRM_EQ(binResult
[i
], i
);
1092 strcpy(action
, "getting and testing of imported binary type");
1093 res
= theBundle
.get("importtest",status
);
1094 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1095 CONFIRM_EQ(res
.getType(), URES_BINARY
);
1096 binResult
=(uint8_t*)res
.getBinary(len
, status
);
1097 if(U_SUCCESS(status
)){
1098 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1099 CONFIRM_EQ(len
, 15);
1100 for(i
= 0; i
<15; i
++) {
1101 CONFIRM_EQ(binResult
[i
], i
);
1105 strcpy(action
, "getting and testing of integer types");
1106 res
= theBundle
.get("one", status
);
1107 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1108 CONFIRM_EQ(res
.getType(), URES_INT
);
1109 intResult
=res
.getInt(status
);
1110 uintResult
= res
.getUInt(status
);
1111 if(U_SUCCESS(status
)){
1112 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1113 CONFIRM_EQ(uintResult
, (uint32_t)intResult
);
1114 CONFIRM_EQ(intResult
, 1);
1117 strcpy(action
, "getting minusone");
1118 res
= theBundle
.get((const char*)"minusone", status
);
1119 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1120 CONFIRM_EQ(res
.getType(), URES_INT
);
1121 intResult
=res
.getInt(status
);
1122 uintResult
= res
.getUInt(status
);
1123 if(U_SUCCESS(status
)){
1124 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1125 CONFIRM_EQ(uintResult
, 0x0FFFFFFF); /* a 28 bit integer */
1126 CONFIRM_EQ(intResult
, -1);
1127 CONFIRM_NE(uintResult
, (uint32_t)intResult
);
1130 strcpy(action
, "getting plusone");
1131 res
= theBundle
.get("plusone",status
);
1132 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1133 CONFIRM_EQ(res
.getType(), URES_INT
);
1134 intResult
=res
.getInt(status
);
1135 uintResult
= res
.getUInt(status
);
1136 if(U_SUCCESS(status
)){
1137 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1138 CONFIRM_EQ(uintResult
, (uint32_t)intResult
);
1139 CONFIRM_EQ(intResult
, 1);
1142 res
= theBundle
.get("onehundredtwentythree",status
);
1143 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1144 CONFIRM_EQ(res
.getType(), URES_INT
);
1145 intResult
=res
.getInt(status
);
1146 if(U_SUCCESS(status
)){
1147 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1148 CONFIRM_EQ(intResult
, 123);
1151 /* this tests if escapes are preserved or not */
1153 UnicodeString str
= theBundle
.getStringEx("testescape",status
);
1154 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1155 if(U_SUCCESS(status
)){
1156 u_charsToUChars(expect
,uExpect
,(int32_t)uprv_strlen(expect
)+1);
1157 if(str
.compare(uExpect
)!=0){
1158 errln("Did not get the expected string for testescape expected. Expected : "
1159 +UnicodeString(uExpect
)+ " Got: " + str
);
1163 /* test for jitterbug#1435 */
1165 UnicodeString str
= theBundle
.getStringEx("test_underscores",status
);
1166 expect
="test message ....";
1167 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1168 u_charsToUChars(expect
,uExpect
,(int32_t)uprv_strlen(expect
)+1);
1169 if(str
.compare(uExpect
)!=0){
1170 errln("Did not get the expected string for test_underscores.\n");
1178 NewResourceBundleTest::TestGetByFallback() {
1179 UErrorCode status
= U_ZERO_ERROR
;
1181 ResourceBundle
heRes(NULL
, "he", status
);
1183 heRes
.getWithFallback("calendar", status
).getWithFallback("islamic-civil", status
).getWithFallback("DateTime", status
);
1184 if(U_SUCCESS(status
)) {
1185 errln("he locale's Islamic-civil DateTime resource exists. How did it get here?\n");
1187 status
= U_ZERO_ERROR
;
1189 heRes
.getWithFallback("calendar", status
).getWithFallback("islamic-civil", status
).getWithFallback("eras", status
);
1190 if(U_FAILURE(status
)) {
1191 dataerrln("Didn't get Islamic Eras. I know they are there! - %s", u_errorName(status
));
1193 status
= U_ZERO_ERROR
;
1195 ResourceBundle
rootRes(NULL
, "root", status
);
1196 rootRes
.getWithFallback("calendar", status
).getWithFallback("islamic-civil", status
).getWithFallback("DateTime", status
);
1197 if(U_SUCCESS(status
)) {
1198 errln("Root's Islamic-civil's DateTime resource exists. How did it get here?\n");
1200 status
= U_ZERO_ERROR
;
1205 #define REQUIRE_SUCCESS(status) { \
1206 if (status.errIfFailureAndReset("line %d", __LINE__)) { \
1211 #define REQUIRE_ERROR(expected, status) { \
1212 if (!status.expectErrorAndReset(expected, "line %d", __LINE__)) { \
1218 * Tests the --filterDir option in genrb.
1220 * Input resource text file: test/testdata/filtertest.txt
1221 * Input filter rule file: test/testdata/filters/filtertest.txt
1223 * The resource bundle should contain no keys matched by the filter
1224 * and should contain all other keys.
1226 void NewResourceBundleTest::TestFilter() {
1227 IcuTestErrorCode
status(*this, "TestFilter");
1229 ResourceBundle
rb(loadTestData(status
), "filtertest", status
);
1230 REQUIRE_SUCCESS(status
);
1231 assertEquals("rb", rb
.getType(), URES_TABLE
);
1233 ResourceBundle alabama
= rb
.get("alabama", status
);
1234 REQUIRE_SUCCESS(status
);
1235 assertEquals("alabama", alabama
.getType(), URES_TABLE
);
1238 ResourceBundle alaska
= alabama
.get("alaska", status
);
1239 REQUIRE_SUCCESS(status
);
1240 assertEquals("alaska", alaska
.getType(), URES_TABLE
);
1243 ResourceBundle arizona
= alaska
.get("arizona", status
);
1244 REQUIRE_SUCCESS(status
);
1245 assertEquals("arizona", arizona
.getType(), URES_STRING
);
1246 assertEquals("arizona", u
"arkansas", arizona
.getString(status
));
1247 REQUIRE_SUCCESS(status
);
1249 // Filter: california should not be included
1250 ResourceBundle california
= alaska
.get("california", status
);
1251 REQUIRE_ERROR(U_MISSING_RESOURCE_ERROR
, status
);
1254 // Filter: connecticut should not be included
1255 ResourceBundle connecticut
= alabama
.get("connecticut", status
);
1256 REQUIRE_ERROR(U_MISSING_RESOURCE_ERROR
, status
);
1259 ResourceBundle fornia
= rb
.get("fornia", status
);
1260 REQUIRE_SUCCESS(status
);
1261 assertEquals("fornia", fornia
.getType(), URES_TABLE
);
1264 ResourceBundle hawaii
= fornia
.get("hawaii", status
);
1265 REQUIRE_SUCCESS(status
);
1266 assertEquals("hawaii", hawaii
.getType(), URES_STRING
);
1267 assertEquals("hawaii", u
"idaho", hawaii
.getString(status
));
1268 REQUIRE_SUCCESS(status
);
1270 // Filter: illinois should not be included
1271 ResourceBundle illinois
= fornia
.get("illinois", status
);
1272 REQUIRE_ERROR(U_MISSING_RESOURCE_ERROR
, status
);
1275 ResourceBundle mississippi
= rb
.get("mississippi", status
);
1276 REQUIRE_SUCCESS(status
);
1277 assertEquals("mississippi", mississippi
.getType(), URES_TABLE
);
1280 ResourceBundle louisiana
= mississippi
.get("louisiana", status
);
1281 REQUIRE_SUCCESS(status
);
1282 assertEquals("louisiana", louisiana
.getType(), URES_TABLE
);
1285 ResourceBundle maine
= louisiana
.get("maine", status
);
1286 REQUIRE_SUCCESS(status
);
1287 assertEquals("maine", maine
.getType(), URES_STRING
);
1288 assertEquals("maine", u
"maryland", maine
.getString(status
));
1289 REQUIRE_SUCCESS(status
);
1291 ResourceBundle iowa
= louisiana
.get("iowa", status
);
1292 REQUIRE_SUCCESS(status
);
1293 assertEquals("iowa", iowa
.getType(), URES_STRING
);
1294 assertEquals("iowa", u
"kansas", iowa
.getString(status
));
1295 REQUIRE_SUCCESS(status
);
1297 // Filter: missouri should not be included
1298 ResourceBundle missouri
= louisiana
.get("missouri", status
);
1299 REQUIRE_ERROR(U_MISSING_RESOURCE_ERROR
, status
);
1302 ResourceBundle michigan
= mississippi
.get("michigan", status
);
1303 REQUIRE_SUCCESS(status
);
1304 assertEquals("michigan", michigan
.getType(), URES_TABLE
);
1307 ResourceBundle maine
= michigan
.get("maine", status
);
1308 REQUIRE_SUCCESS(status
);
1309 assertEquals("maine", maine
.getType(), URES_STRING
);
1310 assertEquals("maine", u
"minnesota", maine
.getString(status
));
1311 REQUIRE_SUCCESS(status
);
1313 // Filter: iowa should not be included
1314 ResourceBundle iowa
= michigan
.get("iowa", status
);
1315 REQUIRE_ERROR(U_MISSING_RESOURCE_ERROR
, status
);
1317 ResourceBundle missouri
= michigan
.get("missouri", status
);
1318 REQUIRE_SUCCESS(status
);
1319 assertEquals("missouri", missouri
.getType(), URES_STRING
);
1320 assertEquals("missouri", u
"nebraska", missouri
.getString(status
));
1321 REQUIRE_SUCCESS(status
);
1324 ResourceBundle nevada
= mississippi
.get("nevada", status
);
1325 REQUIRE_SUCCESS(status
);
1326 assertEquals("nevada", nevada
.getType(), URES_TABLE
);
1329 ResourceBundle maine
= nevada
.get("maine", status
);
1330 REQUIRE_SUCCESS(status
);
1331 assertEquals("maine", maine
.getType(), URES_STRING
);
1332 assertEquals("maine", u
"new-hampshire", maine
.getString(status
));
1333 REQUIRE_SUCCESS(status
);
1335 // Filter: iowa should not be included
1336 ResourceBundle iowa
= nevada
.get("iowa", status
);
1337 REQUIRE_ERROR(U_MISSING_RESOURCE_ERROR
, status
);
1339 // Filter: missouri should not be included
1340 ResourceBundle missouri
= nevada
.get("missouri", status
);
1341 REQUIRE_ERROR(U_MISSING_RESOURCE_ERROR
, status
);