]> git.ozlabs.org Git - ccan/blob - ccan/tally/test/run-histogram.c
tally: simplify, add Y axis to graph.
[ccan] / ccan / tally / test / run-histogram.c
1 #include <ccan/tally/tally.c>
2 #include <ccan/tap/tap.h>
3
4 int main(void)
5 {
6         int i;
7         struct tally *tally;
8         char *graph, *p;
9         bool trunc;
10
11         plan_tests(100 + 1 + 10 + 1 + 100 + 1 + 10 + 1 + 10 * 2 + 1);
12
13         /* Uniform distribution, easy. */
14         tally = tally_new(100);
15         for (i = 0; i < 100; i++)
16                 tally_add(tally, i);
17
18         /* 1:1 height. */
19         graph = p = tally_histogram(tally, 20, 100);
20         for (i = 0; i < 100; i++) {
21                 char *eol = strchr(p, '\n');
22
23                 /* We expect it filled all way to the end. */
24                 ok1(eol - p == 20);
25                 p = eol + 1;
26         }
27         ok1(!*p);
28         free(graph);
29
30         /* Reduced height. */
31         trunc = false;
32         graph = p = tally_histogram(tally, 20, 10);
33         for (i = 0; i < 10; i++) {
34                 char *eol = strchr(p, '\n');
35
36                 /* Last once can be truncated (bucket aliasing) */
37                 if (eol) {
38                         if (eol - p < 20) {
39                                 ok1(!trunc);
40                                 trunc = true;
41                         } else if (eol - p == 20) {
42                                 ok1(!trunc);
43                         } else {
44                                 fail("Overwidth line %s", p);
45                         }
46                 } else
47                         /* We should, at worst, half-fill graph */
48                         ok1(i > 5);
49
50                 if (eol)
51                         p = eol + 1;
52         }
53         ok1(!*p);
54         free(graph);
55
56         /* Enlarged height (gets capped). */
57         graph = p = tally_histogram(tally, 20, 1000);
58         for (i = 0; i < 100; i++) {
59                 char *eol = strchr(p, '\n');
60                 /* We expect it filled all way to the end. */
61                 ok1(eol - p == 20);
62                 p = eol + 1;
63         }
64         ok1(!*p);
65         free(graph);
66         free(tally);
67
68         /* Distinctive increasing pattern. */
69         tally = tally_new(10);
70         for (i = 0; i < 10; i++) {
71                 unsigned int j;
72                 for (j = 0; j <= i; j++)
73                         tally_add(tally, i);
74         }
75
76         graph = p = tally_histogram(tally, 10, 10);
77         for (i = 0; i < 10; i++) {
78                 char *eol = strchr(p, '\n');
79                 ok1(eol - p == i+1);
80                 p = eol + 1;
81         }
82         ok1(!*p);
83         diag("Here's the pretty: %s", graph);
84         free(graph);
85         free(tally);
86
87         /* With negative values. */
88         tally = tally_new(10);
89         for (i = 0; i < 10; i++) {
90                 tally_add(tally, i - 5);
91         }
92
93         graph = p = tally_histogram(tally, 10, 10);
94         for (i = 0; i < 10; i++) {
95                 char *eol = strchr(p, '\n');
96
97                 /* We expect it filled all way to the end. */
98                 ok1(eol - p == 10);
99
100                 /* Check min/max labels. */
101                 if (i == 0)
102                         ok1(strncmp(p, "-5*", 3) == 0);
103                 else if (i == 9)
104                         ok1(strncmp(p, "4*", 2) == 0);
105                 else if (i == 5)
106                         ok1(p[0] == '+'); /* 0 marker */
107                 else
108                         ok1(p[0] == '|');
109                 p = eol + 1;
110         }
111         ok1(!*p);
112         diag("Here's the pretty: %s", graph);
113         free(graph);
114         free(tally);
115
116         return exit_status();
117 }