]> git.ozlabs.org Git - ccan/blob - ccan/rbuf/test/run-partial-read.c
727fade8abdc9a8e2e86e76b2c1fc6678413bbd4
[ccan] / ccan / rbuf / test / run-partial-read.c
1 #include <unistd.h>
2
3 static ssize_t partial_read(int fd, void *buf, size_t count)
4 {
5         return read(fd, buf, 1);
6 }
7 static ssize_t full_read(int fd, void *buf, size_t count)
8 {
9         return read(fd, buf, count);
10 }
11 #define read partial_read
12
13 #include <ccan/rbuf/rbuf.h>
14 /* Include the C files directly. */
15 #include <ccan/rbuf/rbuf.c>
16 #include <ccan/tap/tap.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <stdlib.h>
21
22 int main(void)
23 {
24         struct rbuf in;
25         char buf[4096];
26         char *lines[100], *p;
27         int i, fd = open("test/run.c", O_RDONLY);
28
29         /* This is how many tests you plan to run */
30         plan_tests(160);
31
32         /* Grab ourselves for comparison. */
33         buf[full_read(fd, buf, sizeof(buf))] = '\0';
34         lseek(fd, SEEK_SET, 0);
35
36         for (i = 0, p = buf; *p; i++) {
37                 lines[i] = p;
38                 p = strchr(p, '\n');
39                 *p = '\0';
40                 p++;
41         }
42         lines[i] = NULL;
43
44         rbuf_init(&in, fd, malloc(31), 31, membuf_realloc);
45         ok1(in.fd == fd);
46         ok1(membuf_num_space(&in.m) == 31);
47         p = rbuf_read_str(&in, '\n');
48         ok1(p);
49         ok1(strcmp(p, lines[0]) == 0);
50
51         p = rbuf_read_str(&in, '\n');
52         ok1(p);
53         ok1(strcmp(p, lines[1]) == 0);
54
55         for (i = 2; lines[i]; i++) {
56                 ok1(p = rbuf_read_str(&in, '\n'));
57                 ok1(strcmp(p, lines[i]) == 0);
58         }
59
60         p = rbuf_read_str(&in, '\n');
61         ok1(errno == 0);
62         ok1(p == NULL);
63         free(membuf_cleanup(&in.m));
64
65         /* This exits depending on whether all tests passed */
66         return exit_status();
67 }