]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-enlarge_hash.c
tdb2: fixes and test for hash enlargement.
[ccan] / ccan / tdb2 / test / run-enlarge_hash.c
1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/free.c>
3 #include <ccan/tdb2/lock.c>
4 #include <ccan/tdb2/io.c>
5 #include <ccan/tdb2/check.c>
6 #include <ccan/tap/tap.h>
7 #include "logging.h"
8
9 /* We rig the hash so adjacent-numbered records always clash. */
10 static uint64_t clash(const void *key, size_t len, uint64_t seed, void *priv)
11 {
12         return *(unsigned int *)key / 2;
13 }
14
15 int main(int argc, char *argv[])
16 {
17         unsigned int i;
18         struct tdb_context *tdb;
19         unsigned int v;
20         struct tdb_data key = { (unsigned char *)&v, sizeof(v) };
21         struct tdb_data data = { (unsigned char *)&v, sizeof(v) };
22         union tdb_attribute hattr = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
23                                                 .hash_fn = clash } };
24         int flags[] = { TDB_INTERNAL, TDB_DEFAULT,
25                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT };
26
27         hattr.base.next = &tap_log_attr;
28
29         plan_tests(sizeof(flags) / sizeof(flags[0]) * 11 + 1);
30         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
31                 tdb = tdb_open("run-enlarge-hash.tdb", flags[i],
32                                O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
33                 ok1(tdb);
34                 if (!tdb)
35                         continue;
36
37                 /* Put a single entry in. */
38                 v = 0;
39                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
40                 enlarge_hash(tdb);
41                 ok1(tdb_check(tdb, NULL, NULL) == 0);
42
43                 /* Put a non-clashing entry in. */
44                 v = 2;
45                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
46                 enlarge_hash(tdb);
47                 ok1(tdb_check(tdb, NULL, NULL) == 0);
48
49                 /* Now, make a clash. */
50                 v = 1;
51                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
52                 enlarge_hash(tdb);
53                 ok1(tdb_check(tdb, NULL, NULL) == 0);
54
55                 /* Clash at end. */
56                 v = ((1 << tdb->header.v.hash_bits) - 1) * 2;
57                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
58                 v++;
59                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
60                 ok1(tdb_check(tdb, NULL, NULL) == 0);
61                 enlarge_hash(tdb);
62                 ok1(tdb_check(tdb, NULL, NULL) == 0);
63
64                 tdb_close(tdb);
65         }
66         ok1(tap_log_messages == 0);
67         return exit_status();
68 }