1 // 1999-06-08 bkoz
2 
3 // Copyright (C) 1999 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20 
21 // 21.3 template class basic_string
22 
23 #include <string>
24 #include <stdexcept>
25 #include <testsuite_hooks.h>
26 
27 // Do a quick sanity check on known problems with element access and
28 // ref-counted strings. These should all pass, regardless of the
29 // underlying string implementation, of course.
test01(void)30 bool test01(void)
31 {
32   bool test = true;
33   typedef std::string::size_type csize_type;
34   typedef std::string::iterator siterator;
35   typedef std::string::reverse_iterator sriterator;
36   csize_type npos = std::string::npos;
37   csize_type csz01, csz02;
38   siterator it1;
39   sriterator rit1;
40 
41   std::string str01("montara beach, half moon bay");
42   const std::string str02("ocean beach, san francisco");
43   std::string str03;
44 
45   // 21.3 p 5
46 
47   // References, pointers, and iterators referring to the elements of
48   // a basic_string may be invalidated by the following uses of that
49   // basic_string object:
50 
51   // ...
52 
53   // Susequent to any of the above uses except the forms of insert()
54   // and erase() which return iterators, the first call to non-const
55   // member functions operator[](), at(), begin(), rbegin(), end(), or
56   // rend()
57 
58   str03 = str01;
59   it1 = str01.begin();
60   *it1 = 'x';
61   VERIFY( str01[0] == 'x' );
62   VERIFY( str03[0] == 'm' );
63 
64   str03 = str01;
65   csz01 = str01.size();
66   rit1 = str01.rbegin(); // NB: Pointing at one-past the end, so ...
67   *rit1 = 'z'; 		 // ... but it's taken care of here
68   VERIFY( str01[csz01 - 1] == 'z' );
69   VERIFY( str03[csz01 - 1] == 'y' );
70 
71   str03 = str01;
72   csz01 = str01.size();
73   std::string::reference r1 = str01.at(csz01 - 2);
74   VERIFY( str03 == str01 );
75   r1 = 'd';
76   VERIFY( str01[csz01 - 2] == 'd' );
77   VERIFY( str03[csz01 - 2] == 'a' );
78 
79   str03 = str01;
80   csz01 = str01.size();
81   std::string::reference r2 = str01[csz01 - 3];
82   VERIFY( str03 == str01 );
83   r2 = 'w';
84   VERIFY( str01[csz01 - 3] == 'w' );
85   VERIFY( str03[csz01 - 3] == 'b' );
86 
87   str03 = str01;
88   csz02 = str01.size();
89   it1 = str01.end();
90   VERIFY( str03 == str01 );
91   --it1;
92   *it1 = 'q';
93   VERIFY( str01[csz02 - 1] == 'q' );
94   VERIFY( str03[csz02 - 1] == 'z' );
95 
96   str03 = str01;
97   rit1 = str01.rend();
98   VERIFY( str03 == str01 );
99   --rit1;
100   *rit1 = 'p';
101   VERIFY( str01[0] == 'p' );
102   VERIFY( str03[0] == 'x' );
103 
104   // need to also test for const begin/const end
105 #ifdef DEBUG_ASSERT
106   assert(test);
107 #endif
108   return test;
109 }
110 
111 // Do another sanity check, this time for member functions that return
112 // iterators, namely insert and erase.
test02(void)113 bool test02(void)
114 {
115   bool test = true;
116   typedef std::string::size_type csize_type;
117   typedef std::string::iterator siterator;
118   typedef std::string::reverse_iterator sriterator;
119   csize_type npos = std::string::npos;
120   csize_type csz01, csz02;
121   siterator it1;
122   sriterator rit1;
123 
124   const std::string str01("its beach, santa cruz");
125 
126   std::string str02 = str01;
127   std::string str05 = str02; // optional, so that begin below causes a mutate
128   std::string::iterator p = str02.insert(str02.begin(), ' ');
129   std::string str03 = str02;
130   VERIFY( str03 == str02 );
131   *p = '!';
132   VERIFY( *str03.c_str() == ' ' );
133   str03[0] = '@';
134   VERIFY( str02[0] == '!' );
135   VERIFY( *p == '!' );
136   VERIFY( str02 != str05 );
137   VERIFY( str02 != str03 );
138 
139   std::string str10 = str01;
140   std::string::iterator p2 = str10.insert(str10.begin(), 'a');
141   std::string str11 = str10;
142   *p2 = 'e';
143   VERIFY( str11 != str10 );
144 
145   std::string str06 = str01;
146   std::string str07 = str06; // optional, so that begin below causes a mutate
147   p = str06.erase(str06.begin());
148   std::string str08 = str06;
149   VERIFY( str08 == str06 );
150   *p = '!';
151   VERIFY( *str08.c_str() == 't' );
152   str08[0] = '@';
153   VERIFY( str06[0] == '!' );
154   VERIFY( *p == '!' );
155   VERIFY( str06 != str07 );
156   VERIFY( str06 != str08 );
157 
158   std::string str12 = str01;
159   p2 = str12.erase(str12.begin(), str12.begin() + str12.size() - 1);
160   std::string str13 = str12;
161   *p2 = 'e';
162   VERIFY( str12 != str13 );
163 
164 #ifdef DEBUG_ASSERT
165   assert(test);
166 #endif
167   return test;
168 }
169 
main()170 int main()
171 {
172   test01();
173   test02();
174   return 0;
175 }
176