]> git.ozlabs.org Git - ccan/blob - ccan/rbuf/test/run.c
endian: add constant versions.
[ccan] / ccan / rbuf / test / run.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];
14         char *lines[100], *p;
15         int i, fd = open("test/run.c", O_RDONLY), len;
16
17         /* This is how many tests you plan to run */
18         plan_tests(144);
19
20         /* Grab ourselves for comparison. */
21         len = read(fd, buf, sizeof(buf));
22         buf[len] = '\0';
23         lseek(fd, SEEK_SET, 0);
24
25         for (i = 0, p = buf; *p; i++) {
26                 lines[i] = p;
27                 p = strchr(p, '\n');
28                 *p = '\0';
29                 p++;
30         }
31         lines[i] = NULL;
32
33         rbuf_init(&in, fd, malloc(31), 31);
34         ok1(in.fd == fd);
35         ok1(in.buf_end - in.buf == 31);
36         p = rbuf_read_str(&in, '\n', NULL);
37         ok1(p);
38         ok1(strcmp(p, lines[0]) == 0);
39
40         p = rbuf_read_str(&in, '\n', realloc);
41         ok1(p);
42         ok1(strcmp(p, lines[1]) == 0);
43
44         for (i = 2; lines[i]; i++) {
45                 ok1(p = rbuf_read_str(&in, '\n', realloc));
46                 ok1(strcmp(p, lines[i]) == 0);
47         }
48
49         p = rbuf_read_str(&in, '\n', realloc);
50         ok1(errno == 0);
51         ok1(p == NULL);
52         free(in.buf);
53
54         /* Another way of reading the entire (text) file. */
55         lseek(fd, SEEK_SET, 0);
56         rbuf_init(&in, fd, NULL, 0);
57         p = rbuf_read_str(&in, 0, realloc);
58         ok1(p);
59         ok1(strlen(p) == len);
60
61         close(fd);
62         p = rbuf_read_str(&in, 0, realloc);
63         ok1(errno == EBADF);
64         ok1(!p);
65         free(in.buf);
66
67         return exit_status();
68 }