Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

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 // Last modified on Sun Jan  8 20:25:59 EST 2006 by ken@xorian.net        
00020 //      modified on Tue Jun 18 13:58:39 EDT 2002 by kcschalk@shr.intel.com
00021 //      modified on Mon Dec 23 13:54:59 PST 1996 by heydon 
00022 //      modified on Thu Jun 27 18:54:14 PDT 1996 by mann  
00023 //      modified on Fri May 26 10:54:33 PDT 1995 by levin 
00024 
00025 // TestText -- tests the Text interface and implementation
00026 
00027 #include "Basics.H"
00028 #include "Text.H"
00029 
00030 using std::cout;
00031 using std::cerr;
00032 using std::endl;
00033 
00034 void TestHash(const Text& t)
00035 {
00036   char hash_buff[17];
00037   sprintf(hash_buff, "%016" FORMAT_LENGTH_INT_64 "x", t.Hash());
00038   cout << hash_buff << " = Hash(\"" << t << "\")\n";
00039 }
00040 
00041 void IndexChar(const Text& t, int i)
00042 {
00043   cout << "T[" << i << "] = '" << t[i] << "'\n";
00044 }
00045 
00046 Text TestPad(const Text& t, const Text &expected, const Text &expected2)
00047 {
00048   Text padded = t.PadLeft(10).PadRight(20,"-");
00049   if(padded != expected)
00050     {
00051       cerr << "Padding test failed:" << endl
00052            << "  expected = \"" << expected << "\"" << endl
00053            << "  actual   = \"" << padded << "\"" << endl;
00054       exit(1);
00055     }
00056 
00057   Text padded2 = t.PadLeft(10, "/|\\|").PadRight(20,"+-=");
00058   if(padded2 != expected2)
00059     {
00060       cerr << "Padding test failed:" << endl
00061            << "  expected = \"" << expected2 << "\"" << endl
00062            << "  actual   = \"" << padded2 << "\"" << endl;
00063       exit(1);
00064     }
00065 
00066   // Useful if changing the tests:
00067   /*
00068   cout << "TestPad(\"" << t << "\"," << endl
00069        << "        \"" << padded << "\"," << endl
00070        << "        \"" << padded2 << "\");" << endl;
00071   */
00072 }
00073 
00074 int main()
00075 {
00076     char *str = "Hello, World!";
00077     const int len = strlen(str);
00078 
00079     // Text(char *), Text(Text &)
00080     Text t1(str);
00081     assert(t1 == str);
00082     assert(strcmp(t1.cchars(), str) == 0);
00083     cout << "String copy of '" << str << "' = '" << t1 << "'\n";
00084     Text t2(t1);
00085     assert(t1 == str);
00086     assert(t1 == t2);
00087     assert(strcmp(t1.cchars(), str) == 0);
00088     assert(strcmp(t1.cchars(), t2.cchars()) == 0);
00089     cout << "Text copy of '" << str << "' = '" << t2 << "'\n";
00090     (cout << "\n").flush();
00091 
00092     // Text(char)
00093     cout << "Using FromChar = '" << t1 << Text(' ') << t1 << "'\n";
00094     (cout << "\n").flush();
00095 
00096     // explicit conversion to (char *)
00097     cout << "Text as (char *) = '" << t1.chars() << "'\n";
00098     cout << "Text as (const char *) = '" << t1.cchars() << "'\n";
00099     (cout << "\n").flush();
00100 
00101     // Text::Concat
00102     cout << "Concatenated twice = '" << (t1 + t1) << "'\n";
00103     t2 += t1;
00104     cout << "Destructively concatenated twice = '" << t2 << "'\n";
00105     (cout << "\n").flush();
00106 
00107     // Text::Length
00108     cout << "Length = " << t1.Length() << "\n";
00109     (cout << "\n").flush();
00110 
00111     // Text::Sub
00112     cout << "Sub(0) = '" << t1.Sub(0) << "'\n";
00113     cout << "Sub(1) = '" << t1.Sub(1) << "'\n";
00114     cout << "Sub(2) = '" << t1.Sub(2) << "'\n";
00115     cout << "Sub(7) = '" << t1.Sub(7) << "'\n";
00116     cout << "Sub(" << len-1 << ") = '" << t1.Sub(len-1) << "'\n";
00117     cout << "Sub(" << len << ") = '" << t1.Sub(len) << "'\n";
00118     cout << "Sub(7, 0) = '" << t1.Sub(7, 0) << "'\n";
00119     cout << "Sub(7, 1) = '" << t1.Sub(7, 1) << "'\n";
00120     cout << "Sub(7, 2) = '" << t1.Sub(7, 2) << "'\n";
00121     (cout << "\n").flush();
00122 
00123     // Text::FindChar
00124     cout << "FindChar('o') = " << t1.FindChar('o') << "\n";
00125     cout << "FindChar('o', 0) = " << t1.FindChar('o', 0) << "\n";
00126     cout << "FindChar('o', 4) = " << t1.FindChar('o', 4) << "\n";
00127     cout << "FindChar('o', 5) = " << t1.FindChar('o', 5) << "\n";
00128     cout << "FindChar('o', 8) = " << t1.FindChar('o', 8) << "\n";
00129     cout << "FindChar('o', 9) = " << t1.FindChar('o', 9) << "\n";
00130     cout << "FindChar('o', 13) = " << t1.FindChar('o', 13) << "\n";
00131     cout << "FindChar('o', 20) = " << t1.FindChar('o', 20) << "\n";
00132     (cout << "\n").flush();
00133 
00134     // Text::FindCharR
00135     cout << "FindCharR('o') = " << t1.FindCharR('o') << "\n";
00136     cout << "FindCharR('o', 12) = " << t1.FindCharR('o', 12) << "\n";
00137     cout << "FindCharR('o', 8) = " << t1.FindCharR('o', 8) << "\n";
00138     cout << "FindCharR('o', 7) = " << t1.FindCharR('o', 7) << "\n";
00139     cout << "FindCharR('o', 4) = " << t1.FindCharR('o', 4) << "\n";
00140     cout << "FindCharR('o', 3) = " << t1.FindCharR('o', 3) << "\n";
00141     cout << "FindCharR('o', -1) = " << t1.FindCharR('o', -1) << "\n";
00142     cout << "FindCharR('o', -5) = " << t1.FindCharR('o', -5) << "\n";
00143     (cout << "\n").flush();
00144 
00145     // Print a message about endian differences, and test Text::WordWrap.
00146     cout << Text("Note: the Hash values will be different between "
00147                  "big-endian and little-endian systems.  However "
00148                  "they should be the same on all systems of the "
00149                  "same byte order.").WordWrap() << endl << endl;
00150 
00151     // Text::Hash
00152     TestHash("defghabcdefghabcdefgh");
00153     TestHash("abcdefghabcdefghabcdefgh");
00154     TestHash("defg");
00155     TestHash("abcdefg");
00156     TestHash("a");
00157     TestHash("ab");
00158     TestHash("abc");
00159     TestHash("abcdefgh");
00160     TestHash("abcdefghabcdefgh");
00161     TestHash("abcdefghi");
00162     (cout << "\n").flush();
00163 
00164     // t[i]
00165     cout << "T = \"" << t1 << "\"\n";
00166     IndexChar(t1, 0);
00167     IndexChar(t1, 1);
00168     IndexChar(t1, t1.Length()-1);
00169     (cout << "\n").flush();
00170 
00171     TestPad("a",
00172             "         a----------",
00173             "/|\\|/|\\|/a+-=+-=+-=+");
00174     TestPad("ab",
00175             "        ab----------",
00176             "/|\\|/|\\|ab+-=+-=+-=+");
00177     TestPad("abc",
00178             "       abc----------",
00179             "/|\\|/|\\abc+-=+-=+-=+");
00180     TestPad("abcdefgh",
00181             "  abcdefgh----------",
00182             "/|abcdefgh+-=+-=+-=+");
00183     TestPad("abcdefghij",
00184             "abcdefghij----------",
00185             "abcdefghij+-=+-=+-=+");
00186     TestPad("abcdefghijkl",
00187             "abcdefghijkl--------",
00188             "abcdefghijkl+-=+-=+-");
00189     TestPad("abcdefghijklmn",
00190             "abcdefghijklmn------",
00191             "abcdefghijklmn+-=+-=");
00192     TestPad("abcdefghijklmnop",
00193             "abcdefghijklmnop----",
00194             "abcdefghijklmnop+-=+");
00195     TestPad("abcdefghijklmnopqr",
00196             "abcdefghijklmnopqr--",
00197             "abcdefghijklmnopqr+-");
00198     TestPad("abcdefghijklmnopqrst",
00199             "abcdefghijklmnopqrst",
00200             "abcdefghijklmnopqrst");
00201     TestPad("abcdefghijklmnopqrstu",
00202             "abcdefghijklmnopqrstu",
00203             "abcdefghijklmnopqrstu");
00204     TestPad("abcdefghijklmnopqrstuvwx",
00205             "abcdefghijklmnopqrstuvwx",
00206             "abcdefghijklmnopqrstuvwx");
00207 
00208     return 0;
00209 }

Generated on Mon May 8 00:48:29 2006 for Vesta by  doxygen 1.4.2