]> git.ozlabs.org Git - ccan/blob - ccan/rbuf/test/run-term-eof.c
097dcbbda351492786cb79f0f4c11d4cb89f193c
[ccan] / ccan / rbuf / test / run-term-eof.c
1 #include <ccan/rbuf/rbuf.h>
2 /* Include the C files directly. */
3 #include <ccan/rbuf/rbuf.c>
4 #include <ccan/tap/tap.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <stdlib.h>
9
10 int main(void)
11 {
12         struct rbuf in;
13         char buf[4096], *p;
14         int fd = open("test/run-term-eof.c", O_RDONLY), len;
15
16         /* This is how many tests you plan to run */
17         plan_tests(6);
18
19         /* Grab ourselves for comparison. */
20         len = read(fd, buf, sizeof(buf));
21         buf[len] = '\0';
22         lseek(fd, SEEK_SET, 0);
23
24         /* We have exact-size buffer, which causes problems adding term. */
25         rbuf_init(&in, fd, malloc(len), len);
26         p = rbuf_read_str(&in, 64, NULL); /* At symbol does not appear. */
27         ok1(errno == ENOMEM);
28         ok1(!p);
29         /* This should succeed... */
30         p = rbuf_read_str(&in, 64, realloc);
31         ok1(p);
32         ok1(strcmp(p, buf) == 0);
33         free(in.buf);
34
35         /* Try again. */
36         lseek(fd, SEEK_SET, 0);
37         rbuf_init(&in, fd, malloc(len), len);
38         p = rbuf_read_str(&in, 64, realloc);
39         ok1(p);
40         ok1(strcmp(p, buf) == 0);
41         free(in.buf);
42
43         return exit_status();
44 }