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 default: name
= ""; break; //needed to end loop
202 //***************************************************************************************
205 NewResourceBundleTest::TestResourceBundles()
207 UErrorCode status
= U_ZERO_ERROR
;
208 loadTestData(status
);
209 if(U_FAILURE(status
))
211 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status
)));
215 /* Make sure that users using te_IN for the default locale don't get test failures. */
216 Locale originalDefault
;
217 if (Locale::getDefault() == Locale("te_IN")) {
218 Locale::setDefault(Locale("en_US"), status
);
221 testTag("only_in_Root", TRUE
, FALSE
, FALSE
);
222 testTag("only_in_te", FALSE
, TRUE
, FALSE
);
223 testTag("only_in_te_IN", FALSE
, FALSE
, TRUE
);
224 testTag("in_Root_te", TRUE
, TRUE
, FALSE
);
225 testTag("in_Root_te_te_IN", TRUE
, TRUE
, TRUE
);
226 testTag("in_Root_te_IN", TRUE
, FALSE
, TRUE
);
227 testTag("in_te_te_IN", FALSE
, TRUE
, TRUE
);
228 testTag("nonexistent", FALSE
, FALSE
, FALSE
);
229 logln("Passed: %d\nFailed: %d", pass
, fail
);
231 /* Restore the default locale for the other tests. */
232 Locale::setDefault(originalDefault
, status
);
236 NewResourceBundleTest::TestConstruction()
238 UErrorCode err
= U_ZERO_ERROR
;
239 Locale
locale("te", "IN");
241 const char* testdatapath
;
242 testdatapath
=loadTestData(err
);
245 dataerrln("Could not load testdata.dat %s " + 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
);
258 UnicodeString result1
;
259 UnicodeString result2
;
261 result1
= test1
.getStringEx("string_in_Root_te_te_IN", err
);
262 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, root.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");
275 const char* version1
;
276 const char* version2
;
278 version1
= test1
.getVersionNumber();
279 version2
= test2
.getVersionNumber();
281 char *versionID1
= new char[1 + strlen(U_ICU_VERSION
) + strlen(version1
)]; // + 1 for zero byte
282 char *versionID2
= new char[1 + strlen(U_ICU_VERSION
) + strlen(version2
)]; // + 1 for zero byte
284 strcpy(versionID1
, "44.0"); // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed.
286 strcpy(versionID2
, "55.0"); // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed.
288 logln(UnicodeString("getVersionNumber on default.txt returned ") + version1
+ UnicodeString(" Expect: " ) + versionID1
);
289 logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2
+ UnicodeString(" Expect: " ) + versionID2
);
291 if (strcmp(version1
, versionID1
) != 0) {
292 errln("getVersionNumber(version1) failed. %s != %s", version1
, versionID1
);
294 if (strcmp(version2
, versionID2
) != 0) {
295 errln("getVersionNumber(version2) failed. %s != %s", version2
, versionID2
);
300 /* Restore the default locale for the other tests. */
301 Locale::setDefault(originalDefault
, err
);
305 NewResourceBundleTest::TestIteration()
307 UErrorCode err
= U_ZERO_ERROR
;
308 const char* testdatapath
;
310 "string_in_Root_te_te_IN", "1",
311 "array_in_Root_te_te_IN", "5",
312 "array_2d_in_Root_te_te_IN", "4",
315 Locale
*locale
=new Locale("te_IN");
317 testdatapath
=loadTestData(err
);
320 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
324 ResourceBundle
test1(testdatapath
, *locale
, err
);
326 errln("Construction failed");
329 int32_t count
, row
=0, col
=0;
331 UnicodeString expected
;
332 UnicodeString
element("TE_IN");
333 UnicodeString action
;
336 for(i
=0; i
<UPRV_LENGTHOF(data
); i
=i
+2){
342 ResourceBundle bundle
= test1
.get(data
[i
], err
);
345 action
+=".getKey()";
347 CONFIRM_EQ((UnicodeString
)bundle
.getKey(), (UnicodeString
)data
[i
]);
351 while(bundle
.hasNext()){
353 action
+=".getNextString(err)";
355 UnicodeString got
=bundle
.getNextString(err
);
358 if(bundle
.getSize() > 1){
359 CONFIRM_EQ(bundle
.getType(), URES_ARRAY
);
360 expected
+=itoa(row
, buf
);
361 ResourceBundle rowbundle
=bundle
.get(row
, err
);
362 if(!U_FAILURE(err
) && rowbundle
.getSize()>1){
364 while(rowbundle
.hasNext()){
366 got
=rowbundle
.getNextString(err
);
368 expected
+=itoa(row
, buf
);
369 expected
+=itoa(col
, buf
);
371 CONFIRM_EQ(got
, expected
);
374 CONFIRM_EQ(col
, rowbundle
.getSize());
378 CONFIRM_EQ(bundle
.getType(), (int32_t)URES_STRING
);
381 CONFIRM_EQ(got
, expected
);
385 action
+=".getSize()";
386 CONFIRM_EQ(bundle
.getSize(), count
);
387 CONFIRM_EQ(count
, atoi(data
[i
+1]));
388 //after reaching the end
390 ResourceBundle errbundle
=bundle
.getNext(err
);
391 action
= "After reaching the end of the Iterator:- ";
393 action
+=".getNext()";
394 CONFIRM_NE(err
, (int32_t)U_ZERO_ERROR
);
395 CONFIRM_EQ(u_errorName(err
), u_errorName(U_INDEX_OUTOFBOUNDS_ERROR
));
398 bundle
.resetIterator();
399 /* The following code is causing a crash
405 errln("ERROR: getNext() throw an error");
412 // TODO: add operator== and != to ResourceBundle
414 equalRB(ResourceBundle
&a
, ResourceBundle
&b
) {
422 a
.getLocale()==b
.getLocale() &&
423 0==strcmp(a
.getName(), b
.getName()) &&
425 a
.getString(status
)==b
.getString(status
) :
427 a
.getInt(status
)==b
.getInt(status
) :
432 NewResourceBundleTest::TestOtherAPI(){
433 UErrorCode err
= U_ZERO_ERROR
;
434 const char* testdatapath
=loadTestData(err
);
435 UnicodeString tDataPathUS
= UnicodeString(testdatapath
, "");
439 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
443 /* Make sure that users using te_IN for the default locale don't get test failures. */
444 Locale originalDefault
;
445 if (Locale::getDefault() == Locale("te_IN")) {
446 Locale::setDefault(Locale("en_US"), err
);
449 Locale
*locale
=new Locale("te_IN");
451 ResourceBundle
test0(tDataPathUS
, *locale
, err
);
453 errln("Construction failed");
457 ResourceBundle
test1(testdatapath
, *locale
, err
);
459 errln("Construction failed");
463 logln("Testing getLocale()\n");
464 if(strcmp(test1
.getLocale().getName(), locale
->getName()) !=0 ){
465 errln("FAIL: ResourceBundle::getLocale() failed\n");
470 logln("Testing ResourceBundle(UErrorCode)\n");
471 ResourceBundle
defaultresource(err
);
472 ResourceBundle
explicitdefaultresource(NULL
, Locale::getDefault(), err
);
474 errcheckln(err
, "Construction of default resourcebundle failed - %s", u_errorName(err
));
477 // You can't compare the default locale to the resolved locale in the
478 // resource bundle due to aliasing, keywords in the default locale
479 // or the chance that the machine running these tests is using a locale
480 // that isn't available in ICU.
481 if(strcmp(defaultresource
.getLocale().getName(), explicitdefaultresource
.getLocale().getName()) != 0){
482 errln("Construction of default resourcebundle didn't take the defaultlocale. Expected %s Got %s err=%s\n",
483 explicitdefaultresource
.getLocale().getName(), defaultresource
.getLocale().getName(), u_errorName(err
));
487 ResourceBundle
copyRes(defaultresource
);
488 if(strcmp(copyRes
.getName(), defaultresource
.getName() ) !=0 ||
489 strcmp(test1
.getName(), defaultresource
.getName() ) ==0 ||
490 strcmp(copyRes
.getLocale().getName(), defaultresource
.getLocale().getName() ) !=0 ||
491 strcmp(test1
.getLocale().getName(), defaultresource
.getLocale().getName() ) ==0 )
493 errln("copy construction failed\n");
496 ResourceBundle defaultSub
= defaultresource
.get((int32_t)0, err
);
497 ResourceBundle
defSubCopy(defaultSub
);
498 if(strcmp(defSubCopy
.getName(), defaultSub
.getName() ) !=0 ||
499 strcmp(defSubCopy
.getLocale().getName(), defaultSub
.getLocale().getName() ) !=0 ){
500 errln("copy construction for subresource failed\n");
505 p
= defaultresource
.clone();
506 if(p
== &defaultresource
|| !equalRB(*p
, defaultresource
)) {
507 errln("ResourceBundle.clone() failed");
511 p
= defaultSub
.clone();
512 if(p
== &defaultSub
|| !equalRB(*p
, defaultSub
)) {
513 errln("2nd ResourceBundle.clone() failed");
518 copyRes
.getVersion(ver
);
520 logln("Version returned: [%d.%d.%d.%d]\n", ver
[0], ver
[1], ver
[2], ver
[3]);
522 logln("Testing C like UnicodeString APIs\n");
524 UResourceBundle
*testCAPI
= NULL
, *bundle
= NULL
, *rowbundle
= NULL
, *temp
= NULL
;
527 "string_in_Root_te_te_IN", "1",
528 "array_in_Root_te_te_IN", "5",
529 "array_2d_in_Root_te_te_IN", "4",
533 testCAPI
= ures_open(testdatapath
, "te_IN", &err
);
540 int32_t count
, row
=0, col
=0;
542 UnicodeString expected
;
543 UnicodeString
element("TE_IN");
544 UnicodeString action
;
547 for(i
=0; i
<UPRV_LENGTHOF(data
); i
=i
+2){
553 bundle
= ures_getByKey(testCAPI
, data
[i
], bundle
, &err
);
555 const char* key
= NULL
;
557 action
+=".getKey()";
559 CONFIRM_EQ((UnicodeString
)ures_getKey(bundle
), (UnicodeString
)data
[i
]);
563 while(ures_hasNext(bundle
)){
565 action
+=".getNextString(err)";
567 UnicodeString got
=ures_getNextUnicodeString(bundle
, &key
, &err
);
570 if(ures_getSize(bundle
) > 1){
571 CONFIRM_EQ(ures_getType(bundle
), URES_ARRAY
);
572 expected
+=itoa(row
, buf
);
573 rowbundle
=ures_getByIndex(bundle
, row
, rowbundle
, &err
);
574 if(!U_FAILURE(err
) && ures_getSize(rowbundle
)>1){
576 while(ures_hasNext(rowbundle
)){
578 got
=ures_getNextUnicodeString(rowbundle
, &key
, &err
);
579 temp
= ures_getByIndex(rowbundle
, col
, temp
, &err
);
580 UnicodeString bla
= ures_getUnicodeString(temp
, &err
);
581 UnicodeString bla2
= ures_getUnicodeStringByIndex(rowbundle
, col
, &err
);
583 expected
+=itoa(row
, buf
);
584 expected
+=itoa(col
, buf
);
586 CONFIRM_EQ(got
, expected
);
587 CONFIRM_EQ(bla
, expected
);
588 CONFIRM_EQ(bla2
, expected
);
591 CONFIRM_EQ(col
, ures_getSize(rowbundle
));
595 CONFIRM_EQ(ures_getType(bundle
), (int32_t)URES_STRING
);
598 CONFIRM_EQ(got
, expected
);
604 // Check that ures_getUnicodeString() & variants return a bogus string if failure.
605 // Same relevant code path whether the failure code is passed in
606 // or comes from a lookup error.
607 UErrorCode failure
= U_INTERNAL_PROGRAM_ERROR
;
608 assertTrue("ures_getUnicodeString(failure).isBogus()",
609 ures_getUnicodeString(testCAPI
, &failure
).isBogus());
610 assertTrue("ures_getNextUnicodeString(failure).isBogus()",
611 ures_getNextUnicodeString(testCAPI
, NULL
, &failure
).isBogus());
612 assertTrue("ures_getUnicodeStringByIndex(failure).isBogus()",
613 ures_getUnicodeStringByIndex(testCAPI
, 999, &failure
).isBogus());
614 assertTrue("ures_getUnicodeStringByKey(failure).isBogus()",
615 ures_getUnicodeStringByKey(testCAPI
, "bogus key", &failure
).isBogus());
618 ures_close(rowbundle
);
620 ures_close(testCAPI
);
622 errln("failed to open a resource bundle\n");
625 /* Restore the default locale for the other tests. */
626 Locale::setDefault(originalDefault
, err
);
632 //***************************************************************************************
635 NewResourceBundleTest::testTag(const char* frag
,
640 int32_t failOrig
= fail
;
642 // Make array from input params
644 UBool is_in
[] = { in_Root
, in_te
, in_te_IN
};
646 const char* NAME
[] = { "ROOT", "TE", "TE_IN" };
648 // Now try to load the desired items
651 UnicodeString action
;
653 int32_t i
,j
,row
,col
, actual_bundle
;
655 const char* testdatapath
;
657 UErrorCode status
= U_ZERO_ERROR
;
658 testdatapath
=loadTestData(status
);
659 if(U_FAILURE(status
))
661 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status
)));
665 for (i
=0; i
<bundles_count
; ++i
)
667 action
= "Constructor for ";
668 action
+= param
[i
].name
;
670 status
= U_ZERO_ERROR
;
671 ResourceBundle
theBundle( testdatapath
, *param
[i
].locale
, status
);
672 //ResourceBundle theBundle( "c:\\icu\\icu\\source\\test\\testdata\\testdata", *param[i].locale, status);
673 CONFIRM_UErrorCode(status
,param
[i
].expected_constructor_status
);
676 actual_bundle
= 0; /* ne -> default */
678 actual_bundle
= 1; /* te_NE -> te */
680 actual_bundle
= 2; /* te_IN_NE -> te_IN */
685 UErrorCode expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
686 for (j
=e_te_IN
; j
>=e_Root
; --j
)
688 if (is_in
[j
] && param
[i
].inherits
[j
])
690 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
691 expected_resource_status
= U_ZERO_ERROR
;
693 expected_resource_status
= U_USING_DEFAULT_WARNING
;
695 expected_resource_status
= U_USING_FALLBACK_WARNING
;
701 UErrorCode expected_status
;
704 for (j
=param
[i
].where
; j
>=0; --j
)
713 //--------------------------------------------------------------------------
716 uprv_strcpy(tag
, "string_");
717 uprv_strcat(tag
, frag
);
719 action
= param
[i
].name
;
720 action
+= ".getStringEx(";
725 status
= U_ZERO_ERROR
;
726 UnicodeString string
= theBundle
.getStringEx(tag
, status
);
727 if(U_FAILURE(status
)) {
728 string
.setTo(TRUE
, kErrorUChars
, kErrorLength
);
731 CONFIRM_UErrorCode(status
, expected_resource_status
);
733 UnicodeString
expected_string(kErrorUChars
);
734 if (U_SUCCESS(status
)) {
735 expected_string
= base
;
738 CONFIRM_EQ(string
, expected_string
);
740 //--------------------------------------------------------------------------
741 // array ResourceBundle using the key
743 uprv_strcpy(tag
, "array_");
744 uprv_strcat(tag
, frag
);
746 action
= param
[i
].name
;
751 int32_t count
= kERROR_COUNT
;
752 status
= U_ZERO_ERROR
;
753 ResourceBundle array
= theBundle
.get(tag
, status
);
754 CONFIRM_UErrorCode(status
,expected_resource_status
);
757 if (U_SUCCESS(status
))
759 //confirm the resource type is an array
760 UResType bundleType
=array
.getType();
761 CONFIRM_EQ(bundleType
, URES_ARRAY
);
763 count
=array
.getSize();
766 for (j
=0; j
<count
; ++j
)
769 expected_string
= base
;
770 expected_string
+= itoa(j
,buf
);
771 CONFIRM_EQ(array
.getNextString(status
),expected_string
);
777 CONFIRM_EQ(count
,kERROR_COUNT
);
778 // CONFIRM_EQ((int32_t)(unsigned long)array,(int32_t)0);
782 //--------------------------------------------------------------------------
783 // arrayItem ResourceBundle using the index
786 for (j
=0; j
<100; ++j
)
788 index
= count
? (randi(count
* 3) - count
) : (randi(200) - 100);
789 status
= U_ZERO_ERROR
;
790 string
= kErrorUChars
;
791 ResourceBundle array
= theBundle
.get(tag
, status
);
792 if(!U_FAILURE(status
)){
793 UnicodeString t
= array
.getStringEx(index
, status
);
794 if(!U_FAILURE(status
)) {
799 expected_status
= (index
>= 0 && index
< count
) ? expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
800 CONFIRM_UErrorCode(status
,expected_status
);
802 if (U_SUCCESS(status
)){
804 expected_string
= base
;
805 expected_string
+= itoa(index
,buf
);
807 expected_string
= kErrorUChars
;
809 CONFIRM_EQ(string
,expected_string
);
813 //--------------------------------------------------------------------------
816 uprv_strcpy(tag
, "array_2d_");
817 uprv_strcat(tag
, frag
);
819 action
= param
[i
].name
;
825 int32_t row_count
= kERROR_COUNT
, column_count
= kERROR_COUNT
;
826 status
= U_ZERO_ERROR
;
827 ResourceBundle array2d
=theBundle
.get(tag
, status
);
829 //const UnicodeString** array2d = theBundle.get2dArray(tag, row_count, column_count, status);
830 CONFIRM_UErrorCode(status
,expected_resource_status
);
832 if (U_SUCCESS(status
))
834 //confirm the resource type is an 2darray
835 UResType bundleType
=array2d
.getType();
836 CONFIRM_EQ(bundleType
, URES_ARRAY
);
838 row_count
=array2d
.getSize();
839 CONFIRM_GE(row_count
,1);
841 for(row
=0; row
<row_count
; ++row
){
842 ResourceBundle tablerow
=array2d
.get(row
, status
);
843 CONFIRM_UErrorCode(status
, expected_resource_status
);
844 if(U_SUCCESS(status
)){
845 //confirm the resourcetype of each table row is an array
846 UResType rowType
=tablerow
.getType();
847 CONFIRM_EQ(rowType
, URES_ARRAY
);
849 column_count
=tablerow
.getSize();
850 CONFIRM_GE(column_count
,1);
852 for (col
=0; j
<column_count
; ++j
) {
854 expected_string
= base
;
855 expected_string
+= itoa(row
,buf
);
856 expected_string
+= itoa(col
,buf
);
857 CONFIRM_EQ(tablerow
.getNextString(status
),expected_string
);
862 CONFIRM_EQ(row_count
,kERROR_COUNT
);
863 CONFIRM_EQ(column_count
,kERROR_COUNT
);
864 row_count
=column_count
=0;
870 //--------------------------------------------------------------------------
872 for (j
=0; j
<200; ++j
)
874 row
= row_count
? (randi(row_count
* 3) - row_count
) : (randi(200) - 100);
875 col
= column_count
? (randi(column_count
* 3) - column_count
) : (randi(200) - 100);
876 status
= U_ZERO_ERROR
;
877 string
= kErrorUChars
;
878 ResourceBundle array2d
=theBundle
.get(tag
, status
);
879 if(U_SUCCESS(status
)){
880 ResourceBundle tablerow
=array2d
.get(row
, status
);
881 if(U_SUCCESS(status
)) {
882 UnicodeString t
=tablerow
.getStringEx(col
, status
);
883 if(U_SUCCESS(status
)){
888 expected_status
= (row
>= 0 && row
< row_count
&& col
>= 0 && col
< column_count
) ?
889 expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
890 CONFIRM_UErrorCode(status
,expected_status
);
892 if (U_SUCCESS(status
)){
894 expected_string
= base
;
895 expected_string
+= itoa(row
,buf
);
896 expected_string
+= itoa(col
,buf
);
898 expected_string
= kErrorUChars
;
900 CONFIRM_EQ(string
,expected_string
);
904 //--------------------------------------------------------------------------
907 uprv_strcpy(tag
, "tagged_array_");
908 uprv_strcat(tag
, frag
);
910 action
= param
[i
].name
;
916 status
= U_ZERO_ERROR
;
918 ResourceBundle tags
=theBundle
.get(tag
, status
);
919 CONFIRM_UErrorCode(status
, expected_resource_status
);
921 if (U_SUCCESS(status
)) {
922 UResType bundleType
=tags
.getType();
923 CONFIRM_EQ(bundleType
, URES_TABLE
);
925 tag_count
=tags
.getSize();
926 CONFIRM_GE((int32_t)tag_count
, (int32_t)0);
928 for(index
=0; index
<tag_count
; index
++){
929 ResourceBundle tagelement
=tags
.get(index
, status
);
930 UnicodeString key
=tagelement
.getKey();
931 UnicodeString value
=tagelement
.getNextString(status
);
932 logln("tag = " + key
+ ", value = " + value
);
933 if(key
.startsWith("tag") && value
.startsWith(base
)){
941 for(index
=0; index
<tag_count
; index
++){
942 ResourceBundle tagelement
=tags
.get(index
, status
);
943 const char *tkey
=NULL
;
944 UnicodeString value
=tagelement
.getNextString(&tkey
, status
);
945 UnicodeString
key(tkey
);
946 logln("tag = " + key
+ ", value = " + value
);
947 if(value
.startsWith(base
)){
961 //--------------------------------------------------------------------------
964 action
= param
[i
].name
;
970 for (index
=-20; index
<20; ++index
)
973 status
= U_ZERO_ERROR
;
974 string
= kErrorUChars
;
976 uprv_strcpy(item_tag
, "tag");
977 uprv_strcat(item_tag
, itoa(index
,buf
));
978 ResourceBundle tags
=theBundle
.get(tag
, status
);
979 if(U_SUCCESS(status
)){
980 ResourceBundle tagelement
=tags
.get(item_tag
, status
);
981 if(!U_FAILURE(status
)){
982 UResType elementType
=tagelement
.getType();
983 CONFIRM_EQ(elementType
, (int32_t)URES_STRING
);
984 const char* key
=tagelement
.getKey();
985 CONFIRM_EQ((UnicodeString
)key
, (UnicodeString
)item_tag
);
986 UnicodeString t
=tagelement
.getString(status
);
987 if(!U_FAILURE(status
)){
992 CONFIRM_UErrorCode(status
,U_MISSING_RESOURCE_ERROR
);
995 if (status
!= U_MISSING_RESOURCE_ERROR
) {
997 expected_string
= base
;
998 expected_string
+= buf
;
999 CONFIRM_EQ(string
,expected_string
);
1005 CONFIRM_EQ(count
, tag_count
);
1008 return (UBool
)(failOrig
== fail
);
1012 NewResourceBundleTest::record_pass()
1017 NewResourceBundleTest::record_fail()
1025 NewResourceBundleTest::TestNewTypes() {
1027 const char* testdatapath
;
1028 UErrorCode status
= U_ZERO_ERROR
;
1029 uint8_t *binResult
= NULL
;
1032 int32_t intResult
= 0;
1033 uint32_t uintResult
= 0;
1034 UChar expected
[] = { 'a','b','c','\0','d','e','f' };
1035 const char* expect
="tab:\t cr:\r ff:\f newline:\n backslash:\\\\ quote=\\\' doubleQuote=\\\" singlequoutes=''";
1038 testdatapath
=loadTestData(status
);
1040 if(U_FAILURE(status
))
1042 dataerrln("Could not load testdata.dat %s \n",u_errorName(status
));
1046 ResourceBundle
theBundle(testdatapath
, "testtypes", status
);
1047 ResourceBundle
bundle(testdatapath
, Locale("te_IN"),status
);
1049 UnicodeString emptyStr
= theBundle
.getStringEx("emptystring", status
);
1050 if(emptyStr
.length() != 0) {
1051 logln("Empty string returned invalid value\n");
1054 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1056 /* This test reads the string "abc\u0000def" from the bundle */
1057 /* if everything is working correctly, the size of this string */
1058 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
1060 strcpy(action
, "getting and testing of string with embeded zero");
1061 ResourceBundle res
= theBundle
.get("zerotest", status
);
1062 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1063 CONFIRM_EQ(res
.getType(), URES_STRING
);
1064 UnicodeString zeroString
=res
.getString(status
);
1065 len
= zeroString
.length();
1066 if(U_SUCCESS(status
)){
1067 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1072 if(zeroString
[i
]!= expected
[i
]){
1073 logln("Output didnot match Expected: \\u%4X Got: \\u%4X", expected
[i
], zeroString
[i
]);
1077 strcpy(action
, "getting and testing of binary type");
1078 res
= theBundle
.get("binarytest", status
);
1079 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1080 CONFIRM_EQ(res
.getType(), URES_BINARY
);
1081 binResult
=(uint8_t*)res
.getBinary(len
, status
);
1082 if(U_SUCCESS(status
)){
1083 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1084 CONFIRM_EQ(len
, 15);
1085 for(i
= 0; i
<15; i
++) {
1086 CONFIRM_EQ(binResult
[i
], i
);
1090 strcpy(action
, "getting and testing of imported binary type");
1091 res
= theBundle
.get("importtest",status
);
1092 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1093 CONFIRM_EQ(res
.getType(), URES_BINARY
);
1094 binResult
=(uint8_t*)res
.getBinary(len
, status
);
1095 if(U_SUCCESS(status
)){
1096 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1097 CONFIRM_EQ(len
, 15);
1098 for(i
= 0; i
<15; i
++) {
1099 CONFIRM_EQ(binResult
[i
], i
);
1103 strcpy(action
, "getting and testing of integer types");
1104 res
= theBundle
.get("one", status
);
1105 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1106 CONFIRM_EQ(res
.getType(), URES_INT
);
1107 intResult
=res
.getInt(status
);
1108 uintResult
= res
.getUInt(status
);
1109 if(U_SUCCESS(status
)){
1110 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1111 CONFIRM_EQ(uintResult
, (uint32_t)intResult
);
1112 CONFIRM_EQ(intResult
, 1);
1115 strcpy(action
, "getting minusone");
1116 res
= theBundle
.get((const char*)"minusone", status
);
1117 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1118 CONFIRM_EQ(res
.getType(), URES_INT
);
1119 intResult
=res
.getInt(status
);
1120 uintResult
= res
.getUInt(status
);
1121 if(U_SUCCESS(status
)){
1122 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1123 CONFIRM_EQ(uintResult
, 0x0FFFFFFF); /* a 28 bit integer */
1124 CONFIRM_EQ(intResult
, -1);
1125 CONFIRM_NE(uintResult
, (uint32_t)intResult
);
1128 strcpy(action
, "getting plusone");
1129 res
= theBundle
.get("plusone",status
);
1130 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1131 CONFIRM_EQ(res
.getType(), URES_INT
);
1132 intResult
=res
.getInt(status
);
1133 uintResult
= res
.getUInt(status
);
1134 if(U_SUCCESS(status
)){
1135 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1136 CONFIRM_EQ(uintResult
, (uint32_t)intResult
);
1137 CONFIRM_EQ(intResult
, 1);
1140 res
= theBundle
.get("onehundredtwentythree",status
);
1141 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1142 CONFIRM_EQ(res
.getType(), URES_INT
);
1143 intResult
=res
.getInt(status
);
1144 if(U_SUCCESS(status
)){
1145 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1146 CONFIRM_EQ(intResult
, 123);
1149 /* this tests if escapes are preserved or not */
1151 UnicodeString str
= theBundle
.getStringEx("testescape",status
);
1152 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1153 if(U_SUCCESS(status
)){
1154 u_charsToUChars(expect
,uExpect
,(int32_t)uprv_strlen(expect
)+1);
1155 if(str
.compare(uExpect
)!=0){
1156 errln("Did not get the expected string for testescape expected. Expected : "
1157 +UnicodeString(uExpect
)+ " Got: " + str
);
1161 /* test for jitterbug#1435 */
1163 UnicodeString str
= theBundle
.getStringEx("test_underscores",status
);
1164 expect
="test message ....";
1165 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1166 u_charsToUChars(expect
,uExpect
,(int32_t)uprv_strlen(expect
)+1);
1167 if(str
.compare(uExpect
)!=0){
1168 errln("Did not get the expected string for test_underscores.\n");
1176 NewResourceBundleTest::TestGetByFallback() {
1177 UErrorCode status
= U_ZERO_ERROR
;
1179 ResourceBundle
heRes(NULL
, "he", status
);
1181 heRes
.getWithFallback("calendar", status
).getWithFallback("islamic-civil", status
).getWithFallback("DateTime", status
);
1182 if(U_SUCCESS(status
)) {
1183 errln("he locale's Islamic-civil DateTime resource exists. How did it get here?\n");
1185 status
= U_ZERO_ERROR
;
1187 heRes
.getWithFallback("calendar", status
).getWithFallback("islamic-civil", status
).getWithFallback("eras", status
);
1188 if(U_FAILURE(status
)) {
1189 dataerrln("Didn't get Islamic Eras. I know they are there! - %s", u_errorName(status
));
1191 status
= U_ZERO_ERROR
;
1193 ResourceBundle
rootRes(NULL
, "root", status
);
1194 rootRes
.getWithFallback("calendar", status
).getWithFallback("islamic-civil", status
).getWithFallback("DateTime", status
);
1195 if(U_SUCCESS(status
)) {
1196 errln("Root's Islamic-civil's DateTime resource exists. How did it get here?\n");
1198 status
= U_ZERO_ERROR
;