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