]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-delete.c
tdb2: use immobile free buckets, rename tests to show some ordering.
[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         /* Now, this will be ideally placed. */
66         v = val + 2;
67         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
68         ok1(tdb_check(tdb, NULL, NULL) == 0);
69
70         /* This will collide with both. */
71         v = val;
72         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
73
74         /* We can still find them all, right? */
75         ok1(tdb_fetch(tdb, key).dsize == data.dsize);
76         v = val + 1;
77         ok1(tdb_fetch(tdb, key).dsize == data.dsize);
78         v = val + 2;
79         ok1(tdb_fetch(tdb, key).dsize == data.dsize);
80
81         /* And if we delete val + 1, that val + 2 should not move! */
82         v = val + 1;
83         ok1(tdb_delete(tdb, key) == 0);
84         ok1(tdb_check(tdb, NULL, NULL) == 0);
85
86         v = val;
87         ok1(tdb_fetch(tdb, key).dsize == data.dsize);
88         v = val + 2;
89         ok1(tdb_fetch(tdb, key).dsize == data.dsize);
90
91         /* Delete those two, so we are empty. */
92         ok1(tdb_delete(tdb, key) == 0);
93         v = val;
94         ok1(tdb_delete(tdb, key) == 0);
95
96         ok1(tdb_check(tdb, NULL, NULL) == 0);
97 }
98
99 int main(int argc, char *argv[])
100 {
101         unsigned int i;
102         struct tdb_context *tdb;
103         union tdb_attribute hattr = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
104                                                 .hash_fn = clash } };
105         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
106                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
107                         TDB_NOMMAP|TDB_CONVERT };
108
109         hattr.base.next = &tap_log_attr;
110
111         plan_tests(sizeof(flags) / sizeof(flags[0]) * 66 + 1);
112         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
113                 tdb = tdb_open("run-delete.tdb", flags[i],
114                                O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
115                 ok1(tdb);
116                 if (!tdb)
117                         continue;
118
119                 /* Check start of hash table. */
120                 test_val(tdb, 0);
121
122                 /* Check end of hash table (will wrap around!). */
123                 test_val(tdb, ((1 << tdb->header.v.hash_bits) - 1) * 2);
124
125                 ok1(!tdb_has_locks(tdb));
126                 tdb_close(tdb);
127         }
128
129         ok1(tap_log_messages == 0);
130         return exit_status();
131 }