| 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 <algorithm> // equal. |
| 9 | #include <fstream> |
| 10 | #include <boost/iostreams/device/back_inserter.hpp> |
| 11 | #include <boost/iostreams/device/file.hpp> |
| 12 | #include <boost/iostreams/device/null.hpp> |
| 13 | #include <boost/iostreams/filtering_stream.hpp> |
| 14 | #include <boost/iostreams/stream.hpp> |
| 15 | #include <boost/iostreams/operations.hpp> |
| 16 | #include <boost/test/test_tools.hpp> |
| 17 | #include <boost/test/unit_test.hpp> |
| 18 | #include "detail/filters.hpp" |
| 19 | #include "detail/temp_file.hpp" |
| 20 | #include "detail/verification.hpp" |
| 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 | void flush_test() |
| 29 | { |
| 30 | { |
| 31 | stream_buffer<null_sink> null; |
| 32 | null.open(t: null_sink()); |
| 33 | BOOST_CHECK_MESSAGE( |
| 34 | iostreams::flush(null), |
| 35 | "failed flushing stream_buffer" |
| 36 | ); |
| 37 | BOOST_CHECK_MESSAGE( |
| 38 | null.strict_sync(), |
| 39 | "failed strict-syncing stream_buffer with " |
| 40 | "non-flushable resource" |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | { |
| 45 | stream<null_sink> null; |
| 46 | null.open(t: null_sink()); |
| 47 | BOOST_CHECK_MESSAGE( |
| 48 | iostreams::flush(null), |
| 49 | "failed flushing stream" |
| 50 | ); |
| 51 | BOOST_CHECK_MESSAGE( |
| 52 | null.strict_sync(), |
| 53 | "failed strict-syncing stream with " |
| 54 | "non-flushable resource" |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | { |
| 59 | filtering_ostream null; |
| 60 | null.push(t: null_sink()); |
| 61 | BOOST_CHECK_MESSAGE( |
| 62 | iostreams::flush(null), |
| 63 | "failed flushing filtering_ostream" |
| 64 | ); |
| 65 | BOOST_CHECK_MESSAGE( |
| 66 | null.strict_sync(), |
| 67 | "failed strict-syncing filtering_ostream with " |
| 68 | "non-flushable resource" |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | { |
| 73 | filtering_ostream null; |
| 74 | null.push(t: tolower_filter()); |
| 75 | null.push(t: null_sink()); |
| 76 | BOOST_CHECK_MESSAGE( |
| 77 | iostreams::flush(null), |
| 78 | "failed flushing filtering_ostream with non-flushable filter" |
| 79 | ); |
| 80 | BOOST_CHECK_MESSAGE( |
| 81 | !null.strict_sync(), |
| 82 | "strict-syncing filtering_ostream with " |
| 83 | "non-flushable filter succeeded" |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | { |
| 88 | vector<char> dest1; |
| 89 | vector<char> dest2; |
| 90 | filtering_ostream out; |
| 91 | out.set_auto_close(false); |
| 92 | out.push(t: flushable_output_filter()); |
| 93 | |
| 94 | // Write to dest1. |
| 95 | out.push(t: iostreams::back_inserter(cnt&: dest1)); |
| 96 | write_data_in_chunks(os&: out); |
| 97 | out.flush(); |
| 98 | |
| 99 | // Write to dest2. |
| 100 | out.pop(); |
| 101 | out.push(t: iostreams::back_inserter(cnt&: dest2)); |
| 102 | write_data_in_chunks(os&: out); |
| 103 | out.flush(); |
| 104 | |
| 105 | BOOST_CHECK_MESSAGE( |
| 106 | dest1.size() == dest2.size() && |
| 107 | std::equal(dest1.begin(), dest1.end(), dest2.begin()), |
| 108 | "failed flush filtering_ostream with auto_close disabled" |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | { |
| 113 | vector<char> dest1; |
| 114 | vector<char> dest2; |
| 115 | filtering_ostream out; |
| 116 | out.set_auto_close(false); |
| 117 | out.push(t: flushable_output_filter()); |
| 118 | out.push(t: flushable_output_filter()); |
| 119 | |
| 120 | // Write to dest1. |
| 121 | out.push(t: iostreams::back_inserter(cnt&: dest1)); |
| 122 | write_data_in_chunks(os&: out); |
| 123 | out.flush(); |
| 124 | |
| 125 | // Write to dest2. |
| 126 | out.pop(); |
| 127 | out.push(t: iostreams::back_inserter(cnt&: dest2)); |
| 128 | write_data_in_chunks(os&: out); |
| 129 | out.flush(); |
| 130 | |
| 131 | BOOST_CHECK_MESSAGE( |
| 132 | dest1.size() == dest2.size() && |
| 133 | std::equal(dest1.begin(), dest1.end(), dest2.begin()), |
| 134 | "failed flush filtering_ostream with two flushable filters " |
| 135 | "with auto_close disabled" |
| 136 | ); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | test_suite* init_unit_test_suite(int, char* []) |
| 141 | { |
| 142 | test_suite* test = BOOST_TEST_SUITE("flush test" ); |
| 143 | test->add(BOOST_TEST_CASE(&flush_test)); |
| 144 | return test; |
| 145 | } |
| 146 | |