home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / cintltst / cturtst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  5.7 KB  |  153 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 CTURTST.C
  15. *
  16. * Modification History:
  17. *        Name                     Description            
  18. *     Madhu Katragadda            Ported for C API
  19. *********************************************************************************
  20. /**
  21.  * CollationTurkishTest 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 "cturtst.h"
  33. #include "ustring.h"
  34. #include "string.h"
  35. #include <memory.h>
  36.  
  37. static UCollator *myCollation;
  38. const static UChar testSourceCases[][MAX_TOKEN_LEN] = {
  39.     {0x0073/*'s'*/, 0x0327, 0x0000},
  40.     {0x0076/*'v'*/, 0x00E4, 0x0074/*'t'*/, 0x0000},
  41.     {0x006f/*'o'*/, 0x006c/*'l'*/, 0x0064/*'d'*/, 0x0000},
  42.     {0x00FC, 0x006f/*'o'*/, 0x0069/*'i'*/, 0x0064/*'d'*/, 0x0000},
  43.     {0x0068/*'h'*/, 0x011E, 0x0061/*'a'*/, 0x006c/*'l'*/, 0x0074/*'t'*/, 0x0000},
  44.     {0x0073/*'s'*/, 0x0074/*'t'*/, 0x0072/*'r'*/, 0x0065/*'e'*/, 0x0073/*'s'*/, 0x015E, 0x0000},
  45.     {0x0076/*'v'*/, 0x006f/*'o'*/, 0x0131, 0x0064/*'d'*/, 0x0000},
  46.     {0x0069/*'i'*/, 0x0064/*'d'*/, 0x0065/*'e'*/, 0x0061/*'a'*/, 0x0000},
  47.     {0x00FC, 0x006f/*'o'*/, 0x0069/*'i'*/, 0x0064 /*d'*/, 0x0000},
  48.     {0x0076/*'v'*/, 0x006f/*'o'*/, 0x0131, 0x0064 /*d'*/, 0x0000},
  49.     {0x0069/*'i'*/, 0x0064/*'d'*/, 0x0065/*'e'*/, 0x0061/*'a'*/, 0x0000},
  50. };
  51.  
  52. const static UChar testTargetCases[][MAX_TOKEN_LEN] = {
  53.     {0x0075/*'u'*/, 0x0308, 0x0000},
  54.     {0x0076/*'v'*/, 0x0062/*'b'*/, 0x0074/*'t'*/, 0x0000},
  55.     {0x00D6, 0x0061/*'a'*/, 0x0079/*'y'*/, 0x0000},
  56.     {0x0076/*'v'*/, 0x006f/*'o'*/, 0x0069/*'i'*/, 0x0064 /*d'*/, 0x0000},
  57.     {0x0068/*'h'*/, 0x0061/*'a'*/,  0x006c/*'l'*/, 0x0074/*'t'*/, 0x0000},
  58.     {0x015E, 0x0074/*'t'*/, 0x0072/*'r'*/, 0x0065/*'e'*/, 0x015E, 0x0073/*'s'*/, 0x0000},
  59.     {0x0076/*'v'*/, 0x006f/*'o'*/, 0x0069/*'i'*/, 0x0064 /*d'*/, 0x0000},
  60.     {0x0049/*'I'*/, 0x0064/*'d'*/, 0x0065/*'e'*/, 0x0061/*'a'*/, 0x0000},
  61.     {0x0076/*'v'*/, 0x006f/*'o'*/, 0x0069/*'i'*/, 0x0064 /*d'*/, 0x0000},
  62.     {0x0076/*'v'*/, 0x006f/*'o'*/, 0x0069/*'i'*/, 0x0064 /*d'*/, 0x0000},
  63.     {0x0049/*'I'*/, 0x0064/*'d'*/, 0x0065/*'e'*/, 0x0061/*'a'*/, 0x0000},
  64. };
  65.  
  66. const static UCollationResult results[] = {
  67.     UCOL_LESS,
  68.     UCOL_LESS,
  69.     UCOL_LESS,
  70.     UCOL_LESS,
  71.     UCOL_GREATER,
  72.     UCOL_LESS,
  73.     UCOL_LESS,
  74.     UCOL_GREATER,
  75.     /* test priamry > 8 */
  76.     UCOL_LESS,
  77.     UCOL_EQUAL,
  78.     UCOL_EQUAL
  79. };
  80.  
  81.  
  82.  
  83. void addTurkishCollTest(TestNode** root)
  84. {
  85.     
  86.     addTest(root, &TestPrimary, "tscoll/cturtst/TestPrimary");
  87.     addTest(root, &TestTertiary, "tscoll/cturtst/TestTertiary");
  88.  
  89.  
  90. }
  91. void doTest(UCollator* myCollation, const UChar source[], const UChar target[], UCollationResult result)
  92. {
  93.     int32_t sortklen, temp;
  94.     UCollationResult compareResult, keyResult;
  95.     uint8_t *sortKey1, *sortKey2;
  96.     
  97.     compareResult = ucol_strcoll(myCollation, source, u_strlen(source), target, u_strlen(target));
  98.     
  99.     sortklen=ucol_getSortKey(myCollation, source, u_strlen(source),  NULL, 0);
  100.     sortKey1=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
  101.     ucol_getSortKey(myCollation, source, u_strlen(source), sortKey1, sortklen+1);
  102.     
  103.     sortklen=ucol_getSortKey(myCollation, target, u_strlen(target),  NULL, 0);
  104.     sortKey2=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
  105.     ucol_getSortKey(myCollation, target, u_strlen(target), sortKey2, sortklen+1);
  106.     
  107.  
  108.     temp= memcmp(sortKey1, sortKey2, sortklen);
  109.     if(temp < 0) keyResult=UCOL_LESS;
  110.     else if(temp > 0) keyResult= UCOL_GREATER;
  111.     else keyResult = UCOL_EQUAL;
  112.     reportCResult( source, target, sortKey1, sortKey2, compareResult, keyResult, result );
  113. }
  114.  
  115. void TestTertiary( )
  116. {
  117.     
  118.     int32_t i;
  119.  
  120.     UErrorCode status = U_ZERO_ERROR;
  121.     myCollation = ucol_open("tr", &status);
  122.     if(U_FAILURE(status)){
  123.         log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
  124.     }
  125.     log_verbose("Testing Turkish Collation with Tertiary strength\n");
  126.     ucol_setStrength(myCollation, UCOL_TERTIARY);
  127.     for (i = 0; i < 8 ; i++)
  128.     {
  129.         doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
  130.     }
  131.     ucol_close(myCollation);
  132. }
  133.  
  134. void TestPrimary()
  135. {
  136.     
  137.     int32_t i;
  138.  
  139.     UErrorCode status = U_ZERO_ERROR;
  140.     myCollation = ucol_open("tr", &status);
  141.     if(U_FAILURE(status)){
  142.         log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
  143.     }
  144.     log_verbose("Testing Turkish Collation with Primary strength\n");
  145.     ucol_setStrength(myCollation, UCOL_PRIMARY);
  146.     for (i = 8; i < 11; i++)
  147.     {
  148.         doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
  149.     }
  150.     ucol_close(myCollation);
  151. }
  152.  
  153.