]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/api-93-repack.c
htable: fix bug where first entry has hash of 0 or 1.
[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 1000
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                         TDB_VERSION1, TDB_VERSION1|TDB_NOMMAP,
57                         TDB_VERSION1|TDB_CONVERT,
58                         TDB_VERSION1|TDB_NOMMAP|TDB_CONVERT
59         };
60
61         plan_tests(sizeof(flags) / sizeof(flags[0]) * 6 + 1);
62
63         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
64                 tdb = tdb_open("run-93-repack.tdb", flags[i],
65                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
66                 ok1(tdb);
67                 if (!tdb)
68                         break;
69
70                 ok1(store_all(tdb));
71
72                 ok1(tdb_repack(tdb) == TDB_SUCCESS);
73                 memset(found, 0, sizeof(found));
74                 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
75                 ok1(tdb_traverse(tdb, mark_entry, found) == NUM_TESTS);
76                 ok1(is_all_set(found, NUM_TESTS));
77                 tdb_close(tdb);
78         }
79
80         ok1(tap_log_messages == 0);
81         return exit_status();
82 }