lib/ntdb optimize includes in ntdb tests
[Samba.git] / lib / ntdb / test / api-13-delete.c
blob730ade5afbb8d5366482ae484e4590a0f13ab7fc
1 #include "private.h" // For NTDB_TOPLEVEL_HASH_BITS
2 #include <ccan/hash/hash.h>
3 #include "ntdb.h"
4 #include "tap-interface.h"
5 #include "logging.h"
7 /* We rig the hash so adjacent-numbered records always clash. */
8 static uint32_t clash(const void *key, size_t len, uint32_t seed, void *priv)
10 return *((const unsigned int *)key) / 2;
13 /* We use the same seed which we saw a failure on. */
14 static uint32_t fixedhash(const void *key, size_t len, uint32_t seed, void *p)
16 return hash64_stable((const unsigned char *)key, len,
17 *(uint64_t *)p);
20 static bool store_records(struct ntdb_context *ntdb)
22 int i;
23 NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
24 NTDB_DATA d, data = { (unsigned char *)&i, sizeof(i) };
26 for (i = 0; i < 1000; i++) {
27 if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
28 return false;
29 ntdb_fetch(ntdb, key, &d);
30 if (!ntdb_deq(d, data))
31 return false;
32 free(d.dptr);
34 return true;
37 static void test_val(struct ntdb_context *ntdb, uint64_t val)
39 uint64_t v;
40 NTDB_DATA key = { (unsigned char *)&v, sizeof(v) };
41 NTDB_DATA d, data = { (unsigned char *)&v, sizeof(v) };
43 /* Insert an entry, then delete it. */
44 v = val;
45 /* Delete should fail. */
46 ok1(ntdb_delete(ntdb, key) == NTDB_ERR_NOEXIST);
47 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
49 /* Insert should succeed. */
50 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
51 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
53 /* Delete should succeed. */
54 ok1(ntdb_delete(ntdb, key) == 0);
55 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
57 /* Re-add it, then add collision. */
58 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
59 v = val + 1;
60 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
61 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
63 /* Can find both? */
64 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
65 ok1(d.dsize == data.dsize);
66 free(d.dptr);
67 v = val;
68 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
69 ok1(d.dsize == data.dsize);
70 free(d.dptr);
72 /* Delete second one. */
73 v = val + 1;
74 ok1(ntdb_delete(ntdb, key) == 0);
75 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
77 /* Re-add */
78 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
79 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
81 /* Now, try deleting first one. */
82 v = val;
83 ok1(ntdb_delete(ntdb, key) == 0);
84 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
86 /* Can still find second? */
87 v = val + 1;
88 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
89 ok1(d.dsize == data.dsize);
90 free(d.dptr);
92 /* Now, this will be ideally placed. */
93 v = val + 2;
94 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
95 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
97 /* This will collide with both. */
98 v = val;
99 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
101 /* We can still find them all, right? */
102 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
103 ok1(d.dsize == data.dsize);
104 free(d.dptr);
105 v = val + 1;
106 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
107 ok1(d.dsize == data.dsize);
108 free(d.dptr);
109 v = val + 2;
110 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
111 ok1(d.dsize == data.dsize);
112 free(d.dptr);
114 /* And if we delete val + 1, that val + 2 should not move! */
115 v = val + 1;
116 ok1(ntdb_delete(ntdb, key) == 0);
117 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
119 v = val;
120 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
121 ok1(d.dsize == data.dsize);
122 free(d.dptr);
123 v = val + 2;
124 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
125 ok1(d.dsize == data.dsize);
126 free(d.dptr);
128 /* Delete those two, so we are empty. */
129 ok1(ntdb_delete(ntdb, key) == 0);
130 v = val;
131 ok1(ntdb_delete(ntdb, key) == 0);
133 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
136 int main(int argc, char *argv[])
138 unsigned int i, j;
139 struct ntdb_context *ntdb;
140 uint64_t seed = 16014841315512641303ULL;
141 union ntdb_attribute clash_hattr
142 = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
143 .fn = clash } };
144 union ntdb_attribute fixed_hattr
145 = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
146 .fn = fixedhash,
147 .data = &seed } };
148 int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
149 NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
150 NTDB_NOMMAP|NTDB_CONVERT };
151 /* These two values gave trouble before. */
152 int vals[] = { 755, 837 };
154 clash_hattr.base.next = &tap_log_attr;
155 fixed_hattr.base.next = &tap_log_attr;
157 plan_tests(sizeof(flags) / sizeof(flags[0])
158 * (39 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
159 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
160 ntdb = ntdb_open("run-13-delete.ntdb", flags[i]|MAYBE_NOSYNC,
161 O_RDWR|O_CREAT|O_TRUNC, 0600, &clash_hattr);
162 ok1(ntdb);
163 if (!ntdb)
164 continue;
166 /* Check start of hash table. */
167 test_val(ntdb, 0);
169 /* Check end of hash table. */
170 test_val(ntdb, -1ULL);
172 /* Check mixed bitpattern. */
173 test_val(ntdb, 0x123456789ABCDEF0ULL);
175 ok1(!ntdb->file || (ntdb->file->allrecord_lock.count == 0
176 && ntdb->file->num_lockrecs == 0));
177 ntdb_close(ntdb);
179 /* Deleting these entries in the db gave problems. */
180 ntdb = ntdb_open("run-13-delete.ntdb", flags[i]|MAYBE_NOSYNC,
181 O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
182 ok1(ntdb);
183 if (!ntdb)
184 continue;
186 ok1(store_records(ntdb));
187 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
188 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
189 NTDB_DATA key;
191 key.dptr = (unsigned char *)&vals[j];
192 key.dsize = sizeof(vals[j]);
193 ok1(ntdb_delete(ntdb, key) == 0);
194 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
196 ntdb_close(ntdb);
199 ok1(tap_log_messages == 0);
200 return exit_status();