// 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;
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) {
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");
printf("test_decoding()\n");
test_decoding();
+ printf("test_padded_decoding()\n");
+ test_padded_decoding();
+
printf("All tests pass.\n");
return 0;
}