b681c05c1b6a5519e173d4d88541a3634a8593c3
[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/tdb2/hash.c>
7 #include <ccan/tap/tap.h>
8 #include "logging.h"
9
10 int main(int argc, char *argv[])
11 {
12         unsigned int i;
13         uint64_t val;
14         struct tdb_context *tdb;
15         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
16                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
17                         TDB_NOMMAP|TDB_CONVERT };
18
19         plan_tests(sizeof(flags) / sizeof(flags[0]) * 18 + 1);
20
21         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
22                 tdb = tdb_open("run-expand.tdb", flags[i],
23                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
24                 ok1(tdb);
25                 if (!tdb)
26                         continue;
27
28                 /* First expand. Should not fill zone. */
29                 val = tdb->map_size - sizeof(struct tdb_header);
30                 ok1(tdb_expand(tdb, 1) == 0);
31                 ok1(tdb->map_size < sizeof(struct tdb_header)
32                     + (1 << INITIAL_ZONE_BITS));
33                 ok1(tdb_check(tdb, NULL, NULL) == 0);
34
35                 /* Fill zone. */
36                 val = (1<<INITIAL_ZONE_BITS)
37                         - sizeof(struct tdb_used_record)
38                         - (tdb->map_size - sizeof(struct tdb_header));
39                 ok1(tdb_expand(tdb, val) == 0);
40                 ok1(tdb->map_size == sizeof(struct tdb_header)
41                     + (1 << INITIAL_ZONE_BITS));
42                 ok1(tdb_check(tdb, NULL, NULL) == 0);
43
44                 /* Second expand, adds another zone of same size. */
45                 ok1(tdb_expand(tdb, 4 << INITIAL_ZONE_BITS) == 0);
46                 ok1(tdb->map_size ==
47                     (2<<INITIAL_ZONE_BITS) + sizeof(struct tdb_header));
48                 ok1(tdb_check(tdb, NULL, NULL) == 0);
49
50                 /* Large expand now will double file. */
51                 ok1(tdb_expand(tdb, 4 << INITIAL_ZONE_BITS) == 0);
52                 ok1(tdb->map_size ==
53                     (4<<INITIAL_ZONE_BITS) + sizeof(struct tdb_header));
54                 ok1(tdb_check(tdb, NULL, NULL) == 0);
55
56                 /* And again? */
57                 ok1(tdb_expand(tdb, 4 << INITIAL_ZONE_BITS) == 0);
58                 ok1(tdb->map_size ==
59                     (8<<INITIAL_ZONE_BITS) + sizeof(struct tdb_header));
60                 ok1(tdb_check(tdb, NULL, NULL) == 0);
61
62                 /* Below comfort level, won't fill zone. */
63                 ok1(tdb_expand(tdb,
64                                ((3 << INITIAL_ZONE_BITS)
65                                 >> TDB_COMFORT_FACTOR_BITS)
66                                - sizeof(struct tdb_used_record)) == 0);
67                 ok1(tdb->map_size < (12<<INITIAL_ZONE_BITS)
68                     + sizeof(struct tdb_header));
69                 tdb_close(tdb);
70         }
71
72         ok1(tap_log_messages == 0);
73         return exit_status();
74 }