| 1 | // 1999-06-03 bkoz |
| 2 | |
| 3 | // Copyright (C) 1999-2025 Free Software Foundation, Inc. |
| 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 |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 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 |
| 17 | // with this library; see the file COPYING3. If not see |
| 18 | // <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | // 21.3.5.4 basic_string::insert |
| 21 | |
| 22 | #include <stdexcept> |
| 23 | #include <testsuite_hooks.h> |
| 24 | |
| 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 | |
| 33 | void test01(void) |
| 34 | { |
| 35 | typedef string::size_type csize_type; |
| 36 | typedef string::iterator citerator; |
| 37 | csize_type csz01, csz02; |
| 38 | |
| 39 | const string str01("rodeo beach, marin"); |
| 40 | const string str02("baker beach, san francisco"); |
| 41 | string str03; |
| 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); |
| 61 | VERIFY( false ); |
| 62 | } |
| 63 | catch(std::out_of_range& fail) { |
| 64 | VERIFY( true ); |
| 65 | } |
| 66 | catch(...) { |
| 67 | VERIFY( false ); |
| 68 | } |
| 69 | |
| 70 | str03 = str01; |
| 71 | csz01 = str03.size(); |
| 72 | csz02 = str02.size(); |
| 73 | try { |
| 74 | str03.insert(0, str02, csz02 + 1, 5); |
| 75 | VERIFY( false ); |
| 76 | } |
| 77 | catch(std::out_of_range& fail) { |
| 78 | VERIFY( true ); |
| 79 | } |
| 80 | catch(...) { |
| 81 | VERIFY( false ); |
| 82 | } |
| 83 | |
| 84 | csz01 = str01.max_size(); |
| 85 | try { |
| 86 | string str04(csz01, 'b'); |
| 87 | str03 = str04; |
| 88 | csz02 = str02.size(); |
| 89 | try { |
| 90 | str03.insert(0, str02, 0, 5); |
| 91 | VERIFY( false ); |
| 92 | } |
| 93 | catch(std::length_error& fail) { |
| 94 | VERIFY( true ); |
| 95 | } |
| 96 | catch(...) { |
| 97 | VERIFY( false ); |
| 98 | } |
| 99 | } |
| 100 | catch(std::bad_alloc& failure){ |
| 101 | VERIFY( true ); |
| 102 | } |
| 103 | catch(std::exception& failure){ |
| 104 | VERIFY( false ); |
| 105 | } |
| 106 | |
| 107 | str03 = str01; |
| 108 | csz01 = str03.size(); |
| 109 | csz02 = str02.size(); |
| 110 | str03.insert(13, str02, 0, 12); |
| 111 | VERIFY( str03 == "rodeo beach, baker beach,marin" ); |
| 112 | |
| 113 | str03 = str01; |
| 114 | csz01 = str03.size(); |
| 115 | csz02 = str02.size(); |
| 116 | str03.insert(0, str02, 0, 12); |
| 117 | VERIFY( str03 == "baker beach,rodeo beach, marin" ); |
| 118 | |
| 119 | str03 = str01; |
| 120 | csz01 = str03.size(); |
| 121 | csz02 = str02.size(); |
| 122 | str03.insert(csz01, str02, 0, csz02); |
| 123 | VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" ); |
| 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); |
| 131 | VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" ); |
| 132 | |
| 133 | str03 = str01; |
| 134 | csz01 = str03.size(); |
| 135 | csz02 = str02.size(); |
| 136 | str03.insert(0, str02); |
| 137 | VERIFY( str03 == "baker beach, san franciscorodeo beach, marin" ); |
| 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); |
| 144 | VERIFY( str03 == "-break at the bridgebaker beach, san francisco" ); |
| 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"); |
| 150 | VERIFY( str03 == "-break at the bridgebaker beach, san francisco" ); |
| 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'); |
| 157 | VERIFY( str03 == "baker beach, san franciscozzzzz" ); |
| 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'); |
| 164 | VERIFY( str03 == "ubaker beach, san francisco" ); |
| 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'); |
| 171 | VERIFY( str03 == "uuuuubaker beach, san francisco" ); |
| 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()); |
| 181 | VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" ); |
| 182 | |
| 183 | str03 = str02; |
| 184 | csz01 = str03.size(); |
| 185 | str03.insert(str03.end(), str01.begin(), str01.end()); |
| 186 | VERIFY( str03 == "baker beach, san franciscorodeo beach, marin" ); |
| 187 | } |
| 188 | |
| 189 | int main() |
| 190 | { |
| 191 | __gnu_test::set_memory_limits(); |
| 192 | test01(); |
| 193 | return 0; |
| 194 | } |