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