]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/iostreams/test/tee_test.cpp
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.)
6 // See http://www.boost.org/libs/iostreams for documentation.
9 #include <boost/iostreams/compose.hpp>
10 #include <boost/iostreams/device/file.hpp>
11 #include <boost/iostreams/filtering_stream.hpp>
12 #include <boost/iostreams/tee.hpp>
13 #include <boost/test/test_tools.hpp>
14 #include <boost/test/unit_test.hpp>
15 #include "detail/closable.hpp"
16 #include "detail/operation_sequence.hpp"
17 #include "detail/temp_file.hpp"
18 #include "detail/verification.hpp"
21 using namespace boost
;
22 using namespace boost::iostreams
;
23 using namespace boost::iostreams::test
;
24 using boost::unit_test::test_suite
;
26 void read_write_test()
31 filtering_istream first
, second
;
32 first
.push(tee(file_sink(dest
.name(), out_mode
)));
33 first
.push(file_source(src1
.name(), in_mode
));
34 second
.push(file_source(src2
.name(), in_mode
));
35 compare_streams_in_chars(first
, second
); // ignore return value
38 compare_files(dest
.name(), src1
.name()),
39 "failed reading from a tee_filter in chars"
46 filtering_istream first
, second
;
47 first
.push(tee(file_sink(dest
.name(), out_mode
)));
48 first
.push(file_source(src1
.name(), in_mode
));
49 second
.push(file_source(src2
.name(), in_mode
));
50 compare_streams_in_chunks(first
, second
); // ignore return value
53 compare_files(dest
.name(), src1
.name()),
54 "failed reading from a tee_filter in chunks"
61 filtering_ostream out
;
62 out
.push(tee(file_sink(dest1
.name(), out_mode
)));
63 out
.push(file_sink(dest2
.name(), out_mode
));
64 write_data_in_chars(out
);
67 compare_files(dest1
.name(), dest2
.name()),
68 "failed writing to a tee_filter in chars"
75 filtering_ostream out
;
76 out
.push(tee(file_sink(dest1
.name(), out_mode
)));
77 out
.push(file_sink(dest2
.name(), out_mode
));
78 write_data_in_chunks(out
);
81 compare_files(dest1
.name(), dest2
.name()),
82 "failed writing to a tee_filter in chunks"
89 filtering_istream first
, second
;
90 first
.push( tee( file_source(src1
.name(), in_mode
),
91 file_sink(dest
.name(), out_mode
) ) );
92 second
.push(file_source(src2
.name(), in_mode
));
93 compare_streams_in_chars(first
, second
); // ignore return value
96 compare_files(dest
.name(), src1
.name()),
97 "failed reading from a tee_device in chars"
102 test_file src1
, src2
;
104 filtering_istream first
, second
;
105 first
.push( tee( file_source(src1
.name(), in_mode
),
106 file_sink(dest
.name(), out_mode
) ) );
107 second
.push(file_source(src2
.name(), in_mode
));
108 compare_streams_in_chunks(first
, second
); // ignore return value
111 compare_files(dest
.name(), src1
.name()),
112 "failed reading from a tee_device in chunks"
119 filtering_ostream out
;
120 out
.push( tee( file_sink(dest1
.name(), out_mode
),
121 file_sink(dest2
.name(), out_mode
) ) );
122 write_data_in_chars(out
);
125 compare_files(dest1
.name(), dest2
.name()),
126 "failed writing to a tee_device in chars"
133 filtering_ostream out
;
134 out
.push( tee( file_sink(dest1
.name(), out_mode
),
135 file_sink(dest2
.name(), out_mode
) ) );
136 write_data_in_chunks(out
);
139 compare_files(dest1
.name(), dest2
.name()),
140 "failed writing to a tee_device in chunks"
147 // Note: The implementation of tee_device closes the first
148 // sink before the second
150 // Tee two sinks (Borland <= 5.8.2 needs a little help compiling this case,
151 // but it executes the closing algorithm correctly)
153 operation_sequence seq
;
156 boost::iostreams::tee(
157 closable_device
<output
>(seq
.new_operation(1)),
159 #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x582))
164 >(seq
.new_operation(2))
167 BOOST_CHECK_NO_THROW(ch
.reset());
168 BOOST_CHECK_OPERATION_SEQUENCE(seq
);
171 // Tee two bidirectional devices
173 operation_sequence seq
;
176 boost::iostreams::tee(
177 closable_device
<bidirectional
>(
178 seq
.new_operation(1),
181 closable_device
<bidirectional
>(
182 seq
.new_operation(3),
187 BOOST_CHECK_NO_THROW(ch
.reset());
188 BOOST_CHECK_OPERATION_SEQUENCE(seq
);
191 // Tee two seekable devices
193 operation_sequence seq
;
196 boost::iostreams::tee(
197 closable_device
<seekable
>(seq
.new_operation(1)),
198 closable_device
<seekable
>(seq
.new_operation(2))
201 BOOST_CHECK_NO_THROW(ch
.reset());
202 BOOST_CHECK_OPERATION_SEQUENCE(seq
);
207 operation_sequence seq
;
209 ch
.push(boost::iostreams::tee(closable_device
<output
>(seq
.new_operation(1))));
210 ch
.push(closable_device
<output
>(seq
.new_operation(2)));
211 BOOST_CHECK_NO_THROW(ch
.reset());
212 BOOST_CHECK_OPERATION_SEQUENCE(seq
);
215 // Tee a bidirectional device
217 operation_sequence seq
;
220 boost::iostreams::tee(
221 closable_device
<bidirectional
>(
222 seq
.new_operation(1),
227 ch
.push(closable_device
<output
>(seq
.new_operation(3)));
228 BOOST_CHECK_NO_THROW(ch
.reset());
229 BOOST_CHECK_OPERATION_SEQUENCE(seq
);
232 // Tee a seekable device
234 operation_sequence seq
;
236 ch
.push(boost::iostreams::tee(closable_device
<seekable
>(seq
.new_operation(1))));
237 ch
.push(closable_device
<seekable
>(seq
.new_operation(2)));
238 BOOST_CHECK_NO_THROW(ch
.reset());
239 BOOST_CHECK_OPERATION_SEQUENCE(seq
);
248 filtering_ostream out
;
249 std::ofstream
stream1(dest1
.name().c_str(), out_mode
);
250 out
.push(tee(stream1
));
251 out
.push(file_sink(dest2
.name(), out_mode
));
252 write_data_in_chunks(out
);
254 compare_files(dest1
.name(), dest2
.name()),
255 "failed writing to a tee_device in chunks"
261 filtering_ostream out
;
262 std::ofstream
stream1(dest1
.name().c_str(), out_mode
);
263 out
.push( tee ( stream1
,
264 file_sink(dest2
.name(), out_mode
) ) );
265 write_data_in_chunks(out
);
267 compare_files(dest1
.name(), dest2
.name()),
268 "failed writing to a tee_device in chunks"
274 filtering_ostream out
;
275 std::ofstream
stream2(dest2
.name().c_str(), out_mode
);
276 out
.push( tee ( file_sink(dest1
.name(), out_mode
),
278 write_data_in_chunks(out
);
280 compare_files(dest1
.name(), dest2
.name()),
281 "failed writing to a tee_device in chunks"
287 filtering_ostream out
;
288 std::ofstream
stream1(dest1
.name().c_str(), out_mode
);
289 std::ofstream
stream2(dest2
.name().c_str(), out_mode
);
290 out
.push(tee(stream1
, stream2
));
291 write_data_in_chunks(out
);
293 compare_files(dest1
.name(), dest2
.name()),
294 "failed writing to a tee_device in chunks"
299 void tee_composite_test()
301 // This test is probably redundant, given the above test and the tests in
302 // compose_test.cpp, but it verifies that ticket #1002 is fixed
304 // Tee a composite sink with a sink
306 operation_sequence seq
;
309 boost::iostreams::tee(
310 boost::iostreams::compose(
311 closable_filter
<output
>(seq
.new_operation(1)),
312 closable_device
<output
>(seq
.new_operation(2))
314 closable_device
<output
>(seq
.new_operation(3))
317 BOOST_CHECK_NO_THROW(ch
.reset());
318 BOOST_CHECK_OPERATION_SEQUENCE(seq
);
321 // Tee a composite bidirectional device with a sink
323 operation_sequence seq
;
326 boost::iostreams::tee(
327 boost::iostreams::compose(
328 closable_filter
<bidirectional
>(
329 seq
.new_operation(2),
332 closable_device
<bidirectional
>(
333 seq
.new_operation(1),
337 closable_device
<output
>(seq
.new_operation(5))
340 BOOST_CHECK_NO_THROW(ch
.reset());
341 BOOST_CHECK_OPERATION_SEQUENCE(seq
);
344 // Tee a composite composite seekable device with a sink
346 operation_sequence seq
;
349 boost::iostreams::tee(
350 boost::iostreams::compose(
351 closable_filter
<seekable
>(seq
.new_operation(1)),
352 closable_device
<seekable
>(seq
.new_operation(2))
354 closable_device
<output
>(seq
.new_operation(3))
357 BOOST_CHECK_NO_THROW(ch
.reset());
358 BOOST_CHECK_OPERATION_SEQUENCE(seq
);
362 // Tee a composite sink
364 operation_sequence seq
;
367 boost::iostreams::tee(
368 boost::iostreams::compose(
369 closable_filter
<output
>(seq
.new_operation(1)),
370 closable_device
<output
>(seq
.new_operation(2))
374 ch
.push(closable_device
<output
>(seq
.new_operation(3)));
375 BOOST_CHECK_NO_THROW(ch
.reset());
376 BOOST_CHECK_OPERATION_SEQUENCE(seq
);
379 // Tee a composite bidirectional device with a sink
381 operation_sequence seq
;
384 boost::iostreams::tee(
385 boost::iostreams::compose(
386 closable_filter
<bidirectional
>(
387 seq
.new_operation(2),
390 closable_device
<bidirectional
>(
391 seq
.new_operation(1),
397 ch
.push(closable_device
<output
>(seq
.new_operation(5)));
398 BOOST_CHECK_NO_THROW(ch
.reset());
399 BOOST_CHECK_OPERATION_SEQUENCE(seq
);
402 // Tee a composite composite seekable device with a sink
404 operation_sequence seq
;
407 boost::iostreams::tee(
408 boost::iostreams::compose(
409 closable_filter
<seekable
>(seq
.new_operation(1)),
410 closable_device
<seekable
>(seq
.new_operation(2))
414 ch
.push(closable_device
<output
>(seq
.new_operation(3)));
415 BOOST_CHECK_NO_THROW(ch
.reset());
416 BOOST_CHECK_OPERATION_SEQUENCE(seq
);
420 test_suite
* init_unit_test_suite(int, char* [])
422 test_suite
* test
= BOOST_TEST_SUITE("tee test");
423 test
->add(BOOST_TEST_CASE(&read_write_test
));
424 test
->add(BOOST_TEST_CASE(&close_test
));