tdb2: tdb_repack
[ccan] / ccan / tdb2 / test / run-93-repack.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
4
5 #define NUM_TESTS 50000
6
7 static bool store_all(struct tdb_context *tdb)
8 {
9         unsigned int i;
10         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
11         struct tdb_data dbuf = { (unsigned char *)&i, sizeof(i) };
12
13         for (i = 0; i < NUM_TESTS; i++) {
14                 if (tdb_store(tdb, key, dbuf, TDB_INSERT) != TDB_SUCCESS)
15                         return false;
16         }
17         return true;
18 }
19
20 static int mark_entry(struct tdb_context *tdb,
21                       TDB_DATA key, TDB_DATA data, bool found[])
22 {
23         unsigned int num;
24
25         if (key.dsize != sizeof(num))
26                 return -1;
27         memcpy(&num, key.dptr, key.dsize);
28         if (num >= NUM_TESTS)
29                 return -1;
30         if (found[num])
31                 return -1;
32         found[num] = true;
33         return 0;
34 }
35
36 static bool is_all_set(bool found[], unsigned int num)
37 {
38         unsigned int i;
39
40         for (i = 0; i < num; i++)
41                 if (!found[i])
42                         return false;
43         return true;
44 }
45
46 int main(int argc, char *argv[])
47 {
48         unsigned int i;
49         bool found[NUM_TESTS];
50         struct tdb_context *tdb;
51         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
52                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT,
53         };
54
55         plan_tests(sizeof(flags) / sizeof(flags[0]) * 6 + 1);
56
57         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
58                 tdb = tdb_open("run-93-repack.tdb", flags[i],
59                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
60                 ok1(tdb);
61                 if (!tdb)
62                         break;
63
64                 ok1(store_all(tdb));
65
66                 ok1(tdb_repack(tdb) == TDB_SUCCESS);
67                 memset(found, 0, sizeof(found));
68                 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
69                 ok1(tdb_traverse(tdb, mark_entry, found) == NUM_TESTS);
70                 ok1(is_all_set(found, NUM_TESTS));
71                 tdb_close(tdb);
72         }
73
74         ok1(tap_log_messages == 0);
75         return exit_status();
76 }