tdb2: make tests include a single mega-header to simplify future patches
[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;
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
16         plan_tests(sizeof(flags) / sizeof(flags[0]) * 15 + 4 * 13);
17         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
18                 tdb = tdb_open("run-new_database.tdb", flags[i]|TDB_SEQNUM,
19                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
20                 if (!ok1(tdb))
21                         continue;
22                 
23                 ok1(tdb_get_seqnum(tdb) == 0);
24                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
25                 ok1(tdb_get_seqnum(tdb) == 1);
26                 /* Fetch doesn't change seqnum */
27                 if (ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS))
28                         free(d.dptr);
29                 ok1(tdb_get_seqnum(tdb) == 1);
30                 ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
31                 ok1(tdb_get_seqnum(tdb) == 2);
32
33                 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
34                 ok1(tdb_get_seqnum(tdb) == 3);
35                 /* Empty append works */
36                 ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
37                 ok1(tdb_get_seqnum(tdb) == 4);
38
39                 ok1(tdb_wipe_all(tdb) == TDB_SUCCESS);
40                 ok1(tdb_get_seqnum(tdb) == 5);
41
42                 if (!(flags[i] & TDB_INTERNAL)) {
43                         ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
44                         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
45                         ok1(tdb_get_seqnum(tdb) == 6);
46                         ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
47                         ok1(tdb_get_seqnum(tdb) == 7);
48                         ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
49                         ok1(tdb_get_seqnum(tdb) == 8);
50                         ok1(tdb_transaction_commit(tdb) == TDB_SUCCESS);
51                         ok1(tdb_get_seqnum(tdb) == 8);
52
53                         ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
54                         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
55                         ok1(tdb_get_seqnum(tdb) == 9);
56                         tdb_transaction_cancel(tdb);
57                         ok1(tdb_get_seqnum(tdb) == 8);
58                 }
59                 tdb_close(tdb);
60                 ok1(tap_log_messages == 0);
61         }
62         return exit_status();
63 }