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