]> git.sesse.net Git - fjl/blobdiff - bitsource_test.c
Add optional padding data at the end to the bit source (is that the right place?...
[fjl] / bitsource_test.c
index 2b5217ff91a283fee9c544430bf73271cb565cfc..173e499b169c469414ce6303ccbeced15ca07c2e 100644 (file)
@@ -40,7 +40,7 @@ void test_basic_reading()
        ud.bytes_left = sizeof(bytes);
 
        struct bit_source source;
        ud.bytes_left = sizeof(bytes);
 
        struct bit_source source;
-       init_bit_source(&source, custom_read, &ud);
+       init_bit_source(&source, custom_read, 0, &ud);
 
        for (unsigned i = 0; i < sizeof(bytes) * 8 / 6; ++i) {
                possibly_refill(&source, 6);
 
        for (unsigned i = 0; i < sizeof(bytes) * 8 / 6; ++i) {
                possibly_refill(&source, 6);
@@ -60,7 +60,7 @@ void test_slow_source()
        ud.bytes_left = sizeof(bytes);
 
        struct bit_source source;
        ud.bytes_left = sizeof(bytes);
 
        struct bit_source source;
-       init_bit_source(&source, custom_read_slow, &ud);
+       init_bit_source(&source, custom_read_slow, 0, &ud);
 
        for (unsigned i = 0; i < sizeof(bytes) * 8 / 6; ++i) {
                possibly_refill(&source, 6);
 
        for (unsigned i = 0; i < sizeof(bytes) * 8 / 6; ++i) {
                possibly_refill(&source, 6);
@@ -80,7 +80,7 @@ void test_variable_size()
        ud.bytes_left = sizeof(bytes);
 
        struct bit_source source;
        ud.bytes_left = sizeof(bytes);
 
        struct bit_source source;
-       init_bit_source(&source, custom_read, &ud);
+       init_bit_source(&source, custom_read, 0, &ud);
 
        {
                possibly_refill(&source, 4);
 
        {
                possibly_refill(&source, 4);
@@ -111,6 +111,32 @@ void test_variable_size()
                assert(ret == 0xf);
        }
 
                assert(ret == 0xf);
        }
 
+       assert(ud.bytes_left == 0);
+} 
+
+// Tests that padding bytes are properly added at the end, even crossing reads.
+void test_padding_reads()
+{
+       uint8_t bytes[] = { 0x01, 0x02 };
+
+       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);
+       
+       {
+               possibly_refill(&source, 8);
+               unsigned ret = read_bits(&source, 8);
+               assert(ret == 0x01);
+       }
+       {
+               possibly_refill(&source, 12);
+               unsigned ret = read_bits(&source, 12);
+               assert(ret == 0x020);
+       }
+
        assert(ud.bytes_left == 0);
 }
 
        assert(ud.bytes_left == 0);
 }
 
@@ -125,6 +151,9 @@ int main(void)
        printf("test_variable_size()\n");
        test_variable_size();
        
        printf("test_variable_size()\n");
        test_variable_size();
        
+       printf("test_padding_reads()\n");
+       test_padding_reads();
+       
        printf("All tests pass.\n");
        return 0;
 }
        printf("All tests pass.\n");
        return 0;
 }