1 /********************************************************************
3 * Copyright (c) 1997-2008, 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
, "ExemplarCharacters", &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 UErrorCode status
= U_ZERO_ERROR
;
133 loadTestData(&status
);
134 if(U_FAILURE(status
)) {
135 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
139 testTag("only_in_Root", TRUE
, FALSE
, FALSE
);
140 testTag("in_Root_te", TRUE
, TRUE
, FALSE
);
141 testTag("in_Root_te_te_IN", TRUE
, TRUE
, TRUE
);
142 testTag("in_Root_te_IN", TRUE
, FALSE
, TRUE
);
143 testTag("only_in_te", FALSE
, TRUE
, FALSE
);
144 testTag("only_in_te_IN", FALSE
, FALSE
, TRUE
);
145 testTag("in_te_te_IN", FALSE
, TRUE
, TRUE
);
146 testTag("nonexistent", FALSE
, FALSE
, FALSE
);
148 log_verbose("Passed:= %d Failed= %d \n", pass
, fail
);
151 void TestConstruction1()
153 UResourceBundle
*test1
= 0, *test2
= 0;
154 const UChar
*result1
, *result2
;
158 UErrorCode err
= U_ZERO_ERROR
;
159 const char* testdatapath
;
160 const char* locale
="te_IN";
162 log_verbose("Testing ures_open()......\n");
165 testdatapath
=loadTestData(&err
);
168 log_data_err("Could not load testdata.dat %s \n",myErrorName(err
));
172 test1
=ures_open(testdatapath
, NULL
, &err
);
175 log_err("construction of %s did not succeed : %s \n",NULL
, myErrorName(err
));
180 test2
=ures_open(testdatapath
, locale
, &err
);
183 log_err("construction of %s did not succeed : %s \n",locale
, myErrorName(err
));
186 result1
= ures_getStringByKey(test1
, "string_in_Root_te_te_IN", &resultLen
, &err
);
187 result2
= ures_getStringByKey(test2
, "string_in_Root_te_te_IN", &resultLen
, &err
);
190 if (U_FAILURE(err
)) {
191 log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(err
));
195 u_uastrcpy(temp
, "TE_IN");
197 if(u_strcmp(result2
, temp
)!=0)
201 log_err("Construction test failed for ures_open();\n");
203 log_info("(run verbose for more information)\n");
205 log_verbose("\nGot->");
206 for(n
=0;result2
[n
];n
++)
208 log_verbose("%04X ",result2
[n
]);
212 log_verbose("\nWant>");
215 log_verbose("%04X ",temp
[n
]);
221 log_verbose("for string_in_Root_te_te_IN, default.txt had %s\n", austrdup(result1
));
222 log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", austrdup(result2
));
224 /* Test getVersionNumber*/
225 log_verbose("Testing version number\n");
226 log_verbose("for getVersionNumber : %s\n", ures_getVersionNumber(test1
));
232 /*****************************************************************************/
233 /*****************************************************************************/
235 UBool
testTag(const char* frag
,
240 int32_t passNum
=pass
;
242 /* Make array from input params */
245 const char *NAME
[] = { "ROOT", "TE", "TE_IN" };
247 /* Now try to load the desired items */
248 UResourceBundle
* theBundle
= NULL
;
251 UErrorCode status
= U_ZERO_ERROR
,expected_resource_status
= U_ZERO_ERROR
;
253 UChar
* expected_string
= NULL
;
254 const UChar
* string
= NULL
;
257 int32_t actual_bundle
;
259 const char *testdatapath
= loadTestData(&status
);
265 strcpy(item_tag
, "tag");
267 status
= U_ZERO_ERROR
;
268 theBundle
= ures_open(testdatapath
, "root", &status
);
269 if(U_FAILURE(status
))
271 ures_close(theBundle
);
272 log_err("Couldn't open root bundle in %s", testdatapath
);
275 ures_close(theBundle
);
279 for (i
=0; i
<bundles_count
; ++i
)
281 strcpy(action
,"construction for");
282 strcat(action
, param
[i
].name
);
285 status
= U_ZERO_ERROR
;
287 theBundle
= ures_open(testdatapath
, param
[i
].name
, &status
);
288 /*theBundle = ures_open("c:\\icu\\icu\\source\\test\\testdata\\testdata", param[i].name, &status);*/
290 CONFIRM_ErrorCode(status
,param
[i
].expected_constructor_status
);
295 actual_bundle
= 0; /* ne -> default */
297 actual_bundle
= 1; /* te_NE -> te */
299 actual_bundle
= 2; /* te_IN_NE -> te_IN */
303 expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
304 for (j
=e_te_IN
; j
>=e_Root
; --j
)
306 if (is_in
[j
] && param
[i
].inherits
[j
])
309 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
310 expected_resource_status
= U_ZERO_ERROR
;
312 expected_resource_status
= U_USING_DEFAULT_WARNING
;
314 expected_resource_status
= U_USING_FALLBACK_WARNING
;
316 log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>. actual_bundle=%s\n",
323 param
[i
].inherits
[j
]?"Yes":"No",
324 param
[actual_bundle
].name
);
330 for (j
=param
[i
].where
; j
>=0; --j
)
339 base
=(UChar
*)malloc(sizeof(UChar
)*(strlen(NAME
[j
]) + 1));
340 u_uastrcpy(base
,NAME
[j
]);
349 base
= (UChar
*) malloc(sizeof(UChar
) * 1);
354 /*-------------------------------------------------------------------- */
357 strcpy(tag
,"string_");
360 strcpy(action
,param
[i
].name
);
361 strcat(action
, ".ures_get(" );
367 status
= U_ZERO_ERROR
;
369 ures_getStringByKey(theBundle
, tag
, &resultLen
, &status
);
370 if(U_SUCCESS(status
))
372 status
= U_ZERO_ERROR
;
373 string
=ures_getStringByKey(theBundle
, tag
, &resultLen
, &status
);
376 log_verbose("%s got %d, expected %d\n", action
, status
, expected_resource_status
);
378 CONFIRM_ErrorCode(status
, expected_resource_status
);
381 if(U_SUCCESS(status
)){
382 expected_string
=(UChar
*)malloc(sizeof(UChar
)*(u_strlen(base
) + 3));
383 u_strcpy(expected_string
,base
);
388 expected_string
= (UChar
*)malloc(sizeof(UChar
)*(u_strlen(kERROR
) + 1));
389 u_strcpy(expected_string
,kERROR
);
393 CONFIRM_EQ(string
, expected_string
);
395 free(expected_string
);
396 ures_close(theBundle
);
399 return (UBool
)(passNum
== pass
);
413 * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR
417 static void TestFallback()
419 UErrorCode status
= U_ZERO_ERROR
;
420 UResourceBundle
*fr_FR
= NULL
;
421 UResourceBundle
*subResource
= NULL
;
422 const UChar
*junk
; /* ignored */
425 log_verbose("Opening fr_FR..");
426 fr_FR
= ures_open(NULL
, "fr_FR", &status
);
427 if(U_FAILURE(status
))
429 log_err("Couldn't open fr_FR - %d\n", status
);
433 status
= U_ZERO_ERROR
;
436 /* clear it out.. just do some calls to get the gears turning */
437 junk
= ures_getStringByKey(fr_FR
, "LocaleID", &resultLen
, &status
);
438 status
= U_ZERO_ERROR
;
439 junk
= ures_getStringByKey(fr_FR
, "LocaleString", &resultLen
, &status
);
440 status
= U_ZERO_ERROR
;
441 junk
= ures_getStringByKey(fr_FR
, "LocaleID", &resultLen
, &status
);
442 status
= U_ZERO_ERROR
;
444 /* OK first one. This should be a Default value. */
445 subResource
= ures_getByKey(fr_FR
, "MeasurementSystem", NULL
, &status
);
446 if(status
!= U_USING_DEFAULT_WARNING
)
448 log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get CurrencyMap from fr_FR, got %s\n",
449 u_errorName(status
));
451 ures_close(subResource
);
452 status
= U_ZERO_ERROR
;
454 /* and this is a Fallback, to fr */
455 junk
= ures_getStringByKey(fr_FR
, "Countries", &resultLen
, &status
);
456 if(status
!= U_USING_FALLBACK_WARNING
)
458 log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get Countries from fr_FR, got %s\n",
459 u_errorName(status
));
462 status
= U_ZERO_ERROR
;
468 TestOpenDirect(void) {
469 UResourceBundle
*idna_rules
, *casing
, *te_IN
, *ne
, *item
;
470 UErrorCode errorCode
;
473 * test that ures_openDirect() opens a resource bundle
474 * where one can look up its own items but not fallback items
475 * from root or similar
477 errorCode
=U_ZERO_ERROR
;
478 idna_rules
=ures_openDirect(loadTestData(&errorCode
), "idna_rules", &errorCode
);
479 if(U_FAILURE(errorCode
)) {
480 log_data_err("ures_openDirect(\"idna_rules\") failed: %s\n", u_errorName(errorCode
));
484 if(0!=uprv_strcmp("idna_rules", ures_getLocale(idna_rules
, &errorCode
))) {
485 log_err("ures_openDirect(\"idna_rules\").getLocale()!=idna_rules\n");
487 errorCode
=U_ZERO_ERROR
;
489 /* try an item in idna_rules, must work */
490 item
=ures_getByKey(idna_rules
, "UnassignedSet", NULL
, &errorCode
);
491 if(U_FAILURE(errorCode
)) {
492 log_err("translit_index.getByKey(local key) failed: %s\n", u_errorName(errorCode
));
493 errorCode
=U_ZERO_ERROR
;
498 /* try an item in root, must fail */
499 item
=ures_getByKey(idna_rules
, "ShortLanguage", NULL
, &errorCode
);
500 if(U_FAILURE(errorCode
)) {
501 errorCode
=U_ZERO_ERROR
;
503 log_err("idna_rules.getByKey(root key) succeeded!\n");
506 ures_close(idna_rules
);
508 /* now make sure that "idna_rules" will not work with ures_open() */
509 errorCode
=U_ZERO_ERROR
;
510 idna_rules
=ures_open("testdata", "idna_rules", &errorCode
);
511 if(U_FAILURE(errorCode
) || errorCode
==U_USING_DEFAULT_WARNING
|| errorCode
==U_USING_FALLBACK_WARNING
) {
512 /* falling back to default or root is ok */
513 errorCode
=U_ZERO_ERROR
;
514 } else if(0!=uprv_strcmp("idna_rules", ures_getLocale(idna_rules
, &errorCode
))) {
515 /* Opening this file will work in "files mode" on Windows and the Mac,
516 which have case insensitive file systems */
517 log_err("ures_open(\"idna_rules\") succeeded, should fail! Got: %s\n", u_errorName(errorCode
));
519 ures_close(idna_rules
);
521 /* ures_openDirect("translit_index_WronG") must fail */
522 idna_rules
=ures_openDirect(NULL
, "idna_rules_WronG", &errorCode
);
523 if(U_FAILURE(errorCode
)) {
524 errorCode
=U_ZERO_ERROR
;
526 log_err("ures_openDirect(\"idna_rules_WronG\") succeeded, should fail!\n");
528 ures_close(idna_rules
);
530 errorCode
= U_USING_FALLBACK_WARNING
;;
531 idna_rules
=ures_openDirect("testdata", "idna_rules", &errorCode
);
532 if(U_FAILURE(errorCode
)) {
533 log_data_err("ures_openDirect(\"idna_rules\") failed when U_USING_FALLBACK_WARNING was set prior to call: %s\n", u_errorName(errorCode
));
536 ures_close(idna_rules
);
539 * ICU 3.6 has new resource bundle syntax and data for bundles that do not
540 * participate in locale fallback. Now,
541 * - ures_open() works like ures_openDirect() on a bundle with a top-level
542 * type of ":table(nofallback)" _if_ the bundle exists
543 * - ures_open() will continue to find a root bundle if the requested one
544 * does not exist, unlike ures_openDirect()
546 * Test with a different bundle than above to avoid confusion in the cache.
550 * verify that ures_open("casing"), which now has a nofallback declaration,
551 * does not enable fallbacks
553 errorCode
=U_ZERO_ERROR
;
554 casing
=ures_open("testdata", "casing", &errorCode
);
555 if(U_FAILURE(errorCode
)) {
556 log_data_err("ures_open(\"casing\") failed: %s\n", u_errorName(errorCode
));
560 errorCode
=U_ZERO_ERROR
;
561 item
=ures_getByKey(casing
, "Info", NULL
, &errorCode
);
562 if(U_FAILURE(errorCode
)) {
563 log_err("casing.getByKey(Info) failed - %s\n", u_errorName(errorCode
));
568 errorCode
=U_ZERO_ERROR
;
569 item
=ures_getByKey(casing
, "ShortLanguage", NULL
, &errorCode
);
570 if(U_SUCCESS(errorCode
)) {
571 log_err("casing.getByKey(root key) succeeded despite nofallback declaration - %s\n", u_errorName(errorCode
));
577 * verify that ures_open("ne") finds the root bundle but
578 * ures_openDirect("ne") does not
580 errorCode
=U_ZERO_ERROR
;
581 ne
=ures_open("testdata", "ne", &errorCode
);
582 if(U_FAILURE(errorCode
)) {
583 log_data_err("ures_open(\"ne\") failed (expected to get root): %s\n", u_errorName(errorCode
));
585 if(errorCode
!=U_USING_DEFAULT_WARNING
|| 0!=uprv_strcmp("root", ures_getLocale(ne
, &errorCode
))) {
586 log_err("ures_open(\"ne\") found something other than \"root\" - %s\n", u_errorName(errorCode
));
590 errorCode
=U_ZERO_ERROR
;
591 ne
=ures_openDirect("testdata", "ne", &errorCode
);
592 if(U_SUCCESS(errorCode
)) {
593 log_data_err("ures_openDirect(\"ne\") succeeded unexpectedly\n");
597 /* verify that ures_openDirect("te_IN") does not enable fallbacks */
598 errorCode
=U_ZERO_ERROR
;
599 te_IN
=ures_openDirect("testdata", "te_IN", &errorCode
);
600 if(U_FAILURE(errorCode
)) {
601 log_data_err("ures_open(\"te_IN\") failed: %s\n", u_errorName(errorCode
));
604 errorCode
=U_ZERO_ERROR
;
605 item
=ures_getByKey(te_IN
, "ShortLanguage", NULL
, &errorCode
);
606 if(U_SUCCESS(errorCode
)) {
607 log_err("te_IN.getByKey(root key) succeeded despite use of ures_openDirect() - %s\n", u_errorName(errorCode
));
614 parseTable32Key(const char *key
) {
619 while((c
=*key
++)!=0) {
630 static const struct {
634 { "ooooooooooooooooo", 0 },
635 { "oooooooooooooooo1", 1 },
636 { "ooooooooooooooo1o", 2 },
637 { "oo11ooo1ooo11111o", 25150 },
638 { "oo11ooo1ooo111111", 25151 },
639 { "o1111111111111111", 65535 },
640 { "1oooooooooooooooo", 65536 },
641 { "1ooooooo11o11ooo1", 65969 },
642 { "1ooooooo11o11oo1o", 65970 },
643 { "1ooooooo111oo1111", 65999 }
646 /* ### TODO UResourceBundle staticItem={ 0 }; - need to know the size */
647 UResourceBundle
*res
, *item
;
650 UErrorCode errorCode
;
651 int32_t i
, j
, number
, parsedNumber
, length
, count
;
653 errorCode
=U_ZERO_ERROR
;
654 res
=ures_open(loadTestData(&errorCode
), "testtable32", &errorCode
);
655 if(U_FAILURE(errorCode
)) {
656 log_data_err("unable to open testdata/testtable32.res - %s\n", u_errorName(errorCode
));
659 if(ures_getType(res
)!=URES_TABLE
) {
660 log_data_err("testdata/testtable32.res has type %d instead of URES_TABLE\n", ures_getType(res
));
663 count
=ures_getSize(res
);
665 log_err("testdata/testtable32.res should have 66000 entries but has %d\n", count
);
668 /* get the items by index */
670 for(i
=0; i
<count
; ++i
) {
671 item
=ures_getByIndex(res
, i
, item
, &errorCode
);
672 if(U_FAILURE(errorCode
)) {
673 log_err("unable to get item %d of %d in testdata/testtable32.res - %s\n",
674 i
, count
, u_errorName(errorCode
));
678 key
=ures_getKey(item
);
679 parsedNumber
=parseTable32Key(key
);
681 switch(ures_getType(item
)) {
683 s
=ures_getString(item
, &length
, &errorCode
);
684 if(U_FAILURE(errorCode
) || s
==NULL
) {
685 log_err("unable to access the string \"%s\" at %d in testdata/testtable32.res - %s\n",
686 key
, i
, u_errorName(errorCode
));
690 U16_NEXT(s
, j
, length
, number
);
694 number
=ures_getInt(item
, &errorCode
);
695 if(U_FAILURE(errorCode
)) {
696 log_err("unable to access the integer \"%s\" at %d in testdata/testtable32.res - %s\n",
697 key
, i
, u_errorName(errorCode
));
702 log_err("unexpected resource type %d for \"%s\" at %d in testdata/testtable32.res - %s\n",
703 ures_getType(item
), key
, i
, u_errorName(errorCode
));
708 if(number
>=0 && number
!=parsedNumber
) {
709 log_err("\"%s\" at %d in testdata/testtable32.res has a string/int value of %d, expected %d\n",
710 key
, i
, number
, parsedNumber
);
714 /* search for some items by key */
715 for(i
=0; i
<LENGTHOF(testcases
); ++i
) {
716 item
=ures_getByKey(res
, testcases
[i
].key
, item
, &errorCode
);
717 if(U_FAILURE(errorCode
)) {
718 log_err("unable to find the key \"%s\" in testdata/testtable32.res - %s\n",
719 testcases
[i
].key
, u_errorName(errorCode
));
723 switch(ures_getType(item
)) {
725 s
=ures_getString(item
, &length
, &errorCode
);
726 if(U_FAILURE(errorCode
) || s
==NULL
) {
727 log_err("unable to access the string \"%s\" in testdata/testtable32.res - %s\n",
728 testcases
[i
].key
, u_errorName(errorCode
));
732 U16_NEXT(s
, j
, length
, number
);
736 number
=ures_getInt(item
, &errorCode
);
737 if(U_FAILURE(errorCode
)) {
738 log_err("unable to access the integer \"%s\" in testdata/testtable32.res - %s\n",
739 testcases
[i
].key
, u_errorName(errorCode
));
744 log_err("unexpected resource type %d for \"%s\" in testdata/testtable32.res - %s\n",
745 ures_getType(item
), testcases
[i
].key
, u_errorName(errorCode
));
750 if(number
>=0 && number
!=testcases
[i
].number
) {
751 log_err("\"%s\" in testdata/testtable32.res has a string/int value of %d, expected %d\n",
752 testcases
[i
].key
, number
, testcases
[i
].number
);
755 key
=ures_getKey(item
);
756 if(0!=uprv_strcmp(key
, testcases
[i
].key
)) {
757 log_err("\"%s\" in testdata/testtable32.res claims to have the key \"%s\"\n",
758 testcases
[i
].key
, key
);
766 static void TestFileStream(void){
769 UErrorCode status
= U_ZERO_ERROR
;
770 const char* testdatapath
= loadTestData(&status
);
771 char* fileName
= (char*) malloc(uprv_strlen(testdatapath
) +10);
772 FileStream
* stream
= NULL
;
773 /* these should not be closed */
774 FileStream
* pStdin
= T_FileStream_stdin();
775 FileStream
* pStdout
= T_FileStream_stdout();
776 FileStream
* pStderr
= T_FileStream_stderr();
778 const char* testline
= "This is a test line";
779 int32_t bufLen
= (int32_t)strlen(testline
)+10;
780 char* buf
= (char*) malloc(bufLen
);
784 log_err("failed to get T_FileStream_stdin()");
787 log_err("failed to get T_FileStream_stdout()");
790 log_err("failed to get T_FileStream_stderr()");
793 uprv_strcpy(fileName
,testdatapath
);
794 uprv_strcat(fileName
,".dat");
795 stream
= T_FileStream_open(fileName
, "r");
797 log_data_err("T_FileStream_open failed to open %s\n",fileName
);
799 if(!T_FileStream_file_exists(fileName
)){
800 log_data_err("T_FileStream_file_exists failed to verify existence of %s \n",fileName
);
803 retLen
=T_FileStream_read(stream
,&c
,1);
805 log_data_err("T_FileStream_read failed to read from %s \n",fileName
);
808 T_FileStream_rewind(stream
);
809 T_FileStream_read(stream
,&c1
,1);
811 log_data_err("T_FileStream_rewind failed to rewind %s \n",fileName
);
813 T_FileStream_rewind(stream
);
814 c1
= T_FileStream_peek(stream
);
816 log_data_err("T_FileStream_peek failed to peekd %s \n",fileName
);
818 c
= T_FileStream_getc(stream
);
819 T_FileStream_ungetc(c
,stream
);
820 if(c
!= T_FileStream_getc(stream
)){
821 log_data_err("T_FileStream_ungetc failed to d %s \n",fileName
);
824 if(T_FileStream_size(stream
)<=0){
825 log_data_err("T_FileStream_size failed to d %s \n",fileName
);
827 if(T_FileStream_error(stream
)){
828 log_data_err("T_FileStream_error shouldn't have an error %s\n",fileName
);
830 if(!T_FileStream_error(NULL
)){
831 log_err("T_FileStream_error didn't get an error %s\n",fileName
);
833 T_FileStream_putc(stream
, 0x20);
834 if(!T_FileStream_error(stream
)){
837 writing to a read-only file may not consistently fail on all platforms
838 (e.g. HP-UX, FreeBSD, MacOSX)
840 log_verbose("T_FileStream_error didn't get an error when writing to a readonly file %s\n",fileName
);
843 T_FileStream_close(stream
);
845 /* test writing function */
847 uprv_strcpy(fileName
,testdatapath
);
848 uprv_strcat(fileName
,".tmp");
849 stream
= T_FileStream_open(fileName
,"w+");
852 log_data_err("Could not open %s for writing\n",fileName
);
855 T_FileStream_putc(stream
,c
);
856 T_FileStream_rewind(stream
);
857 if(c
!= T_FileStream_getc(stream
)){
858 log_data_err("T_FileStream_putc failed %s\n",fileName
);
861 T_FileStream_rewind(stream
);
862 T_FileStream_writeLine(stream
,testline
);
863 T_FileStream_rewind(stream
);
864 T_FileStream_readLine(stream
,buf
,bufLen
);
865 if(uprv_strncmp(testline
, buf
,uprv_strlen(buf
))!=0){
866 log_data_err("T_FileStream_writeLine failed %s\n",fileName
);
869 T_FileStream_rewind(stream
);
870 T_FileStream_write(stream
,testline
,(int32_t)strlen(testline
));
871 T_FileStream_rewind(stream
);
872 retLen
= T_FileStream_read(stream
, buf
, bufLen
);
873 if(uprv_strncmp(testline
, buf
,retLen
)!=0){
874 log_data_err("T_FileStream_write failed %s\n",fileName
);
877 T_FileStream_close(stream
);
879 if(!T_FileStream_remove(fileName
)){
880 log_data_err("T_FileStream_remove failed to delete %s\n",fileName
);
889 static void TestGetSize(void) {
897 { "integerarray", 1},
900 { "emptystring", 1}, /* empty string is still a string */
904 { "collations", 1}, /* not 2 - there is hidden %%CollationBin */
907 UErrorCode status
= U_ZERO_ERROR
;
909 UResourceBundle
*rb
= NULL
;
910 UResourceBundle
*res
= NULL
;
911 UResourceBundle
*helper
= NULL
;
912 const char* testdatapath
= loadTestData(&status
);
913 int32_t i
= 0, j
= 0;
916 if(U_FAILURE(status
))
918 log_data_err("Could not load testdata.dat %s\n", u_errorName(status
));
922 rb
= ures_open(testdatapath
, "testtypes", &status
);
923 if(U_FAILURE(status
))
925 log_err("Could not testtypes resource bundle %s\n", u_errorName(status
));
929 for(i
= 0; i
< sizeof(test
)/sizeof(test
[0]); i
++) {
930 res
= ures_getByKey(rb
, test
[i
].key
, res
, &status
);
931 if(U_FAILURE(status
))
933 log_err("Couldn't find the key %s. Error: %s\n", test
[i
].key
, u_errorName(status
));
937 size
= ures_getSize(res
);
938 if(size
!= test
[i
].size
) {
939 log_err("Expected size %i, got size %i for key %s\n", test
[i
].size
, size
, test
[i
].key
);
940 for(j
= 0; j
< size
; j
++) {
941 helper
= ures_getByIndex(res
, j
, helper
, &status
);
942 log_err("%s\n", ures_getKey(helper
));
951 static void TestGetLocaleByType(void) {
952 static const struct {
953 const char *requestedLocale
;
954 const char *resourceKey
;
955 const char *validLocale
;
956 const char *actualLocale
;
958 { "te_IN_BLAH", "string_only_in_te_IN", "te_IN", "te_IN" },
959 { "te_IN_BLAH", "string_only_in_te", "te_IN", "te" },
960 { "te_IN_BLAH", "string_only_in_Root", "te_IN", "root" },
961 { "te_IN_BLAH_01234567890_01234567890_01234567890_01234567890_01234567890_01234567890", "array_2d_only_in_Root", "te_IN", "root" },
962 { "te_IN_BLAH@currency=euro", "array_2d_only_in_te_IN", "te_IN", "te_IN" },
963 { "te_IN_BLAH@collation=phonebook;calendar=thai", "array_2d_only_in_te", "te_IN", "te" }
966 UErrorCode status
= U_ZERO_ERROR
;
968 UResourceBundle
*rb
= NULL
;
969 UResourceBundle
*res
= NULL
;
970 const char* testdatapath
= loadTestData(&status
);
972 const char *locale
= NULL
;
974 if(U_FAILURE(status
))
976 log_data_err("Could not load testdata.dat %s\n", u_errorName(status
));
980 for(i
= 0; i
< sizeof(test
)/sizeof(test
[0]); i
++) {
981 rb
= ures_open(testdatapath
, test
[i
].requestedLocale
, &status
);
982 if(U_FAILURE(status
))
984 log_err("Could not open resource bundle %s (error %s)\n", test
[i
].requestedLocale
, u_errorName(status
));
985 status
= U_ZERO_ERROR
;
989 res
= ures_getByKey(rb
, test
[i
].resourceKey
, res
, &status
);
990 if(U_FAILURE(status
))
992 log_err("Couldn't find the key %s. Error: %s\n", test
[i
].resourceKey
, u_errorName(status
));
994 status
= U_ZERO_ERROR
;
998 locale
= ures_getLocaleByType(res
, ULOC_REQUESTED_LOCALE
, &status
);
1000 log_err("Requested locale should return NULL\n");
1002 locale
= ures_getLocaleByType(res
, ULOC_VALID_LOCALE
, &status
);
1003 if(!locale
|| strcmp(locale
, test
[i
].validLocale
) != 0) {
1004 log_err("Expected valid locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
);
1006 locale
= ures_getLocaleByType(res
, ULOC_ACTUAL_LOCALE
, &status
);
1007 if(!locale
|| strcmp(locale
, test
[i
].actualLocale
) != 0) {
1008 log_err("Expected actual locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
);