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