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