]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-14-exists.c
f264a6f2c98330655c1debd4b3063dfacc96b9ed
[ccan] / ccan / tdb2 / test / run-14-exists.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 static bool test_records(struct tdb_context *tdb)
13 {
14         int i;
15         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
16         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
17
18         for (i = 0; i < 1000; i++) {
19                 if (tdb_exists(tdb, key))
20                         return false;
21                 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
22                         return false;
23                 if (!tdb_exists(tdb, key))
24                         return false;
25         }
26
27         for (i = 0; i < 1000; i++) {
28                 if (!tdb_exists(tdb, key))
29                         return false;
30                 if (tdb_delete(tdb, key) != 0)
31                         return false;
32                 if (tdb_exists(tdb, key))
33                         return false;
34         }
35         return true;
36 }
37
38 int main(int argc, char *argv[])
39 {
40         unsigned int i;
41         struct tdb_context *tdb;
42         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
43                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
44                         TDB_NOMMAP|TDB_CONVERT };
45
46         plan_tests(sizeof(flags) / sizeof(flags[0]) * 2 + 1);
47         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
48                 tdb = tdb_open("run-14-exists.tdb", flags[i],
49                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
50                 if (ok1(tdb))
51                         ok1(test_records(tdb));
52                 tdb_close(tdb);
53         }
54
55         ok1(tap_log_messages == 0);
56         return exit_status();
57 }