]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-12-store.c
tdb2: unify tdb1_append into tdb_append
[ccan] / ccan / tdb2 / test / run-12-store.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
4
5 /* We use the same seed which we saw a failure on. */
6 static uint64_t fixedhash(const void *key, size_t len, uint64_t seed, void *p)
7 {
8         return hash64_stable((const unsigned char *)key, len,
9                              *(uint64_t *)p);
10 }
11
12 int main(int argc, char *argv[])
13 {
14         unsigned int i, j;
15         struct tdb_context *tdb;
16         uint64_t seed = 16014841315512641303ULL;
17         union tdb_attribute fixed_hattr
18                 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
19                               .fn = fixedhash,
20                               .data = &seed } };
21         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
22                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
23                         TDB_NOMMAP|TDB_CONVERT };
24         struct tdb_data key = { (unsigned char *)&j, sizeof(j) };
25         struct tdb_data data = { (unsigned char *)&j, sizeof(j) };
26
27         fixed_hattr.base.next = &tap_log_attr;
28
29         plan_tests(sizeof(flags) / sizeof(flags[0]) * (1 + 500 * 3) + 1);
30         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
31                 tdb = tdb_open("run-12-store.tdb", flags[i],
32                                O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
33                 ok1(tdb);
34                 if (!tdb)
35                         continue;
36
37                 /* We seemed to lose some keys.
38                  * Insert and check they're in there! */
39                 for (j = 0; j < 500; j++) {
40                         struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */
41                         ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
42                         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
43                         ok1(tdb_deq(d, data));
44                         free(d.dptr);
45                 }
46                 tdb_close(tdb);
47         }
48
49         ok1(tap_log_messages == 0);
50         return exit_status();
51 }
52
53