#include "test_util.h"
static void
assert_decompression_error(const u8 *in, size_t in_nbytes)
{
struct libdeflate_decompressor *d;
z_stream z;
u8 out[128];
const size_t out_nbytes_avail = sizeof(out);
size_t actual_out_nbytes;
enum libdeflate_result res;
d = libdeflate_alloc_decompressor();
ASSERT(d != NULL);
res = libdeflate_deflate_decompress(d, in, in_nbytes,
out, out_nbytes_avail,
&actual_out_nbytes);
ASSERT(res == LIBDEFLATE_BAD_DATA);
libdeflate_free_decompressor(d);
memset(&z, 0, sizeof(z));
res = inflateInit2(&z, -15);
ASSERT(res == Z_OK);
z.next_in = (void *)in;
z.avail_in = in_nbytes;
z.next_out = (void *)out;
z.avail_out = out_nbytes_avail;
res = inflate(&z, Z_FINISH);
ASSERT(res == Z_DATA_ERROR);
inflateEnd(&z);
}
static void
test_too_many_codeword_lengths(void)
{
u8 in[128];
struct output_bitstream os = { .next = in, .end = in + sizeof(in) };
int i;
ASSERT(put_bits(&os, 1, 1));
ASSERT(put_bits(&os, 2, 2));
ASSERT(put_bits(&os, 0, 5));
ASSERT(put_bits(&os, 0, 5));
ASSERT(put_bits(&os, 14, 4));
for (i = 0; i < 2; i++)
ASSERT(put_bits(&os, 0, 3));
ASSERT(put_bits(&os, 1, 3));
ASSERT(put_bits(&os, 0, 3));
for (i = 0; i < 13; i++)
ASSERT(put_bits(&os, 0, 3));
ASSERT(put_bits(&os, 1, 3));
ASSERT(put_bits(&os, 0x1, 1) &&
put_bits(&os, 117, 7));
ASSERT(put_bits(&os, 0x1, 1) &&
put_bits(&os, 116, 7));
ASSERT(put_bits(&os, 0x0, 1));
ASSERT(put_bits(&os, 0x0, 1));
ASSERT(put_bits(&os, 0x1, 1) &&
put_bits(&os, 117, 7));
ASSERT(put_bits(&os, 0x0, 0));
ASSERT(put_bits(&os, 0x1, 1));
ASSERT(flush_bits(&os));
assert_decompression_error(in, os.next - in);
}
int
tmain(int argc, tchar *argv[])
{
begin_program(argv);
test_too_many_codeword_lengths();
return 0;
}