tdb2: change API to return the error value.
[ccan] / ccan / tdb2 / test / run-25-hashoverload.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/hash.c>
6 #include <ccan/tdb2/transaction.c>
7 #include <ccan/tdb2/traverse.c>
8 #include <ccan/tdb2/check.c>
9 #include <ccan/tap/tap.h>
10 #include "logging.h"
11
12 static uint64_t badhash(const void *key, size_t len, uint64_t seed, void *priv)
13 {
14         return 0;
15 }
16
17 static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *p)
18 {
19         if (p)
20                 return tdb_delete(tdb, key);
21         return 0;
22 }
23
24 int main(int argc, char *argv[])
25 {
26         unsigned int i, j;
27         struct tdb_context *tdb;
28         struct tdb_data key = { (unsigned char *)&j, sizeof(j) };
29         struct tdb_data dbuf = { (unsigned char *)&j, sizeof(j) };
30         union tdb_attribute hattr = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
31                                                 .hash_fn = badhash } };
32         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
33                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
34                         TDB_NOMMAP|TDB_CONVERT,
35         };
36
37         hattr.base.next = &tap_log_attr;
38
39         plan_tests(6883);
40         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
41                 struct tdb_data d;
42
43                 tdb = tdb_open("run-25-hashoverload.tdb", flags[i],
44                                O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
45                 ok1(tdb);
46                 if (!tdb)
47                         continue;
48
49                 /* Fill a group. */
50                 for (j = 0; j < (1 << TDB_HASH_GROUP_BITS); j++) {
51                         ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
52                 }
53                 ok1(tdb_check(tdb, NULL, NULL) == 0);
54
55                 /* Now store one last value: should form chain. */
56                 ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
57                 ok1(tdb_check(tdb, NULL, NULL) == 0);
58
59                 /* Check we can find them all. */
60                 for (j = 0; j < (1 << TDB_HASH_GROUP_BITS) + 1; j++) {
61                         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
62                         ok1(d.dsize == sizeof(j));
63                         ok1(d.dptr != NULL);
64                         ok1(d.dptr && memcmp(d.dptr, &j, d.dsize) == 0);
65                         free(d.dptr);
66                 }
67
68                 /* Now add a *lot* more. */
69                 for (j = (1 << TDB_HASH_GROUP_BITS) + 1;
70                      j < (16 << TDB_HASH_GROUP_BITS);
71                      j++) {
72                         ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
73                         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
74                         ok1(d.dsize == sizeof(j));
75                         ok1(d.dptr != NULL);
76                         ok1(d.dptr && memcmp(d.dptr, &j, d.dsize) == 0);
77                         free(d.dptr);
78                 }
79                 ok1(tdb_check(tdb, NULL, NULL) == 0);
80
81                 /* Traverse through them. */
82                 ok1(tdb_traverse(tdb, trav, NULL) == j);
83
84                 /* Empty the first chain-worth. */
85                 for (j = 0; j < (1 << TDB_HASH_GROUP_BITS); j++)
86                         ok1(tdb_delete(tdb, key) == 0);
87
88                 ok1(tdb_check(tdb, NULL, NULL) == 0);
89
90                 for (j = (1 << TDB_HASH_GROUP_BITS);
91                      j < (16 << TDB_HASH_GROUP_BITS);
92                      j++) {
93                         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
94                         ok1(d.dsize == sizeof(j));
95                         ok1(d.dptr != NULL);
96                         ok1(d.dptr && memcmp(d.dptr, &j, d.dsize) == 0);
97                         free(d.dptr);
98                 }
99
100                 /* Traverse through them. */
101                 ok1(tdb_traverse(tdb, trav, NULL)
102                     == (15 << TDB_HASH_GROUP_BITS));
103
104                 /* Re-add */
105                 for (j = 0; j < (1 << TDB_HASH_GROUP_BITS); j++) {
106                         ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
107                 }
108                 ok1(tdb_check(tdb, NULL, NULL) == 0);
109
110                 /* Now try deleting as we go. */
111                 ok1(tdb_traverse(tdb, trav, trav)
112                     == (16 << TDB_HASH_GROUP_BITS));
113                 ok1(tdb_check(tdb, NULL, NULL) == 0);
114                 ok1(tdb_traverse(tdb, trav, NULL) == 0);
115                 tdb_close(tdb);
116         }
117
118         ok1(tap_log_messages == 0);
119         return exit_status();
120 }