1 /********************************************************************
2 * Copyright (c) 1997-2016, International Business Machines
3 * Corporation and others. All Rights Reserved.
4 ********************************************************************/
6 #include "unicode/utypes.h"
10 #include "unicode/unistr.h"
11 #include "unicode/resbund.h"
19 //***************************************************************************************
21 static const UChar kErrorUChars
[] = { 0x45, 0x52, 0x52, 0x4f, 0x52, 0 };
22 static const int32_t kErrorLength
= 5;
23 static const int32_t kERROR_COUNT
= -1234567;
25 //***************************************************************************************
35 //***************************************************************************************
37 #define CONFIRM_EQ(actual,expected) if ((expected)==(actual)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of " + (expected)); }
38 #define CONFIRM_GE(actual,expected) if ((actual)>=(expected)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x >= " + (expected)); }
39 #define CONFIRM_NE(actual,expected) if ((expected)!=(actual)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x != " + (expected)); }
41 #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)); }
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", 0, U_ZERO_ERROR
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} },
103 { "te", 0, U_ZERO_ERROR
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
104 { "te_IN", 0, U_ZERO_ERROR
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
105 { "te_NE", 0, U_USING_FALLBACK_WARNING
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
106 { "te_IN_NE", 0, U_USING_FALLBACK_WARNING
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
107 { "ne", 0, U_USING_DEFAULT_WARNING
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} }
110 static int32_t bundles_count
= UPRV_LENGTHOF(param
);
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 static 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 NewResourceBundleTest::NewResourceBundleTest()
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 NewResourceBundleTest::~NewResourceBundleTest()
172 if (param
[5].locale
) {
174 for (idx
= 0; idx
< UPRV_LENGTHOF(param
); idx
++) {
175 delete param
[idx
].locale
;
176 param
[idx
].locale
= NULL
;
181 void NewResourceBundleTest::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
= "TestIteration"; if (exec
) TestIteration(); break;
189 case 3: name
= "TestOtherAPI"; if(exec
) TestOtherAPI(); break;
190 case 4: name
= "TestNewTypes"; if(exec
) TestNewTypes(); break;
192 case 0: case 1: case 2: case 3: case 4: name
= "skip"; break;
195 case 5: name
= "TestGetByFallback"; if(exec
) TestGetByFallback(); break;
196 default: name
= ""; break; //needed to end loop
200 //***************************************************************************************
203 NewResourceBundleTest::TestResourceBundles()
205 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 NewResourceBundleTest::TestConstruction()
236 UErrorCode err
= U_ZERO_ERROR
;
237 Locale
locale("te", "IN");
239 const char* testdatapath
;
240 testdatapath
=loadTestData(err
);
243 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
247 /* Make sure that users using te_IN for the default locale don't get test failures. */
248 Locale originalDefault
;
249 if (Locale::getDefault() == Locale("te_IN")) {
250 Locale::setDefault(Locale("en_US"), err
);
253 ResourceBundle
test1((UnicodeString
)testdatapath
, err
);
254 ResourceBundle
test2(testdatapath
, locale
, err
);
256 UnicodeString result1
;
257 UnicodeString result2
;
259 result1
= test1
.getStringEx("string_in_Root_te_te_IN", err
);
260 result2
= test2
.getStringEx("string_in_Root_te_te_IN", err
);
261 if (U_FAILURE(err
)) {
262 errln("Something threw an error in TestConstruction()");
266 logln("for string_in_Root_te_te_IN, root.txt had " + result1
);
267 logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2
);
269 if (result1
!= "ROOT" || result2
!= "TE_IN") {
270 errln("Construction test failed; run verbose for more information");
273 const char* version1
;
274 const char* version2
;
276 version1
= test1
.getVersionNumber();
277 version2
= test2
.getVersionNumber();
279 char *versionID1
= new char[1 + strlen(U_ICU_VERSION
) + strlen(version1
)]; // + 1 for zero byte
280 char *versionID2
= new char[1 + strlen(U_ICU_VERSION
) + strlen(version2
)]; // + 1 for zero byte
282 strcpy(versionID1
, "44.0"); // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed.
284 strcpy(versionID2
, "55.0"); // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed.
286 logln(UnicodeString("getVersionNumber on default.txt returned ") + version1
+ UnicodeString(" Expect: " ) + versionID1
);
287 logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2
+ UnicodeString(" Expect: " ) + versionID2
);
289 if (strcmp(version1
, versionID1
) != 0) {
290 errln("getVersionNumber(version1) failed. %s != %s", version1
, versionID1
);
292 if (strcmp(version2
, versionID2
) != 0) {
293 errln("getVersionNumber(version2) failed. %s != %s", version2
, versionID2
);
298 /* Restore the default locale for the other tests. */
299 Locale::setDefault(originalDefault
, err
);
303 NewResourceBundleTest::TestIteration()
305 UErrorCode err
= U_ZERO_ERROR
;
306 const char* testdatapath
;
308 "string_in_Root_te_te_IN", "1",
309 "array_in_Root_te_te_IN", "5",
310 "array_2d_in_Root_te_te_IN", "4",
313 Locale
*locale
=new Locale("te_IN");
315 testdatapath
=loadTestData(err
);
318 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
322 ResourceBundle
test1(testdatapath
, *locale
, err
);
324 errln("Construction failed");
327 int32_t count
, row
=0, col
=0;
329 UnicodeString expected
;
330 UnicodeString
element("TE_IN");
331 UnicodeString action
;
334 for(i
=0; i
<UPRV_LENGTHOF(data
); i
=i
+2){
340 ResourceBundle bundle
= test1
.get(data
[i
], err
);
343 action
+=".getKey()";
345 CONFIRM_EQ((UnicodeString
)bundle
.getKey(), (UnicodeString
)data
[i
]);
349 while(bundle
.hasNext()){
351 action
+=".getNextString(err)";
353 UnicodeString got
=bundle
.getNextString(err
);
356 if(bundle
.getSize() > 1){
357 CONFIRM_EQ(bundle
.getType(), URES_ARRAY
);
358 expected
+=itoa(row
, buf
);
359 ResourceBundle rowbundle
=bundle
.get(row
, err
);
360 if(!U_FAILURE(err
) && rowbundle
.getSize()>1){
362 while(rowbundle
.hasNext()){
364 got
=rowbundle
.getNextString(err
);
366 expected
+=itoa(row
, buf
);
367 expected
+=itoa(col
, buf
);
369 CONFIRM_EQ(got
, expected
);
372 CONFIRM_EQ(col
, rowbundle
.getSize());
376 CONFIRM_EQ(bundle
.getType(), (int32_t)URES_STRING
);
379 CONFIRM_EQ(got
, expected
);
383 action
+=".getSize()";
384 CONFIRM_EQ(bundle
.getSize(), count
);
385 CONFIRM_EQ(count
, atoi(data
[i
+1]));
386 //after reaching the end
388 ResourceBundle errbundle
=bundle
.getNext(err
);
389 action
= "After reaching the end of the Iterator:- ";
391 action
+=".getNext()";
392 CONFIRM_NE(err
, (int32_t)U_ZERO_ERROR
);
393 CONFIRM_EQ(u_errorName(err
), u_errorName(U_INDEX_OUTOFBOUNDS_ERROR
));
396 bundle
.resetIterator();
397 /* The following code is causing a crash
403 errln("ERROR: getNext() throw an error");
410 // TODO: add operator== and != to ResourceBundle
412 equalRB(ResourceBundle
&a
, ResourceBundle
&b
) {
420 a
.getLocale()==b
.getLocale() &&
421 0==strcmp(a
.getName(), b
.getName()) &&
423 a
.getString(status
)==b
.getString(status
) :
425 a
.getInt(status
)==b
.getInt(status
) :
430 NewResourceBundleTest::TestOtherAPI(){
431 UErrorCode err
= U_ZERO_ERROR
;
432 const char* testdatapath
=loadTestData(err
);
433 UnicodeString tDataPathUS
= UnicodeString(testdatapath
, "");
437 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
441 /* Make sure that users using te_IN for the default locale don't get test failures. */
442 Locale originalDefault
;
443 if (Locale::getDefault() == Locale("te_IN")) {
444 Locale::setDefault(Locale("en_US"), err
);
447 Locale
*locale
=new Locale("te_IN");
449 ResourceBundle
test0(tDataPathUS
, *locale
, err
);
451 errln("Construction failed");
455 ResourceBundle
test1(testdatapath
, *locale
, err
);
457 errln("Construction failed");
461 logln("Testing getLocale()\n");
462 if(strcmp(test1
.getLocale().getName(), locale
->getName()) !=0 ){
463 errln("FAIL: ResourceBundle::getLocale() failed\n");
468 logln("Testing ResourceBundle(UErrorCode)\n");
469 ResourceBundle
defaultresource(err
);
470 ResourceBundle
explicitdefaultresource(NULL
, Locale::getDefault(), err
);
472 errcheckln(err
, "Construction of default resourcebundle failed - %s", u_errorName(err
));
475 // You can't compare the default locale to the resolved locale in the
476 // resource bundle due to aliasing, keywords in the default locale
477 // or the chance that the machine running these tests is using a locale
478 // that isn't available in ICU.
479 if(strcmp(defaultresource
.getLocale().getName(), explicitdefaultresource
.getLocale().getName()) != 0){
480 errln("Construction of default resourcebundle didn't take the defaultlocale. Expected %s Got %s err=%s\n",
481 explicitdefaultresource
.getLocale().getName(), defaultresource
.getLocale().getName(), u_errorName(err
));
485 ResourceBundle
copyRes(defaultresource
);
486 if(strcmp(copyRes
.getName(), defaultresource
.getName() ) !=0 ||
487 strcmp(test1
.getName(), defaultresource
.getName() ) ==0 ||
488 strcmp(copyRes
.getLocale().getName(), defaultresource
.getLocale().getName() ) !=0 ||
489 strcmp(test1
.getLocale().getName(), defaultresource
.getLocale().getName() ) ==0 )
491 errln("copy construction failed\n");
494 ResourceBundle defaultSub
= defaultresource
.get((int32_t)0, err
);
495 ResourceBundle
defSubCopy(defaultSub
);
496 if(strcmp(defSubCopy
.getName(), defaultSub
.getName() ) !=0 ||
497 strcmp(defSubCopy
.getLocale().getName(), defaultSub
.getLocale().getName() ) !=0 ){
498 errln("copy construction for subresource failed\n");
503 p
= defaultresource
.clone();
504 if(p
== &defaultresource
|| !equalRB(*p
, defaultresource
)) {
505 errln("ResourceBundle.clone() failed");
509 p
= defaultSub
.clone();
510 if(p
== &defaultSub
|| !equalRB(*p
, defaultSub
)) {
511 errln("2nd ResourceBundle.clone() failed");
516 copyRes
.getVersion(ver
);
518 logln("Version returned: [%d.%d.%d.%d]\n", ver
[0], ver
[1], ver
[2], ver
[3]);
520 logln("Testing C like UnicodeString APIs\n");
522 UResourceBundle
*testCAPI
= NULL
, *bundle
= NULL
, *rowbundle
= NULL
, *temp
= NULL
;
525 "string_in_Root_te_te_IN", "1",
526 "array_in_Root_te_te_IN", "5",
527 "array_2d_in_Root_te_te_IN", "4",
531 testCAPI
= ures_open(testdatapath
, "te_IN", &err
);
538 int32_t count
, row
=0, col
=0;
540 UnicodeString expected
;
541 UnicodeString
element("TE_IN");
542 UnicodeString action
;
545 for(i
=0; i
<UPRV_LENGTHOF(data
); i
=i
+2){
551 bundle
= ures_getByKey(testCAPI
, data
[i
], bundle
, &err
);
553 const char* key
= NULL
;
555 action
+=".getKey()";
557 CONFIRM_EQ((UnicodeString
)ures_getKey(bundle
), (UnicodeString
)data
[i
]);
561 while(ures_hasNext(bundle
)){
563 action
+=".getNextString(err)";
565 UnicodeString got
=ures_getNextUnicodeString(bundle
, &key
, &err
);
568 if(ures_getSize(bundle
) > 1){
569 CONFIRM_EQ(ures_getType(bundle
), URES_ARRAY
);
570 expected
+=itoa(row
, buf
);
571 rowbundle
=ures_getByIndex(bundle
, row
, rowbundle
, &err
);
572 if(!U_FAILURE(err
) && ures_getSize(rowbundle
)>1){
574 while(ures_hasNext(rowbundle
)){
576 got
=ures_getNextUnicodeString(rowbundle
, &key
, &err
);
577 temp
= ures_getByIndex(rowbundle
, col
, temp
, &err
);
578 UnicodeString bla
= ures_getUnicodeString(temp
, &err
);
579 UnicodeString bla2
= ures_getUnicodeStringByIndex(rowbundle
, col
, &err
);
581 expected
+=itoa(row
, buf
);
582 expected
+=itoa(col
, buf
);
584 CONFIRM_EQ(got
, expected
);
585 CONFIRM_EQ(bla
, expected
);
586 CONFIRM_EQ(bla2
, expected
);
589 CONFIRM_EQ(col
, ures_getSize(rowbundle
));
593 CONFIRM_EQ(ures_getType(bundle
), (int32_t)URES_STRING
);
596 CONFIRM_EQ(got
, expected
);
602 // Check that ures_getUnicodeString() & variants return a bogus string if failure.
603 // Same relevant code path whether the failure code is passed in
604 // or comes from a lookup error.
605 UErrorCode failure
= U_INTERNAL_PROGRAM_ERROR
;
606 assertTrue("ures_getUnicodeString(failure).isBogus()",
607 ures_getUnicodeString(testCAPI
, &failure
).isBogus());
608 assertTrue("ures_getNextUnicodeString(failure).isBogus()",
609 ures_getNextUnicodeString(testCAPI
, NULL
, &failure
).isBogus());
610 assertTrue("ures_getUnicodeStringByIndex(failure).isBogus()",
611 ures_getUnicodeStringByIndex(testCAPI
, 999, &failure
).isBogus());
612 assertTrue("ures_getUnicodeStringByKey(failure).isBogus()",
613 ures_getUnicodeStringByKey(testCAPI
, "bogus key", &failure
).isBogus());
616 ures_close(rowbundle
);
618 ures_close(testCAPI
);
620 errln("failed to open a resource bundle\n");
623 /* Restore the default locale for the other tests. */
624 Locale::setDefault(originalDefault
, err
);
630 //***************************************************************************************
633 NewResourceBundleTest::testTag(const char* frag
,
638 int32_t failOrig
= fail
;
640 // Make array from input params
642 UBool is_in
[] = { in_Root
, in_te
, in_te_IN
};
644 const char* NAME
[] = { "ROOT", "TE", "TE_IN" };
646 // Now try to load the desired items
649 UnicodeString action
;
651 int32_t i
,j
,row
,col
, actual_bundle
;
653 const char* testdatapath
;
655 UErrorCode status
= U_ZERO_ERROR
;
656 testdatapath
=loadTestData(status
);
657 if(U_FAILURE(status
))
659 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status
)));
663 for (i
=0; i
<bundles_count
; ++i
)
665 action
= "Constructor for ";
666 action
+= param
[i
].name
;
668 status
= U_ZERO_ERROR
;
669 ResourceBundle
theBundle( testdatapath
, *param
[i
].locale
, status
);
670 //ResourceBundle theBundle( "c:\\icu\\icu\\source\\test\\testdata\\testdata", *param[i].locale, status);
671 CONFIRM_UErrorCode(status
,param
[i
].expected_constructor_status
);
674 actual_bundle
= 0; /* ne -> default */
676 actual_bundle
= 1; /* te_NE -> te */
678 actual_bundle
= 2; /* te_IN_NE -> te_IN */
683 UErrorCode expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
684 for (j
=e_te_IN
; j
>=e_Root
; --j
)
686 if (is_in
[j
] && param
[i
].inherits
[j
])
688 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
689 expected_resource_status
= U_ZERO_ERROR
;
691 expected_resource_status
= U_USING_DEFAULT_WARNING
;
693 expected_resource_status
= U_USING_FALLBACK_WARNING
;
699 UErrorCode expected_status
;
702 for (j
=param
[i
].where
; j
>=0; --j
)
711 //--------------------------------------------------------------------------
714 uprv_strcpy(tag
, "string_");
715 uprv_strcat(tag
, frag
);
717 action
= param
[i
].name
;
718 action
+= ".getStringEx(";
723 status
= U_ZERO_ERROR
;
724 UnicodeString string
= theBundle
.getStringEx(tag
, status
);
725 if(U_FAILURE(status
)) {
726 string
.setTo(TRUE
, kErrorUChars
, kErrorLength
);
729 CONFIRM_UErrorCode(status
, expected_resource_status
);
731 UnicodeString
expected_string(kErrorUChars
);
732 if (U_SUCCESS(status
)) {
733 expected_string
= base
;
736 CONFIRM_EQ(string
, expected_string
);
738 //--------------------------------------------------------------------------
739 // array ResourceBundle using the key
741 uprv_strcpy(tag
, "array_");
742 uprv_strcat(tag
, frag
);
744 action
= param
[i
].name
;
749 int32_t count
= kERROR_COUNT
;
750 status
= U_ZERO_ERROR
;
751 ResourceBundle array
= theBundle
.get(tag
, status
);
752 CONFIRM_UErrorCode(status
,expected_resource_status
);
755 if (U_SUCCESS(status
))
757 //confirm the resource type is an array
758 UResType bundleType
=array
.getType();
759 CONFIRM_EQ(bundleType
, URES_ARRAY
);
761 count
=array
.getSize();
764 for (j
=0; j
<count
; ++j
)
767 expected_string
= base
;
768 expected_string
+= itoa(j
,buf
);
769 CONFIRM_EQ(array
.getNextString(status
),expected_string
);
775 CONFIRM_EQ(count
,kERROR_COUNT
);
776 // CONFIRM_EQ((int32_t)(unsigned long)array,(int32_t)0);
780 //--------------------------------------------------------------------------
781 // arrayItem ResourceBundle using the index
784 for (j
=0; j
<100; ++j
)
786 index
= count
? (randi(count
* 3) - count
) : (randi(200) - 100);
787 status
= U_ZERO_ERROR
;
788 string
= kErrorUChars
;
789 ResourceBundle array
= theBundle
.get(tag
, status
);
790 if(!U_FAILURE(status
)){
791 UnicodeString t
= array
.getStringEx(index
, status
);
792 if(!U_FAILURE(status
)) {
797 expected_status
= (index
>= 0 && index
< count
) ? expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
798 CONFIRM_UErrorCode(status
,expected_status
);
800 if (U_SUCCESS(status
)){
802 expected_string
= base
;
803 expected_string
+= itoa(index
,buf
);
805 expected_string
= kErrorUChars
;
807 CONFIRM_EQ(string
,expected_string
);
811 //--------------------------------------------------------------------------
814 uprv_strcpy(tag
, "array_2d_");
815 uprv_strcat(tag
, frag
);
817 action
= param
[i
].name
;
823 int32_t row_count
= kERROR_COUNT
, column_count
= kERROR_COUNT
;
824 status
= U_ZERO_ERROR
;
825 ResourceBundle array2d
=theBundle
.get(tag
, status
);
827 //const UnicodeString** array2d = theBundle.get2dArray(tag, row_count, column_count, status);
828 CONFIRM_UErrorCode(status
,expected_resource_status
);
830 if (U_SUCCESS(status
))
832 //confirm the resource type is an 2darray
833 UResType bundleType
=array2d
.getType();
834 CONFIRM_EQ(bundleType
, URES_ARRAY
);
836 row_count
=array2d
.getSize();
837 CONFIRM_GE(row_count
,1);
839 for(row
=0; row
<row_count
; ++row
){
840 ResourceBundle tablerow
=array2d
.get(row
, status
);
841 CONFIRM_UErrorCode(status
, expected_resource_status
);
842 if(U_SUCCESS(status
)){
843 //confirm the resourcetype of each table row is an array
844 UResType rowType
=tablerow
.getType();
845 CONFIRM_EQ(rowType
, URES_ARRAY
);
847 column_count
=tablerow
.getSize();
848 CONFIRM_GE(column_count
,1);
850 for (col
=0; j
<column_count
; ++j
) {
852 expected_string
= base
;
853 expected_string
+= itoa(row
,buf
);
854 expected_string
+= itoa(col
,buf
);
855 CONFIRM_EQ(tablerow
.getNextString(status
),expected_string
);
860 CONFIRM_EQ(row_count
,kERROR_COUNT
);
861 CONFIRM_EQ(column_count
,kERROR_COUNT
);
862 row_count
=column_count
=0;
868 //--------------------------------------------------------------------------
870 for (j
=0; j
<200; ++j
)
872 row
= row_count
? (randi(row_count
* 3) - row_count
) : (randi(200) - 100);
873 col
= column_count
? (randi(column_count
* 3) - column_count
) : (randi(200) - 100);
874 status
= U_ZERO_ERROR
;
875 string
= kErrorUChars
;
876 ResourceBundle array2d
=theBundle
.get(tag
, status
);
877 if(U_SUCCESS(status
)){
878 ResourceBundle tablerow
=array2d
.get(row
, status
);
879 if(U_SUCCESS(status
)) {
880 UnicodeString t
=tablerow
.getStringEx(col
, status
);
881 if(U_SUCCESS(status
)){
886 expected_status
= (row
>= 0 && row
< row_count
&& col
>= 0 && col
< column_count
) ?
887 expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
888 CONFIRM_UErrorCode(status
,expected_status
);
890 if (U_SUCCESS(status
)){
892 expected_string
= base
;
893 expected_string
+= itoa(row
,buf
);
894 expected_string
+= itoa(col
,buf
);
896 expected_string
= kErrorUChars
;
898 CONFIRM_EQ(string
,expected_string
);
902 //--------------------------------------------------------------------------
905 uprv_strcpy(tag
, "tagged_array_");
906 uprv_strcat(tag
, frag
);
908 action
= param
[i
].name
;
914 status
= U_ZERO_ERROR
;
916 ResourceBundle tags
=theBundle
.get(tag
, status
);
917 CONFIRM_UErrorCode(status
, expected_resource_status
);
919 if (U_SUCCESS(status
)) {
920 UResType bundleType
=tags
.getType();
921 CONFIRM_EQ(bundleType
, URES_TABLE
);
923 tag_count
=tags
.getSize();
924 CONFIRM_GE((int32_t)tag_count
, (int32_t)0);
926 for(index
=0; index
<tag_count
; index
++){
927 ResourceBundle tagelement
=tags
.get(index
, status
);
928 UnicodeString key
=tagelement
.getKey();
929 UnicodeString value
=tagelement
.getNextString(status
);
930 logln("tag = " + key
+ ", value = " + value
);
931 if(key
.startsWith("tag") && value
.startsWith(base
)){
939 for(index
=0; index
<tag_count
; index
++){
940 ResourceBundle tagelement
=tags
.get(index
, status
);
941 const char *tkey
=NULL
;
942 UnicodeString value
=tagelement
.getNextString(&tkey
, status
);
943 UnicodeString
key(tkey
);
944 logln("tag = " + key
+ ", value = " + value
);
945 if(value
.startsWith(base
)){
959 //--------------------------------------------------------------------------
962 action
= param
[i
].name
;
968 for (index
=-20; index
<20; ++index
)
971 status
= U_ZERO_ERROR
;
972 string
= kErrorUChars
;
974 uprv_strcpy(item_tag
, "tag");
975 uprv_strcat(item_tag
, itoa(index
,buf
));
976 ResourceBundle tags
=theBundle
.get(tag
, status
);
977 if(U_SUCCESS(status
)){
978 ResourceBundle tagelement
=tags
.get(item_tag
, status
);
979 if(!U_FAILURE(status
)){
980 UResType elementType
=tagelement
.getType();
981 CONFIRM_EQ(elementType
, (int32_t)URES_STRING
);
982 const char* key
=tagelement
.getKey();
983 CONFIRM_EQ((UnicodeString
)key
, (UnicodeString
)item_tag
);
984 UnicodeString t
=tagelement
.getString(status
);
985 if(!U_FAILURE(status
)){
990 CONFIRM_UErrorCode(status
,U_MISSING_RESOURCE_ERROR
);
993 if (status
!= U_MISSING_RESOURCE_ERROR
) {
995 expected_string
= base
;
996 expected_string
+= buf
;
997 CONFIRM_EQ(string
,expected_string
);
1003 CONFIRM_EQ(count
, tag_count
);
1006 return (UBool
)(failOrig
== fail
);
1010 NewResourceBundleTest::record_pass()
1015 NewResourceBundleTest::record_fail()
1023 NewResourceBundleTest::TestNewTypes() {
1025 const char* testdatapath
;
1026 UErrorCode status
= U_ZERO_ERROR
;
1027 uint8_t *binResult
= NULL
;
1030 int32_t intResult
= 0;
1031 uint32_t uintResult
= 0;
1032 UChar expected
[] = { 'a','b','c','\0','d','e','f' };
1033 const char* expect
="tab:\t cr:\r ff:\f newline:\n backslash:\\\\ quote=\\\' doubleQuote=\\\" singlequoutes=''";
1036 testdatapath
=loadTestData(status
);
1038 if(U_FAILURE(status
))
1040 dataerrln("Could not load testdata.dat %s \n",u_errorName(status
));
1044 ResourceBundle
theBundle(testdatapath
, "testtypes", status
);
1045 ResourceBundle
bundle(testdatapath
, Locale("te_IN"),status
);
1047 UnicodeString emptyStr
= theBundle
.getStringEx("emptystring", status
);
1048 if(emptyStr
.length() != 0) {
1049 logln("Empty string returned invalid value\n");
1052 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1054 /* This test reads the string "abc\u0000def" from the bundle */
1055 /* if everything is working correctly, the size of this string */
1056 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
1058 strcpy(action
, "getting and testing of string with embeded zero");
1059 ResourceBundle res
= theBundle
.get("zerotest", status
);
1060 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1061 CONFIRM_EQ(res
.getType(), URES_STRING
);
1062 UnicodeString zeroString
=res
.getString(status
);
1063 len
= zeroString
.length();
1064 if(U_SUCCESS(status
)){
1065 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1070 if(zeroString
[i
]!= expected
[i
]){
1071 logln("Output didnot match Expected: \\u%4X Got: \\u%4X", expected
[i
], zeroString
[i
]);
1075 strcpy(action
, "getting and testing of binary type");
1076 res
= theBundle
.get("binarytest", status
);
1077 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1078 CONFIRM_EQ(res
.getType(), URES_BINARY
);
1079 binResult
=(uint8_t*)res
.getBinary(len
, status
);
1080 if(U_SUCCESS(status
)){
1081 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1082 CONFIRM_EQ(len
, 15);
1083 for(i
= 0; i
<15; i
++) {
1084 CONFIRM_EQ(binResult
[i
], i
);
1088 strcpy(action
, "getting and testing of imported binary type");
1089 res
= theBundle
.get("importtest",status
);
1090 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1091 CONFIRM_EQ(res
.getType(), URES_BINARY
);
1092 binResult
=(uint8_t*)res
.getBinary(len
, status
);
1093 if(U_SUCCESS(status
)){
1094 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1095 CONFIRM_EQ(len
, 15);
1096 for(i
= 0; i
<15; i
++) {
1097 CONFIRM_EQ(binResult
[i
], i
);
1101 strcpy(action
, "getting and testing of integer types");
1102 res
= theBundle
.get("one", status
);
1103 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1104 CONFIRM_EQ(res
.getType(), URES_INT
);
1105 intResult
=res
.getInt(status
);
1106 uintResult
= res
.getUInt(status
);
1107 if(U_SUCCESS(status
)){
1108 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1109 CONFIRM_EQ(uintResult
, (uint32_t)intResult
);
1110 CONFIRM_EQ(intResult
, 1);
1113 strcpy(action
, "getting minusone");
1114 res
= theBundle
.get((const char*)"minusone", status
);
1115 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1116 CONFIRM_EQ(res
.getType(), URES_INT
);
1117 intResult
=res
.getInt(status
);
1118 uintResult
= res
.getUInt(status
);
1119 if(U_SUCCESS(status
)){
1120 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1121 CONFIRM_EQ(uintResult
, 0x0FFFFFFF); /* a 28 bit integer */
1122 CONFIRM_EQ(intResult
, -1);
1123 CONFIRM_NE(uintResult
, (uint32_t)intResult
);
1126 strcpy(action
, "getting plusone");
1127 res
= theBundle
.get("plusone",status
);
1128 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1129 CONFIRM_EQ(res
.getType(), URES_INT
);
1130 intResult
=res
.getInt(status
);
1131 uintResult
= res
.getUInt(status
);
1132 if(U_SUCCESS(status
)){
1133 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1134 CONFIRM_EQ(uintResult
, (uint32_t)intResult
);
1135 CONFIRM_EQ(intResult
, 1);
1138 res
= theBundle
.get("onehundredtwentythree",status
);
1139 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1140 CONFIRM_EQ(res
.getType(), URES_INT
);
1141 intResult
=res
.getInt(status
);
1142 if(U_SUCCESS(status
)){
1143 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1144 CONFIRM_EQ(intResult
, 123);
1147 /* this tests if escapes are preserved or not */
1149 UnicodeString str
= theBundle
.getStringEx("testescape",status
);
1150 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1151 if(U_SUCCESS(status
)){
1152 u_charsToUChars(expect
,uExpect
,(int32_t)uprv_strlen(expect
)+1);
1153 if(str
.compare(uExpect
)!=0){
1154 errln("Did not get the expected string for testescape expected. Expected : "
1155 +UnicodeString(uExpect
)+ " Got: " + str
);
1159 /* test for jitterbug#1435 */
1161 UnicodeString str
= theBundle
.getStringEx("test_underscores",status
);
1162 expect
="test message ....";
1163 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1164 u_charsToUChars(expect
,uExpect
,(int32_t)uprv_strlen(expect
)+1);
1165 if(str
.compare(uExpect
)!=0){
1166 errln("Did not get the expected string for test_underscores.\n");
1174 NewResourceBundleTest::TestGetByFallback() {
1175 UErrorCode status
= U_ZERO_ERROR
;
1177 ResourceBundle
heRes(NULL
, "he", status
);
1179 heRes
.getWithFallback("calendar", status
).getWithFallback("islamic-civil", status
).getWithFallback("DateTime", status
);
1180 if(U_SUCCESS(status
)) {
1181 errln("he locale's Islamic-civil DateTime resource exists. How did it get here?\n");
1183 status
= U_ZERO_ERROR
;
1185 heRes
.getWithFallback("calendar", status
).getWithFallback("islamic-civil", status
).getWithFallback("eras", status
);
1186 if(U_FAILURE(status
)) {
1187 dataerrln("Didn't get Islamic Eras. I know they are there! - %s", u_errorName(status
));
1189 status
= U_ZERO_ERROR
;
1191 ResourceBundle
rootRes(NULL
, "root", status
);
1192 rootRes
.getWithFallback("calendar", status
).getWithFallback("islamic-civil", status
).getWithFallback("DateTime", status
);
1193 if(U_SUCCESS(status
)) {
1194 errln("Root's Islamic-civil's DateTime resource exists. How did it get here?\n");
1196 status
= U_ZERO_ERROR
;