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