1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
// Boost string_algo library substr_test.cpp file ------------------//
// Copyright Pavol Droba 2002-2003. Use, modification and
// distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org for updates, documentation, and revision history.
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/erase.hpp>
#include <boost/algorithm/string/std/list_traits.hpp>
#include <boost/algorithm/string/std/string_traits.hpp>
#include <boost/algorithm/string/finder.hpp>
#include <boost/algorithm/string/formatter.hpp>
#include <boost/algorithm/string/classification.hpp>
// Include unit test framework
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <string>
#include <vector>
#include <list>
#include <iostream>
// equals predicate is used for result comparison
#include <boost/algorithm/string/predicate.hpp>
#include <boost/test/test_tools.hpp>
using namespace std;
using namespace boost;
void sequence_traits_test()
{
// basic_string traits
BOOST_CHECK( boost::algorithm::has_native_replace<string>::value );
BOOST_CHECK( !boost::algorithm::has_stable_iterators<string>::value );
BOOST_CHECK( !boost::algorithm::has_const_time_insert<string>::value );
BOOST_CHECK( !boost::algorithm::has_const_time_erase<string>::value );
// vector traits
BOOST_CHECK( !boost::algorithm::has_native_replace< vector<char> >::value );
BOOST_CHECK( !boost::algorithm::has_stable_iterators< vector<char> >::value );
BOOST_CHECK( !boost::algorithm::has_const_time_insert< vector<char> >::value );
BOOST_CHECK( !boost::algorithm::has_const_time_erase< vector<char> >::value );
// list traits
BOOST_CHECK( !boost::algorithm::has_native_replace< list<char> >::value );
BOOST_CHECK( boost::algorithm::has_stable_iterators< list<char> >::value );
BOOST_CHECK( boost::algorithm::has_const_time_insert< list<char> >::value );
BOOST_CHECK( boost::algorithm::has_const_time_erase< list<char> >::value );
}
// Combine tests for all variants of the algorithm
#define C_ ,
#define TEST_ALGO( Algo, Input, Params, Output ) \
{\
BOOST_TEST_CHECKPOINT( #Algo " - Copy" );\
\
string str1(Input);\
\
/* Copy test */ \
BOOST_CHECK( Algo##_copy( str1, Params )==Output );\
\
BOOST_TEST_CHECKPOINT( #Algo " - Iterator" );\
/* Iterator test */\
string strout;\
Algo##_copy( back_inserter(strout), str1, Params );\
BOOST_CHECK( strout==Output ); \
\
/* In-place test */\
vector<char> vec1( str1.begin(), str1.end() );\
list<char> list1( str1.begin(), str1.end() );\
\
BOOST_TEST_CHECKPOINT( #Algo " - Inplace(string)" );\
Algo( str1, Params ); \
BOOST_CHECK( equals( str1, Output ) ); \
\
BOOST_TEST_CHECKPOINT( #Algo " - Inplace(vector)" );\
Algo( vec1, Params ); \
BOOST_CHECK( equals( vec1, Output ) );\
\
BOOST_TEST_CHECKPOINT( #Algo " - Inplace(list)" );\
Algo( list1, Params ); \
BOOST_CHECK( equals( list1, Output ) );\
}
void replace_first_test()
{
// replace first
TEST_ALGO( replace_first, "1abc3abc2", string("abc") C_ string("YYY"), string("1YYY3abc2") );
TEST_ALGO( ireplace_first, "1AbC3abc2", "aBc" C_ "YYY", string("1YYY3abc2") );
TEST_ALGO( replace_first, "1abc3abc2", string("abc") C_ string("Z"), string("1Z3abc2") );
TEST_ALGO( replace_first, "1abc3abc2", string("abc") C_ string("XXXX"), string("1XXXX3abc2") );
TEST_ALGO( replace_first, "1abc3abc2", string("") C_ string("XXXX"), string("1abc3abc2") );
TEST_ALGO( replace_first, "1abc3abc2", "" C_ "XXXX", string("1abc3abc2") );
TEST_ALGO( replace_first, "", string("") C_ string("XXXX"), string("") );
TEST_ALGO( erase_first, "1abc3abc2", string("abc"), string("13abc2") );
TEST_ALGO( ierase_first, "1aBc3abc2", "abC", "13abc2" );
TEST_ALGO( erase_first, "1abc3abc2", "abc", "13abc2" );
TEST_ALGO( erase_first, "1abc3abc2", string(""), string("1abc3abc2") );
TEST_ALGO( erase_first, "", string("abc"), string("") );
}
void replace_last_test()
{
// replace last
TEST_ALGO( replace_last, "1abc3abc2", string("abc") C_ string("YYY"), string("1abc3YYY2") );
TEST_ALGO( ireplace_last, "1abc3AbC2", "aBc" C_ "YYY", string("1abc3YYY2") );
TEST_ALGO( replace_last, "1abc3abc2", string("abc") C_ string("Z"), string("1abc3Z2") );
TEST_ALGO( replace_last, "1abc3abc2", string("abc") C_ string("XXXX"), string("1abc3XXXX2") );
TEST_ALGO( replace_last, "1abc3abc2", "abc" C_ "XXXX", string("1abc3XXXX2") );
TEST_ALGO( replace_last, "", string("") C_ string("XXXX"), string("") );
TEST_ALGO( erase_last, "1abc3abc2", string("abc"), string("1abc32") );
TEST_ALGO( ierase_last, "1aBc3aBc2", "ABC", string("1aBc32") );
TEST_ALGO( erase_last, "1abc3abc2", "abc", string("1abc32") );
TEST_ALGO( erase_last, "1abc3abc2", string(""), string("1abc3abc2") );
TEST_ALGO( erase_last, "", string("abc"), string("") );
}
void replace_all_test()
{
// replace all
TEST_ALGO( replace_all, "1abc3abc2", string("abc") C_ string("YYY"), string("1YYY3YYY2") );
TEST_ALGO( replace_all, string("1abc3abc2"), "/" C_ "\\", string("1abc3abc2") );
TEST_ALGO( ireplace_all, "1aBc3AbC2", "abC" C_ "YYY", string("1YYY3YYY2") );
TEST_ALGO( replace_all, "1abc3abc2", string("abc") C_ string("Z"), string("1Z3Z2") );
TEST_ALGO( replace_all, "1abc3abc2", string("abc") C_ string("XXXX"), string("1XXXX3XXXX2") );
TEST_ALGO( replace_all, "1abc3abc2", "abc" C_ "XXXX", string("1XXXX3XXXX2") );
TEST_ALGO( replace_all, "", string("") C_ string("XXXX"), string("") );
TEST_ALGO( erase_all, "1abc3abc2", string("abc"), string("132") );
TEST_ALGO( ierase_all, "1aBc3aBc2", "aBC", string("132") );
TEST_ALGO( erase_all, "1abc3abc2", "abc", string("132") );
TEST_ALGO( erase_all, "1abc3abc2", string(""), string("1abc3abc2") );
TEST_ALGO( erase_all, "", string("abc"), string("") );
}
void replace_nth_test()
{
// replace nth
TEST_ALGO( replace_nth, "1abc3abc2", string("abc") C_ 0 C_ string("YYY"), string("1YYY3abc2") );
TEST_ALGO( replace_nth, "1abc3abc2", string("abc") C_ -1 C_ string("YYY"), string("1abc3YYY2") );
TEST_ALGO( ireplace_nth, "1AbC3abc2", "aBc" C_ 0 C_ "YYY", string("1YYY3abc2") );
TEST_ALGO( ireplace_nth, "1AbC3abc2", "aBc" C_ -1 C_ "YYY", string("1AbC3YYY2") );
TEST_ALGO( replace_nth, "1abc3abc2", string("abc") C_ 0 C_ string("Z"), string("1Z3abc2") );
TEST_ALGO( replace_nth, "1abc3abc2", string("abc") C_ 0 C_ string("XXXX"), string("1XXXX3abc2") );
TEST_ALGO( replace_nth, "1abc3abc2", "abc" C_ 0 C_ "XXXX", string("1XXXX3abc2") );
TEST_ALGO( replace_nth, "1abc3abc2", "abc" C_ 3 C_ "XXXX", string("1abc3abc2") );
TEST_ALGO( replace_nth, "1abc3abc2", "abc" C_ -3 C_ "XXXX", string("1abc3abc2") );
TEST_ALGO( replace_nth, "1abc3abc2", string("") C_ 0 C_ string("XXXX"), string("1abc3abc2") );
TEST_ALGO( replace_nth, "", string("") C_ 0 C_ string("XXXX"), string("") );
TEST_ALGO( replace_nth, "", string("") C_ -1 C_ string("XXXX"), string("") );
TEST_ALGO( erase_nth, "1abc3abc2", string("abc") C_ 0, string("13abc2") );
TEST_ALGO( erase_nth, "1abc3abc2", string("abc") C_ -1, string("1abc32") );
TEST_ALGO( erase_nth, "1abc3abc2", string("abc") C_ -3, string("1abc3abc2") );
TEST_ALGO( ierase_nth, "1aBc3aBc2", "ABC" C_ 0, string("13aBc2") );
TEST_ALGO( ierase_nth, "1aBc3aBc2", "ABC" C_ -1, string("1aBc32") );
TEST_ALGO( ierase_nth, "1aBc3aBc2", "ABC" C_ -3, string("1aBc3aBc2") );
TEST_ALGO( erase_nth, "1abc3abc2", "abc" C_ 0, string("13abc2") );
TEST_ALGO( erase_nth, "1abc3abc2", string("") C_ 0, string("1abc3abc2") );
TEST_ALGO( erase_nth, "", string("abc") C_ 0, string("") );
TEST_ALGO( erase_nth, "", string("abc") C_ -1, string("") );
TEST_ALGO( replace_nth, "1abc3abc2", string("abc") C_ 1 C_ string("YYY"), string("1abc3YYY2") );
TEST_ALGO( replace_nth, "1abc3abc2", string("abc") C_ 2 C_ string("YYY"), string("1abc3abc2") );
}
void replace_head_test()
{
// replace head
TEST_ALGO( replace_head, "abc3abc2", 3 C_ string("YYY"), string("YYY3abc2") );
TEST_ALGO( replace_head, "abc3abc2", -3 C_ string("YYY"), string("YYYbc2") );
TEST_ALGO( replace_head, "abc3abc2", 3 C_ "YYY", string("YYY3abc2") );
TEST_ALGO( replace_head, "abc", 3 C_ string("Z"), string("Z") );
TEST_ALGO( replace_head, "abc", 6 C_ string("XXXX"), string("XXXX") );
TEST_ALGO( replace_head, "abc", -6 C_ string("XXXX"), string("abc") );
TEST_ALGO( replace_head, "abc3abc2", 0 C_ string("XXXX"), string("abc3abc2") );
TEST_ALGO( replace_head, "", 4 C_ string("XXXX"), string("") );
TEST_ALGO( replace_head, "", -4 C_ string("XXXX"), string("") );
TEST_ALGO( erase_head, "abc3abc2", 3, string("3abc2") );
TEST_ALGO( erase_head, "abc3abc2", -3, string("bc2") );
TEST_ALGO( erase_head, "abc3abc2", 0, string("abc3abc2") );
TEST_ALGO( erase_head, "", 4, string("") );
TEST_ALGO( erase_head, "", -4, string("") );
}
void replace_tail_test()
{
// replace tail
TEST_ALGO( replace_tail, "abc3abc", 3 C_ string("YYY"), string("abc3YYY") );
TEST_ALGO( replace_tail, "abc3abc", -3 C_ "YYY", string("abcYYY") );
TEST_ALGO( replace_tail, "abc", 3 C_ string("Z"), string("Z") );
TEST_ALGO( replace_tail, "abc", 6 C_ string("XXXX"), string("XXXX") );
TEST_ALGO( replace_tail, "abc", -6 C_ string("XXXX"), string("abc") );
TEST_ALGO( replace_tail, "abc3abc", 0 C_ string("XXXX"), string("abc3abc") );
TEST_ALGO( replace_tail, "", 4 C_ string("XXXX"), string("") );
TEST_ALGO( replace_tail, "", -4 C_ string("XXXX"), string("") );
TEST_ALGO( erase_tail, "abc3abc", 3, string("abc3") );
TEST_ALGO( erase_tail, "abc3abc", -3, string("abc") );
TEST_ALGO( erase_tail, "abc3abc", 0, string("abc3abc") );
TEST_ALGO( erase_tail, "", 4, string("") );
TEST_ALGO( erase_tail, "", -4, string("") );
}
void replace_range_test()
{
// replace_range
{
BOOST_TEST_CHECKPOINT( "replace_range" );
string str1("1abc3abc2");
BOOST_CHECK(
replace_range_copy(
str1,
make_iterator_range(str1.begin()+1, str1.begin()+4),
string("XXX") )==string("1XXX3abc2") );
string strout;
replace_range_copy(
back_inserter( strout ),
str1,
make_iterator_range(str1.begin()+1, str1.begin()+4),
string("XXX") );
BOOST_CHECK( strout==string("1XXX3abc2") );
replace_range(
str1,
make_iterator_range(str1.begin()+1, str1.begin()+4),
string("XXX") );
BOOST_CHECK( str1==string("1XXX3abc2") );
}
// erase_range
{
BOOST_TEST_CHECKPOINT( "erase_range" );
string str1("1abc3abc2");
BOOST_CHECK(
erase_range_copy(
str1,
make_iterator_range(str1.begin()+1, str1.begin()+4))==string("13abc2") );
string strout;
erase_range_copy(
back_inserter( strout ),
str1,
make_iterator_range(str1.begin()+1, str1.begin()+4));
BOOST_CHECK( strout==string("13abc2") );
erase_range(
str1,
make_iterator_range(str1.begin()+1, str1.begin()+4));
BOOST_CHECK( str1==string("13abc2") );
}
}
void collection_comp_test()
{
// container traits compatibility tests
{
string strout;
replace_first_copy( back_inserter(strout), "1abc3abc2", "abc", "YYY" );
BOOST_CHECK( strout==string("1YYY3abc2") );
}
{
string strout;
replace_last_copy( back_inserter(strout), "1abc3abc2", "abc", "YYY" );
BOOST_CHECK( strout==string("1abc3YYY2") );
}
{
string strout;
replace_all_copy( back_inserter(strout), "1abc3abc2", "abc", "YYY" );
BOOST_CHECK( strout==string("1YYY3YYY2") );
}
{
string strout;
replace_nth_copy( back_inserter(strout), "1abc3abc2", "abc", 1, "YYY" );
BOOST_CHECK( strout==string("1abc3YYY2") );
}
{
string strout;
replace_head_copy( back_inserter(strout), "abc3abc2", 3 , "YYY" );
BOOST_CHECK( strout==string("YYY3abc2") );
}
{
string strout;
replace_tail_copy( back_inserter(strout), "abc3abc", 3 , "YYY" );
BOOST_CHECK( strout==string("abc3YYY") );
}
}
void dissect_format_test()
{
BOOST_CHECK(
find_format_all_copy(
string("aBc123Abc"),
first_finder("abc", is_iequal()),
dissect_formatter(token_finder(is_upper())))=="B123A");
BOOST_CHECK(
find_format_all_copy(
string("abc 123 abc"),
token_finder(is_space(), token_compress_on),
dissect_formatter(head_finder(1)))=="abc 123 abc");
}
BOOST_AUTO_TEST_CASE( test_main )
{
sequence_traits_test();
replace_first_test();
replace_last_test();
replace_all_test();
replace_nth_test();
replace_head_test();
replace_tail_test();
replace_range_test();
collection_comp_test();
dissect_format_test();
}
|