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);
23 int i, fd = open("test/run.c", O_RDONLY), len;
25 /* This is how many tests you plan to run */
28 /* Grab ourselves for comparison. */
29 len = read(fd, buf, sizeof(buf));
31 lseek(fd, SEEK_SET, 0);
33 for (i = 0, p = buf; *p; i++) {
41 rbuf_init(&in, fd, malloc(31), 31, test_realloc);
43 ok1(membuf_num_space(&in.m) == 31);
44 test_realloc_fail = true;
45 p = rbuf_read_str(&in, '\n');
47 ok1(strcmp(p, lines[0]) == 0);
49 test_realloc_fail = false;
50 p = rbuf_read_str(&in, '\n');
52 ok1(strcmp(p, lines[1]) == 0);
54 for (i = 2; lines[i]; i++) {
55 ok1(p = rbuf_read_str(&in, '\n'));
56 ok1(strcmp(p, lines[i]) == 0);
59 p = rbuf_read_str(&in, '\n');
62 free(rbuf_cleanup(&in));
64 /* Another way of reading the entire (text) file. */
65 lseek(fd, SEEK_SET, 0);
66 rbuf_init(&in, fd, NULL, 0, test_realloc);
67 p = rbuf_read_str(&in, 0);
69 ok1(strlen(p) == len);
72 p = rbuf_read_str(&in, 0);
75 free(rbuf_cleanup(&in));