]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-02-expand.c
tdb2: use immobile free buckets, rename tests to show some ordering.
[ccan] / ccan / tdb2 / test / run-02-expand.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/check.c>
6 #include <ccan/tap/tap.h>
7 #include "logging.h"
8
9 int main(int argc, char *argv[])
10 {
11         unsigned int i;
12         uint64_t val;
13         struct tdb_context *tdb;
14         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
15                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
16                         TDB_NOMMAP|TDB_CONVERT };
17
18         plan_tests(sizeof(flags) / sizeof(flags[0]) * 18 + 1);
19
20         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
21                 tdb = tdb_open("run-expand.tdb", flags[i],
22                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
23                 ok1(tdb);
24                 if (!tdb)
25                         continue;
26
27                 /* First expand. Should add a zone, doubling file size.. */
28                 val = tdb->map_size - 1 - sizeof(struct tdb_header);
29                 ok1(tdb_expand(tdb, 1, 1, false) == 0);
30                 ok1(tdb->map_size == 2 * val + 1 + sizeof(struct tdb_header));
31                 ok1(tdb_check(tdb, NULL, NULL) == 0);
32
33                 /* Second expand, add another zone of same size. */
34                 ok1(tdb_expand(tdb, 1, 1, false) == 0);
35                 ok1(tdb->map_size == 3 * val + 1 + sizeof(struct tdb_header));
36                 ok1(tdb_check(tdb, NULL, NULL) == 0);
37
38                 /* Large expand, but can only add 4th zone of same size. */
39                 ok1(tdb_expand(tdb, 0, 4*val, false) == 0);
40                 ok1(tdb->map_size == 4 * val + 1 + sizeof(struct tdb_header));
41                 ok1(tdb_check(tdb, NULL, NULL) == 0);
42
43                 /* Large expand now will double file. */
44                 ok1(tdb_expand(tdb, 0, 4*val, false) == 0);
45                 ok1(tdb->map_size == 8 * val + 1 + sizeof(struct tdb_header));
46                 ok1(tdb_check(tdb, NULL, NULL) == 0);
47
48                 /* And again? */
49                 ok1(tdb_expand(tdb, 0, 4*val, false) == 0);
50                 ok1(tdb->map_size == 16 * val + 1 + sizeof(struct tdb_header));
51                 ok1(tdb_check(tdb, NULL, NULL) == 0);
52
53                 /* Below comfort level, will add a single 8*val zone. */
54                 ok1(tdb_expand(tdb, 0, ((8*val) >> TDB_COMFORT_FACTOR_BITS)
55                                - sizeof(struct tdb_used_record), false) == 0);
56                 ok1(tdb->map_size == 24 * val + 1 + sizeof(struct tdb_header));
57                 tdb_close(tdb);
58         }
59
60         ok1(tap_log_messages == 0);
61         return exit_status();
62 }