X-Git-Url: https://git.sesse.net/?p=fjl;a=blobdiff_plain;f=bytesource_test.c;h=6c692cbf1078447e654ff0bf1341d76327caeb5a;hp=4dc7661c0a3e66cb150e6e95c1c6f935b39027dd;hb=19a58db08afd149a862506936093423db756a2dc;hpb=56ceb8f3300b0ee121bddf572f935f87feedfea8 diff --git a/bytesource_test.c b/bytesource_test.c index 4dc7661..6c692cb 100644 --- a/bytesource_test.c +++ b/bytesource_test.c @@ -148,6 +148,39 @@ void test_broken_marker() assert(ret == -1); } +// Testing small reads -- even with a fast source, we shouldn't get more data +// back than we asked for. +void test_small_reads() +{ + uint8_t bytes[] = { 0xff, 0x80, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; + + struct custom_read_userdata ud; + ud.bytes = bytes; + ud.bytes_left = sizeof(bytes); + + struct byte_source source; + init_byte_source(&source, custom_read, &ud); + + uint8_t buf[4096]; + ssize_t ret; + + ret = byte_source_input_func(&source, buf, 4096); + assert(ret == 0); + + uint8_t marker = byte_source_read_marker(&source); + assert(marker == 0x80); + + for (unsigned i = 0; i < 8; ++i) { + ret = byte_source_input_func(&source, buf, 1); + assert(ret == 1); + assert(buf[0] == i); + } + + // Now EOF. + ret = byte_source_input_func(&source, buf, 4096); + assert(ret == 0); +} + int main(void) { init_choices(); @@ -161,6 +194,9 @@ int main(void) printf("test_broken_marker()\n"); test_broken_marker(); + printf("test_small_reads()\n"); + test_small_reads(); + printf("All tests pass.\n"); return 0; }