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