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 | |
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 | |
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 | |
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 | |
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 | |
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" , |
---|