2d7193efcc407027c93dc4d6d5fbb0fc519c9502
[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 #include "external-transaction.h"
16
17 static struct agent *agent;
18
19 static bool correct_key(TDB_DATA key)
20 {
21         return key.dsize == strlen("hi")
22                 && memcmp(key.dptr, "hi", key.dsize) == 0;
23 }
24
25 static bool correct_data(TDB_DATA data)
26 {
27         return data.dsize == strlen("world")
28                 && memcmp(data.dptr, "world", data.dsize) == 0;
29 }
30
31 static int traverse2(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
32                      void *p)
33 {
34         ok1(correct_key(key));
35         ok1(correct_data(data));
36         return 0;
37 }
38
39 static int traverse1(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
40                      void *p)
41 {
42         ok1(correct_key(key));
43         ok1(correct_data(data));
44         ok1(!external_agent_transaction(agent, tdb_name(tdb)));
45         tdb_traverse(tdb, traverse2, NULL);
46
47         /* That should *not* release the transaction lock! */
48         ok1(!external_agent_transaction(agent, tdb_name(tdb)));
49         return 0;
50 }
51
52 int main(int argc, char *argv[])
53 {
54         struct tdb_context *tdb;
55         TDB_DATA key, data;
56
57         plan_tests(15);
58         agent = prepare_external_agent();
59         if (!agent)
60                 err(1, "preparing agent");
61
62         tdb = tdb_open("/tmp/test3.tdb", 1024, TDB_CLEAR_IF_FIRST,
63                        O_CREAT|O_TRUNC|O_RDWR, 0600);
64         ok1(tdb);
65
66         ok1(external_agent_transaction(agent, tdb_name(tdb)));
67
68         key.dsize = strlen("hi");
69         key.dptr = (void *)"hi";
70         data.dptr = (void *)"world";
71         data.dsize = strlen("world");
72
73         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
74         tdb_traverse(tdb, traverse1, NULL);
75         tdb_traverse_read(tdb, traverse1, NULL);
76         tdb_close(tdb);
77
78         return exit_status();
79 }