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