home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / intltest / g7coll.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  11.6 KB  |  304 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 _G7COLL
  31. #include "g7coll.h"
  32. #endif
  33.  
  34. const Locale G7CollationTest::locales[8] = {
  35.     Locale("en", "US", ""),
  36.     Locale("en", "GB", ""),
  37.     Locale("en", "CA", ""),
  38.     Locale("fr", "FR", ""),
  39.     Locale("fr", "CA", ""),
  40.     Locale("de", "DE", ""),
  41.     Locale("it", "IT", ""),
  42.     Locale("ja", "JP", "")
  43. };
  44.  
  45. const UChar G7CollationTest::testCases[][G7CollationTest::MAX_TOKEN_LEN] = {
  46.     { 'b', 'l', 'a', 'c', 'k', '-', 'b', 'i', 'r', 'd', 's', 0},   // 0
  47.     { 'P', 'a', 't', 0},                                           // 1
  48.     { 'p', 0x00E9, 'c', 'h', 0x00E9, 0},                           // 2
  49.     { 'p', 0x00EA, 'c', 'h', 'e', 0},                              // 3
  50.     { 'p', 0x00E9, 'c', 'h', 'e', 'r', 0},                         // 4
  51.     { 'p', 0x00EA, 'c', 'h', 'e', 'r', 0},                         // 5
  52.     { 'T', 'o', 'd', 0},                                           // 6
  53.     { 'T', 0x00F6, 'n', 'e', 0},                                   // 7
  54.     { 'T', 'o', 'f', 'u', 0},                                      // 8
  55.     { 'b', 'l', 'a', 'c', 'k', 'b', 'i', 'r', 'd', 's', 0},        // 9
  56.     { 'T', 'o', 'n', 0},                                           // 10
  57.     { 'P', 'A', 'T', 0},                                           // 11
  58.     { 'b', 'l', 'a', 'c', 'k', 'b', 'i', 'r', 'd', 0},             // 12
  59.     { 'b', 'l', 'a', 'c', 'k', '-', 'b', 'i', 'r', 'd', 0},        // 13
  60.     { 'p', 'a', 't', 0},                                           // 14
  61.     // Additional tests
  62.     { 'c', 'z', 'a', 'r', 0 },                                     // 15
  63.     { 'c', 'h', 'u', 'r', 'o', 0 },                                // 16
  64.     { 'c', 'a', 't', 0 },                                          // 17
  65.     { 'd', 'a', 'r', 'n', 0 },                                     // 18
  66.     { '?', 0 },                                                    // 19
  67.     { 'q', 'u', 'i', 'c', 'k', 0 },                                // 20
  68.     { '#', 0 },                                                    // 21
  69.     { '&', 0 },                                                    // 22
  70.     { 'a', 'a', 'r', 'd', 'v', 'a', 'r', 'k', 0},                  // 23
  71.     { 'a', '-', 'r', 'd', 'v', 'a', 'r', 'k', 0},                  // 24
  72.     { 'a', 'b', 'b', 'o', 't', 0},                                 // 25
  73.     { 'c', 'o', 'o', 'p', 0},                                      // 26
  74.     { 'c', 'o', '-', 'p', 0},                                      // 27
  75.     { 'c', 'o', 'p', 0},                                           // 28 
  76.     { 'z', 'e', 'b', 'r', 'a', 0}                                  // 29
  77. };
  78.  
  79. const int32_t G7CollationTest::results[G7CollationTest::TESTLOCALES][G7CollationTest::TOTALTESTSET] = {
  80.     { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // en_US
  81.     { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // en_GB
  82.     { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // en_CA
  83.     { 12, 13, 9, 0, 14, 1, 11, 3, 2, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // fr_FR
  84.     { 12, 13, 9, 0, 14, 1, 11, 3, 2, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // fr_CA
  85.     { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // de_DE
  86.     { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // it_IT
  87.     { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // ja_JP
  88.     // new table collation with rules "& Z < p, P"  loop to FIXEDTESTSET
  89.     { 12, 13, 9, 0, 6, 8, 10, 7, 14, 1, 11, 2, 3, 4, 5, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 },
  90.     // new table collation with rules "& C < ch , cH, Ch, CH " // loop to TOTALTESTSET
  91.     { 19, 22, 21, 23, 25, 24, 12, 13, 9, 0, 17, 26, 28, 27, 15, 16, 18, 14, 1, 11, 2, 3, 4, 5, 20, 6, 8, 10, 7, 29 },
  92.     // new table collation with rules "& Question-mark ; ? & Hash-mark ; # & Ampersand ; '&'  " // loop to TOTALTESTSET
  93.     { 23, 25, 22, 24, 12, 13, 9, 0, 17, 16, 26, 28, 27, 15, 18, 21, 14, 1, 11, 2, 3, 4, 5, 19, 20, 6, 8, 10, 7, 29 },
  94.     // analogous to Japanese rules " & aa ; a- & ee ; e- & ii ; i- & oo ; o- & uu ; u- "  // loop to TOTALTESTSET
  95.     { 19, 22, 21, 23, 24, 25, 12, 13, 9, 0, 17, 16, 26, 27, 28, 15, 18, 14, 1, 11, 2, 3, 4, 5, 20, 6, 8, 10, 7, 29 }
  96. };
  97.  
  98.  
  99. void G7CollationTest::doTest( Collator* myCollation, UnicodeString source, UnicodeString target, Collator::EComparisonResult result)
  100. {
  101.     Collator::EComparisonResult compareResult = myCollation->compare(source, target);
  102.     CollationKey sortKey1, sortKey2;
  103.     UErrorCode key1status = U_ZERO_ERROR, key2status = U_ZERO_ERROR; //nos
  104.     myCollation->getCollationKey(source, /*nos*/ sortKey1, key1status );
  105.     myCollation->getCollationKey(target, /*nos*/ sortKey2, key2status );
  106.     if (U_FAILURE(key1status) || U_FAILURE(key2status)) {
  107.         errln("SortKey generation Failed.\n");
  108.         return;
  109.     }
  110.     Collator::EComparisonResult keyResult = sortKey1.compareTo(sortKey2);
  111.     reportCResult( source, target, sortKey1, sortKey2, compareResult, keyResult, result );
  112. }
  113.  
  114. void G7CollationTest::TestG7Locales( char* par )
  115. {
  116.     int32_t i;
  117.  
  118.     for (i = 0; i < 8; i++)
  119.     {
  120.         Collator *myCollation= 0;
  121.         UnicodeString dispName;
  122.         UErrorCode status = U_ZERO_ERROR;
  123.         RuleBasedCollator* tblColl1 = 0;
  124.  
  125.         myCollation = Collator::createInstance(locales[i], status);
  126.         if (U_FAILURE(status))
  127.         {
  128.             UnicodeString msg;
  129.  
  130.             msg += "Locale ";
  131.             msg += locales[i].getDisplayName(dispName);
  132.             msg += "creation failed.";
  133.  
  134.             errln(msg);
  135.             continue;
  136.         }
  137.  
  138.         const UnicodeString& defRules = ((RuleBasedCollator*)myCollation)->getRules();
  139.         status = U_ZERO_ERROR;
  140.         tblColl1 = new RuleBasedCollator(((RuleBasedCollator*)myCollation)->getRules(), status);
  141.         if (U_FAILURE(status))
  142.         {
  143.             UnicodeString msg, name;
  144.  
  145.             msg += "Recreate ";
  146.             msg += locales[i].getDisplayName(name);
  147.             msg += "collation failed.";
  148.  
  149.             errln(msg);
  150.             continue;
  151.         }
  152.  
  153.         UnicodeString msg;
  154.  
  155.         msg += "Locale ";
  156.         msg += locales[i].getDisplayName(dispName);
  157.         msg += "tests start :";
  158.         logln(msg);
  159.  
  160.         int32_t j = 0, n = 0;
  161.         for (j = 0; j < FIXEDTESTSET; j++)
  162.         {
  163.             for (n = j+1; n < FIXEDTESTSET; n++)
  164.             {
  165.                 doTest(tblColl1, testCases[results[i][j]], testCases[results[i][n]], Collator::LESS);
  166.             }
  167.         }
  168.  
  169.         delete myCollation;
  170.         delete tblColl1;
  171.     }
  172. }
  173.  
  174. void G7CollationTest::TestDemo1( char* par )
  175. {
  176.     logln("Demo Test 1 : Create a new table collation with rules \"& Z < p, P\"");
  177.     UErrorCode status = U_ZERO_ERROR;
  178.     Collator *col = Collator::createInstance(status);
  179.     const UnicodeString baseRules = ((RuleBasedCollator*)col)->getRules();
  180.     UnicodeString newRules(" & Z < p, P");
  181.     newRules.insert(0, baseRules);
  182.     RuleBasedCollator *myCollation = new RuleBasedCollator(newRules, status);
  183.  
  184.     if (U_FAILURE(status))
  185.     {
  186.         errln( "Demo Test 1 Table Collation object creation failed.");
  187.         return;
  188.     }
  189.  
  190.     int32_t j = 0, n = 0;
  191.     for (j = 0; j < FIXEDTESTSET; j++)
  192.     {
  193.         for (n = j+1; n < FIXEDTESTSET; n++)
  194.         {
  195.             doTest(myCollation, testCases[results[8][j]], testCases[results[8][n]], Collator::LESS);
  196.         }
  197.     }
  198.  
  199.     delete myCollation; 
  200.     delete col;
  201. }
  202.  
  203. void G7CollationTest::TestDemo2( char* par )
  204. {
  205.     logln("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\"");
  206.     UErrorCode status = U_ZERO_ERROR;
  207.     Collator *col = Collator::createInstance(status);
  208.     const UnicodeString baseRules = ((RuleBasedCollator*)col)->getRules();
  209.     UnicodeString newRules("& C < ch , cH, Ch, CH");
  210.     newRules.insert(0, baseRules);
  211.     RuleBasedCollator *myCollation = new RuleBasedCollator(newRules, status);
  212.  
  213.     if (U_FAILURE(status))
  214.     {
  215.         errln("Demo Test 2 Table Collation object creation failed.");
  216.         return;
  217.     }
  218.  
  219.     int32_t j = 0;
  220.     for (j; j < TOTALTESTSET; j++)
  221.     {
  222.         for (int32_t n = j+1; n < TOTALTESTSET; n++)
  223.         {
  224.             doTest(myCollation, testCases[results[9][j]], testCases[results[9][n]], Collator::LESS);
  225.         }
  226.     }
  227.     
  228.     delete myCollation; 
  229.     delete col;
  230. }
  231.  
  232. void G7CollationTest::TestDemo3( char* par )
  233. {
  234.     logln("Demo Test 3 : Create a new table collation with rules \"& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'\"");
  235.     UErrorCode status = U_ZERO_ERROR;
  236.     Collator *col = Collator::createInstance(status);
  237.     const UnicodeString baseRules = ((RuleBasedCollator*)col)->getRules();
  238.     UnicodeString newRules = "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'";
  239.     newRules.insert(0, baseRules);
  240.     RuleBasedCollator *myCollation = new RuleBasedCollator(newRules, status);
  241.  
  242.     if (U_FAILURE(status))
  243.     {
  244.         errln("Demo Test 3 Table Collation object creation failed.");
  245.         return;
  246.     }
  247.  
  248.     int32_t j = 0;
  249.     for (j = 0; j < TOTALTESTSET; j++)
  250.     {
  251.         for (int32_t n = j+1; n < TOTALTESTSET; n++)
  252.         {
  253.             doTest(myCollation, testCases[results[10][j]], testCases[results[10][n]], Collator::LESS);
  254.         }
  255.     }
  256.     
  257.     delete myCollation; 
  258.     delete col;
  259. }
  260.  
  261. void G7CollationTest::TestDemo4( char* par )
  262. {
  263.     logln("Demo Test 4 : Create a new table collation with rules \" & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' \"");
  264.     UErrorCode status = U_ZERO_ERROR;
  265.     Collator *col = Collator::createInstance(status);
  266.     const UnicodeString baseRules = ((RuleBasedCollator*)col)->getRules();
  267.     UnicodeString newRules = " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' ";
  268.     newRules.insert(0, baseRules);
  269.     RuleBasedCollator *myCollation = new RuleBasedCollator(newRules, status);
  270.  
  271.     if (U_FAILURE(status))
  272.     {
  273.         errln( "Demo Test 4 Table Collation object creation failed." );
  274.         return;
  275.     }
  276.  
  277.     int32_t j;
  278.     for (j = 0; j < TOTALTESTSET; j++)
  279.     {
  280.         for (int32_t n = j+1; n < TOTALTESTSET; n++)
  281.         {
  282.             doTest(myCollation, testCases[results[11][j]], testCases[results[11][n]], Collator::LESS);
  283.         }
  284.     }
  285.  
  286.     delete myCollation; 
  287.     delete col;
  288. }
  289.  
  290. void G7CollationTest::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  291. {
  292.     if (exec) logln("TestSuite G7CollationTest: ");
  293.     switch (index) {
  294.         case 0: name = "TestG7Locales"; if (exec)   TestG7Locales( par ); break;
  295.         case 1: name = "TestDemo1"; if (exec)   TestDemo1( par ); break;
  296.         case 2: name = "TestDemo2"; if (exec)   TestDemo2( par ); break;
  297.         case 3: name = "TestDemo3"; if (exec)   TestDemo3( par ); break;
  298.         case 4: name = "TestDemo4"; if (exec)   TestDemo4( par ); break;
  299.         default: name = ""; break;
  300.     }
  301. }
  302.  
  303.  
  304.