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