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)
8 #ifndef BEAST_ZTEST_HPP
9 #define BEAST_ZTEST_HPP
11 #include "zlib-1.2.8/zlib.h"
18 int level_ = Z_DEFAULT_COMPRESSION;
21 int strategy_ = Z_DEFAULT_STRATEGY;
26 // 1..9 = faster<-->better
52 operator()(std::string const& in)
56 memset(&zs, 0, sizeof(zs));
57 result = deflateInit2(
66 throw std::logic_error("deflateInit2 failed");
68 out.resize(deflateBound(&zs,
69 static_cast<uLong>(in.size())));
70 zs.next_in = (Bytef*)in.data();
71 zs.avail_in = static_cast<uInt>(in.size());
72 zs.next_out = (Bytef*)&out[0];
73 zs.avail_out = static_cast<uInt>(out.size());
74 result = deflate(&zs, Z_FULL_FLUSH);
76 throw std::logic_error("deflate failed");
77 out.resize(zs.total_out);
87 operator()(std::string const& in)
92 memset(&zs, 0, sizeof(zs));
93 result = inflateInit2(&zs, -15);
96 zs.next_in = (Bytef*)in.data();
97 zs.avail_in = static_cast<uInt>(in.size());
100 out.resize(zs.total_out + 1024);
101 zs.next_out = (Bytef*)&out[zs.total_out];
102 zs.avail_out = static_cast<uInt>(
103 out.size() - zs.total_out);
104 result = inflate(&zs, Z_SYNC_FLUSH);
105 if( result == Z_NEED_DICT ||
106 result == Z_DATA_ERROR ||
107 result == Z_MEM_ERROR)
109 throw std::logic_error("inflate failed");
113 if(result == Z_STREAM_END)
116 out.resize(zs.total_out);
128 // Lots of repeats, limited char range
131 corpus1(std::size_t n)
133 static std::string const alphabet{
134 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
139 std::uniform_int_distribution<std::size_t> d0{
140 0, alphabet.size() - 1};
141 std::uniform_int_distribution<std::size_t> d1{
145 auto const rep = d1(g);
146 auto const ch = alphabet[d0(g)];
147 s.insert(s.end(), rep, ch);
156 corpus2(std::size_t n)
161 std::uniform_int_distribution<std::uint32_t> d0{0, 255};
163 s.push_back(static_cast<char>(d0(g)));