[29] | 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 <boost/iostreams/detail/buffer.hpp> |
---|
| 8 | #include <boost/iostreams/device/file.hpp> |
---|
| 9 | #include <boost/iostreams/filter/symmetric.hpp> |
---|
| 10 | #include <boost/iostreams/filter/test.hpp> |
---|
| 11 | #include <boost/test/test_tools.hpp> |
---|
| 12 | #include <boost/test/unit_test.hpp> |
---|
| 13 | #include "./detail/constants.hpp" |
---|
| 14 | #include "./detail/temp_file.hpp" |
---|
| 15 | #include "./detail/verification.hpp" |
---|
| 16 | |
---|
| 17 | // Must come last. |
---|
| 18 | #include <boost/iostreams/detail/config/disable_warnings.hpp> |
---|
| 19 | |
---|
| 20 | using namespace boost::iostreams; |
---|
| 21 | using namespace boost::iostreams::test; |
---|
| 22 | using boost::unit_test::test_suite; |
---|
| 23 | |
---|
| 24 | // Note: The filter is given an internal buffer -- unnecessary in this simple |
---|
| 25 | // case -- to stress test symmetric_filter. |
---|
| 26 | struct toupper_symmetric_filter_impl { |
---|
| 27 | typedef char char_type; |
---|
| 28 | explicit toupper_symmetric_filter_impl( |
---|
| 29 | std::streamsize buffer_size = |
---|
| 30 | default_filter_buffer_size |
---|
| 31 | ) |
---|
| 32 | : buf_(default_filter_buffer_size) |
---|
| 33 | { |
---|
| 34 | buf_.set(0, 0); |
---|
| 35 | } |
---|
| 36 | bool filter( const char*& src_begin, const char* src_end, |
---|
| 37 | char*& dest_begin, char* dest_end, bool /* flush */ ) |
---|
| 38 | { |
---|
| 39 | while ( can_read(src_begin, src_end) || |
---|
| 40 | can_write(dest_begin, dest_end) ) |
---|
| 41 | { |
---|
| 42 | if (can_read(src_begin, src_end)) |
---|
| 43 | read(src_begin, src_end); |
---|
| 44 | if (can_write(dest_begin, dest_end)) |
---|
| 45 | write(dest_begin, dest_end); |
---|
| 46 | } |
---|
| 47 | bool result = buf_.ptr() != buf_.eptr(); |
---|
| 48 | return result; |
---|
| 49 | } |
---|
| 50 | void close() { buf_.set(0, 0); } |
---|
| 51 | void read(const char*& src_begin, const char* src_end) |
---|
| 52 | { |
---|
| 53 | std::ptrdiff_t count = |
---|
| 54 | (std::min) ( src_end - src_begin, |
---|
| 55 | static_cast<std::ptrdiff_t>(buf_.size()) - |
---|
| 56 | (buf_.eptr() - buf_.data()) ); |
---|
| 57 | while (count-- > 0) |
---|
| 58 | *buf_.eptr()++ = std::toupper(*src_begin++); |
---|
| 59 | } |
---|
| 60 | void write(char*& dest_begin, char* dest_end) |
---|
| 61 | { |
---|
| 62 | std::ptrdiff_t count = |
---|
| 63 | (std::min) ( dest_end - dest_begin, |
---|
| 64 | buf_.eptr() - buf_.ptr() ); |
---|
| 65 | while (count-- > 0) |
---|
| 66 | *dest_begin++ = *buf_.ptr()++; |
---|
| 67 | if (buf_.ptr() == buf_.eptr()) |
---|
| 68 | buf_.set(0, 0); |
---|
| 69 | } |
---|
| 70 | bool can_read(const char*& src_begin, const char* src_end) |
---|
| 71 | { return src_begin != src_end && buf_.eptr() != buf_.end(); } |
---|
| 72 | bool can_write(char*& dest_begin, char* dest_end) |
---|
| 73 | { return dest_begin != dest_end && buf_.ptr() != buf_.eptr(); } |
---|
| 74 | boost::iostreams::detail::buffer<char> buf_; |
---|
| 75 | }; |
---|
| 76 | |
---|
| 77 | typedef symmetric_filter<toupper_symmetric_filter_impl> |
---|
| 78 | toupper_symmetric_filter; |
---|
| 79 | |
---|
| 80 | void read_symmetric_filter_test() |
---|
| 81 | { |
---|
| 82 | test_file test; |
---|
| 83 | uppercase_file upper; |
---|
| 84 | BOOST_CHECK( |
---|
| 85 | test_input_filter( toupper_symmetric_filter(default_filter_buffer_size), |
---|
| 86 | file_source(test.name(), in_mode), |
---|
| 87 | file_source(upper.name(), in_mode) ) |
---|
| 88 | ); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void write_symmetric_filter_test() |
---|
| 92 | { |
---|
| 93 | test_file test; |
---|
| 94 | uppercase_file upper; |
---|
| 95 | BOOST_CHECK( |
---|
| 96 | test_output_filter( toupper_symmetric_filter(default_filter_buffer_size), |
---|
| 97 | file_source(test.name(), in_mode), |
---|
| 98 | file_source(upper.name(), in_mode) ) |
---|
| 99 | ); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | test_suite* init_unit_test_suite(int, char* []) |
---|
| 103 | { |
---|
| 104 | test_suite* test = BOOST_TEST_SUITE("symmetric_filter test"); |
---|
| 105 | test->add(BOOST_TEST_CASE(&read_symmetric_filter_test)); |
---|
| 106 | test->add(BOOST_TEST_CASE(&write_symmetric_filter_test)); |
---|
| 107 | return test; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | #include <boost/iostreams/detail/config/enable_warnings.hpp> |
---|