]> git.ozlabs.org Git - ccan/blobdiff - ccan/rbuf/test/run-term-eof.c
rbuf: adapt to work on ccan/membuf.
[ccan] / ccan / rbuf / test / run-term-eof.c
index 097dcbbda351492786cb79f0f4c11d4cb89f193c..25b060f73a60cdb4fc176bd24d6a911b049972dc 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;
@@ -14,7 +22,7 @@ int main(void)
        int fd = open("test/run-term-eof.c", O_RDONLY), len;
 
        /* This is how many tests you plan to run */
-       plan_tests(6);
+       plan_tests(10);
 
        /* Grab ourselves for comparison. */
        len = read(fd, buf, sizeof(buf));
@@ -22,23 +30,35 @@ int main(void)
        lseek(fd, SEEK_SET, 0);
 
        /* We have exact-size buffer, which causes problems adding term. */
-       rbuf_init(&in, fd, malloc(len), len);
-       p = rbuf_read_str(&in, 64, NULL); /* At symbol does not appear. */
+       rbuf_init(&in, fd, malloc(len), len, test_realloc);
+       test_realloc_fail = true;
+       p = rbuf_read_str(&in, 64); /* At symbol does not appear. */
        ok1(errno == ENOMEM);
        ok1(!p);
        /* This should succeed... */
-       p = rbuf_read_str(&in, 64, realloc);
+       test_realloc_fail = false;
+       p = rbuf_read_str(&in, 64);
        ok1(p);
        ok1(strcmp(p, buf) == 0);
-       free(in.buf);
+       ok1(rbuf_start(&in) == p + strlen(p));
+       free(rbuf_cleanup(&in));
 
        /* Try again. */
        lseek(fd, SEEK_SET, 0);
-       rbuf_init(&in, fd, malloc(len), len);
-       p = rbuf_read_str(&in, 64, realloc);
+       rbuf_init(&in, fd, malloc(len), len, test_realloc);
+       p = rbuf_read_str(&in, 64);
        ok1(p);
        ok1(strcmp(p, buf) == 0);
-       free(in.buf);
+       ok1(rbuf_start(&in) == p + strlen(p));
+       free(rbuf_cleanup(&in));
 
+       /* Normal case, we get rbuf_start after nul */
+       lseek(fd, SEEK_SET, 0);
+       rbuf_init(&in, fd, NULL, 0, test_realloc);
+       p = rbuf_read_str(&in, '^');
+       ok1(p);
+       ok1(rbuf_start(&in) == p + strlen(p) + 1);
+       free(rbuf_cleanup(&in));
+       
        return exit_status();
 }