]> git.sesse.net Git - fjl/blobdiff - dehuff_test.c
Add optional padding data at the end to the bit source (is that the right place?...
[fjl] / dehuff_test.c
index 0212244bdf4594be6cf4b54280a5eb80003d0a45..eed09520dab288214cf9a57236d4a24197045902 100644 (file)
@@ -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;
 }