Skip to content

Commit

Permalink
[WIP] unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Oct 19, 2016
1 parent ac4c622 commit a93dd61
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 40 deletions.
2 changes: 1 addition & 1 deletion include/beast/zlib/basic_deflate_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace zlib {
This is a port of zlib's "deflate" functionality to C++.
*/
template<class Allocator>
class basic_deflate_stream : public z_stream
class basic_deflate_stream : public z_params
{
// maximum heap size
static std::uint16_t constexpr HEAP_SIZE = 2 * limits::lCodes + 1;
Expand Down
8 changes: 4 additions & 4 deletions include/beast/zlib/basic_inflate_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace zlib {
This is a port of zlib's "inflate" functionality to C++.
*/
template<class Allocator>
class basic_inflate_stream : public z_stream
class basic_inflate_stream : public z_params
{
public:
struct params
Expand Down Expand Up @@ -142,13 +142,13 @@ class basic_inflate_stream : public z_stream
}

void
inflate_fast(z_stream& zs, unsigned start);
inflate_fast(z_params& zs, unsigned start);

int
write(z_stream& zs, int flush);
write(z_params& zs, int flush);

void
resetKeep(z_stream& zs);
resetKeep(z_params& zs);

void
fixedTables();
Expand Down
1 change: 1 addition & 0 deletions include/beast/zlib/detail/bitstream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include <boost/assert.hpp>
#include <cstdint>
#include <iterator>

namespace beast {
namespace zlib {
Expand Down
41 changes: 22 additions & 19 deletions include/beast/zlib/impl/basic_inflate_stream.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ write(int flush)
template<class Allocator>
void
basic_inflate_stream<Allocator>::
resetKeep(z_stream& zs)
resetKeep(z_params& zs)
{
zs.total_in = 0;
zs.total_out = 0;
Expand Down Expand Up @@ -200,8 +200,12 @@ updatewindow(const Byte *end, unsigned copy)
template<class Allocator>
int
basic_inflate_stream<Allocator>::
write(z_stream& zs, int flush)
write(z_params& zs, int flush)
{
unsigned in;
unsigned out; // save starting available input and output
int result = Z_OK;

auto put = zs.next_out;
auto next = zs.next_in;
auto const outend = put + zs.avail_out;
Expand All @@ -220,12 +224,11 @@ write(z_stream& zs, int flush)
zs.avail_out = outend - put;
zs.next_in = next;
zs.avail_in = end - next;
return Z_OK;
if (((in == 0 && out == 0) || flush == Z_FINISH) && result == Z_OK)
result = Z_BUF_ERROR;
return result;
};

unsigned in, out; // save starting available input and output
int ret = Z_OK;

if(zs.next_out == 0 ||
(zs.next_in == 0 && zs.avail_in != 0))
return Z_STREAM_ERROR;
Expand Down Expand Up @@ -373,9 +376,9 @@ write(z_stream& zs, int flush)
next_ = &codes_[0];
lencode_ = next_;
lenbits_ = 7;
ret = inflate_table(detail::CODES, &lens_[0],
result = inflate_table(detail::CODES, &lens_[0],
order.size(), &next_, &lenbits_, work_);
if(ret)
if(result)
{
zs.msg = (char *)"invalid code lengths set";
mode_ = BAD;
Expand Down Expand Up @@ -463,19 +466,19 @@ write(z_stream& zs, int flush)
next_ = &codes_[0];
lencode_ = next_;
lenbits_ = 9;
ret = inflate_table(detail::LENS,
result = inflate_table(detail::LENS,
&lens_[0], nlen_, &next_, &lenbits_, work_);
if(ret)
if(result)
{
zs.msg = (char *)"invalid literal/lengths set";
mode_ = BAD;
break;
}
distcode_ = next_;
distbits_ = 6;
ret = inflate_table(detail::DISTS,
result = inflate_table(detail::DISTS,
lens_ + nlen_, ndist_, &next_, &distbits_, work_);
if(ret)
if(result)
{
zs.msg = (char *)"invalid distances set";
mode_ = BAD;
Expand All @@ -497,7 +500,7 @@ write(z_stream& zs, int flush)
if(avail_in >= 6 && avail_out >= 258)
{
auto const nwritten = put - zs.next_out;
z_stream zc = zs;
z_params zc = zs;
zc.next_out = put;
zc.avail_out = outend - put;
zc.next_in = next;
Expand Down Expand Up @@ -677,11 +680,11 @@ write(z_stream& zs, int flush)
// fall through

case DONE:
ret = Z_STREAM_END;
result = Z_STREAM_END;
goto inf_leave;

case BAD:
ret = Z_DATA_ERROR;
result = Z_DATA_ERROR;
goto inf_leave;

case MEM:
Expand Down Expand Up @@ -716,9 +719,9 @@ write(z_stream& zs, int flush)
zs.data_type = bits_ + (last_ ? 64 : 0) +
(mode_ == TYPE ? 128 : 0) +
(mode_ == LEN_ || mode_ == COPY_ ? 256 : 0);
if(((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
ret = Z_BUF_ERROR;
return ret;
if(((in == 0 && out == 0) || flush == Z_FINISH) && result == Z_OK)
result = Z_BUF_ERROR;
return result;
}

/*
Expand Down Expand Up @@ -760,7 +763,7 @@ template<class Allocator>
void
basic_inflate_stream<Allocator>::
inflate_fast(
z_stream& zs,
z_params& zs,
unsigned start) // inflate()'s starting value for strm->avail_out
{
unsigned char const* in; // local strm->next_in
Expand Down
2 changes: 1 addition & 1 deletion include/beast/zlib/zlib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ typedef unsigned int uInt; /* 16 bits or more */
uncompressed data and may be saved for use in the decompressor (particularly
if the decompressor wants to decompress everything in a single step).
*/
struct z_stream
struct z_params
{
Byte const* next_in; // next input byte
std::size_t avail_in; // number of bytes available at next_in
Expand Down
4 changes: 3 additions & 1 deletion test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ unit-test zlib-tests :
zlib/zlib-1.2.8/trees.c
zlib/zlib-1.2.8/uncompr.c
zlib/zlib-1.2.8/zutil.c
zlib/inflate_stream.cpp
zlib/basic_deflate_stream.cpp
zlib/basic_inflate_stream.cpp
zlib/deflate_stream.cpp
zlib/error.cpp
zlib/inflate_stream.cpp
zlib/zlib.cpp
;
1 change: 1 addition & 0 deletions test/zlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ add_executable (zlib-tests
error.cpp
inflate_stream.cpp
zlib.cpp

)

if (NOT WIN32)
Expand Down
55 changes: 46 additions & 9 deletions test/zlib/inflate_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ namespace zlib {
class inflate_stream_test : public beast::unit_test::suite
{
public:
using self = inflate_stream_test;
typedef void(self::*pmf_t)(
int windowBits, std::string const& in, std::string const& check);

// Decompress using ZLib
void
doInflate(int windowBits,
doInflate_zlib(int windowBits,
std::string const& in, std::string const& check)
{
BEAST_EXPECT(in.size() > 1);
Expand Down Expand Up @@ -72,9 +76,9 @@ class inflate_stream_test : public beast::unit_test::suite
}
}

// Decompress using ZLib
// Decompress using Beast
void
doInflate2(int windowBits,
doInflate(int windowBits,
std::string const& in, std::string const& check)
{
BEAST_EXPECT(in.size() > 1);
Expand Down Expand Up @@ -131,18 +135,51 @@ class inflate_stream_test : public beast::unit_test::suite
}

void
doMatrix(std::string const& check)
doMatrix1(std::string const& check, pmf_t pmf)
{
z_deflator zd;
doInflate2(15, zd(check), check);
for(int level = 0; level <= 9; ++level)
{
for(int windowBits = 8; windowBits <= 15; ++windowBits)
{
for(int strategy = 0; strategy <= 4; ++strategy)
{
z_deflator zd;
zd.level(level);
zd.windowBits(windowBits);
zd.strategy(strategy);
auto const in = zd(check);
(this->*pmf)(windowBits, in, check);
doInflate_zlib(windowBits, in, check);
}
}
}
}

void
testInflate()
{
doMatrix("Hello, world!");
doMatrix("Hello, world! Hello, world!");
doMatrix("Hello, world! Hello, world! Hello, world! Hello, world! Hello, world!");
doMatrix1("Hello, world!", &self::doInflate_zlib);
doMatrix1("Hello, world!", &self::doInflate);

doMatrix1(
"iEYEARECAAYFAjdY"
"CQoACgkQJ9S6ULt1"
"dqz6IwCfQ7wP6i/i"
"8HhbcOSKF4ELyQB1"
"oCoAoOuqpRqEzr4k"
"OkQqHRLE/b8/Rw2k",
&self::doInflate_zlib);

doMatrix1(
"iEYEARECAAYFAjdY"
"CQoACgkQJ9S6ULt1"
"dqz6IwCfQ7wP6i/i"
"8HhbcOSKF4ELyQB1"
"oCoAoOuqpRqEzr4k"
"OkQqHRLE/b8/Rw2k",
&self::doInflate);

//doMatrix("Hello, world! Hello, world! Hello, world! Hello, world! Hello, world!");
}

void
Expand Down
2 changes: 1 addition & 1 deletion test/zlib/zlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class zlib_test : public beast::unit_test::suite
}
};

BEAST_DEFINE_TESTSUITE(zlib,core,beast);
//BEAST_DEFINE_TESTSUITE(zlib,core,beast);

} // zlib
} // beast
44 changes: 40 additions & 4 deletions test/zlib/ztest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,39 @@

class z_deflator
{
int level_ = Z_DEFAULT_COMPRESSION;
int windowBits_ = 15;
int memLevel_ = 4;
int strategy_ = Z_DEFAULT_STRATEGY;

public:
// -1 = default
// 0 = none
// 1..9 = faster<-->better
void
level(int n)
{
level_ = n;
}

void
windowBits(int n)
{
windowBits_ = n;
}

void
memLevel(int n)
{
memLevel_ = n;
}

void
strategy(int n)
{
strategy_ = n;
}

std::string
operator()(std::string const& in)
{
Expand All @@ -22,19 +54,23 @@ class z_deflator
memset(&zs, 0, sizeof(zs));
result = deflateInit2(
&zs,
Z_DEFAULT_COMPRESSION,
level_,
Z_DEFLATED,
-15,
4,
Z_DEFAULT_STRATEGY
-windowBits_,
memLevel_,
strategy_
);
if(result != Z_OK)
throw std::logic_error("deflateInit2 failed");
std::string out;
out.resize(deflateBound(&zs, in.size()));
zs.next_in = (Bytef*)in.data();
zs.avail_in = in.size();
zs.next_out = (Bytef*)&out[0];
zs.avail_out = out.size();
result = deflate(&zs, Z_FULL_FLUSH);
if(result != Z_OK)
throw std::logic_error("deflate failed");
out.resize(zs.total_out);
deflateEnd(&zs);
return out;
Expand Down

0 comments on commit a93dd61

Please sign in to comment.