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