home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / intltest / ustrtest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-16  |  25.6 KB  |  655 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 "ustrtest.h"
  15. #include "unistr.h"
  16. #include "locid.h"
  17. #include <stdio.h>
  18.  
  19. UnicodeStringTest::UnicodeStringTest()
  20. {
  21. }
  22.  
  23. UnicodeStringTest::~UnicodeStringTest()
  24. {
  25. }
  26.  
  27. void UnicodeStringTest::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  28. {
  29.     if (exec) logln("TestSuite LocaleTest: ");
  30.     switch (index) {
  31.         case 0: name = "TestBasicManipulation"; if (exec) TestBasicManipulation(); break;
  32.         case 1: name = "TestCompare"; if (exec) TestCompare(); break;
  33.         case 2: name = "TestExtract"; if (exec) TestExtract(); break;
  34.         case 3: name = "TestRemoveReplace"; if (exec) TestRemoveReplace(); break;
  35.         case 4: name = "TestCaseConversion"; if (exec) TestCaseConversion(); break;
  36.         case 6: name = "TestSearching"; if (exec) TestSearching(); break;
  37.         case 7: name = "TestSpacePadding"; if (exec) TestSpacePadding(); break;
  38.         case 8: name = "TestPrefixAndSuffix"; if (exec) TestPrefixAndSuffix(); break;
  39.         case 9: name = "TestFindAndReplace"; if (exec) TestFindAndReplace(); break;
  40.         case 10: name = "TestCellWidth"; if (exec) TestCellWidth(); break;
  41.         case 11: name = "TestReverse"; if (exec) TestReverse(); break;
  42.         case 12: name = "TestMiscellaneous"; if (exec) TestMiscellaneous(); break;
  43.         case 13: name = "TestStackAllocation"; if (exec) TestStackAllocation(); break;
  44.  
  45.         default: name = ""; break; //needed to end loop
  46.     }
  47. }
  48.  
  49. void
  50. UnicodeStringTest::TestBasicManipulation()
  51. {
  52.     UnicodeString   test1("Now is the time for all men to come swiftly to the aid of the party.\n");
  53.     UnicodeString   expectedValue;
  54.  
  55.     test1.insert(24, "good ");
  56.     expectedValue = "Now is the time for all good men to come swiftly to the aid of the party.\n";
  57.     if (test1 != expectedValue)
  58.         errln("insert() failed:  expected \"" + expectedValue + "\"\n,got \"" + test1 + "\"");
  59.  
  60.     test1.remove(41, 8);
  61.     expectedValue = "Now is the time for all good men to come to the aid of the party.\n";
  62.     if (test1 != expectedValue)
  63.         errln("remove() failed:  expected \"" + expectedValue + "\"\n,got \"" + test1 + "\"");
  64.     
  65.     test1.replace(58, 6, "ir country");
  66.     expectedValue = "Now is the time for all good men to come to the aid of their country.\n";
  67.     if (test1 != expectedValue)
  68.         errln("replace() failed:  expected \"" + expectedValue + "\"\n,got \"" + test1 + "\"");
  69.     
  70.     UChar     temp[80];
  71.     test1.extract(0, 15, temp);
  72.     
  73.     UnicodeString       test2(temp, 15);
  74.     
  75.     expectedValue = "Now is the time";
  76.     if (test2 != expectedValue)
  77.         errln("extract() failed:  expected \"" + expectedValue + "\"\n,got \"" + test2 + "\"");
  78.     
  79.     test2 += " for me to go!\n";
  80.     expectedValue = "Now is the time for me to go!\n";
  81.     if (test2 != expectedValue)
  82.         errln("operator+=() failed:  expected \"" + expectedValue + "\"\n,got \"" + test2 + "\"");
  83.     
  84.     if (test1.size() != 70)
  85.         errln("size() failed: expected 70, got " + test1.size());
  86.     if (test2.size() != 30)
  87.         errln("size() failed: expected 30, got " + test2.size());
  88. }
  89.  
  90. void
  91. UnicodeStringTest::TestCompare()
  92. {
  93.     UnicodeString   test1("this is a test");
  94.     UnicodeString   test2("this is a test");
  95.     UnicodeString   test3("this is a test of the emergency broadcast system");
  96.     UnicodeString   test4("never say, \"this is a test\"!!");
  97.  
  98.     UChar         uniChars[] = { 't', 'h', 'i', 's', ' ', 'i', 's', 
  99.                  ' ', 'a', ' ', 't', 'e', 's', 't', 0 };
  100.     char            chars[] = "this is a test";
  101.  
  102.     // test operator== and operator!=
  103.     if (test1 != test2 || test1 == test3 || test1 == test4)
  104.         errln("operator== or operator!= failed");
  105.  
  106.     // test operator> and operator<
  107.     if (test1 > test2 || test1 < test2 || test1 > test3 || test1 < test4)
  108.         errln("operator> or operator< failed");
  109.  
  110.     // test operator>= and operator<=
  111.     if (!(test1 >= test2) || !(test1 <= test2) || !(test1 <= test3) || !(test1 >= test4))
  112.         errln("operator>= or operator<= failed");
  113.  
  114.     // test compare(UnicodeString)
  115.     if (test1.compare(test2) != 0 || test1.compare(test3) >= 0 || test1.compare(test4) <= 0)
  116.         errln("compare(UnicodeString) failed");
  117.  
  118.     //test compare(offset, length, UnicodeString)
  119.     if(test1.compare(0, 14, test2) != 0 ||
  120.         test3.compare(0, 14, test2) != 0 ||
  121.         test4.compare(12, 14, test2) != 0 ||
  122.         test3.compare(0, 18, test1) <=0  )
  123.         errln("compare(offset, length, UnicodeString) failes");
  124.  
  125.     // test compare(UChar*)
  126.     if (test2.compare(uniChars) != 0 || test3.compare(uniChars) <= 0 || test4.compare(uniChars) >= 0)
  127.         errln("compare(UChar*) failed");
  128.  
  129.     // test compare(char*)
  130.     if (test2.compare(chars) != 0 || test3.compare(chars) <= 0 || test4.compare(chars) >= 0)
  131.         errln("compare(char*) failed");
  132.  
  133.     // test compare(UChar*, length)
  134.     if (test1.compare(uniChars, 4) <= 0 || test1.compare(uniChars, 4) <= 0)
  135.         errln("compare(UChar*, length) failed");
  136.  
  137.     // test compare(thisOffset, thisLength, that, thatOffset, thatLength)
  138.     if (test1.compare(0, 14, test2, 0, 14) != 0 
  139.     || test1.compare(0, 14, test3, 0, 14) != 0
  140.     || test1.compare(0, 14, test4, 12, 14) != 0)
  141.         errln("1. compare(thisOffset, thisLength, that, thatOffset, thatLength) failed");
  142.  
  143.     if (test1.compare(10, 4, test2, 0, 4) >= 0 
  144.     || test1.compare(10, 4, test3, 22, 9) <= 0
  145.     || test1.compare(10, 4, test4, 22, 4) != 0)
  146.         errln("2. compare(thisOffset, thisLength, that, thatOffset, thatLength) failed");
  147.  
  148.     // test compareBetween
  149.     if (test1.compareBetween(0, 14, test2, 0, 14) != 0 || test1.compareBetween(0, 14, test3, 0, 14) != 0
  150.                     || test1.compareBetween(0, 14, test4, 12, 26) != 0)
  151.         errln("compareBetween failed");
  152.  
  153.     if (test1.compareBetween(10, 14, test2, 0, 4) >= 0 || test1.compareBetween(10, 14, test3, 22, 31) <= 0
  154.                     || test1.compareBetween(10, 14, test4, 22, 26) != 0)
  155.         errln("compareBetween failed");
  156. }
  157.  
  158. void
  159. UnicodeStringTest::TestExtract()
  160. {
  161.     UnicodeString   test1("Now is the time for all good men to come to the aid of their country.");
  162.     UnicodeString   test2;
  163.     UChar         test3[13];
  164.     char            test4[13];
  165.     UnicodeString   test5;
  166.  
  167.     test1.extract(11, 12, test2);
  168.     test1.extract(11, 12, test3);
  169.     test1.extract(11, 12, test4);
  170.     test1.extractBetween(11, 23, test5);
  171.  
  172.     for (UTextOffset i = 0; i < 12; i++) {
  173.         if (test1[(UTextOffset)(11 + i)] != test2[i]) {
  174.             errln(UnicodeString("extracting into a UnicodeString failed at position ") + i);
  175.             break;
  176.         }
  177.         if (test1[(UTextOffset)(11 + i)] != test3[i]) {
  178.             errln(UnicodeString("extracting into an array of UChar failed at position ") + i);
  179.             break;
  180.         }
  181.         if ((((char)test1[(UTextOffset)(11 + i)]) & 0xff) != test4[i]) {
  182.             errln(UnicodeString("extracting into an array of char failed at position ") + i);
  183.             break;
  184.         }
  185.         if (test1[(UTextOffset)(11 + i)] != test5[i]) {
  186.             errln(UnicodeString("extracting with extractBetween failed at position ") + i);
  187.             break;
  188.         }
  189.     }
  190. }
  191.  
  192. void
  193. UnicodeStringTest::TestRemoveReplace()
  194. {
  195.     UnicodeString   test1("The rain in Spain stays mainly on the plain");
  196.     UnicodeString   test2("eat SPAMburgers!");
  197.     UChar         test3[] = { 'S', 'P', 'A', 'M', 'M', 0 };
  198.     char            test4[] = "SPAM";
  199.     UnicodeString&  test5 = test1;
  200.  
  201.     test1.replace(4, 4, test2, 4, 4);
  202.     test1.replace(12, 5, test3, 4);
  203.     test3[4] = 0;
  204.     test1.replace(17, 4, test3);
  205.     test1.replace(23, 4, test4);
  206.     test1.replaceBetween(37, 42, test2, 4, 8);
  207.  
  208.     if (test1 != "The SPAM in SPAM SPAMs SPAMly on the SPAM")
  209.         errln("One of the replace methods failed:\n"
  210.               "  expected \"The SPAM in SPAM SPAMs SPAMly on the SPAM\",\n"
  211.               "  got \"" + test1 + "\"");
  212.  
  213.     test1.remove(21, 1);
  214.     test1.removeBetween(26, 28);
  215.  
  216.     if (test1 != "The SPAM in SPAM SPAM SPAM on the SPAM")
  217.         errln("One of the remove methods failed:\n"
  218.               "  expected \"The SPAM in SPAM SPAM SPAM on the SPAM\",\n"
  219.               "  got \"" + test1 + "\"");
  220.  
  221.     for (UTextOffset i = 0; i < test1.size(); i++)
  222.         if (test5[i] != 'S' && test5[i] != 'P' && test5[i] != 'A' && test5[i] != 'M' && test5[i] != ' ')
  223.             test1[i] = 'x';
  224.  
  225.     if (test1 != "xxx SPAM xx SPAM SPAM SPAM xx xxx SPAM")
  226.         errln("One of the remove methods failed:\n"
  227.               "  expected \"xxx SPAM xx SPAM SPAM SPAM xx xxx SPAM\",\n"
  228.               "  got \"" + test1 + "\"");
  229.  
  230.     test1.remove();
  231.     if (test1.size() != 0)
  232.         errln("Remove() failed: expected empty string, got \"" + test1 + "\"");
  233. }
  234.  
  235. void
  236. UnicodeStringTest::TestCaseConversion()
  237. {
  238.     UChar uppercaseGreek[] =
  239.         { 0x399, 0x395, 0x3a3, 0x3a5, 0x3a3, ' ', 0x03a7, 0x3a1, 0x399, 0x3a3, 0x3a4,
  240.         0x39f, 0x3a3, 0 };
  241.         // "IESUS CHRISTOS"
  242.  
  243.     UChar lowercaseGreek[] = 
  244.         { 0x3b9, 0x3b5, 0x3c3, 0x3c5, 0x3c2, ' ', 0x03c7, 0x3c1, 0x3b9, 0x3c3, 0x3c4,
  245.         0x3bf, 0x3c2, 0 };
  246.         // "iesus christos"
  247.  
  248.     UChar lowercaseTurkish[] = 
  249.         { 'i', 's', 't', 'a', 'n', 'b', 'u', 'l', ',', ' ', 'n', 'o', 't', ' ', 'c', 'o', 
  250.         'n', 's', 't', 'a', 'n', 't', 0x0131, 'n', 'o', 'p', 'l', 'e', '!', 0 };
  251.  
  252.     UChar uppercaseTurkish[] = 
  253.         { 'T', 'O', 'P', 'K', 'A', 'P', 'I', ' ', 'P', 'A', 'L', 'A', 'C', 'E', ',', ' ',
  254.         0x0130, 'S', 'T', 'A', 'N', 'B', 'U', 'L', 0 };
  255.     
  256.     UnicodeString expectedResult;
  257.     UnicodeString   test3;
  258.  
  259.     test3 += 0x0130;
  260.     test3 += "STANBUL, NOT CONSTANTINOPLE!";
  261.  
  262.     UnicodeString   test4(test3);
  263.     test4.toLower();
  264.     expectedResult = "istanbul, not constantinople!";
  265.     if (test4 != expectedResult)
  266.         errln("1. toLower failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
  267.  
  268.     test4 = test3;
  269.     test4.toLower(Locale("tr", "TR"));
  270.     expectedResult = lowercaseTurkish;
  271.     if (test4 != expectedResult)
  272.         errln("2. toLower failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
  273.  
  274.     test3 = "topkap";
  275.     test3 += 0x0131;
  276.     test3 += " palace, istanbul";
  277.     test4 = test3;
  278.  
  279.     test4.toUpper();
  280.     expectedResult = "TOPKAPI PALACE, ISTANBUL";
  281.     if (test4 != expectedResult)
  282.         errln("toUpper failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
  283.  
  284.     test4 = test3;
  285.     test4.toUpper(Locale("tr", "TR"));
  286.     expectedResult = uppercaseTurkish;
  287.     if (test4 != expectedResult)
  288.         errln("toUpper failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
  289.  
  290.     test3 = "Süßmayrstraße";
  291.  
  292.     test3.toUpper(Locale("de", "DE"));
  293.     expectedResult = "SÜSSMAYRSTRASSE";
  294.     if (test3 != expectedResult)
  295.         errln("toUpper failed: expected \"" + expectedResult + "\", got \"" + test3 + "\".");
  296.     
  297.     test4.replace(0, test4.size(), uppercaseGreek);
  298.  
  299.     test4.toLower(Locale("el", "GR"));
  300.     expectedResult = lowercaseGreek;
  301.     if (test4 != expectedResult)
  302.         errln("toLower failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
  303.     
  304.     test4.replace(0, test4.size(), lowercaseGreek);
  305.  
  306.     test4.toUpper();
  307.     expectedResult = uppercaseGreek;
  308.     if (test4 != expectedResult)
  309.         errln("toUpper failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
  310. }
  311.  
  312. void
  313. UnicodeStringTest::TestSearching()
  314. {
  315.     UnicodeString test1("test test ttest tetest testesteststt");
  316.     UnicodeString test2("test");
  317.     UChar testChar = 't';
  318.  
  319.     uint16_t occurrences = 0;
  320.     UTextOffset startPos = 0;
  321.     for ( ;
  322.           startPos != -1 && startPos < test1.size();
  323.           (startPos = test1.indexOf(test2, startPos)) != -1 ? (++occurrences, startPos += 4) : 0)
  324.         ;
  325.     if (occurrences != 6)
  326.         errln("indexOf failed: expected to find 6 occurrences, found " + occurrences);
  327.  
  328.     for ( occurrences = 0, startPos = 10;
  329.           startPos != -1 && startPos < test1.size();
  330.           (startPos = test1.indexOf(test2, startPos)) != -1 ? (++occurrences, startPos += 4) : 0)
  331.         ;
  332.     if (occurrences != 4)
  333.         errln("indexOf with starting offset failed: expected to find 4 occurrences, found " + occurrences);
  334.  
  335.     UTextOffset endPos = 28;
  336.     for ( occurrences = 0, startPos = 5;
  337.           startPos != -1 && startPos < test1.size();
  338.           (startPos = test1.indexOf(test2, startPos, endPos - startPos)) != -1 ? (++occurrences, startPos += 4) : 0)
  339.         ;
  340.     if (occurrences != 4)
  341.         errln("indexOf with starting and ending offsets failed: expected to find 4 occurrences, found " + occurrences);
  342.  
  343.     for ( occurrences = 0, startPos = 0;
  344.           startPos != -1 && startPos < test1.size();
  345.           (startPos = test1.indexOf(testChar, startPos)) != -1 ? (++occurrences, startPos += 1) : 0)
  346.         ;
  347.     if (occurrences != 16)
  348.         errln("indexOf with character failed: expected to find 16 occurrences, found " + occurrences);
  349.  
  350.     for ( occurrences = 0, startPos = 10;
  351.           startPos != -1 && startPos < test1.size();
  352.           (startPos = test1.indexOf(testChar, startPos)) != -1 ? (++occurrences, startPos += 1) : 0)
  353.         ;
  354.     if (occurrences != 12)
  355.         errln("indexOf with character & start offset failed: expected to find 12 occurrences, found " + occurrences);
  356.  
  357.     for ( occurrences = 0, startPos = 5, endPos = 28;
  358.           startPos != -1 && startPos < test1.size();
  359.           (startPos = test1.indexOf(testChar, startPos, endPos - startPos)) != -1 ? (++occurrences, startPos += 1) : 0)
  360.         ;
  361.     if (occurrences != 10)
  362.         errln("indexOf with character & start & end offsets failed: expected to find 10 occurrences, found " + occurrences);
  363.  
  364.     for ( occurrences = 0, startPos = 9999;
  365.           startPos != -1;
  366.           (startPos = test1.lastIndexOf(test2, startPos)) != -1 ? ++occurrences : 0)
  367.         ;
  368.     if (occurrences != 6)
  369.         errln("lastIndexOf failed: expected to find 6 occurrences, found " + occurrences);
  370.  
  371.     for ( occurrences = 0, startPos = 32;
  372.           startPos != -1;
  373.           (startPos = test1.lastIndexOf(test2, startPos)) != -1 ? ++occurrences : 0)
  374.         ;
  375.     if (occurrences != 5)
  376.         errln("lastIndexOf with starting offset failed: expected to find 5 occurrences, found " + occurrences);
  377.  
  378.     for ( occurrences = 0, startPos = 32, endPos = 5;
  379.           startPos != -1;
  380.           (startPos = test1.lastIndexOf(test2, startPos, startPos - endPos)) != -1 ? ++occurrences : 0)
  381.         ;
  382.     if (occurrences != 4)
  383.         errln("lastIndexOf with starting and ending offsets failed: expected to find 4 occurrences, found " + occurrences);
  384.  
  385.     for ( occurrences = 0, startPos = 9999;
  386.           startPos != -1;
  387.           (startPos = test1.lastIndexOf(testChar, startPos)) != -1 ? ++occurrences : 0)
  388.         ;
  389.     if (occurrences != 16)
  390.         errln("lastIndexOf with character failed: expected to find 16 occurrences, found " + occurrences);
  391.  
  392.     for ( occurrences = 0, startPos = 32;
  393.           startPos != -1 && startPos < test1.size();
  394.           (startPos = test1.lastIndexOf(testChar, startPos)) != -1 ? ++occurrences : 0)
  395.         ;
  396.     if (occurrences != 13)
  397.         errln("lastIndexOf with character & start offset failed: expected to find 13 occurrences, found " + occurrences);
  398.  
  399.     for ( occurrences = 0, startPos = 32, endPos = 5;
  400.           startPos != -1 && startPos < test1.size();
  401.           (startPos = test1.lastIndexOf(testChar, startPos, startPos - endPos)) != -1 ? ++occurrences : 0)
  402.         ;
  403.     if (occurrences != 11)
  404.         errln("lastIndexOf with character & start & end offsets failed: expected to find 11 occurrences, found " + occurrences);
  405. }
  406.  
  407. void
  408. UnicodeStringTest::TestSpacePadding()
  409. {
  410.     UnicodeString test1("hello");
  411.     UnicodeString test2("   there");
  412.     UnicodeString test3("Hi!  How ya doin'?  Beautiful day, isn't it?");
  413.     UnicodeString test4;
  414.     bool_t returnVal;
  415.     UnicodeString expectedValue;
  416.  
  417.     returnVal = test1.padLeading(15);
  418.     expectedValue = "          hello";
  419.     if (returnVal == FALSE || test1 != expectedValue)
  420.         errln("padLeading() failed: expected \"" + expectedValue + "\", got \"" + test1 + "\".");
  421.  
  422.     returnVal = test2.padTrailing(15);
  423.     expectedValue = "   there       ";
  424.     if (returnVal == FALSE || test2 != expectedValue)
  425.         errln("padTrailing() failed: expected \"" + expectedValue + "\", got \"" + test2 + "\".");
  426.  
  427.     expectedValue = test3;
  428.     returnVal = test3.padTrailing(15);
  429.     if (returnVal == TRUE || test3 != expectedValue)
  430.         errln("padTrailing() failed: expected \"" + expectedValue + "\", got \"" + test3 + "\".");
  431.  
  432.     expectedValue = "hello";
  433.     test4.setTo(test1).trim();
  434.  
  435.     if (test4 != expectedValue || test1 == expectedValue || test4 != expectedValue)
  436.         errln("trim(UnicodeString&) failed");
  437.     
  438.     test1.trim();
  439.     if (test1 != expectedValue)
  440.         errln("trim() failed: expected \"" + expectedValue + "\", got \"" + test1 + "\".");
  441.  
  442.     test2.trim();
  443.     expectedValue = "there";
  444.     if (test2 != expectedValue)
  445.         errln("trim() failed: expected \"" + expectedValue + "\", got \"" + test2 + "\".");
  446.  
  447.     test3.trim();
  448.     expectedValue = "Hi!  How ya doin'?  Beautiful day, isn't it?";
  449.     if (test3 != expectedValue)
  450.         errln("trim() failed: expected \"" + expectedValue + "\", got \"" + test3 + "\".");
  451.  
  452.     returnVal = test1.truncate(15);
  453.     expectedValue = "hello";
  454.     if (returnVal == TRUE || test1 != expectedValue)
  455.         errln("truncate() failed: expected \"" + expectedValue + "\", got \"" + test1 + "\".");
  456.  
  457.     returnVal = test2.truncate(15);
  458.     expectedValue = "there";
  459.     if (returnVal == TRUE || test2 != expectedValue)
  460.         errln("truncate() failed: expected \"" + expectedValue + "\", got \"" + test2 + "\".");
  461.  
  462.     returnVal = test3.truncate(15);
  463.     expectedValue = "Hi!  How ya doi";
  464.     if (returnVal == FALSE || test3 != expectedValue)
  465.         errln("truncate() failed: expected \"" + expectedValue + "\", got \"" + test3 + "\".");
  466. }
  467.  
  468. void
  469. UnicodeStringTest::TestPrefixAndSuffix()
  470. {
  471.     UnicodeString test1("Now is the time for all good men to come to the aid of their country.");
  472.     UnicodeString test2("Now");
  473.     UnicodeString test3("country.");
  474.     UnicodeString test4("count");
  475.  
  476.     if (!test1.startsWith(test2))
  477.         errln("startsWith() failed: \"" + test2 + "\" should be a prefix of \"" + test1 + "\".");
  478.  
  479.     if (test1.startsWith(test3))
  480.         errln("startsWith() failed: \"" + test3 + "\" shouldn't be a prefix of \"" + test1 + "\".");
  481.  
  482.     if (test1.endsWith(test2))
  483.         errln("endsWith() failed: \"" + test2 + "\" shouldn't be a suffix of \"" + test1 + "\".");
  484.  
  485.     if (!test1.endsWith(test3))
  486.         errln("endsWith() failed: \"" + test3 + "\" should be a suffix of \"" + test1 + "\".");
  487.  
  488.     if (!test3.startsWith(test4))
  489.         errln("startsWith() failed: \"" + test4 + "\" should be a prefix of \"" + test3 + "\".");
  490.  
  491.     if (test4.startsWith(test3))
  492.         errln("startsWith() failed: \"" + test3 + "\" shouldn't be a prefix of \"" + test4 + "\".");
  493. }
  494.  
  495. void
  496. UnicodeStringTest::TestFindAndReplace()
  497. {
  498.     UnicodeString test1("One potato, two potato, three potato, four\n");
  499.     UnicodeString test2("potato");
  500.     UnicodeString test3("MISSISSIPPI");
  501.  
  502.     UnicodeString expectedValue;
  503.  
  504.     test1.findAndReplace(test2, test3);
  505.     expectedValue = "One MISSISSIPPI, two MISSISSIPPI, three MISSISSIPPI, four\n";
  506.     if (test1 != expectedValue)
  507.         errln("findAndReplace failed: expected \"" + expectedValue + "\", got \"" + test1 + "\".");
  508.     test1.findAndReplace(test3, test2, 2, 32);
  509.     expectedValue = "One potato, two potato, three MISSISSIPPI, four\n";
  510.     if (test1 != expectedValue)
  511.         errln("findAndReplace failed: expected \"" + expectedValue + "\", got \"" + test1 + "\".");
  512. }
  513.  
  514. void
  515. UnicodeStringTest::TestCellWidth()
  516. {
  517.     UChar     testData2[] = { 'M', 'o', 0x308, 't', 'l', 'e', 'y', ' ', 'C', 'r', 'u', 0x308, 'e', 0x0000 };
  518.     UChar     testData3[] = { '1', '9', '9', '7', 0x5e74, ' ', 0x516d, 0x6708, ' ', '0', '3', 0x65e5, 0x5e73, 0x6210, 0x0000 };
  519.     UChar     testData4[] = { '9', '7', 0xb144, '6', 0xc6d4, '0', '3', 0xc77c, 0x0000 };
  520.     UChar     testData5[] = { '9', '7', 0x1103, 0x1167, 0x11ab, '6', 0x110b, 0x117b, 0x11af, '0', '3', 0x110b, 0x1175, 0x11af, 0x0000 };
  521.     
  522.     UnicodeString   test1("The rain in Spain stays mainly on the plain.");
  523.     UnicodeString   test2(testData2);
  524.     UnicodeString   test3(testData3);
  525.     UnicodeString   test4(testData4);
  526.     UnicodeString   test5(testData5);
  527.  
  528.     if (test1.numDisplayCells() != 44)
  529.         errln("numDisplayCells() failed: expected 44, got " + test1.numDisplayCells());
  530.     if (test2.numDisplayCells() != 11)
  531.         errln("numDisplayCells() failed: expected 11, got " + test2.numDisplayCells());
  532.     if (test3.numDisplayCells() != 20)
  533.         errln("numDisplayCells() failed: expected 20, got " + test3.numDisplayCells());
  534.     if (test4.numDisplayCells() != 11)
  535.         errln("numDisplayCells() failed: expected 11, got " + test4.numDisplayCells());
  536.     if (test5.numDisplayCells() != 11)
  537.         errln("numDisplayCells() failed: expected 11, got " + test5.numDisplayCells());
  538. }
  539.  
  540. void
  541. UnicodeStringTest::TestReverse()
  542. {
  543.     UnicodeString test("backwards words say to used I");
  544.  
  545.     test.reverse();
  546.     test.reverse(2, 6);
  547.     test.reverse(7, 9);
  548.     test.reverse(10, 13);
  549.     test.reverse(14, 19);
  550.     test.reverse(20, 29);
  551.  
  552.     if (test != "I used to say words backwards")
  553.         errln("reverse() failed:  Expected \"I used to say words backwards\",\n got \""
  554.             + test + "\"");
  555. }
  556.  
  557. void
  558. UnicodeStringTest::TestMiscellaneous()
  559. {
  560.     UnicodeString   test1("This is a test");
  561.     UnicodeString   test2("This is a test");
  562.     UnicodeString   test3("Me too!");
  563.     const UChar*  test4;
  564.  
  565.     if (test1.isBogus() || test2.isBogus() || test3.isBogus())
  566.         errln("A string returned true for isBogus()!");
  567.  
  568.     if (test1.hashCode() != test2.hashCode() || test1.hashCode() == test3.hashCode())
  569.         errln("hashCode() failed");
  570.  
  571.     test4 = test1.getUChars();
  572.  
  573.     if (test1 != test2)
  574.         errln("getUChars() affected the string!");
  575.  
  576.     UTextOffset i;
  577.     for (i = 0; i < test2.size(); i++)
  578.         if (test2[i] != test4[i])
  579.             errln(UnicodeString("getUChars() failed: strings differ at position ") + i);
  580.  
  581.     test4 = test1.orphanStorage();
  582.  
  583.     if (test1.size() != 0)
  584.         errln("orphanStorage() failed: orphaned string's contents is " + test1);
  585.  
  586.     for (i = 0; i < test2.size(); i++)
  587.         if (test2[i] != test4[i])
  588.             errln(UnicodeString("orphanStorage() failed: strings differ at position ") + i);
  589.  
  590.     delete (UChar*)test4;
  591. }
  592.  
  593. void
  594. UnicodeStringTest::TestStackAllocation()
  595. {
  596.      UChar            testString[] ={ 
  597.         'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'c', 'r', 'a', 'z', 'y', ' ', 't', 'e', 's', 't','.'};
  598.     UChar           guardWord = 0x4DED;
  599.     UnicodeString*  test = 0;
  600.  
  601.     test = new  UnicodeString(testString);
  602.     if (*test != "This is a crazy test.")
  603.         errln("Test string failed to initialize properly.");
  604.     if (guardWord != 0x04DED)
  605.         errln("Test string initialization overwrote guard word!");
  606.  
  607.     test->insert(8, "only ");
  608.     test->remove(15, 6);
  609.     if (*test != "This is only a test.")
  610.         errln("Manipulation of test string failed to work right.");
  611.     if (guardWord != 0x4DED)
  612.         errln("Manipulation of test string overwrote guard word!");
  613.  
  614.     // we have to deinitialize and release the backing store by calling the destructor
  615.     // explicitly, since we can't overload operator delete
  616.     test->~UnicodeString();
  617.  
  618.     UChar workingBuffer[] = {
  619.         'N', 'o', 'w', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 't', 'i', 'm', 'e', ' ',
  620.         'f', 'o', 'r', ' ', 'a', 'l', 'l', ' ', 'm', 'e', 'n', ' ', 't', 'o', ' ',
  621.         'c', 'o', 'm', 'e', 0xffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  622.     UChar guardWord2 = 0x4DED;
  623.  
  624.     test = new UnicodeString(workingBuffer, 35, 50);
  625.     if (*test != "Now is the time for all men to come")
  626.         errln("Stack-allocated backing store failed to initialize correctly.");
  627.     if (guardWord2 != 0x4DED)
  628.         errln("Stack-allocated backing store overwrote guard word!");
  629.  
  630.     test->insert(24, "good ");
  631.     if (*test != "Now is the time for all good men to come")
  632.         errln("insert() on stack-allocated UnicodeString didn't work right");
  633.     if (guardWord2 != 0x4DED)
  634.         errln("insert() on stack-allocated UnicodeString overwrote guard word!");
  635.     if (workingBuffer[24] != 'g')
  636.         errln("insert() on stack-allocated UnicodeString didn't affect backing store");
  637.  
  638.     *test += " to the aid of their country.";
  639.     if (*test != "Now is the time for all good men to come to the aid of their country.")
  640.         errln("Stack-allocated UnicodeString overflow didn't work");
  641.     if (guardWord2 != 0x4DED)
  642.         errln("Stack-allocated UnicodeString overflow overwrote guard word!");
  643.  
  644.     *test = "ha!";
  645.     if (*test != "ha!")
  646.         errln("Assignment to stack-allocated UnicodeString didn't work");
  647.     if (workingBuffer[0] != 'N')
  648.         errln("Change to UnicodeString after overflow are stil affecting original buffer");
  649.     if (guardWord2 != 0x4DED)
  650.         errln("Change to UnicodeString after overflow overwrote guard word!");
  651. #ifdef _WIN32
  652.     test->~UnicodeString();
  653. #endif
  654. }
  655.