X-Git-Url: https://git.sesse.net/?p=fjl;a=blobdiff_plain;f=bitsource_test.c;h=173e499b169c469414ce6303ccbeced15ca07c2e;hp=2b5217ff91a283fee9c544430bf73271cb565cfc;hb=9069ae93f3e9e9b44f5f28968f508d09f0711737;hpb=8445e04bae016b6bd74705902972e66ae7fe37c1 diff --git a/bitsource_test.c b/bitsource_test.c index 2b5217f..173e499 100644 --- a/bitsource_test.c +++ b/bitsource_test.c @@ -40,7 +40,7 @@ void test_basic_reading() 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); @@ -60,7 +60,7 @@ void test_slow_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); @@ -80,7 +80,7 @@ void test_variable_size() 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); @@ -111,6 +111,32 @@ void test_variable_size() 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); } @@ -125,6 +151,9 @@ int main(void) printf("test_variable_size()\n"); test_variable_size(); + printf("test_padding_reads()\n"); + test_padding_reads(); + printf("All tests pass.\n"); return 0; }