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