1 // 1999-06-10 bkoz
2
3 // Copyright (C) 1994, 1999, 2001, 2002 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.6 basic_string::replace
22
23 #include <string>
24 #include <stdexcept>
25 #include <testsuite_hooks.h>
26
test01(void)27 bool test01(void)
28 {
29 bool test = true;
30 typedef std::string::size_type csize_type;
31 typedef std::string::const_reference cref;
32 typedef std::string::reference ref;
33 csize_type npos = std::string::npos;
34 csize_type csz01, csz02;
35
36 const char str_lit01[] = "ventura, california";
37 const std::string str01(str_lit01);
38 std::string str02("del mar, california");
39 std::string str03(" and ");
40 std::string str05;
41
42 // string& replace(size_type pos, size_type n, const string& string)
43 // string& replace(size_type pos1, size_type n1, const string& string,
44 // size_type pos2, size_type n2)
45 // string& replace(size_type pos, size_type n1, const char* s, size_type n2)
46 // string& replace(size_type pos, size_type n1, const char* s)
47 // string& replace(size_type pos, size_type n1, size_type n2, char c)
48 // string& replace(iterator it1, iterator it2, const string& str)
49 // string& replace(iterator it1, iterator it2, const chat* s, size_type n)
50 // string& replace(iterator it1, iterator it2, const chat* s)
51 // string& replace(iterator it1, iterator it2, size_type n, char c)
52 // template<typename InputIter>
53 // string& replace(iterator it1, iterator it2, InputIter j1, InputIter j2)
54
55 // with mods, from tstring.cc, from jason merrill, et. al.
56 std::string X = "Hello";
57 std::string x = X;
58
59 char ch = x[0];
60 VERIFY( ch == 'H' );
61
62 std::string z = x.substr(2, 3);
63 VERIFY( z == "llo" );
64
65 x.replace(2, 2, "r");
66 VERIFY( x == "Hero" );
67
68 x = X;
69 x.replace(0, 1, "j");
70 VERIFY( x == "jello" );
71
72 int ar[] = { 'H', 'e', 'l', 'l', 'o' };
73 x.replace(std::find(x.begin(), x.end(), 'l'),
74 std::find(x.rbegin(), x.rend(), 'l').base(), ar,
75 ar + sizeof(ar) / sizeof(ar[0]));
76 VERIFY( x == "jeHelloo" );
77
78 #ifdef DEBUG_ASSERT
79 assert(test);
80 #endif
81 return test;
82 }
83
84 void
test02()85 test02()
86 {
87 bool test = true;
88 const char* strlit = "../the long pier/Hanalei Bay/Kauai/Hawaii";
89 std::string aux = strlit;
90 aux.replace(aux.begin()+5, aux.begin()+20,
91 aux.begin()+10, aux.begin()+15);
92 VERIFY(aux == "../thg piealei Bay/Kauai/Hawaii");
93
94 aux = strlit;
95 aux.replace(aux.begin() + 10, aux.begin() + 15,
96 aux.begin() + 5, aux.begin() + 20);
97 VERIFY(aux == "../the lone long pier/Hanr/Hanalei Bay/Kauai/Hawaii");
98 }
99
100 // Some more miscellaneous tests
101 void
test03()102 test03()
103 {
104 bool test = true;
105 const char* title01 = "nine types of ambiguity";
106 const char* title02 = "ultra";
107 std::string str01 = title01;
108 std::string str02 = title02;
109
110 str01.replace(0, 4, str02);
111 VERIFY(str01 == "ultra types of ambiguity");
112
113 str01.replace(15, 9, str02, 2, 2);
114 VERIFY(str01 == "ultra types of tr");
115
116 str01 = title01;
117 str02.replace(0, 0, str01, 0, std::string::npos);
118 VERIFY(str02 == "nine types of ambiguityultra");
119
120 str02.replace(11, 2, title02, 5);
121 VERIFY(str02 == "nine types ultra ambiguityultra");
122
123 str02.replace(11, 5, title01, 2);
124 VERIFY(str02 == "nine types ni ambiguityultra");
125
126 str01.replace(str01.size(), 0, title02);
127 VERIFY(str01 == "nine types of ambiguityultra");
128
129 str01 = title01;
130 str02 = title02;
131 str01.replace(str01.begin(), str01.end(), str02);
132 VERIFY(str01 == "ultra");
133
134 str01.replace(str01.begin(), str01.begin(), title01, 4);
135 VERIFY(str01 == "nineultra");
136
137 str01.replace(str01.end(), str01.end(), title01 + 5, 5);
138 VERIFY(str01 == "nineultratypes");
139
140 str01.replace(str01.begin(), str01.end(), title02);
141 VERIFY(str01 == "ultra");
142 }
143
144 // Some more tests for
145 // template<typename InputIter>
146 // string& replace(iterator it1, iterator it2, InputIter j1, InputIter j2)
147 void
test04()148 test04()
149 {
150 bool test = true;
151 std::string str01 = "geogaddi";
152 std::string str02;
153
154 typedef std::string::iterator iterator;
155 typedef std::string::const_iterator const_iterator;
156
157 iterator it1 = str01.begin();
158 iterator it2 = str01.end();
159 str02.replace(str02.begin(), str02.end(), it1, it2);
160 VERIFY(str02 == "geogaddi");
161
162 str02 = "boards";
163 const_iterator c_it1 = str01.begin();
164 const_iterator c_it2 = str01.end();
165 str02.replace(str02.begin(), str02.end(), c_it1, c_it2);
166 VERIFY(str02 == "geogaddi");
167
168 str02 = "boards";
169 const char* c_ptr1 = str01.c_str();
170 const char* c_ptr2 = str01.c_str() + 8;
171 str02.replace(str02.begin(), str02.end(), c_ptr1, c_ptr2);
172 VERIFY(str02 == "geogaddi");
173
174 str02 = "boards";
175 char* ptr1 = &*str01.begin();
176 char* ptr2 = &*str01.end();
177 str02.replace(str02.begin(), str02.end(), ptr1, ptr2);
178 VERIFY(str02 == "geogaddi");
179 }
180
181 // We wrongly used __n1 instead of __foldn1 in the length_error
182 // check at the beginning of replace(__pos, __n1, __s, __n2)
183 void
test05()184 test05()
185 {
186 bool test = true;
187 std::string str01 = "londinium";
188 std::string str02 = "cydonia";
189
190 str01.replace(0, 20, str02.c_str(), 3);
191 VERIFY(str01 == "cyd");
192 }
193
main()194 int main()
195 {
196 test01();
197 test02();
198 test03();
199 test04();
200 test05();
201 return 0;
202 }
203