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