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