00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00067
00068
00069
00070
00071
00072 }
00073
00074 int main()
00075 {
00076 char *str = "Hello, World!";
00077 const int len = strlen(str);
00078
00079
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
00093 cout << "Using FromChar = '" << t1 << Text(' ') << t1 << "'\n";
00094 (cout << "\n").flush();
00095
00096
00097 cout << "Text as (char *) = '" << t1.chars() << "'\n";
00098 cout << "Text as (const char *) = '" << t1.cchars() << "'\n";
00099 (cout << "\n").flush();
00100
00101
00102 cout << "Concatenated twice = '" << (t1 + t1) << "'\n";
00103 t2 += t1;
00104 cout << "Destructively concatenated twice = '" << t2 << "'\n";
00105 (cout << "\n").flush();
00106
00107
00108 cout << "Length = " << t1.Length() << "\n";
00109 (cout << "\n").flush();
00110
00111
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
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
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
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
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
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 }