tdb2: test: convert (non-invasive) run tests to api tests.
[ccan] / ccan / tdb2 / test / api-91-get-stats.c
1 #include <ccan/tdb2/tdb2.h>
2 #include <ccan/tap/tap.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdlib.h>
7 #include <stddef.h>
8 #include "logging.h"
9
10 int main(int argc, char *argv[])
11 {
12         unsigned int i;
13         struct tdb_context *tdb;
14         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
15                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT,
16                         TDB_VERSION1, TDB_NOMMAP|TDB_VERSION1,
17                         TDB_CONVERT|TDB_VERSION1,
18                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
19
20         plan_tests(sizeof(flags) / sizeof(flags[0]) * 11);
21
22         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
23                 union tdb_attribute *attr;
24                 struct tdb_data key = tdb_mkdata("key", 3);
25
26                 tdb = tdb_open("run-91-get-stats.tdb", flags[i],
27                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
28                 ok1(tdb);
29                 ok1(tdb_store(tdb, key, key, TDB_REPLACE) == 0);
30
31                 /* Use malloc so valgrind will catch overruns. */
32                 attr = malloc(sizeof *attr);
33                 attr->stats.base.attr = TDB_ATTRIBUTE_STATS;
34                 attr->stats.size = sizeof(*attr);
35
36                 ok1(tdb_get_attribute(tdb, attr) == 0);
37                 ok1(attr->stats.size == sizeof(*attr));
38                 ok1(attr->stats.allocs > 0);
39                 ok1(attr->stats.expands > 0);
40                 ok1(attr->stats.locks > 0);
41                 free(attr);
42
43                 /* Try short one. */
44                 attr = malloc(offsetof(struct tdb_attribute_stats, allocs)
45                               + sizeof(attr->stats.allocs));
46                 attr->stats.base.attr = TDB_ATTRIBUTE_STATS;
47                 attr->stats.size = offsetof(struct tdb_attribute_stats, allocs)
48                         + sizeof(attr->stats.allocs);
49                 ok1(tdb_get_attribute(tdb, attr) == 0);
50                 ok1(attr->stats.size == sizeof(*attr));
51                 ok1(attr->stats.allocs > 0);
52                 free(attr);
53                 ok1(tap_log_messages == 0);
54
55                 tdb_close(tdb);
56
57         }
58         return exit_status();
59 }