1 /********************************************************************
3 * Copyright (c) 1997-2016, 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"
26 #define RESTEST_HEAP_CHECK 0
28 #include "unicode/ures.h"
30 #include "unicode/ctest.h"
32 static void TestOpenDirect(void);
33 static void TestFallback(void);
34 static void TestTable32(void);
35 static void TestFileStream(void);
36 /*****************************************************************************/
38 const UChar kERROR
[] = { 0x0045 /*E*/, 0x0052 /*'R'*/, 0x0052 /*'R'*/,
39 0x004F /*'O'*/, 0x0052/*'R'*/, 0x0000 /*'\0'*/};
41 /*****************************************************************************/
50 typedef enum E_Where E_Where
;
51 /*****************************************************************************/
53 #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)); }
55 #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)); }
58 /* Array of our test objects */
63 UErrorCode expected_constructor_status
;
65 UBool like
[e_Where_count
];
66 UBool inherits
[e_Where_count
];
70 /* "IN" means inherits */
71 /* "NE" or "ne" means "does not exist" */
73 { "root", U_ZERO_ERROR
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} },
74 { "te", U_ZERO_ERROR
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
75 { "te_IN", U_ZERO_ERROR
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
76 { "te_NE", U_USING_FALLBACK_WARNING
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
77 { "te_IN_NE", U_USING_FALLBACK_WARNING
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
78 { "ne", U_USING_DEFAULT_WARNING
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} }
81 static int32_t bundles_count
= UPRV_LENGTHOF(param
);
85 /***************************************************************************************/
87 /* Array of our test objects */
89 void addResourceBundleTest(TestNode
** root
);
91 void addResourceBundleTest(TestNode
** root
)
93 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
94 addTest(root
, &TestConstruction1
, "tsutil/crestst/TestConstruction1");
95 addTest(root
, &TestOpenDirect
, "tsutil/crestst/TestOpenDirect");
96 addTest(root
, &TestResourceBundles
, "tsutil/crestst/TestResourceBundles");
97 addTest(root
, &TestTable32
, "tsutil/crestst/TestTable32");
98 addTest(root
, &TestFileStream
, "tsutil/crestst/TestFileStream");
99 addTest(root
, &TestGetSize
, "tsutil/crestst/TestGetSize");
100 addTest(root
, &TestGetLocaleByType
, "tsutil/crestst/TestGetLocaleByType");
102 addTest(root
, &TestFallback
, "tsutil/crestst/TestFallback");
103 addTest(root
, &TestAliasConflict
, "tsutil/crestst/TestAliasConflict");
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_status(status
, "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_status(status
, "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");
202 if(!getTestOption(VERBOSITY_OPTION
))
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_status(status
, "Couldn't open fr_FR - %s\n", u_errorName(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 (void)junk
; /* Suppress set but not used warning. */
439 status
= U_ZERO_ERROR
;
440 junk
= ures_getStringByKey(fr_FR
, "LocaleString", &resultLen
, &status
);
441 status
= U_ZERO_ERROR
;
442 junk
= ures_getStringByKey(fr_FR
, "LocaleID", &resultLen
, &status
);
443 status
= U_ZERO_ERROR
;
445 /* OK first one. This should be a Default value. */
446 subResource
= ures_getByKey(fr_FR
, "layout", NULL
, &status
);
447 if(status
!= U_USING_DEFAULT_WARNING
)
449 log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get layout from fr_FR, got %s\n",
450 u_errorName(status
));
452 ures_close(subResource
);
453 status
= U_ZERO_ERROR
;
455 /* and this is a Fallback, to fr */
456 junk
= ures_getStringByKey(fr_FR
, "ExemplarCharacters", &resultLen
, &status
);
457 if(status
!= U_USING_FALLBACK_WARNING
)
459 log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get ExemplarCharacters from fr_FR, got %s\n",
460 u_errorName(status
));
463 status
= U_ZERO_ERROR
;
469 TestOpenDirect(void) {
470 UResourceBundle
*idna_rules
, *casing
, *te_IN
, *ne
, *item
;
471 UErrorCode errorCode
;
474 * test that ures_openDirect() opens a resource bundle
475 * where one can look up its own items but not fallback items
476 * from root or similar
478 errorCode
=U_ZERO_ERROR
;
479 idna_rules
=ures_openDirect(loadTestData(&errorCode
), "idna_rules", &errorCode
);
480 if(U_FAILURE(errorCode
)) {
481 log_data_err("ures_openDirect(\"idna_rules\") failed: %s\n", u_errorName(errorCode
));
485 if(0!=uprv_strcmp("idna_rules", ures_getLocale(idna_rules
, &errorCode
))) {
486 log_err("ures_openDirect(\"idna_rules\").getLocale()!=idna_rules\n");
488 errorCode
=U_ZERO_ERROR
;
490 /* try an item in idna_rules, must work */
491 item
=ures_getByKey(idna_rules
, "UnassignedSet", NULL
, &errorCode
);
492 if(U_FAILURE(errorCode
)) {
493 log_err("translit_index.getByKey(local key) failed: %s\n", u_errorName(errorCode
));
494 errorCode
=U_ZERO_ERROR
;
499 /* try an item in root, must fail */
500 item
=ures_getByKey(idna_rules
, "ShortLanguage", NULL
, &errorCode
);
501 if(U_FAILURE(errorCode
)) {
502 errorCode
=U_ZERO_ERROR
;
504 log_err("idna_rules.getByKey(root key) succeeded!\n");
507 ures_close(idna_rules
);
509 /* now make sure that "idna_rules" will not work with ures_open() */
510 errorCode
=U_ZERO_ERROR
;
511 idna_rules
=ures_open("testdata", "idna_rules", &errorCode
);
512 if(U_FAILURE(errorCode
) || errorCode
==U_USING_DEFAULT_WARNING
|| errorCode
==U_USING_FALLBACK_WARNING
) {
513 /* falling back to default or root is ok */
514 errorCode
=U_ZERO_ERROR
;
515 } else if(0!=uprv_strcmp("idna_rules", ures_getLocale(idna_rules
, &errorCode
))) {
516 /* Opening this file will work in "files mode" on Windows and the Mac,
517 which have case insensitive file systems */
518 log_err("ures_open(\"idna_rules\") succeeded, should fail! Got: %s\n", u_errorName(errorCode
));
520 ures_close(idna_rules
);
522 /* ures_openDirect("translit_index_WronG") must fail */
523 idna_rules
=ures_openDirect(NULL
, "idna_rules_WronG", &errorCode
);
524 if(U_FAILURE(errorCode
)) {
525 errorCode
=U_ZERO_ERROR
;
527 log_err("ures_openDirect(\"idna_rules_WronG\") succeeded, should fail!\n");
529 ures_close(idna_rules
);
531 errorCode
= U_USING_FALLBACK_WARNING
;;
532 idna_rules
=ures_openDirect("testdata", "idna_rules", &errorCode
);
533 if(U_FAILURE(errorCode
)) {
534 log_data_err("ures_openDirect(\"idna_rules\") failed when U_USING_FALLBACK_WARNING was set prior to call: %s\n", u_errorName(errorCode
));
537 ures_close(idna_rules
);
540 * ICU 3.6 has new resource bundle syntax and data for bundles that do not
541 * participate in locale fallback. Now,
542 * - ures_open() works like ures_openDirect() on a bundle with a top-level
543 * type of ":table(nofallback)" _if_ the bundle exists
544 * - ures_open() will continue to find a root bundle if the requested one
545 * does not exist, unlike ures_openDirect()
547 * Test with a different bundle than above to avoid confusion in the cache.
551 * verify that ures_open("casing"), which now has a nofallback declaration,
552 * does not enable fallbacks
554 errorCode
=U_ZERO_ERROR
;
555 casing
=ures_open("testdata", "casing", &errorCode
);
556 if(U_FAILURE(errorCode
)) {
557 log_data_err("ures_open(\"casing\") failed: %s\n", u_errorName(errorCode
));
561 errorCode
=U_ZERO_ERROR
;
562 item
=ures_getByKey(casing
, "Info", NULL
, &errorCode
);
563 if(U_FAILURE(errorCode
)) {
564 log_err("casing.getByKey(Info) failed - %s\n", u_errorName(errorCode
));
569 errorCode
=U_ZERO_ERROR
;
570 item
=ures_getByKey(casing
, "ShortLanguage", NULL
, &errorCode
);
571 if(U_SUCCESS(errorCode
)) {
572 log_err("casing.getByKey(root key) succeeded despite nofallback declaration - %s\n", u_errorName(errorCode
));
578 * verify that ures_open("ne") finds the root bundle but
579 * ures_openDirect("ne") does not
581 errorCode
=U_ZERO_ERROR
;
582 ne
=ures_open("testdata", "ne", &errorCode
);
583 if(U_FAILURE(errorCode
)) {
584 log_data_err("ures_open(\"ne\") failed (expected to get root): %s\n", u_errorName(errorCode
));
586 if(errorCode
!=U_USING_DEFAULT_WARNING
|| 0!=uprv_strcmp("root", ures_getLocale(ne
, &errorCode
))) {
587 log_err("ures_open(\"ne\") found something other than \"root\" - %s\n", u_errorName(errorCode
));
591 errorCode
=U_ZERO_ERROR
;
592 ne
=ures_openDirect("testdata", "ne", &errorCode
);
593 if(U_SUCCESS(errorCode
)) {
594 log_data_err("ures_openDirect(\"ne\") succeeded unexpectedly\n");
598 /* verify that ures_openDirect("te_IN") does not enable fallbacks */
599 errorCode
=U_ZERO_ERROR
;
600 te_IN
=ures_openDirect("testdata", "te_IN", &errorCode
);
601 if(U_FAILURE(errorCode
)) {
602 log_data_err("ures_open(\"te_IN\") failed: %s\n", u_errorName(errorCode
));
605 errorCode
=U_ZERO_ERROR
;
606 item
=ures_getByKey(te_IN
, "ShortLanguage", NULL
, &errorCode
);
607 if(U_SUCCESS(errorCode
)) {
608 log_err("te_IN.getByKey(root key) succeeded despite use of ures_openDirect() - %s\n", u_errorName(errorCode
));
615 parseTable32Key(const char *key
) {
620 while((c
=*key
++)!=0) {
631 static const struct {
635 { "ooooooooooooooooo", 0 },
636 { "oooooooooooooooo1", 1 },
637 { "ooooooooooooooo1o", 2 },
638 { "oo11ooo1ooo11111o", 25150 },
639 { "oo11ooo1ooo111111", 25151 },
640 { "o1111111111111111", 65535 },
641 { "1oooooooooooooooo", 65536 },
642 { "1ooooooo11o11ooo1", 65969 },
643 { "1ooooooo11o11oo1o", 65970 },
644 { "1ooooooo111oo1111", 65999 }
647 /* ### TODO UResourceBundle staticItem={ 0 }; - need to know the size */
648 UResourceBundle
*res
, *item
;
651 UErrorCode errorCode
;
652 int32_t i
, j
, number
, parsedNumber
, length
, count
;
654 errorCode
=U_ZERO_ERROR
;
655 res
=ures_open(loadTestData(&errorCode
), "testtable32", &errorCode
);
656 if(U_FAILURE(errorCode
)) {
657 log_data_err("unable to open testdata/testtable32.res - %s\n", u_errorName(errorCode
));
660 if(ures_getType(res
)!=URES_TABLE
) {
661 log_data_err("testdata/testtable32.res has type %d instead of URES_TABLE\n", ures_getType(res
));
664 count
=ures_getSize(res
);
666 log_err("testdata/testtable32.res should have 66000 entries but has %d\n", count
);
669 /* get the items by index */
671 for(i
=0; i
<count
; ++i
) {
672 item
=ures_getByIndex(res
, i
, item
, &errorCode
);
673 if(U_FAILURE(errorCode
)) {
674 log_err("unable to get item %d of %d in testdata/testtable32.res - %s\n",
675 i
, count
, u_errorName(errorCode
));
679 key
=ures_getKey(item
);
680 parsedNumber
=parseTable32Key(key
);
682 switch(ures_getType(item
)) {
684 s
=ures_getString(item
, &length
, &errorCode
);
685 if(U_FAILURE(errorCode
) || s
==NULL
) {
686 log_err("unable to access the string \"%s\" at %d in testdata/testtable32.res - %s\n",
687 key
, i
, u_errorName(errorCode
));
691 U16_NEXT(s
, j
, length
, number
);
695 number
=ures_getInt(item
, &errorCode
);
696 if(U_FAILURE(errorCode
)) {
697 log_err("unable to access the integer \"%s\" at %d in testdata/testtable32.res - %s\n",
698 key
, i
, u_errorName(errorCode
));
703 log_err("unexpected resource type %d for \"%s\" at %d in testdata/testtable32.res - %s\n",
704 ures_getType(item
), key
, i
, u_errorName(errorCode
));
709 if(number
>=0 && number
!=parsedNumber
) {
710 log_err("\"%s\" at %d in testdata/testtable32.res has a string/int value of %d, expected %d\n",
711 key
, i
, number
, parsedNumber
);
715 /* search for some items by key */
716 for(i
=0; i
<UPRV_LENGTHOF(testcases
); ++i
) {
717 item
=ures_getByKey(res
, testcases
[i
].key
, item
, &errorCode
);
718 if(U_FAILURE(errorCode
)) {
719 log_err("unable to find the key \"%s\" in testdata/testtable32.res - %s\n",
720 testcases
[i
].key
, u_errorName(errorCode
));
724 switch(ures_getType(item
)) {
726 s
=ures_getString(item
, &length
, &errorCode
);
727 if(U_FAILURE(errorCode
) || s
==NULL
) {
728 log_err("unable to access the string \"%s\" in testdata/testtable32.res - %s\n",
729 testcases
[i
].key
, u_errorName(errorCode
));
733 U16_NEXT(s
, j
, length
, number
);
737 number
=ures_getInt(item
, &errorCode
);
738 if(U_FAILURE(errorCode
)) {
739 log_err("unable to access the integer \"%s\" in testdata/testtable32.res - %s\n",
740 testcases
[i
].key
, u_errorName(errorCode
));
745 log_err("unexpected resource type %d for \"%s\" in testdata/testtable32.res - %s\n",
746 ures_getType(item
), testcases
[i
].key
, u_errorName(errorCode
));
751 if(number
>=0 && number
!=testcases
[i
].number
) {
752 log_err("\"%s\" in testdata/testtable32.res has a string/int value of %d, expected %d\n",
753 testcases
[i
].key
, number
, testcases
[i
].number
);
756 key
=ures_getKey(item
);
757 if(0!=uprv_strcmp(key
, testcases
[i
].key
)) {
758 log_err("\"%s\" in testdata/testtable32.res claims to have the key \"%s\"\n",
759 testcases
[i
].key
, key
);
767 static void TestFileStream(void){
770 UErrorCode status
= U_ZERO_ERROR
;
771 const char* testdatapath
= loadTestData(&status
);
772 char* fileName
= (char*) malloc(uprv_strlen(testdatapath
) +10);
773 FileStream
* stream
= NULL
;
774 /* these should not be closed */
775 FileStream
* pStdin
= T_FileStream_stdin();
776 FileStream
* pStdout
= T_FileStream_stdout();
777 FileStream
* pStderr
= T_FileStream_stderr();
779 const char* testline
= "This is a test line";
780 int32_t bufLen
= (int32_t)strlen(testline
)+10;
781 char* buf
= (char*) malloc(bufLen
);
785 log_err("failed to get T_FileStream_stdin()");
788 log_err("failed to get T_FileStream_stdout()");
791 log_err("failed to get T_FileStream_stderr()");
794 uprv_strcpy(fileName
,testdatapath
);
795 uprv_strcat(fileName
,".dat");
796 stream
= T_FileStream_open(fileName
, "r");
798 log_data_err("T_FileStream_open failed to open %s\n",fileName
);
800 if(!T_FileStream_file_exists(fileName
)){
801 log_data_err("T_FileStream_file_exists failed to verify existence of %s \n",fileName
);
804 retLen
=T_FileStream_read(stream
,&c
,1);
806 log_data_err("T_FileStream_read failed to read from %s \n",fileName
);
809 T_FileStream_rewind(stream
);
810 T_FileStream_read(stream
,&c1
,1);
812 log_data_err("T_FileStream_rewind failed to rewind %s \n",fileName
);
814 T_FileStream_rewind(stream
);
815 c1
= T_FileStream_peek(stream
);
817 log_data_err("T_FileStream_peek failed to peekd %s \n",fileName
);
819 c
= T_FileStream_getc(stream
);
820 T_FileStream_ungetc(c
,stream
);
821 if(c
!= T_FileStream_getc(stream
)){
822 log_data_err("T_FileStream_ungetc failed to d %s \n",fileName
);
825 if(T_FileStream_size(stream
)<=0){
826 log_data_err("T_FileStream_size failed to d %s \n",fileName
);
828 if(T_FileStream_error(stream
)){
829 log_data_err("T_FileStream_error shouldn't have an error %s\n",fileName
);
831 if(!T_FileStream_error(NULL
)){
832 log_err("T_FileStream_error didn't get an error %s\n",fileName
);
834 T_FileStream_putc(stream
, 0x20);
835 if(!T_FileStream_error(stream
)){
838 writing to a read-only file may not consistently fail on all platforms
839 (e.g. HP-UX, FreeBSD, MacOSX)
841 log_verbose("T_FileStream_error didn't get an error when writing to a readonly file %s\n",fileName
);
844 T_FileStream_close(stream
);
846 /* test writing function */
848 uprv_strcpy(fileName
,testdatapath
);
849 uprv_strcat(fileName
,".tmp");
850 stream
= T_FileStream_open(fileName
,"w+");
853 log_data_err("Could not open %s for writing\n",fileName
);
856 T_FileStream_putc(stream
,c
);
857 T_FileStream_rewind(stream
);
858 if(c
!= T_FileStream_getc(stream
)){
859 log_data_err("T_FileStream_putc failed %s\n",fileName
);
862 T_FileStream_rewind(stream
);
863 T_FileStream_writeLine(stream
,testline
);
864 T_FileStream_rewind(stream
);
865 T_FileStream_readLine(stream
,buf
,bufLen
);
866 if(uprv_strncmp(testline
, buf
,uprv_strlen(buf
))!=0){
867 log_data_err("T_FileStream_writeLine failed %s\n",fileName
);
870 T_FileStream_rewind(stream
);
871 T_FileStream_write(stream
,testline
,(int32_t)strlen(testline
));
872 T_FileStream_rewind(stream
);
873 retLen
= T_FileStream_read(stream
, buf
, bufLen
);
874 if(uprv_strncmp(testline
, buf
,retLen
)!=0){
875 log_data_err("T_FileStream_write failed %s\n",fileName
);
878 T_FileStream_close(stream
);
880 if(!T_FileStream_remove(fileName
)){
881 log_data_err("T_FileStream_remove failed to delete %s\n",fileName
);
890 static void TestGetSize(void) {
898 { "integerarray", 1},
901 { "emptystring", 1}, /* empty string is still a string */
905 { "collations", 1}, /* not 2 - there is hidden %%CollationBin */
908 UErrorCode status
= U_ZERO_ERROR
;
910 UResourceBundle
*rb
= NULL
;
911 UResourceBundle
*res
= NULL
;
912 UResourceBundle
*helper
= NULL
;
913 const char* testdatapath
= loadTestData(&status
);
914 int32_t i
= 0, j
= 0;
917 if(U_FAILURE(status
))
919 log_data_err("Could not load testdata.dat %s\n", u_errorName(status
));
923 rb
= ures_open(testdatapath
, "testtypes", &status
);
924 if(U_FAILURE(status
))
926 log_err("Could not testtypes resource bundle %s\n", u_errorName(status
));
930 for(i
= 0; i
< UPRV_LENGTHOF(test
); i
++) {
931 res
= ures_getByKey(rb
, test
[i
].key
, res
, &status
);
932 if(U_FAILURE(status
))
934 log_err("Couldn't find the key %s. Error: %s\n", test
[i
].key
, u_errorName(status
));
938 size
= ures_getSize(res
);
939 if(size
!= test
[i
].size
) {
940 log_err("Expected size %i, got size %i for key %s\n", test
[i
].size
, size
, test
[i
].key
);
941 for(j
= 0; j
< size
; j
++) {
942 helper
= ures_getByIndex(res
, j
, helper
, &status
);
943 log_err("%s\n", ures_getKey(helper
));
952 static void TestGetLocaleByType(void) {
953 static const struct {
954 const char *requestedLocale
;
955 const char *resourceKey
;
956 const char *validLocale
;
957 const char *actualLocale
;
959 { "te_IN_BLAH", "string_only_in_te_IN", "te_IN", "te_IN" },
960 { "te_IN_BLAH", "string_only_in_te", "te_IN", "te" },
961 { "te_IN_BLAH", "string_only_in_Root", "te_IN", "root" },
962 { "te_IN_BLAH_01234567890_01234567890_01234567890_01234567890_01234567890_01234567890", "array_2d_only_in_Root", "te_IN", "root" },
963 { "te_IN_BLAH@currency=euro", "array_2d_only_in_te_IN", "te_IN", "te_IN" },
964 { "te_IN_BLAH@collation=phonebook;calendar=thai", "array_2d_only_in_te", "te_IN", "te" }
967 UErrorCode status
= U_ZERO_ERROR
;
969 UResourceBundle
*rb
= NULL
;
970 UResourceBundle
*res
= NULL
;
971 const char* testdatapath
= loadTestData(&status
);
973 const char *locale
= NULL
;
975 if(U_FAILURE(status
))
977 log_data_err("Could not load testdata.dat %s\n", u_errorName(status
));
981 for(i
= 0; i
< UPRV_LENGTHOF(test
); i
++) {
982 rb
= ures_open(testdatapath
, test
[i
].requestedLocale
, &status
);
983 if(U_FAILURE(status
))
985 log_err("Could not open resource bundle %s (error %s)\n", test
[i
].requestedLocale
, u_errorName(status
));
986 status
= U_ZERO_ERROR
;
990 res
= ures_getByKey(rb
, test
[i
].resourceKey
, res
, &status
);
991 if(U_FAILURE(status
))
993 log_err("Couldn't find the key %s. Error: %s\n", test
[i
].resourceKey
, u_errorName(status
));
995 status
= U_ZERO_ERROR
;
999 locale
= ures_getLocaleByType(res
, ULOC_REQUESTED_LOCALE
, &status
);
1000 if(U_SUCCESS(status
) && locale
!= NULL
) {
1001 log_err("Requested locale should return NULL\n");
1003 status
= U_ZERO_ERROR
;
1004 locale
= ures_getLocaleByType(res
, ULOC_VALID_LOCALE
, &status
);
1005 if(!locale
|| strcmp(locale
, test
[i
].validLocale
) != 0) {
1006 log_err("Expected valid locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
);
1008 locale
= ures_getLocaleByType(res
, ULOC_ACTUAL_LOCALE
, &status
);
1009 if(!locale
|| strcmp(locale
, test
[i
].actualLocale
) != 0) {
1010 log_err("Expected actual locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
);