lib/tdb2: adapt unit tests to SAMBA environment.
[kai/samba-autobuild/.git] / lib / tdb2 / test / run-50-multiple-freelists.c
1 #include "tdb2-source.h"
2 #include "tap-interface.h"
3 #include "logging.h"
4 #include "layout.h"
5
6 int main(int argc, char *argv[])
7 {
8         tdb_off_t off;
9         struct tdb_context *tdb;
10         struct tdb_layout *layout;
11         TDB_DATA key, data;
12         union tdb_attribute seed;
13
14         /* This seed value previously tickled a layout.c bug. */
15         seed.base.attr = TDB_ATTRIBUTE_SEED;
16         seed.seed.seed = 0xb1142bc054d035b4ULL;
17         seed.base.next = &tap_log_attr;
18
19         plan_tests(11);
20         key = tdb_mkdata("Hello", 5);
21         data = tdb_mkdata("world", 5);
22
23         /* Create a TDB with three free tables. */
24         layout = new_tdb_layout();
25         tdb_layout_add_freetable(layout);
26         tdb_layout_add_freetable(layout);
27         tdb_layout_add_freetable(layout);
28         tdb_layout_add_free(layout, 80, 0);
29         /* Used record prevent coalescing. */
30         tdb_layout_add_used(layout, key, data, 6);
31         tdb_layout_add_free(layout, 160, 1);
32         key.dsize--;
33         tdb_layout_add_used(layout, key, data, 7);
34         tdb_layout_add_free(layout, 320, 2);
35         key.dsize--;
36         tdb_layout_add_used(layout, key, data, 8);
37         tdb_layout_add_free(layout, 40, 0);
38         tdb = tdb_layout_get(layout, free, &seed);
39         ok1(tdb_check(tdb, NULL, NULL) == 0);
40
41         off = get_free(tdb, 0, 80 - sizeof(struct tdb_used_record), 0,
42                        TDB_USED_MAGIC, 0);
43         ok1(off == layout->elem[3].base.off);
44         ok1(tdb->tdb2.ftable_off == layout->elem[0].base.off);
45
46         off = get_free(tdb, 0, 160 - sizeof(struct tdb_used_record), 0,
47                        TDB_USED_MAGIC, 0);
48         ok1(off == layout->elem[5].base.off);
49         ok1(tdb->tdb2.ftable_off == layout->elem[1].base.off);
50
51         off = get_free(tdb, 0, 320 - sizeof(struct tdb_used_record), 0,
52                        TDB_USED_MAGIC, 0);
53         ok1(off == layout->elem[7].base.off);
54         ok1(tdb->tdb2.ftable_off == layout->elem[2].base.off);
55
56         off = get_free(tdb, 0, 40 - sizeof(struct tdb_used_record), 0,
57                        TDB_USED_MAGIC, 0);
58         ok1(off == layout->elem[9].base.off);
59         ok1(tdb->tdb2.ftable_off == layout->elem[0].base.off);
60
61         /* Now we fail. */
62         off = get_free(tdb, 0, 0, 1, TDB_USED_MAGIC, 0);
63         ok1(off == 0);
64
65         tdb_close(tdb);
66         tdb_layout_free(layout);
67
68         ok1(tap_log_messages == 0);
69         return exit_status();
70 }