]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-summary.c
d6ccf2fa7c570801f2802322e0dc3d3d238d7b2c
[ccan] / ccan / tdb2 / test / run-summary.c
1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/free.c>
3 #include <ccan/tdb2/lock.c>
4 #include <ccan/tdb2/io.c>
5 #include <ccan/tdb2/hash.c>
6 #include <ccan/tdb2/check.c>
7 #include <ccan/tdb2/summary.c>
8 #include <ccan/tap/tap.h>
9 #include "logging.h"
10
11 int main(int argc, char *argv[])
12 {
13         unsigned int i, j;
14         struct tdb_context *tdb;
15         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
16                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
17                         TDB_NOMMAP|TDB_CONVERT };
18         struct tdb_data key = { (unsigned char *)&j, sizeof(j) };
19         struct tdb_data data = { (unsigned char *)&j, sizeof(j) };
20         char *summary;
21
22         plan_tests(sizeof(flags) / sizeof(flags[0]) * (1 + 2 * 7) + 1);
23         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
24                 tdb = tdb_open("run-summary.tdb", flags[i],
25                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
26                 ok1(tdb);
27                 if (!tdb)
28                         continue;
29
30                 /* Put some stuff in there. */
31                 for (j = 0; j < 500; j++)
32                         if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
33                                 fail("Storing in tdb");
34
35                 for (j = 0;
36                      j <= TDB_SUMMARY_HISTOGRAMS;
37                      j += TDB_SUMMARY_HISTOGRAMS) {
38                         summary = tdb_summary(tdb, j);
39                         ok1(strstr(summary, "Number of records: 500\n"));
40                         ok1(strstr(summary, "Smallest/average/largest keys: 4/4/4\n"));
41                         ok1(strstr(summary, "Smallest/average/largest data: 4/4/4\n"));
42                         ok1(strstr(summary, "Free bucket 8"));
43                         ok1(strstr(summary, "Free bucket 16"));
44                         ok1(strstr(summary, "Free bucket 24"));
45                         if (j == TDB_SUMMARY_HISTOGRAMS)
46                                 ok1(strstr(summary, "|")
47                                     && strstr(summary, "*"));
48                         else
49                                 ok1(!strstr(summary, "|")
50                                     && !strstr(summary, "*"));
51                         free(summary);
52                 }
53                 tdb_close(tdb);
54         }
55
56         ok1(tap_log_messages == 0);
57         return exit_status();
58 }
59
60