]> git.ozlabs.org Git - ccan/blob - ccan/rbuf/test/run-all.c
2cd0b4241d2687e6edaf0e6c757ec656fd4eabe1
[ccan] / ccan / rbuf / test / run-all.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         int i, size, fd = open("run-all-file", O_WRONLY|O_CREAT, 0600);
15
16         /* This is how many tests you plan to run */
17         plan_tests(8);
18
19         /* Make sure we're bigger than a single buffer! */
20         size = rbuf_good_size(fd)*2;
21         for (i = 0; i * sizeof(buf) < size; i++) {
22                 memset(buf, 0x42 + i, sizeof(buf));
23                 write(fd, buf, sizeof(buf));
24         }
25         close(fd);
26
27         ok1(rbuf_open(&in, "run-all-file", NULL, 0));
28         /* Can't fill without realloc. */
29         ok1(!rbuf_fill(&in, NULL));
30         ok1(errno == ENOMEM);
31         ok1(rbuf_fill(&in, realloc));
32         /* But can't load in whole file. */
33         ok1(!rbuf_fill_all(&in, NULL));
34         ok1(errno == ENOMEM);
35         ok1(rbuf_fill_all(&in, realloc));
36         ok1(in.len == size);
37         for (i = 0; i * sizeof(buf) < size; i++) {
38                 memset(buf, 0x42 + i, sizeof(buf));
39                 if (memcmp(buf, in.start, sizeof(buf)) != 0) {
40                         fail("Bad buffer contents");
41                         break;
42                 }
43                 rbuf_consume(&in, sizeof(buf));
44         }
45         free(in.buf);
46
47         /* This exits depending on whether all tests passed */
48         return exit_status();
49 }