home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / cintltst / cdetst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  6.9 KB  |  161 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 CDETST.C
  15. *
  16. * Modification History:
  17. *        Name                     Description            
  18. *     Madhu Katragadda            Ported for C API
  19. *********************************************************************************
  20. /**
  21.  * CollationGermanTest 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 "cdetst.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. {
  39.     {0x0047/*'G'*/, 0x0072/*'r'*/, 0x00F6, 0x00DF, 0x0065/*'e'*/, 0x0000},     
  40.     {0x0061/*'a'*/, 0x0062/*'b'*/, 0x0063/*'c'*/, 0x0000},
  41.     {0x0054/*'T'*/, 0x00F6, 0x006e/*'n'*/, 0x0065/*'e'*/, 0x0000},
  42.     {0x0054/*'T'*/, 0x00F6, 0x006e/*'n'*/, 0x0065/*'e'*/, 0x0000},
  43.     {0x0054/*'T'*/, 0x00F6, 0x006e/*'n'*/, 0x0065/*'e'*/, 0x0000},
  44.     {0x0061/*'a'*/, 0x0308, 0x0062/*'b'*/, 0x0063/*'c'*/, 0x0000},
  45.     {0x00E4, 0x0062/*'b'*/, 0x0063/*'c'*/, 0x0000},                    /*doubt in primary here        */
  46.     {0x00E4, 0x0062/*'b'*/, 0x0063/*'c'*/, 0x0000},                    /*doubt in primary here*/
  47.     {0x0053/*'S'*/, 0x0074/*'t'*/, 0x0072/*'r'*/, 0x0061/*'a'*/, 0x00DF, 0x0065/*'e'*/, 0x0000},
  48.     {0x0065/*'e'*/, 0x0066/*'f'*/, 0x0067/*'g'*/, 0x0000},
  49.     {0x00E4, 0x0062/*'b'*/, 0x0063/*'c'*/, 0x0000},                    /*doubt in primary here*/
  50.     {0x0053/*'S'*/, 0x0074/*'t'*/, 0x0072/*'r'*/, 0x0061/*'a'*/, 0x00DF, 0x0065/*'e'*/, 0x0000}
  51. };
  52.  
  53. const static UChar testTargetCases[][MAX_TOKEN_LEN] =
  54. {
  55.     {0x0047/*'G'*/, 0x0072/*'r'*/, 0x006f/*'o'*/, 0x0073/*'s'*/, 0x0073/*'s'*/, 0x0069/*'i'*/, 0x0073/*'s'*/, 0x0074/*'t'*/, 0x0000},    
  56.     {0x0061/*'a'*/, 0x0308, 0x0062/*'b'*/, 0x0063/*'c'*/, 0x0000},
  57.     {0x0054/*'T'*/, 0x006f/*'o'*/, 0x006e/*'n'*/, 0x0000},
  58.     {0x0054/*'T'*/, 0x006f/*'o'*/, 0x0064/*'d'*/, 0x0000},
  59.     {0x0054/*'T'*/, 0x006f/*'o'*/, 0x0066/*'f'*/, 0x0075/*'u'*/, 0x0000},
  60.     {0x0041/*'A'*/, 0x0308, 0x0062/*'b'*/, 0x0063/*'c'*/, 0x0000},
  61.     {0x0061/*'a'*/, 0x0308, 0x0062/*'b'*/, 0x0063/*'c'*/, 0x0000},                    /*doubt in primary here*/
  62.     {0x0061/*'a'*/, 0x0065/*'e'*/, 0x0062/*'b'*/, 0x0063/*'c'*/, 0x0000},            /*doubt in primary here*/
  63.     {0x0053/*'S'*/, 0x0074/*'t'*/, 0x0072/*'r'*/, 0x0061/*'a'*/, 0x0073/*'s'*/, 0x0073/*'s'*/, 0x0065/*'e'*/, 0x0000},
  64.     {0x0065/*'e'*/, 0x0066/*'f'*/, 0x0067/*'g'*/, 0x0000},
  65.     {0x0061/*'a'*/, 0x0065/*'e'*/, 0x0062/*'b'*/, 0x0063/*'c'*/, 0x0000},        /*doubt in primary here*/
  66.     {0x0053/*'S'*/, 0x0074/*'t'*/, 0x0072/*'r'*/, 0x0061/*'a'*/, 0x0073/*'s'*/, 0x0073/*'s'*/, 0x0065/*'e'*/, 0x0000}
  67. };
  68.  
  69. const static UCollationResult results[][2] =
  70. {
  71.       /*  Primary*/            /*    Tertiary*/
  72.         { UCOL_LESS,        UCOL_LESS },        /*should be UCOL_GREATER for primary*/
  73.         { UCOL_EQUAL,        UCOL_LESS },
  74.         { UCOL_GREATER,        UCOL_GREATER },
  75.         { UCOL_GREATER,        UCOL_GREATER },
  76.         { UCOL_GREATER,        UCOL_GREATER },
  77.         { UCOL_EQUAL,        UCOL_LESS },
  78.         { UCOL_EQUAL,        UCOL_EQUAL },    /*should be UCOL_GREATER for primary*/
  79.         { UCOL_LESS,        UCOL_LESS },    /*should be UCOL_GREATER for primary*/
  80.         { UCOL_EQUAL,        UCOL_GREATER },
  81.         { UCOL_EQUAL,        UCOL_EQUAL },
  82.         { UCOL_LESS,        UCOL_LESS },   /*should be UCOL_GREATER for primary*/
  83.         { UCOL_EQUAL,        UCOL_GREATER }
  84. };
  85.  
  86.  
  87.  
  88.  
  89. void addGermanCollTest(TestNode** root)
  90. {
  91.     
  92.  
  93.     addTest(root, &TestTertiary, "tscoll/cdetst/TestTertiary");
  94.     addTest(root, &TestPrimary, "tscoll/cdetst/TestPrimary");
  95.        
  96.  
  97.  
  98. }
  99. void doTest(UCollator* myCollation, const UChar source[], const UChar target[], UCollationResult result)
  100. {
  101.     int32_t sortklen, temp;
  102.     UCollationResult compareResult, keyResult;
  103.     uint8_t *sortKey1, *sortKey2;
  104.     
  105.     
  106.     compareResult = ucol_strcoll(myCollation, source, u_strlen(source), target, u_strlen(target));
  107.     
  108.     sortklen=ucol_getSortKey(myCollation, source, u_strlen(source),  NULL, 0);
  109.     sortKey1=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
  110.     ucol_getSortKey(myCollation, source, u_strlen(source), sortKey1, sortklen+1);
  111.     
  112.     sortklen=ucol_getSortKey(myCollation, target, u_strlen(target),  NULL, 0);
  113.     sortKey2=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
  114.     ucol_getSortKey(myCollation, target, u_strlen(target), sortKey2, sortklen+1);
  115.     
  116.  
  117.     temp= memcmp(sortKey1, sortKey2, sortklen);
  118.     if(temp < 0) keyResult=UCOL_LESS;
  119.     else if(temp > 0) keyResult= UCOL_GREATER;
  120.     else keyResult = UCOL_EQUAL;
  121.     reportCResult( source, target, sortKey1, sortKey2, compareResult, keyResult, result );
  122. }
  123.  
  124. void TestTertiary( )
  125. {
  126.     
  127.     int32_t i;
  128.     UErrorCode status = U_ZERO_ERROR;
  129.     myCollation = ucol_open("de_DE", &status);
  130.     if(U_FAILURE(status)){
  131.         log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
  132.     }
  133.     log_verbose("Testing German Collation with Tertiary strength\n");
  134.     ucol_setNormalization(myCollation, UCOL_DECOMP_CAN_COMP_COMPAT);
  135.     ucol_setStrength(myCollation, UCOL_TERTIARY);
  136.     for (i = 0; i < 12 ; i++)
  137.     {
  138.         doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i][1]);
  139.     }
  140.     ucol_close(myCollation);
  141. }
  142.  
  143. void TestPrimary()
  144. {
  145.     
  146.     int32_t i;
  147.     UErrorCode status = U_ZERO_ERROR;
  148.     myCollation = ucol_open("de_DE", &status);
  149.     if(U_FAILURE(status)){
  150.         log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status));
  151.     }
  152.     log_verbose("Testing German Collation with primary strength\n");
  153.        ucol_setNormalization(myCollation, UCOL_DEFAULT_NORMALIZATION);
  154.     ucol_setStrength(myCollation, UCOL_PRIMARY);
  155.     for (i = 0; i < 12 ; i++)
  156.     {
  157.         doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i][0]);
  158.     }
  159.     ucol_close(myCollation);
  160. }
  161.