tdb2: make tests include a single mega-header to simplify future patches
[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
39         plan_tests(sizeof(flags) / sizeof(flags[0]) * 2 + 1);
40         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
41                 tdb = tdb_open("run-14-exists.tdb", flags[i],
42                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
43                 if (ok1(tdb))
44                         ok1(test_records(tdb));
45                 tdb_close(tdb);
46         }
47
48         ok1(tap_log_messages == 0);
49         return exit_status();
50 }