1 // © 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"
23 #include "unicode/utf16.h"
28 #include <stdio.h> // for sprintf()
30 #define RESTEST_HEAP_CHECK 0
32 #include "unicode/ures.h"
34 #include "unicode/ctest.h"
37 static void TestOpenDirect(void);
38 static void TestOpenDirectFillIn(void);
39 static void TestFallback(void);
40 static void TestTable32(void);
41 static void TestFileStream(void);
42 static void TestNewSearch(void);
43 /*****************************************************************************/
45 const UChar kERROR
[] = { 0x0045 /*E*/, 0x0052 /*'R'*/, 0x0052 /*'R'*/,
46 0x004F /*'O'*/, 0x0052/*'R'*/, 0x0000 /*'\0'*/};
48 /*****************************************************************************/
57 typedef enum E_Where E_Where
;
58 /*****************************************************************************/
60 #define CONFIRM_EQ(actual,expected) UPRV_BLOCK_MACRO_BEGIN { \
61 if (u_strcmp(expected,actual)==0) { \
65 log_err("%s returned %s instead of %s\n", action, austrdup(actual), austrdup(expected)); \
67 } UPRV_BLOCK_MACRO_END
69 #define CONFIRM_ErrorCode(actual,expected) UPRV_BLOCK_MACRO_BEGIN { \
70 if ((expected)==(actual)) { \
74 log_err("%s returned %s instead of %s\n", action, myErrorName(actual), myErrorName(expected)); \
76 } UPRV_BLOCK_MACRO_END
79 /* Array of our test objects */
84 UErrorCode expected_constructor_status
;
86 UBool like
[e_Where_count
];
87 UBool inherits
[e_Where_count
];
91 /* "IN" means inherits */
92 /* "NE" or "ne" means "does not exist" */
94 { "root", U_ZERO_ERROR
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} },
95 { "te", U_ZERO_ERROR
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
96 { "te_IN", U_ZERO_ERROR
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
97 { "te_NE", U_USING_FALLBACK_WARNING
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
98 { "te_IN_NE", U_USING_FALLBACK_WARNING
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
99 { "ne", U_USING_DEFAULT_WARNING
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} }
102 static int32_t bundles_count
= UPRV_LENGTHOF(param
);
106 /***************************************************************************************/
108 /* Array of our test objects */
110 void addResourceBundleTest(TestNode
** root
);
112 void addResourceBundleTest(TestNode
** root
)
114 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
115 addTest(root
, &TestConstruction1
, "tsutil/crestst/TestConstruction1");
116 addTest(root
, &TestOpenDirect
, "tsutil/crestst/TestOpenDirect");
117 addTest(root
, &TestOpenDirectFillIn
, "tsutil/crestst/TestOpenDirectFillIn");
118 addTest(root
, &TestResourceBundles
, "tsutil/crestst/TestResourceBundles");
119 addTest(root
, &TestTable32
, "tsutil/crestst/TestTable32");
120 addTest(root
, &TestFileStream
, "tsutil/crestst/TestFileStream");
121 addTest(root
, &TestGetSize
, "tsutil/crestst/TestGetSize");
122 addTest(root
, &TestGetLocaleByType
, "tsutil/crestst/TestGetLocaleByType");
124 addTest(root
, &TestFallback
, "tsutil/crestst/TestFallback");
125 addTest(root
, &TestAliasConflict
, "tsutil/crestst/TestAliasConflict");
126 addTest(root
, &TestNewSearch
, "tsutil/crestst/TestNewSearch");
131 /***************************************************************************************/
132 void TestAliasConflict(void) {
133 UErrorCode status
= U_ZERO_ERROR
;
134 UResourceBundle
*he
= NULL
;
135 UResourceBundle
*iw
= NULL
;
136 const UChar
*result
= NULL
;
139 he
= ures_open(NULL
, "he", &status
);
140 iw
= ures_open(NULL
, "iw", &status
);
141 if(U_FAILURE(status
)) {
142 log_err_status(status
, "Failed to get resource with %s\n", myErrorName(status
));
145 result
= ures_getStringByKey(he
, "ExemplarCharacters", &resultLen
, &status
);
146 if(U_FAILURE(status
) || result
== NULL
) {
147 log_err_status(status
, "Failed to get resource with %s\n", myErrorName(status
));
153 void TestResourceBundles()
155 UErrorCode status
= U_ZERO_ERROR
;
156 loadTestData(&status
);
157 if(U_FAILURE(status
)) {
158 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
162 testTag("only_in_Root", TRUE
, FALSE
, FALSE
);
163 testTag("in_Root_te", TRUE
, TRUE
, FALSE
);
164 testTag("in_Root_te_te_IN", TRUE
, TRUE
, TRUE
);
165 testTag("in_Root_te_IN", TRUE
, FALSE
, TRUE
);
166 testTag("only_in_te", FALSE
, TRUE
, FALSE
);
167 testTag("only_in_te_IN", FALSE
, FALSE
, TRUE
);
168 testTag("in_te_te_IN", FALSE
, TRUE
, TRUE
);
169 testTag("nonexistent", FALSE
, FALSE
, FALSE
);
171 log_verbose("Passed:= %d Failed= %d \n", pass
, fail
);
174 void TestConstruction1()
176 UResourceBundle
*test1
= 0, *test2
= 0;
177 const UChar
*result1
, *result2
;
181 UErrorCode err
= U_ZERO_ERROR
;
182 const char* testdatapath
;
183 const char* locale
="te_IN";
185 log_verbose("Testing ures_open()......\n");
188 testdatapath
=loadTestData(&err
);
191 log_data_err("Could not load testdata.dat %s \n",myErrorName(err
));
195 test1
=ures_open(testdatapath
, NULL
, &err
);
198 log_err("construction of %s did not succeed : %s \n",NULL
, myErrorName(err
));
203 test2
=ures_open(testdatapath
, locale
, &err
);
206 log_err("construction of %s did not succeed : %s \n",locale
, myErrorName(err
));
209 result1
= ures_getStringByKey(test1
, "string_in_Root_te_te_IN", &resultLen
, &err
);
210 result2
= ures_getStringByKey(test2
, "string_in_Root_te_te_IN", &resultLen
, &err
);
213 if (U_FAILURE(err
)) {
214 log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(err
));
218 u_uastrcpy(temp
, "TE_IN");
220 if(u_strcmp(result2
, temp
)!=0)
224 log_err("Construction test failed for ures_open();\n");
225 if(!getTestOption(VERBOSITY_OPTION
))
226 log_info("(run verbose for more information)\n");
228 log_verbose("\nGot->");
229 for(n
=0;result2
[n
];n
++)
231 log_verbose("%04X ",result2
[n
]);
235 log_verbose("\nWant>");
238 log_verbose("%04X ",temp
[n
]);
244 log_verbose("for string_in_Root_te_te_IN, default.txt had %s\n", austrdup(result1
));
245 log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", austrdup(result2
));
247 /* Test getVersionNumber*/
248 log_verbose("Testing version number\n");
249 log_verbose("for getVersionNumber : %s\n", ures_getVersionNumber(test1
));
255 /*****************************************************************************/
256 /*****************************************************************************/
258 UBool
testTag(const char* frag
,
263 int32_t passNum
=pass
;
265 /* Make array from input params */
268 const char *NAME
[] = { "ROOT", "TE", "TE_IN" };
270 /* Now try to load the desired items */
271 UResourceBundle
* theBundle
= NULL
;
274 UErrorCode status
= U_ZERO_ERROR
,expected_resource_status
= U_ZERO_ERROR
;
276 UChar
* expected_string
= NULL
;
277 const UChar
* string
= NULL
;
280 int32_t actual_bundle
;
282 const char *testdatapath
= loadTestData(&status
);
288 strcpy(item_tag
, "tag");
290 status
= U_ZERO_ERROR
;
291 theBundle
= ures_open(testdatapath
, "root", &status
);
292 if(U_FAILURE(status
))
294 ures_close(theBundle
);
295 log_err("Couldn't open root bundle in %s", testdatapath
);
298 ures_close(theBundle
);
302 for (i
=0; i
<bundles_count
; ++i
)
304 strcpy(action
,"construction for");
305 strcat(action
, param
[i
].name
);
308 status
= U_ZERO_ERROR
;
310 theBundle
= ures_open(testdatapath
, param
[i
].name
, &status
);
311 /*theBundle = ures_open("c:\\icu\\icu\\source\\test\\testdata\\testdata", param[i].name, &status);*/
313 CONFIRM_ErrorCode(status
,param
[i
].expected_constructor_status
);
318 actual_bundle
= 0; /* ne -> default */
320 actual_bundle
= 1; /* te_NE -> te */
322 actual_bundle
= 2; /* te_IN_NE -> te_IN */
326 expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
327 for (j
=e_te_IN
; j
>=e_Root
; --j
)
329 if (is_in
[j
] && param
[i
].inherits
[j
])
332 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
333 expected_resource_status
= U_ZERO_ERROR
;
335 expected_resource_status
= U_USING_DEFAULT_WARNING
;
337 expected_resource_status
= U_USING_FALLBACK_WARNING
;
339 log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>. actual_bundle=%s\n",
346 param
[i
].inherits
[j
]?"Yes":"No",
347 param
[actual_bundle
].name
);
353 for (j
=param
[i
].where
; j
>=0; --j
)
362 base
=(UChar
*)malloc(sizeof(UChar
)*(strlen(NAME
[j
]) + 1));
363 u_uastrcpy(base
,NAME
[j
]);
372 base
= (UChar
*) malloc(sizeof(UChar
) * 1);
377 /*-------------------------------------------------------------------- */
380 strcpy(tag
,"string_");
383 strcpy(action
,param
[i
].name
);
384 strcat(action
, ".ures_get(" );
390 status
= U_ZERO_ERROR
;
392 ures_getStringByKey(theBundle
, tag
, &resultLen
, &status
);
393 if(U_SUCCESS(status
))
395 status
= U_ZERO_ERROR
;
396 string
=ures_getStringByKey(theBundle
, tag
, &resultLen
, &status
);
399 log_verbose("%s got %d, expected %d\n", action
, status
, expected_resource_status
);
401 CONFIRM_ErrorCode(status
, expected_resource_status
);
404 if(U_SUCCESS(status
)){
405 expected_string
=(UChar
*)malloc(sizeof(UChar
)*(u_strlen(base
) + 3));
406 u_strcpy(expected_string
,base
);
411 expected_string
= (UChar
*)malloc(sizeof(UChar
)*(u_strlen(kERROR
) + 1));
412 u_strcpy(expected_string
,kERROR
);
416 CONFIRM_EQ(string
, expected_string
);
418 free(expected_string
);
419 ures_close(theBundle
);
422 return (UBool
)(passNum
== pass
);
436 * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR
440 static void TestFallback()
442 UErrorCode status
= U_ZERO_ERROR
;
443 UResourceBundle
*fr_FR
= NULL
;
444 UResourceBundle
*subResource
= NULL
;
445 const UChar
*junk
; /* ignored */
448 log_verbose("Opening fr_FR..");
449 fr_FR
= ures_open(NULL
, "fr_FR", &status
);
450 if(U_FAILURE(status
))
452 log_err_status(status
, "Couldn't open fr_FR - %s\n", u_errorName(status
));
456 status
= U_ZERO_ERROR
;
459 /* clear it out.. just do some calls to get the gears turning */
460 junk
= ures_getStringByKey(fr_FR
, "LocaleID", &resultLen
, &status
);
461 (void)junk
; /* Suppress set but not used warning. */
462 status
= U_ZERO_ERROR
;
463 junk
= ures_getStringByKey(fr_FR
, "LocaleString", &resultLen
, &status
);
464 status
= U_ZERO_ERROR
;
465 junk
= ures_getStringByKey(fr_FR
, "LocaleID", &resultLen
, &status
);
466 status
= U_ZERO_ERROR
;
468 /* OK first one. This should be a Default value. */
469 subResource
= ures_getByKey(fr_FR
, "layout", NULL
, &status
);
470 if(status
!= U_USING_DEFAULT_WARNING
)
472 log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get layout from fr_FR, got %s\n",
473 u_errorName(status
));
475 ures_close(subResource
);
476 status
= U_ZERO_ERROR
;
478 /* and this is a Fallback, to fr */
479 junk
= ures_getStringByKey(fr_FR
, "ExemplarCharacters", &resultLen
, &status
);
480 if(status
!= U_USING_FALLBACK_WARNING
)
482 log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get ExemplarCharacters from fr_FR, got %s\n",
483 u_errorName(status
));
486 status
= U_ZERO_ERROR
;
492 TestOpenDirect(void) {
493 UResourceBundle
*idna_rules
, *casing
, *te_IN
, *ne
, *item
;
494 UErrorCode errorCode
;
497 * test that ures_openDirect() opens a resource bundle
498 * where one can look up its own items but not fallback items
499 * from root or similar
501 errorCode
=U_ZERO_ERROR
;
502 idna_rules
=ures_openDirect(loadTestData(&errorCode
), "idna_rules", &errorCode
);
503 if(U_FAILURE(errorCode
)) {
504 log_data_err("ures_openDirect(\"idna_rules\") failed: %s\n", u_errorName(errorCode
));
508 if(0!=uprv_strcmp("idna_rules", ures_getLocale(idna_rules
, &errorCode
))) {
509 log_err("ures_openDirect(\"idna_rules\").getLocale()!=idna_rules\n");
511 errorCode
=U_ZERO_ERROR
;
513 /* try an item in idna_rules, must work */
514 item
=ures_getByKey(idna_rules
, "UnassignedSet", NULL
, &errorCode
);
515 if(U_FAILURE(errorCode
)) {
516 log_err("translit_index.getByKey(local key) failed: %s\n", u_errorName(errorCode
));
517 errorCode
=U_ZERO_ERROR
;
522 /* try an item in root, must fail */
523 item
=ures_getByKey(idna_rules
, "ShortLanguage", NULL
, &errorCode
);
524 if(U_FAILURE(errorCode
)) {
525 errorCode
=U_ZERO_ERROR
;
527 log_err("idna_rules.getByKey(root key) succeeded!\n");
530 ures_close(idna_rules
);
532 /* now make sure that "idna_rules" will not work with ures_open() */
533 errorCode
=U_ZERO_ERROR
;
534 idna_rules
=ures_open("testdata", "idna_rules", &errorCode
);
535 if(U_FAILURE(errorCode
) || errorCode
==U_USING_DEFAULT_WARNING
|| errorCode
==U_USING_FALLBACK_WARNING
) {
536 /* falling back to default or root is ok */
537 errorCode
=U_ZERO_ERROR
;
538 } else if(0!=uprv_strcmp("idna_rules", ures_getLocale(idna_rules
, &errorCode
))) {
539 /* Opening this file will work in "files mode" on Windows and the Mac,
540 which have case insensitive file systems */
541 log_err("ures_open(\"idna_rules\") succeeded, should fail! Got: %s\n", u_errorName(errorCode
));
543 ures_close(idna_rules
);
545 /* ures_openDirect("translit_index_WronG") must fail */
546 idna_rules
=ures_openDirect(NULL
, "idna_rules_WronG", &errorCode
);
547 if(U_FAILURE(errorCode
)) {
548 errorCode
=U_ZERO_ERROR
;
550 log_err("ures_openDirect(\"idna_rules_WronG\") succeeded, should fail!\n");
552 ures_close(idna_rules
);
554 errorCode
= U_USING_FALLBACK_WARNING
;
555 idna_rules
=ures_openDirect("testdata", "idna_rules", &errorCode
);
556 if(U_FAILURE(errorCode
)) {
557 log_data_err("ures_openDirect(\"idna_rules\") failed when U_USING_FALLBACK_WARNING was set prior to call: %s\n", u_errorName(errorCode
));
560 ures_close(idna_rules
);
563 * ICU 3.6 has new resource bundle syntax and data for bundles that do not
564 * participate in locale fallback. Now,
565 * - ures_open() works like ures_openDirect() on a bundle with a top-level
566 * type of ":table(nofallback)" _if_ the bundle exists
567 * - ures_open() will continue to find a root bundle if the requested one
568 * does not exist, unlike ures_openDirect()
570 * Test with a different bundle than above to avoid confusion in the cache.
574 * verify that ures_open("casing"), which now has a nofallback declaration,
575 * does not enable fallbacks
577 errorCode
=U_ZERO_ERROR
;
578 casing
=ures_open("testdata", "casing", &errorCode
);
579 if(U_FAILURE(errorCode
)) {
580 log_data_err("ures_open(\"casing\") failed: %s\n", u_errorName(errorCode
));
584 errorCode
=U_ZERO_ERROR
;
585 item
=ures_getByKey(casing
, "Info", NULL
, &errorCode
);
586 if(U_FAILURE(errorCode
)) {
587 log_err("casing.getByKey(Info) failed - %s\n", u_errorName(errorCode
));
592 errorCode
=U_ZERO_ERROR
;
593 item
=ures_getByKey(casing
, "ShortLanguage", NULL
, &errorCode
);
594 if(U_SUCCESS(errorCode
)) {
595 log_err("casing.getByKey(root key) succeeded despite nofallback declaration - %s\n", u_errorName(errorCode
));
601 * verify that ures_open("ne") finds the root bundle but
602 * ures_openDirect("ne") does not
604 errorCode
=U_ZERO_ERROR
;
605 ne
=ures_open("testdata", "ne", &errorCode
);
606 if(U_FAILURE(errorCode
)) {
607 log_data_err("ures_open(\"ne\") failed (expected to get root): %s\n", u_errorName(errorCode
));
609 if(errorCode
!=U_USING_DEFAULT_WARNING
|| 0!=uprv_strcmp("root", ures_getLocale(ne
, &errorCode
))) {
610 log_err("ures_open(\"ne\") found something other than \"root\" - %s\n", u_errorName(errorCode
));
614 errorCode
=U_ZERO_ERROR
;
615 ne
=ures_openDirect("testdata", "ne", &errorCode
);
616 if(U_SUCCESS(errorCode
)) {
617 log_data_err("ures_openDirect(\"ne\") succeeded unexpectedly\n");
621 /* verify that ures_openDirect("te_IN") does not enable fallbacks */
622 errorCode
=U_ZERO_ERROR
;
623 te_IN
=ures_openDirect("testdata", "te_IN", &errorCode
);
624 if(U_FAILURE(errorCode
)) {
625 log_data_err("ures_open(\"te_IN\") failed: %s\n", u_errorName(errorCode
));
628 errorCode
=U_ZERO_ERROR
;
629 item
=ures_getByKey(te_IN
, "ShortLanguage", NULL
, &errorCode
);
630 if(U_SUCCESS(errorCode
)) {
631 log_err("te_IN.getByKey(root key) succeeded despite use of ures_openDirect() - %s\n", u_errorName(errorCode
));
638 TestOpenDirectFillIn(void) {
639 // Test that ures_openDirectFillIn() opens a stack allocated resource bundle, similar to ures_open().
640 // Since ures_openDirectFillIn is just a wrapper function, this is just a very basic test copied from
641 // the TestOpenDirect test above.
642 UErrorCode errorCode
= U_ZERO_ERROR
;
643 UResourceBundle
*item
;
644 UResourceBundle idna_rules
;
645 ures_initStackObject(&idna_rules
);
647 ures_openDirectFillIn(&idna_rules
, loadTestData(&errorCode
), "idna_rules", &errorCode
);
648 if(U_FAILURE(errorCode
)) {
649 log_data_err("ures_openDirectFillIn(\"idna_rules\") failed: %s\n", u_errorName(errorCode
));
653 if(0!=uprv_strcmp("idna_rules", ures_getLocale(&idna_rules
, &errorCode
))) {
654 log_err("ures_openDirectFillIn(\"idna_rules\").getLocale()!=idna_rules\n");
656 errorCode
=U_ZERO_ERROR
;
658 /* try an item in idna_rules, must work */
659 item
=ures_getByKey(&idna_rules
, "UnassignedSet", NULL
, &errorCode
);
660 if(U_FAILURE(errorCode
)) {
661 log_err("translit_index.getByKey(local key) failed: %s\n", u_errorName(errorCode
));
662 errorCode
=U_ZERO_ERROR
;
667 /* try an item in root, must fail */
668 item
=ures_getByKey(&idna_rules
, "ShortLanguage", NULL
, &errorCode
);
669 if(U_FAILURE(errorCode
)) {
670 errorCode
=U_ZERO_ERROR
;
672 log_err("idna_rules.getByKey(root key) succeeded!\n");
675 ures_close(&idna_rules
);
679 parseTable32Key(const char *key
) {
684 while((c
=*key
++)!=0) {
695 static const struct {
699 { "ooooooooooooooooo", 0 },
700 { "oooooooooooooooo1", 1 },
701 { "ooooooooooooooo1o", 2 },
702 { "oo11ooo1ooo11111o", 25150 },
703 { "oo11ooo1ooo111111", 25151 },
704 { "o1111111111111111", 65535 },
705 { "1oooooooooooooooo", 65536 },
706 { "1ooooooo11o11ooo1", 65969 },
707 { "1ooooooo11o11oo1o", 65970 },
708 { "1ooooooo111oo1111", 65999 }
711 /* ### TODO UResourceBundle staticItem={ 0 }; - need to know the size */
712 UResourceBundle
*res
, *item
;
715 UErrorCode errorCode
;
716 int32_t i
, j
, number
, parsedNumber
, length
, count
;
718 errorCode
=U_ZERO_ERROR
;
719 res
=ures_open(loadTestData(&errorCode
), "testtable32", &errorCode
);
720 if(U_FAILURE(errorCode
)) {
721 log_data_err("unable to open testdata/testtable32.res - %s\n", u_errorName(errorCode
));
724 if(ures_getType(res
)!=URES_TABLE
) {
725 log_data_err("testdata/testtable32.res has type %d instead of URES_TABLE\n", ures_getType(res
));
728 count
=ures_getSize(res
);
730 log_err("testdata/testtable32.res should have 66000 entries but has %d\n", count
);
733 /* get the items by index */
735 for(i
=0; i
<count
; ++i
) {
736 item
=ures_getByIndex(res
, i
, item
, &errorCode
);
737 if(U_FAILURE(errorCode
)) {
738 log_err("unable to get item %d of %d in testdata/testtable32.res - %s\n",
739 i
, count
, u_errorName(errorCode
));
743 key
=ures_getKey(item
);
744 parsedNumber
=parseTable32Key(key
);
746 switch(ures_getType(item
)) {
748 s
=ures_getString(item
, &length
, &errorCode
);
749 if(U_FAILURE(errorCode
) || s
==NULL
) {
750 log_err("unable to access the string \"%s\" at %d in testdata/testtable32.res - %s\n",
751 key
, i
, u_errorName(errorCode
));
755 U16_NEXT(s
, j
, length
, number
);
759 number
=ures_getInt(item
, &errorCode
);
760 if(U_FAILURE(errorCode
)) {
761 log_err("unable to access the integer \"%s\" at %d in testdata/testtable32.res - %s\n",
762 key
, i
, u_errorName(errorCode
));
767 log_err("unexpected resource type %d for \"%s\" at %d in testdata/testtable32.res - %s\n",
768 ures_getType(item
), key
, i
, u_errorName(errorCode
));
773 if(number
>=0 && number
!=parsedNumber
) {
774 log_err("\"%s\" at %d in testdata/testtable32.res has a string/int value of %d, expected %d\n",
775 key
, i
, number
, parsedNumber
);
779 /* search for some items by key */
780 for(i
=0; i
<UPRV_LENGTHOF(testcases
); ++i
) {
781 item
=ures_getByKey(res
, testcases
[i
].key
, item
, &errorCode
);
782 if(U_FAILURE(errorCode
)) {
783 log_err("unable to find the key \"%s\" in testdata/testtable32.res - %s\n",
784 testcases
[i
].key
, u_errorName(errorCode
));
788 switch(ures_getType(item
)) {
790 s
=ures_getString(item
, &length
, &errorCode
);
791 if(U_FAILURE(errorCode
) || s
==NULL
) {
792 log_err("unable to access the string \"%s\" in testdata/testtable32.res - %s\n",
793 testcases
[i
].key
, u_errorName(errorCode
));
797 U16_NEXT(s
, j
, length
, number
);
801 number
=ures_getInt(item
, &errorCode
);
802 if(U_FAILURE(errorCode
)) {
803 log_err("unable to access the integer \"%s\" in testdata/testtable32.res - %s\n",
804 testcases
[i
].key
, u_errorName(errorCode
));
809 log_err("unexpected resource type %d for \"%s\" in testdata/testtable32.res - %s\n",
810 ures_getType(item
), testcases
[i
].key
, u_errorName(errorCode
));
815 if(number
>=0 && number
!=testcases
[i
].number
) {
816 log_err("\"%s\" in testdata/testtable32.res has a string/int value of %d, expected %d\n",
817 testcases
[i
].key
, number
, testcases
[i
].number
);
820 key
=ures_getKey(item
);
821 if(0!=uprv_strcmp(key
, testcases
[i
].key
)) {
822 log_err("\"%s\" in testdata/testtable32.res claims to have the key \"%s\"\n",
823 testcases
[i
].key
, key
);
831 static void TestFileStream(void){
834 UErrorCode status
= U_ZERO_ERROR
;
835 const char* testdatapath
= loadTestData(&status
);
836 char* fileName
= (char*) malloc(uprv_strlen(testdatapath
) +10);
837 FileStream
* stream
= NULL
;
838 /* these should not be closed */
839 FileStream
* pStdin
= T_FileStream_stdin();
840 FileStream
* pStdout
= T_FileStream_stdout();
841 FileStream
* pStderr
= T_FileStream_stderr();
843 const char* testline
= "This is a test line";
844 int32_t bufLen
= (int32_t)strlen(testline
)+10;
845 char* buf
= (char*) malloc(bufLen
);
849 log_err("failed to get T_FileStream_stdin()");
852 log_err("failed to get T_FileStream_stdout()");
855 log_err("failed to get T_FileStream_stderr()");
858 uprv_strcpy(fileName
,testdatapath
);
859 uprv_strcat(fileName
,".dat");
860 stream
= T_FileStream_open(fileName
, "r");
862 log_data_err("T_FileStream_open failed to open %s\n",fileName
);
864 if(!T_FileStream_file_exists(fileName
)){
865 log_data_err("T_FileStream_file_exists failed to verify existence of %s \n",fileName
);
868 retLen
=T_FileStream_read(stream
,&c
,1);
870 log_data_err("T_FileStream_read failed to read from %s \n",fileName
);
873 T_FileStream_rewind(stream
);
874 T_FileStream_read(stream
,&c1
,1);
876 log_data_err("T_FileStream_rewind failed to rewind %s \n",fileName
);
878 T_FileStream_rewind(stream
);
879 c1
= T_FileStream_peek(stream
);
881 log_data_err("T_FileStream_peek failed to peekd %s \n",fileName
);
883 c
= T_FileStream_getc(stream
);
884 T_FileStream_ungetc(c
,stream
);
885 if(c
!= T_FileStream_getc(stream
)){
886 log_data_err("T_FileStream_ungetc failed to d %s \n",fileName
);
889 if(T_FileStream_size(stream
)<=0){
890 log_data_err("T_FileStream_size failed to d %s \n",fileName
);
892 if(T_FileStream_error(stream
)){
893 log_data_err("T_FileStream_error shouldn't have an error %s\n",fileName
);
895 if(!T_FileStream_error(NULL
)){
896 log_err("T_FileStream_error didn't get an error %s\n",fileName
);
898 T_FileStream_putc(stream
, 0x20);
899 if(!T_FileStream_error(stream
)){
902 writing to a read-only file may not consistently fail on all platforms
903 (e.g. HP-UX, FreeBSD, MacOSX)
905 log_verbose("T_FileStream_error didn't get an error when writing to a readonly file %s\n",fileName
);
908 T_FileStream_close(stream
);
910 /* test writing function */
912 uprv_strcpy(fileName
,testdatapath
);
913 uprv_strcat(fileName
,".tmp");
914 stream
= T_FileStream_open(fileName
,"w+");
917 log_data_err("Could not open %s for writing\n",fileName
);
920 T_FileStream_putc(stream
,c
);
921 T_FileStream_rewind(stream
);
922 if(c
!= T_FileStream_getc(stream
)){
923 log_data_err("T_FileStream_putc failed %s\n",fileName
);
926 T_FileStream_rewind(stream
);
927 T_FileStream_writeLine(stream
,testline
);
928 T_FileStream_rewind(stream
);
929 T_FileStream_readLine(stream
,buf
,bufLen
);
930 if(uprv_strncmp(testline
, buf
,uprv_strlen(buf
))!=0){
931 log_data_err("T_FileStream_writeLine failed %s\n",fileName
);
934 T_FileStream_rewind(stream
);
935 T_FileStream_write(stream
,testline
,(int32_t)strlen(testline
));
936 T_FileStream_rewind(stream
);
937 retLen
= T_FileStream_read(stream
, buf
, bufLen
);
938 if(uprv_strncmp(testline
, buf
,retLen
)!=0){
939 log_data_err("T_FileStream_write failed %s\n",fileName
);
942 T_FileStream_close(stream
);
944 if(!T_FileStream_remove(fileName
)){
945 log_data_err("T_FileStream_remove failed to delete %s\n",fileName
);
954 static void TestGetSize(void) {
962 { "integerarray", 1},
965 { "emptystring", 1}, /* empty string is still a string */
969 { "collations", 1}, /* not 2 - there is hidden %%CollationBin */
972 UErrorCode status
= U_ZERO_ERROR
;
974 UResourceBundle
*rb
= NULL
;
975 UResourceBundle
*res
= NULL
;
976 UResourceBundle
*helper
= NULL
;
977 const char* testdatapath
= loadTestData(&status
);
978 int32_t i
= 0, j
= 0;
981 if(U_FAILURE(status
))
983 log_data_err("Could not load testdata.dat %s\n", u_errorName(status
));
987 rb
= ures_open(testdatapath
, "testtypes", &status
);
988 if(U_FAILURE(status
))
990 log_err("Could not testtypes resource bundle %s\n", u_errorName(status
));
994 for(i
= 0; i
< UPRV_LENGTHOF(test
); i
++) {
995 res
= ures_getByKey(rb
, test
[i
].key
, res
, &status
);
996 if(U_FAILURE(status
))
998 log_err("Couldn't find the key %s. Error: %s\n", test
[i
].key
, u_errorName(status
));
1002 size
= ures_getSize(res
);
1003 if(size
!= test
[i
].size
) {
1004 log_err("Expected size %i, got size %i for key %s\n", test
[i
].size
, size
, test
[i
].key
);
1005 for(j
= 0; j
< size
; j
++) {
1006 helper
= ures_getByIndex(res
, j
, helper
, &status
);
1007 log_err("%s\n", ures_getKey(helper
));
1016 static void TestGetLocaleByType(void) {
1017 static const struct {
1018 const char *requestedLocale
;
1019 const char *resourceKey
;
1020 const char *validLocale
;
1021 const char *actualLocale
;
1023 { "te_IN_BLAH", "string_only_in_te_IN", "te_IN", "te_IN" },
1024 { "te_IN_BLAH", "string_only_in_te", "te_IN", "te" },
1025 { "te_IN_BLAH", "string_only_in_Root", "te_IN", "root" },
1026 { "te_IN_BLAH_01234567890_01234567890_01234567890_01234567890_01234567890_01234567890", "array_2d_only_in_Root", "te_IN", "root" },
1027 { "te_IN_BLAH@currency=euro", "array_2d_only_in_te_IN", "te_IN", "te_IN" },
1028 { "te_IN_BLAH@collation=phonebook;calendar=thai", "array_2d_only_in_te", "te_IN", "te" }
1031 UErrorCode status
= U_ZERO_ERROR
;
1033 UResourceBundle
*rb
= NULL
;
1034 UResourceBundle
*res
= NULL
;
1035 const char* testdatapath
= loadTestData(&status
);
1037 const char *locale
= NULL
;
1039 if(U_FAILURE(status
))
1041 log_data_err("Could not load testdata.dat %s\n", u_errorName(status
));
1045 for(i
= 0; i
< UPRV_LENGTHOF(test
); i
++) {
1046 rb
= ures_open(testdatapath
, test
[i
].requestedLocale
, &status
);
1047 if(U_FAILURE(status
))
1049 log_err("Could not open resource bundle %s (error %s)\n", test
[i
].requestedLocale
, u_errorName(status
));
1050 status
= U_ZERO_ERROR
;
1054 res
= ures_getByKey(rb
, test
[i
].resourceKey
, res
, &status
);
1055 if(U_FAILURE(status
))
1057 log_err("Couldn't find the key %s. Error: %s\n", test
[i
].resourceKey
, u_errorName(status
));
1059 status
= U_ZERO_ERROR
;
1063 locale
= ures_getLocaleByType(res
, ULOC_REQUESTED_LOCALE
, &status
);
1064 if(U_SUCCESS(status
) && locale
!= NULL
) {
1065 log_err("Requested locale should return NULL\n");
1067 status
= U_ZERO_ERROR
;
1068 locale
= ures_getLocaleByType(res
, ULOC_VALID_LOCALE
, &status
);
1069 if(!locale
|| strcmp(locale
, test
[i
].validLocale
) != 0) {
1070 log_err("Expected valid locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
);
1072 locale
= ures_getLocaleByType(res
, ULOC_ACTUAL_LOCALE
, &status
);
1073 if(!locale
|| strcmp(locale
, test
[i
].actualLocale
) != 0) {
1074 log_err("Expected actual locale to be %s. Got %s\n", test
[i
].requestedLocale
, locale
);
1081 static void TestNewSearch(void) {
1082 // first column is input locale, second column is expected output locale
1083 const char* locales
[] = {
1084 "de_Latn_LI", "de_LI",
1086 "yi_Latn_DE", "root",
1088 "zh_Hant_SG", "zh_Hant"
1091 for (int32_t i
= 0; i
< sizeof(locales
) / sizeof(char*); i
+= 2) {
1092 UErrorCode err
= U_ZERO_ERROR
;
1093 UResourceBundle
* rb
= ures_open(NULL
, locales
[i
], &err
);
1094 char errorMessage
[100];
1096 sprintf(errorMessage
, "Error %s opening resource bundle for locale %s", u_errorName(err
), locales
[i
]);
1097 if (assertSuccess(errorMessage
, &err
)) {
1098 const char* resourceLocale
= ures_getLocaleByType(rb
, ULOC_ACTUAL_LOCALE
, &err
);
1100 sprintf(errorMessage
, "Error %s getting resource locale for locale %s", u_errorName(err
), locales
[i
]);
1101 if (assertSuccess(errorMessage
, &err
)) {
1102 sprintf(errorMessage
, "Mismatch for locale %s", locales
[i
]);
1103 assertEquals(errorMessage
, locales
[i
+ 1], resourceLocale
);