home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / intltest / citrtest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-16  |  7.2 KB  |  194 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. #include "citrtest.h"
  15. #include "schriter.h"
  16.  
  17. CharIterTest::CharIterTest()
  18. {
  19. }
  20.  
  21. CharIterTest::~CharIterTest()
  22. {
  23. }
  24.  
  25. void CharIterTest::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  26. {
  27.     if (exec) logln("TestSuite LocaleTest: ");
  28.     switch (index) {
  29.         case 0: name = "TestConstructionAndEquality"; if (exec) TestConstructionAndEquality(); break;
  30.         case 1: name = "TestIteration"; if (exec) TestIteration(); break;
  31.  
  32.         default: name = ""; break; //needed to end loop
  33.     }
  34. }
  35.  
  36. void CharIterTest::TestConstructionAndEquality() {
  37.     UnicodeString  testText("Now is the time for all good men to come to the aid of their country.");
  38.     UnicodeString  testText2("Don't bother using this string.");
  39.  
  40.     CharacterIterator* test1 = new StringCharacterIterator(testText);
  41.     CharacterIterator* test2 = new StringCharacterIterator(testText, 5);
  42.     CharacterIterator* test3 = new StringCharacterIterator(testText, 2, 20, 5);
  43.     CharacterIterator* test4 = new StringCharacterIterator(testText2);
  44.     CharacterIterator* test5 = test1->clone();
  45.  
  46.     if (*test1 == *test2 || *test1 == *test3 || *test1 == *test4)
  47.         errln("Construction or operator== failed: Unequal objects compared equal");
  48.     if (*test1 != *test5)
  49.         errln("clone() or equals() failed: Two clones tested unequal");
  50.  
  51.     if (test1->hashCode() == test2->hashCode() || test1->hashCode() == test3->hashCode()
  52.                     || test1->hashCode() == test4->hashCode())
  53.         errln("hashCode() failed:  different objects have same hash code");
  54.  
  55.     if (test1->hashCode() != test5->hashCode())
  56.         errln("hashCode() failed:  identical objects have different hash codes");
  57.  
  58.     test1->setIndex(5);
  59.     if (*test1 != *test2 || *test1 == *test5)
  60.         errln("setIndex() failed");
  61.  
  62.     *((StringCharacterIterator*)test1) = *((StringCharacterIterator*)test3);
  63.     if (*test1 != *test3 || *test1 == *test5)
  64.         errln("operator= failed");
  65.  
  66.     delete test1;
  67.     delete test2;
  68.     delete test3;
  69.     delete test4;
  70.     delete test5;
  71. }
  72.  
  73. void CharIterTest::TestIteration() {
  74.     UnicodeString text("Now is the time for all good men to come to the aid of their country.");
  75.  
  76.     UChar c;
  77.     UTextOffset i;
  78.     {
  79.         StringCharacterIterator   iter(text, 5);
  80.  
  81.         UnicodeString iterText;
  82.         iter.getText(iterText);
  83.         if (iterText != text)
  84.           errln("iter.getText() failed");
  85.  
  86.         if (iter.current() != text[(UTextOffset)5])
  87.             errln("Iterator didn't start out in the right place.");
  88.  
  89.         c = iter.first();
  90.         i = 0;
  91.  
  92.         if (iter.startIndex() != 0 || iter.endIndex() != text.size())
  93.             errln("startIndex() or endIndex() failed");
  94.  
  95.         logln("Testing forward iteration...");
  96.         do {
  97.             if (c == CharacterIterator::DONE && i != text.size())
  98.                 errln("Iterator reached end prematurely");
  99.             else if (c != text[i])
  100.                 errln((UnicodeString)"Character mismatch at position " + i +
  101.                                     ", iterator has " + UCharToUnicodeString(c) +
  102.                                     ", string has " + UCharToUnicodeString(text[i]));
  103.  
  104.             if (iter.current() != c)
  105.                 errln("current() isn't working right");
  106.             if (iter.getIndex() != i)
  107.                 errln("getIndex() isn't working right");
  108.  
  109.             if (c != CharacterIterator::DONE) {
  110.                 c = iter.next();
  111.                 i++;
  112.             }
  113.         } while (c != CharacterIterator::DONE);
  114.  
  115.         c = iter.last();
  116.         i = text.size() - 1;
  117.  
  118.         logln("Testing backward iteration...");
  119.         do {
  120.             if (c == CharacterIterator::DONE && i >= 0)
  121.                 errln("Iterator reached end prematurely");
  122.             else if (c != text[i])
  123.                 errln((UnicodeString)"Character mismatch at position " + i +
  124.                                     ", iterator has " + UCharToUnicodeString(c) +
  125.                                     ", string has " + UCharToUnicodeString(text[i]));
  126.  
  127.             if (iter.current() != c)
  128.                 errln("current() isn't working right");
  129.             if (iter.getIndex() != i)
  130.                 errln("getIndex() isn't working right");
  131.  
  132.             if (c != CharacterIterator::DONE) {
  133.                 c = iter.previous();
  134.                 i--;
  135.             }
  136.         } while (c != CharacterIterator::DONE);
  137.     }
  138.  
  139.     {
  140.         StringCharacterIterator iter(text, 5, 15, 10);
  141.         if (iter.startIndex() != 5 || iter.endIndex() != 15)
  142.             errln("creation of a restricted-range iterator failed");
  143.  
  144.         if (iter.getIndex() != 10 || iter.current() != text[(UTextOffset)10])
  145.             errln("starting the iterator in the middle didn't work");
  146.  
  147.         c = iter.first();
  148.         i = 5;
  149.  
  150.         logln("Testing forward iteration over a range...");
  151.         do {
  152.             if (c == CharacterIterator::DONE && i != 15)
  153.                 errln("Iterator reached end prematurely");
  154.             else if (c != text[i])
  155.                 errln((UnicodeString)"Character mismatch at position " + i +
  156.                                     ", iterator has " + UCharToUnicodeString(c) +
  157.                                     ", string has " + UCharToUnicodeString(text[i]));
  158.  
  159.             if (iter.current() != c)
  160.                 errln("current() isn't working right");
  161.             if (iter.getIndex() != i)
  162.                 errln("getIndex() isn't working right");
  163.  
  164.             if (c != CharacterIterator::DONE) {
  165.                 c = iter.next();
  166.                 i++;
  167.             }
  168.         } while (c != CharacterIterator::DONE);
  169.  
  170.         c = iter.last();
  171.         i = 14;
  172.  
  173.         logln("Testing backward iteration over a range...");
  174.         do {
  175.             if (c == CharacterIterator::DONE && i >= 5)
  176.                 errln("Iterator reached end prematurely");
  177.             else if (c != text[i])
  178.                 errln((UnicodeString)"Character mismatch at position " + i +
  179.                                     ", iterator has " + UCharToUnicodeString(c) +
  180.                                     ", string has " + UCharToUnicodeString(text[i]));
  181.  
  182.             if (iter.current() != c)
  183.                 errln("current() isn't working right");
  184.             if (iter.getIndex() != i)
  185.                 errln("getIndex() isn't working right");
  186.  
  187.             if (c != CharacterIterator::DONE) {
  188.                 c = iter.previous();
  189.                 i--;
  190.             }
  191.         } while (c != CharacterIterator::DONE);
  192.     }
  193. }
  194.