bzip2_test.cpp 3.2 KB

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