tdb2: allow multiple chain locks.
[ccan] / ccan / tdb2 / test / run-50-multiple-freelists.c
1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/open.c>
3 #include <ccan/tdb2/free.c>
4 #include <ccan/tdb2/lock.c>
5 #include <ccan/tdb2/io.c>
6 #include <ccan/tdb2/hash.c>
7 #include <ccan/tdb2/check.c>
8 #include <ccan/tap/tap.h>
9 #include <ccan/tdb2/transaction.c>
10 #include "logging.h"
11 #include "layout.h"
12
13 int main(int argc, char *argv[])
14 {
15         tdb_off_t off;
16         struct tdb_context *tdb;
17         struct tdb_layout *layout;
18         TDB_DATA key, data;
19
20         plan_tests(11);
21         key = tdb_mkdata("Hello", 5);
22         data = tdb_mkdata("world", 5);
23
24         /* Create a TDB with three free tables. */
25         layout = new_tdb_layout(NULL);
26         tdb_layout_add_freetable(layout);
27         tdb_layout_add_freetable(layout);
28         tdb_layout_add_freetable(layout);
29         tdb_layout_add_free(layout, 80, 0);
30         /* Used record prevent coalescing. */
31         tdb_layout_add_used(layout, key, data, 6);
32         tdb_layout_add_free(layout, 160, 1);
33         key.dsize--;
34         tdb_layout_add_used(layout, key, data, 7);
35         tdb_layout_add_free(layout, 320, 2);
36         key.dsize--;
37         tdb_layout_add_used(layout, key, data, 8);
38         tdb_layout_add_free(layout, 40, 0);
39         tdb = tdb_layout_get(layout);
40         ok1(tdb_check(tdb, NULL, NULL) == 0);
41
42         off = get_free(tdb, 0, 80 - sizeof(struct tdb_used_record), 0,
43                        TDB_USED_MAGIC, 0);
44         ok1(off == layout->elem[3].base.off);
45         ok1(tdb->ftable_off == layout->elem[0].base.off);
46
47         off = get_free(tdb, 0, 160 - sizeof(struct tdb_used_record), 0,
48                        TDB_USED_MAGIC, 0);
49         ok1(off == layout->elem[5].base.off);
50         ok1(tdb->ftable_off == layout->elem[1].base.off);
51
52         off = get_free(tdb, 0, 320 - sizeof(struct tdb_used_record), 0,
53                        TDB_USED_MAGIC, 0);
54         ok1(off == layout->elem[7].base.off);
55         ok1(tdb->ftable_off == layout->elem[2].base.off);
56
57         off = get_free(tdb, 0, 40 - sizeof(struct tdb_used_record), 0,
58                        TDB_USED_MAGIC, 0);
59         ok1(off == layout->elem[9].base.off);
60         ok1(tdb->ftable_off == layout->elem[0].base.off);
61
62         /* Now we fail. */
63         off = get_free(tdb, 0, 0, 1, TDB_USED_MAGIC, 0);
64         ok1(off == 0);
65
66         tdb_close(tdb);
67         tdb_layout_free(layout);
68
69         ok1(tap_log_messages == 0);
70         return exit_status();
71 }