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