X-Git-Url: https://git.sesse.net/?p=fjl;a=blobdiff_plain;f=dehuff_test.c;fp=dehuff_test.c;h=eed09520dab288214cf9a57236d4a24197045902;hp=0212244bdf4594be6cf4b54280a5eb80003d0a45;hb=9069ae93f3e9e9b44f5f28968f508d09f0711737;hpb=8445e04bae016b6bd74705902972e66ae7fe37c1 diff --git a/dehuff_test.c b/dehuff_test.c index 0212244..eed0952 100644 --- a/dehuff_test.c +++ b/dehuff_test.c @@ -78,7 +78,7 @@ void test_table_gen() // Test that we can decode a simple bit stream. // Note that since we end on a long code, we won't crash into -// the end-of-stream problems we currently have. +// the end-of-stream problems we have without padding. void test_decoding() { huffman_tables_t tables; @@ -97,7 +97,7 @@ void test_decoding() ud.bytes_left = sizeof(bytes); struct bit_source source; - init_bit_source(&source, custom_read, &ud); + init_bit_source(&source, custom_read, 0, &ud); struct huffman_table* tbl = &tables[DC_CLASS][0]; for (unsigned i = 0; i < 12; ++i) { @@ -108,6 +108,35 @@ void test_decoding() assert(source.bits_available == 0); } +// Test that we can decode a bit stream that ends on a short code, +// if we've got padding. +void test_padded_decoding() +{ + huffman_tables_t tables; + read_example_tables(&tables); + + // Our stream looks like this: + // + // 0 1 2 3 4 + // 00 010 011 100 101 + uint8_t bytes[] = { + 0x13, 0x94 + }; + + struct custom_read_userdata ud; + ud.bytes = bytes; + ud.bytes_left = sizeof(bytes); + + struct bit_source source; + init_bit_source(&source, custom_read, 2, &ud); + + struct huffman_table* tbl = &tables[DC_CLASS][0]; + for (unsigned i = 0; i < 5; ++i) { + unsigned symbol = read_huffman_symbol(tbl, &source); + assert(symbol == i); + } +} + int main(void) { printf("test_table_gen()\n"); @@ -116,6 +145,9 @@ int main(void) printf("test_decoding()\n"); test_decoding(); + printf("test_padded_decoding()\n"); + test_padded_decoding(); + printf("All tests pass.\n"); return 0; }