tdb2: test: fix run-57-die-during-transaction.c to be more efficient.
[ccan] / ccan / tdb2 / test / run-14-exists.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
4
5 static bool test_records(struct tdb_context *tdb)
6 {
7         int i;
8         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
9         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
10
11         for (i = 0; i < 1000; i++) {
12                 if (tdb_exists(tdb, key))
13                         return false;
14                 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
15                         return false;
16                 if (!tdb_exists(tdb, key))
17                         return false;
18         }
19
20         for (i = 0; i < 1000; i++) {
21                 if (!tdb_exists(tdb, key))
22                         return false;
23                 if (tdb_delete(tdb, key) != 0)
24                         return false;
25                 if (tdb_exists(tdb, key))
26                         return false;
27         }
28         return true;
29 }
30
31 int main(int argc, char *argv[])
32 {
33         unsigned int i;
34         struct tdb_context *tdb;
35         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
36                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
37                         TDB_NOMMAP|TDB_CONVERT,
38                         TDB_INTERNAL|TDB_VERSION1, TDB_VERSION1,
39                         TDB_NOMMAP|TDB_VERSION1,
40                         TDB_INTERNAL|TDB_CONVERT|TDB_VERSION1,
41                         TDB_CONVERT|TDB_VERSION1,
42                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
43
44         plan_tests(sizeof(flags) / sizeof(flags[0]) * 2 + 1);
45         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
46                 tdb = tdb_open("run-14-exists.tdb", flags[i],
47                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
48                 if (ok1(tdb))
49                         ok1(test_records(tdb));
50                 tdb_close(tdb);
51         }
52
53         ok1(tap_log_messages == 0);
54         return exit_status();
55 }