home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / cintltst / crestst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  16.1 KB  |  625 lines

  1. /*
  2. *****************************************************************************************
  3. *                                                                                       *
  4. * COPYRIGHT:                                                                            *
  5. *   (C) Copyright Taligent, Inc.,  1996                                                 *
  6. *   (C) Copyright International Business Machines Corporation,  1999                    *
  7. *   Licensed Material - Program-Property of IBM - All Rights Reserved.                  *
  8. *   US Government Users Restricted Rights - Use, duplication, or disclosure             *
  9. *   restricted by GSA ADP Schedule Contract with IBM Corp.                              *
  10. *                                                                                       *
  11. *****************************************************************************************
  12. ********************************************************************************
  13. *
  14. * File CRESTST.C
  15. *
  16. * Modification History:
  17. *        Name                     Description
  18. *     Madhu Katragadda            Ported for C API
  19. *  06/14/99     stephen           Updated for RB API changes (no suffix).
  20. *********************************************************************************
  21. */
  22.  
  23.  
  24. #include "utypes.h"
  25. #include "cintltst.h"
  26. #include "utypes.h"
  27. #include  "ustring.h"
  28. #include "string.h"
  29. #include <time.h>
  30.  
  31. #define RESTEST_HEAP_CHECK 0
  32.  
  33. #include "uloc.h"
  34. #include "ures.h"
  35. #include "crestst.h"
  36. #include "ctest.h"
  37.  
  38. void TestFallback();
  39.  
  40. /*****************************************************************************/
  41.  
  42. /**
  43.  * Convert an integer, positive or negative, to a character string radix 10.
  44.  */
  45. static char*
  46. itoa1(int32_t i, char* buf)
  47. {
  48.   char *p = 0;
  49.   char* result = buf;
  50.   /* Handle negative */
  51.   if(i < 0) {
  52.     *buf++ = '-';
  53.     i = -i;
  54.   }
  55.  
  56.   /* Output digits in reverse order */
  57.   p = buf;
  58.   do {
  59.     *p++ = '0' + (i % 10);
  60.     i /= 10;
  61.   }
  62.   while(i);
  63.   *p-- = 0;
  64.  
  65.   /* Reverse the string */
  66.   while(buf < p) {
  67.     char c = *buf;
  68.     *buf++ = *p;
  69.     *p-- = c;
  70.   }
  71.  
  72.   return result;
  73. }
  74.  
  75. const UChar kERROR[] = { 0x0045 /*E*/, 0x0052 /*'R'*/, 0x0052 /*'R'*/,
  76.              0x004F /*'O'*/, 0x0052/*'R'*/, 0x0000 /*'\0'*/};
  77.  
  78. /*****************************************************************************/
  79.  
  80. enum E_Where
  81. {
  82.   e_Default,
  83.   e_te,
  84.   e_te_IN,
  85.   e_Where_count
  86. };
  87. typedef enum E_Where E_Where;
  88. /*****************************************************************************/
  89.  
  90. #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)); pass=FALSE; }
  91.  
  92. #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)); pass=FALSE; }
  93.  
  94.  
  95. /* Array of our test objects */
  96.  
  97. static struct
  98. {
  99.   const char* name;
  100.   UErrorCode expected_constructor_status;
  101.   E_Where where;
  102.   bool_t like[e_Where_count];
  103.   bool_t inherits[e_Where_count];
  104. }
  105. param[] =
  106. {
  107.   /* "te" means test */
  108.   /* "IN" means inherits */
  109.   /* "NE" or "ne" means "does not exist" */
  110.  
  111.   { "default",             U_ZERO_ERROR,             e_Default,      { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } },
  112.   { "te",                  U_ZERO_ERROR,             e_te,           { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } },
  113.   { "te_IN",               U_ZERO_ERROR,             e_te_IN,        { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } },
  114.   { "te_NE",               U_USING_FALLBACK_ERROR,   e_te,           { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } },
  115.   { "te_IN_NE",            U_USING_FALLBACK_ERROR,   e_te_IN,        { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } },
  116.   { "ne",                  U_USING_DEFAULT_ERROR,    e_Default,      { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } }
  117. };
  118.  
  119. static int32_t bundles_count = sizeof(param) / sizeof(param[0]);
  120.  
  121.  
  122.  
  123. /***************************************************************************************/
  124.  
  125. /* Array of our test objects */
  126.  
  127. void addResourceBundleTest(TestNode** root)
  128. {
  129.   setUpDataTable();
  130.  
  131.   addTest(root, &TestConstruction1, "tsutil/crestst/TestConstruction1");
  132.   addTest(root, &TestConstruction2, "tsutil/crestst/TestConstruction2");
  133.   addTest(root, &TestResourceBundles, "tsutil/crestst/TestResourceBundle");
  134.   addTest(root, &TestFallback, "tsutil/crestst/TestFallback");
  135.  
  136. }
  137.  
  138.  
  139. /***************************************************************************************/
  140.  
  141. void TestResourceBundles()
  142. {
  143.  
  144.   testTag("only_in_Default", TRUE, FALSE, FALSE);
  145.   testTag("in_Default_te", TRUE, TRUE, FALSE);
  146.   testTag("in_Default_te_te_IN", TRUE, TRUE, TRUE);
  147.   testTag("in_Default_te_IN", TRUE, FALSE, TRUE);
  148.   testTag("only_in_te", FALSE, TRUE, FALSE);
  149.   testTag("only_in_te_IN", FALSE, FALSE, TRUE);
  150.   testTag("in_te_te_IN", FALSE, TRUE, TRUE);
  151.   testTag("nonexistent", FALSE, FALSE, FALSE);
  152.  
  153.   log_verbose("Passed:=  %d   Failed=   %d \n", pass, fail);
  154.  
  155. }
  156.  
  157.  
  158. void TestConstruction1()
  159. {
  160.   UResourceBundle *test1 = 0, *test2 = 0;
  161.   const UChar *result1, *result2;
  162.   UErrorCode status= U_ZERO_ERROR;
  163.   UErrorCode   err = U_ZERO_ERROR;
  164.   const char*        directory;
  165.   const char*      locale="te_IN";
  166.  
  167.   directory= ctest_getTestDirectory();
  168.   log_verbose("Testing ures_open()......\n");
  169.   test1=ures_open(directory, NULL, &err);
  170.   test2=ures_open(directory, locale, &err);
  171.  
  172.   if(U_FAILURE(err))
  173.     {
  174.       log_err("construction did not succeed :  %s \n", myErrorName(status));
  175.       return;
  176.     }
  177.  
  178.   result1= ures_get(test1, "string_in_Default_te_te_IN", &err);
  179.   result2= ures_get(test2, "string_in_Default_te_te_IN", &err);
  180.  
  181.  
  182.   if (U_FAILURE(err)) {
  183.  
  184.     log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(status));
  185.     return;
  186.   }
  187.  
  188.  
  189.   log_verbose("for string_in_Default_te_te_IN, default.txt had  %s\n", austrdup(result1));
  190.   log_verbose("for string_in_Default_te_te_IN, te_IN.txt had %s\n", austrdup(result2));
  191.  
  192.  
  193.   /* Test getVersionNumber*/
  194.   log_verbose("Testing version number\n");
  195.   log_verbose("for getVersionNumber :  %s\n", ures_getVersionNumber(test1));
  196.  
  197.   ures_close(test1);
  198.   ures_close(test2);
  199. }
  200.  
  201. void TestConstruction2()
  202. {
  203.   int n;
  204.   UChar temp[7];
  205.   UResourceBundle *test4 = 0;
  206.   const UChar*   result4;
  207.   UErrorCode   err = U_ZERO_ERROR;
  208.   const char*     directory;
  209.   const char*    locale="te_IN";
  210.   wchar_t widedirectory[256];
  211.   directory= ctest_getTestDirectory();
  212.   mbstowcs(widedirectory, directory, 256);
  213.  
  214.   log_verbose("Testing ures_openW().......\n");
  215.  
  216.   test4=ures_openW(widedirectory, locale, &err);
  217.   if(U_FAILURE(err)){
  218.     log_err("Error in the construction using ures_openW():  %s\n", myErrorName(err));
  219.     return;
  220.   }
  221.  
  222.   result4=ures_get(test4, "string_in_Default_te_te_IN", &err);
  223.  
  224.   if (U_FAILURE(err)) {
  225.     log_err("Something threw an error in TestConstruction()  %s\n", myErrorName(err));
  226.     return;
  227.   }
  228.  
  229.   log_verbose("for string_in_Default_te_te_IN, te_IN.txt had  %s\n", austrdup(result4));
  230.   u_uastrcpy(temp, "TE_IN");
  231.  
  232.   if(u_strcmp(result4, temp)!=0)
  233.   {
  234.  
  235.     log_err("Construction test failed for ures_openW();\n");
  236.     if(!VERBOSITY)
  237.          log_info("(run verbose for more information)\n");
  238.  
  239.       log_verbose("\nGot->");
  240.     for(n=0;result4[n];n++)
  241.        {
  242.          log_verbose("%04X ",result4[n]);
  243.        }
  244.     log_verbose("<\n");
  245.  
  246.     log_verbose("\nWant>");
  247.     for(n=0;temp[n];n++)
  248.        {
  249.          log_verbose("%04X ",temp[n]);
  250.        }
  251.     log_verbose("<\n");
  252.  
  253.   }
  254.  
  255.   ures_close(test4);
  256. }
  257.  
  258. /*****************************************************************************/
  259. /*****************************************************************************/
  260.  
  261. bool_t testTag(const char* frag,
  262.            bool_t in_Default,
  263.            bool_t in_te,
  264.            bool_t in_te_IN)
  265. {
  266.   bool_t pass=TRUE;
  267.  
  268.   /* Make array from input params */
  269.  
  270.   bool_t is_in[3];
  271.   const char *NAME[] = { "DEFAULT", "TE", "TE_IN" };
  272.  
  273.   /* Now try to load the desired items */
  274.   UResourceBundle* theBundle = NULL;
  275.   char tag[99];
  276.   char action[256];
  277.   UErrorCode expected_status,status = U_ZERO_ERROR,expected_resource_status = U_ZERO_ERROR;
  278.   UChar* base = NULL;
  279.   UChar* expected_string = NULL;
  280.   const UChar* string = NULL;
  281.   char buf[5];
  282.   char item_tag[10];
  283.   int32_t i,j,k,row,col;
  284.   int32_t actual_bundle;
  285.   int32_t count = 0;
  286.   int32_t row_count=0;
  287.   int32_t column_count=0;
  288.   int32_t index = 0;
  289.   const char*    directory =  ctest_getTestDirectory();
  290.  
  291.   is_in[0] = in_Default;
  292.   is_in[1] = in_te;
  293.   is_in[2] = in_te_IN;
  294.  
  295.   strcpy(item_tag, "tag");
  296.  
  297.   for (i=0; i<bundles_count; ++i)
  298.     {
  299.       strcpy(action,"construction for");
  300.       strcat(action, param[i].name);
  301.  
  302.  
  303.       status = U_ZERO_ERROR;
  304.  
  305.  
  306.       theBundle = ures_open(directory, param[i].name, &status);
  307.  
  308.       CONFIRM_ErrorCode(status,param[i].expected_constructor_status);
  309.  
  310.       if(i == 5)
  311.     actual_bundle = 0; /* ne -> default */
  312.       else if(i == 3)
  313.     actual_bundle = 1; /* te_NE -> te */
  314.       else if(i == 4)
  315.     actual_bundle = 2; /* te_IN_NE -> te_IN */
  316.       else
  317.     actual_bundle = i;
  318.  
  319.       expected_resource_status = U_MISSING_RESOURCE_ERROR;
  320.       for (j=e_te_IN; j>=e_Default; --j)
  321.         {
  322.       if (is_in[j] && param[i].inherits[j])
  323.             {
  324.           
  325.           if(j == actual_bundle) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
  326.         expected_resource_status = U_ZERO_ERROR;
  327.           else if(j == 0)
  328.         expected_resource_status = U_USING_DEFAULT_ERROR;
  329.           else
  330.         expected_resource_status = U_USING_FALLBACK_ERROR;
  331.  
  332.           log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>.  actual_bundle=%s\n",
  333.               param[i].name, 
  334.               i,
  335.               frag,
  336.               j,
  337.               is_in[j]?"Yes":"No",
  338.               j,
  339.               param[i].inherits[j]?"Yes":"No",
  340.               param[actual_bundle].name);
  341.  
  342.           break;
  343.             }
  344.         }
  345.  
  346.       for (j=param[i].where; j>=0; --j)
  347.         {
  348.       if (is_in[j])
  349.         {
  350.           base=(UChar*)malloc(sizeof(UChar)*(strlen(NAME[j]) + 1));
  351.           u_uastrcpy(base,NAME[j]);
  352.  
  353.           break;
  354.             }
  355.       else {
  356.         base = (UChar*) malloc(sizeof(UChar) * 1);
  357.         *base = 0x0000;
  358.       }
  359.         }
  360.  
  361.       /*-------------------------------------------------------------------- */
  362.       /* string */
  363.  
  364.       strcpy(tag,"string_");
  365.       strcat(tag,frag);
  366.  
  367.       strcpy(action,param[i].name);
  368.       strcat(action, ".ures_get(" );
  369.       strcat(action,tag);
  370.       strcat(action, ")");
  371.  
  372.       string=    kERROR;
  373.  
  374.       status = U_ZERO_ERROR;
  375.  
  376.       ures_get(theBundle, tag, &status);
  377.       if(U_SUCCESS(status))
  378.     {
  379.       status = U_ZERO_ERROR;
  380.       string=ures_get(theBundle, tag, &status);
  381.     }
  382.  
  383.       log_verbose("%s got %d, expected %d\n", action, status, expected_resource_status);
  384.  
  385.       CONFIRM_ErrorCode(status, expected_resource_status);
  386.  
  387.  
  388.       if(U_SUCCESS(status)){
  389.     expected_string=(UChar*)malloc(sizeof(UChar)*(u_strlen(base) + 3));
  390.     u_strcpy(expected_string,base);
  391.     
  392.     
  393.       }
  394.       else
  395.     {
  396.       expected_string = (UChar*)malloc(sizeof(UChar)*(u_strlen(kERROR) + 1));
  397.       u_strcpy(expected_string,kERROR);
  398.  
  399.     }
  400.  
  401.  
  402.  
  403.       CONFIRM_EQ(string, expected_string);
  404.  
  405.  
  406.  
  407.       /*-------------------------------------------------------------------- */
  408.       /*-------------------------------------------------------------------- */
  409.       /* arrayItem */
  410.  
  411.       strcpy(tag,"array_");
  412.       strcat(tag,frag);
  413.  
  414.       strcpy(action,param[i].name);
  415.       strcat(action, ".ures_getArrayItem(");
  416.       strcat(action, tag);
  417.       strcat(action, ")");
  418.       count=ures_countArrayItems(theBundle, tag, &status);
  419.  
  420.  
  421.       for(j = 0; j < count; j++)
  422.         {
  423.  
  424.       status = U_ZERO_ERROR;
  425.       string = kERROR;
  426.  
  427.       index=j;
  428.       ures_getArrayItem(theBundle, tag,index , &status);
  429.       if(U_SUCCESS(status))
  430.         string=ures_getArrayItem(theBundle, tag, index, &status);
  431.  
  432.  
  433.       /* how could 'index==j' ever be >= count ? */
  434.       expected_status = (index >= 0 && index < count) ? expected_resource_status : U_MISSING_RESOURCE_ERROR;
  435.  
  436.       log_verbose("Status for %s was %d, expected %d\n", action, status, expected_status);
  437.  
  438.       CONFIRM_ErrorCode(status,expected_status);
  439.  
  440.       if (U_SUCCESS(status))
  441.             {
  442.           UChar element[3];
  443.  
  444.           u_uastrcpy(element, itoa1(index,buf));
  445.  
  446.           u_strcpy(expected_string,base);
  447.           u_strcat(expected_string,element);
  448.  
  449.  
  450.             }
  451.       else
  452.             {
  453.           u_strcpy(expected_string,kERROR);
  454.             }
  455.       CONFIRM_EQ(string,expected_string);
  456.         }
  457.  
  458.  
  459.       /*-------------------------------------------------------------------- */
  460.       /* 2dArrayItem */
  461.  
  462.       strcpy(tag,"array_2d_");
  463.       strcat(tag,frag);
  464.  
  465.       strcpy(action,param[i].name);
  466.       strcat(action, ".get2dArrayItem(");
  467.       strcat(action, tag);
  468.       strcat(action, ")");
  469.       row_count=ures_countArrayItems(theBundle, tag, &status);
  470.       column_count=2;
  471.  
  472.       for(k=0;k<row_count;k++){
  473.     for (j=0; j<column_count; ++j){
  474.  
  475.       status = U_ZERO_ERROR;
  476.       string = kERROR;
  477.       row=k;
  478.       col=j;
  479.       ures_get2dArrayItem( theBundle, tag, row, col, &status);
  480.       if(U_SUCCESS(status))
  481.         string=ures_get2dArrayItem(theBundle, tag, row, col, &status);
  482.  
  483.  
  484.       expected_status = (row >= 0 && row < row_count && col >= 0 && col < column_count) ?
  485.         expected_resource_status : U_MISSING_RESOURCE_ERROR;
  486.  
  487.       CONFIRM_ErrorCode(status,expected_status);
  488.  
  489.       if (U_SUCCESS(status))
  490.     {
  491.           UChar element[3];
  492.           u_strcpy(expected_string,base);
  493.           u_uastrcpy(element,itoa1(row,buf));
  494.           u_strcat(expected_string, element);
  495.           u_uastrcpy(element,itoa1(col,buf));
  496.           u_strcat(expected_string, element);
  497.       
  498.     }
  499.       else
  500.     {
  501.       
  502.           u_strcpy(expected_string,kERROR);
  503.     }
  504.       CONFIRM_EQ(string,expected_string);
  505.     }
  506.       }
  507.  
  508.  
  509.  
  510.       /*-------------------------------------------------------------------- */
  511.       /* taggedArrayItem */
  512.  
  513.       strcpy(tag,"tagged_array_");
  514.       strcat(tag,frag);
  515.  
  516.       strcpy(action,param[i].name);
  517.       strcat(action,".getTaggedArrayItem(");
  518.       strcat(action, tag);
  519.       strcat(action,")");
  520.  
  521.       count = 0;
  522.       for (index=-20; index<20; ++index)
  523.         {
  524.       strcpy(item_tag, "tag");
  525.       strcat(item_tag, itoa1(index,buf));
  526.  
  527.       status = U_ZERO_ERROR;
  528.       string = kERROR;
  529.  
  530.  
  531.       ures_getTaggedArrayItem( theBundle, tag, item_tag, &status);
  532.       if(U_SUCCESS(status))
  533.         string=ures_getTaggedArrayItem(theBundle, tag, item_tag, &status);
  534.  
  535.  
  536.       if (index < 0)
  537.             {
  538.           CONFIRM_ErrorCode(status,U_MISSING_RESOURCE_ERROR);
  539.             }
  540.       else
  541.     {
  542.           UChar* element;
  543.           if (strcmp(myErrorName(status),"U_MISSING_RESOURCE_ERROR")!=0) {
  544.         count++;
  545.         u_strcpy(expected_string,base);
  546.         element=(UChar*)malloc(sizeof(UChar) * (strlen(buf)+1));
  547.         u_uastrcpy(element,buf);
  548.         u_strcat(expected_string,element);
  549.         free(element);
  550.         CONFIRM_EQ(string,expected_string);
  551.           }
  552.     }
  553.       
  554.         }
  555.       
  556.       free(expected_string);
  557.       free(base);
  558.       ures_close(theBundle);
  559.     }
  560.   return pass;
  561. }
  562.  
  563. void record_pass()
  564. {
  565.   ++pass;
  566. }
  567.  
  568. void record_fail()
  569. {
  570.   ++fail;
  571. }
  572.  
  573. /**
  574.  * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR
  575.  * are set correctly
  576.  */
  577.  
  578. void TestFallback()
  579. {
  580.   UErrorCode status = U_ZERO_ERROR;
  581.   UResourceBundle *fr_FR = NULL;
  582.   const UChar *junk; /* ignored */
  583.   
  584.   log_verbose("Opening fr_FR..");
  585.   fr_FR = ures_open(NULL, "fr_FR", &status);
  586.   if(U_FAILURE(status))
  587.     {
  588.       log_err("Couldn't open fr_FR - %d\n", status);
  589.       return;
  590.     }
  591.  
  592.   status = U_ZERO_ERROR;
  593.  
  594.  
  595.   /* clear it out..  just do some calls to get the gears turning */
  596.   junk = ures_get(fr_FR, "LocaleID", &status);
  597.   status = U_ZERO_ERROR;
  598.   junk = ures_get(fr_FR, "LocaleString", &status);
  599.   status = U_ZERO_ERROR;
  600.   junk = ures_get(fr_FR, "LocaleID", &status);
  601.   status = U_ZERO_ERROR;
  602.  
  603.   /* OK first one. This should be a Default value. */
  604.   junk = ures_get(fr_FR, "Version", &status);
  605.   if(status != U_USING_DEFAULT_ERROR)
  606.     {
  607.       log_err("Expected U_USING_DEFAULT_ERROR when trying to get Version from fr_FR, got %d\n", 
  608.           status);
  609.     }
  610.   
  611.   status = U_ZERO_ERROR;
  612.  
  613.   /* and this is a Fallback, to fr */
  614.   junk = ures_get(fr_FR, "ShortLanguage", &status);
  615.   if(status != U_USING_FALLBACK_ERROR)
  616.     {
  617.       log_err("Expected U_USING_FALLBACK_ERROR when trying to get ShortLanguage from fr_FR, got %d\n", 
  618.           status);
  619.     }
  620.   
  621.   status = U_ZERO_ERROR;
  622.   
  623.   ures_close(fr_FR);
  624. }
  625.