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