11558Srgrimes// 1999-06-03 bkoz 21558Srgrimes 31558Srgrimes// Copyright (C) 1999, 2003 Free Software Foundation, Inc. 41558Srgrimes// 51558Srgrimes// This file is part of the GNU ISO C++ Library. This library is free 61558Srgrimes// software; you can redistribute it and/or modify it under the 71558Srgrimes// terms of the GNU General Public License as published by the 81558Srgrimes// Free Software Foundation; either version 2, or (at your option) 91558Srgrimes// any later version. 101558Srgrimes 111558Srgrimes// This library is distributed in the hope that it will be useful, 121558Srgrimes// but WITHOUT ANY WARRANTY; without even the implied warranty of 131558Srgrimes// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 141558Srgrimes// GNU General Public License for more details. 151558Srgrimes 161558Srgrimes// You should have received a copy of the GNU General Public License along 171558Srgrimes// with this library; see the file COPYING. If not, write to the Free 181558Srgrimes// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 191558Srgrimes// USA. 201558Srgrimes 211558Srgrimes// 21.3.5.4 basic_string::insert 221558Srgrimes 231558Srgrimes#include <string> 241558Srgrimes#include <stdexcept> 251558Srgrimes#include <testsuite_hooks.h> 261558Srgrimes 271558Srgrimesint test01(void) 281558Srgrimes{ 291558Srgrimes bool test __attribute__((unused)) = true; 301558Srgrimes typedef std::string::size_type csize_type; 3137906Scharnier typedef std::string::iterator citerator; 321558Srgrimes csize_type csz01, csz02; 331558Srgrimes 341558Srgrimes const std::string str01("rodeo beach, marin"); 351558Srgrimes const std::string str02("baker beach, san francisco"); 361558Srgrimes std::string str03; 3737906Scharnier 3823685Speter // string& insert(size_type p1, const string& str, size_type p2, size_type n) 3937906Scharnier // requires: 401558Srgrimes // 1) p1 <= size() 411558Srgrimes // 2) p2 <= str.size() 42146754Scharnier // 3) rlen = min(n, str.size() - p2) 43146754Scharnier // throws: 44146754Scharnier // 1) out_of_range if p1 > size() || p2 > str.size() 451558Srgrimes // 2) length_error if size() >= npos - rlen 4637906Scharnier // effects: 471558Srgrimes // replaces *this with new string of length size() + rlen such that 4823685Speter // nstr[0] to nstr[p1] == thisstr[0] to thisstr[p1] 491558Srgrimes // nstr[p1 + 1] to nstr[p1 + rlen] == str[p2] to str[p2 + rlen] 501558Srgrimes // nstr[p1 + 1 + rlen] to nstr[...] == thisstr[p1 + 1] to thisstr[...] 511558Srgrimes str03 = str01; 52103949Smike csz01 = str03.size(); 53118526Sache csz02 = str02.size(); 5473986Sobrien try { 551558Srgrimes str03.insert(csz01 + 1, str02, 0, 5); 561558Srgrimes VERIFY( false ); 5778732Sdd } 5823685Speter catch(std::out_of_range& fail) { 591558Srgrimes VERIFY( true ); 601558Srgrimes } 611558Srgrimes catch(...) { 621558Srgrimes VERIFY( false ); 63164911Sdwmalone } 641558Srgrimes 6535852Sjkh str03 = str01; 66128175Sgreen csz01 = str03.size(); 671558Srgrimes csz02 = str02.size(); 681558Srgrimes try { 691558Srgrimes str03.insert(0, str02, csz02 + 1, 5); 701558Srgrimes VERIFY( false ); 711558Srgrimes } 7223685Speter catch(std::out_of_range& fail) { 731558Srgrimes VERIFY( true ); 741558Srgrimes } 751558Srgrimes catch(...) { 761558Srgrimes VERIFY( false ); 771558Srgrimes } 7892837Simp 7992837Simp csz01 = str01.max_size(); 801558Srgrimes try { 811558Srgrimes std::string str04(csz01, 'b'); 8292837Simp str03 = str04; 831558Srgrimes csz02 = str02.size(); 841558Srgrimes try { 851558Srgrimes str03.insert(0, str02, 0, 5); 8621149Simp VERIFY( false ); 871558Srgrimes } 881558Srgrimes catch(std::length_error& fail) { 891558Srgrimes VERIFY( true ); 9021149Simp } 9121149Simp catch(...) { 9221149Simp VERIFY( false ); 931558Srgrimes } 941558Srgrimes } 951558Srgrimes catch(std::bad_alloc& failure){ 96118526Sache VERIFY( true ); 97118526Sache } 98128175Sgreen catch(std::exception& failure){ 991558Srgrimes VERIFY( false ); 100164911Sdwmalone } 1011558Srgrimes 1021558Srgrimes str03 = str01; 1031558Srgrimes csz01 = str03.size(); 1041558Srgrimes csz02 = str02.size(); 1051558Srgrimes str03.insert(13, str02, 0, 12); 1061558Srgrimes VERIFY( str03 == "rodeo beach, baker beach,marin" ); 1071558Srgrimes 1081558Srgrimes str03 = str01; 1091558Srgrimes csz01 = str03.size(); 1101558Srgrimes csz02 = str02.size(); 1111558Srgrimes str03.insert(0, str02, 0, 12); 1121558Srgrimes VERIFY( str03 == "baker beach,rodeo beach, marin" ); 1131558Srgrimes 114164911Sdwmalone str03 = str01; 115164911Sdwmalone csz01 = str03.size(); 116164911Sdwmalone csz02 = str02.size(); 1171558Srgrimes str03.insert(csz01, str02, 0, csz02); 118128175Sgreen VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" ); 119128175Sgreen 120128175Sgreen // string& insert(size_type __p, const string& string); 1211558Srgrimes // insert(p1, str, 0, npos) 1221558Srgrimes str03 = str01; 123128175Sgreen csz01 = str03.size(); 124128175Sgreen csz02 = str02.size(); 125128175Sgreen str03.insert(csz01, str02); 126128175Sgreen VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" ); 127128175Sgreen 128128175Sgreen str03 = str01; 129128175Sgreen csz01 = str03.size(); 1301558Srgrimes csz02 = str02.size(); 1311558Srgrimes str03.insert(0, str02); 1321558Srgrimes VERIFY( str03 == "baker beach, san franciscorodeo beach, marin" ); 1331558Srgrimes 1341558Srgrimes // string& insert(size_type __p, const char* s, size_type n); 1351558Srgrimes // insert(p1, string(s,n)) 1361558Srgrimes str03 = str02; 1371558Srgrimes csz01 = str03.size(); 1381558Srgrimes str03.insert(0, "-break at the bridge", 20); 1391558Srgrimes VERIFY( str03 == "-break at the bridgebaker beach, san francisco" ); 1401558Srgrimes 1411558Srgrimes // string& insert(size_type __p, const char* s); 1421558Srgrimes // insert(p1, string(s)) 1431558Srgrimes str03 = str02; 1441558Srgrimes str03.insert(0, "-break at the bridge"); 1451558Srgrimes VERIFY( str03 == "-break at the bridgebaker beach, san francisco" ); 1461558Srgrimes 1471558Srgrimes // string& insert(size_type __p, size_type n, char c) 1481558Srgrimes // insert(p1, string(n,c)) 1491558Srgrimes str03 = str02; 1501558Srgrimes csz01 = str03.size(); 1511558Srgrimes str03.insert(csz01, 5, 'z'); 1521558Srgrimes VERIFY( str03 == "baker beach, san franciscozzzzz" ); 1531558Srgrimes 1541558Srgrimes // iterator insert(iterator p, char c) 1551558Srgrimes // inserts a copy of c before the character referred to by p 1561558Srgrimes str03 = str02; 1571558Srgrimes citerator cit01 = str03.begin(); 15835852Sjkh str03.insert(cit01, 'u'); 15935852Sjkh VERIFY( str03 == "ubaker beach, san francisco" ); 16035852Sjkh 1611558Srgrimes // iterator insert(iterator p, size_type n, char c) 1621558Srgrimes // inserts n copies of c before the character referred to by p 1631558Srgrimes str03 = str02; 1641558Srgrimes cit01 = str03.begin(); 1651558Srgrimes str03.insert(cit01, 5, 'u'); 1661558Srgrimes VERIFY( str03 == "uuuuubaker beach, san francisco" ); 1671558Srgrimes 1681558Srgrimes // template<inputit> 1691558Srgrimes // void 1701558Srgrimes // insert(iterator p, inputit first, inputit, last) 1711558Srgrimes // ISO-14882: defect #7 part 1 clarifies this member function to be: 1721558Srgrimes // insert(p - begin(), string(first,last)) 1731558Srgrimes str03 = str02; 1741558Srgrimes csz01 = str03.size(); 1751558Srgrimes str03.insert(str03.begin(), str01.begin(), str01.end()); 1761558Srgrimes VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" ); 1771558Srgrimes 1781558Srgrimes str03 = str02; 1791558Srgrimes csz01 = str03.size(); 1801558Srgrimes str03.insert(str03.end(), str01.begin(), str01.end()); 1811558Srgrimes VERIFY( str03 == "baker beach, san franciscorodeo beach, marin" ); 182128175Sgreen return test; 183128175Sgreen} 184128175Sgreen 1851558Srgrimesint main() 1861558Srgrimes{ 1871558Srgrimes __gnu_test::set_memory_limits(); 1881558Srgrimes test01(); 1891558Srgrimes return 0; 1901558Srgrimes} 1911558Srgrimes