]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
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) | |
6 | // | |
7 | ||
8 | // Test that header file is self-contained. | |
9 | #include <beast/detail/zlib/deflate_stream.hpp> | |
10 | #include <beast/detail/zlib/inflate_stream.hpp> | |
11 | ||
12 | #include <beast/unit_test/suite.hpp> | |
13 | #include <array> | |
14 | #include <cassert> | |
15 | #include <memory> | |
16 | #include <random> | |
17 | ||
18 | namespace beast { | |
19 | namespace zlib { | |
20 | ||
21 | class zlib_test : public beast::unit_test::suite | |
22 | { | |
23 | public: | |
24 | class buffer | |
25 | { | |
26 | std::size_t size_ = 0; | |
27 | std::size_t capacity_ = 0; | |
28 | std::unique_ptr<std::uint8_t[]> p_; | |
29 | ||
30 | public: | |
31 | buffer() = default; | |
32 | buffer(buffer&&) = default; | |
33 | buffer& operator=(buffer&&) = default; | |
34 | ||
35 | ||
36 | explicit | |
37 | buffer(std::size_t capacity) | |
38 | { | |
39 | reserve(capacity); | |
40 | } | |
41 | ||
42 | bool | |
43 | empty() const | |
44 | { | |
45 | return size_ == 0; | |
46 | } | |
47 | ||
48 | std::size_t | |
49 | size() const | |
50 | { | |
51 | return size_; | |
52 | } | |
53 | ||
54 | std::size_t | |
55 | capacity() const | |
56 | { | |
57 | return capacity_; | |
58 | } | |
59 | ||
60 | std::uint8_t const* | |
61 | data() const | |
62 | { | |
63 | return p_.get(); | |
64 | } | |
65 | ||
66 | std::uint8_t* | |
67 | data() | |
68 | { | |
69 | return p_.get(); | |
70 | } | |
71 | ||
72 | void | |
73 | reserve(std::size_t capacity) | |
74 | { | |
75 | if(capacity != capacity_) | |
76 | { | |
77 | p_.reset(new std::uint8_t[capacity]); | |
78 | capacity_ = capacity; | |
79 | } | |
80 | } | |
81 | ||
82 | void | |
83 | resize(std::size_t size) | |
84 | { | |
85 | assert(size <= capacity_); | |
86 | size_ = size; | |
87 | } | |
88 | }; | |
89 | ||
90 | buffer | |
91 | make_source1(std::size_t size) | |
92 | { | |
93 | std::mt19937 rng; | |
94 | buffer b(size); | |
95 | auto p = b.data(); | |
96 | std::size_t n = 0; | |
97 | static std::string const chars( | |
98 | "01234567890{}\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | |
99 | "{{{{{{{{{{}}}}}}}}}} "); | |
100 | while(n < size) | |
101 | { | |
102 | *p++ = chars[rng()%chars.size()]; | |
103 | ++n; | |
104 | } | |
105 | b.resize(n); | |
106 | return b; | |
107 | } | |
108 | ||
109 | buffer | |
110 | make_source2(std::size_t size) | |
111 | { | |
112 | std::mt19937 rng; | |
113 | std::array<double, 2> const i{0, 65535}; | |
114 | std::array<double, 2> const w{0, 1}; | |
115 | std::piecewise_linear_distribution<double> d( | |
116 | i.begin(), i.end(), w.begin()); | |
117 | buffer b(size); | |
118 | auto p = b.data(); | |
119 | std::size_t n = 0; | |
120 | while(n < size) | |
121 | { | |
122 | if(n == 1) | |
123 | { | |
124 | *p++ = rng()%256; | |
125 | ++n; | |
126 | continue; | |
127 | } | |
128 | auto const v = static_cast<std::uint16_t>(d(rng)); | |
129 | *p++ = v>>8; | |
130 | *p++ = v&0xff; | |
131 | n += 2; | |
132 | ||
133 | } | |
134 | b.resize(n); | |
135 | return b; | |
136 | } | |
137 | ||
138 | void | |
139 | checkInflate(buffer const& input, buffer const& original) | |
140 | { | |
141 | for(std::size_t i = 0; i < input.size(); ++i) | |
142 | { | |
143 | buffer output(original.size()); | |
144 | inflate_stream zs; | |
145 | zs.avail_in = 0; | |
146 | zs.next_in = 0; | |
147 | zs.next_out = output.data(); | |
148 | zs.avail_out = output.capacity(); | |
149 | if(i > 0) | |
150 | { | |
151 | zs.next_in = (Byte*)input.data(); | |
152 | zs.avail_in = i; | |
153 | auto result = zs.write(Z_FULL_FLUSH); | |
154 | expect(result == Z_OK); | |
155 | } | |
156 | zs.next_in = (Byte*)input.data() + i; | |
157 | zs.avail_in = input.size() - i; | |
158 | auto result = zs.write(Z_FULL_FLUSH); | |
159 | output.resize(output.capacity() - zs.avail_out); | |
160 | expect(result == Z_OK); | |
161 | expect(output.size() == original.size()); | |
162 | expect(std::memcmp( | |
163 | output.data(), original.data(), original.size()) == 0); | |
164 | } | |
165 | } | |
166 | ||
167 | void testSpecial() | |
168 | { | |
169 | { | |
170 | deflate_stream zs; | |
171 | } | |
172 | { | |
173 | inflate_stream zs; | |
174 | } | |
175 | } | |
176 | ||
177 | void testCompress() | |
178 | { | |
179 | static std::size_t constexpr N = 2048; | |
180 | for(int source = 0; source <= 1; ++source) | |
181 | { | |
182 | buffer original; | |
183 | switch(source) | |
184 | { | |
185 | case 0: | |
186 | original = make_source1(N); | |
187 | break; | |
188 | case 1: | |
189 | original = make_source2(N); | |
190 | break; | |
191 | } | |
192 | for(int level = 0; level <= 9; ++level) | |
193 | { | |
194 | for(int strategy = 0; strategy <= 4; ++strategy) | |
195 | { | |
196 | for(int wbits = 15; wbits <= 15; ++wbits) | |
197 | { | |
198 | deflate_stream zs; | |
199 | zs.avail_in = 0; | |
200 | zs.next_in = 0; | |
201 | expect(deflate_stream::deflateInit2(&zs, | |
202 | level, | |
203 | wbits, | |
204 | 4, | |
205 | strategy) == Z_OK); | |
206 | buffer output(deflate_stream::deflateBound(&zs, original.size())); | |
207 | zs.next_in = (Byte*)original.data(); | |
208 | zs.avail_in = original.size(); | |
209 | zs.next_out = output.data(); | |
210 | zs.avail_out = output.capacity(); | |
211 | auto result = zs.deflate(Z_FULL_FLUSH); | |
212 | expect(result == Z_OK); | |
213 | output.resize(output.capacity() - zs.avail_out); | |
214 | checkInflate(output, original); | |
215 | } | |
216 | } | |
217 | } | |
218 | } | |
219 | } | |
220 | ||
221 | void run() override | |
222 | { | |
223 | testSpecial(); | |
224 | testCompress(); | |
225 | } | |
226 | }; | |
227 | ||
228 | BEAST_DEFINE_TESTSUITE(zlib,core,beast); | |
229 | ||
230 | } // zlib | |
231 | } // beast | |
232 |