1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8 /*******************************************************************************
12 * Modification History:
14 * Madhu Katragadda Ported for C API
15 * 06/14/99 stephen Updated for RB API changes (no suffix).
16 ********************************************************************************
20 #include "unicode/utypes.h"
22 #include "unicode/ustring.h"
28 #define RESTEST_HEAP_CHECK 0
30 #include "unicode/ures.h"
32 #include "unicode/ctest.h"
34 static void TestOpenDirect(void);
35 static void TestFallback(void);
36 static void TestTable32(void);
37 static void TestFileStream(void);
38 /*****************************************************************************/
40 const UChar kERROR[] = { 0x0045 /*E*/, 0x0052 /*'R'*/, 0x0052 /*'R'*/,
41 0x004F /*'O'*/, 0x0052/*'R'*/, 0x0000 /*'\0'*/};
43 /*****************************************************************************/
52 typedef enum E_Where E_Where;
53 /*****************************************************************************/
55 #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)); }
57 #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)); }
60 /* Array of our test objects */
65 UErrorCode expected_constructor_status;
67 UBool like[e_Where_count];
68 UBool inherits[e_Where_count];
72 /* "IN" means inherits */
73 /* "NE" or "ne" means "does not exist" */
75 { "root", U_ZERO_ERROR, e_Root, { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } },
76 { "te", U_ZERO_ERROR, e_te, { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } },
77 { "te_IN", U_ZERO_ERROR, e_te_IN, { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } },
78 { "te_NE", U_USING_FALLBACK_WARNING, e_te, { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } },
79 { "te_IN_NE", U_USING_FALLBACK_WARNING, e_te_IN, { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } },
80 { "ne", U_USING_DEFAULT_WARNING, e_Root, { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } }
83 static int32_t bundles_count = UPRV_LENGTHOF(param);
87 /***************************************************************************************/
89 /* Array of our test objects */
91 void addResourceBundleTest(TestNode** root);
93 void addResourceBundleTest(TestNode** root)
95 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
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, &TestFileStream, "tsutil/crestst/TestFileStream");
101 addTest(root, &TestGetSize, "tsutil/crestst/TestGetSize");
102 addTest(root, &TestGetLocaleByType, "tsutil/crestst/TestGetLocaleByType");
104 addTest(root, &TestFallback, "tsutil/crestst/TestFallback");
105 addTest(root, &TestAliasConflict, "tsutil/crestst/TestAliasConflict");
110 /***************************************************************************************/
111 void TestAliasConflict(void) {
112 UErrorCode status = U_ZERO_ERROR;
113 UResourceBundle *he = NULL;
114 UResourceBundle *iw = NULL;
115 const UChar *result = NULL;
118 he = ures_open(NULL, "he", &status);
119 iw = ures_open(NULL, "iw", &status);
120 if(U_FAILURE(status)) {
121 log_err_status(status, "Failed to get resource with %s\n", myErrorName(status));
124 result = ures_getStringByKey(he, "ExemplarCharacters", &resultLen, &status);
125 if(U_FAILURE(status) || result == NULL) {
126 log_err_status(status, "Failed to get resource with %s\n", myErrorName(status));
132 void TestResourceBundles()
134 UErrorCode status = U_ZERO_ERROR;
135 loadTestData(&status);
136 if(U_FAILURE(status)) {
137 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status));
141 testTag("only_in_Root", TRUE, FALSE, FALSE);
142 testTag("in_Root_te", TRUE, TRUE, FALSE);
143 testTag("in_Root_te_te_IN", TRUE, TRUE, TRUE);
144 testTag("in_Root_te_IN", TRUE, FALSE, TRUE);
145 testTag("only_in_te", FALSE, TRUE, FALSE);
146 testTag("only_in_te_IN", FALSE, FALSE, TRUE);
147 testTag("in_te_te_IN", FALSE, TRUE, TRUE);
148 testTag("nonexistent", FALSE, FALSE, FALSE);
150 log_verbose("Passed:= %d Failed= %d \n", pass, fail);
153 void TestConstruction1()
155 UResourceBundle *test1 = 0, *test2 = 0;
156 const UChar *result1, *result2;
160 UErrorCode err = U_ZERO_ERROR;
161 const char* testdatapath ;
162 const char* locale="te_IN";
164 log_verbose("Testing ures_open()......\n");
167 testdatapath=loadTestData(&err);
170 log_data_err("Could not load testdata.dat %s \n",myErrorName(err));
174 test1=ures_open(testdatapath, NULL, &err);
177 log_err("construction of %s did not succeed : %s \n",NULL, myErrorName(err));
182 test2=ures_open(testdatapath, locale, &err);
185 log_err("construction of %s did not succeed : %s \n",locale, myErrorName(err));
188 result1= ures_getStringByKey(test1, "string_in_Root_te_te_IN", &resultLen, &err);
189 result2= ures_getStringByKey(test2, "string_in_Root_te_te_IN", &resultLen, &err);
192 if (U_FAILURE(err)) {
193 log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(err));
197 u_uastrcpy(temp, "TE_IN");
199 if(u_strcmp(result2, temp)!=0)
203 log_err("Construction test failed for ures_open();\n");
204 if(!getTestOption(VERBOSITY_OPTION))
205 log_info("(run verbose for more information)\n");
207 log_verbose("\nGot->");
208 for(n=0;result2[n];n++)
210 log_verbose("%04X ",result2[n]);
214 log_verbose("\nWant>");
217 log_verbose("%04X ",temp[n]);
223 log_verbose("for string_in_Root_te_te_IN, default.txt had %s\n", austrdup(result1));
224 log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", austrdup(result2));
226 /* Test getVersionNumber*/
227 log_verbose("Testing version number\n");
228 log_verbose("for getVersionNumber : %s\n", ures_getVersionNumber(test1));
234 /*****************************************************************************/
235 /*****************************************************************************/
237 UBool testTag(const char* frag,
242 int32_t passNum=pass;
244 /* Make array from input params */
247 const char *NAME[] = { "ROOT", "TE", "TE_IN" };
249 /* Now try to load the desired items */
250 UResourceBundle* theBundle = NULL;
253 UErrorCode status = U_ZERO_ERROR,expected_resource_status = U_ZERO_ERROR;
255 UChar* expected_string = NULL;
256 const UChar* string = NULL;
259 int32_t actual_bundle;
261 const char *testdatapath = loadTestData(&status);
267 strcpy(item_tag, "tag");
269 status = U_ZERO_ERROR;
270 theBundle = ures_open(testdatapath, "root", &status);
271 if(U_FAILURE(status))
273 ures_close(theBundle);
274 log_err("Couldn't open root bundle in %s", testdatapath);
277 ures_close(theBundle);
281 for (i=0; i<bundles_count; ++i)
283 strcpy(action,"construction for");
284 strcat(action, param[i].name);
287 status = U_ZERO_ERROR;
289 theBundle = ures_open(testdatapath, param[i].name, &status);
290 /*theBundle = ures_open("c:\\icu\\icu\\source\\test\\testdata\\testdata", param[i].name, &status);*/
292 CONFIRM_ErrorCode(status,param[i].expected_constructor_status);
297 actual_bundle = 0; /* ne -> default */
299 actual_bundle = 1; /* te_NE -> te */
301 actual_bundle = 2; /* te_IN_NE -> te_IN */
305 expected_resource_status = U_MISSING_RESOURCE_ERROR;
306 for (j=e_te_IN; j>=e_Root; --j)
308 if (is_in[j] && param[i].inherits[j])
311 if(j == actual_bundle) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
312 expected_resource_status = U_ZERO_ERROR;
314 expected_resource_status = U_USING_DEFAULT_WARNING;
316 expected_resource_status = U_USING_FALLBACK_WARNING;
318 log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>. actual_bundle=%s\n",
325 param[i].inherits[j]?"Yes":"No",
326 param[actual_bundle].name);
332 for (j=param[i].where; j>=0; --j)
341 base=(UChar*)malloc(sizeof(UChar)*(strlen(NAME[j]) + 1));
342 u_uastrcpy(base,NAME[j]);
351 base = (UChar*) malloc(sizeof(UChar) * 1);
356 /*-------------------------------------------------------------------- */
359 strcpy(tag,"string_");
362 strcpy(action,param[i].name);
363 strcat(action, ".ures_get(" );
369 status = U_ZERO_ERROR;
371 ures_getStringByKey(theBundle, tag, &resultLen, &status);
372 if(U_SUCCESS(status))
374 status = U_ZERO_ERROR;
375 string=ures_getStringByKey(theBundle, tag, &resultLen, &status);
378 log_verbose("%s got %d, expected %d\n", action, status, expected_resource_status);
380 CONFIRM_ErrorCode(status, expected_resource_status);
383 if(U_SUCCESS(status)){
384 expected_string=(UChar*)malloc(sizeof(UChar)*(u_strlen(base) + 3));
385 u_strcpy(expected_string,base);
390 expected_string = (UChar*)malloc(sizeof(UChar)*(u_strlen(kERROR) + 1));
391 u_strcpy(expected_string,kERROR);
395 CONFIRM_EQ(string, expected_string);
397 free(expected_string);
398 ures_close(theBundle);
401 return (UBool)(passNum == pass);
415 * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR
419 static void TestFallback()
421 UErrorCode status = U_ZERO_ERROR;
422 UResourceBundle *fr_FR = NULL;
423 UResourceBundle *subResource = NULL;
424 const UChar *junk; /* ignored */
427 log_verbose("Opening fr_FR..");
428 fr_FR = ures_open(NULL, "fr_FR", &status);
429 if(U_FAILURE(status))
431 log_err_status(status, "Couldn't open fr_FR - %s\n", u_errorName(status));
435 status = U_ZERO_ERROR;
438 /* clear it out.. just do some calls to get the gears turning */
439 junk = ures_getStringByKey(fr_FR, "LocaleID", &resultLen, &status);
440 (void)junk; /* Suppress set but not used warning. */
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, "layout", NULL, &status);
449 if(status != U_USING_DEFAULT_WARNING)
451 log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get layout 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<UPRV_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 < UPRV_LENGTHOF(test); 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 < UPRV_LENGTHOF(test); 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);
1002 if(U_SUCCESS(status) && locale != NULL) {
1003 log_err("Requested locale should return NULL\n");
1005 status = U_ZERO_ERROR;
1006 locale = ures_getLocaleByType(res, ULOC_VALID_LOCALE, &status);
1007 if(!locale || strcmp(locale, test[i].validLocale) != 0) {
1008 log_err("Expected valid locale to be %s. Got %s\n", test[i].requestedLocale, locale);
1010 locale = ures_getLocaleByType(res, ULOC_ACTUAL_LOCALE, &status);
1011 if(!locale || strcmp(locale, test[i].actualLocale) != 0) {
1012 log_err("Expected actual locale to be %s. Got %s\n", test[i].requestedLocale, locale);