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 <string>
9 #include <boost/iostreams/filter/bzip2.hpp>
10 #include <boost/iostreams/filter/test.hpp>
11 #include <boost/iostreams/filtering_stream.hpp>
12 #include <boost/test/test_tools.hpp>
13 #include <boost/test/unit_test.hpp>
14 #include "detail/sequence.hpp"
15 
16 using namespace std;
17 using namespace boost;
18 using namespace boost::iostreams;
19 using namespace boost::iostreams::test;
20 using boost::unit_test::test_suite;
21 namespace io = boost::iostreams;
22 
23 struct bzip2_alloc : std::allocator<char> { };
24 
bzip2_test()25 void bzip2_test()
26 {
27     text_sequence data;
28     BOOST_CHECK(
29         test_filter_pair( bzip2_compressor(),
30                           bzip2_decompressor(),
31                           std::string(data.begin(), data.end()) )
32     );
33     BOOST_CHECK(
34         test_filter_pair( basic_bzip2_compressor<bzip2_alloc>(),
35                           basic_bzip2_decompressor<bzip2_alloc>(),
36                           std::string(data.begin(), data.end()) )
37     );
38     BOOST_CHECK(
39         test_filter_pair( bzip2_compressor(),
40                           bzip2_decompressor(),
41                           std::string() )
42     );
43     {
44         filtering_istream strm;
45         strm.push( bzip2_compressor() );
46         strm.push( null_source() );
47     }
48     {
49         filtering_istream strm;
50         strm.push( bzip2_decompressor() );
51         strm.push( null_source() );
52     }
53 }
54 
multiple_member_test()55 void multiple_member_test()
56 {
57     text_sequence      data;
58     std::vector<char>  temp, dest;
59 
60     // Write compressed data to temp, twice in succession
61     filtering_ostream out;
62     out.push(bzip2_compressor());
63     out.push(io::back_inserter(temp));
64     io::copy(make_iterator_range(data), out);
65     out.push(io::back_inserter(temp));
66     io::copy(make_iterator_range(data), out);
67 
68     // Read compressed data from temp into dest
69     filtering_istream in;
70     in.push(bzip2_decompressor());
71     in.push(array_source(&temp[0], temp.size()));
72     io::copy(in, io::back_inserter(dest));
73 
74     // Check that dest consists of two copies of data
75     BOOST_REQUIRE_EQUAL(data.size() * 2, dest.size());
76     BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin()));
77     BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + dest.size() / 2));
78 
79     dest.clear();
80     io::copy(
81         array_source(&temp[0], temp.size()),
82         io::compose(bzip2_decompressor(), io::back_inserter(dest)));
83 
84     // Check that dest consists of two copies of data
85     BOOST_REQUIRE_EQUAL(data.size() * 2, dest.size());
86     BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin()));
87     BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + dest.size() / 2));
88 }
89 
init_unit_test_suite(int,char * [])90 test_suite* init_unit_test_suite(int, char* [])
91 {
92     test_suite* test = BOOST_TEST_SUITE("bzip2 test");
93     test->add(BOOST_TEST_CASE(&bzip2_test));
94     test->add(BOOST_TEST_CASE(&multiple_member_test));
95     return test;
96 }
97