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

  1. /*
  2. ********************************************************************
  3. * COPYRIGHT: 
  4. * (C) Copyright Taligent, Inc., 1997
  5. * (C) Copyright International Business Machines Corporation, 1997 - 1998
  6. * Licensed Material - Program-Property of IBM - All Rights Reserved. 
  7. * US Government Users Restricted Rights - Use, duplication, or disclosure 
  8. * restricted by GSA ADP Schedule Contract with IBM Corp. 
  9. *
  10. ********************************************************************
  11. */
  12.  
  13. #ifndef _COLL
  14. #include "coll.h"
  15. #endif
  16.  
  17. #ifndef _TBLCOLL
  18. #include "tblcoll.h"
  19. #endif
  20.  
  21. #ifndef _UNISTR
  22. #include "unistr.h"
  23. #endif
  24.  
  25. #ifndef _SORTKEY
  26. #include "sortkey.h"
  27. #endif
  28.  
  29. #ifndef _CURRCOLL
  30. #include "currcoll.h"
  31. #endif
  32.  
  33. #define ARRAY_LENGTH(array) (sizeof array / sizeof array[0])
  34.  
  35. CollationCurrencyTest::CollationCurrencyTest()
  36. {
  37. }
  38.  
  39. CollationCurrencyTest::~CollationCurrencyTest()
  40. {
  41. }
  42.  
  43. void CollationCurrencyTest::currencyTest(char *par)
  44. {
  45.     // All the currency symbols, in collation order
  46.     static const UChar currency[] =
  47.     {
  48.         0x00a4, // generic currency
  49.         0x0e3f, // baht
  50.         0x00a2, // cent
  51.         0x20a1, // colon
  52.         0x20a2, // cruzeiro
  53.         0x0024, // dollar
  54.         0x20ab, // dong
  55.         0x20ac, // euro
  56.         0x20a3, // franc
  57.         0x20a4, // lira
  58.         0x20a5, // mill
  59.         0x20a6, // naira
  60.         0x20a7, // peseta
  61.         0x00a3, // pound
  62.         0x20a8, // rupee
  63.         0x20aa, // shekel
  64.         0x20a9, // won
  65.         0x00a5  // yen
  66.     };
  67.     
  68.  
  69.     int32_t i, j;
  70.     UErrorCode status = U_ZERO_ERROR;
  71.     Collator::EComparisonResult expectedResult = Collator::EQUAL;
  72.     RuleBasedCollator *c = (RuleBasedCollator *)Collator::createInstance(status);
  73.  
  74.     if (U_FAILURE(status))
  75.     {
  76.         errln ("Collator::createInstance() failed!");
  77.         return;
  78.     }
  79.  
  80.     // Compare each currency symbol against all the
  81.     // currency symbols, including itself
  82.     for (i = 0; i < ARRAY_LENGTH(currency); i += 1)
  83.     {
  84.         for (j = 0; j < ARRAY_LENGTH(currency); j += 1)
  85.         {
  86.             UnicodeString source(¤cy[i], 1);
  87.             UnicodeString target(¤cy[j], 1);
  88.  
  89.             if (i < j)
  90.             {
  91.                 expectedResult = Collator::LESS;
  92.             }
  93.             else if ( i == j)
  94.             {
  95.                 expectedResult = Collator::EQUAL;
  96.             }
  97.             else
  98.             {
  99.                 expectedResult = Collator::GREATER;
  100.             }
  101.  
  102.             Collator::EComparisonResult compareResult = c->compare(source, target);
  103.  
  104.             CollationKey sourceKey, targetKey;
  105.             UErrorCode status = U_ZERO_ERROR;
  106.  
  107.             c->getCollationKey(source, sourceKey, status);
  108.  
  109.             if (U_FAILURE(status))
  110.             {
  111.                 errln("Couldn't get collationKey for source");
  112.                 continue;
  113.             }
  114.  
  115.             c->getCollationKey(target, targetKey, status);
  116.  
  117.             if (U_FAILURE(status))
  118.             {
  119.                 errln("Couldn't get collationKey for target");
  120.                 continue;
  121.             }
  122.  
  123.             Collator::EComparisonResult keyResult = sourceKey.compareTo(targetKey);
  124.  
  125.             reportCResult(source, target, sourceKey, targetKey,
  126.                           compareResult, keyResult, expectedResult);
  127.  
  128.         }
  129.     }
  130. }
  131.  
  132. void CollationCurrencyTest::runIndexedTest(int32_t index, bool_t exec, char* &name, char* par)
  133. {
  134.     if (exec)
  135.     {
  136.         logln("Collation Currency Tests: ");
  137.     }
  138.  
  139.     switch (index)
  140.     {
  141.         case  0: name = "currencyTest"; if (exec) currencyTest(par); break;
  142.         default: name = ""; break;
  143.     }
  144. }
  145.