tdb2: test: fix run-57-die-during-transaction.c to be more efficient.
[ccan] / ccan / tdb2 / test / run-91-get-stats.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         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
10                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT,
11                         TDB_VERSION1, TDB_NOMMAP|TDB_VERSION1,
12                         TDB_CONVERT|TDB_VERSION1,
13                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
14
15         plan_tests(sizeof(flags) / sizeof(flags[0]) * 11);
16
17         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
18                 union tdb_attribute *attr;
19                 struct tdb_data key = tdb_mkdata("key", 3);
20
21                 tdb = tdb_open("run-91-get-stats.tdb", flags[i],
22                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
23                 ok1(tdb);
24                 ok1(tdb_store(tdb, key, key, TDB_REPLACE) == 0);
25
26                 /* Use malloc so valgrind will catch overruns. */
27                 attr = malloc(sizeof *attr);
28                 attr->stats.base.attr = TDB_ATTRIBUTE_STATS;
29                 attr->stats.size = sizeof(*attr);
30
31                 ok1(tdb_get_attribute(tdb, attr) == 0);
32                 ok1(attr->stats.size == sizeof(*attr));
33                 ok1(attr->stats.allocs > 0);
34                 ok1(attr->stats.expands > 0);
35                 ok1(attr->stats.locks > 0);
36                 free(attr);
37
38                 /* Try short one. */
39                 attr = malloc(offsetof(struct tdb_attribute_stats, allocs)
40                               + sizeof(attr->stats.allocs));
41                 attr->stats.base.attr = TDB_ATTRIBUTE_STATS;
42                 attr->stats.size = offsetof(struct tdb_attribute_stats, allocs)
43                         + sizeof(attr->stats.allocs);
44                 ok1(tdb_get_attribute(tdb, attr) == 0);
45                 ok1(attr->stats.size == sizeof(*attr));
46                 ok1(attr->stats.allocs > 0);
47                 free(attr);
48                 ok1(tap_log_messages == 0);
49
50                 tdb_close(tdb);
51
52         }
53         return exit_status();
54 }