| 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 <string> |
| 23 | #include <stdexcept> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | void test01(void) |
| 27 | { |
| 28 | typedef std::wstring::size_type csize_type; |
| 29 | typedef std::wstring::iterator citerator; |
| 30 | csize_type csz01, csz02; |
| 31 | |
| 32 | const std::wstring str01(L"rodeo beach, marin"); |
| 33 | const std::wstring str02(L"baker beach, san francisco"); |
| 34 | std::wstring str03; |
| 35 | |
| 36 | // wstring& insert(size_type p1, const wstring& 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 wstring 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); |
| 54 | VERIFY( false ); |
| 55 | } |
| 56 | catch(std::out_of_range& fail) { |
| 57 | VERIFY( true ); |
| 58 | } |
| 59 | catch(...) { |
| 60 | VERIFY( false ); |
| 61 | } |
| 62 | |
| 63 | str03 = str01; |
| 64 | csz01 = str03.size(); |
| 65 | csz02 = str02.size(); |
| 66 | try { |
| 67 | str03.insert(0, str02, csz02 + 1, 5); |
| 68 | VERIFY( false ); |
| 69 | } |
| 70 | catch(std::out_of_range& fail) { |
| 71 | VERIFY( true ); |
| 72 | } |
| 73 | catch(...) { |
| 74 | VERIFY( false ); |
| 75 | } |
| 76 | |
| 77 | csz01 = str01.max_size(); |
| 78 | try { |
| 79 | std::wstring str04(csz01, L'b'); |
| 80 | str03 = str04; |
| 81 | csz02 = str02.size(); |
| 82 | try { |
| 83 | str03.insert(0, str02, 0, 5); |
| 84 | VERIFY( false ); |
| 85 | } |
| 86 | catch(std::length_error& fail) { |
| 87 | VERIFY( true ); |
| 88 | } |
| 89 | catch(...) { |
| 90 | VERIFY( false ); |
| 91 | } |
| 92 | } |
| 93 | catch(std::bad_alloc& failure){ |
| 94 | VERIFY( true ); |
| 95 | } |
| 96 | catch(std::exception& failure){ |
| 97 | VERIFY( false ); |
| 98 | } |
| 99 | |
| 100 | str03 = str01; |
| 101 | csz01 = str03.size(); |
| 102 | csz02 = str02.size(); |
| 103 | str03.insert(13, str02, 0, 12); |
| 104 | VERIFY( str03 == L"rodeo beach, baker beach,marin" ); |
| 105 | |
| 106 | str03 = str01; |
| 107 | csz01 = str03.size(); |
| 108 | csz02 = str02.size(); |
| 109 | str03.insert(0, str02, 0, 12); |
| 110 | VERIFY( str03 == L"baker beach,rodeo beach, marin" ); |
| 111 | |
| 112 | str03 = str01; |
| 113 | csz01 = str03.size(); |
| 114 | csz02 = str02.size(); |
| 115 | str03.insert(csz01, str02, 0, csz02); |
| 116 | VERIFY( str03 == L"rodeo beach, marinbaker beach, san francisco" ); |
| 117 | |
| 118 | // wstring& insert(size_type __p, const wstring& wstr); |
| 119 | // insert(p1, str, 0, npos) |
| 120 | str03 = str01; |
| 121 | csz01 = str03.size(); |
| 122 | csz02 = str02.size(); |
| 123 | str03.insert(csz01, str02); |
| 124 | VERIFY( str03 == L"rodeo beach, marinbaker beach, san francisco" ); |
| 125 | |
| 126 | str03 = str01; |
| 127 | csz01 = str03.size(); |
| 128 | csz02 = str02.size(); |
| 129 | str03.insert(0, str02); |
| 130 | VERIFY( str03 == L"baker beach, san franciscorodeo beach, marin" ); |
| 131 | |
| 132 | // wstring& insert(size_type __p, const wchar_t* s, size_type n); |
| 133 | // insert(p1, wstring(s,n)) |
| 134 | str03 = str02; |
| 135 | csz01 = str03.size(); |
| 136 | str03.insert(0, L"-break at the bridge", 20); |
| 137 | VERIFY( str03 == L"-break at the bridgebaker beach, san francisco" ); |
| 138 | |
| 139 | // wstring& insert(size_type __p, const wchar_t* s); |
| 140 | // insert(p1, wstring(s)) |
| 141 | str03 = str02; |
| 142 | str03.insert(0, L"-break at the bridge"); |
| 143 | VERIFY( str03 == L"-break at the bridgebaker beach, san francisco" ); |
| 144 | |
| 145 | // wstring& insert(size_type __p, size_type n, wchar_t c) |
| 146 | // insert(p1, wstring(n,c)) |
| 147 | str03 = str02; |
| 148 | csz01 = str03.size(); |
| 149 | str03.insert(csz01, 5, L'z'); |
| 150 | VERIFY( str03 == L"baker beach, san franciscozzzzz" ); |
| 151 | |
| 152 | // iterator insert(iterator p, wchar_t 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, L'u'); |
| 157 | VERIFY( str03 == L"ubaker beach, san francisco" ); |
| 158 | |
| 159 | // iterator insert(iterator p, size_type n, wchar_t 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, L'u'); |
| 164 | VERIFY( str03 == L"uuuuubaker beach, san francisco" ); |
| 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(), wstring(first,last)) |
| 171 | str03 = str02; |
| 172 | csz01 = str03.size(); |
| 173 | str03.insert(str03.begin(), str01.begin(), str01.end()); |
| 174 | VERIFY( str03 == L"rodeo beach, marinbaker beach, san francisco" ); |
| 175 | |
| 176 | str03 = str02; |
| 177 | csz01 = str03.size(); |
| 178 | str03.insert(str03.end(), str01.begin(), str01.end()); |
| 179 | VERIFY( str03 == L"baker beach, san franciscorodeo beach, marin" ); |
| 180 | } |
| 181 | |
| 182 | int main() |
| 183 | { |
| 184 | __gnu_test::set_memory_limits(); |
| 185 | test01(); |
| 186 | return 0; |
| 187 | } |