From acee16d884515339fc9df08e328b65e133783a40 Mon Sep 17 00:00:00 2001 From: leitner Date: Fri, 30 Oct 2020 15:18:37 +0000 Subject: [PATCH] add unit tests for bs_capacitycheck (same as for bs_capacityassert basically) --- buffer/bs_capacitycheck.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/buffer/bs_capacitycheck.c b/buffer/bs_capacitycheck.c index dfc2c84..5c4d9da 100644 --- a/buffer/bs_capacitycheck.c +++ b/buffer/bs_capacitycheck.c @@ -10,3 +10,31 @@ int bs_capacitycheck(struct bytestream* bs,size_t capacity) { if (bs->max - bs->cur < capacity) return 0; // not EOF but less than that many bytes left return 1; } + +#ifdef UNITTEST +#include + +#undef UNITTEST +#include "buffer/bs_err.c" + +int main() { + struct bytestream bs; + // test basic functionality + bs.cur=0; bs.max=100; + assert(bs_capacitycheck(&bs, 100) == 1); + assert(bs_err(&bs) == 0); + + bs.cur=50; bs.max=100; + assert(bs_capacitycheck(&bs, 50) == 1); + assert(bs_err(&bs) == 0); + + assert(bs_capacitycheck(&bs, 51) == 0); + assert(bs_err(&bs) == 0); // capacitycheck should not set error. capacityassert should + + // try to provoke a numeric overflow + bs.cur=100; bs.max=(size_t)-1; + assert(bs_capacitycheck(&bs, (size_t)-50) == 0); + assert(bs_err(&bs) == 0); // should still not set an error +} + +#endif