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