1 /********************************************************************
3 * Copyright (c) 1997-2003, 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 default: name
= ""; break; //needed to end loop
194 //***************************************************************************************
197 NewResourceBundleTest::TestResourceBundles()
199 testTag("only_in_Root", TRUE
, FALSE
, FALSE
);
200 testTag("only_in_te", FALSE
, TRUE
, FALSE
);
201 testTag("only_in_te_IN", FALSE
, FALSE
, TRUE
);
202 testTag("in_Root_te", TRUE
, TRUE
, FALSE
);
203 testTag("in_Root_te_te_IN", TRUE
, TRUE
, TRUE
);
204 testTag("in_Root_te_IN", TRUE
, FALSE
, TRUE
);
205 testTag("in_te_te_IN", FALSE
, TRUE
, TRUE
);
206 testTag("nonexistent", FALSE
, FALSE
, FALSE
);
207 logln("Passed: %d\nFailed: %d", pass
, fail
);
211 NewResourceBundleTest::TestConstruction()
214 UErrorCode err
= U_ZERO_ERROR
;
215 const char* testdatapath
;
216 Locale
locale("te", "IN");
217 testdatapath
=loadTestData(err
);
220 errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
224 ResourceBundle
test1((UnicodeString
)testdatapath
, err
);
225 ResourceBundle
test2(testdatapath
, locale
, err
);
227 UnicodeString result1
;
228 UnicodeString result2
;
230 result1
= test1
.getStringEx("string_in_Root_te_te_IN", err
);
231 result2
= test2
.getStringEx("string_in_Root_te_te_IN", err
);
232 if (U_FAILURE(err
)) {
233 errln("Something threw an error in TestConstruction()");
237 logln("for string_in_Root_te_te_IN, root.txt had " + result1
);
238 logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2
);
240 if (result1
!= "ROOT" || result2
!= "TE_IN")
241 errln("Construction test failed; run verbose for more information");
243 const char* version1
;
244 const char* version2
;
246 version1
= test1
.getVersionNumber();
247 version2
= test2
.getVersionNumber();
249 char *versionID1
= new char[1 + strlen(U_ICU_VERSION
) + strlen(version1
)]; // + 1 for zero byte
250 char *versionID2
= new char[1 + strlen(U_ICU_VERSION
) + strlen(version2
)]; // + 1 for zero byte
252 strcpy(versionID1
, "44.0"); // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed.
254 strcpy(versionID2
, "55.0"); // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed.
256 logln(UnicodeString("getVersionNumber on default.txt returned ") + version1
+ UnicodeString(" Expect: " ) + versionID1
);
257 logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2
+ UnicodeString(" Expect: " ) + versionID2
);
259 if (strcmp(version1
, versionID1
) != 0 || strcmp(version2
, versionID2
) != 0)
260 errln("getVersionNumber() failed");
267 NewResourceBundleTest::TestIteration()
269 UErrorCode err
= U_ZERO_ERROR
;
270 const char* testdatapath
;
272 "string_in_Root_te_te_IN", "1",
273 "array_in_Root_te_te_IN", "5",
274 "array_2d_in_Root_te_te_IN", "4",
277 Locale
*locale
=new Locale("te_IN");
279 testdatapath
=loadTestData(err
);
282 errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
286 ResourceBundle
test1(testdatapath
, *locale
, err
);
288 errln("Construction failed");
291 int32_t count
, row
=0, col
=0;
293 UnicodeString expected
;
294 UnicodeString
element("TE_IN");
295 UnicodeString action
;
298 for(i
=0; i
<sizeof(data
)/sizeof(data
[0]); i
=i
+2){
304 ResourceBundle bundle
= test1
.get(data
[i
], err
);
307 action
+=".getKey()";
309 CONFIRM_EQ((UnicodeString
)bundle
.getKey(), (UnicodeString
)data
[i
]);
313 while(bundle
.hasNext()){
315 action
+=".getNextString(err)";
317 UnicodeString got
=bundle
.getNextString(err
);
320 if(bundle
.getSize() > 1){
321 CONFIRM_EQ(bundle
.getType(), URES_ARRAY
);
322 expected
+=itoa(row
, buf
);
323 ResourceBundle rowbundle
=bundle
.get(row
, err
);
324 if(!U_FAILURE(err
) && rowbundle
.getSize()>1){
326 while(rowbundle
.hasNext()){
328 got
=rowbundle
.getNextString(err
);
330 expected
+=itoa(row
, buf
);
331 expected
+=itoa(col
, buf
);
333 CONFIRM_EQ(got
, expected
);
336 CONFIRM_EQ(col
, rowbundle
.getSize());
340 CONFIRM_EQ(bundle
.getType(), (int32_t)URES_STRING
);
343 CONFIRM_EQ(got
, expected
);
347 action
+=".getSize()";
348 CONFIRM_EQ(bundle
.getSize(), count
);
349 CONFIRM_EQ(count
, atoi(data
[i
+1]));
350 //after reaching the end
352 ResourceBundle errbundle
=bundle
.getNext(err
);
353 action
= "After reaching the end of the Iterator:- ";
355 action
+=".getNext()";
356 CONFIRM_NE(err
, (int32_t)U_ZERO_ERROR
);
357 CONFIRM_EQ(u_errorName(err
), u_errorName(U_INDEX_OUTOFBOUNDS_ERROR
));
360 bundle
.resetIterator();
361 /* The following code is causing a crash
367 errln("ERROR: getNext() throw an error");
379 // TODO: add operator== and != to ResourceBundle
381 equalRB(ResourceBundle
&a
, ResourceBundle
&b
) {
389 a
.getLocale()==b
.getLocale() &&
390 0==strcmp(a
.getName(), b
.getName()) &&
392 a
.getString(status
)==b
.getString(status
) :
394 a
.getInt(status
)==b
.getInt(status
) :
399 NewResourceBundleTest::TestOtherAPI(){
400 UErrorCode err
= U_ZERO_ERROR
;
401 const char* testdatapath
;
402 testdatapath
=loadTestData(err
);
403 UnicodeString tDataPathUS
= UnicodeString(testdatapath
, "");
407 errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err
)));
410 Locale
*locale
=new Locale("te_IN");
412 ResourceBundle
test0(tDataPathUS
, *locale
, err
);
414 errln("Construction failed");
418 ResourceBundle
test1(testdatapath
, *locale
, err
);
420 errln("Construction failed");
424 logln("Testing getLocale()\n");
425 if(strcmp(test1
.getLocale().getName(), locale
->getName()) !=0 ){
426 errln("FAIL: ResourceBundle::getLocale() failed\n");
431 logln("Testing ResourceBundle(UErrorCode)\n");
432 ResourceBundle
defaultresource(err
);
434 errln("Construction of default resourcebundle failed");
437 if(strcmp(defaultresource
.getLocale().getName(), Locale::getDefault().getName()) != 0){
438 errln("Construction of default resourcebundle didn't take the defaultlocale\n");
442 ResourceBundle
copyRes(defaultresource
);
443 if(strcmp(copyRes
.getName(), defaultresource
.getName() ) !=0 ||
444 strcmp(test1
.getName(), defaultresource
.getName() ) ==0 ||
445 strcmp(copyRes
.getLocale().getName(), defaultresource
.getLocale().getName() ) !=0 ||
446 strcmp(test1
.getLocale().getName(), defaultresource
.getLocale().getName() ) ==0 ){
447 errln("copy construction failed\n");
450 ResourceBundle defaultSub
= defaultresource
.get(1, err
);
451 ResourceBundle
defSubCopy(defaultSub
);
452 if(strcmp(defSubCopy
.getName(), defaultSub
.getName() ) !=0 ||
453 strcmp(defSubCopy
.getLocale().getName(), defaultSub
.getLocale().getName() ) !=0 ){
454 errln("copy construction for subresource failed\n");
459 p
= defaultresource
.clone();
460 if(p
== &defaultresource
|| !equalRB(*p
, defaultresource
)) {
461 errln("ResourceBundle.clone() failed");
465 p
= defaultSub
.clone();
466 if(p
== &defaultSub
|| !equalRB(*p
, defaultSub
)) {
467 errln("2nd ResourceBundle.clone() failed");
472 copyRes
.getVersion(ver
);
474 logln("Version returned: [%d.%d.%d.%d]\n", ver
[0], ver
[1], ver
[2], ver
[3]);
476 logln("Testing C like UnicodeString APIs\n");
478 UResourceBundle
*testCAPI
= NULL
, *bundle
= NULL
, *rowbundle
= NULL
, *temp
= NULL
;
481 "string_in_Root_te_te_IN", "1",
482 "array_in_Root_te_te_IN", "5",
483 "array_2d_in_Root_te_te_IN", "4",
488 testCAPI
= ures_open(testdatapath
, "te_IN", &err
);
495 int32_t count
, row
=0, col
=0;
497 UnicodeString expected
;
498 UnicodeString
element("TE_IN");
499 UnicodeString action
;
502 for(i
=0; i
<sizeof(data
)/sizeof(data
[0]); i
=i
+2){
508 bundle
= ures_getByKey(testCAPI
, data
[i
], bundle
, &err
);
510 const char* key
= NULL
;
512 action
+=".getKey()";
514 CONFIRM_EQ((UnicodeString
)ures_getKey(bundle
), (UnicodeString
)data
[i
]);
518 while(ures_hasNext(bundle
)){
520 action
+=".getNextString(err)";
522 UnicodeString got
=ures_getNextUnicodeString(bundle
, &key
, &err
);
525 if(ures_getSize(bundle
) > 1){
526 CONFIRM_EQ(ures_getType(bundle
), URES_ARRAY
);
527 expected
+=itoa(row
, buf
);
528 rowbundle
=ures_getByIndex(bundle
, row
, rowbundle
, &err
);
529 if(!U_FAILURE(err
) && ures_getSize(rowbundle
)>1){
531 while(ures_hasNext(rowbundle
)){
533 got
=ures_getNextUnicodeString(rowbundle
, &key
, &err
);
534 temp
= ures_getByIndex(rowbundle
, col
, temp
, &err
);
535 UnicodeString bla
= ures_getUnicodeString(temp
, &err
);
536 UnicodeString bla2
= ures_getUnicodeStringByIndex(rowbundle
, col
, &err
);
538 expected
+=itoa(row
, buf
);
539 expected
+=itoa(col
, buf
);
541 CONFIRM_EQ(got
, expected
);
542 CONFIRM_EQ(bla
, expected
);
543 CONFIRM_EQ(bla2
, expected
);
546 CONFIRM_EQ(col
, ures_getSize(rowbundle
));
550 CONFIRM_EQ(ures_getType(bundle
), (int32_t)URES_STRING
);
553 CONFIRM_EQ(got
, expected
);
559 ures_close(rowbundle
);
561 ures_close(testCAPI
);
563 errln("failed to open a resource bundle\n");
580 //***************************************************************************************
583 NewResourceBundleTest::testTag(const char* frag
,
588 int32_t failOrig
= fail
;
590 // Make array from input params
592 UBool is_in
[] = { in_Root
, in_te
, in_te_IN
};
594 const char* NAME
[] = { "ROOT", "TE", "TE_IN" };
596 // Now try to load the desired items
599 UnicodeString action
;
601 int32_t i
,j
,row
,col
, actual_bundle
;
603 const char* testdatapath
;
605 UErrorCode status
= U_ZERO_ERROR
;
606 testdatapath
=loadTestData(status
);
607 if(U_FAILURE(status
))
609 errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status
)));
613 for (i
=0; i
<bundles_count
; ++i
)
615 action
= "Constructor for ";
616 action
+= param
[i
].name
;
618 status
= U_ZERO_ERROR
;
619 ResourceBundle
theBundle( testdatapath
, *param
[i
].locale
, status
);
620 //ResourceBundle theBundle( "c:\\icu\\icu\\source\\test\\testdata\\testdata", *param[i].locale, status);
621 CONFIRM_UErrorCode(status
,param
[i
].expected_constructor_status
);
624 actual_bundle
= 0; /* ne -> default */
626 actual_bundle
= 1; /* te_NE -> te */
628 actual_bundle
= 2; /* te_IN_NE -> te_IN */
633 UErrorCode expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
634 for (j
=e_te_IN
; j
>=e_Root
; --j
)
636 if (is_in
[j
] && param
[i
].inherits
[j
])
638 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
639 expected_resource_status
= U_ZERO_ERROR
;
641 expected_resource_status
= U_USING_DEFAULT_WARNING
;
643 expected_resource_status
= U_USING_FALLBACK_WARNING
;
649 UErrorCode expected_status
;
652 for (j
=param
[i
].where
; j
>=0; --j
)
661 //--------------------------------------------------------------------------
664 uprv_strcpy(tag
, "string_");
665 uprv_strcat(tag
, frag
);
667 action
= param
[i
].name
;
668 action
+= ".getStringEx(";
673 status
= U_ZERO_ERROR
;
674 UnicodeString string
= theBundle
.getStringEx(tag
, status
);
675 if(U_FAILURE(status
)) {
676 string
.setTo(TRUE
, kErrorUChars
, kErrorLength
);
679 CONFIRM_UErrorCode(status
, expected_resource_status
);
681 UnicodeString
expected_string(kErrorUChars
);
682 if (U_SUCCESS(status
)) {
683 expected_string
= base
;
686 CONFIRM_EQ(string
, expected_string
);
688 //--------------------------------------------------------------------------
689 // array ResourceBundle using the key
691 uprv_strcpy(tag
, "array_");
692 uprv_strcat(tag
, frag
);
694 action
= param
[i
].name
;
699 int32_t count
= kERROR_COUNT
;
700 status
= U_ZERO_ERROR
;
701 ResourceBundle array
= theBundle
.get(tag
, status
);
702 CONFIRM_UErrorCode(status
,expected_resource_status
);
705 if (U_SUCCESS(status
))
707 //confirm the resource type is an array
708 UResType bundleType
=array
.getType();
709 CONFIRM_EQ(bundleType
, URES_ARRAY
);
711 count
=array
.getSize();
714 for (j
=0; j
<count
; ++j
)
717 expected_string
= base
;
718 expected_string
+= itoa(j
,buf
);
719 CONFIRM_EQ(array
.getNextString(status
),expected_string
);
725 CONFIRM_EQ(count
,kERROR_COUNT
);
726 // CONFIRM_EQ((int32_t)(unsigned long)array,(int32_t)0);
730 //--------------------------------------------------------------------------
731 // arrayItem ResourceBundle using the index
734 for (j
=0; j
<100; ++j
)
736 index
= count
? (randi(count
* 3) - count
) : (randi(200) - 100);
737 status
= U_ZERO_ERROR
;
738 string
= kErrorUChars
;
739 ResourceBundle array
= theBundle
.get(tag
, status
);
740 if(!U_FAILURE(status
)){
741 UnicodeString t
= array
.getStringEx(index
, status
);
742 if(!U_FAILURE(status
)) {
747 expected_status
= (index
>= 0 && index
< count
) ? expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
748 CONFIRM_UErrorCode(status
,expected_status
);
750 if (U_SUCCESS(status
)){
752 expected_string
= base
;
753 expected_string
+= itoa(index
,buf
);
755 expected_string
= kErrorUChars
;
757 CONFIRM_EQ(string
,expected_string
);
761 //--------------------------------------------------------------------------
764 uprv_strcpy(tag
, "array_2d_");
765 uprv_strcat(tag
, frag
);
767 action
= param
[i
].name
;
773 int32_t row_count
= kERROR_COUNT
, column_count
= kERROR_COUNT
;
774 status
= U_ZERO_ERROR
;
775 ResourceBundle array2d
=theBundle
.get(tag
, status
);
777 //const UnicodeString** array2d = theBundle.get2dArray(tag, row_count, column_count, status);
778 CONFIRM_UErrorCode(status
,expected_resource_status
);
780 if (U_SUCCESS(status
))
782 //confirm the resource type is an 2darray
783 UResType bundleType
=array2d
.getType();
784 CONFIRM_EQ(bundleType
, URES_ARRAY
);
786 row_count
=array2d
.getSize();
787 CONFIRM_GE(row_count
,1);
789 for(row
=0; row
<row_count
; ++row
){
790 ResourceBundle tablerow
=array2d
.get(row
, status
);
791 CONFIRM_UErrorCode(status
, expected_resource_status
);
792 if(U_SUCCESS(status
)){
793 //confirm the resourcetype of each table row is an array
794 UResType rowType
=tablerow
.getType();
795 CONFIRM_EQ(rowType
, URES_ARRAY
);
797 column_count
=tablerow
.getSize();
798 CONFIRM_GE(column_count
,1);
800 for (col
=0; j
<column_count
; ++j
) {
802 expected_string
= base
;
803 expected_string
+= itoa(row
,buf
);
804 expected_string
+= itoa(col
,buf
);
805 CONFIRM_EQ(tablerow
.getNextString(status
),expected_string
);
810 CONFIRM_EQ(row_count
,kERROR_COUNT
);
811 CONFIRM_EQ(column_count
,kERROR_COUNT
);
812 row_count
=column_count
=0;
818 //--------------------------------------------------------------------------
820 for (j
=0; j
<200; ++j
)
822 row
= row_count
? (randi(row_count
* 3) - row_count
) : (randi(200) - 100);
823 col
= column_count
? (randi(column_count
* 3) - column_count
) : (randi(200) - 100);
824 status
= U_ZERO_ERROR
;
825 string
= kErrorUChars
;
826 ResourceBundle array2d
=theBundle
.get(tag
, status
);
827 if(U_SUCCESS(status
)){
828 ResourceBundle tablerow
=array2d
.get(row
, status
);
829 if(U_SUCCESS(status
)) {
830 UnicodeString t
=tablerow
.getStringEx(col
, status
);
831 if(U_SUCCESS(status
)){
836 expected_status
= (row
>= 0 && row
< row_count
&& col
>= 0 && col
< column_count
) ?
837 expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
838 CONFIRM_UErrorCode(status
,expected_status
);
840 if (U_SUCCESS(status
)){
842 expected_string
= base
;
843 expected_string
+= itoa(row
,buf
);
844 expected_string
+= itoa(col
,buf
);
846 expected_string
= kErrorUChars
;
848 CONFIRM_EQ(string
,expected_string
);
852 //--------------------------------------------------------------------------
855 uprv_strcpy(tag
, "tagged_array_");
856 uprv_strcat(tag
, frag
);
858 action
= param
[i
].name
;
864 status
= U_ZERO_ERROR
;
866 ResourceBundle tags
=theBundle
.get(tag
, status
);
867 CONFIRM_UErrorCode(status
, expected_resource_status
);
869 if (U_SUCCESS(status
)) {
870 UResType bundleType
=tags
.getType();
871 CONFIRM_EQ(bundleType
, URES_TABLE
);
873 tag_count
=tags
.getSize();
874 CONFIRM_GE((int32_t)tag_count
, (int32_t)0);
876 for(index
=0; index
<tag_count
; index
++){
877 ResourceBundle tagelement
=tags
.get(index
, status
);
878 UnicodeString key
=tagelement
.getKey();
879 UnicodeString value
=tagelement
.getNextString(status
);
880 logln("tag = " + key
+ ", value = " + value
);
881 if(key
.startsWith("tag") && value
.startsWith(base
)){
889 for(index
=0; index
<tag_count
; index
++){
890 ResourceBundle tagelement
=tags
.get(index
, status
);
891 const char *tkey
=NULL
;
892 UnicodeString value
=tagelement
.getNextString(&tkey
, status
);
893 UnicodeString
key(tkey
);
894 logln("tag = " + key
+ ", value = " + value
);
895 if(value
.startsWith(base
)){
909 //--------------------------------------------------------------------------
912 action
= param
[i
].name
;
918 for (index
=-20; index
<20; ++index
)
921 status
= U_ZERO_ERROR
;
922 string
= kErrorUChars
;
924 uprv_strcpy(item_tag
, "tag");
925 uprv_strcat(item_tag
, itoa(index
,buf
));
926 ResourceBundle tags
=theBundle
.get(tag
, status
);
927 if(U_SUCCESS(status
)){
928 ResourceBundle tagelement
=tags
.get(item_tag
, status
);
929 if(!U_FAILURE(status
)){
930 UResType elementType
=tagelement
.getType();
931 CONFIRM_EQ(elementType
, (int32_t)URES_STRING
);
932 const char* key
=tagelement
.getKey();
933 CONFIRM_EQ((UnicodeString
)key
, (UnicodeString
)item_tag
);
934 UnicodeString t
=tagelement
.getString(status
);
935 if(!U_FAILURE(status
)){
940 CONFIRM_UErrorCode(status
,U_MISSING_RESOURCE_ERROR
);
943 if (status
!= U_MISSING_RESOURCE_ERROR
) {
945 expected_string
= base
;
946 expected_string
+= buf
;
947 CONFIRM_EQ(string
,expected_string
);
953 CONFIRM_EQ(count
, tag_count
);
956 return (UBool
)(failOrig
== fail
);
960 NewResourceBundleTest::record_pass()
965 NewResourceBundleTest::record_fail()
973 NewResourceBundleTest::TestNewTypes() {
975 const char* testdatapath
;
976 UErrorCode status
= U_ZERO_ERROR
;
977 uint8_t *binResult
= NULL
;
980 int32_t intResult
= 0;
981 uint32_t uintResult
= 0;
982 UChar expected
[] = { 'a','b','c','\0','d','e','f' };
983 const char* expect
="tab:\t cr:\r ff:\f newline:\n backslash:\\\\ quote=\\\' doubleQuote=\\\" singlequoutes=''";
986 testdatapath
=loadTestData(status
);
988 if(U_FAILURE(status
))
990 logln("Could not load testdata.dat %s \n",u_errorName(status
));
994 ResourceBundle
theBundle(testdatapath
, "testtypes", status
);
995 ResourceBundle
bundle(testdatapath
, Locale("te_IN"),status
);
997 UnicodeString emptyStr
= theBundle
.getStringEx("emptystring", status
);
998 if(!emptyStr
.length()==0) {
999 logln("Empty string returned invalid value\n");
1002 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1004 /* This test reads the string "abc\u0000def" from the bundle */
1005 /* if everything is working correctly, the size of this string */
1006 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
1008 strcpy(action
, "getting and testing of string with embeded zero");
1009 ResourceBundle res
= theBundle
.get("zerotest", status
);
1010 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1011 CONFIRM_EQ(res
.getType(), URES_STRING
);
1012 UnicodeString zeroString
=res
.getString(status
);
1013 len
= zeroString
.length();
1014 if(U_SUCCESS(status
)){
1015 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1020 if(zeroString
[i
]!= expected
[i
]){
1021 logln("Output didnot match Expected: \\u%4X Got: \\u%4X", expected
[i
], zeroString
[i
]);
1025 strcpy(action
, "getting and testing of binary type");
1026 res
= theBundle
.get("binarytest", status
);
1027 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1028 CONFIRM_EQ(res
.getType(), URES_BINARY
);
1029 binResult
=(uint8_t*)res
.getBinary(len
, status
);
1030 if(U_SUCCESS(status
)){
1031 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1032 CONFIRM_EQ(len
, 15);
1033 for(i
= 0; i
<15; i
++) {
1034 CONFIRM_EQ(binResult
[i
], i
);
1038 strcpy(action
, "getting and testing of imported binary type");
1039 res
= theBundle
.get("importtest",status
);
1040 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1041 CONFIRM_EQ(res
.getType(), URES_BINARY
);
1042 binResult
=(uint8_t*)res
.getBinary(len
, status
);
1043 if(U_SUCCESS(status
)){
1044 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1045 CONFIRM_EQ(len
, 15);
1046 for(i
= 0; i
<15; i
++) {
1047 CONFIRM_EQ(binResult
[i
], i
);
1051 strcpy(action
, "getting and testing of integer types");
1052 res
= theBundle
.get("one", status
);
1053 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1054 CONFIRM_EQ(res
.getType(), URES_INT
);
1055 intResult
=res
.getInt(status
);
1056 uintResult
= res
.getUInt(status
);
1057 if(U_SUCCESS(status
)){
1058 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1059 CONFIRM_EQ(uintResult
, (uint32_t)intResult
);
1060 CONFIRM_EQ(intResult
, 1);
1063 strcpy(action
, "getting minusone");
1064 res
= theBundle
.get((const char*)"minusone", status
);
1065 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1066 CONFIRM_EQ(res
.getType(), URES_INT
);
1067 intResult
=res
.getInt(status
);
1068 uintResult
= res
.getUInt(status
);
1069 if(U_SUCCESS(status
)){
1070 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1071 CONFIRM_EQ(uintResult
, 0x0FFFFFFF); /* a 28 bit integer */
1072 CONFIRM_EQ(intResult
, -1);
1073 CONFIRM_NE(uintResult
, (uint32_t)intResult
);
1076 strcpy(action
, "getting plusone");
1077 res
= theBundle
.get("plusone",status
);
1078 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1079 CONFIRM_EQ(res
.getType(), URES_INT
);
1080 intResult
=res
.getInt(status
);
1081 uintResult
= res
.getUInt(status
);
1082 if(U_SUCCESS(status
)){
1083 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1084 CONFIRM_EQ(uintResult
, (uint32_t)intResult
);
1085 CONFIRM_EQ(intResult
, 1);
1088 res
= theBundle
.get("onehundredtwentythree",status
);
1089 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1090 CONFIRM_EQ(res
.getType(), URES_INT
);
1091 intResult
=res
.getInt(status
);
1092 if(U_SUCCESS(status
)){
1093 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1094 CONFIRM_EQ(intResult
, 123);
1097 /* this tests if escapes are preserved or not */
1099 UnicodeString str
= theBundle
.getStringEx("testescape",status
);
1100 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1101 if(U_SUCCESS(status
)){
1102 u_charsToUChars(expect
,uExpect
,(int32_t)uprv_strlen(expect
)+1);
1103 if(str
.compare(uExpect
)!=0){
1104 errln("Did not get the expected string for testescape expected. Expected : "
1105 +UnicodeString(uExpect
)+ " Got: " + str
);
1109 /* test for jitterbug#1435 */
1111 UnicodeString str
= theBundle
.getStringEx("test_underscores",status
);
1112 expect
="test message ....";
1113 CONFIRM_UErrorCode(status
, U_ZERO_ERROR
);
1114 u_charsToUChars(expect
,uExpect
,(int32_t)uprv_strlen(expect
)+1);
1115 if(str
.compare(uExpect
)!=0){
1116 errln("Did not get the expected string for test_underscores.\n");