s4 dns: Implement RFC-compatible update prescan
[Samba/gebeck_regimport.git] / lib / ccan / tally / test / run-histogram.c
bloba9894ecd8539eb3113241a2dfa171bf4bd6a09d2
1 #include <ccan/tally/tally.c>
2 #include <ccan/tap/tap.h>
4 int main(void)
6 int i;
7 struct tally *tally;
8 char *graph, *p;
10 plan_tests(100 + 1 + 10 + 1 + 100 + 1 + 10 + 1 + 10 * 2 + 1);
12 /* Uniform distribution, easy. */
13 tally = tally_new(100);
14 for (i = 0; i < 100; i++)
15 tally_add(tally, i);
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');
22 /* We expect it filled all way to the end. */
23 ok1(eol - p == 20);
24 p = eol + 1;
26 ok1(!*p);
27 free(graph);
29 /* Reduced height. */
30 graph = p = tally_histogram(tally, 20, 10);
31 for (i = 0; i < 10; i++) {
32 char *eol = strchr(p, '\n');
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);
41 if (eol)
42 p = eol + 1;
44 ok1(!*p);
45 free(graph);
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;
55 ok1(!*p);
56 free(graph);
57 free(tally);
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);
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;
73 ok1(!*p);
74 diag("Here's the pretty: %s", graph);
75 free(graph);
76 free(tally);
78 /* With negative values. */
79 tally = tally_new(10);
80 for (i = 0; i < 10; i++) {
81 tally_add(tally, i - 5);
84 graph = p = tally_histogram(tally, 10, 10);
85 for (i = 0; i < 10; i++) {
86 char *eol = strchr(p, '\n');
88 /* We expect it filled all way to the end. */
89 ok1(eol - p == 10);
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;
102 ok1(!*p);
103 diag("Here's the pretty: %s", graph);
104 free(graph);
105 free(tally);
107 return exit_status();