home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / cintltst / citertst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  11.4 KB  |  393 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 CITERTST.C
  15. *
  16. * Modification History:
  17. *        Name                     Description            
  18. *     Madhu Katragadda            Ported for C API
  19. *********************************************************************************
  20. /*
  21.  * Collation Iterator tests.
  22.  * (Let me reiterate my position...)
  23.  */
  24.  
  25. #include "utypes.h"
  26. #include "ucol.h"
  27. #include "uloc.h"
  28. #include "cintltst.h"
  29. #include "citertst.h"
  30. #include "ustring.h"
  31.  
  32. #include <memory.h>
  33.  
  34. #define ARRAY_LENGTH(array) (sizeof array / sizeof array[0])
  35.  
  36. static UErrorCode status = U_ZERO_ERROR;
  37. UCollator *en_us;
  38.  
  39.  
  40. void addCollIterTest(TestNode** root)
  41. {
  42.     
  43.     
  44.     addTest(root, &TestPrevious, "tscoll/citertst/TestPrevious");
  45.     addTest(root, &TestOffset, "tscoll/citertst/TestOffset");
  46.     addTest(root, &TestSetText, "tscoll/citertst/TestSetText");
  47.     
  48. }
  49.  
  50.  
  51. /**
  52.  * Test for CollationElementIterator.previous()
  53.  *
  54.  * @bug 4108758 - Make sure it works with contracting characters
  55.  * 
  56.  */
  57. void TestPrevious()
  58. {
  59.     UChar rule[50];
  60.     UChar *source;
  61.     UCollator *c1, *c2, *c3;
  62.     UCollationElements *iter;
  63.     UErrorCode status = U_ZERO_ERROR;
  64.     test1=(UChar*)malloc(sizeof(UChar) * 50);
  65.     test2=(UChar*)malloc(sizeof(UChar) * 50);
  66.     u_uastrcpy(test1, "What subset of all possible test cases?");
  67.     u_uastrcpy(test2, "has the highest probability of detecting");
  68.     en_us = ucol_open("en_US", &status);
  69.     
  70.     iter=ucol_openElements(en_us, test1, u_strlen(test1), &status);
  71.     if(U_FAILURE(status)){
  72.         log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n", 
  73.             myErrorName(status));
  74.         return;
  75.     }
  76.     /* A basic test to see if it's working at all */
  77.     backAndForth(iter);
  78.     ucol_closeElements(iter);
  79.  
  80.     /* Test with a contracting character sequence */
  81.     u_uastrcpy(rule, " < a,A < b,B < c,C, d,D < z,Z < ch,cH,Ch,CH");
  82.     c1 = ucol_openRules(rule, u_strlen(rule), UCOL_NO_NORMALIZATION, UCOL_DEFAULT_STRENGTH, &status);
  83.     if (c1 == NULL || U_FAILURE(status))
  84.     {
  85.         log_err("Couldn't create a RuleBasedCollator with a contracting sequence\n %s\n", 
  86.             myErrorName(status));
  87.         return;
  88.     }
  89.     source=(UChar*)malloc(sizeof(UChar) * 20);
  90.     u_uastrcpy(source, "abchdcba");
  91.     iter=ucol_openElements(c1, source, u_strlen(source), &status);
  92.     if(U_FAILURE(status)){
  93.         log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n", 
  94.             myErrorName(status));
  95.         return;
  96.     }
  97.     backAndForth(iter);
  98.     ucol_closeElements(iter);
  99.     ucol_close(c1);
  100.     free(source);
  101.  
  102.     /* Test with an expanding character sequence */
  103.     u_uastrcpy(rule, "< a < b < c/abd < d");
  104.     c2 = ucol_openRules(rule, u_strlen(rule), UCOL_NO_NORMALIZATION, UCOL_DEFAULT_STRENGTH,  &status);
  105.     if (c2 == NULL || U_FAILURE(status))
  106.     {
  107.         log_err("Couldn't create a RuleBasedCollator with a contracting sequence.\n %s\n", 
  108.             myErrorName(status));
  109.         return;
  110.     }
  111.     source=(UChar*)malloc(sizeof(UChar) * 5);    
  112.     u_uastrcpy(source, "abcd");
  113.     iter=ucol_openElements(c2, source, u_strlen(source), &status);
  114.     if(U_FAILURE(status)){
  115.         log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n", 
  116.             myErrorName(status));
  117.         return;
  118.     }
  119.     backAndForth(iter);
  120.     ucol_closeElements(iter);
  121.     ucol_close(c2);
  122.     free(source);
  123.     /* Now try both */
  124.     u_uastrcpy(rule, "< a < b < c/aba < d < z < ch");
  125.     c3 = ucol_openRules(rule, u_strlen(rule), UCOL_DEFAULT_NORMALIZATION,  UCOL_DEFAULT_STRENGTH, &status);
  126.     if (c3 == NULL || U_FAILURE(status))
  127.     {
  128.         log_err("Couldn't create a RuleBasedCollator with a contracting sequence.\n %s\n", 
  129.             myErrorName(status));
  130.         return;
  131.     }
  132.     source=(UChar*)malloc(sizeof(UChar) * 10);    
  133.     u_uastrcpy(source, "abcdbchdc");
  134.     iter=ucol_openElements(c3, source, u_strlen(source), &status);
  135.     if(U_FAILURE(status)){
  136.         log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n", 
  137.             myErrorName(status));
  138.         return;
  139.     }
  140.     backAndForth(iter);
  141.     ucol_closeElements(iter);
  142.     ucol_close(c3);
  143.     free(source);
  144.     ucol_close(en_us);
  145.     free(test1);
  146.     free(test2);
  147. }
  148.  
  149. /**
  150.  * Test for getOffset() and setOffset()
  151.  */
  152. void TestOffset()
  153. {    
  154.     UErrorCode status= U_ZERO_ERROR;
  155.     UCollationElements *iter, *pristine;
  156.     int32_t offset;
  157.     int32_t *orders;
  158.     int32_t orderLength=0;
  159.     test1=(UChar*)malloc(sizeof(UChar) * 50);
  160.     test2=(UChar*)malloc(sizeof(UChar) * 50);
  161.     u_uastrcpy(test1, "What subset of all possible test cases?");
  162.     u_uastrcpy(test2, "has the highest probability of detecting");
  163.     en_us = ucol_open("en_US", &status);
  164.     log_verbose("Testing getOffset and setOffset for CollationElements\n");
  165.     iter=ucol_openElements(en_us, test1, u_strlen(test1), &status);
  166.     if(U_FAILURE(status)){
  167.         log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n", 
  168.             myErrorName(status));
  169.         return;
  170.     }
  171.     /* Run all the way through the iterator, then get the offset */
  172.  
  173.     orders = getOrders(iter, &orderLength);
  174.  
  175.     offset = ucol_getOffset(iter);
  176.  
  177.     if (offset != u_strlen(test1))
  178.     {
  179.         log_err("offset at end != length %d vs %d\n", offset, 
  180.             u_strlen(test1) );
  181.     }
  182.  
  183.     /* Now set the offset back to the beginning and see if it works */
  184.     pristine=ucol_openElements(en_us, test1, u_strlen(test1), &status);
  185.     if(U_FAILURE(status)){
  186.         log_err("ERROR: in creation of collation element iterator using ucol_openElements()\n %s\n", 
  187.             myErrorName(status));
  188.         return;
  189.     }
  190.     status = U_ZERO_ERROR;
  191.  
  192.     ucol_setOffset(iter, 0, &status);
  193.     if (U_FAILURE(status))
  194.     {
  195.         log_err("setOffset failed. %s\n",    myErrorName(status));
  196.     }
  197.     else
  198.     {
  199.         assertEqual(iter, pristine);
  200.     }
  201.        
  202.     ucol_closeElements(pristine);
  203.     ucol_closeElements(iter);
  204.     free(orders);
  205.     ucol_close(en_us);
  206.     free(test1);
  207.     free(test2);
  208. }
  209.  
  210. /**
  211.  * Test for setText()
  212.  */
  213. void TestSetText()
  214. {
  215.     int32_t c,i;
  216.     UErrorCode status = U_ZERO_ERROR;
  217.     UCollationElements *iter1, *iter2;
  218.     test1=(UChar*)malloc(sizeof(UChar) * 50);
  219.     test2=(UChar*)malloc(sizeof(UChar) * 50);
  220.     u_uastrcpy(test1, "What subset of all possible test cases?");
  221.     u_uastrcpy(test2, "has the highest probability of detecting");
  222.     en_us = ucol_open("en_US", &status);
  223.     log_verbose("testing setText for Collation elements\n");
  224.     iter1=ucol_openElements(en_us, test1, u_strlen(test1), &status);
  225.     if(U_FAILURE(status)){
  226.         log_err("ERROR: in creation of collation element iterator1 using ucol_openElements()\n %s\n", 
  227.             myErrorName(status));
  228.         return;
  229.     }
  230.     iter2=ucol_openElements(en_us, test2, u_strlen(test2), &status);
  231.     if(U_FAILURE(status)){
  232.         log_err("ERROR: in creation of collation element iterator2 using ucol_openElements()\n %s\n", 
  233.             myErrorName(status));
  234.         return;
  235.     }
  236.    
  237.     /* Run through the second iterator just to exercise it */
  238.     c = ucol_next(iter2, &status);
  239.     i = 0;
  240.  
  241.     while ( ++i < 10 && c != UCOL_NULLORDER)
  242.     {
  243.         if (U_FAILURE(status))
  244.         {
  245.             log_err("iter2->next() returned an error. %s\n", myErrorName(status));
  246.             ucol_closeElements(iter2);
  247.             ucol_closeElements(iter1);
  248.             return;
  249.         }
  250.  
  251.         c = ucol_next(iter2, &status);
  252.     }
  253.  
  254.     /* Now set it to point to the same string as the first iterator */
  255.     ucol_setText(iter2, test1, u_strlen(test1), &status);
  256.     if (U_FAILURE(status))
  257.     {
  258.         log_err("call to iter2->setText(test1) failed. %s\n", myErrorName(status));
  259.     }
  260.     else
  261.     {
  262.         assertEqual(iter1, iter2);
  263.     }
  264.     
  265.     ucol_closeElements(iter2);
  266.     ucol_closeElements(iter1);
  267. ucol_close(en_us);
  268.     free(test1);
  269.     free(test2);
  270. }
  271.  
  272.  
  273.  
  274. void backAndForth(UCollationElements *iter)
  275. {
  276.     /* Run through the iterator forwards and stick it into an array */
  277.     int32_t index, o;
  278.     UErrorCode status = U_ZERO_ERROR;
  279.     int32_t orderLength = 0;
  280.     int32_t *orders;
  281.     orders= getOrders(iter, &orderLength);
  282.     
  283.     
  284.     /* Now go through it backwards and make sure we get the same values */
  285.     index = orderLength;
  286.     
  287.     while ((o = ucol_previous(iter, &status)) != UCOL_NULLORDER)
  288.     {
  289.         if (o != orders[--index])
  290.         {
  291.             
  292.             log_err("Mismatch at index : %d\n", index);
  293.             break;
  294.         }
  295.     }
  296.  
  297.     if (index != 0)
  298.     {
  299.         
  300.         log_err("Didn't get back to beginning - index is %d\n", index);
  301.  
  302.         ucol_reset(iter);
  303.         log_err("\nnext: ");
  304.         while ((o = ucol_next(iter, &status)) != UCOL_NULLORDER)
  305.         {
  306.             log_err("Error at %d\n", o);
  307.         }
  308.         log_err("\nprev: ");
  309.         while ((o = ucol_previous(iter, &status)) != UCOL_NULLORDER)
  310.         {
  311.             log_err("Error at %d\n", o);
  312.         }
  313.         log_verbose("\n");
  314.     }
  315.  
  316.     
  317. }
  318.  
  319.  
  320. /**
  321.  * Return an integer array containing all of the collation orders
  322.  * returned by calls to next on the specified iterator
  323.  */
  324. int32_t* getOrders(UCollationElements *iter, int32_t *orderLength)
  325. {
  326.     UErrorCode status;
  327.     int32_t order;
  328.     int32_t maxSize = 100;
  329.     int32_t size = 0;
  330.     int32_t *temp;
  331.     int32_t *orders =(int32_t*)malloc(sizeof(int32_t) * maxSize);
  332.     status= U_ZERO_ERROR;
  333.  
  334.    
  335.     
  336.     while ((order=ucol_next(iter, &status)) != UCOL_NULLORDER)
  337.     {
  338.         if (size == maxSize)
  339.         {
  340.             maxSize *= 2;
  341.             temp = (int32_t*)malloc(sizeof(int32_t) * maxSize);
  342.  
  343.             memcpy(temp, orders, size * sizeof(int32_t));
  344.             free(orders);
  345.             orders = temp;
  346.             
  347.         }
  348.         
  349.         orders[size++] = order;
  350.         
  351.         
  352.         
  353.     }
  354.  
  355.     if (maxSize > size)
  356.     {
  357.         temp = (int32_t*)malloc(sizeof(int32_t) * size);
  358.         
  359.         memcpy(temp, orders, size * sizeof(int32_t));
  360.         free(orders);
  361.         orders = temp;
  362.        
  363.  
  364.     }
  365. /*     ucol_previous(iter, &status); */
  366.     *orderLength = size;
  367.     return orders;
  368. }
  369.  
  370.  
  371. void assertEqual(UCollationElements *i1, UCollationElements *i2)
  372. {
  373.     int32_t c1, c2;
  374.     int32_t count = 0;
  375.     UErrorCode status = U_ZERO_ERROR;
  376.  
  377.     do
  378.     {
  379.         c1 = ucol_next(i1, &status);
  380.         c2 = ucol_next(i2, &status);
  381.      
  382.         if (c1 != c2)
  383.         {
  384.             log_err("Error in iteration %d assetEqual between\n  %d  and   %d, they are not equal\n", count, c1, c2);
  385.             break;
  386.         }
  387.  
  388.         count += 1;
  389.     }
  390.     while (c1 != UCOL_NULLORDER);
  391. }
  392.  
  393.