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