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