home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / cintltst / cestst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  5.7 KB  |  143 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 CESTST.C
  15. *
  16. * Modification History:
  17. *        Name                     Description            
  18. *     Madhu Katragadda            Ported for C API
  19. *********************************************************************************
  20. /**
  21.  * CollationSpanishTest 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 "cestst.h"
  32. #include "ccolltst.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.     {0x0062/*'a'*/, 0x006c/*'l'*/, 0x0069/*'i'*/, 0x0061/*'a'*/, 0x0073/*'s'*/, 0x0000},
  39.     {0x0045/*'E'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x0069/*'i'*/, 0x006f/*'o'*/, 0x0074/*'t'*/, 0x0000},
  40.     {0x0048/*'H'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x006f/*'o'*/, 0x0000},
  41.     {0x0061/*'a'*/, 0x0063/*'c'*/, 0x0048/*'H'*/, 0x0063/*'c'*/, 0x0000},
  42.     {0x0061/*'a'*/, 0x0063/*'c'*/, 0x0063/*'c'*/, 0x0000},
  43.     {0x0061/*'a'*/, 0x006c/*'l'*/, 0x0069/*'i'*/, 0x0061/*'a'*/, 0x0073/*'s'*/, 0x0000},
  44.     {0x0061/*'a'*/, 0x0063/*'c'*/, 0x0048/*'H'*/, 0x0063/*'c'*/, 0x0000},
  45.     {0x0061/*'a'*/, 0x0063/*'c'*/, 0x0063/*'c'*/, 0x0000},
  46.     {0x0048/*'H'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x006f/*'o'*/, 0x0000},
  47. };
  48.  
  49. const static UChar testTargetCases[][MAX_TOKEN_LEN] = {
  50.     {0x0062/*'a'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x0069/*'i'*/, 0x0061/*'a'*/, 0x0073/*'s'*/, 0x0000},
  51.     {0x0045/*'E'*/, 0x006d/*'m'*/, 0x0069/*'i'*/, 0x006f/*'o'*/, 0x0074/*'t'*/, 0x0000},
  52.     {0x0068/*'h'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x006f/*'O'*/, 0x0000},
  53.     {0x0061/*'a'*/, 0x0043/*'C'*/, 0x0048/*'H'*/, 0x0063/*'c'*/, 0x0000},
  54.     {0x0061/*'a'*/, 0x0043/*'C'*/, 0x0048/*'H'*/, 0x0063/*'c'*/, 0x0000},
  55.     {0x0062/*'a'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x0069/*'i'*/, 0x0061/*'a'*/, 0x0073/*'s'*/, 0x0000},
  56.     {0x0061/*'a'*/, 0x0043/*'C'*/, 0x0048/*'H'*/, 0x0063/*'c'*/, 0x0000},
  57.     {0x0061/*'a'*/, 0x0043/*'C'*/, 0x0048/*'H'*/, 0x0063/*'c'*/, 0x0000},
  58.     {0x0068/*'h'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x006f/*'O'*/, 0x0000},
  59. };
  60.  
  61. const static UCollationResult results[] = {
  62.     UCOL_LESS,
  63.     UCOL_LESS,
  64.     UCOL_GREATER,
  65.     UCOL_LESS,
  66.     UCOL_LESS,
  67.     /* test primary > 5*/
  68.     UCOL_LESS,
  69.     UCOL_EQUAL,
  70.     UCOL_LESS,
  71.     UCOL_EQUAL
  72. };
  73.  
  74.  
  75. void addSpanishCollTest(TestNode** root)
  76. {
  77.     
  78.     
  79.     addTest(root, &TestPrimary, "tscoll/cestst/TestPrimary");
  80.     addTest(root, &TestTertiary, "tscoll/cestst/TestTertiary");
  81.  
  82. }
  83. void doTest(UCollator* myCollation, const UChar source[], const UChar target[], UCollationResult result)
  84. {
  85.     int32_t sortklen, temp;
  86.     UCollationResult compareResult, keyResult;
  87.     uint8_t *sortKey1, *sortKey2;
  88.     
  89.     compareResult = ucol_strcoll(myCollation, source, u_strlen(source), target, u_strlen(target));
  90.     
  91.     sortklen=ucol_getSortKey(myCollation, source, u_strlen(source),  NULL, 0);
  92.     sortKey1=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
  93.     ucol_getSortKey(myCollation, source, u_strlen(source), sortKey1, sortklen+1);
  94.     
  95.     sortklen=ucol_getSortKey(myCollation, target, u_strlen(target),  NULL, 0);
  96.     sortKey2=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
  97.     ucol_getSortKey(myCollation, target, u_strlen(target), sortKey2, sortklen+1);
  98.     
  99.  
  100.     temp= memcmp(sortKey1, sortKey2, sortklen);
  101.     if(temp < 0) keyResult=UCOL_LESS;
  102.     else if(temp > 0) keyResult= UCOL_GREATER;
  103.     else keyResult = UCOL_EQUAL;
  104.     reportCResult( source, target, sortKey1, sortKey2, compareResult, keyResult, result );
  105. }
  106.  
  107. void TestTertiary( )
  108. {
  109.     
  110.     int32_t i;
  111.     UErrorCode status = U_ZERO_ERROR;
  112.     myCollation = ucol_open("es_ES", &status);
  113.     if(U_FAILURE(status)){
  114.         log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
  115.     }
  116.     log_verbose("Testing Spanish Collation with Tertiary strength\n");
  117.     ucol_setStrength(myCollation, UCOL_TERTIARY);
  118.     for (i = 0; i < 5 ; i++)
  119.     {
  120.         doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
  121.     }
  122.     ucol_close(myCollation);
  123. }
  124.  
  125. void TestPrimary()
  126. {
  127.     
  128.     int32_t i;
  129.     UErrorCode status = U_ZERO_ERROR;
  130.     myCollation = ucol_open("es_ES", &status);
  131.     if(U_FAILURE(status)){
  132.         log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
  133.     }
  134.     log_verbose("Testing Spanish Collation with Primary strength\n");
  135.     ucol_setStrength(myCollation, UCOL_PRIMARY);
  136.     for (i = 5; i < 9; i++)
  137.     {
  138.         doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
  139.     }
  140.     ucol_close(myCollation);
  141. }
  142.  
  143.