]>
Commit | Line | Data |
---|---|---|
b2dad0e3 BK |
1 | // 1999-06-03 bkoz |
2 | ||
8d9254fc | 3 | // Copyright (C) 1999-2020 Free Software Foundation, Inc. |
b2dad0e3 BK |
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 | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
b2dad0e3 BK |
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 | |
748086b7 JJ |
17 | // with this library; see the file COPYING3. If not see |
18 | // <http://www.gnu.org/licenses/>. | |
b2dad0e3 BK |
19 | |
20 | // 21.3.5.4 basic_string::insert | |
21 | ||
b2dad0e3 | 22 | #include <stdexcept> |
fe413112 | 23 | #include <testsuite_hooks.h> |
b2dad0e3 | 24 | |
96eb9df6 FD |
25 | #ifdef _GLIBCXX_DEBUG |
26 | #include <debug/string> | |
27 | using namespace __gnu_debug; | |
28 | #else | |
29 | #include <string> | |
30 | using namespace std; | |
31 | #endif | |
32 | ||
118c8424 | 33 | void test01(void) |
b2dad0e3 | 34 | { |
96eb9df6 FD |
35 | typedef string::size_type csize_type; |
36 | typedef string::iterator citerator; | |
b2dad0e3 BK |
37 | csize_type csz01, csz02; |
38 | ||
96eb9df6 FD |
39 | const string str01("rodeo beach, marin"); |
40 | const string str02("baker beach, san francisco"); | |
41 | string str03; | |
b2dad0e3 BK |
42 | |
43 | // string& insert(size_type p1, const string& str, size_type p2, size_type n) | |
44 | // requires: | |
45 | // 1) p1 <= size() | |
46 | // 2) p2 <= str.size() | |
47 | // 3) rlen = min(n, str.size() - p2) | |
48 | // throws: | |
49 | // 1) out_of_range if p1 > size() || p2 > str.size() | |
50 | // 2) length_error if size() >= npos - rlen | |
51 | // effects: | |
52 | // replaces *this with new string of length size() + rlen such that | |
53 | // nstr[0] to nstr[p1] == thisstr[0] to thisstr[p1] | |
54 | // nstr[p1 + 1] to nstr[p1 + rlen] == str[p2] to str[p2 + rlen] | |
55 | // nstr[p1 + 1 + rlen] to nstr[...] == thisstr[p1 + 1] to thisstr[...] | |
56 | str03 = str01; | |
57 | csz01 = str03.size(); | |
58 | csz02 = str02.size(); | |
59 | try { | |
60 | str03.insert(csz01 + 1, str02, 0, 5); | |
aa1b2f7d | 61 | VERIFY( false ); |
b2dad0e3 BK |
62 | } |
63 | catch(std::out_of_range& fail) { | |
aa1b2f7d | 64 | VERIFY( true ); |
b2dad0e3 BK |
65 | } |
66 | catch(...) { | |
aa1b2f7d | 67 | VERIFY( false ); |
b2dad0e3 BK |
68 | } |
69 | ||
70 | str03 = str01; | |
71 | csz01 = str03.size(); | |
72 | csz02 = str02.size(); | |
73 | try { | |
74 | str03.insert(0, str02, csz02 + 1, 5); | |
aa1b2f7d | 75 | VERIFY( false ); |
b2dad0e3 BK |
76 | } |
77 | catch(std::out_of_range& fail) { | |
aa1b2f7d | 78 | VERIFY( true ); |
b2dad0e3 BK |
79 | } |
80 | catch(...) { | |
aa1b2f7d | 81 | VERIFY( false ); |
b2dad0e3 BK |
82 | } |
83 | ||
84 | csz01 = str01.max_size(); | |
85 | try { | |
96eb9df6 | 86 | string str04(csz01, 'b'); |
b2dad0e3 BK |
87 | str03 = str04; |
88 | csz02 = str02.size(); | |
89 | try { | |
90 | str03.insert(0, str02, 0, 5); | |
aa1b2f7d | 91 | VERIFY( false ); |
b2dad0e3 BK |
92 | } |
93 | catch(std::length_error& fail) { | |
aa1b2f7d | 94 | VERIFY( true ); |
b2dad0e3 BK |
95 | } |
96 | catch(...) { | |
aa1b2f7d | 97 | VERIFY( false ); |
b2dad0e3 BK |
98 | } |
99 | } | |
100 | catch(std::bad_alloc& failure){ | |
aa1b2f7d | 101 | VERIFY( true ); |
b2dad0e3 BK |
102 | } |
103 | catch(std::exception& failure){ | |
aa1b2f7d | 104 | VERIFY( false ); |
b2dad0e3 BK |
105 | } |
106 | ||
107 | str03 = str01; | |
108 | csz01 = str03.size(); | |
109 | csz02 = str02.size(); | |
110 | str03.insert(13, str02, 0, 12); | |
aa1b2f7d | 111 | VERIFY( str03 == "rodeo beach, baker beach,marin" ); |
b2dad0e3 BK |
112 | |
113 | str03 = str01; | |
114 | csz01 = str03.size(); | |
115 | csz02 = str02.size(); | |
116 | str03.insert(0, str02, 0, 12); | |
aa1b2f7d | 117 | VERIFY( str03 == "baker beach,rodeo beach, marin" ); |
b2dad0e3 BK |
118 | |
119 | str03 = str01; | |
120 | csz01 = str03.size(); | |
121 | csz02 = str02.size(); | |
122 | str03.insert(csz01, str02, 0, csz02); | |
aa1b2f7d | 123 | VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" ); |
b2dad0e3 BK |
124 | |
125 | // string& insert(size_type __p, const string& string); | |
126 | // insert(p1, str, 0, npos) | |
127 | str03 = str01; | |
128 | csz01 = str03.size(); | |
129 | csz02 = str02.size(); | |
130 | str03.insert(csz01, str02); | |
aa1b2f7d | 131 | VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" ); |
b2dad0e3 BK |
132 | |
133 | str03 = str01; | |
134 | csz01 = str03.size(); | |
135 | csz02 = str02.size(); | |
136 | str03.insert(0, str02); | |
aa1b2f7d | 137 | VERIFY( str03 == "baker beach, san franciscorodeo beach, marin" ); |
b2dad0e3 BK |
138 | |
139 | // string& insert(size_type __p, const char* s, size_type n); | |
140 | // insert(p1, string(s,n)) | |
141 | str03 = str02; | |
142 | csz01 = str03.size(); | |
143 | str03.insert(0, "-break at the bridge", 20); | |
aa1b2f7d | 144 | VERIFY( str03 == "-break at the bridgebaker beach, san francisco" ); |
b2dad0e3 BK |
145 | |
146 | // string& insert(size_type __p, const char* s); | |
147 | // insert(p1, string(s)) | |
148 | str03 = str02; | |
149 | str03.insert(0, "-break at the bridge"); | |
aa1b2f7d | 150 | VERIFY( str03 == "-break at the bridgebaker beach, san francisco" ); |
b2dad0e3 BK |
151 | |
152 | // string& insert(size_type __p, size_type n, char c) | |
153 | // insert(p1, string(n,c)) | |
154 | str03 = str02; | |
155 | csz01 = str03.size(); | |
156 | str03.insert(csz01, 5, 'z'); | |
aa1b2f7d | 157 | VERIFY( str03 == "baker beach, san franciscozzzzz" ); |
b2dad0e3 BK |
158 | |
159 | // iterator insert(iterator p, char c) | |
160 | // inserts a copy of c before the character referred to by p | |
161 | str03 = str02; | |
162 | citerator cit01 = str03.begin(); | |
163 | str03.insert(cit01, 'u'); | |
aa1b2f7d | 164 | VERIFY( str03 == "ubaker beach, san francisco" ); |
b2dad0e3 BK |
165 | |
166 | // iterator insert(iterator p, size_type n, char c) | |
167 | // inserts n copies of c before the character referred to by p | |
168 | str03 = str02; | |
169 | cit01 = str03.begin(); | |
170 | str03.insert(cit01, 5, 'u'); | |
aa1b2f7d | 171 | VERIFY( str03 == "uuuuubaker beach, san francisco" ); |
b2dad0e3 BK |
172 | |
173 | // template<inputit> | |
174 | // void | |
175 | // insert(iterator p, inputit first, inputit, last) | |
176 | // ISO-14882: defect #7 part 1 clarifies this member function to be: | |
177 | // insert(p - begin(), string(first,last)) | |
178 | str03 = str02; | |
179 | csz01 = str03.size(); | |
180 | str03.insert(str03.begin(), str01.begin(), str01.end()); | |
aa1b2f7d | 181 | VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" ); |
b2dad0e3 BK |
182 | |
183 | str03 = str02; | |
184 | csz01 = str03.size(); | |
185 | str03.insert(str03.end(), str01.begin(), str01.end()); | |
aa1b2f7d | 186 | VERIFY( str03 == "baker beach, san franciscorodeo beach, marin" ); |
b2dad0e3 BK |
187 | } |
188 | ||
189 | int main() | |
190 | { | |
aecf642c | 191 | __gnu_test::set_memory_limits(); |
b2dad0e3 | 192 | test01(); |
04d930e6 | 193 | return 0; |
b2dad0e3 | 194 | } |