]> git.ozlabs.org Git - ccan/blobdiff - ccan/rbuf/test/run-all.c
rbuf: adapt to work on ccan/membuf.
[ccan] / ccan / rbuf / test / run-all.c
index 2cd0b4241d2687e6edaf0e6c757ec656fd4eabe1..d7c1a3f1e32964379756c91ca5b8962dac7eaaa3 100644 (file)
@@ -7,6 +7,14 @@
 #include <fcntl.h>
 #include <stdlib.h>
 
+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();