]> git.ozlabs.org Git - ccan/blobdiff - ccan/rbuf/test/run.c
rbuf, tools: clean up rbuf usage.
[ccan] / ccan / rbuf / test / run.c
index e576188180e421fe7d7b4bd698a89a0d52841a1d..593ef2708b3d93d4e433c17edee2d4bdf9b09175 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;
@@ -15,7 +23,7 @@ int main(void)
        int i, fd = open("test/run.c", O_RDONLY), len;
 
        /* This is how many tests you plan to run */
-       plan_tests(144);
+       plan_tests(164);
 
        /* Grab ourselves for comparison. */
        len = read(fd, buf, sizeof(buf));
@@ -30,39 +38,41 @@ int main(void)
        }
        lines[i] = NULL;
 
-       rbuf_init(&in, fd, malloc(31), 31);
+       rbuf_init(&in, fd, malloc(31), 31, test_realloc);
        ok1(in.fd == fd);
-       ok1(in.buf_end - in.buf == 31);
-       p = rbuf_read_str(&in, '\n', NULL);
+       ok1(membuf_num_space(&in.m) == 31);
+       test_realloc_fail = true;
+       p = rbuf_read_str(&in, '\n');
        ok1(p);
        ok1(strcmp(p, lines[0]) == 0);
 
-       p = rbuf_read_str(&in, '\n', realloc);
+       test_realloc_fail = false;
+       p = rbuf_read_str(&in, '\n');
        ok1(p);
        ok1(strcmp(p, lines[1]) == 0);
 
        for (i = 2; lines[i]; i++) {
-               ok1(p = rbuf_read_str(&in, '\n', realloc));
+               ok1(p = rbuf_read_str(&in, '\n'));
                ok1(strcmp(p, lines[i]) == 0);
        }
 
-       p = rbuf_read_str(&in, '\n', realloc);
+       p = rbuf_read_str(&in, '\n');
        ok1(errno == 0);
        ok1(p == NULL);
-       free(in.buf);
+       free(rbuf_cleanup(&in));
 
        /* Another way of reading the entire (text) file. */
        lseek(fd, SEEK_SET, 0);
-       rbuf_init(&in, fd, NULL, 0);
-       p = rbuf_read_str(&in, 0, realloc);
+       rbuf_init(&in, fd, NULL, 0, test_realloc);
+       p = rbuf_read_str(&in, 0);
        ok1(p);
        ok1(strlen(p) == len);
 
        close(fd);
-       p = rbuf_read_str(&in, 0, realloc);
+       p = rbuf_read_str(&in, 0);
        ok1(errno == EBADF);
        ok1(!p);
-       free(in.buf);
+       free(rbuf_cleanup(&in));
 
        return exit_status();
 }