]> git.ozlabs.org Git - ccan/blob - junkcode/rusty@rustcorp.com.au-ntdb/test/api-93-repack.c
ccan/ntdb: demote to junkcode.
[ccan] / junkcode / rusty@rustcorp.com.au-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 #include "helpapi-external-agent.h"
7
8 #define NUM_TESTS 1000
9
10 static bool store_all(struct ntdb_context *ntdb)
11 {
12         unsigned int i;
13         NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
14         NTDB_DATA dbuf = { (unsigned char *)&i, sizeof(i) };
15
16         for (i = 0; i < NUM_TESTS; i++) {
17                 if (ntdb_store(ntdb, key, dbuf, NTDB_INSERT) != NTDB_SUCCESS)
18                         return false;
19         }
20         return true;
21 }
22
23 static int mark_entry(struct ntdb_context *ntdb,
24                       NTDB_DATA key, NTDB_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 ntdb_context *ntdb;
54         int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
55                         NTDB_CONVERT, NTDB_NOMMAP|NTDB_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                 ntdb = ntdb_open("run-93-repack.ntdb",
62                                  flags[i]|MAYBE_NOSYNC,
63                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
64                 ok1(ntdb);
65                 if (!ntdb)
66                         break;
67
68                 ok1(store_all(ntdb));
69
70                 ok1(ntdb_repack(ntdb) == NTDB_SUCCESS);
71                 memset(found, 0, sizeof(found));
72                 ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
73                 ok1(ntdb_traverse(ntdb, mark_entry, found) == NUM_TESTS);
74                 ok1(is_all_set(found, NUM_TESTS));
75                 ntdb_close(ntdb);
76         }
77
78         ok1(tap_log_messages == 0);
79         return exit_status();
80 }