OSDN Git Service

5e1eda8695911c06150bb0b1b823bb1e4fe0a0b8
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 21_strings / insert.cc
1 // 1999-06-03 bkoz
2
3 // Copyright (C) 1999, 2003 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 #include <testsuite_hooks.h>
26
27 int test01(void)
28 {
29   bool test = true;
30   typedef std::string::size_type csize_type;
31   typedef std::string::iterator citerator;
32   csize_type npos = std::string::npos;
33   csize_type csz01, csz02;
34
35   const std::string str01("rodeo beach, marin");
36   const std::string str02("baker beach, san francisco");
37   std::string str03;
38
39   // string& insert(size_type p1, const string& str, size_type p2, size_type n)
40   // requires:
41   //   1) p1 <= size()
42   //   2) p2 <= str.size()
43   //   3) rlen = min(n, str.size() - p2)
44   // throws:
45   //   1) out_of_range if p1 > size() || p2 > str.size()
46   //   2) length_error if size() >= npos - rlen
47   // effects:
48   // replaces *this with new string of length size() + rlen such that
49   // nstr[0]  to nstr[p1] == thisstr[0] to thisstr[p1]
50   // nstr[p1 + 1] to nstr[p1 + rlen] == str[p2] to str[p2 + rlen]
51   // nstr[p1 + 1 + rlen] to nstr[...] == thisstr[p1 + 1] to thisstr[...]  
52   str03 = str01; 
53   csz01 = str03.size();
54   csz02 = str02.size();
55   try {
56     str03.insert(csz01 + 1, str02, 0, 5);
57     VERIFY( false );
58   }              
59   catch(std::out_of_range& fail) {
60     VERIFY( true );
61   }
62   catch(...) {
63     VERIFY( false );
64   }
65
66   str03 = str01; 
67   csz01 = str03.size();
68   csz02 = str02.size();
69   try {
70     str03.insert(0, str02, csz02 + 1, 5);
71     VERIFY( false );
72   }              
73   catch(std::out_of_range& fail) {
74     VERIFY( true );
75   }
76   catch(...) {
77     VERIFY( false );
78   }
79
80   csz01 = str01.max_size();
81   try {
82     std::string str04(csz01, 'b'); 
83     str03 = str04; 
84     csz02 = str02.size();
85     try {
86       str03.insert(0, str02, 0, 5);
87       VERIFY( false );
88     }            
89     catch(std::length_error& fail) {
90       VERIFY( true );
91     }
92     catch(...) {
93       VERIFY( false );
94     }
95   }
96   catch(std::bad_alloc& failure){
97     VERIFY( true ); 
98   }
99   catch(std::exception& failure){
100     VERIFY( false );
101   }
102
103   str03 = str01; 
104   csz01 = str03.size();
105   csz02 = str02.size();
106   str03.insert(13, str02, 0, 12); 
107   VERIFY( str03 == "rodeo beach, baker beach,marin" );
108
109   str03 = str01; 
110   csz01 = str03.size();
111   csz02 = str02.size();
112   str03.insert(0, str02, 0, 12); 
113   VERIFY( str03 == "baker beach,rodeo beach, marin" );
114
115   str03 = str01; 
116   csz01 = str03.size();
117   csz02 = str02.size();
118   str03.insert(csz01, str02, 0, csz02); 
119   VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" );
120
121   // string& insert(size_type __p, const string& string);
122   // insert(p1, str, 0, npos)
123   str03 = str01; 
124   csz01 = str03.size();
125   csz02 = str02.size();
126   str03.insert(csz01, str02); 
127   VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" );
128
129   str03 = str01; 
130   csz01 = str03.size();
131   csz02 = str02.size();
132   str03.insert(0, str02); 
133   VERIFY( str03 == "baker beach, san franciscorodeo beach, marin" );
134
135   // string& insert(size_type __p, const char* s, size_type n);
136   // insert(p1, string(s,n))
137   str03 = str02; 
138   csz01 = str03.size();
139   str03.insert(0, "-break at the bridge", 20); 
140   VERIFY( str03 == "-break at the bridgebaker beach, san francisco" );
141
142   // string& insert(size_type __p, const char* s);
143   // insert(p1, string(s))
144   str03 = str02; 
145   str03.insert(0, "-break at the bridge"); 
146   VERIFY( str03 == "-break at the bridgebaker beach, san francisco" );
147
148   // string& insert(size_type __p, size_type n, char c)
149   // insert(p1, string(n,c))
150   str03 = str02; 
151   csz01 = str03.size();
152   str03.insert(csz01, 5, 'z'); 
153   VERIFY( str03 == "baker beach, san franciscozzzzz" );
154
155   // iterator insert(iterator p, char c)
156   // inserts a copy of c before the character referred to by p
157   str03 = str02; 
158   citerator cit01 = str03.begin();
159   str03.insert(cit01, 'u'); 
160   VERIFY( str03 == "ubaker beach, san francisco" );
161
162   // iterator insert(iterator p, size_type n,  char c)
163   // inserts n copies of c before the character referred to by p
164   str03 = str02; 
165   cit01 = str03.begin();
166   str03.insert(cit01, 5, 'u'); 
167   VERIFY( str03 == "uuuuubaker beach, san francisco" );
168
169   // template<inputit>
170   //   void 
171   //   insert(iterator p, inputit first, inputit, last)
172   // ISO-14882: defect #7 part 1 clarifies this member function to be:
173   // insert(p - begin(), string(first,last))
174   str03 = str02; 
175   csz01 = str03.size();
176   str03.insert(str03.begin(), str01.begin(), str01.end()); 
177   VERIFY( str03 == "rodeo beach, marinbaker beach, san francisco" );
178
179   str03 = str02; 
180   csz01 = str03.size();
181   str03.insert(str03.end(), str01.begin(), str01.end()); 
182   VERIFY( str03 == "baker beach, san franciscorodeo beach, marin" );
183
184 #ifdef DEBUG_ASSERT
185   assert(test);
186 #endif
187   return test;
188 }
189
190 // Once more
191 //   string& insert(size_type __p, const char* s, size_type n);
192 //   string& insert(size_type __p, const char* s);
193 // but now s points inside the _Rep
194 int test02(void)
195 {
196   bool test = true;
197
198   std::string str01;
199   const char* title = "Everything was beautiful, and nothing hurt";
200   // Increasing size: str01 is reallocated every time.
201   str01 = title;
202   str01.insert(0, str01.c_str() + str01.size() - 4, 4);
203   VERIFY( str01 == "hurtEverything was beautiful, and nothing hurt" );
204   str01 = title;
205   str01.insert(0, str01.c_str(), 5);
206   VERIFY( str01 == "EveryEverything was beautiful, and nothing hurt" );
207   str01 = title;
208   str01.insert(10, str01.c_str() + 4, 6);
209   VERIFY( str01 == "Everythingything was beautiful, and nothing hurt" );
210   str01 = title;
211   str01.insert(15, str01.c_str(), 10);
212   VERIFY( str01 == "Everything was Everythingbeautiful, and nothing hurt" );
213   str01 = title;
214   str01.insert(15, str01.c_str() + 11, 13);
215   VERIFY( str01 == "Everything was was beautifulbeautiful, and nothing hurt" );
216   str01 = title;
217   str01.insert(0, str01.c_str());
218   VERIFY( str01 == "Everything was beautiful, and nothing hurt"
219           "Everything was beautiful, and nothing hurt");
220   // Again: no reallocations.
221   str01 = title;
222   str01.insert(0, str01.c_str() + str01.size() - 4, 4);
223   VERIFY( str01 == "hurtEverything was beautiful, and nothing hurt" );
224   str01 = title;
225   str01.insert(0, str01.c_str(), 5);
226   VERIFY( str01 == "EveryEverything was beautiful, and nothing hurt" );
227   str01 = title;
228   str01.insert(10, str01.c_str() + 4, 6);
229   VERIFY( str01 == "Everythingything was beautiful, and nothing hurt" );
230   str01 = title;
231   str01.insert(15, str01.c_str(), 10);
232   VERIFY( str01 == "Everything was Everythingbeautiful, and nothing hurt" );
233   str01 = title;
234   str01.insert(15, str01.c_str() + 11, 13);
235   VERIFY( str01 == "Everything was was beautifulbeautiful, and nothing hurt" );
236   str01 = title;
237   str01.insert(0, str01.c_str());
238   VERIFY( str01 == "Everything was beautiful, and nothing hurt"
239           "Everything was beautiful, and nothing hurt");
240
241 #ifdef DEBUG_ASSERT
242   assert(test);
243 #endif
244   return test;
245 }
246
247 int main()
248
249   __gnu_cxx_test::set_memory_limits();
250   test01();
251   test02();
252   return 0;
253 }