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