]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string/modifiers/insert/char/1.cc
3 // Copyright (C) 1999-2016 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 3, 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 COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 // 21.3.5.4 basic_string::insert
24 #include <testsuite_hooks.h>
28 bool test
__attribute__((unused
)) = true;
29 typedef std::string::size_type csize_type
;
30 typedef std::string::iterator citerator
;
31 csize_type csz01
, csz02
;
33 const std::string
str01("rodeo beach, marin");
34 const std::string
str02("baker beach, san francisco");
37 // string& insert(size_type p1, const string& str, size_type p2, size_type n)
40 // 2) p2 <= str.size()
41 // 3) rlen = min(n, str.size() - p2)
43 // 1) out_of_range if p1 > size() || p2 > str.size()
44 // 2) length_error if size() >= npos - rlen
46 // replaces *this with new string of length size() + rlen such that
47 // nstr[0] to nstr[p1] == thisstr[0] to thisstr[p1]
48 // nstr[p1 + 1] to nstr[p1 + rlen] == str[p2] to str[p2 + rlen]
49 // nstr[p1 + 1 + rlen] to nstr[...] == thisstr[p1 + 1] to thisstr[...]
54 str03
.insert(csz01
+ 1, str02
, 0, 5);
57 catch(std::out_of_range
& fail
) {
68 str03
.insert(0, str02
, csz02
+ 1, 5);
71 catch(std::out_of_range
& fail
) {
78 csz01
= str01
.max_size();
80 std::string
str04(csz01
, 'b');
84 str03
.insert(0, str02
, 0, 5);
87 catch(std::length_error
& fail
) {
94 catch(std::bad_alloc
& failure
){
97 catch(std::exception
& failure
){
102 csz01
= str03
.size();
103 csz02
= str02
.size();
104 str03
.insert(13, str02
, 0, 12);
105 VERIFY( str03
== "rodeo beach, baker beach,marin" );
108 csz01
= str03
.size();
109 csz02
= str02
.size();
110 str03
.insert(0, str02
, 0, 12);
111 VERIFY( str03
== "baker beach,rodeo beach, marin" );
114 csz01
= str03
.size();
115 csz02
= str02
.size();
116 str03
.insert(csz01
, str02
, 0, csz02
);
117 VERIFY( str03
== "rodeo beach, marinbaker beach, san francisco" );
119 // string& insert(size_type __p, const string& string);
120 // insert(p1, str, 0, npos)
122 csz01
= str03
.size();
123 csz02
= str02
.size();
124 str03
.insert(csz01
, str02
);
125 VERIFY( str03
== "rodeo beach, marinbaker beach, san francisco" );
128 csz01
= str03
.size();
129 csz02
= str02
.size();
130 str03
.insert(0, str02
);
131 VERIFY( str03
== "baker beach, san franciscorodeo beach, marin" );
133 // string& insert(size_type __p, const char* s, size_type n);
134 // insert(p1, string(s,n))
136 csz01
= str03
.size();
137 str03
.insert(0, "-break at the bridge", 20);
138 VERIFY( str03
== "-break at the bridgebaker beach, san francisco" );
140 // string& insert(size_type __p, const char* s);
141 // insert(p1, string(s))
143 str03
.insert(0, "-break at the bridge");
144 VERIFY( str03
== "-break at the bridgebaker beach, san francisco" );
146 // string& insert(size_type __p, size_type n, char c)
147 // insert(p1, string(n,c))
149 csz01
= str03
.size();
150 str03
.insert(csz01
, 5, 'z');
151 VERIFY( str03
== "baker beach, san franciscozzzzz" );
153 // iterator insert(iterator p, char c)
154 // inserts a copy of c before the character referred to by p
156 citerator cit01
= str03
.begin();
157 str03
.insert(cit01
, 'u');
158 VERIFY( str03
== "ubaker beach, san francisco" );
160 // iterator insert(iterator p, size_type n, char c)
161 // inserts n copies of c before the character referred to by p
163 cit01
= str03
.begin();
164 str03
.insert(cit01
, 5, 'u');
165 VERIFY( str03
== "uuuuubaker beach, san francisco" );
169 // insert(iterator p, inputit first, inputit, last)
170 // ISO-14882: defect #7 part 1 clarifies this member function to be:
171 // insert(p - begin(), string(first,last))
173 csz01
= str03
.size();
174 str03
.insert(str03
.begin(), str01
.begin(), str01
.end());
175 VERIFY( str03
== "rodeo beach, marinbaker beach, san francisco" );
178 csz01
= str03
.size();
179 str03
.insert(str03
.end(), str01
.begin(), str01
.end());
180 VERIFY( str03
== "baker beach, san franciscorodeo beach, marin" );
186 __gnu_test::set_memory_limits();