1 /********************************************************************
3 * Copyright (c) 1997-2010, 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 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
97 addTest(root
, &TestConstruction1
, "tsutil/crestst/TestConstruction1");
98 addTest(root
, &TestOpenDirect
, "tsutil/crestst/TestOpenDirect");
99 addTest(root
, &TestResourceBundles
, "tsutil/crestst/TestResourceBundles");
100 addTest(root
, &TestTable32
, "tsutil/crestst/TestTable32");
101 addTest(root
, &TestFileStream
, "tsutil/crestst/TestFileStream");
102 addTest(root
, &TestGetSize
, "tsutil/crestst/TestGetSize");
103 addTest(root
, &TestGetLocaleByType
, "tsutil/crestst/TestGetLocaleByType");
105 addTest(root
, &TestFallback
, "tsutil/crestst/TestFallback");
106 addTest(root
, &TestAliasConflict
, "tsutil/crestst/TestAliasConflict");
111 /***************************************************************************************/
112 void TestAliasConflict(void) {
113 UErrorCode status
= U_ZERO_ERROR
;
114 UResourceBundle
*he
= NULL
;
115 UResourceBundle
*iw
= NULL
;
116 const UChar
*result
= NULL
;
119 he
= ures_open(NULL
, "he", &status
);
120 iw
= ures_open(NULL
, "iw", &status
);
121 if(U_FAILURE(status
)) {
122 log_err_status(status
, "Failed to get resource with %s\n", myErrorName(status
));
125 result
= ures_getStringByKey(he
, "ExemplarCharacters", &resultLen
, &status
);
126 if(U_FAILURE(status
) || result
== NULL
) {
127 log_err_status(status
, "Failed to get resource with %s\n", myErrorName(status
));
133 void TestResourceBundles()
135 UErrorCode status
= U_ZERO_ERROR
;
136 loadTestData(&status
);
137 if(U_FAILURE(status
)) {
138 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
142 testTag("only_in_Root", TRUE
, FALSE
, FALSE
);
143 testTag("in_Root_te", TRUE
, TRUE
, FALSE
);
144 testTag("in_Root_te_te_IN", TRUE
, TRUE
, TRUE
);
145 testTag("in_Root_te_IN", TRUE
, FALSE
, TRUE
);
146 testTag("only_in_te", FALSE
, TRUE
, FALSE
);
147 testTag("only_in_te_IN", FALSE
, FALSE
, TRUE
);
148 testTag("in_te_te_IN", FALSE
, TRUE
, TRUE
);
149 testTag("nonexistent", FALSE
, FALSE
, FALSE
);
151 log_verbose("Passed:= %d Failed= %d \n", pass
, fail
);
154 void TestConstruction1()
156 UResourceBundle
*test1
= 0, *test2
= 0;
157 const UChar
*result1
, *result2
;
161 UErrorCode err
= U_ZERO_ERROR
;
162 const char* testdatapath
;
163 const char* locale
="te_IN";
165 log_verbose("Testing ures_open()......\n");
168 testdatapath
=loadTestData(&err
);
171 log_data_err("Could not load testdata.dat %s \n",myErrorName(err
));
175 test1
=ures_open(testdatapath
, NULL
, &err
);
178 log_err("construction of %s did not succeed : %s \n",NULL
, myErrorName(err
));
183 test2
=ures_open(testdatapath
, locale
, &err
);
186 log_err("construction of %s did not succeed : %s \n",locale
, myErrorName(err
));
189 result1
= ures_getStringByKey(test1
, "string_in_Root_te_te_IN", &resultLen
, &err
);
190 result2
= ures_getStringByKey(test2
, "string_in_Root_te_te_IN", &resultLen
, &err
);
193 if (U_FAILURE(err
)) {
194 log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(err
));
198 u_uastrcpy(temp
, "TE_IN");
200 if(u_strcmp(result2
, temp
)!=0)
204 log_err("Construction test failed for ures_open();\n");
205 if(!getTestOption(VERBOSITY_OPTION
))
206 log_info("(run verbose for more information)\n");
208 log_verbose("\nGot->");
209 for(n
=0;result2
[n
];n
++)
211 log_verbose("%04X ",result2
[n
]);
215 log_verbose("\nWant>");
218 log_verbose("%04X ",temp
[n
]);
224 log_verbose("for string_in_Root_te_te_IN, default.txt had %s\n", austrdup(result1
));
225 log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", austrdup(result2
));
227 /* Test getVersionNumber*/
228 log_verbose("Testing version number\n");
229 log_verbose("for getVersionNumber : %s\n", ures_getVersionNumber(test1
));
235 /*****************************************************************************/
236 /*****************************************************************************/
238 UBool
testTag(const char* frag
,
243 int32_t passNum
=pass
;
245 /* Make array from input params */
248 const char *NAME
[] = { "ROOT", "TE", "TE_IN" };
250 /* Now try to load the desired items */
251 UResourceBundle
* theBundle
= NULL
;
254 UErrorCode status
= U_ZERO_ERROR
,expected_resource_status
= U_ZERO_ERROR
;
256 UChar
* expected_string
= NULL
;
257 const UChar
* string
= NULL
;
260 int32_t actual_bundle
;
262 const char *testdatapath
= loadTestData(&status
);
268 strcpy(item_tag
, "tag");
270 status
= U_ZERO_ERROR
;
271 theBundle
= ures_open(testdatapath
, "root", &status
);
272 if(U_FAILURE(status
))
274 ures_close(theBundle
);
275 log_err("Couldn't open root bundle in %s", testdatapath
);
278 ures_close(theBundle
);
282 for (i
=0; i
<bundles_count
; ++i
)
284 strcpy(action
,"construction for");
285 strcat(action
, param
[i
].name
);
288 status
= U_ZERO_ERROR
;
290 theBundle
= ures_open(testdatapath
, param
[i
].name
, &status
);
291 /*theBundle = ures_open("c:\\icu\\icu\\source\\test\\testdata\\testdata", param[i].name, &status);*/
293 CONFIRM_ErrorCode(status
,param
[i
].expected_constructor_status
);
298 actual_bundle
= 0; /* ne -> default */
300 actual_bundle
= 1; /* te_NE -> te */
302 actual_bundle
= 2; /* te_IN_NE -> te_IN */
306 expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
307 for (j
=e_te_IN
; j
>=e_Root
; --j
)
309 if (is_in
[j
] && param
[i
].inherits
[j
])
312 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
313 expected_resource_status
= U_ZERO_ERROR
;
315 expected_resource_status
= U_USING_DEFAULT_WARNING
;
317 expected_resource_status
= U_USING_FALLBACK_WARNING
;
319 log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>. actual_bundle=%s\n",
326 param
[i
].inherits
[j
]?"Yes":"No",
327 param
[actual_bundle
].name
);
333 for (j
=param
[i
].where
; j
>=0; --j
)
342 base
=(UChar
*)malloc(sizeof(UChar
)*(strlen(NAME
[j
]) + 1));
343 u_uastrcpy(base
,NAME
[j
]);
352 base
= (UChar
*) malloc(sizeof(UChar
) * 1);
357 /*-------------------------------------------------------------------- */
360 strcpy(tag
,"string_");
363 strcpy(action
,param
[i
].name
);
364 strcat(action
, ".ures_get(" );
370 status
= U_ZERO_ERROR
;
372 ures_getStringByKey(theBundle
, tag
, &resultLen
, &status
);
373 if(U_SUCCESS(status
))
375 status
= U_ZERO_ERROR
;
376 string
=ures_getStringByKey(theBundle
, tag
, &resultLen
, &status
);
379 log_verbose("%s got %d, expected %d\n", action
, status
, expected_resource_status
);
381 CONFIRM_ErrorCode(status
, expected_resource_status
);
384 if(U_SUCCESS(status
)){
385 expected_string
=(UChar
*)malloc(sizeof(UChar
)*(u_strlen(base
) + 3));
386 u_strcpy(expected_string
,base
);
391 expected_string
= (UChar
*)malloc(sizeof(UChar
)*(u_strlen(kERROR
) + 1));
392 u_strcpy(expected_string
,kERROR
);
396 CONFIRM_EQ(string
, expected_string
);
398 free(expected_string
);
399 ures_close(theBundle
);
402 return (UBool
)(passNum
== pass
);
416 * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR
420 static void TestFallback()
422 UErrorCode status
= U_ZERO_ERROR
;
423 UResourceBundle
*fr_FR
= NULL
;
424 UResourceBundle
*subResource
= NULL
;
425 const UChar
*junk
; /* ignored */
428 log_verbose("Opening fr_FR..");
429 fr_FR
= ures_open(NULL
, "fr_FR", &status
);
430 if(U_FAILURE(status
))
432 log_err_status(status
, "Couldn't open fr_FR - %s\n", u_errorName(status
));
436 status
= U_ZERO_ERROR
;
439 /* clear it out.. just do some calls to get the gears turning */
440 junk
= ures_getStringByKey(fr_FR
, "LocaleID", &resultLen
, &status
);
441 status
= U_ZERO_ERROR
;
442 junk
= ures_getStringByKey(fr_FR
, "LocaleString", &resultLen
, &status
);
443 status
= U_ZERO_ERROR
;
444 junk
= ures_getStringByKey(fr_FR
, "LocaleID", &resultLen
, &status
);
445 status
= U_ZERO_ERROR
;
447 /* OK first one. This should be a Default value. */
448 subResource
= ures_getByKey(fr_FR
, "MeasurementSystem", NULL
, &status
);
449 if(status
!= U_USING_DEFAULT_WARNING
)
451 log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get CurrencyMap from fr_FR, got %s\n",
452 u_errorName(status
));
454 ures_close(subResource
);
455 status
= U_ZERO_ERROR
;
457 /* and this is a Fallback, to fr */
458 junk
= ures_getStringByKey(fr_FR
, "ExemplarCharacters", &resultLen
, &status
);
459 if(status
!= U_USING_FALLBACK_WARNING
)
461 log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get ExemplarCharacters from fr_FR, got %s\n",
462 u_errorName(status
));
465 status
= U_ZERO_ERROR
;
471 TestOpenDirect(void) {
472 UResourceBundle
*idna_rules
, *casing
, *te_IN
, *ne
, *item
;
473 UErrorCode errorCode
;
476 * test that ures_openDirect() opens a resource bundle
477 * where one can look up its own items but not fallback items
478 * from root or similar
480 errorCode
=U_ZERO_ERROR
;
481 idna_rules
=ures_openDirect(loadTestData(&errorCode
), "idna_rules", &errorCode
);
482 if(U_FAILURE(errorCode
)) {
483 log_data_err("ures_openDirect(\"idna_rules\") failed: %s\n", u_errorName(errorCode
));
487 if(0!=uprv_strcmp("idna_rules", ures_getLocale(idna_rules
, &errorCode
))) {
488 log_err("ures_openDirect(\"idna_rules\").getLocale()!=idna_rules\n");
490 errorCode
=U_ZERO_ERROR
;
492 /* try an item in idna_rules, must work */
493 item
=ures_getByKey(idna_rules
, "UnassignedSet", NULL
, &errorCode
);
494 if(U_FAILURE(errorCode
)) {
495 log_err("translit_index.getByKey(local key) failed: %s\n", u_errorName(errorCode
));
496 errorCode
=U_ZERO_ERROR
;
501 /* try an item in root, must fail */
502 item
=ures_getByKey(idna_rules
, "ShortLanguage", NULL
, &errorCode
);
503 if(U_FAILURE(errorCode
)) {
504 errorCode
=U_ZERO_ERROR
;
506 log_err("idna_rules.getByKey(root key) succeeded!\n");
509 ures_close(idna_rules
);
511 /* now make sure that "idna_rules" will not work with ures_open() */
512 errorCode
=U_ZERO_ERROR
;
513 idna_rules
=ures_open("testdata", "idna_rules", &errorCode
);
514 if(U_FAILURE(errorCode
) || errorCode
==U_USING_DEFAULT_WARNING
|| errorCode
==U_USING_FALLBACK_WARNING
) {
515 /* falling back to default or root is ok */
516 errorCode
=U_ZERO_ERROR
;
517 } else if(0!=uprv_strcmp("idna_rules", ures_getLocale(idna_rules
, &errorCode
))) {
518 /* Opening this file will work in "files mode" on Windows and the Mac,
519 which have case insensitive file systems */
520 log_err("ures_open(\"idna_rules\") succeeded, should fail! Got: %s\n", u_errorName(errorCode
));
522 ures_close(idna_rules
);
524 /* ures_openDirect("translit_index_WronG") must fail */
525 idna_rules
=ures_openDirect(NULL
, "idna_rules_WronG", &errorCode
);
526 if(U_FAILURE(errorCode
)) {
527 errorCode
=U_ZERO_ERROR
;
529 log_err("ures_openDirect(\"idna_rules_WronG\") succeeded, should fail!\n");
531 ures_close(idna_rules
);
533 errorCode
= U_USING_FALLBACK_WARNING
;;
534 idna_rules
=ures_openDirect("testdata", "idna_rules", &errorCode
);
535 if(U_FAILURE(errorCode
)) {
536 log_data_err("ures_openDirect(\"idna_rules\") failed when U_USING_FALLBACK_WARNING was set prior to call: %s\n", u_errorName(errorCode
));
539 ures_close(idna_rules
);
542 * ICU 3.6 has new resource bundle syntax and data for bundles that do not
543 * participate in locale fallback. Now,
544 * - ures_open() works like ures_openDirect() on a bundle with a top-level
545 * type of ":table(nofallback)" _if_ the bundle exists
546 * - ures_open() will continue to find a root bundle if the requested one
547 * does not exist, unlike ures_openDirect()
549 * Test with a different bundle than above to avoid confusion in the cache.
553 * verify that ures_open("casing"), which now has a nofallback declaration,
554 * does not enable fallbacks
556 errorCode
=U_ZERO_ERROR
;
557 casing
=ures_open("testdata", "casing", &errorCode
);
558 if(U_FAILURE(errorCode
)) {
559 log_data_err("ures_open(\"casing\") failed: %s\n", u_errorName(errorCode
));
563 errorCode
=U_ZERO_ERROR
;
564 item
=ures_getByKey(casing
, "Info", NULL
, &errorCode
);
565 if(U_FAILURE(errorCode
)) {
566 log_err("casing.getByKey(Info) failed - %s\n", u_errorName(errorCode
));
571 errorCode
=U_ZERO_ERROR
;
572 item
=ures_getByKey(casing
, "ShortLanguage", NULL
, &errorCode
);
573 if(U_SUCCESS(errorCode
)) {
574 log_err("casing.getByKey(root key) succeeded despite nofallback declaration - %s\n", u_errorName(errorCode
));
580 * verify that ures_open("ne") finds the root bundle but
581 * ures_openDirect("ne") does not
583 errorCode
=U_ZERO_ERROR
;
584 ne
=ures_open("testdata", "ne", &errorCode
);
585 if(U_FAILURE(errorCode
)) {
586 log_data_err("ures_open(\"ne\") failed (expected to get root): %s\n", u_errorName(errorCode
));
588 if(errorCode
!=U_USING_DEFAULT_WARNING
|| 0!=uprv_strcmp("root", ures_getLocale(ne
, &errorCode
))) {
589 log_err("ures_open(\"ne\") found something other than \"root\" - %s\n", u_errorName(errorCode
));
593 errorCode
=U_ZERO_ERROR
;
594 ne
=ures_openDirect("testdata", "ne", &errorCode
);
595 if(U_SUCCESS(errorCode
)) {
596 log_data_err("ures_openDirect(\"ne\") succeeded unexpectedly\n");
600 /* verify that ures_openDirect("te_IN") does not enable fallbacks */
601 errorCode
=U_ZERO_ERROR
;
602 te_IN
=ures_openDirect("testdata", "te_IN", &errorCode
);
603 if(U_FAILURE(errorCode
)) {
604 log_data_err("ures_open(\"te_IN\") failed: %s\n", u_errorName(errorCode
));
607 errorCode
=U_ZERO_ERROR
;
608 item
=ures_getByKey(te_IN
, "ShortLanguage", NULL
, &errorCode
);
609 if(U_SUCCESS(errorCode
)) {
610 log_err("te_IN.getByKey(root key) succeeded despite use of ures_openDirect() - %s\n", u_errorName(errorCode
));
617 parseTable32Key(const char *key
) {
622 while((c
=*key
++)!=0) {
633 static const struct {
637 { "ooooooooooooooooo", 0 },
638 { "oooooooooooooooo1", 1 },
639 { "ooooooooooooooo1o", 2 },
640 { "oo11ooo1ooo11111o", 25150 },
641 { "oo11ooo1ooo111111", 25151 },
642 { "o1111111111111111", 65535 },
643 { "1oooooooooooooooo", 65536 },
644 { "1ooooooo11o11ooo1", 65969 },
645 { "1ooooooo11o11oo1o", 65970 },
646 { "1ooooooo111oo1111", 65999 }
649 /* ### TODO UResourceBundle staticItem={ 0 }; - need to know the size */
650 UResourceBundle
*res
, *item
;
653 UErrorCode errorCode
;
654 int32_t i
, j
, number
, parsedNumber
, length
, count
;
656 errorCode
=U_ZERO_ERROR
;
657 res
=ures_open(loadTestData(&errorCode
), "testtable32", &errorCode
);
658 if(U_FAILURE(errorCode
)) {
659 log_data_err("unable to open testdata/testtable32.res - %s\n", u_errorName(errorCode
));
662 if(ures_getType(res
)!=URES_TABLE
) {
663 log_data_err("testdata/testtable32.res has type %d instead of URES_TABLE\n", ures_getType(res
));
666 count
=ures_getSize(res
);
668 log_err("testdata/testtable32.res should have 66000 entries but has %d\n", count
);
671 /* get the items by index */
673 for(i
=0; i
<count
; ++i
) {
674 item
=ures_getByIndex(res
, i
, item
, &errorCode
);
675 if(U_FAILURE(errorCode
)) {
676 log_err("unable to get item %d of %d in testdata/testtable32.res - %s\n",
677 i
, count
, u_errorName(errorCode
));
681 key
=ures_getKey(item
);
682 parsedNumber
=parseTable32Key(key
);
684 switch(ures_getType(item
)) {
686 s
=ures_getString(item
, &length
, &errorCode
);
687 if(U_FAILURE(errorCode
) || s
==NULL
) {
688 log_err("unable to access the string \"%s\" at %d in testdata/testtable32.res - %s\n",
689 key
, i
, u_errorName(errorCode
));
693 U16_NEXT(s
, j
, length
, number
);
697 number
=ures_getInt(item
, &errorCode
);
698 if(U_FAILURE(errorCode
)) {
699 log_err("unable to access the integer \"%s\" at %d in testdata/testtable32.res - %s\n",
700 key
, i
, u_errorName(errorCode
));
705 log_err("unexpected resource type %d for \"%s\" at %d in testdata/testtable32.res - %s\n",
706 ures_getType(item
), key
, i
, u_errorName(errorCode
));
711 if(number
>=0 && number
!=parsedNumber
) {
712 log_err("\"%s\" at %d in testdata/testtable32.res has a string/int value of %d, expected %d\n",
713 key
, i
, number
, parsedNumber
);
717 /* search for some items by key */
718 for(i
=0; i
<LENGTHOF(testcases
); ++i
) {
719 item
=ures_getByKey(res
, testcases
[i
].key
, item
, &errorCode
);
720 if(U_FAILURE(errorCode
)) {
721 log_err("unable to find the key \"%s\" in testdata/testtable32.res - %s\n",
722 testcases
[i
].key
, u_errorName(errorCode
));
726 switch(ures_getType(item
)) {
728 s
=ures_getString(item
, &length
, &errorCode
);
729 if(U_FAILURE(errorCode
) || s
==NULL
) {
730 log_err("unable to access the string \"%s\" in testdata/testtable32.res - %s\n",
731 testcases
[i
].key
, u_errorName(errorCode
));
735 U16_NEXT(s
, j
, length
, number
);
739 number
=ures_getInt(item
, &errorCode
);
740 if(U_FAILURE(errorCode
)) {
741 log_err("unable to access the integer \"%s\" in testdata/testtable32.res - %s\n",
742 testcases
[i
].key
, u_errorName(errorCode
));
747 log_err("unexpected resource type %d for \"%s\" in testdata/testtable32.res - %s\n",
748 ures_getType(item
), testcases
[i
].key
, u_errorName(errorCode
));
753 if(number
>=0 && number
!=testcases
[i
].number
) {
754 log_err("\"%s\" in testdata/testtable32.res has a string/int value of %d, expected %d\n",
755 testcases
[i
].key
, number
, testcases
[i
].number
);
758 key
=ures_getKey(item
);
759 if(0!=uprv_strcmp(key
, testcases
[i
].key
)) {
760 log_err("\"%s\" in testdata/testtable32.res claims to have the key \"%s\"\n",
761 testcases
[i
].key
, key
);
769 static void TestFileStream(void){
772 UErrorCode status
= U_ZERO_ERROR
;
773 const char* testdatapath
= loadTestData(&status
);
774 char* fileName
= (char*) malloc(uprv_strlen(testdatapath
) +10);
775 FileStream
* stream
= NULL
;
776 /* these should not be closed */
777 FileStream
* pStdin
= T_FileStream_stdin();
778 FileStream
* pStdout
= T_FileStream_stdout();
779 FileStream
* pStderr
= T_FileStream_stderr();
781 const char* testline
= "This is a test line";
782 int32_t bufLen
= (int32_t)strlen(testline
)+10;
783 char* buf
= (char*) malloc(bufLen
);
787 log_err("failed to get T_FileStream_stdin()");
790 log_err("failed to get T_FileStream_stdout()");
793 log_err("failed to get T_FileStream_stderr()");
796 uprv_strcpy(fileName
,testdatapath
);
797 uprv_strcat(fileName
,".dat");
798 stream
= T_FileStream_open(fileName
, "r");
800 log_data_err("T_FileStream_open failed to open %s\n",fileName
);
802 if(!T_FileStream_file_exists(fileName
)){
803 log_data_err("T_FileStream_file_exists failed to verify existence of %s \n",fileName
);
806 retLen
=T_FileStream_read(stream
,&c
,1);
808 log_data_err("T_FileStream_read failed to read from %s \n",fileName
);
811 T_FileStream_rewind(stream
);
812 T_FileStream_read(stream
,&c1
,1);
814 log_data_err("T_FileStream_rewind failed to rewind %s \n",fileName
);
816 T_FileStream_rewind(stream
);
817 c1
= T_FileStream_peek(stream
);
819 log_data_err("T_FileStream_peek failed to peekd %s \n",fileName
);
821 c
= T_FileStream_getc(stream
);
822 T_FileStream_ungetc(c
,stream
);
823 if(c
!= T_FileStream_getc(stream
)){
824 log_data_err("T_FileStream_ungetc failed to d %s \n",fileName
);
827 if(T_FileStream_size(stream
)<=0){
828 log_data_err("T_FileStream_size failed to d %s \n",fileName
);
830 if(T_FileStream_error(stream
)){
831 log_data_err("T_FileStream_error shouldn't have an error %s\n",fileName
);
833 if(!T_FileStream_error(NULL
)){
834 log_err("T_FileStream_error didn't get an error %s\n",fileName
);
836 T_FileStream_putc(stream
, 0x20);
837 if(!T_FileStream_error(stream
)){
840 writing to a read-only file may not consistently fail on all platforms
841 (e.g. HP-UX, FreeBSD, MacOSX)
843 log_verbose("T_FileStream_error didn't get an error when writing to a readonly file %s\n",fileName
);
846 T_FileStream_close(stream
);
848 /* test writing function */
850 uprv_strcpy(fileName
,testdatapath
);
851 uprv_strcat(fileName
,".tmp");
852 stream
= T_FileStream_open(fileName
,"w+");
855 log_data_err("Could not open %s for writing\n",fileName
);
858 T_FileStream_putc(stream
,c
);
859 T_FileStream_rewind(stream
);
860 if(c
!= T_FileStream_getc(stream
)){
861 log_data_err("T_FileStream_putc failed %s\n",fileName
);
864 T_FileStream_rewind(stream
);
865 T_FileStream_writeLine(stream
,testline
);
866 T_FileStream_rewind(stream
);
867 T_FileStream_readLine(stream
,buf
,bufLen
);
868 if(uprv_strncmp(testline
, buf
,uprv_strlen(buf
))!=0){
869 log_data_err("T_FileStream_writeLine failed %s\n",fileName
);
872 T_FileStream_rewind(stream
);
873 T_FileStream_write(stream
,testline
,(int32_t)strlen(testline
));
874 T_FileStream_rewind(stream
);
875 retLen
= T_FileStream_read(stream
, buf
, bufLen
);
876 if(uprv_strncmp(testline
, buf
,retLen
)!=0){
877 log_data_err("T_FileStream_write failed %s\n",fileName
);
880 T_FileStream_close(stream
);
882 if(!T_FileStream_remove(fileName
)){
883 log_data_err("T_FileStream_remove failed to delete %s\n",fileName
);
892 static void TestGetSize(void) {
900 { "integerarray", 1},
903 { "emptystring", 1}, /* empty string is still a string */
907 { "collations", 1}, /* not 2 - there is hidden %%CollationBin */
910 UErrorCode status
= U_ZERO_ERROR
;
912 UResourceBundle
*rb
= NULL
;
913 UResourceBundle
*res
= NULL
;
914 UResourceBundle
*helper
= NULL
;
915 const char* testdatapath
= loadTestData(&status
);
916 int32_t i
= 0, j
= 0;
919 if(U_FAILURE(status
))
921 log_data_err("Could not load testdata.dat %s\n", u_errorName(status
));
925 rb
= ures_open(testdatapath
, "testtypes", &status
);
926 if(U_FAILURE(status
))
928 log_err("Could not testtypes resource bundle %s\n", u_errorName(status
));
932 for(i
= 0; i
< sizeof(test
)/sizeof(test
[0]); i
++) {
933 res
= ures_getByKey(rb
, test
[i
].key
, res
, &status
);
934 if(U_FAILURE(status
))
936 log_err("Couldn't find the key %s. Error: %s\n", test
[i
].key
, u_errorName(status
));
940 size
= ures_getSize(res
);
941 if(size
!= test
[i
].size
) {
942 log_err("Expected size %i, got size %i for key %s\n", test
[i
].size
, size
, test
[i
].key
);
943 for(j
= 0; j
< size
; j
++) {
944 helper
= ures_getByIndex(res
, j
, helper
, &status
);
945 log_err("%s\n", ures_getKey(helper
));
954 static void TestGetLocaleByType(void) {
955 static const struct {
956 const char *requestedLocale
;
957 const char *resourceKey
;
958 const char *validLocale
;
959 const char *actualLocale
;
961 { "te_IN_BLAH", "string_only_in_te_IN", "te_IN", "te_IN" },
962 { "te_IN_BLAH", "string_only_in_te", "te_IN", "te" },
963 { "te_IN_BLAH", "string_only_in_Root", "te_IN", "root" },
964 { "te_IN_BLAH_01234567890_01234567890_01234567890_01234567890_01234567890_01234567890", "array_2d_only_in_Root", "te_IN", "root" },
965 { "te_IN_BLAH@currency=euro", "array_2d_only_in_te_IN", "te_IN", "te_IN" },
966 { "te_IN_BLAH@collation=phonebook;calendar=thai", "array_2d_only_in_te", "te_IN", "te" }
969 UErrorCode status
= U_ZERO_ERROR
;
971 UResourceBundle
*rb
= NULL
;
972 UResourceBundle
*res
= NULL
;
973 const char* testdatapath
= loadTestData(&status
);
975 const char *locale
= NULL
;
977 if(U_FAILURE(status
))
979 log_data_err("Could not load testdata.dat %s\n", u_errorName(status
));
983 for(i
= 0; i
< sizeof(test
)/sizeof(test
[0]); i
++) {
984 rb
= ures_open(testdatapath
, test
[i
].requestedLocale
, &status
);
985 if(U_FAILURE(status
))
987 log_err("Could not open resource bundle %s (error %s)\n", test
[i
].requestedLocale
, u_errorName(status
));
988 status
= U_ZERO_ERROR
;
992 res
= ures_getByKey(rb
, test
[i
].resourceKey
, res
, &status
);
993 if(U_FAILURE(status
))
995 log_err("Couldn't find the key %s. Error: %s\n", test
[i
].resourceKey
, u_errorName(status
));
997 status
= U_ZERO_ERROR
;
1001 locale
= ures_getLocaleByType(res
, ULOC_REQUESTED_LOCALE
, &status
);
1003 log_err("Requested locale should return NULL\n");
1005 locale
= ures_getLocaleByType(res
, ULOC_VALID_LOCALE
, &status
);
1006 if(!locale
|| strcmp(locale
, test
[i
].validLocale
) != 0) {
1007 log_err("Expected valid locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
);
1009 locale
= ures_getLocaleByType(res
, ULOC_ACTUAL_LOCALE
, &status
);
1010 if(!locale
|| strcmp(locale
, test
[i
].actualLocale
) != 0) {
1011 log_err("Expected actual locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
);