tdb2: allow multiple chain locks.
[ccan] / ccan / tdb2 / test / run-add-remove-flags.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/traverse.c>
9 #include <ccan/tdb2/transaction.c>
10 #include <ccan/tap/tap.h>
11 #include "logging.h"
12
13 int main(int argc, char *argv[])
14 {
15         unsigned int i;
16         struct tdb_context *tdb;
17         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
18                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
19                         TDB_NOMMAP|TDB_CONVERT };
20
21         plan_tests(87);
22         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
23                 tdb = tdb_open("run-add-remove-flags.tdb", flags[i],
24                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
25                 ok1(tdb);
26                 if (!tdb)
27                         continue;
28
29                 ok1(tdb_get_flags(tdb) == tdb->flags);
30                 tap_log_messages = 0;
31                 tdb_add_flag(tdb, TDB_NOLOCK);
32                 if (flags[i] & TDB_INTERNAL)
33                         ok1(tap_log_messages == 1);
34                 else {
35                         ok1(tap_log_messages == 0);
36                         ok1(tdb_get_flags(tdb) & TDB_NOLOCK);
37                 }
38
39                 tap_log_messages = 0;
40                 tdb_add_flag(tdb, TDB_NOMMAP);
41                 if (flags[i] & TDB_INTERNAL)
42                         ok1(tap_log_messages == 1);
43                 else {
44                         ok1(tap_log_messages == 0);
45                         ok1(tdb_get_flags(tdb) & TDB_NOMMAP);
46                         ok1(tdb->file->map_ptr == NULL);
47                 }
48
49                 tap_log_messages = 0;
50                 tdb_add_flag(tdb, TDB_NOSYNC);
51                 if (flags[i] & TDB_INTERNAL)
52                         ok1(tap_log_messages == 1);
53                 else {
54                         ok1(tap_log_messages == 0);
55                         ok1(tdb_get_flags(tdb) & TDB_NOSYNC);
56                 }
57
58                 ok1(tdb_get_flags(tdb) == tdb->flags);
59
60                 tap_log_messages = 0;
61                 tdb_remove_flag(tdb, TDB_NOLOCK);
62                 if (flags[i] & TDB_INTERNAL)
63                         ok1(tap_log_messages == 1);
64                 else {
65                         ok1(tap_log_messages == 0);
66                         ok1(!(tdb_get_flags(tdb) & TDB_NOLOCK));
67                 }
68
69                 tap_log_messages = 0;
70                 tdb_remove_flag(tdb, TDB_NOMMAP);
71                 if (flags[i] & TDB_INTERNAL)
72                         ok1(tap_log_messages == 1);
73                 else {
74                         ok1(tap_log_messages == 0);
75                         ok1(!(tdb_get_flags(tdb) & TDB_NOMMAP));
76                         ok1(tdb->file->map_ptr != NULL);
77                 }
78
79                 tap_log_messages = 0;
80                 tdb_remove_flag(tdb, TDB_NOSYNC);
81                 if (flags[i] & TDB_INTERNAL)
82                         ok1(tap_log_messages == 1);
83                 else {
84                         ok1(tap_log_messages == 0);
85                         ok1(!(tdb_get_flags(tdb) & TDB_NOSYNC));
86                 }
87
88                 tdb_close(tdb);
89         }
90
91         ok1(tap_log_messages == 0);
92         return exit_status();
93 }