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