File: grep_test.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (282 lines) | stat: -rw-r--r-- 8,938 bytes parent folder | download | duplicates (19)
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
/*
 * Distributed under 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/libs/iostreams for documentation.

 * File:        libs/iostreams/test/grep_test.cpp
 * Date:        Mon May 26 17:48:45 MDT 2008
 * Copyright:   2008 CodeRage, LLC
 * Author:      Jonathan Turkanis
 * Contact:     turkanis at coderage dot com
 *
 * Tests the class template basic_grep_filter.
 */

#include <iostream>

#include <boost/config.hpp>  // Make sure ptrdiff_t is in std.
#include <algorithm>
#include <cstddef>           // std::ptrdiff_t
#include <string>
#include <boost/iostreams/compose.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/filter/grep.hpp>
#include <boost/iostreams/filter/test.hpp>
#include <boost/ref.hpp>
#include <boost/regex.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>

using namespace boost;
using namespace boost::iostreams;
namespace io = boost::iostreams;
using boost::unit_test::test_suite;

// List of addresses of US Appeals Courts, from uscourts.gov
std::string addresses =
    "John Joseph Moakley United States Courthouse, Suite 2500\n"
    "One Courthouse Way\n"
    "Boston, MA 02210-3002\n"
    "\n"
    "Thurgood Marshall United States Courthouse, 18th Floor\n"
    "40 Centre Street\n"
    "New York, NY 10007-1501\n"
    "\n"
    "21400 James A. Byrne United States Courthouse\n"
    "601 Market Street\n"
    "Philadelphia, PA 19106-1729\n"
    "\n"
    "Lewis F. Powell, Jr. United States Courthouse Annex, Suite 501\n"
    "1100 East Main Street\n"
    "Richmond, VA 23219-3525\n"
    "\n"
    "F. Edward Hebert Federal Bldg\n"
    "600 South Maestri Place\n"
    "New Orleans, LA 70130\n"
    "\n"
    "Bob Casey United States Courthouse, 1st Floor\n"
    "515 Rusk Street\n"
    "Houston, TX 77002-2600\n"
    "\n"
    "Potter Stewart United States Courthouse, Suite 540\n"
    "100 East Fifth Street\n"
    "Cincinnati, OH 45202\n"
    "\n"
    "2722 Everett McKinley Dirksen United States Courthouse\n"
    "219 South Dearborn Street\n"
    "Chicago, IL 60604\n";

// Lines containing "United States Courthouse"
std::string us_courthouse =
    "John Joseph Moakley United States Courthouse, Suite 2500\n"
    "Thurgood Marshall United States Courthouse, 18th Floor\n"
    "21400 James A. Byrne United States Courthouse\n"
    "Lewis F. Powell, Jr. United States Courthouse Annex, Suite 501\n"
    "Bob Casey United States Courthouse, 1st Floor\n"
    "Potter Stewart United States Courthouse, Suite 540\n"
    "2722 Everett McKinley Dirksen United States Courthouse\n";

// Lines not containing "United States Courthouse"
std::string us_courthouse_inv = 
    "One Courthouse Way\n"
    "Boston, MA 02210-3002\n"
    "\n"
    "40 Centre Street\n"
    "New York, NY 10007-1501\n"
    "\n"
    "601 Market Street\n"
    "Philadelphia, PA 19106-1729\n"
    "\n"
    "1100 East Main Street\n"
    "Richmond, VA 23219-3525\n"
    "\n"
    "F. Edward Hebert Federal Bldg\n"
    "600 South Maestri Place\n"
    "New Orleans, LA 70130\n"
    "\n"
    "515 Rusk Street\n"
    "Houston, TX 77002-2600\n"
    "\n"
    "100 East Fifth Street\n"
    "Cincinnati, OH 45202\n"
    "\n"
    "219 South Dearborn Street\n"
    "Chicago, IL 60604\n";

// Lines containing a state and zip
std::string state_and_zip =
    "Boston, MA 02210-3002\n"
    "New York, NY 10007-1501\n"
    "Philadelphia, PA 19106-1729\n"
    "Richmond, VA 23219-3525\n"
    "New Orleans, LA 70130\n"
    "Houston, TX 77002-2600\n"
    "Cincinnati, OH 45202\n"
    "Chicago, IL 60604\n";

// Lines not containing a state and zip
std::string state_and_zip_inv =
    "John Joseph Moakley United States Courthouse, Suite 2500\n"
    "One Courthouse Way\n"
    "\n"
    "Thurgood Marshall United States Courthouse, 18th Floor\n"
    "40 Centre Street\n"
    "\n"
    "21400 James A. Byrne United States Courthouse\n"
    "601 Market Street\n"
    "\n"
    "Lewis F. Powell, Jr. United States Courthouse Annex, Suite 501\n"
    "1100 East Main Street\n"
    "\n"
    "F. Edward Hebert Federal Bldg\n"
    "600 South Maestri Place\n"
    "\n"
    "Bob Casey United States Courthouse, 1st Floor\n"
    "515 Rusk Street\n"
    "\n"
    "Potter Stewart United States Courthouse, Suite 540\n"
    "100 East Fifth Street\n"
    "\n"
    "2722 Everett McKinley Dirksen United States Courthouse\n"
    "219 South Dearborn Street\n";

// Lines containing at least three words
std::string three_words =
    "John Joseph Moakley United States Courthouse, Suite 2500\n"
    "One Courthouse Way\n"
    "Thurgood Marshall United States Courthouse, 18th Floor\n"
    "40 Centre Street\n"
    "21400 James A. Byrne United States Courthouse\n"
    "601 Market Street\n"
    "Lewis F. Powell, Jr. United States Courthouse Annex, Suite 501\n"
    "1100 East Main Street\n"
    "F. Edward Hebert Federal Bldg\n"
    "600 South Maestri Place\n"
    "Bob Casey United States Courthouse, 1st Floor\n"
    "515 Rusk Street\n"
    "Potter Stewart United States Courthouse, Suite 540\n"
    "100 East Fifth Street\n"
    "2722 Everett McKinley Dirksen United States Courthouse\n"
    "219 South Dearborn Street\n";

// Lines containing exactly three words
std::string exactly_three_words =
    "One Courthouse Way\n"
    "40 Centre Street\n"
    "601 Market Street\n"
    "515 Rusk Street\n";

// Lines that don't contain exactly three words
std::string exactly_three_words_inv =
    "John Joseph Moakley United States Courthouse, Suite 2500\n"
    "Boston, MA 02210-3002\n"
    "\n"
    "Thurgood Marshall United States Courthouse, 18th Floor\n"
    "New York, NY 10007-1501\n"
    "\n"
    "21400 James A. Byrne United States Courthouse\n"
    "Philadelphia, PA 19106-1729\n"
    "\n"
    "Lewis F. Powell, Jr. United States Courthouse Annex, Suite 501\n"
    "1100 East Main Street\n"
    "Richmond, VA 23219-3525\n"
    "\n"
    "F. Edward Hebert Federal Bldg\n"
    "600 South Maestri Place\n"
    "New Orleans, LA 70130\n"
    "\n"
    "Bob Casey United States Courthouse, 1st Floor\n"
    "Houston, TX 77002-2600\n"
    "\n"
    "Potter Stewart United States Courthouse, Suite 540\n"
    "100 East Fifth Street\n"
    "Cincinnati, OH 45202\n"
    "\n"
    "2722 Everett McKinley Dirksen United States Courthouse\n"
    "219 South Dearborn Street\n"
    "Chicago, IL 60604\n";

void test_filter( grep_filter grep, 
                  const std::string& input, 
                  const std::string& output );

void grep_filter_test()
{
    regex match_us_courthouse("\\bUnited States Courthouse\\b");
    regex match_state_and_zip("\\b[A-Z]{2}\\s+[0-9]{5}(-[0-9]{4})?\\b");
    regex match_three_words("\\b\\w+\\s+\\w+\\s+\\w+\\b");
    regex_constants::match_flag_type match_default = 
        regex_constants::match_default;

    {
        grep_filter grep(match_us_courthouse);
        test_filter(grep, addresses, us_courthouse);
    }

    {
        grep_filter grep(match_us_courthouse, match_default, grep::invert);
        test_filter(grep, addresses, us_courthouse_inv);
    }

    {
        grep_filter grep(match_state_and_zip);
        test_filter(grep, addresses, state_and_zip);
    }

    {
        grep_filter grep(match_state_and_zip, match_default, grep::invert);
        test_filter(grep, addresses, state_and_zip_inv);
    }

    {
        grep_filter grep(match_three_words);
        test_filter(grep, addresses, three_words);
    }

    {
        grep_filter grep(match_three_words, match_default, grep::whole_line);
        test_filter(grep, addresses, exactly_three_words);
    }

    {
        int options = grep::whole_line | grep::invert;
        grep_filter grep(match_three_words, match_default, options);
        test_filter(grep, addresses, exactly_three_words_inv);
    }
}

void test_filter( grep_filter grep, 
                  const std::string& input, 
                  const std::string& output )
{
    // Count lines in output
    std::ptrdiff_t count = std::count(output.begin(), output.end(), '\n');

    // Test as input filter
    {
        array_source  src(input.data(), input.data() + input.size());
        std::string   dest;
        io::copy(compose(boost::ref(grep), src), io::back_inserter(dest));
        BOOST_CHECK(dest == output);
        BOOST_CHECK(grep.count() == count);
    }

    // Test as output filter
    {
        array_source  src(input.data(), input.data() + input.size());
        std::string   dest;
        io::copy(src, compose(boost::ref(grep), io::back_inserter(dest)));
        BOOST_CHECK(dest == output);
        BOOST_CHECK(grep.count() == count);
    }
}

test_suite* init_unit_test_suite(int, char* [])
{
    test_suite* test = BOOST_TEST_SUITE("grep_filter test");
    test->add(BOOST_TEST_CASE(&grep_filter_test));
    return test;
}