1 /********************************************************************
3 * Copyright (c) 1997-2005, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #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) + (UnicodeString)"\n"); }
38 #define CONFIRM_GE(actual,expected) if ((actual)>=(expected)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x >= " + (expected) + (UnicodeString)"\n"); }
39 #define CONFIRM_NE(actual,expected) if ((expected)!=(actual)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x != " + (expected) + (UnicodeString)"\n"); }
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) + (UnicodeString)"\n"); }
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
= sizeof(param
) / sizeof(param
[0]);
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
< (int)(sizeof(param
)/sizeof(param
[0])); 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 case 0: name
= "TestResourceBundles"; if (exec
) TestResourceBundles(); break;
186 case 1: name
= "TestConstruction"; if (exec
) TestConstruction(); break;
187 case 2: name
= "TestIteration"; if (exec
) TestIteration(); break;
188 case 3: name
= "TestOtherAPI"; if(exec
) TestOtherAPI(); break;
189 case 4: name
= "TestNewTypes"; if(exec
) TestNewTypes(); break;
190 case 5: name
= "TestGetByFallback"; if(exec
) TestGetByFallback(); break;
191 default: name
= ""; break; //needed to end loop
195 //***************************************************************************************
198 NewResourceBundleTest::TestResourceBundles()
200 testTag("only_in_Root", TRUE
, FALSE
, FALSE
);
201 testTag("only_in_te", FALSE
, TRUE
, FALSE
);
202 testTag("only_in_te_IN", FALSE
, FALSE
, TRUE
);
203 testTag("in_Root_te", TRUE
, TRUE
, FALSE
);
204 testTag("in_Root_te_te_IN", TRUE
, TRUE
, TRUE
);
205 testTag("in_Root_te_IN", TRUE
, FALSE
, TRUE
);
206 testTag("in_te_te_IN", FALSE
, TRUE
, TRUE
);
207 testTag("nonexistent", FALSE
, FALSE
, FALSE
);
208 logln("Passed: %d\nFailed: %d", pass
, fail
);
212 NewResourceBundleTest::TestConstruction()
215 UErrorCode err
= U_ZERO_ERROR
;
216 const char* testdatapath
;
217 Locale
locale("te", "IN");
218 testdatapath
=loadTestData(err
);
221 errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
225 ResourceBundle
test1((UnicodeString
)testdatapath
, err
);
226 ResourceBundle
test2(testdatapath
, locale
, err
);
228 UnicodeString result1
;
229 UnicodeString result2
;
231 result1
= test1
.getStringEx("string_in_Root_te_te_IN", err
);
232 result2
= test2
.getStringEx("string_in_Root_te_te_IN", err
);
233 if (U_FAILURE(err
)) {
234 errln("Something threw an error in TestConstruction()");
238 logln("for string_in_Root_te_te_IN, root.txt had " + result1
);
239 logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2
);
241 if (result1
!= "ROOT" || result2
!= "TE_IN")
242 errln("Construction test failed; run verbose for more information");
244 const char* version1
;
245 const char* version2
;
247 version1
= test1
.getVersionNumber();
248 version2
= test2
.getVersionNumber();
250 char *versionID1
= new char[1 + strlen(U_ICU_VERSION
) + strlen(version1
)]; // + 1 for zero byte
251 char *versionID2
= new char[1 + strlen(U_ICU_VERSION
) + strlen(version2
)]; // + 1 for zero byte
253 strcpy(versionID1
, "44.0"); // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed.
255 strcpy(versionID2
, "55.0"); // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed.
257 logln(UnicodeString("getVersionNumber on default.txt returned ") + version1
+ UnicodeString(" Expect: " ) + versionID1
);
258 logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2
+ UnicodeString(" Expect: " ) + versionID2
);
260 if (strcmp(version1
, versionID1
) != 0 || strcmp(version2
, versionID2
) != 0)
261 errln("getVersionNumber() failed");
268 NewResourceBundleTest::TestIteration()
270 UErrorCode err
= U_ZERO_ERROR
;
271 const char* testdatapath
;
273 "string_in_Root_te_te_IN", "1",
274 "array_in_Root_te_te_IN", "5",
275 "array_2d_in_Root_te_te_IN", "4",
278 Locale
*locale
=new Locale("te_IN");
280 testdatapath
=loadTestData(err
);
283 errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
287 ResourceBundle
test1(testdatapath
, *locale
, err
);
289 errln("Construction failed");
292 int32_t count
, row
=0, col
=0;
294 UnicodeString expected
;
295 UnicodeString
element("TE_IN");
296 UnicodeString action
;
299 for(i
=0; i
<sizeof(data
)/sizeof(data
[0]); i
=i
+2){
305 ResourceBundle bundle
= test1
.get(data
[i
], err
);
308 action
+=".getKey()";
310 CONFIRM_EQ((UnicodeString
)bundle
.getKey(), (UnicodeString
)data
[i
]);
314 while(bundle
.hasNext()){
316 action
+=".getNextString(err)";
318 UnicodeString got
=bundle
.getNextString(err
);
321 if(bundle
.getSize() > 1){
322 CONFIRM_EQ(bundle
.getType(), URES_ARRAY
);
323 expected
+=itoa(row
, buf
);
324 ResourceBundle rowbundle
=bundle
.get(row
, err
);
325 if(!U_FAILURE(err
) && rowbundle
.getSize()>1){
327 while(rowbundle
.hasNext()){
329 got
=rowbundle
.getNextString(err
);
331 expected
+=itoa(row
, buf
);
332 expected
+=itoa(col
, buf
);
334 CONFIRM_EQ(got
, expected
);
337 CONFIRM_EQ(col
, rowbundle
.getSize());
341 CONFIRM_EQ(bundle
.getType(), (int32_t)URES_STRING
);
344 CONFIRM_EQ(got
, expected
);
348 action
+=".getSize()";
349 CONFIRM_EQ(bundle
.getSize(), count
);
350 CONFIRM_EQ(count
, atoi(data
[i
+1]));
351 //after reaching the end
353 ResourceBundle errbundle
=bundle
.getNext(err
);
354 action
= "After reaching the end of the Iterator:- ";
356 action
+=".getNext()";
357 CONFIRM_NE(err
, (int32_t)U_ZERO_ERROR
);
358 CONFIRM_EQ(u_errorName(err
), u_errorName(U_INDEX_OUTOFBOUNDS_ERROR
));
361 bundle
.resetIterator();
362 /* The following code is causing a crash
368 errln("ERROR: getNext() throw an error");
380 // TODO: add operator== and != to ResourceBundle
382 equalRB(ResourceBundle
&a
, ResourceBundle
&b
) {
390 a
.getLocale()==b
.getLocale() &&
391 0==strcmp(a
.getName(), b
.getName()) &&
393 a
.getString(status
)==b
.getString(status
) :
395 a
.getInt(status
)==b
.getInt(status
) :
400 NewResourceBundleTest::TestOtherAPI(){
401 UErrorCode err
= U_ZERO_ERROR
;
402 const char* testdatapath
;
403 testdatapath
=loadTestData(err
);
404 UnicodeString tDataPathUS
= UnicodeString(testdatapath
, "");
408 errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
411 Locale
*locale
=new Locale("te_IN");
413 ResourceBundle
test0(tDataPathUS
, *locale
, err
);
415 errln("Construction failed");
419 ResourceBundle
test1(testdatapath
, *locale
, err
);
421 errln("Construction failed");
425 logln("Testing getLocale()\n");
426 if(strcmp(test1
.getLocale().getName(), locale
->getName()) !=0 ){
427 errln("FAIL: ResourceBundle::getLocale() failed\n");
432 logln("Testing ResourceBundle(UErrorCode)\n");
433 ResourceBundle
defaultresource(err
);
435 errln("Construction of default resourcebundle failed");
438 if(strcmp(defaultresource
.getLocale().getName(), Locale::getDefault().getName()) != 0){
439 errln("Construction of default resourcebundle didn't take the defaultlocale\n");
443 ResourceBundle
copyRes(defaultresource
);
444 if(strcmp(copyRes
.getName(), defaultresource
.getName() ) !=0 ||
445 strcmp(test1
.getName(), defaultresource
.getName() ) ==0 ||
446 strcmp(copyRes
.getLocale().getName(), defaultresource
.getLocale().getName() ) !=0 ||
447 strcmp(test1
.getLocale().getName(), defaultresource
.getLocale().getName() ) ==0 ){
448 errln("copy construction failed\n");
451 ResourceBundle defaultSub
= defaultresource
.get(1, err
);
452 ResourceBundle
defSubCopy(defaultSub
);
453 if(strcmp(defSubCopy
.getName(), defaultSub
.getName() ) !=0 ||
454 strcmp(defSubCopy
.getLocale().getName(), defaultSub
.getLocale().getName() ) !=0 ){
455 errln("copy construction for subresource failed\n");
460 p
= defaultresource
.clone();
461 if(p
== &defaultresource
|| !equalRB(*p
, defaultresource
)) {
462 errln("ResourceBundle.clone() failed");
466 p
= defaultSub
.clone();
467 if(p
== &defaultSub
|| !equalRB(*p
, defaultSub
)) {
468 errln("2nd ResourceBundle.clone() failed");
473 copyRes
.getVersion(ver
);
475 logln("Version returned: [%d.%d.%d.%d]\n", ver
[0], ver
[1], ver
[2], ver
[3]);
477 logln("Testing C like UnicodeString APIs\n");
479 UResourceBundle
*testCAPI
= NULL
, *bundle
= NULL
, *rowbundle
= NULL
, *temp
= NULL
;
482 "string_in_Root_te_te_IN", "1",
483 "array_in_Root_te_te_IN", "5",
484 "array_2d_in_Root_te_te_IN", "4",
489 testCAPI
= ures_open(testdatapath
, "te_IN", &err
);
496 int32_t count
, row
=0, col
=0;
498 UnicodeString expected
;
499 UnicodeString
element("TE_IN");
500 UnicodeString action
;
503 for(i
=0; i
<sizeof(data
)/sizeof(data
[0]); i
=i
+2){
509 bundle
= ures_getByKey(testCAPI
, data
[i
], bundle
, &err
);
511 const char* key
= NULL
;
513 action
+=".getKey()";
515 CONFIRM_EQ((UnicodeString
)ures_getKey(bundle
), (UnicodeString
)data
[i
]);
519 while(ures_hasNext(bundle
)){
521 action
+=".getNextString(err)";
523 UnicodeString got
=ures_getNextUnicodeString(bundle
, &key
, &err
);
526 if(ures_getSize(bundle
) > 1){
527 CONFIRM_EQ(ures_getType(bundle
), URES_ARRAY
);
528 expected
+=itoa(row
, buf
);
529 rowbundle
=ures_getByIndex(bundle
, row
, rowbundle
, &err
);
530 if(!U_FAILURE(err
) && ures_getSize(rowbundle
)>1){
532 while(ures_hasNext(rowbundle
)){
534 got
=ures_getNextUnicodeString(rowbundle
, &key
, &err
);
535 temp
= ures_getByIndex(rowbundle
, col
, temp
, &err
);
536 UnicodeString bla
= ures_getUnicodeString(temp
, &err
);
537 UnicodeString bla2
= ures_getUnicodeStringByIndex(rowbundle
, col
, &err
);
539 expected
+=itoa(row
, buf
);
540 expected
+=itoa(col
, buf
);
542 CONFIRM_EQ(got
, expected
);
543 CONFIRM_EQ(bla
, expected
);
544 CONFIRM_EQ(bla2
, expected
);
547 CONFIRM_EQ(col
, ures_getSize(rowbundle
));
551 CONFIRM_EQ(ures_getType(bundle
), (int32_t)URES_STRING
);
554 CONFIRM_EQ(got
, expected
);
560 ures_close(rowbundle
);
562 ures_close(testCAPI
);
564 errln("failed to open a resource bundle\n");
581 //***************************************************************************************
584 NewResourceBundleTest::testTag(const char* frag
,
589 int32_t failOrig
= fail
;
591 // Make array from input params
593 UBool is_in
[] = { in_Root
, in_te
, in_te_IN
};
595 const char* NAME
[] = { "ROOT", "TE", "TE_IN" };
597 // Now try to load the desired items
600 UnicodeString action
;
602 int32_t i
,j
,row
,col
, actual_bundle
;
604 const char* testdatapath
;
606 UErrorCode status
= U_ZERO_ERROR
;
607 testdatapath
=loadTestData(status
);
608 if(U_FAILURE(status
))
610 errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status
)));
614 for (i
=0; i
<bundles_count
; ++i
)
616 action
= "Constructor for ";
617 action
+= param
[i
].name
;
619 status
= U_ZERO_ERROR
;
620 ResourceBundle
theBundle( testdatapath
, *param
[i
].locale
, status
);
621 //ResourceBundle theBundle( "c:\\icu\\icu\\source\\test\\testdata\\testdata", *param[i].locale, status);
622 CONFIRM_UErrorCode(status
,param
[i
].expected_constructor_status
);
625 actual_bundle
= 0; /* ne -> default */
627 actual_bundle
= 1; /* te_NE -> te */
629 actual_bundle
= 2; /* te_IN_NE -> te_IN */
634 UErrorCode expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
635 for (j
=e_te_IN
; j
>=e_Root
; --j
)
637 if (is_in
[j
] && param
[i
].inherits
[j
])
639 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
640 expected_resource_status
= U_ZERO_ERROR
;
642 expected_resource_status
= U_USING_DEFAULT_WARNING
;
644 expected_resource_status
= U_USING_FALLBACK_WARNING
;
650 UErrorCode expected_status
;
653 for (j
=param
[i
].where
; j
>=0; --j
)
662 //--------------------------------------------------------------------------
665 uprv_strcpy(tag
, "string_");
666 uprv_strcat(tag
, frag
);
668 action
= param
[i
].name
;
669 action
+= ".getStringEx(";
674 status
= U_ZERO_ERROR
;
675 UnicodeString string
= theBundle
.getStringEx(tag
, status
);
676 if(U_FAILURE(status
)) {
677 string
.setTo(TRUE
, kErrorUChars
, kErrorLength
);
680 CONFIRM_UErrorCode(status
, expected_resource_status
);
682 UnicodeString
expected_string(kErrorUChars
);
683 if (U_SUCCESS(status
)) {
684 expected_string
= base
;
687 CONFIRM_EQ(string
, expected_string
);
689 //--------------------------------------------------------------------------
690 // array ResourceBundle using the key
692 uprv_strcpy(tag
, "array_");
693 uprv_strcat(tag
, frag
);
695 action
= param
[i
].name
;
700 int32_t count
= kERROR_COUNT
;
701 status
= U_ZERO_ERROR
;
702 ResourceBundle array
= theBundle
.get(tag
, status
);
703 CONFIRM_UErrorCode(status
,expected_resource_status
);
706 if (U_SUCCESS(status
))
708 //confirm the resource type is an array
709 UResType bundleType
=array
.getType();
710 CONFIRM_EQ(bundleType
, URES_ARRAY
);
712 count
=array
.getSize();
715 for (j
=0; j
<count
; ++j
)
718 expected_string
= base
;
719 expected_string
+= itoa(j
,buf
);
720 CONFIRM_EQ(array
.getNextString(status
),expected_string
);
726 CONFIRM_EQ(count
,kERROR_COUNT
);
727 // CONFIRM_EQ((int32_t)(unsigned long)array,(int32_t)0);
731 //--------------------------------------------------------------------------
732 // arrayItem ResourceBundle using the index
735 for (j
=0; j
<100; ++j
)
737 index
= count
? (randi(count
* 3) - count
) : (randi(200) - 100);
738 status
= U_ZERO_ERROR
;
739 string
= kErrorUChars
;
740 ResourceBundle array
= theBundle
.get(tag
, status
);
741 if(!U_FAILURE(status
)){
742 UnicodeString t
= array
.getStringEx(index
, status
);
743 if(!U_FAILURE(status
)) {
748 expected_status
= (index
>= 0 && index
< count
) ? expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
749 CONFIRM_UErrorCode(status
,expected_status
);
751 if (U_SUCCESS(status
)){
753 expected_string
= base
;
754 expected_string
+= itoa(index
,buf
);
756 expected_string
= kErrorUChars
;
758 CONFIRM_EQ(string
,expected_string
);
762 //--------------------------------------------------------------------------
765 uprv_strcpy(tag
, "array_2d_");
766 uprv_strcat(tag
, frag
);
768 action
= param
[i
].name
;
774 int32_t row_count
= kERROR_COUNT
, column_count
= kERROR_COUNT
;
775 status
= U_ZERO_ERROR
;
776 ResourceBundle array2d
=theBundle
.get(tag
, status
);
778 //const UnicodeString** array2d = theBundle.get2dArray(tag, row_count, column_count, status);
779 CONFIRM_UErrorCode(status
,expected_resource_status
);
781 if (U_SUCCESS(status
))
783 //confirm the resource type is an 2darray
784 UResType bundleType
=array2d
.getType();
785 CONFIRM_EQ(bundleType
, URES_ARRAY
);
787 row_count
=array2d
.getSize();
788 CONFIRM_GE(row_count
,1);
790 for(row
=0; row
<row_count
; ++row
){
791 ResourceBundle tablerow
=array2d
.get(row
, status
);
792 CONFIRM_UErrorCode(status
, expected_resource_status
);
793 if(U_SUCCESS(status
)){
794 //confirm the resourcetype of each table row is an array
795 UResType rowType
=tablerow
.getType();
796 CONFIRM_EQ(rowType
, URES_ARRAY
);
798 column_count
=tablerow
.getSize();
799 CONFIRM_GE(column_count
,1);
801 for (col
=0; j
<column_count
; ++j
) {
803 expected_string
= base
;
804 expected_string
+= itoa(row
,buf
);
805 expected_string
+= itoa(col
,buf
);
806 CONFIRM_EQ(tablerow
.getNextString(status
),expected_string
);
811 CONFIRM_EQ(row_count
,kERROR_COUNT
);
812 CONFIRM_EQ(column_count
,kERROR_COUNT
);
813 row_count
=column_count
=0;
819 //--------------------------------------------------------------------------
821 for (j
=0; j
<200; ++j
)
823 row
= row_count
? (randi(row_count
* 3) - row_count
) : (randi(200) - 100);
824 col
= column_count
? (randi(column_count
* 3) - column_count
) : (randi(200) - 100);
825 status
= U_ZERO_ERROR
;
826 string
= kErrorUChars
;
827 ResourceBundle array2d
=theBundle
.get(tag
, status
);
828 if(U_SUCCESS(status
)){
829 ResourceBundle tablerow
=array2d
.get(row
, status
);
830 if(U_SUCCESS(status
)) {
831 UnicodeString t
=tablerow
.getStringEx(col
, status
);
832 if(U_SUCCESS(status
)){
837 expected_status
= (row
>= 0 && row
< row_count
&& col
>= 0 && col
< column_count
) ?
838 expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
839 CONFIRM_UErrorCode(status
,expected_status
);
841 if (U_SUCCESS(status
)){
843 expected_string
= base
;
844 expected_string
+= itoa(row
,buf
);
845 expected_string
+= itoa(col
,buf
);
847 expected_string
= kErrorUChars
;
849 CONFIRM_EQ(string
,expected_string
);
853 //--------------------------------------------------------------------------
856 uprv_strcpy(tag
, "tagged_array_");
857 uprv_strcat(tag
, frag
);
859 action
= param
[i
].name
;
865 status
= U_ZERO_ERROR
;
867 ResourceBundle tags
=theBundle
.get(tag
, status
);
868 CONFIRM_UErrorCode(status
, expected_resource_status
);
870 if (U_SUCCESS(status
)) {
871 UResType bundleType
=tags
.getType();
872 CONFIRM_EQ(bundleType
, URES_TABLE
);
874 tag_count
=tags
.getSize();
875 CONFIRM_GE((int32_t)tag_count
, (int32_t)0);
877 for(index
=0; index
<tag_count
; index
++){
878 ResourceBundle tagelement
=tags
.get(index
, status
);
879 UnicodeString key
=tagelement
.getKey();
880 UnicodeString value
=tagelement
.getNextString(status
);
881 logln("tag = " + key
+ ", value = " + value
);
882 if(key
.startsWith("tag") && value
.startsWith(base
)){
890 for(index
=0; index
<tag_count
; index
++){
891 ResourceBundle tagelement
=tags
.get(index
, status
);
892 const char *tkey
=NULL
;
893 UnicodeString value
=tagelement
.getNextString(&tkey
, status
);
894 UnicodeString
key(tkey
);
895 logln("tag = " + key
+ ", value = " + value
);
896 if(value
.startsWith(base
)){
910 //--------------------------------------------------------------------------
913 action
= param
[i
].name
;
919 for (index
=-20; index
<20; ++index
)
922 status
= U_ZERO_ERROR
;
923 string
= kErrorUChars
;
925 uprv_strcpy(item_tag
, "tag");
926 uprv_strcat(item_tag
, itoa(index
,buf
));
927 ResourceBundle tags
=theBundle
.get(tag
, status
);
928 if(U_SUCCESS(status
)){
929 ResourceBundle tagelement
=tags
.get(item_tag
, status
);
930 if(!U_FAILURE(status
)){
931 UResType elementType
=tagelement
.getType();
932 CONFIRM_EQ(elementType
, (int32_t)URES_STRING
);
933 const char* key
=tagelement
.getKey();
934 CONFIRM_EQ((UnicodeString
)key
, (UnicodeString
)item_tag
);
935 UnicodeString t
=tagelement
.getString(status
);
936 if(!U_FAILURE(status
)){
941 CONFIRM_UErrorCode(status
,U_MISSING_RESOURCE_ERROR
);
944 if (status
!= U_MISSING_RESOURCE_ERROR
) {
946 expected_string
= base
;
947 expected_string
+= buf
;
948 CONFIRM_EQ(string
,expected_string
);
954 CONFIRM_EQ(count
, tag_count
);
957 return (UBool
)(failOrig
== fail
);
961 NewResourceBundleTest::record_pass()
966 NewResourceBundleTest::record_fail()
974 NewResourceBundleTest::TestNewTypes() {
976 const char* testdatapath
;
977 UErrorCode status
= U_ZERO_ERROR
;
978 uint8_t *binResult
= NULL
;
981 int32_t intResult
= 0;
982 uint32_t uintResult
= 0;
983 UChar expected
[] = { 'a','b','c','\0','d','e','f' };
984 const char* expect
="tab:\t cr:\r ff:\f newline:\n backslash:\\\\ quote=\\\' doubleQuote=\\\" singlequoutes=''";
987 testdatapath
=loadTestData(status
);
989 if(U_FAILURE(status
))
991 logln("Could not load testdata.dat %s \n",u_errorName(status
));
995 ResourceBundle
theBundle(testdatapath
, "testtypes", status
);
996 ResourceBundle
bundle(testdatapath
, Locale("te_IN"),status
);
998 UnicodeString emptyStr
= theBundle
.getStringEx("emptystring", status
);
999 if(!emptyStr
.length()==0) {
1000 logln("Empty string returned invalid value\n");
1003 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1005 /* This test reads the string "abc\u0000def" from the bundle */
1006 /* if everything is working correctly, the size of this string */
1007 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
1009 strcpy(action
, "getting and testing of string with embeded zero");
1010 ResourceBundle res
= theBundle
.get("zerotest", status
);
1011 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1012 CONFIRM_EQ(res
.getType(), URES_STRING
);
1013 UnicodeString zeroString
=res
.getString(status
);
1014 len
= zeroString
.length();
1015 if(U_SUCCESS(status
)){
1016 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1021 if(zeroString
[i
]!= expected
[i
]){
1022 logln("Output didnot match Expected: \\u%4X Got: \\u%4X", expected
[i
], zeroString
[i
]);
1026 strcpy(action
, "getting and testing of binary type");
1027 res
= theBundle
.get("binarytest", status
);
1028 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1029 CONFIRM_EQ(res
.getType(), URES_BINARY
);
1030 binResult
=(uint8_t*)res
.getBinary(len
, status
);
1031 if(U_SUCCESS(status
)){
1032 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1033 CONFIRM_EQ(len
, 15);
1034 for(i
= 0; i
<15; i
++) {
1035 CONFIRM_EQ(binResult
[i
], i
);
1039 strcpy(action
, "getting and testing of imported binary type");
1040 res
= theBundle
.get("importtest",status
);
1041 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1042 CONFIRM_EQ(res
.getType(), URES_BINARY
);
1043 binResult
=(uint8_t*)res
.getBinary(len
, status
);
1044 if(U_SUCCESS(status
)){
1045 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1046 CONFIRM_EQ(len
, 15);
1047 for(i
= 0; i
<15; i
++) {
1048 CONFIRM_EQ(binResult
[i
], i
);
1052 strcpy(action
, "getting and testing of integer types");
1053 res
= theBundle
.get("one", status
);
1054 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1055 CONFIRM_EQ(res
.getType(), URES_INT
);
1056 intResult
=res
.getInt(status
);
1057 uintResult
= res
.getUInt(status
);
1058 if(U_SUCCESS(status
)){
1059 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1060 CONFIRM_EQ(uintResult
, (uint32_t)intResult
);
1061 CONFIRM_EQ(intResult
, 1);
1064 strcpy(action
, "getting minusone");
1065 res
= theBundle
.get((const char*)"minusone", status
);
1066 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1067 CONFIRM_EQ(res
.getType(), URES_INT
);
1068 intResult
=res
.getInt(status
);
1069 uintResult
= res
.getUInt(status
);
1070 if(U_SUCCESS(status
)){
1071 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1072 CONFIRM_EQ(uintResult
, 0x0FFFFFFF); /* a 28 bit integer */
1073 CONFIRM_EQ(intResult
, -1);
1074 CONFIRM_NE(uintResult
, (uint32_t)intResult
);
1077 strcpy(action
, "getting plusone");
1078 res
= theBundle
.get("plusone",status
);
1079 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1080 CONFIRM_EQ(res
.getType(), URES_INT
);
1081 intResult
=res
.getInt(status
);
1082 uintResult
= res
.getUInt(status
);
1083 if(U_SUCCESS(status
)){
1084 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1085 CONFIRM_EQ(uintResult
, (uint32_t)intResult
);
1086 CONFIRM_EQ(intResult
, 1);
1089 res
= theBundle
.get("onehundredtwentythree",status
);
1090 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1091 CONFIRM_EQ(res
.getType(), URES_INT
);
1092 intResult
=res
.getInt(status
);
1093 if(U_SUCCESS(status
)){
1094 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1095 CONFIRM_EQ(intResult
, 123);
1098 /* this tests if escapes are preserved or not */
1100 UnicodeString str
= theBundle
.getStringEx("testescape",status
);
1101 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1102 if(U_SUCCESS(status
)){
1103 u_charsToUChars(expect
,uExpect
,(int32_t)uprv_strlen(expect
)+1);
1104 if(str
.compare(uExpect
)!=0){
1105 errln("Did not get the expected string for testescape expected. Expected : "
1106 +UnicodeString(uExpect
)+ " Got: " + str
);
1110 /* test for jitterbug#1435 */
1112 UnicodeString str
= theBundle
.getStringEx("test_underscores",status
);
1113 expect
="test message ....";
1114 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1115 u_charsToUChars(expect
,uExpect
,(int32_t)uprv_strlen(expect
)+1);
1116 if(str
.compare(uExpect
)!=0){
1117 errln("Did not get the expected string for test_underscores.\n");
1125 NewResourceBundleTest::TestGetByFallback() {
1126 UErrorCode status
= U_ZERO_ERROR
;
1128 ResourceBundle
heRes(NULL
, "he", status
);
1130 heRes
.getWithFallback("calendar", status
).getWithFallback("islamic-civil", status
).getWithFallback("DateTime", status
);
1131 if(U_SUCCESS(status
)) {
1132 errln("he locale's Islamic-civil DateTime resource exists. How did it get here?\n");
1134 status
= U_ZERO_ERROR
;
1136 heRes
.getWithFallback("calendar", status
).getWithFallback("islamic-civil", status
).getWithFallback("eras", status
);
1137 if(U_FAILURE(status
)) {
1138 errln("Didn't get Islamic Eras. I know they are there!\n");
1140 status
= U_ZERO_ERROR
;
1142 ResourceBundle
rootRes(NULL
, "root", status
);
1143 rootRes
.getWithFallback("calendar", status
).getWithFallback("islamic-civil", status
).getWithFallback("DateTime", status
);
1144 if(U_SUCCESS(status
)) {
1145 errln("Root's Islamic-civil's DateTime resource exists. How did it get here?\n");
1147 status
= U_ZERO_ERROR
;