tdb2: make tests include a single mega-header to simplify future patches
[ccan] / ccan / tdb2 / test / run-firstkey-nextkey.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
4
5 #define NUM_RECORDS 1000
6
7 static bool store_records(struct tdb_context *tdb)
8 {
9         int i;
10         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
11         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
12
13         for (i = 0; i < NUM_RECORDS; i++)
14                 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
15                         return false;
16         return true;
17 }
18
19 struct trav_data {
20         unsigned int records[NUM_RECORDS];
21         unsigned int calls;
22 };
23
24 static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *p)
25 {
26         struct trav_data *td = p;
27         int val;
28
29         memcpy(&val, dbuf.dptr, dbuf.dsize);
30         td->records[td->calls++] = val;
31         return 0;
32 }
33
34 /* Since tdb_nextkey frees dptr, we need to clone it. */
35 static TDB_DATA dup_key(TDB_DATA key)
36 {
37         void *p = malloc(key.dsize);
38         memcpy(p, key.dptr, key.dsize);
39         key.dptr = p;
40         return key;
41 }
42
43 int main(int argc, char *argv[])
44 {
45         unsigned int i, j;
46         int num;
47         struct trav_data td;
48         TDB_DATA k;
49         struct tdb_context *tdb;
50         union tdb_attribute seed_attr;
51         enum TDB_ERROR ecode;
52
53         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
54                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
55                         TDB_NOMMAP|TDB_CONVERT };
56
57         seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
58         seed_attr.base.next = &tap_log_attr;
59         seed_attr.seed.seed = 6334326220117065685ULL;
60
61         plan_tests(sizeof(flags) / sizeof(flags[0])
62                    * (NUM_RECORDS*6 + (NUM_RECORDS-1)*3 + 22) + 1);
63         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
64                 tdb = tdb_open("run-traverse.tdb", flags[i],
65                                O_RDWR|O_CREAT|O_TRUNC, 0600, &seed_attr);
66                 ok1(tdb);
67                 if (!tdb)
68                         continue;
69
70                 ok1(tdb_firstkey(tdb, &k) == TDB_ERR_NOEXIST);
71
72                 /* One entry... */
73                 k.dptr = (unsigned char *)&num;
74                 k.dsize = sizeof(num);
75                 num = 0;
76                 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
77                 ok1(tdb_firstkey(tdb, &k) == TDB_SUCCESS);
78                 ok1(k.dsize == sizeof(num));
79                 ok1(memcmp(k.dptr, &num, sizeof(num)) == 0);
80                 ok1(tdb_nextkey(tdb, &k) == TDB_ERR_NOEXIST);
81
82                 /* Two entries. */
83                 k.dptr = (unsigned char *)&num;
84                 k.dsize = sizeof(num);
85                 num = 1;
86                 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
87                 ok1(tdb_firstkey(tdb, &k) == TDB_SUCCESS);
88                 ok1(k.dsize == sizeof(num));
89                 memcpy(&num, k.dptr, sizeof(num));
90                 ok1(num == 0 || num == 1);
91                 ok1(tdb_nextkey(tdb, &k) == TDB_SUCCESS);
92                 ok1(k.dsize == sizeof(j));
93                 memcpy(&j, k.dptr, sizeof(j));
94                 ok1(j == 0 || j == 1);
95                 ok1(j != num);
96                 ok1(tdb_nextkey(tdb, &k) == TDB_ERR_NOEXIST);
97
98                 /* Clean up. */
99                 k.dptr = (unsigned char *)&num;
100                 k.dsize = sizeof(num);
101                 num = 0;
102                 ok1(tdb_delete(tdb, k) == 0);
103                 num = 1;
104                 ok1(tdb_delete(tdb, k) == 0);
105
106                 /* Now lots of records. */
107                 ok1(store_records(tdb));
108                 td.calls = 0;
109
110                 num = tdb_traverse(tdb, trav, &td);
111                 ok1(num == NUM_RECORDS);
112                 ok1(td.calls == NUM_RECORDS);
113
114                 /* Simple loop should match tdb_traverse */
115                 for (j = 0, ecode = tdb_firstkey(tdb, &k); j < td.calls; j++) {
116                         int val;
117
118                         ok1(ecode == TDB_SUCCESS);
119                         ok1(k.dsize == sizeof(val));
120                         memcpy(&val, k.dptr, k.dsize);
121                         ok1(td.records[j] == val);
122                         ecode = tdb_nextkey(tdb, &k);
123                 }
124
125                 /* But arbitrary orderings should work too. */
126                 for (j = td.calls-1; j > 0; j--) {
127                         k.dptr = (unsigned char *)&td.records[j-1];
128                         k.dsize = sizeof(td.records[j-1]);
129                         k = dup_key(k);
130                         ok1(tdb_nextkey(tdb, &k) == TDB_SUCCESS);
131                         ok1(k.dsize == sizeof(td.records[j]));
132                         ok1(memcmp(k.dptr, &td.records[j], k.dsize) == 0);
133                         free(k.dptr);
134                 }
135
136                 /* Even delete should work. */
137                 for (j = 0, ecode = tdb_firstkey(tdb, &k);
138                      ecode != TDB_ERR_NOEXIST;
139                      j++) {
140                         ok1(ecode == TDB_SUCCESS);
141                         ok1(k.dsize == 4);
142                         ok1(tdb_delete(tdb, k) == 0);
143                         ecode = tdb_nextkey(tdb, &k);
144                 }
145
146                 diag("delete using first/nextkey gave %u of %u records",
147                      j, NUM_RECORDS);
148                 ok1(j == NUM_RECORDS);
149                 tdb_close(tdb);
150         }
151
152         ok1(tap_log_messages == 0);
153         return exit_status();
154 }