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>
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)
12 return *(unsigned int *)key / 2;
15 static void test_val(struct tdb_context *tdb, unsigned int val)
18 struct tdb_data key = { (unsigned char *)&v, sizeof(v) };
19 struct tdb_data data = { (unsigned char *)&v, sizeof(v) };
21 /* Insert two entries, with the same hash. */
23 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
25 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
26 ok1(tdb_check(tdb, NULL, NULL) == 0);
30 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
32 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
35 int main(int argc, char *argv[])
38 struct tdb_context *tdb;
39 union tdb_attribute hattr = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
41 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
42 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
43 TDB_NOMMAP|TDB_CONVERT,
46 hattr.base.next = &tap_log_attr;
48 plan_tests(sizeof(flags) / sizeof(flags[0]) * 14 + 1);
49 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
50 tdb = tdb_open("run-hashclash.tdb", flags[i],
51 O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
56 /* Check start of hash table. */
59 ok1(!tdb_has_locks(tdb));
62 tdb = tdb_open("run-hashclash.tdb", flags[i],
63 O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
68 /* Check end of hash table (will wrap around!). */
69 test_val(tdb, ((1 << tdb->header.v.hash_bits) - 1) * 2);
71 ok1(!tdb_has_locks(tdb));
75 ok1(tap_log_messages == 0);