2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // Official repository: https://github.com/boostorg/beast
10 // Test that header file is self-contained.
11 #include <beast/detail/zlib/deflate_stream.hpp>
12 #include <beast/detail/zlib/inflate_stream.hpp>
14 #include <beast/unit_test/suite.hpp>
24 class zlib_test
: public beast::unit_test::suite
29 std::size_t size_
= 0;
30 std::size_t capacity_
= 0;
31 std::unique_ptr
<std::uint8_t[]> p_
;
35 buffer(buffer
&&) = default;
36 buffer
& operator=(buffer
&&) = default;
40 buffer(std::size_t capacity
)
76 reserve(std::size_t capacity
)
78 if(capacity
!= capacity_
)
80 p_
.reset(new std::uint8_t[capacity
]);
86 resize(std::size_t size
)
88 assert(size
<= capacity_
);
94 make_source1(std::size_t size
)
100 static std::string
const chars(
101 "01234567890{}\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
102 "{{{{{{{{{{}}}}}}}}}} ");
105 *p
++ = chars
[rng()%chars
.size()];
113 make_source2(std::size_t size
)
116 std::array
<double, 2> const i
{0, 65535};
117 std::array
<double, 2> const w
{0, 1};
118 std::piecewise_linear_distribution
<double> d(
119 i
.begin(), i
.end(), w
.begin());
131 auto const v
= static_cast<std::uint16_t>(d(rng
));
142 checkInflate(buffer
const& input
, buffer
const& original
)
144 for(std::size_t i
= 0; i
< input
.size(); ++i
)
146 buffer
output(original
.size());
150 zs
.next_out
= output
.data();
151 zs
.avail_out
= output
.capacity();
154 zs
.next_in
= (Byte
*)input
.data();
156 auto result
= zs
.write(Z_FULL_FLUSH
);
157 expect(result
== Z_OK
);
159 zs
.next_in
= (Byte
*)input
.data() + i
;
160 zs
.avail_in
= input
.size() - i
;
161 auto result
= zs
.write(Z_FULL_FLUSH
);
162 output
.resize(output
.capacity() - zs
.avail_out
);
163 expect(result
== Z_OK
);
164 expect(output
.size() == original
.size());
166 output
.data(), original
.data(), original
.size()) == 0);
182 static std::size_t constexpr N
= 2048;
183 for(int source
= 0; source
<= 1; ++source
)
189 original
= make_source1(N
);
192 original
= make_source2(N
);
195 for(int level
= 0; level
<= 9; ++level
)
197 for(int strategy
= 0; strategy
<= 4; ++strategy
)
199 for(int wbits
= 15; wbits
<= 15; ++wbits
)
204 expect(deflate_stream::deflateInit2(&zs
,
209 buffer
output(deflate_stream::deflateBound(&zs
, original
.size()));
210 zs
.next_in
= (Byte
*)original
.data();
211 zs
.avail_in
= original
.size();
212 zs
.next_out
= output
.data();
213 zs
.avail_out
= output
.capacity();
214 auto result
= zs
.deflate(Z_FULL_FLUSH
);
215 expect(result
== Z_OK
);
216 output
.resize(output
.capacity() - zs
.avail_out
);
217 checkInflate(output
, original
);
231 BEAST_DEFINE_TESTSUITE(zlib
,core
,beast
);