123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
- // (C) Copyright 2004-2007 Jonathan Turkanis
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
- // See http://www.boost.org/libs/iostreams for documentation.
- #include <fstream>
- #include <boost/iostreams/compose.hpp>
- #include <boost/iostreams/device/file.hpp>
- #include <boost/iostreams/filtering_stream.hpp>
- #include <boost/iostreams/tee.hpp>
- #include <boost/test/test_tools.hpp>
- #include <boost/test/unit_test.hpp>
- #include "detail/closable.hpp"
- #include "detail/operation_sequence.hpp"
- #include "detail/temp_file.hpp"
- #include "detail/verification.hpp"
- using namespace std;
- using namespace boost;
- using namespace boost::iostreams;
- using namespace boost::iostreams::test;
- using boost::unit_test::test_suite;
- void read_write_test()
- {
- {
- test_file src1, src2;
- temp_file dest;
- filtering_istream first, second;
- first.push(tee(file_sink(dest.name(), out_mode)));
- first.push(file_source(src1.name(), in_mode));
- second.push(file_source(src2.name(), in_mode));
- compare_streams_in_chars(first, second); // ignore return value
- first.reset();
- BOOST_CHECK_MESSAGE(
- compare_files(dest.name(), src1.name()),
- "failed reading from a tee_filter in chars"
- );
- }
- {
- test_file src1, src2;
- temp_file dest;
- filtering_istream first, second;
- first.push(tee(file_sink(dest.name(), out_mode)));
- first.push(file_source(src1.name(), in_mode));
- second.push(file_source(src2.name(), in_mode));
- compare_streams_in_chunks(first, second); // ignore return value
- first.reset();
- BOOST_CHECK_MESSAGE(
- compare_files(dest.name(), src1.name()),
- "failed reading from a tee_filter in chunks"
- );
- }
- {
- temp_file dest1;
- temp_file dest2;
- filtering_ostream out;
- out.push(tee(file_sink(dest1.name(), out_mode)));
- out.push(file_sink(dest2.name(), out_mode));
- write_data_in_chars(out);
- out.reset();
- BOOST_CHECK_MESSAGE(
- compare_files(dest1.name(), dest2.name()),
- "failed writing to a tee_filter in chars"
- );
- }
- {
- temp_file dest1;
- temp_file dest2;
- filtering_ostream out;
- out.push(tee(file_sink(dest1.name(), out_mode)));
- out.push(file_sink(dest2.name(), out_mode));
- write_data_in_chunks(out);
- out.reset();
- BOOST_CHECK_MESSAGE(
- compare_files(dest1.name(), dest2.name()),
- "failed writing to a tee_filter in chunks"
- );
- }
- {
- test_file src1, src2;
- temp_file dest;
- filtering_istream first, second;
- first.push( tee( file_source(src1.name(), in_mode),
- file_sink(dest.name(), out_mode) ) );
- second.push(file_source(src2.name(), in_mode));
- compare_streams_in_chars(first, second); // ignore return value
- first.reset();
- BOOST_CHECK_MESSAGE(
- compare_files(dest.name(), src1.name()),
- "failed reading from a tee_device in chars"
- );
- }
- {
- test_file src1, src2;
- temp_file dest;
- filtering_istream first, second;
- first.push( tee( file_source(src1.name(), in_mode),
- file_sink(dest.name(), out_mode) ) );
- second.push(file_source(src2.name(), in_mode));
- compare_streams_in_chunks(first, second); // ignore return value
- first.reset();
- BOOST_CHECK_MESSAGE(
- compare_files(dest.name(), src1.name()),
- "failed reading from a tee_device in chunks"
- );
- }
- {
- temp_file dest1;
- temp_file dest2;
- filtering_ostream out;
- out.push( tee( file_sink(dest1.name(), out_mode),
- file_sink(dest2.name(), out_mode) ) );
- write_data_in_chars(out);
- out.reset();
- BOOST_CHECK_MESSAGE(
- compare_files(dest1.name(), dest2.name()),
- "failed writing to a tee_device in chars"
- );
- }
- {
- temp_file dest1;
- temp_file dest2;
- filtering_ostream out;
- out.push( tee( file_sink(dest1.name(), out_mode),
- file_sink(dest2.name(), out_mode) ) );
- write_data_in_chunks(out);
- out.reset();
- BOOST_CHECK_MESSAGE(
- compare_files(dest1.name(), dest2.name()),
- "failed writing to a tee_device in chunks"
- );
- }
- }
- void close_test()
- {
- // Note: The implementation of tee_device closes the first
- // sink before the second
- // Tee two sinks (Borland <= 5.8.2 needs a little help compiling this case,
- // but it executes the closing algorithm correctly)
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- boost::iostreams::tee(
- closable_device<output>(seq.new_operation(1)),
- closable_device<
- #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
- borland_output
- #else
- output
- #endif
- >(seq.new_operation(2))
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Tee two bidirectional devices
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- boost::iostreams::tee(
- closable_device<bidirectional>(
- seq.new_operation(1),
- seq.new_operation(2)
- ),
- closable_device<bidirectional>(
- seq.new_operation(3),
- seq.new_operation(4)
- )
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Tee two seekable devices
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- boost::iostreams::tee(
- closable_device<seekable>(seq.new_operation(1)),
- closable_device<seekable>(seq.new_operation(2))
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Tee a sink
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(boost::iostreams::tee(closable_device<output>(seq.new_operation(1))));
- ch.push(closable_device<output>(seq.new_operation(2)));
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Tee a bidirectional device
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- boost::iostreams::tee(
- closable_device<bidirectional>(
- seq.new_operation(1),
- seq.new_operation(2)
- )
- )
- );
- ch.push(closable_device<output>(seq.new_operation(3)));
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Tee a seekable device
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(boost::iostreams::tee(closable_device<seekable>(seq.new_operation(1))));
- ch.push(closable_device<seekable>(seq.new_operation(2)));
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- }
- void test_std_io()
- {
- {
- temp_file dest1;
- temp_file dest2;
- filtering_ostream out;
- std::ofstream stream1(dest1.name().c_str(), out_mode);
- out.push(tee(stream1));
- out.push(file_sink(dest2.name(), out_mode));
- write_data_in_chunks(out);
- BOOST_CHECK_MESSAGE(
- compare_files(dest1.name(), dest2.name()),
- "failed writing to a tee_device in chunks"
- );
- }
- {
- temp_file dest1;
- temp_file dest2;
- filtering_ostream out;
- std::ofstream stream1(dest1.name().c_str(), out_mode);
- out.push( tee ( stream1,
- file_sink(dest2.name(), out_mode) ) );
- write_data_in_chunks(out);
- BOOST_CHECK_MESSAGE(
- compare_files(dest1.name(), dest2.name()),
- "failed writing to a tee_device in chunks"
- );
- }
- {
- temp_file dest1;
- temp_file dest2;
- filtering_ostream out;
- std::ofstream stream2(dest2.name().c_str(), out_mode);
- out.push( tee ( file_sink(dest1.name(), out_mode),
- stream2 ) );
- write_data_in_chunks(out);
- BOOST_CHECK_MESSAGE(
- compare_files(dest1.name(), dest2.name()),
- "failed writing to a tee_device in chunks"
- );
- }
- {
- temp_file dest1;
- temp_file dest2;
- filtering_ostream out;
- std::ofstream stream1(dest1.name().c_str(), out_mode);
- std::ofstream stream2(dest2.name().c_str(), out_mode);
- out.push(tee(stream1, stream2));
- write_data_in_chunks(out);
- BOOST_CHECK_MESSAGE(
- compare_files(dest1.name(), dest2.name()),
- "failed writing to a tee_device in chunks"
- );
- }
- }
- void tee_composite_test()
- {
- // This test is probably redundant, given the above test and the tests in
- // compose_test.cpp, but it verifies that ticket #1002 is fixed
- // Tee a composite sink with a sink
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- boost::iostreams::tee(
- boost::iostreams::compose(
- closable_filter<output>(seq.new_operation(1)),
- closable_device<output>(seq.new_operation(2))
- ),
- closable_device<output>(seq.new_operation(3))
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Tee a composite bidirectional device with a sink
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- boost::iostreams::tee(
- boost::iostreams::compose(
- closable_filter<bidirectional>(
- seq.new_operation(2),
- seq.new_operation(3)
- ),
- closable_device<bidirectional>(
- seq.new_operation(1),
- seq.new_operation(4)
- )
- ),
- closable_device<output>(seq.new_operation(5))
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Tee a composite composite seekable device with a sink
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- boost::iostreams::tee(
- boost::iostreams::compose(
- closable_filter<seekable>(seq.new_operation(1)),
- closable_device<seekable>(seq.new_operation(2))
- ),
- closable_device<output>(seq.new_operation(3))
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Tee a composite sink
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- boost::iostreams::tee(
- boost::iostreams::compose(
- closable_filter<output>(seq.new_operation(1)),
- closable_device<output>(seq.new_operation(2))
- )
- )
- );
- ch.push(closable_device<output>(seq.new_operation(3)));
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Tee a composite bidirectional device with a sink
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- boost::iostreams::tee(
- boost::iostreams::compose(
- closable_filter<bidirectional>(
- seq.new_operation(2),
- seq.new_operation(3)
- ),
- closable_device<bidirectional>(
- seq.new_operation(1),
- seq.new_operation(4)
- )
- )
- )
- );
- ch.push(closable_device<output>(seq.new_operation(5)));
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Tee a composite composite seekable device with a sink
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- boost::iostreams::tee(
- boost::iostreams::compose(
- closable_filter<seekable>(seq.new_operation(1)),
- closable_device<seekable>(seq.new_operation(2))
- )
- )
- );
- ch.push(closable_device<output>(seq.new_operation(3)));
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- }
- test_suite* init_unit_test_suite(int, char* [])
- {
- test_suite* test = BOOST_TEST_SUITE("tee test");
- test->add(BOOST_TEST_CASE(&read_write_test));
- test->add(BOOST_TEST_CASE(&close_test));
- return test;
- }
|