From 7512dfb8454b8f80cefec7c8bb85380f4e9c5087 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Mon, 14 Jan 2019 21:37:48 -0800 Subject: [PATCH] test_incomplete_codes: add another test case --- programs/test_incomplete_codes.c | 86 ++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/programs/test_incomplete_codes.c b/programs/test_incomplete_codes.c index cbeb338..436cd9b 100644 --- a/programs/test_incomplete_codes.c +++ b/programs/test_incomplete_codes.c @@ -286,6 +286,91 @@ test_singleton_offset_code(void) expected_out, sizeof(expected_out)); } +/* Test that an offset code containing only one symbol is accepted, even if that + * symbol is not symbol 0. The codeword should be '0' in either case. */ +static void +test_singleton_offset_code_notsymzero(void) +{ + static const u8 expected_out[] = { 254, 255, 254, 255, 254 }; + u8 in[128]; + u8 out[128]; + struct output_bitstream os = { .next = in, .end = in + sizeof(in) }; + int i; + + ASSERT(put_bits(&os, 1, 1)); /* BFINAL: 1 */ + ASSERT(put_bits(&os, 2, 2)); /* BTYPE: DYNAMIC_HUFFMAN */ + + /* + * Litlen code: + * litlensym_254 len=2 codeword=00 + * litlensym_255 len=2 codeword=10 + * litlensym_256 (end-of-block) len=2 codeword=01 + * litlensym_257 (len 3) len=2 codeword=11 + * Offset code: + * offsetsym_1 (offset 2) len=1 codeword=0 + * + * Litlen and offset codeword lengths: + * [0..253] = 0 presym_{18,18} + * [254] = 2 presym_2 + * [255] = 2 presym_2 + * [256] = 2 presym_2 + * [257] = 2 presym_2 + * [258] = 0 presym_0 + * [259] = 1 presym_1 + * + * Precode: + * presym_0 len=2 codeword=00 + * presym_1 len=2 codeword=10 + * presym_2 len=2 codeword=01 + * presym_18 len=2 codeword=11 + */ + + ASSERT(put_bits(&os, 1, 5)); /* num_litlen_syms: 1 + 257 */ + ASSERT(put_bits(&os, 1, 5)); /* num_offset_syms: 1 + 1 */ + ASSERT(put_bits(&os, 14, 4)); /* num_explicit_precode_lens: 14 + 4 */ + /* + * Precode codeword lengths: order is + * [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15] + */ + for (i = 0; i < 2; i++) /* presym_{16,17}: len=0 */ + ASSERT(put_bits(&os, 0, 3)); + ASSERT(put_bits(&os, 2, 3)); /* presym_18: len=2 */ + ASSERT(put_bits(&os, 2, 3)); /* presym_0: len=2 */ + for (i = 0; i < 11; i++) /* presym_{8,...,13}: len=0 */ + ASSERT(put_bits(&os, 0, 3)); + ASSERT(put_bits(&os, 2, 3)); /* presym_2: len=2 */ + ASSERT(put_bits(&os, 0, 3)); /* presym_14: len=0 */ + ASSERT(put_bits(&os, 2, 3)); /* presym_1: len=2 */ + + /* Litlen and offset codeword lengths */ + ASSERT(put_bits(&os, 0x3, 2) && /* presym_18, 128 zeroes */ + put_bits(&os, 117, 7)); + ASSERT(put_bits(&os, 0x3, 2) && /* presym_18, 126 zeroes */ + put_bits(&os, 115, 7)); + ASSERT(put_bits(&os, 0x1, 2)); /* presym_2 */ + ASSERT(put_bits(&os, 0x1, 2)); /* presym_2 */ + ASSERT(put_bits(&os, 0x1, 2)); /* presym_2 */ + ASSERT(put_bits(&os, 0x1, 2)); /* presym_2 */ + ASSERT(put_bits(&os, 0x0, 2)); /* presym_0 */ + ASSERT(put_bits(&os, 0x2, 2)); /* presym_1 */ + + /* Literals */ + ASSERT(put_bits(&os, 0x0, 2)); /* litlensym_254 */ + ASSERT(put_bits(&os, 0x2, 2)); /* litlensym_255 */ + + /* Match */ + ASSERT(put_bits(&os, 0x3, 2)); /* litlensym_257 */ + ASSERT(put_bits(&os, 0x0, 1)); /* offsetsym_1 */ + + /* End of block */ + ASSERT(put_bits(&os, 0x1, 2)); /* litlensym_256 */ + + ASSERT(flush_bits(&os)); + + verify_decompression(in, os.next - in, out, sizeof(out), + expected_out, sizeof(expected_out)); +} + int tmain(int argc, tchar *argv[]) { @@ -294,6 +379,7 @@ tmain(int argc, tchar *argv[]) test_empty_offset_code(); test_singleton_litrunlen_code(); test_singleton_offset_code(); + test_singleton_offset_code_notsymzero(); return 0; }