]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-50-multiple-freelists.c
28bf2252726e98d78bc633a95a9bc44402a237e3
[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 "logging.h"
9 #include "layout.h"
10
11 int main(int argc, char *argv[])
12 {
13         tdb_off_t off;
14         struct tdb_context *tdb;
15         struct tdb_layout *layout;
16         TDB_DATA key, data;
17
18         plan_tests(11);
19         key.dptr = (unsigned char *)"Hello";
20         data.dptr = (unsigned char *)"world";
21         data.dsize = 5;
22         key.dsize = 5;
23
24         /* Create a TDB with three free lists. */
25         layout = new_tdb_layout(NULL);
26         tdb_layout_add_freelist(layout);
27         tdb_layout_add_freelist(layout);
28         tdb_layout_add_freelist(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, 0);
43         ok1(off == layout->elem[3].base.off);
44         ok1(tdb->flist_off == layout->elem[0].base.off);
45
46         off = get_free(tdb, 0, 160 - sizeof(struct tdb_used_record), 0, 0);
47         ok1(off == layout->elem[5].base.off);
48         ok1(tdb->flist_off == layout->elem[1].base.off);
49
50         off = get_free(tdb, 0, 320 - sizeof(struct tdb_used_record), 0, 0);
51         ok1(off == layout->elem[7].base.off);
52         ok1(tdb->flist_off == layout->elem[2].base.off);
53
54         off = get_free(tdb, 0, 40 - sizeof(struct tdb_used_record), 0, 0);
55         ok1(off == layout->elem[9].base.off);
56         ok1(tdb->flist_off == layout->elem[0].base.off);
57
58         /* Now we fail. */
59         off = get_free(tdb, 0, 0, 1, 0);
60         ok1(off == 0);
61
62         tdb_close(tdb);
63
64         ok1(tap_log_messages == 0);
65         return exit_status();
66 }