home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-base.tgz / libg++-2.7.1-src.tar / fsf / libg++ / libstdc++ / tests / tstring.cc < prev    next >
C/C++ Source or Header  |  1995-06-16  |  4KB  |  190 lines

  1. // Tests for the -*- C++ -*- string classes.
  2. // Copyright (C) 1994 Free Software Foundation
  3.  
  4. // This file is part of the GNU ANSI C++ Library.  This library is free
  5. // software; you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software
  7. // Foundation; either version 2, or (at your option) any later version.
  8.  
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU General Public License for more details.
  13.  
  14. // You should have received a copy of the GNU General Public License
  15. // along with this library; see the file COPYING.  If not, write to the Free
  16. // Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  
  18. #include <std/string.h>
  19. #include <iostream.h>
  20. #include <std/cstdlib.h>
  21. #include <std/cassert.h>
  22.  
  23. string X = "Hello";
  24. string Y = "world";
  25. string N = "123";
  26. string c;
  27. char*  s = ",";
  28.  
  29. void decltest()
  30. {
  31.   string x;
  32.   cout << "an empty string:" << x << "\n";
  33.   assert(x == "");
  34.  
  35.   string y = "Hello";
  36.   cout << "A string initialized to Hello:" << y << "\n";
  37.   assert(y == "Hello");
  38.  
  39.   if (y[y.length()-1] == 'o')
  40.     y = y + '\n';
  41.   assert(y == "Hello\n");
  42.   y = "Hello";
  43.  
  44.   string a = y;
  45.   cout << "A string initialized to previous string:" << a << "\n";
  46.   assert(a == "Hello");
  47.   assert(a == y);
  48.  
  49.   string b (a, 1, 2);
  50.   cout << "A string initialized to (previous string, 1, 2):" << b << "\n";
  51.   assert(b == "el");
  52.  
  53.   char ch = '@';
  54.   string z (1, ch);
  55.   cout << "A string initialized to @:" << z << "\n";
  56.   assert (z == "@");
  57.  
  58.   string n ("20");
  59.   cout << "A string initialized to 20:" << n << "\n";
  60.   assert(n == "20");
  61.  
  62.   int i = atoi(n.c_str ());
  63.   double f = atof(n.c_str ());
  64.   cout << "n = " << n << " atoi(n) = " << i << " atof(n) = " << f << "\n";
  65.   assert(i == 20);
  66.   assert(f == 20);
  67.  
  68. }
  69.  
  70. void cattest()
  71. {
  72.   string x = X;
  73.   string y = Y;
  74.   string z = x + y;
  75.   cout << "z = x + y = " << z << "\n";
  76.   assert(z == "Helloworld");
  77.  
  78.   x += y;
  79.   cout << "x += y; x = " << x << "\n";
  80.   assert(x == "Helloworld");
  81.  
  82.   y = Y;
  83.   x = X;
  84.   y.insert (0, x);
  85.   cout << "y.insert (0, x); y = " << y << "\n";
  86.   assert(y == "Helloworld");
  87.  
  88.   y = Y;
  89.   x = X;
  90.   x = x + y + x;
  91.   cout << "x = x + y + x; x = " << x << "\n";
  92.   assert(x == "HelloworldHello");
  93.  
  94.   y = Y;
  95.   x = X;
  96.   x = y + x + x;
  97.   cout << "x = y + x + x; x = " << x << "\n";
  98.   assert(x == "worldHelloHello");
  99.  
  100.   x = X;
  101.   y = Y;
  102.   z = x + s + ' ' + y.substr (y.find ('w'), 1) + y.substr (y.find ('w') + 1) + ".";
  103.   cout << "z = x + s +  + y.substr (y.find (w), 1) + y.substr (y.find (w) + 1) + . = " << z << "\n";
  104.   assert(z == "Hello, world.");
  105. }
  106.  
  107. void comparetest()
  108. {  
  109.   string x = X;
  110.   string y = Y;
  111.   string n = N;
  112.   string z = x + y;
  113.  
  114.   assert(x != y);
  115.   assert(x == "Hello");
  116.   assert(x != z.substr (0, 4));
  117.   assert(x.compare (y) < 0);
  118.   assert(x.compare (z.substr (0, 6)) < 0);
  119.  
  120.   assert(x.find ("lo") == 3);
  121.   assert(x.find ("l", 2) == 2);
  122.   assert(x.rfind ("l") == 3);
  123. }
  124.  
  125. void substrtest()
  126. {
  127.   string x = X;
  128.  
  129.   char ch = x[0];
  130.   cout << "ch = x[0] = " << ch << "\n";
  131.   assert(ch == 'H');
  132.  
  133.   string z = x.substr (2, 3);
  134.   cout << "z = x.substr (2, 3) = " << z << "\n";
  135.   assert(z == "llo");
  136.  
  137.   x.replace (2, 2, "r");
  138.   cout << "x.replace (2, 2, r); x = " << x << "\n";
  139.   assert(x == "Hero");
  140.  
  141.   x = X;
  142.   x.replace (0, 1, 'j');
  143.   cout << "x.replace (0, 1, 'j'); x = " << x << "\n";
  144.   assert(x == "jello");
  145. }
  146.  
  147. void iotest()
  148. {
  149.   string z;
  150.   cout << "enter a word:";
  151.   cin >> z;
  152.   cout << "word =" << z << " ";
  153.   cout << "length = " << z.length() << "\n";
  154. }
  155.  
  156. void identitytest(string a, string b)
  157. {
  158.   string x = a;
  159.   string y = b;
  160.   x += b;
  161.   y.insert (0, a);
  162.   assert((a + b) == x);
  163.   assert((a + b) == y);
  164.   assert(x == y);
  165.   
  166.   assert((a + b + a) == (a + (b + a)));
  167.  
  168.   x.remove (x.rfind (b));
  169.   assert(x == a);
  170.  
  171.   y.replace (0, y.rfind (b), b);
  172.   assert(y == (b + b));
  173.   y.replace (y.find (b), b.length (), a);
  174.   assert(y == (a + b));
  175. }
  176.  
  177. int main()
  178. {
  179.   decltest();
  180.   cattest();
  181.   comparetest();
  182.   substrtest();
  183.   identitytest(X, X);
  184.   identitytest(X, Y);
  185.   identitytest(X+Y+N+X+Y+N, "A string that will be used in identitytest but is otherwise just another useless string.");
  186.   iotest();
  187.   cout << "\nEnd of test\n";
  188.   return 0;
  189. }
  190.