1 /********************************************************************
2 * Copyright (c) 1997-2008, International Business Machines
3 * Corporation and others. All Rights Reserved.
4 ********************************************************************/
6 #include "unicode/utypes.h"
9 #include "unicode/unistr.h"
10 #include "unicode/resbund.h"
18 //***************************************************************************************
20 static const UChar kErrorUChars
[] = { 0x45, 0x52, 0x52, 0x4f, 0x52, 0 };
21 static const int32_t kErrorLength
= 5;
22 static const int32_t kERROR_COUNT
= -1234567;
24 //***************************************************************************************
34 //***************************************************************************************
36 #define CONFIRM_EQ(actual,expected) if ((expected)==(actual)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of " + (expected)); }
37 #define CONFIRM_GE(actual,expected) if ((actual)>=(expected)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x >= " + (expected)); }
38 #define CONFIRM_NE(actual,expected) if ((expected)!=(actual)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x != " + (expected)); }
40 #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)); }
42 //***************************************************************************************
45 * Convert an integer, positive or negative, to a character string radix 10.
48 itoa(int32_t i
, char* buf
)
59 // Output digits in reverse order
63 *p
++ = (char)('0' + (i
% 10));
82 //***************************************************************************************
84 // Array of our test objects
90 UErrorCode expected_constructor_status
;
92 UBool like
[e_Where_count
];
93 UBool inherits
[e_Where_count
];
98 // "IN" means inherits
99 // "NE" or "ne" means "does not exist"
101 { "root", 0, U_ZERO_ERROR
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} },
102 { "te", 0, U_ZERO_ERROR
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
103 { "te_IN", 0, U_ZERO_ERROR
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
104 { "te_NE", 0, U_USING_FALLBACK_WARNING
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
105 { "te_IN_NE", 0, U_USING_FALLBACK_WARNING
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
106 { "ne", 0, U_USING_DEFAULT_WARNING
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} }
109 static int32_t bundles_count
= sizeof(param
) / sizeof(param
[0]);
111 //***************************************************************************************
114 * Return a random unsigned long l where 0N <= l <= ULONG_MAX.
120 static UBool initialized
= FALSE
;
123 srand((unsigned)time(NULL
));
126 // Assume rand has at least 12 bits of precision
128 for (uint32_t i
=0; i
<sizeof(l
); ++i
)
129 ((char*)&l
)[i
] = (char)((rand() & 0x0FF0) >> 4);
134 * Return a random double x where 0.0 <= x < 1.0.
139 return (double)(randul() / ULONG_MAX
);
143 * Return a random integer i where 0 <= i < n.
145 static int32_t randi(int32_t n
)
147 return (int32_t)(randd() * n
);
150 //***************************************************************************************
153 Don't use more than one of these at a time because of the Locale names
155 NewResourceBundleTest::NewResourceBundleTest()
159 if (param
[5].locale
== NULL
) {
160 param
[0].locale
= new Locale("root");
161 param
[1].locale
= new Locale("te");
162 param
[2].locale
= new Locale("te", "IN");
163 param
[3].locale
= new Locale("te", "NE");
164 param
[4].locale
= new Locale("te", "IN", "NE");
165 param
[5].locale
= new Locale("ne");
169 NewResourceBundleTest::~NewResourceBundleTest()
171 if (param
[5].locale
) {
173 for (idx
= 0; idx
< (int)(sizeof(param
)/sizeof(param
[0])); idx
++) {
174 delete param
[idx
].locale
;
175 param
[idx
].locale
= NULL
;
180 void NewResourceBundleTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
182 if (exec
) logln("TestSuite ResourceBundleTest: ");
184 case 0: name
= "TestResourceBundles"; if (exec
) TestResourceBundles(); break;
185 case 1: name
= "TestConstruction"; if (exec
) TestConstruction(); break;
186 case 2: name
= "TestIteration"; if (exec
) TestIteration(); break;
187 case 3: name
= "TestOtherAPI"; if(exec
) TestOtherAPI(); break;
188 case 4: name
= "TestNewTypes"; if(exec
) TestNewTypes(); break;
189 case 5: name
= "TestGetByFallback"; if(exec
) TestGetByFallback(); break;
190 default: name
= ""; break; //needed to end loop
194 //***************************************************************************************
197 NewResourceBundleTest::TestResourceBundles()
199 UErrorCode status
= U_ZERO_ERROR
;
200 loadTestData(status
);
201 if(U_FAILURE(status
))
203 dataerrln("[DATA] Could not load testdata.dat %s " + UnicodeString(u_errorName(status
)));
207 /* Make sure that users using te_IN for the default locale don't get test failures. */
208 Locale originalDefault
;
209 if (Locale::getDefault() == Locale("te_IN")) {
210 Locale::setDefault(Locale("en_US"), status
);
213 testTag("only_in_Root", TRUE
, FALSE
, FALSE
);
214 testTag("only_in_te", FALSE
, TRUE
, FALSE
);
215 testTag("only_in_te_IN", FALSE
, FALSE
, TRUE
);
216 testTag("in_Root_te", TRUE
, TRUE
, FALSE
);
217 testTag("in_Root_te_te_IN", TRUE
, TRUE
, TRUE
);
218 testTag("in_Root_te_IN", TRUE
, FALSE
, TRUE
);
219 testTag("in_te_te_IN", FALSE
, TRUE
, TRUE
);
220 testTag("nonexistent", FALSE
, FALSE
, FALSE
);
221 logln("Passed: %d\nFailed: %d", pass
, fail
);
223 /* Restore the default locale for the other tests. */
224 Locale::setDefault(originalDefault
, status
);
228 NewResourceBundleTest::TestConstruction()
230 UErrorCode err
= U_ZERO_ERROR
;
231 Locale
locale("te", "IN");
233 const char* testdatapath
;
234 testdatapath
=loadTestData(err
);
237 dataerrln("[DATA] Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
241 /* Make sure that users using te_IN for the default locale don't get test failures. */
242 Locale originalDefault
;
243 if (Locale::getDefault() == Locale("te_IN")) {
244 Locale::setDefault(Locale("en_US"), err
);
247 ResourceBundle
test1((UnicodeString
)testdatapath
, err
);
248 ResourceBundle
test2(testdatapath
, locale
, err
);
250 UnicodeString result1
;
251 UnicodeString result2
;
253 result1
= test1
.getStringEx("string_in_Root_te_te_IN", err
);
254 result2
= test2
.getStringEx("string_in_Root_te_te_IN", err
);
255 if (U_FAILURE(err
)) {
256 errln("Something threw an error in TestConstruction()");
260 logln("for string_in_Root_te_te_IN, root.txt had " + result1
);
261 logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2
);
263 if (result1
!= "ROOT" || result2
!= "TE_IN") {
264 errln("Construction test failed; run verbose for more information");
267 const char* version1
;
268 const char* version2
;
270 version1
= test1
.getVersionNumber();
271 version2
= test2
.getVersionNumber();
273 char *versionID1
= new char[1 + strlen(U_ICU_VERSION
) + strlen(version1
)]; // + 1 for zero byte
274 char *versionID2
= new char[1 + strlen(U_ICU_VERSION
) + strlen(version2
)]; // + 1 for zero byte
276 strcpy(versionID1
, "44.0"); // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed.
278 strcpy(versionID2
, "55.0"); // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed.
280 logln(UnicodeString("getVersionNumber on default.txt returned ") + version1
+ UnicodeString(" Expect: " ) + versionID1
);
281 logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2
+ UnicodeString(" Expect: " ) + versionID2
);
283 if (strcmp(version1
, versionID1
) != 0) {
284 errln("getVersionNumber(version1) failed. %s != %s", version1
, versionID1
);
286 if (strcmp(version2
, versionID2
) != 0) {
287 errln("getVersionNumber(version2) failed. %s != %s", version2
, versionID2
);
292 /* Restore the default locale for the other tests. */
293 Locale::setDefault(originalDefault
, err
);
297 NewResourceBundleTest::TestIteration()
299 UErrorCode err
= U_ZERO_ERROR
;
300 const char* testdatapath
;
302 "string_in_Root_te_te_IN", "1",
303 "array_in_Root_te_te_IN", "5",
304 "array_2d_in_Root_te_te_IN", "4",
307 Locale
*locale
=new Locale("te_IN");
309 testdatapath
=loadTestData(err
);
312 dataerrln("[DATA] Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
316 ResourceBundle
test1(testdatapath
, *locale
, err
);
318 errln("Construction failed");
321 int32_t count
, row
=0, col
=0;
323 UnicodeString expected
;
324 UnicodeString
element("TE_IN");
325 UnicodeString action
;
328 for(i
=0; i
<sizeof(data
)/sizeof(data
[0]); i
=i
+2){
334 ResourceBundle bundle
= test1
.get(data
[i
], err
);
337 action
+=".getKey()";
339 CONFIRM_EQ((UnicodeString
)bundle
.getKey(), (UnicodeString
)data
[i
]);
343 while(bundle
.hasNext()){
345 action
+=".getNextString(err)";
347 UnicodeString got
=bundle
.getNextString(err
);
350 if(bundle
.getSize() > 1){
351 CONFIRM_EQ(bundle
.getType(), URES_ARRAY
);
352 expected
+=itoa(row
, buf
);
353 ResourceBundle rowbundle
=bundle
.get(row
, err
);
354 if(!U_FAILURE(err
) && rowbundle
.getSize()>1){
356 while(rowbundle
.hasNext()){
358 got
=rowbundle
.getNextString(err
);
360 expected
+=itoa(row
, buf
);
361 expected
+=itoa(col
, buf
);
363 CONFIRM_EQ(got
, expected
);
366 CONFIRM_EQ(col
, rowbundle
.getSize());
370 CONFIRM_EQ(bundle
.getType(), (int32_t)URES_STRING
);
373 CONFIRM_EQ(got
, expected
);
377 action
+=".getSize()";
378 CONFIRM_EQ(bundle
.getSize(), count
);
379 CONFIRM_EQ(count
, atoi(data
[i
+1]));
380 //after reaching the end
382 ResourceBundle errbundle
=bundle
.getNext(err
);
383 action
= "After reaching the end of the Iterator:- ";
385 action
+=".getNext()";
386 CONFIRM_NE(err
, (int32_t)U_ZERO_ERROR
);
387 CONFIRM_EQ(u_errorName(err
), u_errorName(U_INDEX_OUTOFBOUNDS_ERROR
));
390 bundle
.resetIterator();
391 /* The following code is causing a crash
397 errln("ERROR: getNext() throw an error");
404 // TODO: add operator== and != to ResourceBundle
406 equalRB(ResourceBundle
&a
, ResourceBundle
&b
) {
414 a
.getLocale()==b
.getLocale() &&
415 0==strcmp(a
.getName(), b
.getName()) &&
417 a
.getString(status
)==b
.getString(status
) :
419 a
.getInt(status
)==b
.getInt(status
) :
424 NewResourceBundleTest::TestOtherAPI(){
425 UErrorCode err
= U_ZERO_ERROR
;
426 const char* testdatapath
=loadTestData(err
);
427 UnicodeString tDataPathUS
= UnicodeString(testdatapath
, "");
431 dataerrln("[DATA] Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
435 /* Make sure that users using te_IN for the default locale don't get test failures. */
436 Locale originalDefault
;
437 if (Locale::getDefault() == Locale("te_IN")) {
438 Locale::setDefault(Locale("en_US"), err
);
441 Locale
*locale
=new Locale("te_IN");
443 ResourceBundle
test0(tDataPathUS
, *locale
, err
);
445 errln("Construction failed");
449 ResourceBundle
test1(testdatapath
, *locale
, err
);
451 errln("Construction failed");
455 logln("Testing getLocale()\n");
456 if(strcmp(test1
.getLocale().getName(), locale
->getName()) !=0 ){
457 errln("FAIL: ResourceBundle::getLocale() failed\n");
462 logln("Testing ResourceBundle(UErrorCode)\n");
463 ResourceBundle
defaultresource(err
);
464 ResourceBundle
explicitdefaultresource(NULL
, Locale::getDefault(), err
);
466 errln("Construction of default resourcebundle failed");
469 // You can't compare the default locale to the resolved locale in the
470 // resource bundle due to aliasing, keywords in the default locale
471 // or the chance that the machine running these tests is using a locale
472 // that isn't available in ICU.
473 if(strcmp(defaultresource
.getLocale().getName(), explicitdefaultresource
.getLocale().getName()) != 0){
474 errln("Construction of default resourcebundle didn't take the defaultlocale. Expected %s Got %s err=%s\n",
475 explicitdefaultresource
.getLocale().getName(), defaultresource
.getLocale().getName(), u_errorName(err
));
479 ResourceBundle
copyRes(defaultresource
);
480 if(strcmp(copyRes
.getName(), defaultresource
.getName() ) !=0 ||
481 strcmp(test1
.getName(), defaultresource
.getName() ) ==0 ||
482 strcmp(copyRes
.getLocale().getName(), defaultresource
.getLocale().getName() ) !=0 ||
483 strcmp(test1
.getLocale().getName(), defaultresource
.getLocale().getName() ) ==0 )
485 errln("copy construction failed\n");
488 ResourceBundle defaultSub
= defaultresource
.get((int32_t)0, err
);
489 ResourceBundle
defSubCopy(defaultSub
);
490 if(strcmp(defSubCopy
.getName(), defaultSub
.getName() ) !=0 ||
491 strcmp(defSubCopy
.getLocale().getName(), defaultSub
.getLocale().getName() ) !=0 ){
492 errln("copy construction for subresource failed\n");
497 p
= defaultresource
.clone();
498 if(p
== &defaultresource
|| !equalRB(*p
, defaultresource
)) {
499 errln("ResourceBundle.clone() failed");
503 p
= defaultSub
.clone();
504 if(p
== &defaultSub
|| !equalRB(*p
, defaultSub
)) {
505 errln("2nd ResourceBundle.clone() failed");
510 copyRes
.getVersion(ver
);
512 logln("Version returned: [%d.%d.%d.%d]\n", ver
[0], ver
[1], ver
[2], ver
[3]);
514 logln("Testing C like UnicodeString APIs\n");
516 UResourceBundle
*testCAPI
= NULL
, *bundle
= NULL
, *rowbundle
= NULL
, *temp
= NULL
;
519 "string_in_Root_te_te_IN", "1",
520 "array_in_Root_te_te_IN", "5",
521 "array_2d_in_Root_te_te_IN", "4",
525 testCAPI
= ures_open(testdatapath
, "te_IN", &err
);
532 int32_t count
, row
=0, col
=0;
534 UnicodeString expected
;
535 UnicodeString
element("TE_IN");
536 UnicodeString action
;
539 for(i
=0; i
<sizeof(data
)/sizeof(data
[0]); i
=i
+2){
545 bundle
= ures_getByKey(testCAPI
, data
[i
], bundle
, &err
);
547 const char* key
= NULL
;
549 action
+=".getKey()";
551 CONFIRM_EQ((UnicodeString
)ures_getKey(bundle
), (UnicodeString
)data
[i
]);
555 while(ures_hasNext(bundle
)){
557 action
+=".getNextString(err)";
559 UnicodeString got
=ures_getNextUnicodeString(bundle
, &key
, &err
);
562 if(ures_getSize(bundle
) > 1){
563 CONFIRM_EQ(ures_getType(bundle
), URES_ARRAY
);
564 expected
+=itoa(row
, buf
);
565 rowbundle
=ures_getByIndex(bundle
, row
, rowbundle
, &err
);
566 if(!U_FAILURE(err
) && ures_getSize(rowbundle
)>1){
568 while(ures_hasNext(rowbundle
)){
570 got
=ures_getNextUnicodeString(rowbundle
, &key
, &err
);
571 temp
= ures_getByIndex(rowbundle
, col
, temp
, &err
);
572 UnicodeString bla
= ures_getUnicodeString(temp
, &err
);
573 UnicodeString bla2
= ures_getUnicodeStringByIndex(rowbundle
, col
, &err
);
575 expected
+=itoa(row
, buf
);
576 expected
+=itoa(col
, buf
);
578 CONFIRM_EQ(got
, expected
);
579 CONFIRM_EQ(bla
, expected
);
580 CONFIRM_EQ(bla2
, expected
);
583 CONFIRM_EQ(col
, ures_getSize(rowbundle
));
587 CONFIRM_EQ(ures_getType(bundle
), (int32_t)URES_STRING
);
590 CONFIRM_EQ(got
, expected
);
596 ures_close(rowbundle
);
598 ures_close(testCAPI
);
600 errln("failed to open a resource bundle\n");
603 /* Restore the default locale for the other tests. */
604 Locale::setDefault(originalDefault
, err
);
610 //***************************************************************************************
613 NewResourceBundleTest::testTag(const char* frag
,
618 int32_t failOrig
= fail
;
620 // Make array from input params
622 UBool is_in
[] = { in_Root
, in_te
, in_te_IN
};
624 const char* NAME
[] = { "ROOT", "TE", "TE_IN" };
626 // Now try to load the desired items
629 UnicodeString action
;
631 int32_t i
,j
,row
,col
, actual_bundle
;
633 const char* testdatapath
;
635 UErrorCode status
= U_ZERO_ERROR
;
636 testdatapath
=loadTestData(status
);
637 if(U_FAILURE(status
))
639 dataerrln("[DATA] Could not load testdata.dat %s " + UnicodeString(u_errorName(status
)));
643 for (i
=0; i
<bundles_count
; ++i
)
645 action
= "Constructor for ";
646 action
+= param
[i
].name
;
648 status
= U_ZERO_ERROR
;
649 ResourceBundle
theBundle( testdatapath
, *param
[i
].locale
, status
);
650 //ResourceBundle theBundle( "c:\\icu\\icu\\source\\test\\testdata\\testdata", *param[i].locale, status);
651 CONFIRM_UErrorCode(status
,param
[i
].expected_constructor_status
);
654 actual_bundle
= 0; /* ne -> default */
656 actual_bundle
= 1; /* te_NE -> te */
658 actual_bundle
= 2; /* te_IN_NE -> te_IN */
663 UErrorCode expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
664 for (j
=e_te_IN
; j
>=e_Root
; --j
)
666 if (is_in
[j
] && param
[i
].inherits
[j
])
668 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
669 expected_resource_status
= U_ZERO_ERROR
;
671 expected_resource_status
= U_USING_DEFAULT_WARNING
;
673 expected_resource_status
= U_USING_FALLBACK_WARNING
;
679 UErrorCode expected_status
;
682 for (j
=param
[i
].where
; j
>=0; --j
)
691 //--------------------------------------------------------------------------
694 uprv_strcpy(tag
, "string_");
695 uprv_strcat(tag
, frag
);
697 action
= param
[i
].name
;
698 action
+= ".getStringEx(";
703 status
= U_ZERO_ERROR
;
704 UnicodeString string
= theBundle
.getStringEx(tag
, status
);
705 if(U_FAILURE(status
)) {
706 string
.setTo(TRUE
, kErrorUChars
, kErrorLength
);
709 CONFIRM_UErrorCode(status
, expected_resource_status
);
711 UnicodeString
expected_string(kErrorUChars
);
712 if (U_SUCCESS(status
)) {
713 expected_string
= base
;
716 CONFIRM_EQ(string
, expected_string
);
718 //--------------------------------------------------------------------------
719 // array ResourceBundle using the key
721 uprv_strcpy(tag
, "array_");
722 uprv_strcat(tag
, frag
);
724 action
= param
[i
].name
;
729 int32_t count
= kERROR_COUNT
;
730 status
= U_ZERO_ERROR
;
731 ResourceBundle array
= theBundle
.get(tag
, status
);
732 CONFIRM_UErrorCode(status
,expected_resource_status
);
735 if (U_SUCCESS(status
))
737 //confirm the resource type is an array
738 UResType bundleType
=array
.getType();
739 CONFIRM_EQ(bundleType
, URES_ARRAY
);
741 count
=array
.getSize();
744 for (j
=0; j
<count
; ++j
)
747 expected_string
= base
;
748 expected_string
+= itoa(j
,buf
);
749 CONFIRM_EQ(array
.getNextString(status
),expected_string
);
755 CONFIRM_EQ(count
,kERROR_COUNT
);
756 // CONFIRM_EQ((int32_t)(unsigned long)array,(int32_t)0);
760 //--------------------------------------------------------------------------
761 // arrayItem ResourceBundle using the index
764 for (j
=0; j
<100; ++j
)
766 index
= count
? (randi(count
* 3) - count
) : (randi(200) - 100);
767 status
= U_ZERO_ERROR
;
768 string
= kErrorUChars
;
769 ResourceBundle array
= theBundle
.get(tag
, status
);
770 if(!U_FAILURE(status
)){
771 UnicodeString t
= array
.getStringEx(index
, status
);
772 if(!U_FAILURE(status
)) {
777 expected_status
= (index
>= 0 && index
< count
) ? expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
778 CONFIRM_UErrorCode(status
,expected_status
);
780 if (U_SUCCESS(status
)){
782 expected_string
= base
;
783 expected_string
+= itoa(index
,buf
);
785 expected_string
= kErrorUChars
;
787 CONFIRM_EQ(string
,expected_string
);
791 //--------------------------------------------------------------------------
794 uprv_strcpy(tag
, "array_2d_");
795 uprv_strcat(tag
, frag
);
797 action
= param
[i
].name
;
803 int32_t row_count
= kERROR_COUNT
, column_count
= kERROR_COUNT
;
804 status
= U_ZERO_ERROR
;
805 ResourceBundle array2d
=theBundle
.get(tag
, status
);
807 //const UnicodeString** array2d = theBundle.get2dArray(tag, row_count, column_count, status);
808 CONFIRM_UErrorCode(status
,expected_resource_status
);
810 if (U_SUCCESS(status
))
812 //confirm the resource type is an 2darray
813 UResType bundleType
=array2d
.getType();
814 CONFIRM_EQ(bundleType
, URES_ARRAY
);
816 row_count
=array2d
.getSize();
817 CONFIRM_GE(row_count
,1);
819 for(row
=0; row
<row_count
; ++row
){
820 ResourceBundle tablerow
=array2d
.get(row
, status
);
821 CONFIRM_UErrorCode(status
, expected_resource_status
);
822 if(U_SUCCESS(status
)){
823 //confirm the resourcetype of each table row is an array
824 UResType rowType
=tablerow
.getType();
825 CONFIRM_EQ(rowType
, URES_ARRAY
);
827 column_count
=tablerow
.getSize();
828 CONFIRM_GE(column_count
,1);
830 for (col
=0; j
<column_count
; ++j
) {
832 expected_string
= base
;
833 expected_string
+= itoa(row
,buf
);
834 expected_string
+= itoa(col
,buf
);
835 CONFIRM_EQ(tablerow
.getNextString(status
),expected_string
);
840 CONFIRM_EQ(row_count
,kERROR_COUNT
);
841 CONFIRM_EQ(column_count
,kERROR_COUNT
);
842 row_count
=column_count
=0;
848 //--------------------------------------------------------------------------
850 for (j
=0; j
<200; ++j
)
852 row
= row_count
? (randi(row_count
* 3) - row_count
) : (randi(200) - 100);
853 col
= column_count
? (randi(column_count
* 3) - column_count
) : (randi(200) - 100);
854 status
= U_ZERO_ERROR
;
855 string
= kErrorUChars
;
856 ResourceBundle array2d
=theBundle
.get(tag
, status
);
857 if(U_SUCCESS(status
)){
858 ResourceBundle tablerow
=array2d
.get(row
, status
);
859 if(U_SUCCESS(status
)) {
860 UnicodeString t
=tablerow
.getStringEx(col
, status
);
861 if(U_SUCCESS(status
)){
866 expected_status
= (row
>= 0 && row
< row_count
&& col
>= 0 && col
< column_count
) ?
867 expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
868 CONFIRM_UErrorCode(status
,expected_status
);
870 if (U_SUCCESS(status
)){
872 expected_string
= base
;
873 expected_string
+= itoa(row
,buf
);
874 expected_string
+= itoa(col
,buf
);
876 expected_string
= kErrorUChars
;
878 CONFIRM_EQ(string
,expected_string
);
882 //--------------------------------------------------------------------------
885 uprv_strcpy(tag
, "tagged_array_");
886 uprv_strcat(tag
, frag
);
888 action
= param
[i
].name
;
894 status
= U_ZERO_ERROR
;
896 ResourceBundle tags
=theBundle
.get(tag
, status
);
897 CONFIRM_UErrorCode(status
, expected_resource_status
);
899 if (U_SUCCESS(status
)) {
900 UResType bundleType
=tags
.getType();
901 CONFIRM_EQ(bundleType
, URES_TABLE
);
903 tag_count
=tags
.getSize();
904 CONFIRM_GE((int32_t)tag_count
, (int32_t)0);
906 for(index
=0; index
<tag_count
; index
++){
907 ResourceBundle tagelement
=tags
.get(index
, status
);
908 UnicodeString key
=tagelement
.getKey();
909 UnicodeString value
=tagelement
.getNextString(status
);
910 logln("tag = " + key
+ ", value = " + value
);
911 if(key
.startsWith("tag") && value
.startsWith(base
)){
919 for(index
=0; index
<tag_count
; index
++){
920 ResourceBundle tagelement
=tags
.get(index
, status
);
921 const char *tkey
=NULL
;
922 UnicodeString value
=tagelement
.getNextString(&tkey
, status
);
923 UnicodeString
key(tkey
);
924 logln("tag = " + key
+ ", value = " + value
);
925 if(value
.startsWith(base
)){
939 //--------------------------------------------------------------------------
942 action
= param
[i
].name
;
948 for (index
=-20; index
<20; ++index
)
951 status
= U_ZERO_ERROR
;
952 string
= kErrorUChars
;
954 uprv_strcpy(item_tag
, "tag");
955 uprv_strcat(item_tag
, itoa(index
,buf
));
956 ResourceBundle tags
=theBundle
.get(tag
, status
);
957 if(U_SUCCESS(status
)){
958 ResourceBundle tagelement
=tags
.get(item_tag
, status
);
959 if(!U_FAILURE(status
)){
960 UResType elementType
=tagelement
.getType();
961 CONFIRM_EQ(elementType
, (int32_t)URES_STRING
);
962 const char* key
=tagelement
.getKey();
963 CONFIRM_EQ((UnicodeString
)key
, (UnicodeString
)item_tag
);
964 UnicodeString t
=tagelement
.getString(status
);
965 if(!U_FAILURE(status
)){
970 CONFIRM_UErrorCode(status
,U_MISSING_RESOURCE_ERROR
);
973 if (status
!= U_MISSING_RESOURCE_ERROR
) {
975 expected_string
= base
;
976 expected_string
+= buf
;
977 CONFIRM_EQ(string
,expected_string
);
983 CONFIRM_EQ(count
, tag_count
);
986 return (UBool
)(failOrig
== fail
);
990 NewResourceBundleTest::record_pass()
995 NewResourceBundleTest::record_fail()
1003 NewResourceBundleTest::TestNewTypes() {
1005 const char* testdatapath
;
1006 UErrorCode status
= U_ZERO_ERROR
;
1007 uint8_t *binResult
= NULL
;
1010 int32_t intResult
= 0;
1011 uint32_t uintResult
= 0;
1012 UChar expected
[] = { 'a','b','c','\0','d','e','f' };
1013 const char* expect
="tab:\t cr:\r ff:\f newline:\n backslash:\\\\ quote=\\\' doubleQuote=\\\" singlequoutes=''";
1016 testdatapath
=loadTestData(status
);
1018 if(U_FAILURE(status
))
1020 dataerrln("[DATA] Could not load testdata.dat %s \n",u_errorName(status
));
1024 ResourceBundle
theBundle(testdatapath
, "testtypes", status
);
1025 ResourceBundle
bundle(testdatapath
, Locale("te_IN"),status
);
1027 UnicodeString emptyStr
= theBundle
.getStringEx("emptystring", status
);
1028 if(!emptyStr
.length()==0) {
1029 logln("Empty string returned invalid value\n");
1032 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1034 /* This test reads the string "abc\u0000def" from the bundle */
1035 /* if everything is working correctly, the size of this string */
1036 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
1038 strcpy(action
, "getting and testing of string with embeded zero");
1039 ResourceBundle res
= theBundle
.get("zerotest", status
);
1040 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1041 CONFIRM_EQ(res
.getType(), URES_STRING
);
1042 UnicodeString zeroString
=res
.getString(status
);
1043 len
= zeroString
.length();
1044 if(U_SUCCESS(status
)){
1045 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1050 if(zeroString
[i
]!= expected
[i
]){
1051 logln("Output didnot match Expected: \\u%4X Got: \\u%4X", expected
[i
], zeroString
[i
]);
1055 strcpy(action
, "getting and testing of binary type");
1056 res
= theBundle
.get("binarytest", status
);
1057 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1058 CONFIRM_EQ(res
.getType(), URES_BINARY
);
1059 binResult
=(uint8_t*)res
.getBinary(len
, status
);
1060 if(U_SUCCESS(status
)){
1061 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1062 CONFIRM_EQ(len
, 15);
1063 for(i
= 0; i
<15; i
++) {
1064 CONFIRM_EQ(binResult
[i
], i
);
1068 strcpy(action
, "getting and testing of imported binary type");
1069 res
= theBundle
.get("importtest",status
);
1070 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1071 CONFIRM_EQ(res
.getType(), URES_BINARY
);
1072 binResult
=(uint8_t*)res
.getBinary(len
, status
);
1073 if(U_SUCCESS(status
)){
1074 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1075 CONFIRM_EQ(len
, 15);
1076 for(i
= 0; i
<15; i
++) {
1077 CONFIRM_EQ(binResult
[i
], i
);
1081 strcpy(action
, "getting and testing of integer types");
1082 res
= theBundle
.get("one", status
);
1083 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1084 CONFIRM_EQ(res
.getType(), URES_INT
);
1085 intResult
=res
.getInt(status
);
1086 uintResult
= res
.getUInt(status
);
1087 if(U_SUCCESS(status
)){
1088 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1089 CONFIRM_EQ(uintResult
, (uint32_t)intResult
);
1090 CONFIRM_EQ(intResult
, 1);
1093 strcpy(action
, "getting minusone");
1094 res
= theBundle
.get((const char*)"minusone", status
);
1095 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1096 CONFIRM_EQ(res
.getType(), URES_INT
);
1097 intResult
=res
.getInt(status
);
1098 uintResult
= res
.getUInt(status
);
1099 if(U_SUCCESS(status
)){
1100 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1101 CONFIRM_EQ(uintResult
, 0x0FFFFFFF); /* a 28 bit integer */
1102 CONFIRM_EQ(intResult
, -1);
1103 CONFIRM_NE(uintResult
, (uint32_t)intResult
);
1106 strcpy(action
, "getting plusone");
1107 res
= theBundle
.get("plusone",status
);
1108 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1109 CONFIRM_EQ(res
.getType(), URES_INT
);
1110 intResult
=res
.getInt(status
);
1111 uintResult
= res
.getUInt(status
);
1112 if(U_SUCCESS(status
)){
1113 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1114 CONFIRM_EQ(uintResult
, (uint32_t)intResult
);
1115 CONFIRM_EQ(intResult
, 1);
1118 res
= theBundle
.get("onehundredtwentythree",status
);
1119 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1120 CONFIRM_EQ(res
.getType(), URES_INT
);
1121 intResult
=res
.getInt(status
);
1122 if(U_SUCCESS(status
)){
1123 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1124 CONFIRM_EQ(intResult
, 123);
1127 /* this tests if escapes are preserved or not */
1129 UnicodeString str
= theBundle
.getStringEx("testescape",status
);
1130 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1131 if(U_SUCCESS(status
)){
1132 u_charsToUChars(expect
,uExpect
,(int32_t)uprv_strlen(expect
)+1);
1133 if(str
.compare(uExpect
)!=0){
1134 errln("Did not get the expected string for testescape expected. Expected : "
1135 +UnicodeString(uExpect
)+ " Got: " + str
);
1139 /* test for jitterbug#1435 */
1141 UnicodeString str
= theBundle
.getStringEx("test_underscores",status
);
1142 expect
="test message ....";
1143 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1144 u_charsToUChars(expect
,uExpect
,(int32_t)uprv_strlen(expect
)+1);
1145 if(str
.compare(uExpect
)!=0){
1146 errln("Did not get the expected string for test_underscores.\n");
1154 NewResourceBundleTest::TestGetByFallback() {
1155 UErrorCode status
= U_ZERO_ERROR
;
1157 ResourceBundle
heRes(NULL
, "he", status
);
1159 heRes
.getWithFallback("calendar", status
).getWithFallback("islamic-civil", status
).getWithFallback("DateTime", status
);
1160 if(U_SUCCESS(status
)) {
1161 errln("he locale's Islamic-civil DateTime resource exists. How did it get here?\n");
1163 status
= U_ZERO_ERROR
;
1165 heRes
.getWithFallback("calendar", status
).getWithFallback("islamic-civil", status
).getWithFallback("eras", status
);
1166 if(U_FAILURE(status
)) {
1167 errln("Didn't get Islamic Eras. I know they are there!\n");
1169 status
= U_ZERO_ERROR
;
1171 ResourceBundle
rootRes(NULL
, "root", status
);
1172 rootRes
.getWithFallback("calendar", status
).getWithFallback("islamic-civil", status
).getWithFallback("DateTime", status
);
1173 if(U_SUCCESS(status
)) {
1174 errln("Root's Islamic-civil's DateTime resource exists. How did it get here?\n");
1176 status
= U_ZERO_ERROR
;