1 | // RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s |
2 | |
3 | #include <sys/param.h> |
4 | |
5 | #include <sys/types.h> |
6 | |
7 | #include <sys/mman.h> |
8 | #include <sys/stat.h> |
9 | |
10 | #include <assert.h> |
11 | #include <cdbr.h> |
12 | #include <cdbw.h> |
13 | #include <fcntl.h> |
14 | #include <stdio.h> |
15 | #include <stdlib.h> |
16 | #include <string.h> |
17 | #include <unistd.h> |
18 | |
19 | static char *name; |
20 | |
21 | const char data1[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; |
22 | const char data2[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17}; |
23 | const char key1[] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27}; |
24 | const char key2[] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37}; |
25 | |
26 | void test_cdbw() { |
27 | uint32_t idx; |
28 | |
29 | struct cdbw *cdbw = cdbw_open(); |
30 | assert(cdbw); |
31 | |
32 | int rv = cdbw_put_data(cdbw, data1, __arraycount(data1), &idx); |
33 | assert(!rv); |
34 | |
35 | rv = cdbw_put_key(cdbw, key1, __arraycount(key1), idx); |
36 | assert(!rv); |
37 | |
38 | rv = cdbw_put(cdbw, key2, __arraycount(key2), data2, __arraycount(data2)); |
39 | assert(!rv); |
40 | |
41 | name = strdup(s: "/tmp/temp.XXXXXX" ); |
42 | assert(name); |
43 | |
44 | name = mktemp(template: name); |
45 | assert(name); |
46 | |
47 | int fd = open(file: name, O_RDWR | O_CREAT, 0644); |
48 | assert(fd != -1); |
49 | |
50 | cdbw_output(cdbw, fd, "TEST1" , cdbw_stable_seeder); |
51 | |
52 | cdbw_close(cdbw); |
53 | |
54 | rv = close(fd: fd); |
55 | assert(rv != -1); |
56 | } |
57 | |
58 | void test_cdbr1() { |
59 | struct cdbr *cdbr = cdbr_open(name, CDBR_DEFAULT); |
60 | assert(cdbr); |
61 | |
62 | uint32_t idx = cdbr_entries(cdbr); |
63 | assert(idx > 0); |
64 | printf("entries: %" PRIu32 "\n" , idx); |
65 | |
66 | const void *data; |
67 | size_t data_len; |
68 | int rv = cdbr_get(cdbr, idx - 1, &data, &data_len); |
69 | assert(rv == 0); |
70 | |
71 | printf(format: "data: " ); |
72 | for (size_t i = 0; i < data_len; i++) |
73 | printf("%02" PRIx8, ((uint8_t *)data)[i]); |
74 | printf(format: "\n" ); |
75 | |
76 | rv = cdbr_find(cdbr, key1, __arraycount(key1), &data, &data_len); |
77 | |
78 | printf(format: "data: " ); |
79 | for (size_t i = 0; i < data_len; i++) |
80 | printf("%02" PRIx8, ((uint8_t *)data)[i]); |
81 | printf(format: "\n" ); |
82 | |
83 | cdbr_close(cdbr); |
84 | } |
85 | |
86 | #define COOKIE ((void *)1) |
87 | |
88 | static void cdbr_unmap(void *cookie, void *base, size_t sz) { |
89 | assert(cookie == COOKIE); |
90 | int rv = munmap(addr: base, len: sz); |
91 | assert(rv != -1); |
92 | } |
93 | |
94 | void test_cdbr2() { |
95 | struct stat sb; |
96 | |
97 | int fd = open(file: name, O_RDONLY); |
98 | assert(fd != -1); |
99 | |
100 | int rv = fstat(fd: fd, buf: &sb); |
101 | assert(rv != -1); |
102 | |
103 | size_t sz = sb.st_size; |
104 | assert(sz < SSIZE_MAX); |
105 | |
106 | void *base = mmap(NULL, len: sz, PROT_READ, MAP_FILE | MAP_SHARED, fd: fd, offset: 0); |
107 | assert(base != MAP_FAILED); |
108 | |
109 | rv = close(fd: fd); |
110 | assert(rv != -1); |
111 | |
112 | struct cdbr *cdbr = cdbr_open_mem(base, sz, CDBR_DEFAULT, cdbr_unmap, COOKIE); |
113 | assert(cdbr); |
114 | |
115 | printf("entries: %" PRIu32 "\n" , cdbr_entries(cdbr)); |
116 | |
117 | cdbr_close(cdbr); |
118 | } |
119 | |
120 | int main(void) { |
121 | printf(format: "cdb\n" ); |
122 | |
123 | test_cdbw(); |
124 | test_cdbr1(); |
125 | test_cdbr2(); |
126 | |
127 | // CHECK: cdb |
128 | // CHECK: entries: 2 |
129 | // CHECK: data: 1011121314151617 |
130 | // CHECK: data: 0001020304050607 |
131 | // CHECK: entries: 2 |
132 | |
133 | return 0; |
134 | } |
135 | |