tdb2: test: fix run-57-die-during-transaction.c to be more efficient.
[Samba/gbeck.git] / lib / tdb2 / test / run-13-delete.c
blobfdbc9db70aefaba4f91982e192b425b065c445ff
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
5 /* We rig the hash so adjacent-numbered records always clash. */
6 static uint64_t clash(const void *key, size_t len, uint64_t seed, void *priv)
8 return ((uint64_t)*(const unsigned int *)key)
9 << (64 - TDB_TOPLEVEL_HASH_BITS - 1);
12 /* We use the same seed which we saw a failure on. */
13 static uint64_t fixedhash(const void *key, size_t len, uint64_t seed, void *p)
15 return hash64_stable((const unsigned char *)key, len,
16 *(uint64_t *)p);
19 static bool store_records(struct tdb_context *tdb)
21 int i;
22 struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
23 struct tdb_data d, data = { (unsigned char *)&i, sizeof(i) };
25 for (i = 0; i < 1000; i++) {
26 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
27 return false;
28 tdb_fetch(tdb, key, &d);
29 if (!tdb_deq(d, data))
30 return false;
31 free(d.dptr);
33 return true;
36 static void test_val(struct tdb_context *tdb, uint64_t val)
38 uint64_t v;
39 struct tdb_data key = { (unsigned char *)&v, sizeof(v) };
40 struct tdb_data d, data = { (unsigned char *)&v, sizeof(v) };
42 /* Insert an entry, then delete it. */
43 v = val;
44 /* Delete should fail. */
45 ok1(tdb_delete(tdb, key) == TDB_ERR_NOEXIST);
46 ok1(tdb_check(tdb, NULL, NULL) == 0);
48 /* Insert should succeed. */
49 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
50 ok1(tdb_check(tdb, NULL, NULL) == 0);
52 /* Delete should succeed. */
53 ok1(tdb_delete(tdb, key) == 0);
54 ok1(tdb_check(tdb, NULL, NULL) == 0);
56 /* Re-add it, then add collision. */
57 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
58 v = val + 1;
59 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
60 ok1(tdb_check(tdb, NULL, NULL) == 0);
62 /* Can find both? */
63 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
64 ok1(d.dsize == data.dsize);
65 free(d.dptr);
66 v = val;
67 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
68 ok1(d.dsize == data.dsize);
69 free(d.dptr);
71 /* Delete second one. */
72 v = val + 1;
73 ok1(tdb_delete(tdb, key) == 0);
74 ok1(tdb_check(tdb, NULL, NULL) == 0);
76 /* Re-add */
77 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
78 ok1(tdb_check(tdb, NULL, NULL) == 0);
80 /* Now, try deleting first one. */
81 v = val;
82 ok1(tdb_delete(tdb, key) == 0);
83 ok1(tdb_check(tdb, NULL, NULL) == 0);
85 /* Can still find second? */
86 v = val + 1;
87 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
88 ok1(d.dsize == data.dsize);
89 free(d.dptr);
91 /* Now, this will be ideally placed. */
92 v = val + 2;
93 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
94 ok1(tdb_check(tdb, NULL, NULL) == 0);
96 /* This will collide with both. */
97 v = val;
98 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
100 /* We can still find them all, right? */
101 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
102 ok1(d.dsize == data.dsize);
103 free(d.dptr);
104 v = val + 1;
105 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
106 ok1(d.dsize == data.dsize);
107 free(d.dptr);
108 v = val + 2;
109 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
110 ok1(d.dsize == data.dsize);
111 free(d.dptr);
113 /* And if we delete val + 1, that val + 2 should not move! */
114 v = val + 1;
115 ok1(tdb_delete(tdb, key) == 0);
116 ok1(tdb_check(tdb, NULL, NULL) == 0);
118 v = val;
119 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
120 ok1(d.dsize == data.dsize);
121 free(d.dptr);
122 v = val + 2;
123 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
124 ok1(d.dsize == data.dsize);
125 free(d.dptr);
127 /* Delete those two, so we are empty. */
128 ok1(tdb_delete(tdb, key) == 0);
129 v = val;
130 ok1(tdb_delete(tdb, key) == 0);
132 ok1(tdb_check(tdb, NULL, NULL) == 0);
135 int main(int argc, char *argv[])
137 unsigned int i, j;
138 struct tdb_context *tdb;
139 uint64_t seed = 16014841315512641303ULL;
140 union tdb_attribute clash_hattr
141 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
142 .fn = clash } };
143 union tdb_attribute fixed_hattr
144 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
145 .fn = fixedhash,
146 .data = &seed } };
147 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
148 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
149 TDB_NOMMAP|TDB_CONVERT,
150 TDB_INTERNAL|TDB_VERSION1, TDB_VERSION1,
151 TDB_NOMMAP|TDB_VERSION1,
152 TDB_INTERNAL|TDB_CONVERT|TDB_VERSION1,
153 TDB_CONVERT|TDB_VERSION1,
154 TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
155 /* These two values gave trouble before. */
156 int vals[] = { 755, 837 };
158 clash_hattr.base.next = &tap_log_attr;
159 fixed_hattr.base.next = &tap_log_attr;
161 plan_tests(sizeof(flags) / sizeof(flags[0])
162 * (39 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
163 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
164 tdb = tdb_open("run-13-delete.tdb", flags[i],
165 O_RDWR|O_CREAT|O_TRUNC, 0600, &clash_hattr);
166 ok1(tdb);
167 if (!tdb)
168 continue;
170 /* Check start of hash table. */
171 test_val(tdb, 0);
173 /* Check end of hash table. */
174 test_val(tdb, -1ULL);
176 /* Check mixed bitpattern. */
177 test_val(tdb, 0x123456789ABCDEF0ULL);
179 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
180 && tdb->file->num_lockrecs == 0));
181 tdb_close(tdb);
183 /* Deleting these entries in the db gave problems. */
184 tdb = tdb_open("run-13-delete.tdb", flags[i],
185 O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
186 ok1(tdb);
187 if (!tdb)
188 continue;
190 ok1(store_records(tdb));
191 ok1(tdb_check(tdb, NULL, NULL) == 0);
192 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
193 struct tdb_data key;
195 key.dptr = (unsigned char *)&vals[j];
196 key.dsize = sizeof(vals[j]);
197 ok1(tdb_delete(tdb, key) == 0);
198 ok1(tdb_check(tdb, NULL, NULL) == 0);
200 tdb_close(tdb);
203 ok1(tap_log_messages == 0);
204 return exit_status();