Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Test/StrTest2.cpp

Go to the documentation of this file.
00001 /*
00002  A test file for Strings
00003 */
00004 
00005 #include <j2k/Fred/String/String.cpp>
00006 
00007 typedef JString  String;
00008 
00009 // Nicely echo assertions because they contain quotes
00010 
00011 #define tassert(ex) \
00012 { \
00013   if (!(ex)) { \
00014     cerr << "Assertion failed at " << __FILE__ << __LINE__ << "\n"; \
00015     assert( (ex) ); \
00016     abort();   \
00017   } \
00018 }
00019 
00020   // Global
00021   String X = "Hello";
00022   String Y = "world";
00023   String N = "123";
00024   String c;
00025   char*  s = ",";
00026 
00027 void basic() {
00028 
00029  String* s1 = new String();
00030  String* s2 = new String("Hello");
00031  String  s3 = "";
00032  String  s4;
00033  s3 += "World";
00034 
00035  cout << "BEGIN\n[";
00036 
00037  cout << *s1;
00038  cout << *s2;
00039  cout << s3;
00040  cout << s4;
00041 
00042  // ...
00043 
00044 
00045  cout << "]\nEND\n";
00046 }
00047 
00048 
00049 void decltest()
00050 {
00051   String x;
00052   cout << "an empty String:" << x << "\n";
00053   tassert(x.OK());
00054   tassert(x == "");
00055 
00056   String y = "Hello";
00057   cout << "A string initialized to Hello:" << y << "\n";
00058   tassert(y.OK());
00059   tassert(y == "Hello");
00060 
00061   if (y[y.length()-1] == 'o')
00062    y = y + '\n';
00063   tassert(y == "Hello\n");
00064   y = "Hello";
00065 
00066   String a = y;
00067   cout << "A string initialized to previous string:" << a << "\n";
00068   tassert(a.OK());
00069   tassert(a == "Hello");
00070   tassert(a == y);
00071 
00072   String b (a.at(1, 2));
00073   cout << "A string initialized to previous string.at(1, 2):" << b << "\n";
00074   tassert(b.OK());
00075   tassert(b == "el");
00076 
00077   char ch = '@';
00078   String z(ch); 
00079   cout << "A string initialized to @:" << z << "\n";
00080   tassert(z.OK());
00081   tassert(z == "@");
00082 
00083   // XXX: `dec' is obsolete.  Since String.h includes iostream.h, and not
00084   //      stream.h, we include stream.h in this file for the time being.  This
00085   //      test will be rewritten to be done the "right" way, but for now, let's
00086   //      save some time and go the easy route.
00087   String n = "20";
00088   cout << "A string initialized to dec(20):" << n << "\n";
00089   tassert(n.OK());
00090   tassert(n == "20");
00091 
00092   int i = atoi(n);
00093   double f = atof(n);
00094   cout << "n = " << n << " atoi(n) = " << i << " atof(n) = " << f << "\n";
00095   tassert(i == 20);
00096   tassert(f == 20);
00097 
00098   tassert(X.OK());
00099   tassert(Y.OK());
00100   tassert(x.OK());
00101   tassert(y.OK());
00102   tassert(z.OK());
00103   tassert(n.OK());
00104 
00105 }
00106 
00107 void cattest()
00108 {
00109   String x = X;
00110   String y = Y;
00111   String z = x + y;
00112   cout << "z = x + y = " << z << "\n";
00113   tassert(x.OK());
00114   tassert(y.OK());
00115   tassert(z.OK());
00116   tassert(z == "Helloworld");
00117 
00118   x += y;
00119   cout << "x += y; x = " << x << "\n";
00120   tassert(x.OK());
00121   tassert(y.OK());
00122   tassert(x == "Helloworld");
00123 
00124   y = Y;
00125   x = X;
00126 /*
00127   y.prepend(x);
00128   cout << "y.prepend(x); y = " << y << "\n";
00129   tassert(y == "Helloworld");
00130 
00131   y = Y;
00132   x = X;
00133   cat(x, y, x, x);
00134   cout << "cat(x, y, x, x); x = " << x << "\n";
00135   tassert(x == "HelloworldHello");
00136 
00137   y = Y;
00138   x = X;
00139   cat(y, x, x, x);
00140   cout << "cat(y, x, x, x); x = " << x << "\n";
00141   tassert(x == "worldHelloHello");
00142 
00143   x = X;
00144   y = Y;
00145   z = x + s + ' ' + y.at("w") + y.after("w") + ".";
00146   cout << "z = x + s +  + y.at(w) + y.after(w) + . = " << z << "\n";
00147   tassert(z.OK());
00148   tassert(z == "Hello, world.");
00149 */
00150 }
00151 
00152 void comparetest()
00153 {  
00154   String x = X;
00155   String y = Y;
00156   String n = N;
00157   String z = x + y;
00158 
00159   tassert(x != y);
00160   tassert(x == "Hello");
00161 /*
00162   tassert(x != z.at(0, 4));
00163   tassert(x < y);
00164   tassert(!(x >= z.at(0, 6)));
00165   tassert(x.contains("He"));
00166   tassert(z.contains(x));
00167   tassert(x.contains(r));
00168 
00169   tassert(!(x.matches(r)));
00170   tassert(x.matches(RXalpha));
00171   tassert(!(n.matches(RXalpha)));
00172   tassert(n.matches(RXint));
00173   tassert(n.matches(RXdouble));
00174 
00175   tassert(x.index("lo") == 3);
00176   tassert(x.index("l", 2) == 2);
00177   tassert(x.index("l", -1) == 3);
00178   tassert(x.index(r)  == 1);
00179   tassert(x.index(r, -2) == 1);
00180 
00181   tassert(x.contains("el", 1));
00182   tassert(x.contains("el"));
00183 
00184   tassert(common_prefix(x, "Help") == "Hel");
00185   tassert(common_suffix(x, "to") == "o");
00186 
00187   tassert(fcompare(x, "hELlo") == 0);
00188   tassert(fcompare(x, "hElp") < 0);
00189 */
00190 }
00191 
00192 void substrtest()
00193 {
00194 /*
00195   String x = X;
00196 
00197   char ch = x[0];
00198   cout << "ch = x[0] = " << ch << "\n";
00199   tassert(ch == 'H');
00200 
00201   String z = x.at(2, 3);
00202   cout << "z = x.at(2, 3) = " << z << "\n";
00203   tassert(z.OK());
00204   tassert(z == "llo");
00205 
00206   x.at(2, 2) = "r";
00207   cout << "x.at(2, 2) = r; x = " << x << "\n";
00208   tassert(x.OK());
00209   tassert(x.at(2,2).OK());
00210   tassert(x == "Hero");
00211 
00212   x = X;
00213   x.at(0, 1) = "j";
00214   cout << "x.at(0, 1) = j; x = " << x << "\n";
00215   tassert(x.OK());
00216   tassert(x == "jello");
00217 
00218   x = X;
00219   x.at("He") = "je";
00220   cout << "x.at(He) = je; x = " << x << "\n";
00221   tassert(x.OK());
00222   tassert(x == "jello");
00223 
00224   x = X;
00225   x.at("l", -1) = "i";
00226   cout << "x.at(l, -1) = i; x = " << x << "\n";
00227   tassert(x.OK());
00228   tassert(x == "Helio");
00229   
00230   x = X;
00231   z = x.at(r);
00232   cout << "z = x.at(r) = " << z << "\n";
00233   tassert(z.OK());
00234   tassert(z == "ello");
00235 
00236   z = x.before("o");
00237   cout << "z = x.before(o) = " << z << "\n";
00238   tassert(z.OK());
00239   tassert(z == "Hell");
00240 
00241   x.before("ll") = "Bri";
00242   cout << "x.before(ll) = Bri; x = " << x << "\n";
00243   tassert(x.OK());
00244   tassert(x == "Brillo");
00245 
00246   x = X;
00247   z = x.before(2);
00248   cout << "z = x.before(2) = " << z << "\n";
00249   tassert(z.OK());
00250   tassert(z == "He");
00251 
00252   z = x.after("Hel");
00253   cout << "z = x.after(Hel) = " << z << "\n";
00254   tassert(z.OK());
00255   tassert(z == "lo");
00256 
00257   x.after("Hel") = "p";  
00258   cout << "x.after(Hel) = p; x = " << x << "\n";
00259   tassert(x.OK());
00260   tassert(x == "Help");
00261 
00262   x = X;
00263   z = x.after(3);
00264   cout << "z = x.after(3) = " << z << "\n";
00265   tassert(z.OK());
00266   tassert(z == "o");
00267 
00268   z = "  a bc";
00269   z  = z.after(RXwhite);
00270   cout << "z =   a bc; z = z.after(RXwhite); z =" << z << "\n";
00271   tassert(z.OK());
00272   tassert(z == "a bc");
00273 */
00274 }
00275 
00276 void utiltest()
00277 {
00278 /*
00279   String x = X;
00280   int matches = x.gsub("l", "ll");
00281   cout << "x.gsub(l, ll); x = " << x << "\n";
00282   tassert(x.OK());
00283   tassert(matches == 2);
00284   tassert(x == "Hellllo");
00285 
00286   x = X;
00287   tassert(x.OK());
00288   matches = x.gsub(r, "ello should have been replaced by this string");
00289   tassert(x.OK());
00290   cout << "x.gsub(r, ...); x = " << x << "\n";
00291   tassert(x.OK());
00292   tassert(matches == 1);
00293   tassert(x == "Hello should have been replaced by this string");
00294 
00295   matches = x.gsub(RXwhite, "#");
00296   cout << "x.gsub(RXwhite, #); x = " << x << "\n";
00297   tassert(matches == 7);
00298   tassert(x.OK());
00299 
00300   String z = X + Y;
00301   z.del("loworl");
00302   cout << "z = x+y; z.del(loworl); z = " << z << "\n";
00303   tassert(z.OK());
00304   tassert(z == "Held");
00305 
00306   x = X;
00307   z = reverse(x);
00308   cout << "reverse(x) = " << z << "\n";
00309   tassert(z.OK());
00310   tassert(z == "olleH");
00311 
00312   x.reverse();
00313   cout << "x.reverse() = " << x << "\n";
00314   tassert(x.OK());
00315   tassert(x == z);
00316 
00317   x = X;
00318   z = upcase(x);
00319   cout << "upcase(x) = " << z << "\n";
00320   tassert(z.OK());
00321   tassert(z == "HELLO");
00322 
00323   z = downcase(x);
00324   cout << "downcase(x) = " << z << "\n";
00325   tassert(z.OK());
00326   tassert(z == "hello");
00327 
00328   z = capitalize(x);
00329   cout << "capitalize(x) = " << z << "\n";
00330   tassert(z.OK());
00331   tassert(z == "Hello");
00332 
00333   // Let's see how apostrophe is handled. 
00334   z = "he asked:'this is nathan's book?'. 'no, it's not',i said.";
00335   cout << "capitalize(z) = " << capitalize (z) << "\n";
00336   
00337   z = replicate('*', 10);
00338   cout << "z = replicate(*, 10) = " << z << "\n";
00339   tassert(z.OK());
00340   tassert(z == "**********");
00341   tassert(z.length() == 10);
00342 */
00343 }
00344 
00345 
00346 
00347 void splittest()
00348 {
00349   String z = "This string\thas\nfive words";
00350   cout << "z = " << z << "\n";
00351 /*
00352   String w[10];
00353   int nw = split(z, w, 10, RXwhite);
00354   tassert(nw == 5);
00355   cout << "from split(z, RXwhite, w, 10), n words = " << nw << ":\n";
00356   for (int i = 0; i < nw; ++i)
00357   {
00358     tassert(w[i].OK());
00359     cout << w[i] << "\n";
00360   }
00361   tassert(w[0] == "This");
00362   tassert(w[1] == "string");
00363   tassert(w[2] == "has");
00364   tassert(w[3] == "five");
00365   tassert(w[4] == "words");
00366   tassert(w[5] == (char*)0);
00367 
00368   z = join(w, nw, "/");
00369   cout << "z = join(w, nw, /); z =" << z << "\n";
00370   tassert(z.OK());
00371   tassert(z == "This/string/has/five/words");
00372 */
00373 }
00374 
00375 
00376 void iotest()
00377 {
00378   String z;
00379   cout << "enter a word:";
00380 //  cin >> z;
00381   cout << "word =" << z << " ";
00382   cout << "length = " << z.length() << "\n";
00383 }
00384 
00385 void identitytest(String a, String b)
00386 {
00387   String x = a;
00388   String y = b;
00389   x += b;
00390 /*
00391   y.prepend(a);
00392 */
00393   tassert((a + b) == x);
00394   tassert((a + b) == y);
00395   tassert(x == y);
00396 /*
00397   tassert(x.after(a) == b);
00398   tassert(x.before(b, -1) == a);
00399   tassert(x.from(a) == x);
00400   tassert(x.through(b, -1) == x);
00401 
00402   tassert(x.at(a) == a);
00403   tassert(x.at(b) == b);
00404 
00405   tassert(reverse(x) == reverse(b) + reverse(a));
00406   
00407   tassert((a + b + a) == (a + (b + a)));
00408 
00409   x.del(b, -1);
00410   tassert(x == a);
00411 
00412   y.before(b, -1) = b;
00413   tassert(y == (b + b));
00414   y.at(b) = a;
00415   tassert(y == (a + b));
00416 
00417   x = a + reverse(a);
00418   for (int i = 0; i < 7; ++i)
00419   {
00420     y = x;
00421     x += x;
00422     tassert(x.OK());
00423     tassert(x == reverse(x));
00424     tassert(x.index(y) == 0);
00425     tassert(x.index(y, -1) == x.length() / 2);
00426   }
00427 */
00428 }
00429 
00430 void freqtest()
00431 {
00432 /*
00433   String x = "Hello World";
00434   const SubString& y = x.at(0,5);
00435   tassert(x.freq('l') == 3);   // char
00436   tassert(x.freq("lo") == 1);  // char*
00437   tassert(x.freq(x) == 1);  // String
00438   tassert(x.freq(y) == 1);  // SubString
00439 */
00440 }
00441 
00442 int main()
00443 {
00444   basic();
00445   decltest();
00446   cattest();
00447   comparetest();
00448   substrtest();
00449   utiltest();
00450   splittest();
00451   freqtest();
00452   identitytest(X, X);
00453   identitytest(X, Y);
00454   identitytest(X+Y+N+X+Y+N, "A string that will be used in identitytest but is otherwise just another useless string.");
00455   iotest();
00456   cout << "\nEnd of test\n";
00457 
00458   int a;
00459   cin >> a;
00460   return 0;
00461 }

Generated on Sun Oct 14 18:46:45 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001