]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-delete.c
tdb2: more tests, hash collision fixes, attribute support.
[ccan] / ccan / tdb2 / test / run-delete.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 static void test_val(struct tdb_context *tdb, unsigned int val)
16 {
17         unsigned int v;
18         struct tdb_data key = { (unsigned char *)&v, sizeof(v) };
19         struct tdb_data data = { (unsigned char *)&v, sizeof(v) };
20
21         /* Insert an entry, then delete it. */
22         v = val;
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);
27
28         /* Insert should succeed. */
29         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
30         ok1(tdb_check(tdb, NULL, NULL) == 0);
31
32         /* Delete should succeed. */
33         ok1(tdb_delete(tdb, key) == 0);
34         ok1(tdb_check(tdb, NULL, NULL) == 0);
35
36         /* Re-add it, then add collision. */
37         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
38         v = val + 1;
39         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
40         ok1(tdb_check(tdb, NULL, NULL) == 0);
41
42         /* Can find both? */
43         ok1(tdb_fetch(tdb, key).dsize == data.dsize);
44         v = val;
45         ok1(tdb_fetch(tdb, key).dsize == data.dsize);
46
47         /* Delete second one. */
48         v = val + 1;
49         ok1(tdb_delete(tdb, key) == 0);
50         ok1(tdb_check(tdb, NULL, NULL) == 0);
51
52         /* Re-add */
53         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
54         ok1(tdb_check(tdb, NULL, NULL) == 0);
55
56         /* Now, try deleting first one. */
57         v = val;
58         ok1(tdb_delete(tdb, key) == 0);
59         ok1(tdb_check(tdb, NULL, NULL) == 0);
60
61         /* Can still find second? */
62         v = val + 1;
63         ok1(tdb_fetch(tdb, key).dsize == data.dsize);
64
65         /* Delete that, so we are empty. */
66         ok1(tdb_delete(tdb, key) == 0);
67         ok1(tdb_check(tdb, NULL, NULL) == 0);
68 }
69
70 int main(int argc, char *argv[])
71 {
72         unsigned int i;
73         struct tdb_context *tdb;
74         union tdb_attribute hattr = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
75                                                 .hash_fn = clash } };
76         int flags[] = { TDB_INTERNAL, TDB_DEFAULT,
77                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT };
78
79         hattr.base.next = &tap_log_attr;
80
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);
85                 ok1(tdb);
86                 if (!tdb)
87                         continue;
88
89                 /* Check start of hash table. */
90                 test_val(tdb, 0);
91
92                 /* Check end of hash table (will wrap around!). */
93                 test_val(tdb, ((1 << tdb->header.v.hash_bits) - 1) * 2);
94
95                 ok1(!tdb_has_locks(tdb));
96                 tdb_close(tdb);
97         }
98
99         ok1(tap_log_messages == 0);
100         return exit_status();
101 }