3 // Copyright (C) 1999, 2003 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // 21.3.5.4 basic_string::insert
25 #include <testsuite_hooks.h>
29 bool test
__attribute__((unused
)) = true;
30 typedef std::string::size_type csize_type
;
31 typedef std::string::iterator citerator
;
32 csize_type csz01
, csz02
;
34 const std::string
str01("rodeo beach, marin");
35 const std::string
str02("baker beach, san francisco");
38 // string& insert(size_type p1, const string& str, size_type p2, size_type n)
41 // 2) p2 <= str.size()
42 // 3) rlen = min(n, str.size() - p2)
44 // 1) out_of_range if p1 > size() || p2 > str.size()
45 // 2) length_error if size() >= npos - rlen
47 // replaces *this with new string of length size() + rlen such that
48 // nstr[0] to nstr[p1] == thisstr[0] to thisstr[p1]
49 // nstr[p1 + 1] to nstr[p1 + rlen] == str[p2] to str[p2 + rlen]
50 // nstr[p1 + 1 + rlen] to nstr[...] == thisstr[p1 + 1] to thisstr[...]
55 str03
.insert(csz01
+ 1, str02
, 0, 5);
58 catch(std::out_of_range
& fail
) {
69 str03
.insert(0, str02
, csz02
+ 1, 5);
72 catch(std::out_of_range
& fail
) {
79 csz01
= str01
.max_size();
81 std::string
str04(csz01
, 'b');
85 str03
.insert(0, str02
, 0, 5);
88 catch(std::length_error
& fail
) {
95 catch(std::bad_alloc
& failure
){
98 catch(std::exception
& failure
){
103 csz01
= str03
.size();
104 csz02
= str02
.size();
105 str03
.insert(13, str02
, 0, 12);
106 VERIFY( str03
== "rodeo beach, baker beach,marin" );
109 csz01
= str03
.size();
110 csz02
= str02
.size();
111 str03
.insert(0, str02
, 0, 12);
112 VERIFY( str03
== "baker beach,rodeo beach, marin" );
115 csz01
= str03
.size();
116 csz02
= str02
.size();
117 str03
.insert(csz01
, str02
, 0, csz02
);
118 VERIFY( str03
== "rodeo beach, marinbaker beach, san francisco" );
120 // string& insert(size_type __p, const string& string);
121 // insert(p1, str, 0, npos)
123 csz01
= str03
.size();
124 csz02
= str02
.size();
125 str03
.insert(csz01
, str02
);
126 VERIFY( str03
== "rodeo beach, marinbaker beach, san francisco" );
129 csz01
= str03
.size();
130 csz02
= str02
.size();
131 str03
.insert(0, str02
);
132 VERIFY( str03
== "baker beach, san franciscorodeo beach, marin" );
134 // string& insert(size_type __p, const char* s, size_type n);
135 // insert(p1, string(s,n))
137 csz01
= str03
.size();
138 str03
.insert(0, "-break at the bridge", 20);
139 VERIFY( str03
== "-break at the bridgebaker beach, san francisco" );
141 // string& insert(size_type __p, const char* s);
142 // insert(p1, string(s))
144 str03
.insert(0, "-break at the bridge");
145 VERIFY( str03
== "-break at the bridgebaker beach, san francisco" );
147 // string& insert(size_type __p, size_type n, char c)
148 // insert(p1, string(n,c))
150 csz01
= str03
.size();
151 str03
.insert(csz01
, 5, 'z');
152 VERIFY( str03
== "baker beach, san franciscozzzzz" );
154 // iterator insert(iterator p, char c)
155 // inserts a copy of c before the character referred to by p
157 citerator cit01
= str03
.begin();
158 str03
.insert(cit01
, 'u');
159 VERIFY( str03
== "ubaker beach, san francisco" );
161 // iterator insert(iterator p, size_type n, char c)
162 // inserts n copies of c before the character referred to by p
164 cit01
= str03
.begin();
165 str03
.insert(cit01
, 5, 'u');
166 VERIFY( str03
== "uuuuubaker beach, san francisco" );
170 // insert(iterator p, inputit first, inputit, last)
171 // ISO-14882: defect #7 part 1 clarifies this member function to be:
172 // insert(p - begin(), string(first,last))
174 csz01
= str03
.size();
175 str03
.insert(str03
.begin(), str01
.begin(), str01
.end());
176 VERIFY( str03
== "rodeo beach, marinbaker beach, san francisco" );
179 csz01
= str03
.size();
180 str03
.insert(str03
.end(), str01
.begin(), str01
.end());
181 VERIFY( str03
== "baker beach, san franciscorodeo beach, marin" );
187 __gnu_test::set_memory_limits();