1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
5 static uint64_t badhash(const void *key, size_t len, uint64_t seed, void *priv)
10 static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *p)
13 return tdb_delete(tdb, key);
17 int main(int argc, char *argv[])
20 struct tdb_context *tdb;
21 struct tdb_data key = { (unsigned char *)&j, sizeof(j) };
22 struct tdb_data dbuf = { (unsigned char *)&j, sizeof(j) };
23 union tdb_attribute hattr = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
25 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
26 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
27 TDB_NOMMAP|TDB_CONVERT,
30 hattr.base.next = &tap_log_attr;
33 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
34 struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */
36 tdb = tdb_open("run-25-hashoverload.tdb", flags[i],
37 O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
43 for (j = 0; j < (1 << TDB_HASH_GROUP_BITS); j++) {
44 ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
46 ok1(tdb_check(tdb, NULL, NULL) == 0);
48 /* Now store one last value: should form chain. */
49 ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
50 ok1(tdb_check(tdb, NULL, NULL) == 0);
52 /* Check we can find them all. */
53 for (j = 0; j < (1 << TDB_HASH_GROUP_BITS) + 1; j++) {
54 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
55 ok1(d.dsize == sizeof(j));
57 ok1(d.dptr && memcmp(d.dptr, &j, d.dsize) == 0);
61 /* Now add a *lot* more. */
62 for (j = (1 << TDB_HASH_GROUP_BITS) + 1;
63 j < (16 << TDB_HASH_GROUP_BITS);
65 ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
66 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
67 ok1(d.dsize == sizeof(j));
69 ok1(d.dptr && memcmp(d.dptr, &j, d.dsize) == 0);
72 ok1(tdb_check(tdb, NULL, NULL) == 0);
74 /* Traverse through them. */
75 ok1(tdb_traverse(tdb, trav, NULL) == j);
77 /* Empty the first chain-worth. */
78 for (j = 0; j < (1 << TDB_HASH_GROUP_BITS); j++)
79 ok1(tdb_delete(tdb, key) == 0);
81 ok1(tdb_check(tdb, NULL, NULL) == 0);
83 for (j = (1 << TDB_HASH_GROUP_BITS);
84 j < (16 << TDB_HASH_GROUP_BITS);
86 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
87 ok1(d.dsize == sizeof(j));
89 ok1(d.dptr && memcmp(d.dptr, &j, d.dsize) == 0);
93 /* Traverse through them. */
94 ok1(tdb_traverse(tdb, trav, NULL)
95 == (15 << TDB_HASH_GROUP_BITS));
98 for (j = 0; j < (1 << TDB_HASH_GROUP_BITS); j++) {
99 ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
101 ok1(tdb_check(tdb, NULL, NULL) == 0);
103 /* Now try deleting as we go. */
104 ok1(tdb_traverse(tdb, trav, trav)
105 == (16 << TDB_HASH_GROUP_BITS));
106 ok1(tdb_check(tdb, NULL, NULL) == 0);
107 ok1(tdb_traverse(tdb, trav, NULL) == 0);
111 ok1(tap_log_messages == 0);
112 return exit_status();