tdb2: test: convert (non-invasive) run tests to api tests.
[ccan] / ccan / tdb2 / test / api-summary.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 "logging.h"
8
9 int main(int argc, char *argv[])
10 {
11         unsigned int i, j;
12         struct tdb_context *tdb;
13         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
14                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
15                         TDB_NOMMAP|TDB_CONVERT,
16                         TDB_INTERNAL|TDB_VERSION1, TDB_VERSION1,
17                         TDB_NOMMAP|TDB_VERSION1,
18                         TDB_INTERNAL|TDB_CONVERT|TDB_VERSION1,
19                         TDB_CONVERT|TDB_VERSION1,
20                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
21         struct tdb_data key = { (unsigned char *)&j, sizeof(j) };
22         struct tdb_data data = { (unsigned char *)&j, sizeof(j) };
23         char *summary;
24
25         plan_tests(sizeof(flags) / sizeof(flags[0]) * (1 + 2 * 5) + 1);
26         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
27                 tdb = tdb_open("run-summary.tdb", flags[i],
28                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
29                 ok1(tdb);
30                 if (!tdb)
31                         continue;
32
33                 /* Put some stuff in there. */
34                 for (j = 0; j < 500; j++) {
35                         /* Make sure padding varies to we get some graphs! */
36                         data.dsize = j % (sizeof(j) + 1);
37                         if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
38                                 fail("Storing in tdb");
39                 }
40
41                 for (j = 0;
42                      j <= TDB_SUMMARY_HISTOGRAMS;
43                      j += TDB_SUMMARY_HISTOGRAMS) {
44                         ok1(tdb_summary(tdb, j, &summary) == TDB_SUCCESS);
45                         ok1(strstr(summary, "Number of records: 500\n"));
46                         ok1(strstr(summary, "Smallest/average/largest keys: 4/4/4\n"));
47                         ok1(strstr(summary, "Smallest/average/largest data: 0/2/4\n"));
48                         if (!(flags[i] & TDB_VERSION1)
49                             && j == TDB_SUMMARY_HISTOGRAMS) {
50                                 ok1(strstr(summary, "|")
51                                     && strstr(summary, "*"));
52                         } else {
53                                 ok1(!strstr(summary, "|")
54                                     && !strstr(summary, "*"));
55                         }
56                         free(summary);
57                 }
58                 tdb_close(tdb);
59         }
60
61         ok1(tap_log_messages == 0);
62         return exit_status();
63 }