/vesta/vestasys.org/basics/basics/36/src/TestText.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 
00003 // This file is part of Vesta.
00004 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // TestText -- tests the Text interface and implementation
00020 
00021 #include "Basics.H"
00022 #include "Text.H"
00023 
00024 using std::cout;
00025 using std::cerr;
00026 using std::endl;
00027 
00028 void TestHash(const Text& t)
00029 {
00030   char hash_buff[17];
00031   sprintf(hash_buff, "%016" FORMAT_LENGTH_INT_64 "x", t.Hash());
00032   cout << hash_buff << " = Hash(\"" << t << "\")\n";
00033 }
00034 
00035 void IndexChar(const Text& t, int i)
00036 {
00037   cout << "T[" << i << "] = '" << t[i] << "'\n";
00038 }
00039 
00040 void TestPad(const Text& t, const Text &expected, const Text &expected2)
00041 {
00042   Text padded = t.PadLeft(10).PadRight(20,"-");
00043   if(padded != expected)
00044     {
00045       cerr << "Padding test failed:" << endl
00046            << "  expected = \"" << expected << "\"" << endl
00047            << "  actual   = \"" << padded << "\"" << endl;
00048       exit(1);
00049     }
00050 
00051   Text padded2 = t.PadLeft(10, "/|\\|").PadRight(20,"+-=");
00052   if(padded2 != expected2)
00053     {
00054       cerr << "Padding test failed:" << endl
00055            << "  expected = \"" << expected2 << "\"" << endl
00056            << "  actual   = \"" << padded2 << "\"" << endl;
00057       exit(1);
00058     }
00059 
00060   // Useful if changing the tests:
00061   /*
00062   cout << "TestPad(\"" << t << "\"," << endl
00063        << "        \"" << padded << "\"," << endl
00064        << "        \"" << padded2 << "\");" << endl;
00065   */
00066 }
00067 
00068 int main()
00069 {
00070     char *str = "Hello, World!";
00071     const int len = strlen(str);
00072 
00073     // Text(char *), Text(Text &)
00074     Text t1(str);
00075     assert(t1 == str);
00076     assert(strcmp(t1.cchars(), str) == 0);
00077     cout << "String copy of '" << str << "' = '" << t1 << "'\n";
00078     Text t2(t1);
00079     assert(t1 == str);
00080     assert(t1 == t2);
00081     assert(strcmp(t1.cchars(), str) == 0);
00082     assert(strcmp(t1.cchars(), t2.cchars()) == 0);
00083     cout << "Text copy of '" << str << "' = '" << t2 << "'\n";
00084     (cout << "\n").flush();
00085 
00086     // Text(char)
00087     cout << "Using FromChar = '" << t1 << Text(' ') << t1 << "'\n";
00088     (cout << "\n").flush();
00089 
00090     // explicit conversion to (char *)
00091     cout << "Text as (char *) = '" << t1.chars() << "'\n";
00092     cout << "Text as (const char *) = '" << t1.cchars() << "'\n";
00093     (cout << "\n").flush();
00094 
00095     // Text::Concat
00096     cout << "Concatenated twice = '" << (t1 + t1) << "'\n";
00097     t2 += t1;
00098     cout << "Destructively concatenated twice = '" << t2 << "'\n";
00099     (cout << "\n").flush();
00100 
00101     // Text::Length
00102     cout << "Length = " << t1.Length() << "\n";
00103     (cout << "\n").flush();
00104 
00105     // Text::Sub
00106     cout << "Sub(0) = '" << t1.Sub(0) << "'\n";
00107     cout << "Sub(1) = '" << t1.Sub(1) << "'\n";
00108     cout << "Sub(2) = '" << t1.Sub(2) << "'\n";
00109     cout << "Sub(7) = '" << t1.Sub(7) << "'\n";
00110     cout << "Sub(" << len-1 << ") = '" << t1.Sub(len-1) << "'\n";
00111     cout << "Sub(" << len << ") = '" << t1.Sub(len) << "'\n";
00112     cout << "Sub(7, 0) = '" << t1.Sub(7, 0) << "'\n";
00113     cout << "Sub(7, 1) = '" << t1.Sub(7, 1) << "'\n";
00114     cout << "Sub(7, 2) = '" << t1.Sub(7, 2) << "'\n";
00115     (cout << "\n").flush();
00116 
00117     // Text::FindChar
00118     cout << "FindChar('o') = " << t1.FindChar('o') << "\n";
00119     cout << "FindChar('o', 0) = " << t1.FindChar('o', 0) << "\n";
00120     cout << "FindChar('o', 4) = " << t1.FindChar('o', 4) << "\n";
00121     cout << "FindChar('o', 5) = " << t1.FindChar('o', 5) << "\n";
00122     cout << "FindChar('o', 8) = " << t1.FindChar('o', 8) << "\n";
00123     cout << "FindChar('o', 9) = " << t1.FindChar('o', 9) << "\n";
00124     cout << "FindChar('o', 13) = " << t1.FindChar('o', 13) << "\n";
00125     cout << "FindChar('o', 20) = " << t1.FindChar('o', 20) << "\n";
00126     (cout << "\n").flush();
00127 
00128     // Text::FindCharR
00129     cout << "FindCharR('o') = " << t1.FindCharR('o') << "\n";
00130     cout << "FindCharR('o', 12) = " << t1.FindCharR('o', 12) << "\n";
00131     cout << "FindCharR('o', 8) = " << t1.FindCharR('o', 8) << "\n";
00132     cout << "FindCharR('o', 7) = " << t1.FindCharR('o', 7) << "\n";
00133     cout << "FindCharR('o', 4) = " << t1.FindCharR('o', 4) << "\n";
00134     cout << "FindCharR('o', 3) = " << t1.FindCharR('o', 3) << "\n";
00135     cout << "FindCharR('o', -1) = " << t1.FindCharR('o', -1) << "\n";
00136     cout << "FindCharR('o', -5) = " << t1.FindCharR('o', -5) << "\n";
00137     (cout << "\n").flush();
00138 
00139     // Print a message about endian differences, and test Text::WordWrap.
00140     cout << Text("Note: the Hash values will be different between "
00141                  "big-endian and little-endian systems.  However "
00142                  "they should be the same on all systems of the "
00143                  "same byte order.").WordWrap() << endl << endl;
00144 
00145     // Text::Hash
00146     TestHash("defghabcdefghabcdefgh");
00147     TestHash("abcdefghabcdefghabcdefgh");
00148     TestHash("defg");
00149     TestHash("abcdefg");
00150     TestHash("a");
00151     TestHash("ab");
00152     TestHash("abc");
00153     TestHash("abcdefgh");
00154     TestHash("abcdefghabcdefgh");
00155     TestHash("abcdefghi");
00156     (cout << "\n").flush();
00157 
00158     // t[i]
00159     cout << "T = \"" << t1 << "\"\n";
00160     IndexChar(t1, 0);
00161     IndexChar(t1, 1);
00162     IndexChar(t1, t1.Length()-1);
00163     (cout << "\n").flush();
00164 
00165     TestPad("a",
00166             "         a----------",
00167             "/|\\|/|\\|/a+-=+-=+-=+");
00168     TestPad("ab",
00169             "        ab----------",
00170             "/|\\|/|\\|ab+-=+-=+-=+");
00171     TestPad("abc",
00172             "       abc----------",
00173             "/|\\|/|\\abc+-=+-=+-=+");
00174     TestPad("abcdefgh",
00175             "  abcdefgh----------",
00176             "/|abcdefgh+-=+-=+-=+");
00177     TestPad("abcdefghij",
00178             "abcdefghij----------",
00179             "abcdefghij+-=+-=+-=+");
00180     TestPad("abcdefghijkl",
00181             "abcdefghijkl--------",
00182             "abcdefghijkl+-=+-=+-");
00183     TestPad("abcdefghijklmn",
00184             "abcdefghijklmn------",
00185             "abcdefghijklmn+-=+-=");
00186     TestPad("abcdefghijklmnop",
00187             "abcdefghijklmnop----",
00188             "abcdefghijklmnop+-=+");
00189     TestPad("abcdefghijklmnopqr",
00190             "abcdefghijklmnopqr--",
00191             "abcdefghijklmnopqr+-");
00192     TestPad("abcdefghijklmnopqrst",
00193             "abcdefghijklmnopqrst",
00194             "abcdefghijklmnopqrst");
00195     TestPad("abcdefghijklmnopqrstu",
00196             "abcdefghijklmnopqrstu",
00197             "abcdefghijklmnopqrstu");
00198     TestPad("abcdefghijklmnopqrstuvwx",
00199             "abcdefghijklmnopqrstuvwx",
00200             "abcdefghijklmnopqrstuvwx");
00201 
00202     return 0;
00203 }

Generated on Thu May 24 23:01:37 2007 for Vesta by  doxygen 1.5.1