123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- // (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 <cctype>
- #include <boost/iostreams/compose.hpp>
- #include <boost/iostreams/device/file.hpp>
- #include <boost/iostreams/filtering_stream.hpp>
- #include <boost/test/test_tools.hpp>
- #include <boost/test/unit_test.hpp>
- #include "detail/closable.hpp"
- #include "detail/operation_sequence.hpp"
- #include "detail/filters.hpp"
- #include "detail/temp_file.hpp"
- #include "detail/verification.hpp"
- // Must come last.
- #include <boost/iostreams/detail/config/disable_warnings.hpp> // BCC 5.x.
- using namespace std;
- using namespace boost::iostreams;
- using namespace boost::iostreams::test;
- using boost::unit_test::test_suite;
- namespace io = boost::iostreams;
- void read_composite()
- {
- test_file src1, src2;
- filtering_istream first, second;
- // Test composite device
- first.push(toupper_filter());
- first.push(padding_filter('a'));
- first.push(file_source(src1.name(), in_mode));
- second.push( compose( toupper_filter(),
- compose( padding_filter('a'),
- file_source(src1.name(), in_mode) ) ) );
- BOOST_CHECK_MESSAGE(
- compare_streams_in_chunks(first, second),
- "failed reading from a stdio_filter"
- );
- // Test composite filter
- first.reset();
- second.reset();
- first.push(toupper_filter());
- first.push(padding_filter('a'));
- first.push(file_source(src1.name(), in_mode));
- second.push( compose( compose( toupper_filter(),
- padding_filter('a') ),
- file_source(src1.name(), in_mode) ) );
- BOOST_CHECK_MESSAGE(
- compare_streams_in_chunks(first, second),
- "failed reading from a stdio_filter"
- );
- }
- void write_composite()
- {
- temp_file dest1, dest2;
- filtering_ostream out1, out2;
- // Test composite device
- out1.push(tolower_filter());
- out1.push(padding_filter('a'));
- out1.push(file_sink(dest1.name(), in_mode));
- out2.push( compose( tolower_filter(),
- compose( padding_filter('a'),
- file_sink(dest2.name(), in_mode) ) ) );
- write_data_in_chunks(out1);
- write_data_in_chunks(out2);
- out1.reset();
- out2.reset();
- {
- ifstream first(dest1.name().c_str());
- ifstream second(dest2.name().c_str());
- BOOST_CHECK_MESSAGE(
- compare_streams_in_chunks(first, second),
- "failed writing to a stdio_filter"
- );
- }
- // Test composite filter
- out1.push(tolower_filter());
- out1.push(padding_filter('a'));
- out1.push(file_sink(dest1.name(), in_mode));
- out2.push( compose( compose( tolower_filter(),
- padding_filter('a') ),
- file_sink(dest2.name(), in_mode) ) );
- write_data_in_chunks(out1);
- write_data_in_chunks(out2);
- out1.reset();
- out2.reset();
- {
- ifstream first(dest1.name().c_str());
- ifstream second(dest2.name().c_str());
- BOOST_CHECK_MESSAGE(
- compare_streams_in_chunks(first, second),
- "failed writing to a stdio_filter"
- );
- }
- }
- void close_composite_device()
- {
- // Compose an input filter with a source
- {
- operation_sequence seq;
- chain<input> ch;
- ch.push(
- io::compose(
- closable_filter<input>(seq.new_operation(2)),
- closable_device<input>(seq.new_operation(1))
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose a bidirectional filter with a source
- {
- operation_sequence seq;
- chain<input> ch;
- ch.push(
- io::compose(
- closable_filter<bidirectional>(
- seq.new_operation(2),
- seq.new_operation(3)
- ),
- closable_device<input>(seq.new_operation(1))
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose a seekable filter with a source
- {
- operation_sequence seq;
- chain<input> ch;
- ch.push(
- io::compose(
- closable_filter<seekable>(seq.new_operation(2)),
- closable_device<input>(seq.new_operation(1))
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose a dual-use filter with a source
- {
- operation_sequence seq;
- chain<input> ch;
- operation dummy;
- ch.push(
- io::compose(
- closable_filter<dual_use>(
- seq.new_operation(2),
- dummy
- ),
- closable_device<input>(seq.new_operation(1))
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose an output filter with a sink
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- io::compose(
- closable_filter<output>(seq.new_operation(1)),
- closable_device<output>(seq.new_operation(2))
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose a bidirectional filter with a sink
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- io::compose(
- closable_filter<bidirectional>(
- seq.new_operation(1),
- seq.new_operation(2)
- ),
- closable_device<output>(seq.new_operation(3))
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose a seekable filter with a sink
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- io::compose(
- closable_filter<seekable>(seq.new_operation(1)),
- closable_device<output>(seq.new_operation(2))
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose a dual-use filter with a sink
- {
- operation_sequence seq;
- chain<output> ch;
- operation dummy;
- ch.push(
- io::compose(
- closable_filter<dual_use>(
- dummy,
- seq.new_operation(1)
- ),
- closable_device<output>(seq.new_operation(2))
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose a bidirectional filter with a bidirectional device
- {
- operation_sequence seq;
- chain<bidirectional> ch;
- ch.push(
- io::compose(
- closable_filter<bidirectional>(
- seq.new_operation(2),
- seq.new_operation(3)
- ),
- closable_device<bidirectional>(
- seq.new_operation(1),
- seq.new_operation(4)
- )
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose a seekable filter with a seekable device
- {
- operation_sequence seq;
- chain<seekable> ch;
- ch.push(
- io::compose(
- closable_filter<seekable>(seq.new_operation(1)),
- closable_device<seekable>(seq.new_operation(2))
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- }
- void close_composite_filter()
- {
- // Compose two input filters
- {
- operation_sequence seq;
- chain<input> ch;
- ch.push(
- io::compose(
- closable_filter<input>(seq.new_operation(3)),
- closable_filter<input>(seq.new_operation(2))
- )
- );
- ch.push(closable_device<input>(seq.new_operation(1)));
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose a bidirectional filter with an input filter
- {
- operation_sequence seq;
- chain<input> ch;
- ch.push(
- io::compose(
- closable_filter<bidirectional>(
- seq.new_operation(3),
- seq.new_operation(4)
- ),
- closable_filter<input>(seq.new_operation(2))
- )
- );
- ch.push(closable_device<input>(seq.new_operation(1)));
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_MESSAGE(seq.is_success(), seq.message());
- }
- // Compose a seekable filter with an input filter
- {
- operation_sequence seq;
- chain<input> ch;
- ch.push(
- io::compose(
- closable_filter<seekable>(seq.new_operation(3)),
- closable_filter<input>(seq.new_operation(2))
- )
- );
- ch.push(closable_device<input>(seq.new_operation(1)));
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose a dual-use filter with an input filter
- {
- operation_sequence seq;
- chain<input> ch;
- operation dummy;
- ch.push(
- io::compose(
- closable_filter<dual_use>(
- seq.new_operation(3),
- dummy
- ),
- closable_filter<input>(seq.new_operation(2))
- )
- );
- ch.push(closable_device<input>(seq.new_operation(1)));
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose two output filters
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- io::compose(
- closable_filter<output>(seq.new_operation(1)),
- closable_filter<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);
- }
- // Compose a bidirectional filter with an output filter
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- io::compose(
- closable_filter<bidirectional>(
- seq.new_operation(1),
- seq.new_operation(2)
- ),
- closable_filter<output>(seq.new_operation(3))
- )
- );
- ch.push(closable_device<output>(seq.new_operation(4)));
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose a seekable filter with an output filter
- {
- operation_sequence seq;
- chain<output> ch;
- ch.push(
- io::compose(
- closable_filter<seekable>(seq.new_operation(1)),
- closable_filter<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);
- }
- // Compose a dual-use filter with an output filter
- {
- operation_sequence seq;
- chain<output> ch;
- operation dummy;
- ch.push(
- io::compose(
- closable_filter<dual_use>(
- dummy,
- seq.new_operation(1)
- ),
- closable_filter<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);
- }
- // Compose two bidirectional filters
- {
- operation_sequence seq;
- chain<bidirectional> ch;
- ch.push(
- io::compose(
- closable_filter<bidirectional>(
- seq.new_operation(3),
- seq.new_operation(4)
- ),
- closable_filter<bidirectional>(
- seq.new_operation(2),
- seq.new_operation(5)
- )
- )
- );
- ch.push(
- closable_device<bidirectional>(
- seq.new_operation(1),
- seq.new_operation(6)
- )
- );
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose two seekable filters
- {
- operation_sequence seq;
- chain<seekable> ch;
- ch.push(
- io::compose(
- closable_filter<seekable>(seq.new_operation(1)),
- closable_filter<seekable>(seq.new_operation(2))
- )
- );
- ch.push(closable_device<seekable>(seq.new_operation(3)));
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose two dual-use filters for input
- {
- operation_sequence seq;
- chain<input> ch;
- operation dummy;
- ch.push(
- io::compose(
- closable_filter<dual_use>(
- seq.new_operation(3),
- dummy
- ),
- closable_filter<dual_use>(
- seq.new_operation(2),
- dummy
- )
- )
- );
- ch.push(closable_device<input>(seq.new_operation(1)));
- BOOST_CHECK_NO_THROW(ch.reset());
- BOOST_CHECK_OPERATION_SEQUENCE(seq);
- }
- // Compose two dual-use filters for output
- {
- operation_sequence seq;
- chain<output> ch;
- operation dummy;
- ch.push(
- io::compose(
- closable_filter<dual_use>(
- dummy,
- seq.new_operation(1)
- ),
- closable_filter<dual_use>(
- dummy,
- 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("line_filter test");
- test->add(BOOST_TEST_CASE(&read_composite));
- test->add(BOOST_TEST_CASE(&write_composite));
- test->add(BOOST_TEST_CASE(&close_composite_device));
- test->add(BOOST_TEST_CASE(&close_composite_filter));
- return test;
- }
- #include <boost/iostreams/detail/config/enable_warnings.hpp> // BCC 5.x.
|