merge
[ccan] / ccan / tdb / test / run-nested-traverse.c
1 #define _XOPEN_SOURCE 500
2 #include "tdb/tdb.h"
3 #include "tdb/io.c"
4 #include "tdb/tdb.c"
5 #include "tdb/lock.c"
6 #include "tdb/freelist.c"
7 #include "tdb/traverse.c"
8 #include "tdb/transaction.c"
9 #include "tdb/error.c"
10 #include "tdb/open.c"
11 #include "tap/tap.h"
12 #include <stdlib.h>
13 #include <stdbool.h>
14 #include <err.h>
15
16 static bool correct_key(TDB_DATA key)
17 {
18         return key.dsize == strlen("hi")
19                 && memcmp(key.dptr, "hi", key.dsize) == 0;
20 }
21
22 static bool correct_data(TDB_DATA data)
23 {
24         return data.dsize == strlen("world")
25                 && memcmp(data.dptr, "world", data.dsize) == 0;
26 }
27
28 static int traverse2(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
29                      void *p)
30 {
31         ok1(correct_key(key));
32         ok1(correct_data(data));
33         return 0;
34 }
35
36 static int traverse1(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
37                      void *p)
38 {
39         ok1(correct_key(key));
40         ok1(correct_data(data));
41         ok1(tdb->have_transaction_lock);
42         tdb_traverse(tdb, traverse2, NULL);
43
44         /* That should *not* release the transaction lock! */
45         ok1(tdb->have_transaction_lock);
46         return 0;
47 }
48
49 int main(int argc, char *argv[])
50 {
51         struct tdb_context *tdb;
52         TDB_DATA key, data;
53
54         plan_tests(14);
55         tdb = tdb_open("/tmp/test.tdb", 1024, TDB_CLEAR_IF_FIRST,
56                        O_CREAT|O_TRUNC|O_RDWR, 0600);
57         ok1(tdb);
58
59         /* Tickle bug on appending zero length buffer to zero length buffer. */
60         key.dsize = strlen("hi");
61         key.dptr = (void *)"hi";
62         data.dptr = (void *)"world";
63         data.dsize = strlen("world");
64
65         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
66         tdb_traverse(tdb, traverse1, NULL);
67         tdb_traverse_read(tdb, traverse1, NULL);
68         tdb_close(tdb);
69
70         return exit_status();
71 }