1 | // Boost string_algo library iterator_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/split.hpp> |
11 | #include <boost/algorithm/string/classification.hpp> |
12 | // equals predicate is used for result comparison |
13 | #include <boost/algorithm/string/predicate.hpp> |
14 | |
15 | // Include unit test framework |
16 | #define BOOST_TEST_MAIN |
17 | #include <boost/test/unit_test.hpp> |
18 | |
19 | #include <string> |
20 | #include <vector> |
21 | #include <list> |
22 | #include <iostream> |
23 | |
24 | #include <boost/test/test_tools.hpp> |
25 | |
26 | |
27 | using namespace std; |
28 | using namespace boost; |
29 | |
30 | template< typename T1, typename T2 > |
31 | void deep_compare( const T1& X, const T2& Y ) |
32 | { |
33 | BOOST_REQUIRE( X.size() == Y.size() ); |
34 | for( unsigned int nIndex=0; nIndex<X.size(); ++nIndex ) |
35 | { |
36 | BOOST_CHECK( equals( X[nIndex], Y[nIndex] ) ); |
37 | } |
38 | } |
39 | |
40 | void iterator_test() |
41 | { |
42 | string str1("xx-abc--xx-abb" ); |
43 | string str2("Xx-abc--xX-abb-xx" ); |
44 | string str3("xx" ); |
45 | string strempty("" ); |
46 | const char* pch1="xx-abc--xx-abb" ; |
47 | vector<string> tokens; |
48 | vector< vector<int> > vtokens; |
49 | |
50 | // find_all tests |
51 | find_all( |
52 | Result&: tokens, |
53 | Input&: pch1, |
54 | Search: "xx" ); |
55 | |
56 | BOOST_REQUIRE( tokens.size()==2 ); |
57 | BOOST_CHECK( tokens[0]==string("xx" ) ); |
58 | BOOST_CHECK( tokens[1]==string("xx" ) ); |
59 | |
60 | ifind_all( |
61 | Result&: tokens, |
62 | Input&: str2, |
63 | Search: "xx" ); |
64 | |
65 | BOOST_REQUIRE( tokens.size()==3 ); |
66 | BOOST_CHECK( tokens[0]==string("Xx" ) ); |
67 | BOOST_CHECK( tokens[1]==string("xX" ) ); |
68 | BOOST_CHECK( tokens[2]==string("xx" ) ); |
69 | |
70 | find_all( |
71 | Result&: tokens, |
72 | Input&: str1, |
73 | Search: "xx" ); |
74 | |
75 | BOOST_REQUIRE( tokens.size()==2 ); |
76 | BOOST_CHECK( tokens[0]==string("xx" ) ); |
77 | BOOST_CHECK( tokens[1]==string("xx" ) ); |
78 | |
79 | find_all( |
80 | Result&: vtokens, |
81 | Input&: str1, |
82 | Search: string("xx" ) ); |
83 | deep_compare( X: tokens, Y: vtokens ); |
84 | |
85 | // split tests |
86 | split( |
87 | Result&: tokens, |
88 | Input&: str2, |
89 | Pred: is_any_of(Set: "xX" ), |
90 | eCompress: token_compress_on ); |
91 | |
92 | BOOST_REQUIRE( tokens.size()==4 ); |
93 | BOOST_CHECK( tokens[0]==string("" ) ); |
94 | BOOST_CHECK( tokens[1]==string("-abc--" ) ); |
95 | BOOST_CHECK( tokens[2]==string("-abb-" ) ); |
96 | BOOST_CHECK( tokens[3]==string("" ) ); |
97 | |
98 | split( |
99 | Result&: tokens, |
100 | Input&: pch1, |
101 | Pred: is_any_of(Set: "x" ), |
102 | eCompress: token_compress_on ); |
103 | |
104 | BOOST_REQUIRE( tokens.size()==3 ); |
105 | BOOST_CHECK( tokens[0]==string("" ) ); |
106 | BOOST_CHECK( tokens[1]==string("-abc--" ) ); |
107 | BOOST_CHECK( tokens[2]==string("-abb" ) ); |
108 | |
109 | split( |
110 | Result&: vtokens, |
111 | Input&: str1, |
112 | Pred: is_any_of(Set: "x" ), |
113 | eCompress: token_compress_on ); |
114 | deep_compare( X: tokens, Y: vtokens ); |
115 | |
116 | split( |
117 | Result&: tokens, |
118 | Input&: str1, |
119 | Pred: is_punct(), |
120 | eCompress: token_compress_off ); |
121 | |
122 | BOOST_REQUIRE( tokens.size()==5 ); |
123 | BOOST_CHECK( tokens[0]==string("xx" ) ); |
124 | BOOST_CHECK( tokens[1]==string("abc" ) ); |
125 | BOOST_CHECK( tokens[2]==string("" ) ); |
126 | BOOST_CHECK( tokens[3]==string("xx" ) ); |
127 | BOOST_CHECK( tokens[4]==string("abb" ) ); |
128 | |
129 | split( |
130 | Result&: tokens, |
131 | Input&: str3, |
132 | Pred: is_any_of(Set: "," ), |
133 | eCompress: token_compress_off); |
134 | |
135 | BOOST_REQUIRE( tokens.size()==1 ); |
136 | BOOST_CHECK( tokens[0]==string("xx" ) ); |
137 | |
138 | split( |
139 | Result&: tokens, |
140 | Input&: strempty, |
141 | Pred: is_punct(), |
142 | eCompress: token_compress_off); |
143 | |
144 | BOOST_REQUIRE( tokens.size()==1 ); |
145 | BOOST_CHECK( tokens[0]==string("" ) ); |
146 | |
147 | |
148 | find_iterator<string::iterator> fiter=make_find_iterator(Collection&: str1, Finder: first_finder(Search: "xx" )); |
149 | find_iterator<string::iterator> fiter2; |
150 | |
151 | BOOST_CHECK(equals(*fiter, "xx" )); |
152 | ++fiter; |
153 | |
154 | fiter2 = fiter; |
155 | BOOST_CHECK(equals(*fiter, "xx" )); |
156 | BOOST_CHECK(equals(*fiter2, "xx" )); |
157 | |
158 | ++fiter; |
159 | BOOST_CHECK(fiter==find_iterator<string::iterator>()); |
160 | BOOST_CHECK(equals(*fiter2, "xx" )); |
161 | |
162 | ++fiter2; |
163 | BOOST_CHECK(fiter2==find_iterator<string::iterator>()); |
164 | |
165 | split_iterator<string::iterator> siter=make_split_iterator(Collection&: str1, Finder: token_finder(Pred: is_any_of(Set: "-" ), eCompress: token_compress_on)); |
166 | split_iterator<string::iterator> siter2; |
167 | BOOST_CHECK(equals(*siter, "xx" )); |
168 | ++siter; |
169 | |
170 | siter2 = siter; |
171 | BOOST_CHECK(equals(*siter, "abc" )); |
172 | BOOST_CHECK(equals(*siter2, "abc" )); |
173 | |
174 | ++siter; |
175 | BOOST_CHECK(equals(*siter, "xx" )); |
176 | BOOST_CHECK(equals(*siter2, "abc" )); |
177 | |
178 | ++siter; |
179 | BOOST_CHECK(equals(*siter, "abb" )); |
180 | ++siter; |
181 | BOOST_CHECK(siter==split_iterator<string::iterator>(siter)); |
182 | BOOST_CHECK(siter==split_iterator<string::iterator>()); |
183 | |
184 | // Make sure we work with forward iterators |
185 | // See bug #7989 |
186 | list<char> l1; |
187 | find_iterator<list<char>::iterator> liter=make_find_iterator(Collection&: l1, Finder: first_finder(Search: "xx" )); |
188 | } |
189 | |
190 | BOOST_AUTO_TEST_CASE( test_main ) |
191 | { |
192 | iterator_test(); |
193 | } |
194 | |