1 #include <ccan/rbuf/rbuf.h>
2 /* Include the C files directly. */
3 #include <ccan/rbuf/rbuf.c>
4 #include <ccan/tap/tap.h>
10 static bool test_realloc_fail;
11 static void *test_realloc(struct membuf *mb, void *buf, size_t n)
13 if (test_realloc_fail)
15 return realloc(buf, n);
22 int fd = open("test/run-term-eof.c", O_RDONLY), len;
24 /* This is how many tests you plan to run */
27 /* Grab ourselves for comparison. */
28 len = read(fd, buf, sizeof(buf));
30 lseek(fd, SEEK_SET, 0);
32 /* We have exact-size buffer, which causes problems adding term. */
33 rbuf_init(&in, fd, malloc(len), len, test_realloc);
34 test_realloc_fail = true;
35 p = rbuf_read_str(&in, 64); /* At symbol does not appear. */
38 /* This should succeed... */
39 test_realloc_fail = false;
40 p = rbuf_read_str(&in, 64);
42 ok1(strcmp(p, buf) == 0);
43 ok1(rbuf_start(&in) == p + strlen(p));
44 free(rbuf_cleanup(&in));
47 lseek(fd, SEEK_SET, 0);
48 rbuf_init(&in, fd, malloc(len), len, test_realloc);
49 p = rbuf_read_str(&in, 64);
51 ok1(strcmp(p, buf) == 0);
52 ok1(rbuf_start(&in) == p + strlen(p));
53 free(rbuf_cleanup(&in));
55 /* Normal case, we get rbuf_start after nul */
56 lseek(fd, SEEK_SET, 0);
57 rbuf_init(&in, fd, NULL, 0, test_realloc);
58 p = rbuf_read_str(&in, '^');
60 ok1(rbuf_start(&in) == p + strlen(p) + 1);
61 free(rbuf_cleanup(&in));