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 const int num_sequences = 10;
58 text_sequence data;
59 std::vector<char> temp, dest;
60
61 // Write compressed data to temp, several times in succession
62 filtering_ostream out;
63 out.push(bzip2_compressor());
64 for(int i = 0; i < num_sequences; ++i)
65 {
66 out.push(io::back_inserter(temp));
67 io::copy(make_iterator_range(data), out);
68 }
69
70 // Read compressed data from temp into dest
71 filtering_istream in;
72 in.push(bzip2_decompressor());
73 in.push(array_source(&temp[0], temp.size()));
74 io::copy(in, io::back_inserter(dest));
75
76 // Check that dest consists of as many copies of data as were provided
77 BOOST_REQUIRE_EQUAL(data.size() * num_sequences, dest.size());
78 for(int i = 0; i < num_sequences; ++i)
79 BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + i * dest.size() / num_sequences));
80
81 dest.clear();
82 io::copy(
83 array_source(&temp[0], temp.size()),
84 io::compose(bzip2_decompressor(), io::back_inserter(dest)));
85
86 // Check that dest consists of as many copies of data as were provided
87 BOOST_REQUIRE_EQUAL(data.size() * num_sequences, dest.size());
88 for(int i = 0; i < num_sequences; ++i)
89 BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + i * dest.size() / num_sequences));
90 }
91
init_unit_test_suite(int,char * [])92 test_suite* init_unit_test_suite(int, char* [])
93 {
94 test_suite* test = BOOST_TEST_SUITE("bzip2 test");
95 test->add(BOOST_TEST_CASE(&bzip2_test));
96 test->add(BOOST_TEST_CASE(&multiple_member_test));
97 return test;
98 }
99