1 | // (C) Copyright Jonathan Turkanis 2004 |
---|
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.) |
---|
4 | |
---|
5 | // See http://www.boost.org/libs/iostreams for documentation. |
---|
6 | |
---|
7 | #include <cctype> |
---|
8 | #include <boost/iostreams/copy.hpp> |
---|
9 | #include <boost/iostreams/device/file.hpp> |
---|
10 | #include <boost/iostreams/filter/line.hpp> |
---|
11 | #include <boost/iostreams/filtering_stream.hpp> |
---|
12 | #include <boost/test/test_tools.hpp> |
---|
13 | #include <boost/test/unit_test.hpp> |
---|
14 | #include "detail/constants.hpp" |
---|
15 | #include "detail/filters.hpp" |
---|
16 | #include "detail/temp_file.hpp" |
---|
17 | #include "detail/verification.hpp" |
---|
18 | |
---|
19 | // Must come last. |
---|
20 | #include <boost/iostreams/detail/config/disable_warnings.hpp> // BCC 5.x. |
---|
21 | |
---|
22 | using namespace std; |
---|
23 | using namespace boost; |
---|
24 | using namespace boost::iostreams; |
---|
25 | using namespace boost::iostreams::test; |
---|
26 | using boost::unit_test::test_suite; |
---|
27 | |
---|
28 | struct toupper_line_filter : line_filter { |
---|
29 | std::string do_filter(const std::string& line) |
---|
30 | { |
---|
31 | std::string result(line); |
---|
32 | for ( std::string::size_type z = 0, len = line.size(); |
---|
33 | z < len; |
---|
34 | ++z ) |
---|
35 | { |
---|
36 | result[z] = std::toupper((unsigned char) result[z]); |
---|
37 | } |
---|
38 | return result; |
---|
39 | } |
---|
40 | }; |
---|
41 | |
---|
42 | bool compare_streams_in_lines(std::istream& first, std::istream& second) |
---|
43 | { |
---|
44 | do { |
---|
45 | std::string line_one; |
---|
46 | std::string line_two; |
---|
47 | std::getline(first, line_one); |
---|
48 | std::getline(second, line_two); |
---|
49 | if (line_one != line_two || first.eof() != second.eof()) |
---|
50 | return false; |
---|
51 | } while (!first.eof()); |
---|
52 | return true; |
---|
53 | } |
---|
54 | |
---|
55 | void read_line_filter() |
---|
56 | { |
---|
57 | test_file src; |
---|
58 | uppercase_file upper; |
---|
59 | filtering_istream first; |
---|
60 | first.push(toupper_line_filter()); |
---|
61 | first.push(file_source(src.name(), in_mode)); |
---|
62 | ifstream second(upper.name().c_str(), in_mode); |
---|
63 | BOOST_CHECK_MESSAGE( |
---|
64 | compare_streams_in_lines(first, second), |
---|
65 | "failed reading from a line_filter" |
---|
66 | ); |
---|
67 | } |
---|
68 | |
---|
69 | void write_line_filter() |
---|
70 | { |
---|
71 | test_file data; |
---|
72 | temp_file dest; |
---|
73 | uppercase_file upper; |
---|
74 | |
---|
75 | filtering_ostream out; |
---|
76 | out.push(toupper_line_filter()); |
---|
77 | out.push(file_sink(dest.name(), out_mode)); |
---|
78 | copy(file_source(data.name(), in_mode), out); |
---|
79 | out.reset(); |
---|
80 | |
---|
81 | ifstream first(dest.name().c_str()); |
---|
82 | ifstream second(upper.name().c_str()); |
---|
83 | BOOST_CHECK_MESSAGE( |
---|
84 | compare_streams_in_lines(first, second), |
---|
85 | "failed writing to a line_filter" |
---|
86 | ); |
---|
87 | } |
---|
88 | |
---|
89 | test_suite* init_unit_test_suite(int, char* []) |
---|
90 | { |
---|
91 | test_suite* test = BOOST_TEST_SUITE("line_filter test"); |
---|
92 | test->add(BOOST_TEST_CASE(&read_line_filter)); |
---|
93 | test->add(BOOST_TEST_CASE(&write_line_filter)); |
---|
94 | return test; |
---|
95 | } |
---|
96 | |
---|
97 | #include <boost/iostreams/detail/config/enable_warnings.hpp> // BCC 5.x. |
---|
98 | |
---|