]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/run-30-exhaust-before-expand.c
ntdb: fix up tests.
[ccan] / ccan / ntdb / test / run-30-exhaust-before-expand.c
1 #include "ntdb-source.h"
2 #include "tap-interface.h"
3 #include "logging.h"
4 #include "helprun-external-agent.h"
5
6 static bool empty_freetable(struct ntdb_context *ntdb)
7 {
8         struct ntdb_freetable ftab;
9         unsigned int i;
10
11         /* Now, free table should be completely exhausted in zone 0 */
12         if (ntdb_read_convert(ntdb, ntdb->ftable_off, &ftab, sizeof(ftab)) != 0)
13                 abort();
14
15         for (i = 0; i < sizeof(ftab.buckets)/sizeof(ftab.buckets[0]); i++) {
16                 if (ftab.buckets[i])
17                         return false;
18         }
19         return true;
20 }
21
22
23 int main(int argc, char *argv[])
24 {
25         unsigned int i, j;
26         struct ntdb_context *ntdb;
27         int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
28                         NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
29                         NTDB_NOMMAP|NTDB_CONVERT };
30
31         plan_tests(sizeof(flags) / sizeof(flags[0]) * 7 + 1);
32
33         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
34                 NTDB_DATA k, d;
35                 uint64_t size;
36                 bool was_empty = false;
37
38                 k.dptr = (void *)&j;
39                 k.dsize = sizeof(j);
40
41                 ntdb = ntdb_open("run-30-exhaust-before-expand.ntdb",
42                                  flags[i]|MAYBE_NOSYNC,
43                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
44                 ok1(ntdb);
45                 if (!ntdb)
46                         continue;
47
48                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
49                 /* There's one empty record in initial db. */
50                 ok1(!empty_freetable(ntdb));
51
52                 size = ntdb->file->map_size;
53
54                 /* Create one record to chew up most space. */
55                 d.dsize = size - NEW_DATABASE_HDR_SIZE(ntdb->hash_bits) - 32;
56                 d.dptr = calloc(d.dsize, 1);
57                 j = 0;
58                 ok1(ntdb_store(ntdb, k, d, NTDB_INSERT) == 0);
59                 ok1(ntdb->file->map_size == size);
60                 free(d.dptr);
61
62                 /* Now insert minimal-length records until we expand. */
63                 for (j = 1; ntdb->file->map_size == size; j++) {
64                         was_empty = empty_freetable(ntdb);
65                         if (ntdb_store(ntdb, k, k, NTDB_INSERT) != 0)
66                                 err(1, "Failed to store record %i", j);
67                 }
68
69                 /* Would have been empty before expansion, but no longer. */
70                 ok1(was_empty);
71                 ok1(!empty_freetable(ntdb));
72                 ntdb_close(ntdb);
73         }
74
75         ok1(tap_log_messages == 0);
76         return exit_status();
77 }