3 // Copyright (C) 1999 Free Software Foundation, Inc.
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)
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.
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,
21 // 21.3 template class basic_string
25 #include <debug_assert.h>
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.
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
;
41 std::string
str01("montara beach, half moon bay");
42 const std::string
str02("ocean beach, san francisco");
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:
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
61 VERIFY( str01
[0] == 'x' );
62 VERIFY( str03
[0] == 'm' );
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' );
73 std::string::reference r1
= str01
.at(csz01
- 2);
74 VERIFY( str03
== str01
);
76 VERIFY( str01
[csz01
- 2] == 'd' );
77 VERIFY( str03
[csz01
- 2] == 'a' );
81 std::string::reference r2
= str01
[csz01
- 3];
82 VERIFY( str03
== str01
);
84 VERIFY( str01
[csz01
- 3] == 'w' );
85 VERIFY( str03
[csz01
- 3] == 'b' );
90 VERIFY( str03
== str01
);
93 VERIFY( str01
[csz02
- 1] == 'q' );
94 VERIFY( str03
[csz02
- 1] == 'z' );
98 VERIFY( str03
== str01
);
101 VERIFY( str01
[0] == 'p' );
102 VERIFY( str03
[0] == 'x' );
104 // need to also test for const begin/const end
111 // Do another sanity check, this time for member functions that return
112 // iterators, namely insert and erase.
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
;
124 const std::string
str01("its beach, santa cruz");
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
);
132 VERIFY( *str03
.c_str() == ' ' );
134 VERIFY( str02
[0] == '!' );
136 VERIFY( str02
!= str05
);
137 VERIFY( str02
!= str03
);
139 std::string str10
= str01
;
140 std::string::iterator p2
= str10
.insert(str10
.begin(), 'a');
141 std::string str11
= str10
;
143 VERIFY( str11
!= str10
);
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
);
151 VERIFY( *str08
.c_str() == 't' );
153 VERIFY( str06
[0] == '!' );
155 VERIFY( str06
!= str07
);
156 VERIFY( str06
!= str08
);
158 std::string str12
= str01
;
159 p2
= str12
.erase(str12
.begin(), str12
.begin() + str12
.size() - 1);
160 std::string str13
= str12
;
162 VERIFY( str12
!= str13
);