home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / cintltst / cfrtst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  8.7 KB  |  230 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 CFRTST.C
  15. *
  16. * Modification History:
  17. *        Name                     Description            
  18. *     Madhu Katragadda            Ported for C API
  19. *********************************************************************************
  20. /**
  21.  * CollationFrenchTest is a third level test class.  This tests the locale
  22.  * specific primary, secondary and tertiary rules.  For example, the ignorable
  23.  * character '-' in string "black-bird".  The en_US locale uses the default
  24.  * collation rules as its sorting sequence.
  25.  */
  26.  
  27. #include "utypes.h"
  28. #include "ucol.h"
  29. #include "uloc.h"
  30. #include "cintltst.h"
  31. #include "ccolltst.h"
  32. #include "cfrtst.h"
  33. #include "ustring.h"
  34. #include "string.h"
  35. #include <memory.h>
  36. static  UCollator *myCollation;
  37. const static UChar testSourceCases[][MAX_TOKEN_LEN] =
  38. {
  39.     {0x0061/*'a'*/, 0x0062/*'b'*/, 0x0063/*'c'*/, 0x0000},
  40.     {0x0043/*'C'*/, 0x004f/*'O'*/, 0x0054/*'T'*/, 0x0045/*'E'*/, 0x0000},
  41.     {0x0063/*'c'*/, 0x006f/*'o'*/, 0x002d/*'-'*/, 0x006f/*'o'*/, 0x0070/*'p'*/, 0x0000},
  42.     {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0000},
  43.     {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
  44.     {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
  45.     {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
  46.     {0x0048/*'H'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x006f/*'o'*/, 0x0000},
  47.     {0x01f1, 0x0000},
  48.     {0xfb00, 0x0000},
  49.     {0x01fa, 0x0000},
  50.     {0x0101, 0x0000}
  51. };
  52.  
  53. const static UChar testTargetCases[][MAX_TOKEN_LEN] =
  54. {
  55.     {0x0041/*'A'*/, 0x0042/*'B'*/, 0x0043/*'C'*/, 0x0000},
  56.     {0x0063/*'c'*/, 0x00f4, 0x0074/*'t'*/, 0x0065/*'e'*/, 0x0000},
  57.     {0x0043/*'C'*/, 0x004f/*'O'*/, 0x004f/*'O'*/, 0x0050/*'P'*/, 0x0000},
  58.     {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x00E9, 0x0000},
  59.     {0x0070/*'p'*/,  0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x00E9, 0x0000},
  60.     {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0000},
  61.     {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
  62.     {0x0068/*'h'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x004f/*'O'*/, 0x0000},
  63.     {0x01ee, 0x0000},
  64.     {0x25ca, 0x0000},
  65.     {0x00e0, 0x0000},
  66.     {0x01df, 0x0000}
  67. };
  68.  
  69. const static UCollationResult results[] =
  70. {
  71.     UCOL_LESS,
  72.     UCOL_LESS,
  73.     UCOL_GREATER,
  74.     UCOL_LESS,
  75.     UCOL_GREATER,
  76.     UCOL_GREATER,
  77.     UCOL_LESS,
  78.     UCOL_GREATER,
  79.     UCOL_GREATER,
  80.     UCOL_GREATER,
  81.     UCOL_GREATER,
  82.     UCOL_GREATER
  83. };
  84.  
  85. /* 0x0300 is grave, 0x0301 is acute*/
  86. /* the order of elements in this array must be different than the order in CollationEnglishTest*/
  87. const static UChar testAcute[][MAX_TOKEN_LEN] =
  88. {
  89.     {0x0065/*'e'*/, 0x0065/*'e'*/,  0x0000},
  90.     {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/,  0x0000},
  91.     {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/,  0x0000},
  92.     {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/,  0x0000},
  93.     {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/,  0x0000},
  94.     {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301, 0x0000}, 
  95.     {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0000},
  96.     {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0000},
  97.     {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0000},
  98.     {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0000},
  99.     {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
  100.     {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
  101.     {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
  102.     {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
  103.     {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
  104.     {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300, 0x0000},
  105.     {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0000},
  106.     {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0000},
  107.     {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0000},
  108.     {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0000},
  109.     {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
  110.     {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
  111.     {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
  112.     {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
  113.     {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000}
  114. };
  115.  
  116. const static UChar testBugs[][MAX_TOKEN_LEN] =
  117. {
  118.     {0x0061/*'a'*/, 0x000},
  119.     {0x0041/*'A'*/, 0x000},
  120.     {0x0065/*'e'*/, 0x000},
  121.     {0x0045/*'E'*/, 0x000},
  122.     {0x00e9, 0x000},
  123.     {0x00e8, 0x000},
  124.     {0x00ea, 0x000},
  125.     {0x00eb, 0x000},
  126.     {0x0065/*'e'*/, 0x0061/*'a'*/, 0x000},
  127.     {0x0078/*'x'*/, 0x000}
  128. };
  129.  
  130.  
  131.  
  132.  
  133. void addFrenchCollTest(TestNode** root)
  134. {
  135.     
  136.  
  137.     addTest(root, &TestSecondary, "tscoll/cfrtst/TestSecondary");
  138.     addTest(root, &TestTertiary, "tscoll/cfrtst/TestTertiary");
  139.     addTest(root, &TestExtra, "tscoll/cfrtst/TestExtra");
  140.        
  141.  
  142. }
  143.  
  144. void doTest(UCollator* myCollation, const UChar source[], const UChar target[], UCollationResult result)
  145. {
  146.     int32_t sortklen, temp;
  147.     UCollationResult compareResult, keyResult;
  148.     uint8_t *sortKey1, *sortKey2;
  149.     
  150.     compareResult = ucol_strcoll(myCollation, source, u_strlen(source), target, u_strlen(target));
  151.     
  152.     sortklen=ucol_getSortKey(myCollation, source, u_strlen(source),  NULL, 0);
  153.     sortKey1=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
  154.     ucol_getSortKey(myCollation, source, u_strlen(source), sortKey1, sortklen+1);
  155.     
  156.     sortklen=ucol_getSortKey(myCollation, target, u_strlen(target),  NULL, 0);
  157.     sortKey2=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
  158.     ucol_getSortKey(myCollation, target, u_strlen(target), sortKey2, sortklen+1);
  159.     
  160.  
  161.     temp= memcmp(sortKey1, sortKey2, sortklen);
  162.     if(temp < 0) keyResult=UCOL_LESS;
  163.     else if(temp > 0) keyResult= UCOL_GREATER;
  164.     else keyResult = UCOL_EQUAL;
  165.     reportCResult( source, target, sortKey1, sortKey2, compareResult, keyResult, result );
  166. }
  167.  
  168. void TestTertiary( )
  169. {
  170.     
  171.     int32_t i;
  172.     UErrorCode status = U_ZERO_ERROR;
  173.     myCollation = ucol_open("fr_FR", &status);
  174.     if(U_FAILURE(status)){
  175.         log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
  176.     }
  177.     log_verbose("Testing French Collation with Tertiary strength\n");
  178.     ucol_setStrength(myCollation, UCOL_TERTIARY);
  179.     for (i = 0; i < 12 ; i++)
  180.     {
  181.         doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
  182.     }
  183.   ucol_close(myCollation);
  184. }
  185.  
  186. void TestSecondary()
  187. {
  188.     int32_t i,j, testAcuteSize;
  189.     UCollationResult expected;
  190.     UErrorCode status = U_ZERO_ERROR;
  191.     myCollation = ucol_open("fr_FR", &status);
  192.     if(U_FAILURE(status)){
  193.         log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
  194.     }
  195.     log_verbose("Testing French Collation with Secondary strength\n");
  196.     /*test acute and grave ordering (compare to french collation)*/
  197.     testAcuteSize = sizeof(testAcute) / sizeof(testAcute[0]);
  198.     for (i = 0; i < testAcuteSize; i++)
  199.     {
  200.         for (j = 0; j < testAcuteSize; j++)
  201.         {
  202.             if (i <  j) expected = UCOL_LESS;
  203.             if (i == j) expected = UCOL_EQUAL;
  204.             if (i >  j) expected = UCOL_GREATER;
  205.             doTest(myCollation, testAcute[i], testAcute[j], expected );
  206.         }
  207.     }
  208. ucol_close(myCollation);
  209. }
  210.  
  211. void TestExtra()
  212. {
  213.     int32_t i, j;
  214.     UErrorCode status = U_ZERO_ERROR;
  215.     myCollation = ucol_open("fr_FR", &status);
  216.     if(U_FAILURE(status)){
  217.         log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
  218.     }
  219.     log_verbose("Testing French Collation extra with secondary strength\n");
  220.     ucol_setStrength(myCollation, UCOL_TERTIARY);
  221.     for (i = 0; i < 9 ; i++)
  222.     {
  223.         for (j = i + 1; j < 10; j += 1)
  224.         {
  225.             doTest(myCollation, testBugs[i], testBugs[j], UCOL_LESS);
  226.         }
  227.     }
  228. ucol_close(myCollation);
  229. }
  230.