• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4  * COPYRIGHT:
5  * Copyright (c) 1997-2016, International Business Machines Corporation and
6  * others. All Rights Reserved.
7  ********************************************************************/
8 /*******************************************************************************
9 *
10 * File CRESTST.C
11 *
12 * Modification History:
13 *        Name                     Description
14 *     Madhu Katragadda            Ported for C API
15 *  06/14/99     stephen           Updated for RB API changes (no suffix).
16 ********************************************************************************
17 */
18 
19 
20 #include "unicode/utypes.h"
21 #include "cintltst.h"
22 #include "unicode/ustring.h"
23 #include "unicode/utf16.h"
24 #include "cmemory.h"
25 #include "cstring.h"
26 #include "filestrm.h"
27 #include <stdlib.h>
28 
29 #define RESTEST_HEAP_CHECK 0
30 
31 #include "unicode/ures.h"
32 #include "crestst.h"
33 #include "unicode/ctest.h"
34 #include "uresimp.h"
35 
36 static void TestOpenDirect(void);
37 static void TestOpenDirectFillIn(void);
38 static void TestFallback(void);
39 static void TestTable32(void);
40 static void TestFileStream(void);
41 /*****************************************************************************/
42 
43 const UChar kERROR[] = { 0x0045 /*E*/, 0x0052 /*'R'*/, 0x0052 /*'R'*/,
44              0x004F /*'O'*/, 0x0052/*'R'*/, 0x0000 /*'\0'*/};
45 
46 /*****************************************************************************/
47 
48 enum E_Where
49 {
50   e_Root,
51   e_te,
52   e_te_IN,
53   e_Where_count
54 };
55 typedef enum E_Where E_Where;
56 /*****************************************************************************/
57 
58 #define CONFIRM_EQ(actual,expected) UPRV_BLOCK_MACRO_BEGIN { \
59     if (u_strcmp(expected,actual)==0) { \
60         record_pass(); \
61     } else { \
62         record_fail(); \
63         log_err("%s  returned  %s  instead of %s\n", action, austrdup(actual), austrdup(expected)); \
64     } \
65 } UPRV_BLOCK_MACRO_END
66 
67 #define CONFIRM_ErrorCode(actual,expected) UPRV_BLOCK_MACRO_BEGIN { \
68     if ((expected)==(actual)) { \
69         record_pass(); \
70     } else { \
71         record_fail(); \
72         log_err("%s returned  %s  instead of %s\n", action, myErrorName(actual), myErrorName(expected)); \
73     } \
74 } UPRV_BLOCK_MACRO_END
75 
76 
77 /* Array of our test objects */
78 
79 static struct
80 {
81   const char* name;
82   UErrorCode expected_constructor_status;
83   E_Where where;
84   UBool like[e_Where_count];
85   UBool inherits[e_Where_count];
86 } param[] =
87 {
88   /* "te" means test */
89   /* "IN" means inherits */
90   /* "NE" or "ne" means "does not exist" */
91 
92   { "root",         U_ZERO_ERROR,             e_Root,    { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } },
93   { "te",           U_ZERO_ERROR,             e_te,      { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } },
94   { "te_IN",        U_ZERO_ERROR,             e_te_IN,   { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } },
95   { "te_NE",        U_USING_FALLBACK_WARNING, e_te,      { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } },
96   { "te_IN_NE",     U_USING_FALLBACK_WARNING, e_te_IN,   { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } },
97   { "ne",           U_USING_DEFAULT_WARNING,  e_Root,    { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } }
98 };
99 
100 static int32_t bundles_count = UPRV_LENGTHOF(param);
101 
102 
103 
104 /***************************************************************************************/
105 
106 /* Array of our test objects */
107 
108 void addResourceBundleTest(TestNode** root);
109 
addResourceBundleTest(TestNode ** root)110 void addResourceBundleTest(TestNode** root)
111 {
112 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
113     addTest(root, &TestConstruction1, "tsutil/crestst/TestConstruction1");
114     addTest(root, &TestOpenDirect, "tsutil/crestst/TestOpenDirect");
115     addTest(root, &TestOpenDirectFillIn, "tsutil/crestst/TestOpenDirectFillIn");
116     addTest(root, &TestResourceBundles, "tsutil/crestst/TestResourceBundles");
117     addTest(root, &TestTable32, "tsutil/crestst/TestTable32");
118     addTest(root, &TestFileStream, "tsutil/crestst/TestFileStream");
119     addTest(root, &TestGetSize, "tsutil/crestst/TestGetSize");
120     addTest(root, &TestGetLocaleByType, "tsutil/crestst/TestGetLocaleByType");
121 #endif
122     addTest(root, &TestFallback, "tsutil/crestst/TestFallback");
123     addTest(root, &TestAliasConflict, "tsutil/crestst/TestAliasConflict");
124 
125 }
126 
127 
128 /***************************************************************************************/
TestAliasConflict(void)129 void TestAliasConflict(void) {
130     UErrorCode status = U_ZERO_ERROR;
131     UResourceBundle *he = NULL;
132     UResourceBundle *iw = NULL;
133     const UChar *result = NULL;
134     int32_t resultLen;
135 
136     he = ures_open(NULL, "he", &status);
137     iw = ures_open(NULL, "iw", &status);
138     if(U_FAILURE(status)) {
139         log_err_status(status, "Failed to get resource with %s\n", myErrorName(status));
140     }
141     ures_close(iw);
142     result = ures_getStringByKey(he, "ExemplarCharacters", &resultLen, &status);
143     if(U_FAILURE(status) || result == NULL) {
144         log_err_status(status, "Failed to get resource with %s\n", myErrorName(status));
145     }
146     ures_close(he);
147 }
148 
149 
TestResourceBundles()150 void TestResourceBundles()
151 {
152     UErrorCode status = U_ZERO_ERROR;
153     loadTestData(&status);
154     if(U_FAILURE(status)) {
155         log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status));
156         return;
157     }
158 
159     testTag("only_in_Root", TRUE, FALSE, FALSE);
160     testTag("in_Root_te", TRUE, TRUE, FALSE);
161     testTag("in_Root_te_te_IN", TRUE, TRUE, TRUE);
162     testTag("in_Root_te_IN", TRUE, FALSE, TRUE);
163     testTag("only_in_te", FALSE, TRUE, FALSE);
164     testTag("only_in_te_IN", FALSE, FALSE, TRUE);
165     testTag("in_te_te_IN", FALSE, TRUE, TRUE);
166     testTag("nonexistent", FALSE, FALSE, FALSE);
167 
168     log_verbose("Passed:=  %d   Failed=   %d \n", pass, fail);
169 }
170 
TestConstruction1()171 void TestConstruction1()
172 {
173     UResourceBundle *test1 = 0, *test2 = 0;
174     const UChar *result1, *result2;
175     int32_t resultLen;
176     UChar temp[7];
177 
178     UErrorCode   err = U_ZERO_ERROR;
179     const char* testdatapath ;
180     const char*      locale="te_IN";
181 
182     log_verbose("Testing ures_open()......\n");
183 
184 
185     testdatapath=loadTestData(&err);
186     if(U_FAILURE(err))
187     {
188         log_data_err("Could not load testdata.dat %s \n",myErrorName(err));
189         return;
190     }
191 
192     test1=ures_open(testdatapath, NULL, &err);
193     if(U_FAILURE(err))
194     {
195         log_err("construction of %s did not succeed :  %s \n",NULL, myErrorName(err));
196         return;
197     }
198 
199 
200     test2=ures_open(testdatapath, locale, &err);
201     if(U_FAILURE(err))
202     {
203         log_err("construction of %s did not succeed :  %s \n",locale, myErrorName(err));
204         return;
205     }
206     result1= ures_getStringByKey(test1, "string_in_Root_te_te_IN", &resultLen, &err);
207     result2= ures_getStringByKey(test2, "string_in_Root_te_te_IN", &resultLen, &err);
208 
209 
210     if (U_FAILURE(err)) {
211         log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(err));
212         return;
213     }
214 
215     u_uastrcpy(temp, "TE_IN");
216 
217     if(u_strcmp(result2, temp)!=0)
218     {
219         int n;
220 
221         log_err("Construction test failed for ures_open();\n");
222         if(!getTestOption(VERBOSITY_OPTION))
223             log_info("(run verbose for more information)\n");
224 
225         log_verbose("\nGot->");
226         for(n=0;result2[n];n++)
227         {
228             log_verbose("%04X ",result2[n]);
229         }
230         log_verbose("<\n");
231 
232         log_verbose("\nWant>");
233         for(n=0;temp[n];n++)
234         {
235             log_verbose("%04X ",temp[n]);
236         }
237         log_verbose("<\n");
238 
239     }
240 
241     log_verbose("for string_in_Root_te_te_IN, default.txt had  %s\n", austrdup(result1));
242     log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", austrdup(result2));
243 
244     /* Test getVersionNumber*/
245     log_verbose("Testing version number\n");
246     log_verbose("for getVersionNumber :  %s\n", ures_getVersionNumber(test1));
247 
248     ures_close(test1);
249     ures_close(test2);
250 }
251 
252 /*****************************************************************************/
253 /*****************************************************************************/
254 
testTag(const char * frag,UBool in_Root,UBool in_te,UBool in_te_IN)255 UBool testTag(const char* frag,
256            UBool in_Root,
257            UBool in_te,
258            UBool in_te_IN)
259 {
260     int32_t passNum=pass;
261 
262     /* Make array from input params */
263 
264     UBool is_in[3];
265     const char *NAME[] = { "ROOT", "TE", "TE_IN" };
266 
267     /* Now try to load the desired items */
268     UResourceBundle* theBundle = NULL;
269     char tag[99];
270     char action[256];
271     UErrorCode status = U_ZERO_ERROR,expected_resource_status = U_ZERO_ERROR;
272     UChar* base = NULL;
273     UChar* expected_string = NULL;
274     const UChar* string = NULL;
275     char item_tag[10];
276     int32_t i,j;
277     int32_t actual_bundle;
278     int32_t resultLen;
279     const char *testdatapath = loadTestData(&status);
280 
281     is_in[0] = in_Root;
282     is_in[1] = in_te;
283     is_in[2] = in_te_IN;
284 
285     strcpy(item_tag, "tag");
286 
287     status = U_ZERO_ERROR;
288     theBundle = ures_open(testdatapath, "root", &status);
289     if(U_FAILURE(status))
290     {
291         ures_close(theBundle);
292         log_err("Couldn't open root bundle in %s", testdatapath);
293         return FALSE;
294     }
295     ures_close(theBundle);
296     theBundle = NULL;
297 
298 
299     for (i=0; i<bundles_count; ++i)
300     {
301         strcpy(action,"construction for");
302         strcat(action, param[i].name);
303 
304 
305         status = U_ZERO_ERROR;
306 
307         theBundle = ures_open(testdatapath, param[i].name, &status);
308         /*theBundle = ures_open("c:\\icu\\icu\\source\\test\\testdata\\testdata", param[i].name, &status);*/
309 
310         CONFIRM_ErrorCode(status,param[i].expected_constructor_status);
311 
312 
313 
314         if(i == 5)
315             actual_bundle = 0; /* ne -> default */
316         else if(i == 3)
317             actual_bundle = 1; /* te_NE -> te */
318         else if(i == 4)
319             actual_bundle = 2; /* te_IN_NE -> te_IN */
320         else
321             actual_bundle = i;
322 
323         expected_resource_status = U_MISSING_RESOURCE_ERROR;
324         for (j=e_te_IN; j>=e_Root; --j)
325         {
326             if (is_in[j] && param[i].inherits[j])
327             {
328 
329                 if(j == actual_bundle) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
330                     expected_resource_status = U_ZERO_ERROR;
331                 else if(j == 0)
332                     expected_resource_status = U_USING_DEFAULT_WARNING;
333                 else
334                     expected_resource_status = U_USING_FALLBACK_WARNING;
335 
336                 log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>.  actual_bundle=%s\n",
337                             param[i].name,
338                             i,
339                             frag,
340                             j,
341                             is_in[j]?"Yes":"No",
342                             j,
343                             param[i].inherits[j]?"Yes":"No",
344                             param[actual_bundle].name);
345 
346                 break;
347             }
348         }
349 
350         for (j=param[i].where; j>=0; --j)
351         {
352             if (is_in[j])
353             {
354                 if(base != NULL) {
355                     free(base);
356                     base = NULL;
357                 }
358 
359                 base=(UChar*)malloc(sizeof(UChar)*(strlen(NAME[j]) + 1));
360                 u_uastrcpy(base,NAME[j]);
361 
362                 break;
363             }
364             else {
365                 if(base != NULL) {
366                     free(base);
367                     base = NULL;
368                 }
369                 base = (UChar*) malloc(sizeof(UChar) * 1);
370                 *base = 0x0000;
371             }
372         }
373 
374         /*-------------------------------------------------------------------- */
375         /* string */
376 
377         strcpy(tag,"string_");
378         strcat(tag,frag);
379 
380         strcpy(action,param[i].name);
381         strcat(action, ".ures_get(" );
382         strcat(action,tag);
383         strcat(action, ")");
384 
385         string=    kERROR;
386 
387         status = U_ZERO_ERROR;
388 
389         ures_getStringByKey(theBundle, tag, &resultLen, &status);
390         if(U_SUCCESS(status))
391         {
392             status = U_ZERO_ERROR;
393             string=ures_getStringByKey(theBundle, tag, &resultLen, &status);
394         }
395 
396         log_verbose("%s got %d, expected %d\n", action, status, expected_resource_status);
397 
398         CONFIRM_ErrorCode(status, expected_resource_status);
399 
400 
401         if(U_SUCCESS(status)){
402             expected_string=(UChar*)malloc(sizeof(UChar)*(u_strlen(base) + 3));
403             u_strcpy(expected_string,base);
404 
405         }
406         else
407         {
408             expected_string = (UChar*)malloc(sizeof(UChar)*(u_strlen(kERROR) + 1));
409             u_strcpy(expected_string,kERROR);
410 
411         }
412 
413         CONFIRM_EQ(string, expected_string);
414 
415         free(expected_string);
416         ures_close(theBundle);
417     }
418     free(base);
419     return (UBool)(passNum == pass);
420 }
421 
record_pass()422 void record_pass()
423 {
424   ++pass;
425 }
426 
record_fail()427 void record_fail()
428 {
429   ++fail;
430 }
431 
432 /**
433  * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR
434  * are set correctly
435  */
436 
TestFallback()437 static void TestFallback()
438 {
439     UErrorCode status = U_ZERO_ERROR;
440     UResourceBundle *fr_FR = NULL;
441     UResourceBundle *subResource = NULL;
442     const UChar *junk; /* ignored */
443     int32_t resultLen;
444 
445     log_verbose("Opening fr_FR..");
446     fr_FR = ures_open(NULL, "fr_FR", &status);
447     if(U_FAILURE(status))
448     {
449         log_err_status(status, "Couldn't open fr_FR - %s\n", u_errorName(status));
450         return;
451     }
452 
453     status = U_ZERO_ERROR;
454 
455 
456     /* clear it out..  just do some calls to get the gears turning */
457     junk = ures_getStringByKey(fr_FR, "LocaleID", &resultLen, &status);
458     (void)junk;    /* Suppress set but not used warning. */
459     status = U_ZERO_ERROR;
460     junk = ures_getStringByKey(fr_FR, "LocaleString", &resultLen, &status);
461     status = U_ZERO_ERROR;
462     junk = ures_getStringByKey(fr_FR, "LocaleID", &resultLen, &status);
463     status = U_ZERO_ERROR;
464 
465     /* OK first one. This should be a Default value. */
466     subResource = ures_getByKey(fr_FR, "layout", NULL, &status);
467     if(status != U_USING_DEFAULT_WARNING)
468     {
469         log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get layout from fr_FR, got %s\n",
470             u_errorName(status));
471     }
472     ures_close(subResource);
473     status = U_ZERO_ERROR;
474 
475     /* and this is a Fallback, to fr */
476     junk = ures_getStringByKey(fr_FR, "ExemplarCharacters", &resultLen, &status);
477     if(status != U_USING_FALLBACK_WARNING)
478     {
479         log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get ExemplarCharacters from fr_FR, got %s\n",
480             u_errorName(status));
481     }
482 
483     status = U_ZERO_ERROR;
484 
485     ures_close(fr_FR);
486 }
487 
488 static void
TestOpenDirect(void)489 TestOpenDirect(void) {
490     UResourceBundle *idna_rules, *casing, *te_IN, *ne, *item;
491     UErrorCode errorCode;
492 
493     /*
494      * test that ures_openDirect() opens a resource bundle
495      * where one can look up its own items but not fallback items
496      * from root or similar
497      */
498     errorCode=U_ZERO_ERROR;
499     idna_rules=ures_openDirect(loadTestData(&errorCode), "idna_rules", &errorCode);
500     if(U_FAILURE(errorCode)) {
501         log_data_err("ures_openDirect(\"idna_rules\") failed: %s\n", u_errorName(errorCode));
502         return;
503     }
504 
505     if(0!=uprv_strcmp("idna_rules", ures_getLocale(idna_rules, &errorCode))) {
506         log_err("ures_openDirect(\"idna_rules\").getLocale()!=idna_rules\n");
507     }
508     errorCode=U_ZERO_ERROR;
509 
510     /* try an item in idna_rules, must work */
511     item=ures_getByKey(idna_rules, "UnassignedSet", NULL, &errorCode);
512     if(U_FAILURE(errorCode)) {
513         log_err("translit_index.getByKey(local key) failed: %s\n", u_errorName(errorCode));
514         errorCode=U_ZERO_ERROR;
515     } else {
516         ures_close(item);
517     }
518 
519     /* try an item in root, must fail */
520     item=ures_getByKey(idna_rules, "ShortLanguage", NULL, &errorCode);
521     if(U_FAILURE(errorCode)) {
522         errorCode=U_ZERO_ERROR;
523     } else {
524         log_err("idna_rules.getByKey(root key) succeeded!\n");
525         ures_close(item);
526     }
527     ures_close(idna_rules);
528 
529     /* now make sure that "idna_rules" will not work with ures_open() */
530     errorCode=U_ZERO_ERROR;
531     idna_rules=ures_open("testdata", "idna_rules", &errorCode);
532     if(U_FAILURE(errorCode) || errorCode==U_USING_DEFAULT_WARNING || errorCode==U_USING_FALLBACK_WARNING) {
533         /* falling back to default or root is ok */
534         errorCode=U_ZERO_ERROR;
535     } else if(0!=uprv_strcmp("idna_rules", ures_getLocale(idna_rules, &errorCode))) {
536         /* Opening this file will work in "files mode" on Windows and the Mac,
537            which have case insensitive file systems */
538         log_err("ures_open(\"idna_rules\") succeeded, should fail! Got: %s\n", u_errorName(errorCode));
539     }
540     ures_close(idna_rules);
541 
542     /* ures_openDirect("translit_index_WronG") must fail */
543     idna_rules=ures_openDirect(NULL, "idna_rules_WronG", &errorCode);
544     if(U_FAILURE(errorCode)) {
545         errorCode=U_ZERO_ERROR;
546     } else {
547         log_err("ures_openDirect(\"idna_rules_WronG\") succeeded, should fail!\n");
548     }
549     ures_close(idna_rules);
550 
551     errorCode = U_USING_FALLBACK_WARNING;
552     idna_rules=ures_openDirect("testdata", "idna_rules", &errorCode);
553     if(U_FAILURE(errorCode)) {
554         log_data_err("ures_openDirect(\"idna_rules\") failed when U_USING_FALLBACK_WARNING was set prior to call: %s\n", u_errorName(errorCode));
555         return;
556     }
557     ures_close(idna_rules);
558 
559     /*
560      * ICU 3.6 has new resource bundle syntax and data for bundles that do not
561      * participate in locale fallback. Now,
562      * - ures_open() works like ures_openDirect() on a bundle with a top-level
563      *   type of ":table(nofallback)" _if_ the bundle exists
564      * - ures_open() will continue to find a root bundle if the requested one
565      *   does not exist, unlike ures_openDirect()
566      *
567      * Test with a different bundle than above to avoid confusion in the cache.
568      */
569 
570     /*
571      * verify that ures_open("casing"), which now has a nofallback declaration,
572      * does not enable fallbacks
573      */
574     errorCode=U_ZERO_ERROR;
575     casing=ures_open("testdata", "casing", &errorCode);
576     if(U_FAILURE(errorCode)) {
577         log_data_err("ures_open(\"casing\") failed: %s\n", u_errorName(errorCode));
578         return;
579     }
580 
581     errorCode=U_ZERO_ERROR;
582     item=ures_getByKey(casing, "Info", NULL, &errorCode);
583     if(U_FAILURE(errorCode)) {
584         log_err("casing.getByKey(Info) failed - %s\n", u_errorName(errorCode));
585     } else {
586         ures_close(item);
587     }
588 
589     errorCode=U_ZERO_ERROR;
590     item=ures_getByKey(casing, "ShortLanguage", NULL, &errorCode);
591     if(U_SUCCESS(errorCode)) {
592         log_err("casing.getByKey(root key) succeeded despite nofallback declaration - %s\n", u_errorName(errorCode));
593         ures_close(item);
594     }
595     ures_close(casing);
596 
597     /*
598      * verify that ures_open("ne") finds the root bundle but
599      * ures_openDirect("ne") does not
600      */
601     errorCode=U_ZERO_ERROR;
602     ne=ures_open("testdata", "ne", &errorCode);
603     if(U_FAILURE(errorCode)) {
604         log_data_err("ures_open(\"ne\") failed (expected to get root): %s\n", u_errorName(errorCode));
605     }
606     if(errorCode!=U_USING_DEFAULT_WARNING || 0!=uprv_strcmp("root", ures_getLocale(ne, &errorCode))) {
607         log_err("ures_open(\"ne\") found something other than \"root\" - %s\n", u_errorName(errorCode));
608     }
609     ures_close(ne);
610 
611     errorCode=U_ZERO_ERROR;
612     ne=ures_openDirect("testdata", "ne", &errorCode);
613     if(U_SUCCESS(errorCode)) {
614         log_data_err("ures_openDirect(\"ne\") succeeded unexpectedly\n");
615         ures_close(ne);
616     }
617 
618     /* verify that ures_openDirect("te_IN") does not enable fallbacks */
619     errorCode=U_ZERO_ERROR;
620     te_IN=ures_openDirect("testdata", "te_IN", &errorCode);
621     if(U_FAILURE(errorCode)) {
622         log_data_err("ures_open(\"te_IN\") failed: %s\n", u_errorName(errorCode));
623         return;
624     }
625     errorCode=U_ZERO_ERROR;
626     item=ures_getByKey(te_IN, "ShortLanguage", NULL, &errorCode);
627     if(U_SUCCESS(errorCode)) {
628         log_err("te_IN.getByKey(root key) succeeded despite use of ures_openDirect() - %s\n", u_errorName(errorCode));
629         ures_close(item);
630     }
631     ures_close(te_IN);
632 }
633 
634 static void
TestOpenDirectFillIn(void)635 TestOpenDirectFillIn(void) {
636     // Test that ures_openDirectFillIn() opens a stack allocated resource bundle, similar to ures_open().
637     // Since ures_openDirectFillIn is just a wrapper function, this is just a very basic test copied from
638     // the TestOpenDirect test above.
639     UErrorCode errorCode = U_ZERO_ERROR;
640     UResourceBundle *item;
641     UResourceBundle idna_rules;
642     ures_initStackObject(&idna_rules);
643 
644     ures_openDirectFillIn(&idna_rules, loadTestData(&errorCode), "idna_rules", &errorCode);
645     if(U_FAILURE(errorCode)) {
646         log_data_err("ures_openDirectFillIn(\"idna_rules\") failed: %s\n", u_errorName(errorCode));
647         return;
648     }
649 
650     if(0!=uprv_strcmp("idna_rules", ures_getLocale(&idna_rules, &errorCode))) {
651         log_err("ures_openDirectFillIn(\"idna_rules\").getLocale()!=idna_rules\n");
652     }
653     errorCode=U_ZERO_ERROR;
654 
655     /* try an item in idna_rules, must work */
656     item=ures_getByKey(&idna_rules, "UnassignedSet", NULL, &errorCode);
657     if(U_FAILURE(errorCode)) {
658         log_err("translit_index.getByKey(local key) failed: %s\n", u_errorName(errorCode));
659         errorCode=U_ZERO_ERROR;
660     } else {
661         ures_close(item);
662     }
663 
664     /* try an item in root, must fail */
665     item=ures_getByKey(&idna_rules, "ShortLanguage", NULL, &errorCode);
666     if(U_FAILURE(errorCode)) {
667         errorCode=U_ZERO_ERROR;
668     } else {
669         log_err("idna_rules.getByKey(root key) succeeded!\n");
670         ures_close(item);
671     }
672     ures_close(&idna_rules);
673 }
674 
675 static int32_t
parseTable32Key(const char * key)676 parseTable32Key(const char *key) {
677     int32_t number;
678     char c;
679 
680     number=0;
681     while((c=*key++)!=0) {
682         number<<=1;
683         if(c=='1') {
684             number|=1;
685         }
686     }
687     return number;
688 }
689 
690 static void
TestTable32(void)691 TestTable32(void) {
692     static const struct {
693         const char *key;
694         int32_t number;
695     } testcases[]={
696         { "ooooooooooooooooo", 0 },
697         { "oooooooooooooooo1", 1 },
698         { "ooooooooooooooo1o", 2 },
699         { "oo11ooo1ooo11111o", 25150 },
700         { "oo11ooo1ooo111111", 25151 },
701         { "o1111111111111111", 65535 },
702         { "1oooooooooooooooo", 65536 },
703         { "1ooooooo11o11ooo1", 65969 },
704         { "1ooooooo11o11oo1o", 65970 },
705         { "1ooooooo111oo1111", 65999 }
706     };
707 
708     /* ### TODO UResourceBundle staticItem={ 0 }; - need to know the size */
709     UResourceBundle *res, *item;
710     const UChar *s;
711     const char *key;
712     UErrorCode errorCode;
713     int32_t i, j, number, parsedNumber, length, count;
714 
715     errorCode=U_ZERO_ERROR;
716     res=ures_open(loadTestData(&errorCode), "testtable32", &errorCode);
717     if(U_FAILURE(errorCode)) {
718         log_data_err("unable to open testdata/testtable32.res - %s\n", u_errorName(errorCode));
719         return;
720     }
721     if(ures_getType(res)!=URES_TABLE) {
722         log_data_err("testdata/testtable32.res has type %d instead of URES_TABLE\n", ures_getType(res));
723     }
724 
725     count=ures_getSize(res);
726     if(count!=66000) {
727         log_err("testdata/testtable32.res should have 66000 entries but has %d\n", count);
728     }
729 
730     /* get the items by index */
731     item=NULL;
732     for(i=0; i<count; ++i) {
733         item=ures_getByIndex(res, i, item, &errorCode);
734         if(U_FAILURE(errorCode)) {
735             log_err("unable to get item %d of %d in testdata/testtable32.res - %s\n",
736                     i, count, u_errorName(errorCode));
737             break;
738         }
739 
740         key=ures_getKey(item);
741         parsedNumber=parseTable32Key(key);
742 
743         switch(ures_getType(item)) {
744         case URES_STRING:
745             s=ures_getString(item, &length, &errorCode);
746             if(U_FAILURE(errorCode) || s==NULL) {
747                 log_err("unable to access the string \"%s\" at %d in testdata/testtable32.res - %s\n",
748                         key, i, u_errorName(errorCode));
749                 number=-1;
750             } else {
751                 j=0;
752                 U16_NEXT(s, j, length, number);
753             }
754             break;
755         case URES_INT:
756             number=ures_getInt(item, &errorCode);
757             if(U_FAILURE(errorCode)) {
758                 log_err("unable to access the integer \"%s\" at %d in testdata/testtable32.res - %s\n",
759                         key, i, u_errorName(errorCode));
760                 number=-1;
761             }
762             break;
763         default:
764             log_err("unexpected resource type %d for \"%s\" at %d in testdata/testtable32.res - %s\n",
765                     ures_getType(item), key, i, u_errorName(errorCode));
766             number=-1;
767             break;
768         }
769 
770         if(number>=0 && number!=parsedNumber) {
771             log_err("\"%s\" at %d in testdata/testtable32.res has a string/int value of %d, expected %d\n",
772                     key, i, number, parsedNumber);
773         }
774     }
775 
776     /* search for some items by key */
777     for(i=0; i<UPRV_LENGTHOF(testcases); ++i) {
778         item=ures_getByKey(res, testcases[i].key, item, &errorCode);
779         if(U_FAILURE(errorCode)) {
780             log_err("unable to find the key \"%s\" in testdata/testtable32.res - %s\n",
781                     testcases[i].key, u_errorName(errorCode));
782             continue;
783         }
784 
785         switch(ures_getType(item)) {
786         case URES_STRING:
787             s=ures_getString(item, &length, &errorCode);
788             if(U_FAILURE(errorCode) || s==NULL) {
789                 log_err("unable to access the string \"%s\" in testdata/testtable32.res - %s\n",
790                         testcases[i].key, u_errorName(errorCode));
791                 number=-1;
792             } else {
793                 j=0;
794                 U16_NEXT(s, j, length, number);
795             }
796             break;
797         case URES_INT:
798             number=ures_getInt(item, &errorCode);
799             if(U_FAILURE(errorCode)) {
800                 log_err("unable to access the integer \"%s\" in testdata/testtable32.res - %s\n",
801                         testcases[i].key, u_errorName(errorCode));
802                 number=-1;
803             }
804             break;
805         default:
806             log_err("unexpected resource type %d for \"%s\" in testdata/testtable32.res - %s\n",
807                     ures_getType(item), testcases[i].key, u_errorName(errorCode));
808             number=-1;
809             break;
810         }
811 
812         if(number>=0 && number!=testcases[i].number) {
813             log_err("\"%s\" in testdata/testtable32.res has a string/int value of %d, expected %d\n",
814                     testcases[i].key, number, testcases[i].number);
815         }
816 
817         key=ures_getKey(item);
818         if(0!=uprv_strcmp(key, testcases[i].key)) {
819             log_err("\"%s\" in testdata/testtable32.res claims to have the key \"%s\"\n",
820                     testcases[i].key, key);
821         }
822     }
823 
824     ures_close(item);
825     ures_close(res);
826 }
827 
TestFileStream(void)828 static void TestFileStream(void){
829     int32_t c = 0;
830     int32_t c1=0;
831     UErrorCode status = U_ZERO_ERROR;
832     const char* testdatapath = loadTestData(&status);
833     char* fileName = (char*) malloc(uprv_strlen(testdatapath) +10);
834     FileStream* stream = NULL;
835     /* these should not be closed */
836     FileStream* pStdin  = T_FileStream_stdin();
837     FileStream* pStdout = T_FileStream_stdout();
838     FileStream* pStderr = T_FileStream_stderr();
839 
840     const char* testline = "This is a test line";
841     int32_t bufLen = (int32_t)strlen(testline)+10;
842     char* buf = (char*) malloc(bufLen);
843     int32_t retLen = 0;
844 
845     if(pStdin==NULL){
846         log_err("failed to get T_FileStream_stdin()");
847     }
848     if(pStdout==NULL){
849         log_err("failed to get T_FileStream_stdout()");
850     }
851     if(pStderr==NULL){
852         log_err("failed to get T_FileStream_stderr()");
853     }
854 
855     uprv_strcpy(fileName,testdatapath);
856     uprv_strcat(fileName,".dat");
857     stream = T_FileStream_open(fileName, "r");
858     if(stream==NULL){
859         log_data_err("T_FileStream_open failed to open %s\n",fileName);
860     } else {
861       if(!T_FileStream_file_exists(fileName)){
862         log_data_err("T_FileStream_file_exists failed to verify existence of %s \n",fileName);
863       }
864 
865       retLen=T_FileStream_read(stream,&c,1);
866       if(retLen==0){
867         log_data_err("T_FileStream_read failed to read from %s \n",fileName);
868       }
869       retLen=0;
870       T_FileStream_rewind(stream);
871       T_FileStream_read(stream,&c1,1);
872       if(c!=c1){
873         log_data_err("T_FileStream_rewind failed to rewind %s \n",fileName);
874       }
875       T_FileStream_rewind(stream);
876       c1 = T_FileStream_peek(stream);
877       if(c!=c1){
878         log_data_err("T_FileStream_peek failed to peekd %s \n",fileName);
879       }
880       c = T_FileStream_getc(stream);
881       T_FileStream_ungetc(c,stream);
882       if(c!= T_FileStream_getc(stream)){
883         log_data_err("T_FileStream_ungetc failed to d %s \n",fileName);
884       }
885 
886       if(T_FileStream_size(stream)<=0){
887         log_data_err("T_FileStream_size failed to d %s \n",fileName);
888       }
889       if(T_FileStream_error(stream)){
890         log_data_err("T_FileStream_error shouldn't have an error %s\n",fileName);
891       }
892       if(!T_FileStream_error(NULL)){
893         log_err("T_FileStream_error didn't get an error %s\n",fileName);
894       }
895       T_FileStream_putc(stream, 0x20);
896       if(!T_FileStream_error(stream)){
897         /*
898           Warning
899           writing to a read-only file may not consistently fail on all platforms
900           (e.g. HP-UX, FreeBSD, MacOSX)
901         */
902         log_verbose("T_FileStream_error didn't get an error when writing to a readonly file %s\n",fileName);
903       }
904 
905       T_FileStream_close(stream);
906     }
907     /* test writing function */
908     stream=NULL;
909     uprv_strcpy(fileName,testdatapath);
910     uprv_strcat(fileName,".tmp");
911     stream = T_FileStream_open(fileName,"w+");
912 
913     if(stream == NULL){
914         log_data_err("Could not open %s for writing\n",fileName);
915     } else {
916       c= '$';
917       T_FileStream_putc(stream,c);
918       T_FileStream_rewind(stream);
919       if(c != T_FileStream_getc(stream)){
920         log_data_err("T_FileStream_putc failed %s\n",fileName);
921       }
922 
923       T_FileStream_rewind(stream);
924       T_FileStream_writeLine(stream,testline);
925       T_FileStream_rewind(stream);
926       T_FileStream_readLine(stream,buf,bufLen);
927       if(uprv_strncmp(testline, buf,uprv_strlen(buf))!=0){
928         log_data_err("T_FileStream_writeLine failed %s\n",fileName);
929       }
930 
931       T_FileStream_rewind(stream);
932       T_FileStream_write(stream,testline,(int32_t)strlen(testline));
933       T_FileStream_rewind(stream);
934       retLen = T_FileStream_read(stream, buf, bufLen);
935       if(uprv_strncmp(testline, buf,retLen)!=0){
936         log_data_err("T_FileStream_write failed %s\n",fileName);
937       }
938 
939       T_FileStream_close(stream);
940     }
941     if(!T_FileStream_remove(fileName)){
942         log_data_err("T_FileStream_remove failed to delete %s\n",fileName);
943     }
944 
945 
946     free(fileName);
947     free(buf);
948 
949 }
950 
TestGetSize(void)951 static void TestGetSize(void) {
952     const struct {
953         const char* key;
954         int32_t size;
955     } test[] = {
956         { "zerotest", 1},
957         { "one", 1},
958         { "importtest", 1},
959         { "integerarray", 1},
960         { "emptyarray", 0},
961         { "emptytable", 0},
962         { "emptystring", 1}, /* empty string is still a string */
963         { "emptyint", 1},
964         { "emptybin", 1},
965         { "testinclude", 1},
966         { "collations", 1}, /* not 2 - there is hidden %%CollationBin */
967     };
968 
969     UErrorCode status = U_ZERO_ERROR;
970 
971     UResourceBundle *rb = NULL;
972     UResourceBundle *res = NULL;
973     UResourceBundle *helper = NULL;
974     const char* testdatapath = loadTestData(&status);
975     int32_t i = 0, j = 0;
976     int32_t size = 0;
977 
978     if(U_FAILURE(status))
979     {
980         log_data_err("Could not load testdata.dat %s\n", u_errorName(status));
981         return;
982     }
983 
984     rb = ures_open(testdatapath, "testtypes", &status);
985     if(U_FAILURE(status))
986     {
987         log_err("Could not testtypes resource bundle %s\n", u_errorName(status));
988         return;
989     }
990 
991     for(i = 0; i < UPRV_LENGTHOF(test); i++) {
992         res = ures_getByKey(rb, test[i].key, res, &status);
993         if(U_FAILURE(status))
994         {
995             log_err("Couldn't find the key %s. Error: %s\n", test[i].key, u_errorName(status));
996             ures_close(rb);
997             return;
998         }
999         size = ures_getSize(res);
1000         if(size != test[i].size) {
1001             log_err("Expected size %i, got size %i for key %s\n", test[i].size, size, test[i].key);
1002             for(j = 0; j < size; j++) {
1003                 helper = ures_getByIndex(res, j, helper, &status);
1004                 log_err("%s\n", ures_getKey(helper));
1005             }
1006         }
1007     }
1008     ures_close(helper);
1009     ures_close(res);
1010     ures_close(rb);
1011 }
1012 
TestGetLocaleByType(void)1013 static void TestGetLocaleByType(void) {
1014     static const struct {
1015         const char *requestedLocale;
1016         const char *resourceKey;
1017         const char *validLocale;
1018         const char *actualLocale;
1019     } test[] = {
1020         { "te_IN_BLAH", "string_only_in_te_IN", "te_IN", "te_IN" },
1021         { "te_IN_BLAH", "string_only_in_te", "te_IN", "te" },
1022         { "te_IN_BLAH", "string_only_in_Root", "te_IN", "root" },
1023         { "te_IN_BLAH_01234567890_01234567890_01234567890_01234567890_01234567890_01234567890", "array_2d_only_in_Root", "te_IN", "root" },
1024         { "te_IN_BLAH@currency=euro", "array_2d_only_in_te_IN", "te_IN", "te_IN" },
1025         { "te_IN_BLAH@collation=phonebook;calendar=thai", "array_2d_only_in_te", "te_IN", "te" }
1026     };
1027 
1028     UErrorCode status = U_ZERO_ERROR;
1029 
1030     UResourceBundle *rb = NULL;
1031     UResourceBundle *res = NULL;
1032     const char* testdatapath = loadTestData(&status);
1033     int32_t i = 0;
1034     const char *locale = NULL;
1035 
1036     if(U_FAILURE(status))
1037     {
1038         log_data_err("Could not load testdata.dat %s\n", u_errorName(status));
1039         return;
1040     }
1041 
1042     for(i = 0; i < UPRV_LENGTHOF(test); i++) {
1043         rb = ures_open(testdatapath, test[i].requestedLocale, &status);
1044         if(U_FAILURE(status))
1045         {
1046             log_err("Could not open resource bundle %s (error %s)\n", test[i].requestedLocale, u_errorName(status));
1047             status = U_ZERO_ERROR;
1048             continue;
1049         }
1050 
1051         res = ures_getByKey(rb, test[i].resourceKey, res, &status);
1052         if(U_FAILURE(status))
1053         {
1054             log_err("Couldn't find the key %s. Error: %s\n", test[i].resourceKey, u_errorName(status));
1055             ures_close(rb);
1056             status = U_ZERO_ERROR;
1057             continue;
1058         }
1059 
1060         locale = ures_getLocaleByType(res, ULOC_REQUESTED_LOCALE, &status);
1061         if(U_SUCCESS(status) && locale != NULL) {
1062             log_err("Requested locale should return NULL\n");
1063         }
1064         status = U_ZERO_ERROR;
1065         locale = ures_getLocaleByType(res, ULOC_VALID_LOCALE, &status);
1066         if(!locale || strcmp(locale, test[i].validLocale) != 0) {
1067             log_err("Expected valid locale to be %s. Got %s\n", test[i].requestedLocale, locale);
1068         }
1069         locale = ures_getLocaleByType(res, ULOC_ACTUAL_LOCALE, &status);
1070         if(!locale || strcmp(locale, test[i].actualLocale) != 0) {
1071             log_err("Expected actual locale to be %s. Got %s\n", test[i].requestedLocale, locale);
1072         }
1073         ures_close(rb);
1074     }
1075     ures_close(res);
1076 }
1077