1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2014, 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
31static void TestOpenDirect(void);
32static void TestFallback(void);
33static void TestTable32(void);
34static void TestFileStream(void);
35/*****************************************************************************/
36
37const UChar kERROR[] = { 0x0045 /*E*/, 0x0052 /*'R'*/, 0x0052 /*'R'*/,
38             0x004F /*'O'*/, 0x0052/*'R'*/, 0x0000 /*'\0'*/};
39
40/*****************************************************************************/
41
42enum E_Where
43{
44  e_Root,
45  e_te,
46  e_te_IN,
47  e_Where_count
48};
49typedef enum E_Where E_Where;
50/*****************************************************************************/
51
52#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)); }
53
54#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)); }
55
56
57/* Array of our test objects */
58
59static struct
60{
61  const char* name;
62  UErrorCode expected_constructor_status;
63  E_Where where;
64  UBool like[e_Where_count];
65  UBool inherits[e_Where_count];
66} param[] =
67{
68  /* "te" means test */
69  /* "IN" means inherits */
70  /* "NE" or "ne" means "does not exist" */
71
72  { "root",         U_ZERO_ERROR,             e_Root,    { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } },
73  { "te",           U_ZERO_ERROR,             e_te,      { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } },
74  { "te_IN",        U_ZERO_ERROR,             e_te_IN,   { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } },
75  { "te_NE",        U_USING_FALLBACK_WARNING, e_te,      { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } },
76  { "te_IN_NE",     U_USING_FALLBACK_WARNING, e_te_IN,   { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } },
77  { "ne",           U_USING_DEFAULT_WARNING,  e_Root,    { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } }
78};
79
80static int32_t bundles_count = sizeof(param) / sizeof(param[0]);
81
82
83
84/***************************************************************************************/
85
86/* Array of our test objects */
87
88void addResourceBundleTest(TestNode** root);
89
90void addResourceBundleTest(TestNode** root)
91{
92#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
93    addTest(root, &TestConstruction1, "tsutil/crestst/TestConstruction1");
94    addTest(root, &TestOpenDirect, "tsutil/crestst/TestOpenDirect");
95    addTest(root, &TestResourceBundles, "tsutil/crestst/TestResourceBundles");
96    addTest(root, &TestTable32, "tsutil/crestst/TestTable32");
97    addTest(root, &TestFileStream, "tsutil/crestst/TestFileStream");
98    addTest(root, &TestGetSize, "tsutil/crestst/TestGetSize");
99    addTest(root, &TestGetLocaleByType, "tsutil/crestst/TestGetLocaleByType");
100#endif
101    addTest(root, &TestFallback, "tsutil/crestst/TestFallback");
102    addTest(root, &TestAliasConflict, "tsutil/crestst/TestAliasConflict");
103
104}
105
106
107/***************************************************************************************/
108void TestAliasConflict(void) {
109    UErrorCode status = U_ZERO_ERROR;
110    UResourceBundle *he = NULL;
111    UResourceBundle *iw = NULL;
112    const UChar *result = NULL;
113    int32_t resultLen;
114
115    he = ures_open(NULL, "he", &status);
116    iw = ures_open(NULL, "iw", &status);
117    if(U_FAILURE(status)) {
118        log_err_status(status, "Failed to get resource with %s\n", myErrorName(status));
119    }
120    ures_close(iw);
121    result = ures_getStringByKey(he, "ExemplarCharacters", &resultLen, &status);
122    if(U_FAILURE(status) || result == NULL) {
123        log_err_status(status, "Failed to get resource with %s\n", myErrorName(status));
124    }
125    ures_close(he);
126}
127
128
129void TestResourceBundles()
130{
131    UErrorCode status = U_ZERO_ERROR;
132    loadTestData(&status);
133    if(U_FAILURE(status)) {
134        log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status));
135        return;
136    }
137
138    testTag("only_in_Root", TRUE, FALSE, FALSE);
139    testTag("in_Root_te", TRUE, TRUE, FALSE);
140    testTag("in_Root_te_te_IN", TRUE, TRUE, TRUE);
141    testTag("in_Root_te_IN", TRUE, FALSE, TRUE);
142    testTag("only_in_te", FALSE, TRUE, FALSE);
143    testTag("only_in_te_IN", FALSE, FALSE, TRUE);
144    testTag("in_te_te_IN", FALSE, TRUE, TRUE);
145    testTag("nonexistent", FALSE, FALSE, FALSE);
146
147    log_verbose("Passed:=  %d   Failed=   %d \n", pass, fail);
148}
149
150void TestConstruction1()
151{
152    UResourceBundle *test1 = 0, *test2 = 0;
153    const UChar *result1, *result2;
154    int32_t resultLen;
155    UChar temp[7];
156
157    UErrorCode   err = U_ZERO_ERROR;
158    const char* testdatapath ;
159    const char*      locale="te_IN";
160
161    log_verbose("Testing ures_open()......\n");
162
163
164    testdatapath=loadTestData(&err);
165    if(U_FAILURE(err))
166    {
167        log_data_err("Could not load testdata.dat %s \n",myErrorName(err));
168        return;
169    }
170
171    test1=ures_open(testdatapath, NULL, &err);
172    if(U_FAILURE(err))
173    {
174        log_err("construction of %s did not succeed :  %s \n",NULL, myErrorName(err));
175        return;
176    }
177
178
179    test2=ures_open(testdatapath, locale, &err);
180    if(U_FAILURE(err))
181    {
182        log_err("construction of %s did not succeed :  %s \n",locale, myErrorName(err));
183        return;
184    }
185    result1= ures_getStringByKey(test1, "string_in_Root_te_te_IN", &resultLen, &err);
186    result2= ures_getStringByKey(test2, "string_in_Root_te_te_IN", &resultLen, &err);
187
188
189    if (U_FAILURE(err)) {
190        log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(err));
191        return;
192    }
193
194    u_uastrcpy(temp, "TE_IN");
195
196    if(u_strcmp(result2, temp)!=0)
197    {
198        int n;
199
200        log_err("Construction test failed for ures_open();\n");
201        if(!getTestOption(VERBOSITY_OPTION))
202            log_info("(run verbose for more information)\n");
203
204        log_verbose("\nGot->");
205        for(n=0;result2[n];n++)
206        {
207            log_verbose("%04X ",result2[n]);
208        }
209        log_verbose("<\n");
210
211        log_verbose("\nWant>");
212        for(n=0;temp[n];n++)
213        {
214            log_verbose("%04X ",temp[n]);
215        }
216        log_verbose("<\n");
217
218    }
219
220    log_verbose("for string_in_Root_te_te_IN, default.txt had  %s\n", austrdup(result1));
221    log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", austrdup(result2));
222
223    /* Test getVersionNumber*/
224    log_verbose("Testing version number\n");
225    log_verbose("for getVersionNumber :  %s\n", ures_getVersionNumber(test1));
226
227    ures_close(test1);
228    ures_close(test2);
229}
230
231/*****************************************************************************/
232/*****************************************************************************/
233
234UBool testTag(const char* frag,
235           UBool in_Root,
236           UBool in_te,
237           UBool in_te_IN)
238{
239    int32_t passNum=pass;
240
241    /* Make array from input params */
242
243    UBool is_in[3];
244    const char *NAME[] = { "ROOT", "TE", "TE_IN" };
245
246    /* Now try to load the desired items */
247    UResourceBundle* theBundle = NULL;
248    char tag[99];
249    char action[256];
250    UErrorCode status = U_ZERO_ERROR,expected_resource_status = U_ZERO_ERROR;
251    UChar* base = NULL;
252    UChar* expected_string = NULL;
253    const UChar* string = NULL;
254    char item_tag[10];
255    int32_t i,j;
256    int32_t actual_bundle;
257    int32_t resultLen;
258    const char *testdatapath = loadTestData(&status);
259
260    is_in[0] = in_Root;
261    is_in[1] = in_te;
262    is_in[2] = in_te_IN;
263
264    strcpy(item_tag, "tag");
265
266    status = U_ZERO_ERROR;
267    theBundle = ures_open(testdatapath, "root", &status);
268    if(U_FAILURE(status))
269    {
270        ures_close(theBundle);
271        log_err("Couldn't open root bundle in %s", testdatapath);
272        return FALSE;
273    }
274    ures_close(theBundle);
275    theBundle = NULL;
276
277
278    for (i=0; i<bundles_count; ++i)
279    {
280        strcpy(action,"construction for");
281        strcat(action, param[i].name);
282
283
284        status = U_ZERO_ERROR;
285
286        theBundle = ures_open(testdatapath, param[i].name, &status);
287        /*theBundle = ures_open("c:\\icu\\icu\\source\\test\\testdata\\testdata", param[i].name, &status);*/
288
289        CONFIRM_ErrorCode(status,param[i].expected_constructor_status);
290
291
292
293        if(i == 5)
294            actual_bundle = 0; /* ne -> default */
295        else if(i == 3)
296            actual_bundle = 1; /* te_NE -> te */
297        else if(i == 4)
298            actual_bundle = 2; /* te_IN_NE -> te_IN */
299        else
300            actual_bundle = i;
301
302        expected_resource_status = U_MISSING_RESOURCE_ERROR;
303        for (j=e_te_IN; j>=e_Root; --j)
304        {
305            if (is_in[j] && param[i].inherits[j])
306            {
307
308                if(j == actual_bundle) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
309                    expected_resource_status = U_ZERO_ERROR;
310                else if(j == 0)
311                    expected_resource_status = U_USING_DEFAULT_WARNING;
312                else
313                    expected_resource_status = U_USING_FALLBACK_WARNING;
314
315                log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>.  actual_bundle=%s\n",
316                            param[i].name,
317                            i,
318                            frag,
319                            j,
320                            is_in[j]?"Yes":"No",
321                            j,
322                            param[i].inherits[j]?"Yes":"No",
323                            param[actual_bundle].name);
324
325                break;
326            }
327        }
328
329        for (j=param[i].where; j>=0; --j)
330        {
331            if (is_in[j])
332            {
333                if(base != NULL) {
334                    free(base);
335                    base = NULL;
336                }
337
338                base=(UChar*)malloc(sizeof(UChar)*(strlen(NAME[j]) + 1));
339                u_uastrcpy(base,NAME[j]);
340
341                break;
342            }
343            else {
344                if(base != NULL) {
345                    free(base);
346                    base = NULL;
347                }
348                base = (UChar*) malloc(sizeof(UChar) * 1);
349                *base = 0x0000;
350            }
351        }
352
353        /*-------------------------------------------------------------------- */
354        /* string */
355
356        strcpy(tag,"string_");
357        strcat(tag,frag);
358
359        strcpy(action,param[i].name);
360        strcat(action, ".ures_get(" );
361        strcat(action,tag);
362        strcat(action, ")");
363
364        string=    kERROR;
365
366        status = U_ZERO_ERROR;
367
368        ures_getStringByKey(theBundle, tag, &resultLen, &status);
369        if(U_SUCCESS(status))
370        {
371            status = U_ZERO_ERROR;
372            string=ures_getStringByKey(theBundle, tag, &resultLen, &status);
373        }
374
375        log_verbose("%s got %d, expected %d\n", action, status, expected_resource_status);
376
377        CONFIRM_ErrorCode(status, expected_resource_status);
378
379
380        if(U_SUCCESS(status)){
381            expected_string=(UChar*)malloc(sizeof(UChar)*(u_strlen(base) + 3));
382            u_strcpy(expected_string,base);
383
384        }
385        else
386        {
387            expected_string = (UChar*)malloc(sizeof(UChar)*(u_strlen(kERROR) + 1));
388            u_strcpy(expected_string,kERROR);
389
390        }
391
392        CONFIRM_EQ(string, expected_string);
393
394        free(expected_string);
395        ures_close(theBundle);
396    }
397    free(base);
398    return (UBool)(passNum == pass);
399}
400
401void record_pass()
402{
403  ++pass;
404}
405
406void record_fail()
407{
408  ++fail;
409}
410
411/**
412 * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR
413 * are set correctly
414 */
415
416static void TestFallback()
417{
418    UErrorCode status = U_ZERO_ERROR;
419    UResourceBundle *fr_FR = NULL;
420    UResourceBundle *subResource = NULL;
421    const UChar *junk; /* ignored */
422    int32_t resultLen;
423
424    log_verbose("Opening fr_FR..");
425    fr_FR = ures_open(NULL, "fr_FR", &status);
426    if(U_FAILURE(status))
427    {
428        log_err_status(status, "Couldn't open fr_FR - %s\n", u_errorName(status));
429        return;
430    }
431
432    status = U_ZERO_ERROR;
433
434
435    /* clear it out..  just do some calls to get the gears turning */
436    junk = ures_getStringByKey(fr_FR, "LocaleID", &resultLen, &status);
437    (void)junk;    /* Suppress set but not used warning. */
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, "layout", NULL, &status);
446    if(status != U_USING_DEFAULT_WARNING)
447    {
448        log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get layout 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, "ExemplarCharacters", &resultLen, &status);
456    if(status != U_USING_FALLBACK_WARNING)
457    {
458        log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get ExemplarCharacters 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
467static void
468TestOpenDirect(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_data_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_data_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_data_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_data_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_data_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_data_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
613static int32_t
614parseTable32Key(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
628static void
629TestTable32(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<UPRV_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
766static 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
889static 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_data_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
951static 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_data_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(U_SUCCESS(status) && locale != NULL) {
1000            log_err("Requested locale should return NULL\n");
1001        }
1002        status = U_ZERO_ERROR;
1003        locale = ures_getLocaleByType(res, ULOC_VALID_LOCALE, &status);
1004        if(!locale || strcmp(locale, test[i].validLocale) != 0) {
1005            log_err("Expected valid locale to be %s. Got %s\n", test[i].requestedLocale, locale);
1006        }
1007        locale = ures_getLocaleByType(res, ULOC_ACTUAL_LOCALE, &status);
1008        if(!locale || strcmp(locale, test[i].actualLocale) != 0) {
1009            log_err("Expected actual locale to be %s. Got %s\n", test[i].requestedLocale, locale);
1010        }
1011        ures_close(rb);
1012    }
1013    ures_close(res);
1014}
1015