X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Frbuf%2Ftest%2Frun-all.c;h=d7c1a3f1e32964379756c91ca5b8962dac7eaaa3;hp=2cd0b4241d2687e6edaf0e6c757ec656fd4eabe1;hb=d8a270fdfc57ffe7603028ecda99eeab3889ca70;hpb=44b37ee8aeb185f6837378fef17d887bda6ff89b diff --git a/ccan/rbuf/test/run-all.c b/ccan/rbuf/test/run-all.c index 2cd0b424..d7c1a3f1 100644 --- a/ccan/rbuf/test/run-all.c +++ b/ccan/rbuf/test/run-all.c @@ -7,6 +7,14 @@ #include #include +static bool test_realloc_fail; +static void *test_realloc(struct membuf *mb, void *buf, size_t n) +{ + if (test_realloc_fail) + return NULL; + return realloc(buf, n); +} + int main(void) { struct rbuf in; @@ -24,25 +32,29 @@ int main(void) } close(fd); - ok1(rbuf_open(&in, "run-all-file", NULL, 0)); - /* Can't fill without realloc. */ - ok1(!rbuf_fill(&in, NULL)); + ok1(rbuf_open(&in, "run-all-file", NULL, 0, test_realloc)); + /* Can't fill if realloc fails. */ + test_realloc_fail = true; + ok1(!rbuf_fill(&in)); ok1(errno == ENOMEM); - ok1(rbuf_fill(&in, realloc)); + test_realloc_fail = false; + ok1(rbuf_fill(&in)); /* But can't load in whole file. */ - ok1(!rbuf_fill_all(&in, NULL)); + test_realloc_fail = true; + ok1(!rbuf_fill_all(&in)); ok1(errno == ENOMEM); - ok1(rbuf_fill_all(&in, realloc)); - ok1(in.len == size); + test_realloc_fail = false; + ok1(rbuf_fill_all(&in)); + ok1(rbuf_len(&in) == size); for (i = 0; i * sizeof(buf) < size; i++) { memset(buf, 0x42 + i, sizeof(buf)); - if (memcmp(buf, in.start, sizeof(buf)) != 0) { + if (memcmp(buf, rbuf_start(&in), sizeof(buf)) != 0) { fail("Bad buffer contents"); break; } rbuf_consume(&in, sizeof(buf)); } - free(in.buf); + free(membuf_cleanup(&in.m)); /* This exits depending on whether all tests passed */ return exit_status();