1 /********************************************************************
3 * Copyright (c) 1997-2004, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /*******************************************************************************
10 * Modification History:
12 * Madhu Katragadda Ported for C API
13 * 06/14/99 stephen Updated for RB API changes (no suffix).
14 ********************************************************************************
18 #include "unicode/utypes.h"
20 #include "unicode/ustring.h"
25 #define RESTEST_HEAP_CHECK 0
27 #include "unicode/ures.h"
29 #include "unicode/ctest.h"
31 #include "ucol_imp.h" /* collation */
33 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
35 static void TestOpenDirect(void);
36 static void TestFallback(void);
37 static void TestTable32(void);
38 static void TestFileStream(void);
39 /*****************************************************************************/
41 const UChar kERROR
[] = { 0x0045 /*E*/, 0x0052 /*'R'*/, 0x0052 /*'R'*/,
42 0x004F /*'O'*/, 0x0052/*'R'*/, 0x0000 /*'\0'*/};
44 /*****************************************************************************/
53 typedef enum E_Where E_Where
;
54 /*****************************************************************************/
56 #define CONFIRM_EQ(actual,expected) if (u_strcmp(expected,actual)==0){ record_pass(); } else { record_fail(); log_err("%s returned %s instead of %s\n", action, austrdup(actual), austrdup(expected)); }
58 #define CONFIRM_ErrorCode(actual,expected) if ((expected)==(actual)) { record_pass(); } else { record_fail(); log_err("%s returned %s instead of %s\n", action, myErrorName(actual), myErrorName(expected)); }
61 /* Array of our test objects */
66 UErrorCode expected_constructor_status
;
68 UBool like
[e_Where_count
];
69 UBool inherits
[e_Where_count
];
73 /* "IN" means inherits */
74 /* "NE" or "ne" means "does not exist" */
76 { "root", U_ZERO_ERROR
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} },
77 { "te", U_ZERO_ERROR
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
78 { "te_IN", U_ZERO_ERROR
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
79 { "te_NE", U_USING_FALLBACK_WARNING
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
80 { "te_IN_NE", U_USING_FALLBACK_WARNING
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
81 { "ne", U_USING_DEFAULT_WARNING
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} }
84 static int32_t bundles_count
= sizeof(param
) / sizeof(param
[0]);
88 /***************************************************************************************/
90 /* Array of our test objects */
92 void addResourceBundleTest(TestNode
** root
);
94 void addResourceBundleTest(TestNode
** root
)
96 addTest(root
, &TestConstruction1
, "tsutil/crestst/TestConstruction1");
97 addTest(root
, &TestOpenDirect
, "tsutil/crestst/TestOpenDirect");
98 addTest(root
, &TestResourceBundles
, "tsutil/crestst/TestResourceBundles");
99 addTest(root
, &TestTable32
, "tsutil/crestst/TestTable32");
100 addTest(root
, &TestFallback
, "tsutil/crestst/TestFallback");
101 addTest(root
, &TestAliasConflict
, "tsutil/crestst/TestAliasConflict");
102 addTest(root
, &TestFileStream
, "tsutil/crestst/TestFileStream");
103 addTest(root
, &TestGetSize
, "tsutil/crestst/TestGetSize");
104 addTest(root
, &TestGetLocaleByType
, "tsutil/crestst/TestGetLocaleByType");
108 /***************************************************************************************/
109 void TestAliasConflict(void) {
110 UErrorCode status
= U_ZERO_ERROR
;
111 UResourceBundle
*he
= NULL
;
112 UResourceBundle
*iw
= NULL
;
113 const UChar
*result
= NULL
;
116 he
= ures_open(NULL
, "he", &status
);
117 iw
= ures_open(NULL
, "iw", &status
);
118 if(U_FAILURE(status
)) {
119 log_err("Failed to get resource with %s\n", myErrorName(status
));
122 result
= ures_getStringByKey(he
, "localPatternChars", &resultLen
, &status
);
123 if(U_FAILURE(status
) || result
== NULL
) {
124 log_err("Failed to get resource with %s\n", myErrorName(status
));
130 void TestResourceBundles()
132 testTag("only_in_Root", TRUE
, FALSE
, FALSE
);
133 testTag("in_Root_te", TRUE
, TRUE
, FALSE
);
134 testTag("in_Root_te_te_IN", TRUE
, TRUE
, TRUE
);
135 testTag("in_Root_te_IN", TRUE
, FALSE
, TRUE
);
136 testTag("only_in_te", FALSE
, TRUE
, FALSE
);
137 testTag("only_in_te_IN", FALSE
, FALSE
, TRUE
);
138 testTag("in_te_te_IN", FALSE
, TRUE
, TRUE
);
139 testTag("nonexistent", FALSE
, FALSE
, FALSE
);
141 log_verbose("Passed:= %d Failed= %d \n", pass
, fail
);
144 void TestConstruction1()
146 UResourceBundle
*test1
= 0, *test2
= 0;
147 const UChar
*result1
, *result2
;
151 UErrorCode err
= U_ZERO_ERROR
;
152 const char* testdatapath
;
153 const char* locale
="te_IN";
155 log_verbose("Testing ures_open()......\n");
158 testdatapath
=loadTestData(&err
);
161 log_err("Could not load testdata.dat %s \n",myErrorName(err
));
165 test1
=ures_open(testdatapath
, NULL
, &err
);
168 log_err("construction of %s did not succeed : %s \n",NULL
, myErrorName(err
));
173 test2
=ures_open(testdatapath
, locale
, &err
);
176 log_err("construction of %s did not succeed : %s \n",locale
, myErrorName(err
));
179 result1
= ures_getStringByKey(test1
, "string_in_Root_te_te_IN", &resultLen
, &err
);
180 result2
= ures_getStringByKey(test2
, "string_in_Root_te_te_IN", &resultLen
, &err
);
183 if (U_FAILURE(err
)) {
184 log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(err
));
188 u_uastrcpy(temp
, "TE_IN");
190 if(u_strcmp(result2
, temp
)!=0)
194 log_err("Construction test failed for ures_open();\n");
196 log_info("(run verbose for more information)\n");
198 log_verbose("\nGot->");
199 for(n
=0;result2
[n
];n
++)
201 log_verbose("%04X ",result2
[n
]);
205 log_verbose("\nWant>");
208 log_verbose("%04X ",temp
[n
]);
214 log_verbose("for string_in_Root_te_te_IN, default.txt had %s\n", austrdup(result1
));
215 log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", austrdup(result2
));
217 /* Test getVersionNumber*/
218 log_verbose("Testing version number\n");
219 log_verbose("for getVersionNumber : %s\n", ures_getVersionNumber(test1
));
225 /*****************************************************************************/
226 /*****************************************************************************/
228 UBool
testTag(const char* frag
,
233 int32_t passNum
=pass
;
235 /* Make array from input params */
238 const char *NAME
[] = { "ROOT", "TE", "TE_IN" };
240 /* Now try to load the desired items */
241 UResourceBundle
* theBundle
= NULL
;
244 UErrorCode status
= U_ZERO_ERROR
,expected_resource_status
= U_ZERO_ERROR
;
246 UChar
* expected_string
= NULL
;
247 const UChar
* string
= NULL
;
250 int32_t actual_bundle
;
252 const char *testdatapath
= loadTestData(&status
);
258 strcpy(item_tag
, "tag");
260 status
= U_ZERO_ERROR
;
261 theBundle
= ures_open(testdatapath
, "root", &status
);
262 if(U_FAILURE(status
))
264 ures_close(theBundle
);
265 log_err("Couldn't open root bundle in %s", testdatapath
);
268 ures_close(theBundle
);
272 for (i
=0; i
<bundles_count
; ++i
)
274 strcpy(action
,"construction for");
275 strcat(action
, param
[i
].name
);
278 status
= U_ZERO_ERROR
;
280 theBundle
= ures_open(testdatapath
, param
[i
].name
, &status
);
281 /*theBundle = ures_open("c:\\icu\\icu\\source\\test\\testdata\\testdata", param[i].name, &status);*/
283 CONFIRM_ErrorCode(status
,param
[i
].expected_constructor_status
);
288 actual_bundle
= 0; /* ne -> default */
290 actual_bundle
= 1; /* te_NE -> te */
292 actual_bundle
= 2; /* te_IN_NE -> te_IN */
296 expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
297 for (j
=e_te_IN
; j
>=e_Root
; --j
)
299 if (is_in
[j
] && param
[i
].inherits
[j
])
302 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
303 expected_resource_status
= U_ZERO_ERROR
;
305 expected_resource_status
= U_USING_DEFAULT_WARNING
;
307 expected_resource_status
= U_USING_FALLBACK_WARNING
;
309 log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>. actual_bundle=%s\n",
316 param
[i
].inherits
[j
]?"Yes":"No",
317 param
[actual_bundle
].name
);
323 for (j
=param
[i
].where
; j
>=0; --j
)
332 base
=(UChar
*)malloc(sizeof(UChar
)*(strlen(NAME
[j
]) + 1));
333 u_uastrcpy(base
,NAME
[j
]);
342 base
= (UChar
*) malloc(sizeof(UChar
) * 1);
347 /*-------------------------------------------------------------------- */
350 strcpy(tag
,"string_");
353 strcpy(action
,param
[i
].name
);
354 strcat(action
, ".ures_get(" );
360 status
= U_ZERO_ERROR
;
362 ures_getStringByKey(theBundle
, tag
, &resultLen
, &status
);
363 if(U_SUCCESS(status
))
365 status
= U_ZERO_ERROR
;
366 string
=ures_getStringByKey(theBundle
, tag
, &resultLen
, &status
);
369 log_verbose("%s got %d, expected %d\n", action
, status
, expected_resource_status
);
371 CONFIRM_ErrorCode(status
, expected_resource_status
);
374 if(U_SUCCESS(status
)){
375 expected_string
=(UChar
*)malloc(sizeof(UChar
)*(u_strlen(base
) + 3));
376 u_strcpy(expected_string
,base
);
381 expected_string
= (UChar
*)malloc(sizeof(UChar
)*(u_strlen(kERROR
) + 1));
382 u_strcpy(expected_string
,kERROR
);
386 CONFIRM_EQ(string
, expected_string
);
388 free(expected_string
);
389 ures_close(theBundle
);
392 return (UBool
)(passNum
== pass
);
406 * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR
410 static void TestFallback()
412 UErrorCode status
= U_ZERO_ERROR
;
413 UResourceBundle
*fr_FR
= NULL
;
414 UResourceBundle
*subResource
= NULL
;
415 const UChar
*junk
; /* ignored */
418 log_verbose("Opening fr_FR..");
419 fr_FR
= ures_open(NULL
, "fr_FR", &status
);
420 if(U_FAILURE(status
))
422 log_err("Couldn't open fr_FR - %d\n", status
);
426 status
= U_ZERO_ERROR
;
429 /* clear it out.. just do some calls to get the gears turning */
430 junk
= ures_getStringByKey(fr_FR
, "LocaleID", &resultLen
, &status
);
431 status
= U_ZERO_ERROR
;
432 junk
= ures_getStringByKey(fr_FR
, "LocaleString", &resultLen
, &status
);
433 status
= U_ZERO_ERROR
;
434 junk
= ures_getStringByKey(fr_FR
, "LocaleID", &resultLen
, &status
);
435 status
= U_ZERO_ERROR
;
437 /* OK first one. This should be a Default value. */
438 subResource
= ures_getByKey(fr_FR
, "MeasurementSystem", NULL
, &status
);
439 if(status
!= U_USING_DEFAULT_WARNING
)
441 log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get CurrencyMap from fr_FR, got %s\n",
442 u_errorName(status
));
444 ures_close(subResource
);
445 status
= U_ZERO_ERROR
;
447 /* and this is a Fallback, to fr */
448 junk
= ures_getStringByKey(fr_FR
, "Countries", &resultLen
, &status
);
449 if(status
!= U_USING_FALLBACK_WARNING
)
451 log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get Countries from fr_FR, got %s\n",
452 u_errorName(status
));
455 status
= U_ZERO_ERROR
;
461 TestOpenDirect(void) {
462 UResourceBundle
*idna_rules
, *item
;
463 UErrorCode errorCode
;
466 * test that ures_openDirect() opens a resource bundle
467 * where one can look up its own items but not fallback items
468 * from root or similar
470 errorCode
=U_ZERO_ERROR
;
471 idna_rules
=ures_openDirect("testdata", "idna_rules", &errorCode
);
472 if(U_FAILURE(errorCode
)) {
473 log_err("ures_openDirect(\"idna_rules\") failed: %s\n", u_errorName(errorCode
));
477 if(0!=uprv_strcmp("idna_rules", ures_getLocale(idna_rules
, &errorCode
))) {
478 log_err("ures_openDirect(\"idna_rules\").getLocale()!=idna_rules\n");
480 errorCode
=U_ZERO_ERROR
;
482 /* try an item in idna_rules, must work */
483 item
=ures_getByKey(idna_rules
, "UnassignedSet", NULL
, &errorCode
);
484 if(U_FAILURE(errorCode
)) {
485 log_err("translit_index.getByKey(local key) failed: %s\n", u_errorName(errorCode
));
486 errorCode
=U_ZERO_ERROR
;
491 /* try an item in root, must fail */
492 item
=ures_getByKey(idna_rules
, "Languages", NULL
, &errorCode
);
493 if(U_FAILURE(errorCode
)) {
494 errorCode
=U_ZERO_ERROR
;
496 log_err("idna_rules.getByKey(root key) succeeded!\n");
499 ures_close(idna_rules
);
501 /* now make sure that "idna_rules" will not work with ures_open() */
502 errorCode
=U_ZERO_ERROR
;
503 idna_rules
=ures_open("testdata", "idna_rules", &errorCode
);
504 if(U_FAILURE(errorCode
) || errorCode
==U_USING_DEFAULT_WARNING
|| errorCode
==U_USING_FALLBACK_WARNING
) {
505 /* falling back to default or root is ok */
506 errorCode
=U_ZERO_ERROR
;
507 } else if(0!=uprv_strcmp("idna_rules", ures_getLocale(idna_rules
, &errorCode
))) {
508 /* Opening this file will work in "files mode" on Windows and the Mac,
509 which have case insensitive file systems */
510 log_err("ures_open(\"idna_rules\") succeeded, should fail! Got: %s\n", u_errorName(errorCode
));
512 ures_close(idna_rules
);
514 /* ures_openDirect("translit_index_WronG") must fail */
515 idna_rules
=ures_openDirect(NULL
, "idna_rules_WronG", &errorCode
);
516 if(U_FAILURE(errorCode
)) {
517 errorCode
=U_ZERO_ERROR
;
519 log_err("ures_openDirect(\"idna_rules_WronG\") succeeded, should fail!\n");
521 ures_close(idna_rules
);
523 errorCode
= U_USING_FALLBACK_WARNING
;;
524 idna_rules
=ures_openDirect("testdata", "idna_rules", &errorCode
);
525 if(U_FAILURE(errorCode
)) {
526 log_err("ures_openDirect(\"idna_rules\") failed when U_USING_FALLBACK_WARNING was set prior to call: %s\n", u_errorName(errorCode
));
529 ures_close(idna_rules
);
533 parseTable32Key(const char *key
) {
538 while((c
=*key
++)!=0) {
549 static const struct {
553 { "ooooooooooooooooo", 0 },
554 { "oooooooooooooooo1", 1 },
555 { "ooooooooooooooo1o", 2 },
556 { "oo11ooo1ooo11111o", 25150 },
557 { "oo11ooo1ooo111111", 25151 },
558 { "o1111111111111111", 65535 },
559 { "1oooooooooooooooo", 65536 },
560 { "1ooooooo11o11ooo1", 65969 },
561 { "1ooooooo11o11oo1o", 65970 },
562 { "1ooooooo111oo1111", 65999 }
565 /* ### TODO UResourceBundle staticItem={ 0 }; - need to know the size */
566 UResourceBundle
*res
, *item
;
569 UErrorCode errorCode
;
570 int32_t i
, j
, number
, parsedNumber
, length
, count
;
572 errorCode
=U_ZERO_ERROR
;
573 res
=ures_open(loadTestData(&errorCode
), "testtable32", &errorCode
);
574 if(U_FAILURE(errorCode
)) {
575 log_data_err("unable to open testdata/testtable32.res - %s\n", u_errorName(errorCode
));
578 if(ures_getType(res
)!=URES_TABLE
) {
579 log_data_err("testdata/testtable32.res has type %d instead of URES_TABLE\n", ures_getType(res
));
582 count
=ures_getSize(res
);
584 log_err("testdata/testtable32.res should have 66000 entries but has %d\n", count
);
587 /* get the items by index */
589 for(i
=0; i
<count
; ++i
) {
590 item
=ures_getByIndex(res
, i
, item
, &errorCode
);
591 if(U_FAILURE(errorCode
)) {
592 log_err("unable to get item %d of %d in testdata/testtable32.res - %s\n",
593 i
, count
, u_errorName(errorCode
));
597 key
=ures_getKey(item
);
598 parsedNumber
=parseTable32Key(key
);
600 switch(ures_getType(item
)) {
602 s
=ures_getString(item
, &length
, &errorCode
);
603 if(U_FAILURE(errorCode
) || s
==NULL
) {
604 log_err("unable to access the string \"%s\" at %d in testdata/testtable32.res - %s\n",
605 key
, i
, u_errorName(errorCode
));
609 U16_NEXT(s
, j
, length
, number
);
613 number
=ures_getInt(item
, &errorCode
);
614 if(U_FAILURE(errorCode
)) {
615 log_err("unable to access the integer \"%s\" at %d in testdata/testtable32.res - %s\n",
616 key
, i
, u_errorName(errorCode
));
621 log_err("unexpected resource type %d for \"%s\" at %d in testdata/testtable32.res - %s\n",
622 ures_getType(item
), key
, i
, u_errorName(errorCode
));
627 if(number
>=0 && number
!=parsedNumber
) {
628 log_err("\"%s\" at %d in testdata/testtable32.res has a string/int value of %d, expected %d\n",
629 key
, i
, number
, parsedNumber
);
633 /* search for some items by key */
634 for(i
=0; i
<LENGTHOF(testcases
); ++i
) {
635 item
=ures_getByKey(res
, testcases
[i
].key
, item
, &errorCode
);
636 if(U_FAILURE(errorCode
)) {
637 log_err("unable to find the key \"%s\" in testdata/testtable32.res - %s\n",
638 testcases
[i
].key
, u_errorName(errorCode
));
642 switch(ures_getType(item
)) {
644 s
=ures_getString(item
, &length
, &errorCode
);
645 if(U_FAILURE(errorCode
) || s
==NULL
) {
646 log_err("unable to access the string \"%s\" in testdata/testtable32.res - %s\n",
647 testcases
[i
].key
, u_errorName(errorCode
));
651 U16_NEXT(s
, j
, length
, number
);
655 number
=ures_getInt(item
, &errorCode
);
656 if(U_FAILURE(errorCode
)) {
657 log_err("unable to access the integer \"%s\" in testdata/testtable32.res - %s\n",
658 testcases
[i
].key
, u_errorName(errorCode
));
663 log_err("unexpected resource type %d for \"%s\" in testdata/testtable32.res - %s\n",
664 ures_getType(item
), testcases
[i
].key
, u_errorName(errorCode
));
669 if(number
>=0 && number
!=testcases
[i
].number
) {
670 log_err("\"%s\" in testdata/testtable32.res has a string/int value of %d, expected %d\n",
671 testcases
[i
].key
, number
, testcases
[i
].number
);
674 key
=ures_getKey(item
);
675 if(0!=uprv_strcmp(key
, testcases
[i
].key
)) {
676 log_err("\"%s\" in testdata/testtable32.res claims to have the key \"%s\"\n",
677 testcases
[i
].key
, key
);
685 static void TestFileStream(void){
688 UErrorCode status
= U_ZERO_ERROR
;
689 const char* testdatapath
= loadTestData(&status
);
690 char* fileName
= (char*) malloc(uprv_strlen(testdatapath
) +10);
691 FileStream
* stream
= NULL
;
692 /* these should not be closed */
693 FileStream
* pStdin
= T_FileStream_stdin();
694 FileStream
* pStdout
= T_FileStream_stdout();
695 FileStream
* pStderr
= T_FileStream_stderr();
697 const char* testline
= "This is a test line";
698 int32_t bufLen
= (int32_t)strlen(testline
)+10;
699 char* buf
= (char*) malloc(bufLen
);
703 log_err("failed to get T_FileStream_stdin()");
706 log_err("failed to get T_FileStream_stdout()");
709 log_err("failed to get T_FileStream_stderr()");
712 uprv_strcpy(fileName
,testdatapath
);
713 uprv_strcat(fileName
,".dat");
714 stream
= T_FileStream_open(fileName
, "r");
716 log_data_err("T_FileStream_open failed to open %s\n",fileName
);
718 if(!T_FileStream_file_exists(fileName
)){
719 log_data_err("T_FileStream_file_exists failed to verify existence of %s \n",fileName
);
722 retLen
=T_FileStream_read(stream
,&c
,1);
724 log_data_err("T_FileStream_read failed to read from %s \n",fileName
);
727 T_FileStream_rewind(stream
);
728 T_FileStream_read(stream
,&c1
,1);
730 log_data_err("T_FileStream_rewind failed to rewind %s \n",fileName
);
732 T_FileStream_rewind(stream
);
733 c1
= T_FileStream_peek(stream
);
735 log_data_err("T_FileStream_peek failed to peekd %s \n",fileName
);
737 c
= T_FileStream_getc(stream
);
738 T_FileStream_ungetc(c
,stream
);
739 if(c
!= T_FileStream_getc(stream
)){
740 log_data_err("T_FileStream_ungetc failed to d %s \n",fileName
);
743 if(T_FileStream_size(stream
)<=0){
744 log_data_err("T_FileStream_size failed to d %s \n",fileName
);
746 if(T_FileStream_error(stream
)){
747 log_data_err("T_FileStream_error shouldn't have an error %s\n",fileName
);
749 if(!T_FileStream_error(NULL
)){
750 log_err("T_FileStream_error didn't get an error %s\n",fileName
);
752 T_FileStream_putc(stream
, 0x20);
753 if(!T_FileStream_error(stream
)){
756 writing to a read-only file may not consistently fail on all platforms
757 (e.g. HP-UX, FreeBSD, MacOSX)
759 log_verbose("T_FileStream_error didn't get an error when writing to a readonly file %s\n",fileName
);
762 T_FileStream_close(stream
);
764 /* test writing function */
766 uprv_strcpy(fileName
,testdatapath
);
767 uprv_strcat(fileName
,".tmp");
768 stream
= T_FileStream_open(fileName
,"w+");
771 log_data_err("Could not open %s for writing\n",fileName
);
774 T_FileStream_putc(stream
,c
);
775 T_FileStream_rewind(stream
);
776 if(c
!= T_FileStream_getc(stream
)){
777 log_data_err("T_FileStream_putc failed %s\n",fileName
);
780 T_FileStream_rewind(stream
);
781 T_FileStream_writeLine(stream
,testline
);
782 T_FileStream_rewind(stream
);
783 T_FileStream_readLine(stream
,buf
,bufLen
);
784 if(uprv_strncmp(testline
, buf
,uprv_strlen(buf
))!=0){
785 log_data_err("T_FileStream_writeLine failed %s\n",fileName
);
788 T_FileStream_rewind(stream
);
789 T_FileStream_write(stream
,testline
,(int32_t)strlen(testline
));
790 T_FileStream_rewind(stream
);
791 retLen
= T_FileStream_read(stream
, buf
, bufLen
);
792 if(uprv_strncmp(testline
, buf
,retLen
)!=0){
793 log_data_err("T_FileStream_write failed %s\n",fileName
);
796 T_FileStream_close(stream
);
798 if(!T_FileStream_remove(fileName
)){
799 log_data_err("T_FileStream_remove failed to delete %s\n",fileName
);
808 static void TestGetSize(void) {
816 { "integerarray", 1},
819 { "emptystring", 1}, /* empty string is still a string */
823 { "collations", 1}, /* not 2 - there is hidden %%CollationBin */
826 UErrorCode status
= U_ZERO_ERROR
;
828 UResourceBundle
*rb
= NULL
;
829 UResourceBundle
*res
= NULL
;
830 UResourceBundle
*helper
= NULL
;
831 const char* testdatapath
= loadTestData(&status
);
832 int32_t i
= 0, j
= 0;
835 if(U_FAILURE(status
))
837 log_err("Could not load testdata.dat %s\n", u_errorName(status
));
841 rb
= ures_open(testdatapath
, "testtypes", &status
);
842 if(U_FAILURE(status
))
844 log_err("Could not testtypes resource bundle %s\n", u_errorName(status
));
848 for(i
= 0; i
< sizeof(test
)/sizeof(test
[0]); i
++) {
849 res
= ures_getByKey(rb
, test
[i
].key
, res
, &status
);
850 if(U_FAILURE(status
))
852 log_err("Couldn't find the key %s. Error: %s\n", test
[i
].key
, u_errorName(status
));
856 size
= ures_getSize(res
);
857 if(size
!= test
[i
].size
) {
858 log_err("Expected size %i, got size %i for key %s\n", test
[i
].size
, size
, test
[i
].key
);
859 for(j
= 0; j
< size
; j
++) {
860 helper
= ures_getByIndex(res
, j
, helper
, &status
);
861 log_err("%s\n", ures_getKey(helper
));
870 static void TestGetLocaleByType(void) {
871 static const struct {
872 const char *requestedLocale
;
873 const char *resourceKey
;
874 const char *validLocale
;
875 const char *actualLocale
;
877 { "te_IN_BLAH", "string_only_in_te_IN", "te_IN", "te_IN" },
878 { "te_IN_BLAH", "string_only_in_te", "te_IN", "te" },
879 { "te_IN_BLAH", "string_only_in_Root", "te_IN", "root" },
880 { "te_IN_BLAH_01234567890_01234567890_01234567890_01234567890_01234567890_01234567890", "array_2d_only_in_Root", "te_IN", "root" },
881 { "te_IN_BLAH@currency=euro", "array_2d_only_in_te_IN", "te_IN", "te_IN" },
882 { "te_IN_BLAH@collation=phonebook;calendar=thai", "array_2d_only_in_te", "te_IN", "te" }
885 UErrorCode status
= U_ZERO_ERROR
;
887 UResourceBundle
*rb
= NULL
;
888 UResourceBundle
*res
= NULL
;
889 const char* testdatapath
= loadTestData(&status
);
891 const char *locale
= NULL
;
893 if(U_FAILURE(status
))
895 log_err("Could not load testdata.dat %s\n", u_errorName(status
));
899 for(i
= 0; i
< sizeof(test
)/sizeof(test
[0]); i
++) {
900 rb
= ures_open(testdatapath
, test
[i
].requestedLocale
, &status
);
901 if(U_FAILURE(status
))
903 log_err("Could not open resource bundle %s (error %s)\n", test
[i
].requestedLocale
, u_errorName(status
));
904 status
= U_ZERO_ERROR
;
908 res
= ures_getByKey(rb
, test
[i
].resourceKey
, res
, &status
);
909 if(U_FAILURE(status
))
911 log_err("Couldn't find the key %s. Error: %s\n", test
[i
].resourceKey
, u_errorName(status
));
913 status
= U_ZERO_ERROR
;
917 locale
= ures_getLocaleByType(res
, ULOC_REQUESTED_LOCALE
, &status
);
919 log_err("Requested locale should return NULL\n");
921 locale
= ures_getLocaleByType(res
, ULOC_VALID_LOCALE
, &status
);
922 if(!locale
|| strcmp(locale
, test
[i
].validLocale
) != 0) {
923 log_err("Expected valid locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
);
925 locale
= ures_getLocaleByType(res
, ULOC_ACTUAL_LOCALE
, &status
);
926 if(!locale
|| strcmp(locale
, test
[i
].actualLocale
) != 0) {
927 log_err("Expected actual locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
);