zstd_test.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // (C) COPYRIGHT 2018 Reimar Döffinger
  2. // Based on zstd_test.cpp by:
  3. // (C) COPYRIGHT 2017 ARM Limited
  4. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  5. // (C) Copyright 2004-2007 Jonathan Turkanis
  6. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  8. // See http://www.boost.org/libs/iostreams for documentation.
  9. // Note: basically a copy-paste of the gzip test
  10. #include <cstddef>
  11. #include <string>
  12. #include <boost/iostreams/copy.hpp>
  13. #include <boost/iostreams/device/array.hpp>
  14. #include <boost/iostreams/device/back_inserter.hpp>
  15. #include <boost/iostreams/filter/zstd.hpp>
  16. #include <boost/iostreams/filter/test.hpp>
  17. #include <boost/iostreams/filtering_stream.hpp>
  18. #include <boost/ref.hpp>
  19. #include <boost/range/iterator_range.hpp>
  20. #include <boost/test/test_tools.hpp>
  21. #include <boost/test/unit_test.hpp>
  22. #include "detail/sequence.hpp"
  23. #include "detail/verification.hpp"
  24. using namespace boost;
  25. using namespace boost::iostreams;
  26. using namespace boost::iostreams::test;
  27. namespace io = boost::iostreams;
  28. using boost::unit_test::test_suite;
  29. struct zstd_alloc : std::allocator<char> {
  30. zstd_alloc() { }
  31. zstd_alloc(const zstd_alloc& other) { }
  32. template<typename T>
  33. zstd_alloc(const std::allocator<T>& other) { }
  34. };
  35. void compression_test()
  36. {
  37. text_sequence data;
  38. // Test compression and decompression with custom allocator
  39. BOOST_CHECK(
  40. test_filter_pair( basic_zstd_compressor<zstd_alloc>(),
  41. basic_zstd_decompressor<zstd_alloc>(),
  42. std::string(data.begin(), data.end()) )
  43. );
  44. }
  45. void multiple_member_test()
  46. {
  47. text_sequence data;
  48. std::vector<char> temp, dest;
  49. // Write compressed data to temp, twice in succession
  50. filtering_ostream out;
  51. out.push(zstd_compressor());
  52. out.push(io::back_inserter(temp));
  53. io::copy(make_iterator_range(data), out);
  54. out.push(io::back_inserter(temp));
  55. io::copy(make_iterator_range(data), out);
  56. BOOST_CHECK(std::equal(temp.begin(), temp.begin() + temp.size()/2, temp.begin() + temp.size()/2));
  57. // Read compressed data from temp into dest
  58. filtering_istream in;
  59. in.push(zstd_decompressor());
  60. in.push(array_source(&temp[0], temp.size()));
  61. io::copy(in, io::back_inserter(dest));
  62. // Check that dest consists of two copies of data
  63. BOOST_REQUIRE_EQUAL(data.size() * 2, dest.size());
  64. BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin()));
  65. BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + dest.size() / 2));
  66. dest.clear();
  67. io::copy(
  68. array_source(&temp[0], temp.size()),
  69. io::compose(zstd_decompressor(), io::back_inserter(dest)));
  70. // Check that dest consists of two copies of data
  71. BOOST_REQUIRE_EQUAL(data.size() * 2, dest.size());
  72. BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin()));
  73. BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + dest.size() / 2));
  74. }
  75. void array_source_test()
  76. {
  77. std::string data = "simple test string.";
  78. std::string encoded;
  79. filtering_ostream out;
  80. out.push(zstd_compressor());
  81. out.push(io::back_inserter(encoded));
  82. io::copy(make_iterator_range(data), out);
  83. std::string res;
  84. io::array_source src(encoded.data(),encoded.length());
  85. io::copy(io::compose(io::zstd_decompressor(), src), io::back_inserter(res));
  86. BOOST_CHECK_EQUAL(data, res);
  87. }
  88. void empty_file_test()
  89. {
  90. // This test is in response to https://svn.boost.org/trac/boost/ticket/5237
  91. // The previous implementation of gzip_compressor only wrote the gzip file
  92. // header when the first bytes of uncompressed input were processed, causing
  93. // incorrect behavior for empty files
  94. BOOST_CHECK(
  95. test_filter_pair( zstd_compressor(),
  96. zstd_decompressor(),
  97. std::string() )
  98. );
  99. }
  100. void multipart_test()
  101. {
  102. // This test verifies that the zstd_decompressor properly handles a file
  103. // that consists of multiple concatenated files (matches unzstd behaviour)
  104. static const char multipart_file[] = {
  105. '\x28', '\xb5', '\x2f', '\xfd', '\x24', '\x15', '\x95', '\x00', '\x00', '\x50', '\x4c', '\x69',
  106. '\x6e', '\x65', '\x20', '\x31', '\x0a', '\x32', '\x33', '\x0a', '\x02', '\x00', '\x60', '\x84',
  107. '\xae', '\x62', '\x04', '\x19', '\xf8', '\xe1', '\x2d', '\x28', '\xb5', '\x2f', '\xfd', '\x24',
  108. '\x15', '\x95', '\x00', '\x00', '\x50', '\x4c', '\x69', '\x6e', '\x65', '\x20', '\x34', '\x0a',
  109. '\x35', '\x36', '\x0a', '\x02', '\x00', '\x60', '\x84', '\xae', '\x62', '\x04', '\x5f', '\xcf',
  110. '\xd5', '\xb8', '\x28', '\xb5', '\x2f', '\xfd', '\x24', '\x00', '\x01', '\x00', '\x00', '\x99',
  111. '\xe9', '\xd8', '\x51', '\x28', '\xb5', '\x2f', '\xfd', '\x24', '\x15', '\x95', '\x00', '\x00',
  112. '\x50', '\x4c', '\x69', '\x6e', '\x65', '\x20', '\x37', '\x0a', '\x38', '\x39', '\x0a', '\x02',
  113. '\x00', '\x60', '\x84', '\xae', '\x62', '\x04', '\x94', '\x13', '\xdb', '\xae'
  114. };
  115. filtering_istream in;
  116. std::string line;
  117. in.push(zstd_decompressor());
  118. in.push(io::array_source(multipart_file, sizeof(multipart_file)));
  119. // First part
  120. std::getline(in, line);
  121. BOOST_CHECK_EQUAL("Line 1", line);
  122. std::getline(in, line);
  123. BOOST_CHECK_EQUAL("Line 2", line);
  124. std::getline(in, line);
  125. BOOST_CHECK_EQUAL("Line 3", line);
  126. // Second part immediately follows
  127. std::getline(in, line);
  128. BOOST_CHECK_EQUAL("Line 4", line);
  129. std::getline(in, line);
  130. BOOST_CHECK_EQUAL("Line 5", line);
  131. std::getline(in, line);
  132. BOOST_CHECK_EQUAL("Line 6", line);
  133. // Then an empty part, followed by one last 3-line part.
  134. std::getline(in, line);
  135. BOOST_CHECK_EQUAL("Line 7", line);
  136. std::getline(in, line);
  137. BOOST_CHECK_EQUAL("Line 8", line);
  138. std::getline(in, line);
  139. BOOST_CHECK_EQUAL("Line 9", line);
  140. // Check for zstd errors too.
  141. BOOST_CHECK(!in.bad());
  142. }
  143. test_suite* init_unit_test_suite(int, char* [])
  144. {
  145. test_suite* test = BOOST_TEST_SUITE("zstd test");
  146. test->add(BOOST_TEST_CASE(&compression_test));
  147. test->add(BOOST_TEST_CASE(&multiple_member_test));
  148. test->add(BOOST_TEST_CASE(&array_source_test));
  149. test->add(BOOST_TEST_CASE(&empty_file_test));
  150. test->add(BOOST_TEST_CASE(&multipart_test));
  151. return test;
  152. }