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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // 21.3.5.4 basic_string::insert
25 #include <testsuite_hooks.h>
30 typedef std::string::size_type csize_type;
31 typedef std::string::iterator citerator;
32 csize_type npos = std::string::npos;
33 csize_type csz01, csz02;
35 const std::string str01("rodeo beach, marin");
36 const std::string str02("baker beach, san francisco");
39 // string& insert(size_type p1, const string& str, size_type p2, size_type n)
42 // 2) p2 <= str.size()
43 // 3) rlen = min(n, str.size() - p2)
45 // 1) out_of_range if p1 > size() || p2 > str.size()
46 // 2) length_error if size() >= npos - rlen
48 // replaces *this with new string of length size() + rlen such that
49 // nstr[0] to nstr[p1] == thisstr[0] to thisstr[p1]
50 // nstr[p1 + 1] to nstr[p1 + rlen] == str[p2] to str[p2 + rlen]
51 // nstr[p1 + 1 + rlen] to nstr[...] == thisstr[p1 + 1] to thisstr[...]
56 str03.insert(csz01 + 1, str02, 0, 5);
59 catch(std::out_of_range& fail) {
70 str03.insert(0, str02, csz02 + 1, 5);
73 catch(std::out_of_range& fail) {
80 csz01 = str01.max_size();
82 std::string str04(csz01, 'b');
86 str03.insert(0, str02, 0, 5);
89 catch(std::length_error& fail) {
96 catch(std::bad_alloc& failure){
99 catch(std::exception& failure){
104 csz01 = str03.size();
105 csz02 = str02.size();
106 str03.insert(13, str02, 0, 12);
107 VERIFY( str03 == "rodeo beach, baker beach,marin" );
110 csz01 = str03.size();
111 csz02 = str02.size();
112 str03.insert(0, str02, 0, 12);
113 VERIFY( str03 == "baker beach,rodeo beach, marin" );
116 csz01 = str03.size();
117 csz02 = str02.size();
118 str03.insert(csz01, str02, 0, csz02);
119 VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" );
121 // string& insert(size_type __p, const string& string);
122 // insert(p1, str, 0, npos)
124 csz01 = str03.size();
125 csz02 = str02.size();
126 str03.insert(csz01, str02);
127 VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" );
130 csz01 = str03.size();
131 csz02 = str02.size();
132 str03.insert(0, str02);
133 VERIFY( str03 == "baker beach, san franciscorodeo beach, marin" );
135 // string& insert(size_type __p, const char* s, size_type n);
136 // insert(p1, string(s,n))
138 csz01 = str03.size();
139 str03.insert(0, "-break at the bridge", 20);
140 VERIFY( str03 == "-break at the bridgebaker beach, san francisco" );
142 // string& insert(size_type __p, const char* s);
143 // insert(p1, string(s))
145 str03.insert(0, "-break at the bridge");
146 VERIFY( str03 == "-break at the bridgebaker beach, san francisco" );
148 // string& insert(size_type __p, size_type n, char c)
149 // insert(p1, string(n,c))
151 csz01 = str03.size();
152 str03.insert(csz01, 5, 'z');
153 VERIFY( str03 == "baker beach, san franciscozzzzz" );
155 // iterator insert(iterator p, char c)
156 // inserts a copy of c before the character referred to by p
158 citerator cit01 = str03.begin();
159 str03.insert(cit01, 'u');
160 VERIFY( str03 == "ubaker beach, san francisco" );
162 // iterator insert(iterator p, size_type n, char c)
163 // inserts n copies of c before the character referred to by p
165 cit01 = str03.begin();
166 str03.insert(cit01, 5, 'u');
167 VERIFY( str03 == "uuuuubaker beach, san francisco" );
171 // insert(iterator p, inputit first, inputit, last)
172 // ISO-14882: defect #7 part 1 clarifies this member function to be:
173 // insert(p - begin(), string(first,last))
175 csz01 = str03.size();
176 str03.insert(str03.begin(), str01.begin(), str01.end());
177 VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" );
180 csz01 = str03.size();
181 str03.insert(str03.end(), str01.begin(), str01.end());
182 VERIFY( str03 == "baker beach, san franciscorodeo beach, marin" );
191 // string& insert(size_type __p, const char* s, size_type n);
192 // string& insert(size_type __p, const char* s);
193 // but now s points inside the _Rep
199 const char* title = "Everything was beautiful, and nothing hurt";
200 // Increasing size: str01 is reallocated every time.
202 str01.insert(0, str01.c_str() + str01.size() - 4, 4);
203 VERIFY( str01 == "hurtEverything was beautiful, and nothing hurt" );
205 str01.insert(0, str01.c_str(), 5);
206 VERIFY( str01 == "EveryEverything was beautiful, and nothing hurt" );
208 str01.insert(10, str01.c_str() + 4, 6);
209 VERIFY( str01 == "Everythingything was beautiful, and nothing hurt" );
211 str01.insert(15, str01.c_str(), 10);
212 VERIFY( str01 == "Everything was Everythingbeautiful, and nothing hurt" );
214 str01.insert(15, str01.c_str() + 11, 13);
215 VERIFY( str01 == "Everything was was beautifulbeautiful, and nothing hurt" );
217 str01.insert(0, str01.c_str());
218 VERIFY( str01 == "Everything was beautiful, and nothing hurt"
219 "Everything was beautiful, and nothing hurt");
220 // Again: no reallocations.
222 str01.insert(0, str01.c_str() + str01.size() - 4, 4);
223 VERIFY( str01 == "hurtEverything was beautiful, and nothing hurt" );
225 str01.insert(0, str01.c_str(), 5);
226 VERIFY( str01 == "EveryEverything was beautiful, and nothing hurt" );
228 str01.insert(10, str01.c_str() + 4, 6);
229 VERIFY( str01 == "Everythingything was beautiful, and nothing hurt" );
231 str01.insert(15, str01.c_str(), 10);
232 VERIFY( str01 == "Everything was Everythingbeautiful, and nothing hurt" );
234 str01.insert(15, str01.c_str() + 11, 13);
235 VERIFY( str01 == "Everything was was beautifulbeautiful, and nothing hurt" );
237 str01.insert(0, str01.c_str());
238 VERIFY( str01 == "Everything was beautiful, and nothing hurt"
239 "Everything was beautiful, and nothing hurt");
249 __gnu_cxx_test::set_memory_limits();