2 * Distributed under the Boost Software License, Version 1.0.(See accompanying
3 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
5 * See http://www.boost.org/libs/iostreams for documentation.
7 * File: libs/iostreams/test/grep_test.cpp
8 * Date: Mon May 26 17:48:45 MDT 2008
9 * Copyright: 2008 CodeRage, LLC
10 * Author: Jonathan Turkanis
11 * Contact: turkanis at coderage dot com
13 * Tests the class template basic_grep_filter.
18 #include <boost/config.hpp> // Make sure ptrdiff_t is in std.
20 #include <cstddef> // std::ptrdiff_t
22 #include <boost/iostreams/compose.hpp>
23 #include <boost/iostreams/copy.hpp>
24 #include <boost/iostreams/device/array.hpp>
25 #include <boost/iostreams/device/back_inserter.hpp>
26 #include <boost/iostreams/filter/grep.hpp>
27 #include <boost/iostreams/filter/test.hpp>
28 #include <boost/ref.hpp>
29 #include <boost/regex.hpp>
30 #include <boost/test/test_tools.hpp>
31 #include <boost/test/unit_test.hpp>
33 using namespace boost
;
34 using namespace boost::iostreams
;
35 namespace io
= boost::iostreams
;
36 using boost::unit_test::test_suite
;
38 // List of addresses of US Appeals Courts, from uscourts.gov
39 std::string addresses
=
40 "John Joseph Moakley United States Courthouse, Suite 2500\n"
41 "One Courthouse Way\n"
42 "Boston, MA 02210-3002\n"
44 "Thurgood Marshall United States Courthouse, 18th Floor\n"
46 "New York, NY 10007-1501\n"
48 "21400 James A. Byrne United States Courthouse\n"
50 "Philadelphia, PA 19106-1729\n"
52 "Lewis F. Powell, Jr. United States Courthouse Annex, Suite 501\n"
53 "1100 East Main Street\n"
54 "Richmond, VA 23219-3525\n"
56 "F. Edward Hebert Federal Bldg\n"
57 "600 South Maestri Place\n"
58 "New Orleans, LA 70130\n"
60 "Bob Casey United States Courthouse, 1st Floor\n"
62 "Houston, TX 77002-2600\n"
64 "Potter Stewart United States Courthouse, Suite 540\n"
65 "100 East Fifth Street\n"
66 "Cincinnati, OH 45202\n"
68 "2722 Everett McKinley Dirksen United States Courthouse\n"
69 "219 South Dearborn Street\n"
70 "Chicago, IL 60604\n";
72 // Lines containing "United States Courthouse"
73 std::string us_courthouse
=
74 "John Joseph Moakley United States Courthouse, Suite 2500\n"
75 "Thurgood Marshall United States Courthouse, 18th Floor\n"
76 "21400 James A. Byrne United States Courthouse\n"
77 "Lewis F. Powell, Jr. United States Courthouse Annex, Suite 501\n"
78 "Bob Casey United States Courthouse, 1st Floor\n"
79 "Potter Stewart United States Courthouse, Suite 540\n"
80 "2722 Everett McKinley Dirksen United States Courthouse\n";
82 // Lines not containing "United States Courthouse"
83 std::string us_courthouse_inv
=
84 "One Courthouse Way\n"
85 "Boston, MA 02210-3002\n"
88 "New York, NY 10007-1501\n"
91 "Philadelphia, PA 19106-1729\n"
93 "1100 East Main Street\n"
94 "Richmond, VA 23219-3525\n"
96 "F. Edward Hebert Federal Bldg\n"
97 "600 South Maestri Place\n"
98 "New Orleans, LA 70130\n"
101 "Houston, TX 77002-2600\n"
103 "100 East Fifth Street\n"
104 "Cincinnati, OH 45202\n"
106 "219 South Dearborn Street\n"
107 "Chicago, IL 60604\n";
109 // Lines containing a state and zip
110 std::string state_and_zip
=
111 "Boston, MA 02210-3002\n"
112 "New York, NY 10007-1501\n"
113 "Philadelphia, PA 19106-1729\n"
114 "Richmond, VA 23219-3525\n"
115 "New Orleans, LA 70130\n"
116 "Houston, TX 77002-2600\n"
117 "Cincinnati, OH 45202\n"
118 "Chicago, IL 60604\n";
120 // Lines not containing a state and zip
121 std::string state_and_zip_inv
=
122 "John Joseph Moakley United States Courthouse, Suite 2500\n"
123 "One Courthouse Way\n"
125 "Thurgood Marshall United States Courthouse, 18th Floor\n"
128 "21400 James A. Byrne United States Courthouse\n"
129 "601 Market Street\n"
131 "Lewis F. Powell, Jr. United States Courthouse Annex, Suite 501\n"
132 "1100 East Main Street\n"
134 "F. Edward Hebert Federal Bldg\n"
135 "600 South Maestri Place\n"
137 "Bob Casey United States Courthouse, 1st Floor\n"
140 "Potter Stewart United States Courthouse, Suite 540\n"
141 "100 East Fifth Street\n"
143 "2722 Everett McKinley Dirksen United States Courthouse\n"
144 "219 South Dearborn Street\n";
146 // Lines containing at least three words
147 std::string three_words
=
148 "John Joseph Moakley United States Courthouse, Suite 2500\n"
149 "One Courthouse Way\n"
150 "Thurgood Marshall United States Courthouse, 18th Floor\n"
152 "21400 James A. Byrne United States Courthouse\n"
153 "601 Market Street\n"
154 "Lewis F. Powell, Jr. United States Courthouse Annex, Suite 501\n"
155 "1100 East Main Street\n"
156 "F. Edward Hebert Federal Bldg\n"
157 "600 South Maestri Place\n"
158 "Bob Casey United States Courthouse, 1st Floor\n"
160 "Potter Stewart United States Courthouse, Suite 540\n"
161 "100 East Fifth Street\n"
162 "2722 Everett McKinley Dirksen United States Courthouse\n"
163 "219 South Dearborn Street\n";
165 // Lines containing exactly three words
166 std::string exactly_three_words
=
167 "One Courthouse Way\n"
169 "601 Market Street\n"
172 // Lines that don't contain exactly three words
173 std::string exactly_three_words_inv
=
174 "John Joseph Moakley United States Courthouse, Suite 2500\n"
175 "Boston, MA 02210-3002\n"
177 "Thurgood Marshall United States Courthouse, 18th Floor\n"
178 "New York, NY 10007-1501\n"
180 "21400 James A. Byrne United States Courthouse\n"
181 "Philadelphia, PA 19106-1729\n"
183 "Lewis F. Powell, Jr. United States Courthouse Annex, Suite 501\n"
184 "1100 East Main Street\n"
185 "Richmond, VA 23219-3525\n"
187 "F. Edward Hebert Federal Bldg\n"
188 "600 South Maestri Place\n"
189 "New Orleans, LA 70130\n"
191 "Bob Casey United States Courthouse, 1st Floor\n"
192 "Houston, TX 77002-2600\n"
194 "Potter Stewart United States Courthouse, Suite 540\n"
195 "100 East Fifth Street\n"
196 "Cincinnati, OH 45202\n"
198 "2722 Everett McKinley Dirksen United States Courthouse\n"
199 "219 South Dearborn Street\n"
200 "Chicago, IL 60604\n";
202 void test_filter( grep_filter grep
,
203 const std::string
& input
,
204 const std::string
& output
);
206 void grep_filter_test()
208 regex
match_us_courthouse("\\bUnited States Courthouse\\b");
209 regex
match_state_and_zip("\\b[A-Z]{2}\\s+[0-9]{5}(-[0-9]{4})?\\b");
210 regex
match_three_words("\\b\\w+\\s+\\w+\\s+\\w+\\b");
211 regex_constants::match_flag_type match_default
=
212 regex_constants::match_default
;
215 grep_filter
grep(match_us_courthouse
);
216 test_filter(grep
, addresses
, us_courthouse
);
220 grep_filter
grep(match_us_courthouse
, match_default
, grep::invert
);
221 test_filter(grep
, addresses
, us_courthouse_inv
);
225 grep_filter
grep(match_state_and_zip
);
226 test_filter(grep
, addresses
, state_and_zip
);
230 grep_filter
grep(match_state_and_zip
, match_default
, grep::invert
);
231 test_filter(grep
, addresses
, state_and_zip_inv
);
235 grep_filter
grep(match_three_words
);
236 test_filter(grep
, addresses
, three_words
);
240 grep_filter
grep(match_three_words
, match_default
, grep::whole_line
);
241 test_filter(grep
, addresses
, exactly_three_words
);
245 int options
= grep::whole_line
| grep::invert
;
246 grep_filter
grep(match_three_words
, match_default
, options
);
247 test_filter(grep
, addresses
, exactly_three_words_inv
);
251 void test_filter( grep_filter grep
,
252 const std::string
& input
,
253 const std::string
& output
)
255 // Count lines in output
256 std::ptrdiff_t count
= std::count(output
.begin(), output
.end(), '\n');
258 // Test as input filter
260 array_source
src(input
.data(), input
.data() + input
.size());
262 io::copy(compose(boost::ref(grep
), src
), io::back_inserter(dest
));
263 BOOST_CHECK(dest
== output
);
264 BOOST_CHECK(grep
.count() == count
);
267 // Test as output filter
269 array_source
src(input
.data(), input
.data() + input
.size());
271 io::copy(src
, compose(boost::ref(grep
), io::back_inserter(dest
)));
272 BOOST_CHECK(dest
== output
);
273 BOOST_CHECK(grep
.count() == count
);
277 test_suite
* init_unit_test_suite(int, char* [])
279 test_suite
* test
= BOOST_TEST_SUITE("grep_filter test");
280 test
->add(BOOST_TEST_CASE(&grep_filter_test
));