home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / intltest / apicoll.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  24.6 KB  |  606 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. //
  15. // File apitest.cpp
  16. //
  17. // 
  18. //
  19. // Created by: Helena Shih
  20. //
  21. // Modification History:
  22. //
  23. //  Date         Name          Description
  24. //  2/5/97      aliu        Added streamIn and streamOut methods.  Added
  25. //                          constructor which reads RuleBasedCollator object from
  26. //                          a binary file.  Added writeToFile method which streams
  27. //                          RuleBasedCollator out to a binary file.  The streamIn
  28. //                          and streamOut methods use istream and ostream objects
  29. //                          in binary mode.
  30. //  6/30/97     helena      Added tests for CollationElementIterator::setText, getOffset
  31. //                          setOffset and DecompositionIterator::getOffset, setOffset.  
  32. //                          DecompositionIterator is made public so add class scope
  33. //                          testing.
  34. //  02/10/98    damiba      Added test for compare(UnicodeString&, UnicodeString&, int32_t)
  35. //===============================================================================
  36. #include <iostream.h>
  37.  
  38. #ifndef COLL_H
  39. #include "coll.h"
  40. #endif
  41.  
  42. #ifndef TBLCOLL_H
  43. #include "tblcoll.h"
  44. #endif
  45.  
  46. #ifndef COLEITR_H
  47. #include "coleitr.h"
  48. #endif
  49.  
  50. #ifndef SORTKEY_H
  51. #include "sortkey.h"
  52. #endif
  53.  
  54. #ifndef _APICOLL
  55. #include "apicoll.h"
  56. #endif
  57.  
  58. void
  59. CollationAPITest::doAssert(bool_t condition, const char *message)
  60. {
  61.     if (!condition) {
  62.         errln(UnicodeString("ERROR : ") + message);
  63.     }
  64. }
  65.  
  66. // Collator Class Properties
  67. // ctor, dtor, createInstance, compare, getStrength/setStrength
  68. // getDecomposition/setDecomposition, getDisplayName
  69. void
  70. CollationAPITest::TestProperty( char* par )
  71. {
  72.     UErrorCode success = U_ZERO_ERROR;
  73.     Collator *col = 0;
  74.  
  75.     logln("The property tests begin : ");
  76.     logln("Test ctors : ");
  77.     col = Collator::createInstance(success);
  78.  
  79.     if (U_FAILURE(success))
  80.     {
  81.         errln("Default Collator creation failed.");
  82.         return;
  83.     }
  84.  
  85.     doAssert((col->compare("ab", "abc") == Collator::LESS), "ab < abc comparison failed");
  86.     doAssert((col->compare("ab", "AB") == Collator::LESS), "ab < AB comparison failed");
  87.     doAssert((col->compare("black-bird", "blackbird") == Collator::GREATER), "black-bird > blackbird comparison failed");
  88.     doAssert((col->compare("black bird", "black-bird") == Collator::LESS), "black bird > black-bird comparison failed");
  89.     doAssert((col->compare("Hello", "hello") == Collator::GREATER), "Hello > hello comparison failed");
  90.  
  91.  
  92.     /*start of update [Bertrand A. D. 02/10/98]*/
  93.     doAssert((col->compare("ab", "abc", 2) == Collator::EQUAL), "ab = abc with length 2 comparison failed");
  94.     doAssert((col->compare("ab", "AB", 2) == Collator::LESS), "ab < AB  with length 2 comparison failed");
  95.     doAssert((col->compare("ab", "Aa", 1) == Collator::LESS), "ab < Aa  with length 1 comparison failed");
  96.     doAssert((col->compare("ab", "Aa", 2) == Collator::GREATER), "ab > Aa  with length 2 comparison failed");
  97.     doAssert((col->compare("black-bird", "blackbird", 5) == Collator::EQUAL), "black-bird = blackbird with length of 5 comparison failed");
  98.     doAssert((col->compare("black bird", "black-bird", 10) == Collator::LESS), "black bird < black-bird with length 10 comparison failed");
  99.     doAssert((col->compare("Hello", "hello", 5) == Collator::GREATER), "Hello > hello with length 5 comparison failed");
  100.     /*end of update [Bertrand A. D. 02/10/98]*/
  101.  
  102.  
  103.     logln("Test ctors ends.");
  104.     logln("testing Collator::getStrength() method ...");
  105.     doAssert((col->getStrength() == Collator::TERTIARY), "collation object has the wrong strength");
  106.     doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference");
  107.         
  108.     logln("testing Collator::setStrength() method ...");
  109.     col->setStrength(Collator::SECONDARY);
  110.     doAssert((col->getStrength() != Collator::TERTIARY), "collation object's strength is secondary difference");
  111.     doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference");
  112.     doAssert((col->getStrength() == Collator::SECONDARY), "collation object has the wrong strength");
  113.  
  114.     logln("testing Collator::setDecomposition() method ...");
  115.     col->setDecomposition(Normalizer::NO_OP);
  116.     doAssert((col->getDecomposition() != Normalizer::DECOMP), "collation object's strength is secondary difference");
  117.     doAssert((col->getDecomposition() != Normalizer::DECOMP_COMPAT), "collation object's strength is primary difference");
  118.     doAssert((col->getDecomposition() == Normalizer::NO_OP), "collation object has the wrong strength");
  119.  
  120.     UnicodeString name;
  121.  
  122.     logln("Get display name for the US English collation in German : ");
  123.     logln(Collator::getDisplayName(Locale::US, Locale::GERMAN, name));  
  124.     doAssert((name == UnicodeString("Englisch (Vereinigte Staaten)")), "getDisplayName failed");
  125.  
  126.     logln("Get display name for the US English collation in English : ");
  127.     logln(Collator::getDisplayName(Locale::US, Locale::ENGLISH, name)); 
  128.     doAssert((name == UnicodeString("English (United States)")), "getDisplayName failed");
  129.  
  130.     logln("Default collation property test ended.");
  131.     logln("Collator::getRules() testing ...");  
  132.     doAssert(((RuleBasedCollator*)col)->getRules().size() != 0, "getRules() result incorrect" );                
  133.     logln("getRules tests end.");
  134.  
  135.     delete col; col = 0;
  136.     col = Collator::createInstance(Locale::FRENCH, success);
  137.     if (U_FAILURE(success))
  138.     {
  139.         errln("Creating French collation failed.");
  140.         return;
  141.     }
  142.  
  143.     col->setStrength(Collator::PRIMARY);
  144.     logln("testing Collator::getStrength() method again ...");
  145.     doAssert((col->getStrength() != Collator::TERTIARY), "collation object has the wrong strength");
  146.     doAssert((col->getStrength() == Collator::PRIMARY), "collation object's strength is not primary difference");
  147.         
  148.     logln("testing French Collator::setStrength() method ...");
  149.     col->setStrength(Collator::TERTIARY);
  150.     doAssert((col->getStrength() == Collator::TERTIARY), "collation object's strength is not tertiary difference");
  151.     doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference");
  152.     doAssert((col->getStrength() != Collator::SECONDARY), "collation object's strength is secondary difference");
  153.  
  154.     logln("Create junk collation: ");
  155.     Locale abcd("ab", "CD", "");
  156.     success = U_ZERO_ERROR;
  157.     Collator *junk = 0;
  158.     junk = Collator::createInstance(abcd, success);
  159.  
  160.     if (U_FAILURE(success))
  161.     {
  162.         errln("Junk collation creation failed, should at least return default.");
  163.         delete col;
  164.         return;
  165.     }
  166.  
  167.     delete col;
  168.     col = Collator::createInstance(success);
  169.     if (U_FAILURE(success))
  170.     {
  171.         errln("Creating default collator failed.");
  172.         delete junk;
  173.         return;
  174.     }
  175.  
  176.     doAssert((*col == *junk), "The default collation should be returned.");
  177.     Collator *frCol = Collator::createInstance(Locale::FRANCE, success);
  178.     if (U_FAILURE(success))
  179.     {
  180.         errln("Creating French collator failed.");
  181.         delete col; delete junk;
  182.         return;
  183.     }
  184.  
  185.     doAssert((*frCol != *junk), "The junk is the same as the French collator.");
  186.     Collator *aFrCol = frCol->clone();
  187.     doAssert((*frCol == *aFrCol), "The cloning of a French collator failed.");
  188.     logln("Collator property test ended.");
  189.  
  190.     delete col;
  191.     delete frCol;
  192.     delete aFrCol;
  193.     delete junk;
  194. }
  195.  
  196. void 
  197. CollationAPITest::TestHashCode( char* par )
  198. {
  199.     logln("hashCode tests begin.");
  200.     UErrorCode success = U_ZERO_ERROR;
  201.     Collator *col1 = 0;
  202.     col1 = Collator::createInstance(success);
  203.     if (U_FAILURE(success))
  204.     {
  205.         errln("Default collation creation failed.");
  206.         return;
  207.     }
  208.  
  209.     Collator *col2 = 0;
  210.     Locale dk("da", "DK", "");
  211.     col2 = Collator::createInstance(dk, success);
  212.     if (U_FAILURE(success))
  213.     {
  214.         errln("Danish collation creation failed.");
  215.         return;
  216.     }
  217.  
  218.     Collator *col3 = 0;
  219.     col3 = Collator::createInstance(success);
  220.     if (U_FAILURE(success))
  221.     {
  222.         errln("2nd default collation creation failed.");
  223.         return;
  224.     }
  225.  
  226.     logln("Collator::hashCode() testing ...");
  227.     
  228.     doAssert(col1->hashCode() != col2->hashCode(), "Hash test1 result incorrect" );                 
  229.     doAssert(!(col1->hashCode() == col2->hashCode()), "Hash test2 result incorrect" );              
  230.     doAssert(col1->hashCode() == col3->hashCode(), "Hash result not equal" );               
  231.  
  232.     logln("hashCode tests end.");
  233.     delete col1;
  234.     delete col2;
  235.     delete col3;
  236. }
  237.  
  238. //----------------------------------------------------------------------------
  239. // CollationKey -- Tests the CollationKey methods
  240. //
  241. void
  242. CollationAPITest::TestCollationKey( char* par )
  243. {       
  244.     logln("testing CollationKey begins...");
  245.     Collator *col = 0;
  246.     UErrorCode success = U_ZERO_ERROR;
  247.     col = Collator::createInstance(success);
  248.     if (U_FAILURE(success))
  249.     {
  250.         errln("Default collation creation failed.");
  251.         return;
  252.     }
  253.  
  254.     CollationKey sortk1, sortk2;
  255.     UnicodeString test1("Abcda"), test2("abcda");
  256.     UErrorCode key1Status = U_ZERO_ERROR, key2Status = U_ZERO_ERROR;
  257.                 
  258.     logln("Use tertiary comparison level testing ....");
  259.  
  260.     doAssert((col->getCollationKey(test1, sortk1, key1Status).compareTo(col->getCollationKey(test2, sortk2, key2Status))) 
  261.                  == Collator::GREATER, 
  262.                 "Result should be \"Abcda\" >>> \"abcda\"");
  263.  
  264.     CollationKey sortk3(sortk2), sortkNew, sortkEmpty;
  265.  
  266.     sortkNew = sortk1;
  267.     doAssert((sortk1 != sortk2), "The sort keys should be different");
  268.     doAssert((sortk1.hashCode() != sortk2.hashCode()), "sort key hashCode() failed");
  269.     doAssert((sortk2 == sortk3), "The sort keys should be the same");
  270.     doAssert((sortk1 == sortkNew), "The sort keys assignment failed");
  271.     doAssert((sortk1.hashCode() == sortkNew.hashCode()), "sort key hashCode() failed");
  272.     doAssert((sortkNew != sortk3), "The sort keys should be different");
  273.     doAssert(sortk1.compareTo(sortk3) == Collator::GREATER, "Result should be \"Abcda\" >>> \"abcda\"");
  274.     doAssert(sortk2.compareTo(sortk3) == Collator::EQUAL, "Result should be \"abcda\" == \"abcda\"");
  275.     doAssert(sortkEmpty.compareTo(sortk1) == Collator::LESS, "Result should be (empty key) <<< \"Abcda\"");
  276.     doAssert(sortk1.compareTo(sortkEmpty) == Collator::GREATER, "Result should be \"Abcda\" >>> (empty key)");
  277.     doAssert(sortkEmpty.compareTo(sortkEmpty) == Collator::EQUAL, "Result should be (empty key) == (empty key)");
  278.  
  279.     int32_t    cnt1, cnt2, cnt3, cnt4;
  280.     uint8_t* byteArray1 = 0;
  281.  
  282.     byteArray1 = sortk1.toByteArray(cnt1);
  283.     uint8_t* byteArray2 = 0;
  284.     
  285.     byteArray2 = sortk2.toByteArray(cnt2);
  286.  
  287.     const uint8_t* byteArray3 = 0;
  288.     byteArray3 = sortk1.getByteArray(cnt3);
  289.  
  290.     const uint8_t* byteArray4 = 0;
  291.     byteArray4 = sortk2.getByteArray(cnt4);
  292.  
  293.     CollationKey sortk4(byteArray1, cnt1), sortk5(byteArray2, cnt2);
  294.     CollationKey sortk6(byteArray3, cnt3), sortk7(byteArray4, cnt4);
  295.  
  296.     doAssert(sortk1.compareTo(sortk4) == Collator::EQUAL, "CollationKey::toByteArray(sortk1) Failed.");
  297.     doAssert(sortk2.compareTo(sortk5) == Collator::EQUAL, "CollationKey::toByteArray(sortk2) Failed.");
  298.     doAssert(sortk4.compareTo(sortk5) == Collator::GREATER, "sortk4 >>> sortk5 Failed");
  299.     doAssert(sortk1.compareTo(sortk6) == Collator::EQUAL, "CollationKey::getByteArray(sortk1) Failed.");
  300.     doAssert(sortk2.compareTo(sortk7) == Collator::EQUAL, "CollationKey::getByteArray(sortk2) Failed.");
  301.     doAssert(sortk6.compareTo(sortk7) == Collator::GREATER, "sortk6 >>> sortk7 Failed");
  302.  
  303.     logln("Equality tests : ");
  304.     doAssert(sortk1 == sortk4, "sortk1 == sortk4 Failed.");
  305.     doAssert(sortk2 == sortk5, "sortk2 == sortk5 Failed.");
  306.     doAssert(sortk1 != sortk5, "sortk1 != sortk5 Failed.");
  307.     doAssert(sortk1 == sortk6, "sortk1 == sortk6 Failed.");
  308.     doAssert(sortk2 == sortk7, "sortk2 == sortk7 Failed.");
  309.     doAssert(sortk1 != sortk7, "sortk1 != sortk7 Failed.");
  310.  
  311.     delete [] byteArray1; byteArray1 = 0;
  312.     delete [] byteArray2; byteArray2 = 0;
  313.  
  314.     sortk3 = sortk1;
  315.     doAssert(sortk1 == sortk3, "sortk1 = sortk3 assignment Failed.");
  316.     doAssert(sortk2 != sortk3, "sortk2 != sortk3 Failed.");
  317.     logln("testing sortkey ends...");
  318.     delete col;
  319. }
  320.  
  321. //----------------------------------------------------------------------------
  322. // Tests the CollatorElementIterator class.
  323. // ctor, RuleBasedCollator::createCollationElementIterator(), operator==, operator!=
  324. // 
  325. void
  326. CollationAPITest::TestElemIter( char* par )
  327. {       
  328.     logln("testing sortkey begins...");
  329.     Collator *col = 0;
  330.     UErrorCode success = U_ZERO_ERROR;
  331.     col = Collator::createInstance(success);
  332.     if (U_FAILURE(success))
  333.     {
  334.         errln("Default collation creation failed.");
  335.         return;
  336.     }
  337.  
  338.     UnicodeString testString1("XFILE What subset of all possible test cases has the highest probability of detecting the most errors?");
  339.     UnicodeString testString2("Xf ile What subset of all possible test cases has the lowest probability of detecting the least errors?");
  340.     logln("Constructors and comparison testing....");
  341.     CollationElementIterator *iterator1 = ((RuleBasedCollator*)col)->createCollationElementIterator(testString1);
  342.  
  343.     // copy ctor
  344.     CollationElementIterator *iterator2 = new CollationElementIterator(*iterator1);
  345.     CollationElementIterator *iterator3 = ((RuleBasedCollator*)col)->createCollationElementIterator(testString2);
  346.     int32_t order1, order2, order3;
  347.     doAssert((*iterator1 == *iterator2), "The two iterators should be the same"); 
  348.     doAssert((*iterator1 != *iterator3), "The two iterators should be different");
  349.     order1 = iterator1->next(success);
  350.     if (U_FAILURE(success))
  351.     {
  352.         errln("Somehow ran out of memory stepping through the iterator.");
  353.         return;
  354.     }
  355.  
  356.     doAssert((*iterator1 != *iterator2), "The first iterator advance failed");
  357.     order2 = iterator2->next(success);
  358.     if (U_FAILURE(success))
  359.     {
  360.         errln("Somehow ran out of memory stepping through the iterator.");
  361.         return;
  362.     }
  363.  
  364.     doAssert((*iterator1 == *iterator2), "The second iterator advance failed"); 
  365.     doAssert((order1 == order2), "The order result should be the same");
  366.     order3 = iterator3->next(success);
  367.     if (U_FAILURE(success))
  368.     {
  369.         errln("Somehow ran out of memory stepping through the iterator.");
  370.         return;
  371.     }
  372.  
  373.     doAssert((CollationElementIterator::primaryOrder(order1) == 
  374.         CollationElementIterator::primaryOrder(order3)), "The primary orders should be the same");
  375.     doAssert((CollationElementIterator::secondaryOrder(order1) == 
  376.         CollationElementIterator::secondaryOrder(order3)), "The secondary orders should be the same");
  377.     doAssert((CollationElementIterator::tertiaryOrder(order1) == 
  378.         CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be the same");
  379.  
  380.     order1 = iterator1->next(success); order3 = iterator3->next(success);
  381.     if (U_FAILURE(success))
  382.     {
  383.         errln("Somehow ran out of memory stepping through the iterator.");
  384.         return;
  385.     }
  386.  
  387.     doAssert((CollationElementIterator::primaryOrder(order1) == 
  388.         CollationElementIterator::primaryOrder(order3)), "The primary orders should be identical");
  389.     doAssert((CollationElementIterator::tertiaryOrder(order1) != 
  390.         CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be different");
  391.  
  392.     order1 = iterator1->next(success); order3 = iterator3->next(success);
  393.     doAssert((CollationElementIterator::secondaryOrder(order1) != 
  394.         CollationElementIterator::secondaryOrder(order3)), "The secondary orders should be different");
  395.     doAssert((order1 != CollationElementIterator::NULLORDER), "Unexpected end of iterator reached");
  396.  
  397.     iterator1->reset(); iterator2->reset(); iterator3->reset();
  398.     order1 = iterator1->next(success);
  399.     if (U_FAILURE(success))
  400.     {
  401.         errln("Somehow ran out of memory stepping through the iterator.");
  402.         return;
  403.     }
  404.  
  405.     doAssert((*iterator1 != *iterator2), "The first iterator advance failed");
  406.  
  407.     order2 = iterator2->next(success);
  408.     if (U_FAILURE(success))
  409.     {
  410.         errln("Somehow ran out of memory stepping through the iterator.");
  411.         return;
  412.     }
  413.  
  414.     doAssert((*iterator1 == *iterator2), "The second iterator advance failed");
  415.     doAssert((order1 == order2), "The order result should be the same");
  416.  
  417.     order3 = iterator3->next(success);
  418.     if (U_FAILURE(success))
  419.     {
  420.         errln("Somehow ran out of memory stepping through the iterator.");
  421.         return;
  422.     }
  423.  
  424.     doAssert((CollationElementIterator::primaryOrder(order1) == 
  425.         CollationElementIterator::primaryOrder(order3)), "The primary orders should be the same");
  426.     doAssert((CollationElementIterator::secondaryOrder(order1) == 
  427.         CollationElementIterator::secondaryOrder(order3)), "The secondary orders should be the same");
  428.     doAssert((CollationElementIterator::tertiaryOrder(order1) == 
  429.         CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be the same");
  430.  
  431.     order1 = iterator1->next(success); order2 = iterator2->next(success); order3 = iterator3->next(success);
  432.     if (U_FAILURE(success))
  433.     {
  434.         errln("Somehow ran out of memory stepping through the iterator.");
  435.         return;
  436.     }
  437.  
  438.     doAssert((CollationElementIterator::primaryOrder(order1) == 
  439.         CollationElementIterator::primaryOrder(order3)), "The primary orders should be identical");
  440.     doAssert((CollationElementIterator::tertiaryOrder(order1) != 
  441.         CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be different");
  442.  
  443.     order1 = iterator1->next(success); order3 = iterator3->next(success);
  444.     if (U_FAILURE(success))
  445.     {
  446.         errln("Somehow ran out of memory stepping through the iterator.");
  447.         return;
  448.     }
  449.  
  450.     doAssert((CollationElementIterator::secondaryOrder(order1) != 
  451.         CollationElementIterator::secondaryOrder(order3)), "The secondary orders should be different");
  452.     doAssert((order1 != CollationElementIterator::NULLORDER), "Unexpected end of iterator reached");
  453.     doAssert((*iterator2 != *iterator3), "The iterators should be different");
  454.  
  455.     delete iterator1;
  456.     delete iterator2;
  457.     delete iterator3;
  458.     delete col;
  459.  
  460.     logln("testing CollationElementIterator ends...");
  461. }
  462.  
  463. // Test RuleBasedCollator ctor, dtor, operator==, operator!=, clone, copy, and getRules
  464. void
  465. CollationAPITest::TestOperators( char* par )
  466. {
  467.     UErrorCode success = U_ZERO_ERROR;
  468.     UnicodeString ruleset1("< a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
  469.     UnicodeString ruleset2("< a, A < b, B < c, C < d, D, e, E");
  470.     RuleBasedCollator *col1 = new RuleBasedCollator(ruleset1, success);
  471.     if (U_FAILURE(success)) {
  472.         errln("RuleBasedCollator creation failed.");
  473.         return;
  474.     }
  475.     success = U_ZERO_ERROR;
  476.     RuleBasedCollator *col2 = new RuleBasedCollator(ruleset2, success);
  477.     if (U_FAILURE(success)) {
  478.         errln("The RuleBasedCollator constructor failed when building with the 2nd rule set.");
  479.         return;
  480.     }
  481.     logln("The operator tests begin : ");
  482.     logln("testing operator==, operator!=, clone  methods ...");
  483.     doAssert((*col1 != *col2), "The two different table collations compared equal");
  484.     *col1 = *col2;
  485.     doAssert((*col1 == *col2), "Collator objects not equal after assignment (operator=)");
  486.         
  487.     success = U_ZERO_ERROR;
  488.     Collator *col3 = Collator::createInstance(success);
  489.     if (U_FAILURE(success)) {
  490.         errln("Default collation creation failed.");
  491.         return;
  492.     }
  493.     doAssert((*col1 != *col3), "The two different table collations compared equal");
  494.     Collator* col4 = col1->clone();
  495.     Collator* col5 = col3->clone();
  496.     doAssert((*col1 == *col4), "Cloned collation objects not equal");
  497.     doAssert((*col3 != *col4), "Two different table collations compared equal");
  498.     doAssert((*col3 == *col5), "Cloned collation objects not equal");
  499.     doAssert((*col4 != *col5), "Two cloned collations compared equal");
  500.  
  501.     const UnicodeString& defRules = ((RuleBasedCollator*)col3)->getRules();
  502.     RuleBasedCollator* col6 = new RuleBasedCollator(defRules, success);
  503.     if (U_FAILURE(success)) {
  504.         errln("Creating default collation with rules failed.");
  505.         return;
  506.     }
  507.     doAssert((((RuleBasedCollator*)col3)->getRules() == col6->getRules()), "Default collator getRules failed");
  508.  
  509.     logln("operator tests ended.");
  510.     delete col1;
  511.     delete col2;
  512.     delete col3;
  513.     delete col4;
  514.     delete col5;
  515.     delete col6;
  516. }
  517.  
  518. // test clone and copy
  519. void 
  520. CollationAPITest::TestDuplicate( char* par )
  521. {
  522.     UErrorCode status = U_ZERO_ERROR;
  523.     Collator *col1 = Collator::createInstance(status);
  524.     if (U_FAILURE(status)) {
  525.         logln("Default collator creation failed.");
  526.         return;
  527.     }
  528.     Collator *col2 = col1->clone();
  529.     doAssert((*col1 == *col2), "Cloned object is not equal to the orginal");
  530.     UnicodeString ruleset("< a, A < b, B < c, C < d, D, e, E");
  531.     RuleBasedCollator *col3 = new RuleBasedCollator(ruleset, status);
  532.     doAssert((*col1 != *col3), "Cloned object is equal to some dummy");
  533.     *col3 = *((RuleBasedCollator*)col1);
  534.     doAssert((*col1 == *col3), "Copied object is not equal to the orginal");
  535.     delete col1;
  536.     delete col2;
  537.     delete col3;
  538. }   
  539.  
  540. void
  541. CollationAPITest::TestCompare( char* par )
  542. {
  543.     logln("The compare tests begin : ");
  544.     Collator *col = 0;
  545.     UErrorCode success = U_ZERO_ERROR;
  546.     col = Collator::createInstance(success);
  547.     if (U_FAILURE(success)) {
  548.         errln("Default collation creation failed.");
  549.         return;
  550.     }
  551.     UnicodeString test1("Abcda"), test2("abcda");
  552.     logln("Use tertiary comparison level testing ....");
  553.                 
  554.     doAssert((!col->equals(test1, test2) ), "Result should be \"Abcda\" != \"abcda\"");
  555.     doAssert((col->greater(test1, test2) ), "Result should be \"Abcda\" >>> \"abcda\"");
  556.     doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" >>> \"abcda\"");    
  557.  
  558.     col->setStrength(Collator::SECONDARY);
  559.     logln("Use secondary comparison level testing ....");
  560.                 
  561.     doAssert((col->equals(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
  562.     doAssert((!col->greater(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
  563.     doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" == \"abcda\""); 
  564.  
  565.     col->setStrength(Collator::PRIMARY);
  566.     logln("Use primary comparison level testing ....");
  567.     
  568.     doAssert((col->equals(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
  569.     doAssert((!col->greater(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
  570.     doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" == \"abcda\""); 
  571.     logln("The compare tests end.");
  572.     delete col;
  573. }
  574.  
  575. void
  576. CollationAPITest::TestGetAll( char* par )
  577. {
  578.     int32_t count;
  579.     const Locale* list = Collator::getAvailableLocales(count);
  580.     for (int32_t i = 0; i < count; ++i) {
  581.         UnicodeString locName, dispName;
  582.         log("Locale name: "); 
  583.         log(list[i].getName(locName));
  584.         log(" , the display name is : ");
  585.         logln(list[i].getDisplayName(dispName));
  586.     }
  587. }
  588.  
  589.  
  590. void CollationAPITest::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  591. {
  592.     if (exec) logln("TestSuite CollationAPITest: ");
  593.     switch (index) {
  594.         case 0: name = "TestProperty";  if (exec)   TestProperty( par ); break;
  595.         case 1: name = "TestOperators"; if (exec)   TestOperators( par ); break;
  596.         case 2: name = "TestDuplicate"; if (exec)   TestDuplicate( par ); break;
  597.         case 3: name = "TestCompare";   if (exec)   TestCompare( par ); break;
  598.         case 4: name = "TestHashCode";  if (exec)   TestHashCode( par ); break;
  599.         case 5: name = "TestCollationKey";  if (exec)   TestCollationKey( par ); break;
  600.         case 6: name = "TestElemIter";  if (exec)   TestElemIter( par ); break;
  601.         case 7: name = "TestGetAll";    if (exec)   TestGetAll( par ); break;
  602.         default: name = ""; break;
  603.     }
  604. }
  605.  
  606.