]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/api-93-repack.c
0fade08224fd0ced678282d7f5f077cf81d93b1b
[ccan] / ccan / ntdb / test / api-93-repack.c
1 #include "config.h"
2 #include "ntdb.h"
3 #include "private.h"
4 #include "tap-interface.h"
5 #include "logging.h"
6
7 #define NUM_TESTS 1000
8
9 static bool store_all(struct ntdb_context *ntdb)
10 {
11         unsigned int i;
12         NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
13         NTDB_DATA dbuf = { (unsigned char *)&i, sizeof(i) };
14
15         for (i = 0; i < NUM_TESTS; i++) {
16                 if (ntdb_store(ntdb, key, dbuf, NTDB_INSERT) != NTDB_SUCCESS)
17                         return false;
18         }
19         return true;
20 }
21
22 static int mark_entry(struct ntdb_context *ntdb,
23                       NTDB_DATA key, NTDB_DATA data, bool found[])
24 {
25         unsigned int num;
26
27         if (key.dsize != sizeof(num))
28                 return -1;
29         memcpy(&num, key.dptr, key.dsize);
30         if (num >= NUM_TESTS)
31                 return -1;
32         if (found[num])
33                 return -1;
34         found[num] = true;
35         return 0;
36 }
37
38 static bool is_all_set(bool found[], unsigned int num)
39 {
40         unsigned int i;
41
42         for (i = 0; i < num; i++)
43                 if (!found[i])
44                         return false;
45         return true;
46 }
47
48 int main(int argc, char *argv[])
49 {
50         unsigned int i;
51         bool found[NUM_TESTS];
52         struct ntdb_context *ntdb;
53         int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
54                         NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT
55         };
56
57         plan_tests(sizeof(flags) / sizeof(flags[0]) * 6 + 1);
58
59         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
60                 ntdb = ntdb_open("run-93-repack.ntdb",
61                                  flags[i]|MAYBE_NOSYNC,
62                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
63                 ok1(ntdb);
64                 if (!ntdb)
65                         break;
66
67                 ok1(store_all(ntdb));
68
69                 ok1(ntdb_repack(ntdb) == NTDB_SUCCESS);
70                 memset(found, 0, sizeof(found));
71                 ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
72                 ok1(ntdb_traverse(ntdb, mark_entry, found) == NUM_TESTS);
73                 ok1(is_all_set(found, NUM_TESTS));
74                 ntdb_close(ntdb);
75         }
76
77         ok1(tap_log_messages == 0);
78         return exit_status();
79 }