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 an entry, then delete it. */
23 /* Delete should fail. */
24 ok1(tdb_delete(tdb, key) == -1);
25 ok1(tdb_error(tdb) == TDB_ERR_NOEXIST);
26 ok1(tdb_check(tdb, NULL, NULL) == 0);
28 /* Insert should succeed. */
29 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
30 ok1(tdb_check(tdb, NULL, NULL) == 0);
32 /* Delete should succeed. */
33 ok1(tdb_delete(tdb, key) == 0);
34 ok1(tdb_check(tdb, NULL, NULL) == 0);
36 /* Re-add it, then add collision. */
37 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
39 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
40 ok1(tdb_check(tdb, NULL, NULL) == 0);
43 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
45 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
47 /* Delete second one. */
49 ok1(tdb_delete(tdb, key) == 0);
50 ok1(tdb_check(tdb, NULL, NULL) == 0);
53 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
54 ok1(tdb_check(tdb, NULL, NULL) == 0);
56 /* Now, try deleting first one. */
58 ok1(tdb_delete(tdb, key) == 0);
59 ok1(tdb_check(tdb, NULL, NULL) == 0);
61 /* Can still find second? */
63 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
65 /* Delete that, so we are empty. */
66 ok1(tdb_delete(tdb, key) == 0);
67 ok1(tdb_check(tdb, NULL, NULL) == 0);
70 int main(int argc, char *argv[])
73 struct tdb_context *tdb;
74 union tdb_attribute hattr = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
76 int flags[] = { TDB_INTERNAL, TDB_DEFAULT,
77 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT };
79 hattr.base.next = &tap_log_attr;
81 plan_tests(sizeof(flags) / sizeof(flags[0]) * 44 + 1);
82 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
83 tdb = tdb_open("run-delete.tdb", flags[i],
84 O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
89 /* Check start of hash table. */
92 /* Check end of hash table (will wrap around!). */
93 test_val(tdb, ((1 << tdb->header.v.hash_bits) - 1) * 2);
95 ok1(!tdb_has_locks(tdb));
99 ok1(tap_log_messages == 0);
100 return exit_status();