home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / intltest / mnkytst.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-07  |  6.2 KB  |  227 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 _MNKYTST
  31. #include "mnkytst.h"
  32. #endif
  33.  
  34.  
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <time.h>
  38.  
  39. #ifndef MIN
  40. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  41. #endif
  42.  
  43. #ifndef MAX
  44. #define MAX(x,y) ((x) > (y) ? (x) : (y))
  45. #endif
  46.  
  47. const UnicodeString CollationMonkeyTest::source("-abcdefghijklmnopqrstuvwxyz#&^$@");
  48.  
  49. CollationMonkeyTest::CollationMonkeyTest()
  50. : myCollator(0)
  51. {
  52.     UErrorCode status = U_ZERO_ERROR;
  53.     myCollator = Collator::createInstance(status);
  54. }
  55.  
  56. CollationMonkeyTest::~CollationMonkeyTest()
  57. {
  58.     delete myCollator;
  59. }
  60.  
  61.  
  62. void 
  63. CollationMonkeyTest::report(UnicodeString& s, UnicodeString& t, int32_t result, int32_t revResult)
  64. {
  65.     if (revResult != -result)
  66.     {
  67.         UnicodeString msg;
  68.  
  69.         msg += s; 
  70.         msg += " and ";
  71.         msg += t;
  72.         msg += " round trip comparison failed";
  73.         msg += (UnicodeString) " (result " + result + ", reverse Result " + revResult + ")"; 
  74.  
  75.         errln(msg);
  76.     }
  77. }
  78.  
  79. int32_t 
  80. CollationMonkeyTest::checkValue(int32_t value)
  81. {
  82.     if (value < 0)
  83.     {
  84.         return -value;
  85.     }
  86.  
  87.     return value;
  88. }
  89.  
  90. void CollationMonkeyTest::TestCollationKey( char* par )
  91. {
  92.     srand( (unsigned)time( NULL ) );
  93.     int32_t s = checkValue(rand() % source.size());
  94.     int32_t t = checkValue(rand() % source.size());
  95.     int32_t slen = checkValue((rand() - source.size()) % source.size());
  96.     int32_t tlen = checkValue((rand() - source.size()) % source.size());
  97.     UnicodeString subs, subt;
  98.  
  99.     source.extract(MIN(s, slen), MAX(s, slen), subs);
  100.     source.extract(MIN(t, tlen), MAX(t, tlen), subt);
  101.  
  102.     CollationKey collationKey1, collationKey2;
  103.     UErrorCode status1 = U_ZERO_ERROR, status2= U_ZERO_ERROR;
  104.  
  105.     myCollator->setStrength(Collator::TERTIARY);
  106.     myCollator->getCollationKey(subs, collationKey1, status1);
  107.     myCollator->getCollationKey(subt, collationKey2, status2);
  108.     int32_t result = collationKey1.compareTo(collationKey2);  // Tertiary
  109.     int32_t revResult = collationKey2.compareTo(collationKey1);  // Tertiary
  110.     report( subs, subt, result, revResult);
  111.  
  112.     myCollator->setStrength(Collator::SECONDARY);
  113.     myCollator->getCollationKey(subs, collationKey1, status1);
  114.     myCollator->getCollationKey(subt, collationKey2, status2);
  115.     result = collationKey1.compareTo(collationKey2);  // Secondary
  116.     revResult = collationKey2.compareTo(collationKey1);   // Secondary
  117.     report( subs, subt, result, revResult);
  118.  
  119.     myCollator->setStrength(Collator::PRIMARY);
  120.     myCollator->getCollationKey(subs, collationKey1, status1);
  121.     myCollator->getCollationKey(subt, collationKey2, status2);
  122.     result = collationKey1.compareTo(collationKey2);  // Primary
  123.     revResult = collationKey2.compareTo(collationKey1);   // Primary
  124.     report(subs, subt, result, revResult);
  125.  
  126.     UnicodeString msg;
  127.     UnicodeString addOne(subs);
  128.     addOne += 0xE000;
  129.  
  130.     myCollator->getCollationKey(subs, collationKey1, status1);
  131.     myCollator->getCollationKey(addOne, collationKey2, status2);
  132.     result = collationKey1.compareTo(collationKey2);
  133.     if (result != -1)
  134.     {
  135.         msg += "CollationKey(";
  136.         msg += subs;
  137.         msg += ") .LT. CollationKey(";
  138.         msg += addOne;
  139.         msg += ") Failed.";
  140.         errln(msg);
  141.     }
  142.  
  143.     msg.remove();
  144.     result = collationKey2.compareTo(collationKey1);
  145.     if (result != 1)
  146.     {
  147.         msg += "CollationKey(";
  148.         msg += addOne;
  149.         msg += ") .GT. CollationKey(";
  150.         msg += subs;
  151.         msg += ") Failed.";
  152.         errln(msg);
  153.     }
  154. }
  155.  
  156. void 
  157. CollationMonkeyTest::TestCompare(char* par)
  158. {
  159.  
  160.     /* Seed the random-number generator with current time so that
  161.      * the numbers will be different every time we run.
  162.      */
  163.     srand( (unsigned)time( NULL ) );
  164.     int32_t s = checkValue(rand() % source.size());
  165.     int32_t t = checkValue(rand() % source.size());
  166.     int32_t slen = checkValue((rand() - source.size()) % source.size());
  167.     int32_t tlen = checkValue((rand() - source.size()) % source.size());
  168.     UnicodeString subs, subt;
  169.  
  170.     source.extract(MIN(s, slen), MAX(s, slen), subs);
  171.     source.extract(MIN(t, tlen), MAX(t, tlen), subt);
  172.  
  173.     myCollator->setStrength(Collator::TERTIARY);
  174.     int32_t result = myCollator->compare(subs, subt);  // Tertiary
  175.     int32_t revResult = myCollator->compare(subt, subs);  // Tertiary
  176.     report(subs, subt, result, revResult);
  177.  
  178.     myCollator->setStrength(Collator::SECONDARY);
  179.     result = myCollator->compare(subs, subt);  // Secondary
  180.     revResult = myCollator->compare(subt, subs);  // Secondary
  181.     report(subs, subt, result, revResult);
  182.  
  183.     myCollator->setStrength(Collator::PRIMARY);
  184.     result = myCollator->compare(subs, subt);  // Primary
  185.     revResult = myCollator->compare(subt, subs);  // Primary
  186.     report(subs, subt, result, revResult);
  187.  
  188.     UnicodeString msg;
  189.     UnicodeString addOne(subs);
  190.     addOne += 0xE000;
  191.  
  192.     result = myCollator->compare(subs, addOne);
  193.     if (result != -1)
  194.     {
  195.         msg += "Test : ";
  196.         msg += subs;
  197.         msg += " .LT. ";
  198.         msg += addOne;
  199.         msg += " Failed.";
  200.         errln(msg);
  201.     }
  202.  
  203.     msg.remove();
  204.     result = myCollator->compare(addOne, subs);
  205.     if (result != 1)
  206.     {
  207.         msg += "Test : ";
  208.         msg += addOne;
  209.         msg += " .GT. ";
  210.         msg += subs;
  211.         msg += " Failed.";
  212.         errln(msg);
  213.     }
  214. }
  215.  
  216. void CollationMonkeyTest::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  217. {
  218.     if (exec) logln("TestSuite CollationMonkeyTest: ");
  219.     switch (index) {
  220.         case 0: name = "TestCompare";   if (exec)   TestCompare( par ); break;
  221.         case 1: name = "TestCollationKey"; if (exec)    TestCollationKey( par ); break;
  222.         default: name = ""; break;
  223.     }
  224. }
  225.  
  226.  
  227.