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
16using namespace std;
17using namespace boost;
18using namespace boost::iostreams;
19using namespace boost::iostreams::test;
20using boost::unit_test::test_suite;
21namespace io = boost::iostreams;
22
23template<class T> struct basic_test_alloc: std::allocator<T>
24{
25 basic_test_alloc()
26 {
27 }
28
29 basic_test_alloc( basic_test_alloc const& /*other*/ )
30 {
31 }
32
33 template<class U>
34 basic_test_alloc( basic_test_alloc<U> const & /*other*/ )
35 {
36 }
37
38 template<class U> struct rebind
39 {
40 typedef basic_test_alloc<U> other;
41 };
42};
43
44typedef basic_test_alloc<char> bzip2_alloc;
45
46void bzip2_test()
47{
48 text_sequence data;
49 BOOST_CHECK(
50 test_filter_pair( bzip2_compressor(),
51 bzip2_decompressor(),
52 std::string(data.begin(), data.end()) )
53 );
54 BOOST_CHECK(
55 test_filter_pair( basic_bzip2_compressor<bzip2_alloc>(),
56 basic_bzip2_decompressor<bzip2_alloc>(),
57 std::string(data.begin(), data.end()) )
58 );
59 BOOST_CHECK(
60 test_filter_pair( bzip2_compressor(),
61 bzip2_decompressor(),
62 std::string() )
63 );
64 {
65 filtering_istream strm;
66 strm.push( t: bzip2_compressor() );
67 strm.push( t: null_source() );
68 }
69 {
70 filtering_istream strm;
71 strm.push( t: bzip2_decompressor() );
72 strm.push( t: null_source() );
73 }
74}
75
76void multiple_member_test()
77{
78 const int num_sequences = 10;
79 text_sequence data;
80 std::vector<char> temp, dest;
81
82 // Write compressed data to temp, several times in succession
83 filtering_ostream out;
84 out.push(t: bzip2_compressor());
85 for(int i = 0; i < num_sequences; ++i)
86 {
87 out.push(t: io::back_inserter(cnt&: temp));
88 io::copy(src: make_iterator_range(r&: data), snk&: out);
89 }
90
91 // Read compressed data from temp into dest
92 filtering_istream in;
93 in.push(t: bzip2_decompressor());
94 in.push(t: array_source(&temp[0], temp.size()));
95 io::copy(src&: in, snk: io::back_inserter(cnt&: dest));
96
97 // Check that dest consists of as many copies of data as were provided
98 BOOST_REQUIRE_EQUAL(data.size() * num_sequences, dest.size());
99 for(int i = 0; i < num_sequences; ++i)
100 BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + i * dest.size() / num_sequences));
101
102 dest.clear();
103 io::copy(
104 src: array_source(&temp[0], temp.size()),
105 snk: io::compose(filter: bzip2_decompressor(), fod: io::back_inserter(cnt&: dest)));
106
107 // Check that dest consists of as many copies of data as were provided
108 BOOST_REQUIRE_EQUAL(data.size() * num_sequences, dest.size());
109 for(int i = 0; i < num_sequences; ++i)
110 BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + i * dest.size() / num_sequences));
111}
112
113test_suite* init_unit_test_suite(int, char* [])
114{
115 test_suite* test = BOOST_TEST_SUITE("bzip2 test");
116 test->add(BOOST_TEST_CASE(&bzip2_test));
117 test->add(BOOST_TEST_CASE(&multiple_member_test));
118 return test;
119}
120

source code of boost/libs/iostreams/test/bzip2_test.cpp