]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/iostreams/test/regex_filter_test.cpp
1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2004-2007 Jonathan Turkanis
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
6 // See http://www.boost.org/libs/iostreams for documentation.
9 #include <boost/iostreams/device/file.hpp>
10 #include <boost/iostreams/filtering_stream.hpp>
11 #include <boost/iostreams/filter/regex.hpp>
12 #include <boost/test/test_tools.hpp>
13 #include <boost/test/unit_test.hpp>
14 #include "detail/temp_file.hpp"
15 #include "detail/verification.hpp"
18 using namespace boost::iostreams
;
19 using namespace boost::iostreams::test
;
20 using boost::unit_test::test_suite
;
22 struct replace_lower
{
23 std::string
operator() (const boost::match_results
<const char*>&)
24 { return "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
25 #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS
26 std::wstring
operator() (const boost::match_results
<const wchar_t*>&)
27 { return L
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
31 void regex_filter_test()
33 // Note: Given the basic stream and filter tests, two regex tests
34 // are probably sufficient: reading with a filter based on a function,
35 // and writing with a filter based on a format string.
40 boost::regex
match_lower("[a-z]+");
41 std::string fmt
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
44 // Note: the ifstream second is placed in a nested scope because
45 // closing and reopening a single ifstream failed for CW 9.4 on Windows.
47 // Test reading from a regex filter based on a function in chars.
49 first(boost::iostreams::regex_filter(match_lower
, replace_lower()));
50 first
.push(file_source(test
.name(), in_mode
));
52 ifstream
second(upper
.name().c_str(), in_mode
);
54 compare_streams_in_chars(first
, second
),
55 "failed reading from function-based regex_filter in chars"
60 // Test reading from a regex filter based on a function in chunks.
61 // (Also tests reusing the regex filter.)
62 first
.push(file_source(test
.name(), in_mode
));
64 ifstream
second(upper
.name().c_str(), in_mode
);
66 compare_streams_in_chunks(first
, second
),
67 "failed reading from function-based regex_filter in chunks"
73 // Note: the ifstream second is placed in a nested scope because
74 // closing and reopening a single ifstream failed for CW 9.4 on Windows.
76 // Test reading from a regex filter based on a format string in chars.
78 first(boost::iostreams::regex_filter(match_lower
, fmt
));
79 first
.push(file_source(test
.name(), in_mode
));
81 ifstream
second(upper
.name().c_str(), in_mode
);
83 compare_streams_in_chars(first
, second
),
84 "failed reading from format-string-based regex_filter in chars"
89 // Test reading from a regex filter based on a format string in chunks.
90 // (Also tests reusing the regex filter.)
91 first
.push(file_source(test
.name(), in_mode
));
93 ifstream
second(upper
.name().c_str(), in_mode
);
95 compare_streams_in_chars(first
, second
),
96 "failed reading from format-string-based regex_filter in chunks"
105 // Test writing to a regex filter based on a function in chars.
107 out(boost::iostreams::regex_filter(match_lower
, replace_lower()));
108 out
.push(file_sink(dest1
.name(), out_mode
));
109 write_data_in_chars(out
);
112 compare_files(dest1
.name(), upper
.name()),
113 "failed writing to function-based regex_filter in chars"
116 // Test writing to a regex filter based on a function in chunks.
117 // (Also tests reusing the regex filter.)
118 out
.push(file_sink(dest2
.name(), out_mode
));
119 write_data_in_chunks(out
);
122 compare_files(dest2
.name(), upper
.name()),
123 "failed writing to function-based regex_filter in chunks"
131 // Test writing to a regex filter based on a format string in chars.
133 out(boost::iostreams::regex_filter(match_lower
, fmt
));
134 out
.push(file_sink(dest1
.name(), out_mode
));
135 write_data_in_chars(out
);
138 compare_files(dest1
.name(), upper
.name()),
139 "failed writing to format-string-based regex_filter in chars"
142 // Test writing to a regex filter based on a format string in chunks.
143 // (Also tests reusing the regex filter.)
144 out
.push(file_sink(dest2
.name(), out_mode
));
145 write_data_in_chunks(out
);
148 compare_files(dest2
.name(), upper
.name()),
149 "failed writing to format-string-based regex_filter in chunks"
154 // Note: the ifstream second is placed in a nested scope because
155 // closing and reopening a single ifstream failed for CW 9.4 on Windows.
157 // Test reading from a regex filter with no matches; this checks that
158 // Ticket #1139 is fixed
159 boost::regex
match_xxx("xxx");
162 first(boost::iostreams::regex_filter(match_xxx
, replace_lower()));
163 first
.push(file_source(test
.name(), in_mode
));
165 ifstream
second(test2
.name().c_str(), in_mode
);
167 compare_streams_in_chars(first
, second
),
168 "failed reading from a regex filter with no matches"
174 #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS
176 void wregex_filter_test()
178 // Note: Given the basic stream and filter tests, two regex tests
179 // are probably sufficient: reading with a filter based on a function,
180 // and writing with a filter based on a format string.
183 uppercase_file upper
;
185 boost::wregex
match_lower(L
"[a-z]+");
186 std::wstring fmt
= L
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
189 // Note: the ifstream second is placed in a nested scope because
190 // closing and reopening a single ifstream failed for CW 9.4 on Windows.
192 // Test reading from a regex filter based on a function in chars.
194 first(boost::iostreams::wregex_filter(match_lower
, replace_lower()));
195 first
.push(wfile_source(test
.name(), in_mode
));
197 wifstream
second(upper
.name().c_str(), in_mode
);
199 compare_streams_in_chars(first
, second
),
200 "failed reading from function-based regex_filter in chars"
205 // Test reading from a regex filter based on a function in chunks.
206 // (Also tests reusing the regex filter.)
207 first
.push(wfile_source(test
.name(), in_mode
));
209 wifstream
second(upper
.name().c_str(), in_mode
);
211 compare_streams_in_chunks(first
, second
),
212 "failed reading from function-based regex_filter in chunks"
218 // Note: the ifstream second is placed in a nested scope because
219 // closing and reopening a single ifstream failed for CW 9.4 on Windows.
221 // Test reading from a regex filter based on a format string in chars.
223 first(boost::iostreams::wregex_filter(match_lower
, fmt
));
224 first
.push(wfile_source(test
.name(), in_mode
));
226 wifstream
second(upper
.name().c_str(), in_mode
);
228 compare_streams_in_chars(first
, second
),
229 "failed reading from format-string-based regex_filter in chars"
234 // Test reading from a regex filter based on a format string in chunks.
235 // (Also tests reusing the regex filter.)
236 first
.push(wfile_source(test
.name(), in_mode
));
238 wifstream
second(upper
.name().c_str(), in_mode
);
240 compare_streams_in_chars(first
, second
),
241 "failed reading from format-string-based regex_filter in chunks"
250 // Test writing to a regex filter based on a function in chars.
252 out(boost::iostreams::wregex_filter(match_lower
, replace_lower()));
253 out
.push(wfile_sink(dest1
.name(), out_mode
));
254 write_data_in_chars(out
);
257 compare_files(dest1
.name(), upper
.name()),
258 "failed writing to function-based regex_filter in chars"
261 // Test writing to a regex filter based on a function in chunks.
262 // (Also tests reusing the regex filter.)
263 out
.push(wfile_sink(dest2
.name(), out_mode
));
264 write_data_in_chunks(out
);
267 compare_files(dest2
.name(), upper
.name()),
268 "failed writing to function-based regex_filter in chunks"
276 // Test writing to a regex filter based on a format string in chars.
278 out(boost::iostreams::wregex_filter(match_lower
, fmt
));
279 out
.push(wfile_sink(dest1
.name(), out_mode
));
280 write_data_in_chars(out
);
283 compare_files(dest1
.name(), upper
.name()),
284 "failed writing to format-string-based regex_filter in chars"
287 // Test writing to a regex filter based on a format string in chunks.
288 // (Also tests reusing the regex filter.)
289 out
.push(wfile_sink(dest2
.name(), out_mode
));
290 write_data_in_chunks(out
);
293 compare_files(dest2
.name(), upper
.name()),
294 "failed writing to format-string-based regex_filter in chunks"
299 // Note: the ifstream second is placed in a nested scope because
300 // closing and reopening a single ifstream failed for CW 9.4 on Windows.
302 // Test reading from a regex filter with no matches; this checks that
303 // Ticket #1139 is fixed
304 boost::wregex
match_xxx(L
"xxx");
307 first(boost::iostreams::wregex_filter(match_xxx
, replace_lower()));
308 first
.push(wfile_source(test
.name(), in_mode
));
310 wifstream
second(test2
.name().c_str(), in_mode
);
312 compare_streams_in_chars(first
, second
),
313 "failed reading from a regex filter with no matches"
321 test_suite
* init_unit_test_suite(int, char* [])
323 test_suite
* test
= BOOST_TEST_SUITE("regex_filter test");
324 test
->add(BOOST_TEST_CASE(®ex_filter_test
));
325 #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS
326 test
->add(BOOST_TEST_CASE(&wregex_filter_test
));