home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / intltest / encoll.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  11.6 KB  |  358 lines

  1.  
  2. /*
  3. ********************************************************************
  4. * COPYRIGHT: 
  5. * (C) Copyright Taligent, Inc., 1997
  6. * (C) Copyright International Business Machines Corporation, 1997 - 1998
  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. #ifndef _COLL
  15. #include "coll.h"
  16. #endif
  17.  
  18. #ifndef _TBLCOLL
  19. #include "tblcoll.h"
  20. #endif
  21.  
  22. #ifndef _UNISTR
  23. #include "unistr.h"
  24. #endif
  25.  
  26. #ifndef _SORTKEY
  27. #include "sortkey.h"
  28. #endif
  29.  
  30. #ifndef _ENCOLL
  31. #include "encoll.h"
  32. #endif
  33.  
  34. CollationEnglishTest::CollationEnglishTest()
  35. : myCollation(0)
  36. {
  37.     UErrorCode status = U_ZERO_ERROR;
  38.     myCollation = Collator::createInstance(Locale::ENGLISH, status);
  39. }
  40.  
  41. CollationEnglishTest::~CollationEnglishTest()
  42. {
  43.     delete myCollation;
  44. }
  45.  
  46. const UChar CollationEnglishTest::testSourceCases[][CollationEnglishTest::MAX_TOKEN_LEN] = {
  47.         {'a', 'b', 0},
  48.         {'b', 'l', 'a', 'c', 'k', '-', 'b', 'i', 'r', 'd', 0},
  49.         {'b', 'l', 'a', 'c', 'k', ' ', 'b', 'i', 'r', 'd', 0},
  50.         {'b', 'l', 'a', 'c', 'k', '-', 'b', 'i', 'r', 'd', 0},
  51.         {'H', 'e', 'l', 'l', 'o', 0},
  52.         {'A', 'B', 'C', 0}, 
  53.         {'a', 'b', 'c', 0},
  54.         {'b', 'l', 'a', 'c', 'k', 'b', 'i', 'r', 'd', 0},
  55.         {'b', 'l', 'a', 'c', 'k', '-', 'b', 'i', 'r', 'd', 0},
  56.         {'b', 'l', 'a', 'c', 'k', '-', 'b', 'i', 'r', 'd', 0},
  57.         {'p', 0x00EA, 'c', 'h', 'e', 0},                                            // 10
  58.         {'p', 0x00E9, 'c', 'h', 0x00E9, 0},
  59.         {0x00C4, 'B', 0x0308, 'C', 0x0308, 0},
  60.         {'a', 0x0308, 'b', 'c', 0},
  61.         {'p', 0x00E9, 'c', 'h', 'e', 'r', 0},
  62.         {'r', 'o', 'l', 'e', 's', 0},
  63.         {'a', 'b', 'c', 0},
  64.         {'A', 0},
  65.         {'A', 0},
  66.         {'a', 'b', 0},                                                                // 20
  67.         {'t', 'c', 'o', 'm', 'p', 'a', 'r', 'e', 'p', 'l', 'a', 'i', 'n', 0},
  68.         {'a', 'b', 0}, 
  69.         {'a', '#', 'b', 0},
  70.         {'a', '#', 'b', 0},
  71.         {'a', 'b', 'c', 0},
  72.         {'A', 'b', 'c', 'd', 'a', 0},
  73.         {'a', 'b', 'c', 'd', 'a', 0},
  74.         {'a', 'b', 'c', 'd', 'a', 0},
  75.         {0x00E6, 'b', 'c', 'd', 'a', 0},
  76.         {0x00E4, 'b', 'c', 'd', 'a', 0},                                            // 30
  77.         {'a', 'b', 'c', 0},
  78.         {'a', 'b', 'c', 0},
  79.         {'a', 'b', 'c', 0},
  80.         {'a', 'b', 'c', 0},
  81.         {'a', 'b', 'c', 0},
  82.         {'a', 'c', 'H', 'c', 0},
  83.         {'a', 0x0308, 'b', 'c', 0},
  84.         {'t', 'h', 'i', 0x0302, 's', 0},
  85.         {'p', 0x00EA, 'c', 'h', 'e'},
  86.         {'a', 'b', 'c', 0},                                                         // 40
  87.         {'a', 'b', 'c', 0},
  88.         {'a', 'b', 'c', 0},
  89.         {'a', 0x00E6, 'c', 0},
  90.         {'a', 'b', 'c', 0},
  91.         {'a', 'b', 'c', 0},
  92.         {'a', 0x00E6, 'c', 0},
  93.         {'a', 'b', 'c', 0},
  94.         {'a', 'b', 'c', 0},               
  95.         {'p', 0x00E9, 'c', 'h', 0x00E9, 0}                                            // 49
  96. };
  97.  
  98. const UChar CollationEnglishTest::testTargetCases[][CollationEnglishTest::MAX_TOKEN_LEN] = {
  99.         {'a', 'b', 'c', 0},
  100.         {'b', 'l', 'a', 'c', 'k', 'b', 'i', 'r', 'd', 0},
  101.         {'b', 'l', 'a', 'c', 'k', '-', 'b', 'i', 'r', 'd', 0},
  102.         {'b', 'l', 'a', 'c', 'k', 0},
  103.         {'h', 'e', 'l', 'l', 'o', 0},
  104.         {'A', 'B', 'C', 0},
  105.         {'A', 'B', 'C', 0},
  106.         {'b', 'l', 'a', 'c', 'k', 'b', 'i', 'r', 'd', 's', 0},
  107.         {'b', 'l', 'a', 'c', 'k', 'b', 'i', 'r', 'd', 's', 0},
  108.         {'b', 'l', 'a', 'c', 'k', 'b', 'i', 'r', 'd', 0},                             // 10
  109.         {'p', 0x00E9, 'c', 'h', 0x00E9, 0},
  110.         {'p', 0x00E9, 'c', 'h', 'e', 'r', 0},
  111.         {0x00C4, 'B', 0x0308, 'C', 0x0308, 0},
  112.         {'A', 0x0308, 'b', 'c', 0},
  113.         {'p', 0x00E9, 'c', 'h', 'e', 0},
  114.         {'r', 'o', 0x0302, 'l', 'e', 0},
  115.         {'A', 0x00E1, 'c', 'd', 0},
  116.         {'A', 0x00E1, 'c', 'd', 0},
  117.         {'a', 'b', 'c', 0},
  118.         {'a', 'b', 'c', 0},                                                             // 20
  119.         {'T', 'C', 'o', 'm', 'p', 'a', 'r', 'e', 'P', 'l', 'a', 'i', 'n', 0},
  120.         {'a', 'B', 'c', 0},
  121.         {'a', '#', 'B', 0},
  122.         {'a', '&', 'b', 0},
  123.         {'a', '#', 'c', 0},
  124.         {'a', 'b', 'c', 'd', 'a', 0},
  125.         {0x00C4, 'b', 'c', 'd', 'a', 0},
  126.         {0x00E4, 'b', 'c', 'd', 'a', 0},
  127.         {0x00C4, 'b', 'c', 'd', 'a', 0},
  128.         {0x00C4, 'b', 'c', 'd', 'a', 0},                                             // 30
  129.         {'a', 'b', '#', 'c', 0},
  130.         {'a', 'b', 'c', 0},
  131.         {'a', 'b', '=', 'c', 0},
  132.         {'a', 'b', 'd', 0},
  133.         {0x00E4, 'b', 'c', 0},
  134.         {'a', 'C', 'H', 'c', 0},
  135.         {0x00E4, 'b', 'c', 0},
  136.         {'t', 'h', 0x00EE, 's', 0},
  137.         {'p', 0x00E9, 'c', 'h', 0x00E9, 0},
  138.         {'a', 'B', 'C', 0},                                                          // 40
  139.         {'a', 'b', 'd', 0},
  140.         {0x00E4, 'b', 'c', 0},
  141.         {'a', 0x00C6, 'c', 0},
  142.         {'a', 'B', 'd', 0},
  143.         {0x00E4, 'b', 'c', 0},
  144.         {'a', 0x00C6, 'c', 0},
  145.         {'a', 'B', 'd', 0},
  146.         {0x00E4, 'b', 'c', 0},          
  147.         {'p', 0x00EA, 'c', 'h', 'e', 0}                                                 // 49
  148. };
  149.  
  150. const Collator::EComparisonResult CollationEnglishTest::results[] = {
  151.         Collator::LESS, 
  152.         Collator::GREATER,
  153.         Collator::LESS,
  154.         Collator::GREATER,
  155.         Collator::GREATER,
  156.         Collator::EQUAL,
  157.         Collator::LESS,
  158.         Collator::LESS,
  159.         Collator::LESS,
  160.         Collator::GREATER,                                                          // 10
  161.         Collator::GREATER,
  162.         Collator::LESS,
  163.         Collator::EQUAL,
  164.         Collator::LESS,
  165.         Collator::GREATER,
  166.         Collator::GREATER,
  167.         Collator::GREATER,
  168.         Collator::LESS,
  169.         Collator::LESS,
  170.         Collator::LESS,                                                             // 20
  171.         Collator::LESS,
  172.         Collator::LESS,
  173.         Collator::LESS,
  174.         Collator::GREATER,
  175.         Collator::GREATER,
  176.         Collator::GREATER,
  177.         // Test Tertiary  > 26
  178.         Collator::LESS,
  179.         Collator::LESS,
  180.         Collator::GREATER,
  181.         Collator::LESS,                                                             // 30
  182.         Collator::GREATER,
  183.         Collator::EQUAL,
  184.         Collator::GREATER,
  185.         Collator::LESS,
  186.         Collator::LESS,
  187.         Collator::LESS,
  188.         // test identical > 36
  189.         Collator::EQUAL,
  190.         Collator::EQUAL,
  191.         // test primary > 38
  192.         Collator::EQUAL,
  193.         Collator::EQUAL,                                                            // 40
  194.         Collator::LESS,
  195.         Collator::EQUAL,
  196.         Collator::EQUAL,
  197.         // test secondary > 43
  198.         Collator::LESS,
  199.         Collator::LESS,
  200.         Collator::EQUAL,
  201.         Collator::LESS,
  202.         Collator::LESS, 
  203.         Collator::LESS                                                                 // 49
  204. };
  205.  
  206. const UChar CollationEnglishTest::testBugs[][CollationEnglishTest::MAX_TOKEN_LEN] = {
  207.     {'a', 0},
  208.     {'A', 0},
  209.     {'e', 0},
  210.     {'E', 0},
  211.     {0x00e9, 0},
  212.     {0x00e8, 0},
  213.     {0x00ea, 0},
  214.     {0x00eb, 0},
  215.     {'e', 'a', 0},
  216.     {'x', 0}
  217. };
  218.  
  219. // 0x0300 is grave, 0x0301 is acute
  220. // the order of elements in this array must be different than the order in CollationFrenchTest
  221. const UChar CollationEnglishTest::testAcute[][CollationEnglishTest::MAX_TOKEN_LEN] = {
  222.     {'e', 'e', 0},
  223.     {'e', 'e', 0x0301, 0},
  224.     {'e', 'e', 0x0301, 0x0300, 0},
  225.     {'e', 'e', 0x0300, 0},
  226.     {'e', 'e', 0x0300, 0x0301, 0},
  227.     {'e', 0x0301, 'e', 0},
  228.     {'e', 0x0301, 'e', 0x0301, 0},
  229.     {'e', 0x0301, 'e', 0x0301, 0x0300, 0},
  230.     {'e', 0x0301, 'e', 0x0300, 0},
  231.     {'e', 0x0301, 'e', 0x0300, 0x0301, 0},
  232.     {'e', 0x0301, 0x0300, 'e', 0},
  233.     {'e', 0x0301, 0x0300, 'e', 0x0301, 0},
  234.     {'e', 0x0301, 0x0300, 'e', 0x0301, 0x0300, 0},
  235.     {'e', 0x0301, 0x0300, 'e', 0x0300, 0},
  236.     {'e', 0x0301, 0x0300, 'e', 0x0300, 0x0301, 0},
  237.     {'e', 0x0300, 'e', 0},
  238.     {'e', 0x0300, 'e', 0x0301, 0},
  239.     {'e', 0x0300, 'e', 0x0301, 0x0300, 0},
  240.     {'e', 0x0300, 'e', 0x0300, 0},
  241.     {'e', 0x0300, 'e', 0x0300, 0x0301, 0},
  242.     {'e', 0x0300, 0x0301, 'e', 0},
  243.     {'e', 0x0300, 0x0301, 'e', 0x0301, 0},
  244.     {'e', 0x0300, 0x0301, 'e', 0x0301, 0x0300, 0},
  245.     {'e', 0x0300, 0x0301, 'e', 0x0300, 0},
  246.     {'e', 0x0300, 0x0301, 'e', 0x0300, 0x0301, 0}
  247. };
  248.  
  249. static const UChar testMore[][CollationEnglishTest::MAX_TOKEN_LEN] = {
  250.     {'a', 'e', 0},
  251.     { 0x00E6, 0},
  252.     { 0x00C6, 0},
  253.     {'a', 'f', 0},
  254.     {'o', 'e', 0},
  255.     { 0x0153, 0},
  256.     { 0x0152, 0},
  257.     {'o', 'f', 0},
  258. };
  259.  
  260. void CollationEnglishTest::doTest( UnicodeString source, UnicodeString target, Collator::EComparisonResult result)
  261. {
  262.     Collator::EComparisonResult compareResult = myCollation->compare(source, target);
  263.     CollationKey sortKey1, sortKey2;
  264.     UErrorCode key1status = U_ZERO_ERROR, key2status = U_ZERO_ERROR; //nos
  265.     myCollation->getCollationKey(source, /*nos*/ sortKey1, key1status );
  266.     myCollation->getCollationKey(target, /*nos*/ sortKey2, key2status );
  267.     if (U_FAILURE(key1status) || U_FAILURE(key2status))
  268.     {
  269.         errln("SortKey generation Failed.\n");
  270.         return;
  271.     }
  272.  
  273.     Collator::EComparisonResult keyResult = sortKey1.compareTo(sortKey2);
  274.     reportCResult( source, target, sortKey1, sortKey2, compareResult, keyResult, result );
  275. }
  276.  
  277. void CollationEnglishTest::TestTertiary( char* par )
  278. {
  279.     int32_t i = 0;
  280.     myCollation->setStrength(Collator::TERTIARY);
  281.     for (i = 0; i < 38 ; i++)
  282.     {
  283.         doTest(testSourceCases[i], testTargetCases[i], results[i]);
  284.     }
  285.  
  286.     int32_t j = 0;
  287.     for (i = 0; i < 10; i++)
  288.     {
  289.         for (j = i+1; j < 10; j++)
  290.         {
  291.             doTest(testBugs[i], testBugs[j], Collator::LESS);
  292.         }
  293.     }
  294.  
  295.     //test more interesting cases
  296.     Collator::EComparisonResult expected;
  297.     const int32_t testMoreSize = sizeof(testMore) / sizeof(testMore[0]);
  298.     for (i = 0; i < testMoreSize; i++)
  299.     {
  300.         for (j = 0; j < testMoreSize; j++)
  301.         {
  302.             if (i <  j) expected = Collator::LESS;
  303.             if (i == j) expected = Collator::EQUAL;
  304.             if (i >  j) expected = Collator::GREATER;
  305.             doTest(testMore[i], testMore[j], expected );
  306.         }
  307.     }
  308.  
  309. }
  310.  
  311. void CollationEnglishTest::TestPrimary( char* par )
  312. {
  313.     int32_t i;
  314.     myCollation->setStrength(Collator::PRIMARY);
  315.     for (i = 38; i < 43 ; i++)
  316.     {
  317.         doTest(testSourceCases[i], testTargetCases[i], results[i]);
  318.     }
  319. }
  320.  
  321. void CollationEnglishTest::TestSecondary( char* par )
  322. {
  323.     int32_t i;
  324.     myCollation->setStrength(Collator::SECONDARY);
  325.     for (i = 43; i < 49 ; i++)
  326.     {
  327.         doTest(testSourceCases[i], testTargetCases[i], results[i]);
  328.     }
  329.  
  330.     //test acute and grave ordering (compare to french collation)
  331.     int32_t j;
  332.     Collator::EComparisonResult expected;
  333.     const int32_t testAcuteSize = sizeof(testAcute) / sizeof(testAcute[0]);
  334.     for (i = 0; i < testAcuteSize; i++)
  335.     {
  336.         for (j = 0; j < testAcuteSize; j++)
  337.         {
  338.             if (i <  j) expected = Collator::LESS;
  339.             if (i == j) expected = Collator::EQUAL;
  340.             if (i >  j) expected = Collator::GREATER;
  341.             doTest(testAcute[i], testAcute[j], expected );
  342.         }
  343.     }
  344. }
  345.  
  346. void CollationEnglishTest::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  347. {
  348.     if (exec) logln("TestSuite CollationEnglishTest: ");
  349.     switch (index) {
  350.         case 0: name = "TestPrimary";   if (exec)   TestPrimary( par ); break;
  351.         case 1: name = "TestSecondary"; if (exec)   TestSecondary( par ); break;
  352.         case 2: name = "TestTertiary";  if (exec)   TestTertiary( par ); break;
  353.         default: name = ""; break;
  354.     }
  355. }
  356.  
  357.  
  358.