tdb2: test: try (almost) all tests with TDB_VERSION1 flag.
[ccan] / ccan / tdb2 / test / run-81-seqnum.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
4
5 int main(int argc, char *argv[])
6 {
7         unsigned int i, seq;
8         struct tdb_context *tdb;
9         struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */
10         struct tdb_data key = tdb_mkdata("key", 3);
11         struct tdb_data data = tdb_mkdata("data", 4);
12         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
13                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
14                         TDB_NOMMAP|TDB_CONVERT,
15                         TDB_INTERNAL|TDB_VERSION1, TDB_VERSION1,
16                         TDB_NOMMAP|TDB_VERSION1,
17                         TDB_INTERNAL|TDB_CONVERT|TDB_VERSION1,
18                         TDB_CONVERT|TDB_VERSION1,
19                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
20
21         plan_tests(sizeof(flags) / sizeof(flags[0]) * 15 + 8 * 13);
22         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
23                 tdb = tdb_open("run-new_database.tdb", flags[i]|TDB_SEQNUM,
24                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
25                 if (!ok1(tdb))
26                         continue;
27                 
28                 seq = 0;
29                 ok1(tdb_get_seqnum(tdb) == seq);
30                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
31                 ok1(tdb_get_seqnum(tdb) == ++seq);
32                 /* Fetch doesn't change seqnum */
33                 if (ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS))
34                         free(d.dptr);
35                 ok1(tdb_get_seqnum(tdb) == seq);
36                 ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
37                 /* Append in tdb1 (or store over value) bumps twice! */
38                 if (flags[i] & TDB_VERSION1)
39                         seq++;
40                 ok1(tdb_get_seqnum(tdb) == ++seq);
41
42                 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
43                 ok1(tdb_get_seqnum(tdb) == ++seq);
44                 /* Empty append works */
45                 ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
46                 ok1(tdb_get_seqnum(tdb) == ++seq);
47
48                 ok1(tdb_wipe_all(tdb) == TDB_SUCCESS);
49                 ok1(tdb_get_seqnum(tdb) == ++seq);
50
51                 if (!(flags[i] & TDB_INTERNAL)) {
52                         ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
53                         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
54                         ok1(tdb_get_seqnum(tdb) == ++seq);
55                         /* Append in tdb1 (or store over value) bumps twice! */
56                         if (flags[i] & TDB_VERSION1)
57                                 seq++;
58                         ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
59                         ok1(tdb_get_seqnum(tdb) == ++seq);
60                         ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
61                         ok1(tdb_get_seqnum(tdb) == ++seq);
62                         ok1(tdb_transaction_commit(tdb) == TDB_SUCCESS);
63                         ok1(tdb_get_seqnum(tdb) == seq);
64
65                         ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
66                         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
67                         ok1(tdb_get_seqnum(tdb) == seq + 1);
68                         tdb_transaction_cancel(tdb);
69                         ok1(tdb_get_seqnum(tdb) == seq);
70                 }
71                 tdb_close(tdb);
72                 ok1(tap_log_messages == 0);
73         }
74         return exit_status();
75 }