1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 
3 #include "src/config.h"
4 #include <assert.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 
9 #include <src/htgram.h>
10 
testSimple(void)11 static void testSimple(void) {
12     HTGRAM_HANDLE h0;
13     int i;
14     int64_t start;
15     int64_t width;
16     uint64_t count;
17 
18     h0 = htgram_mk(0, 5, 1.0, 3, NULL);
19     assert(h0 != NULL);
20     assert(htgram_get_bin_start(h0) == 0);
21     assert(htgram_get_bin_start_width(h0) == 5);
22     assert(htgram_get_bin_width_growth(h0) == 1.0);
23     assert(htgram_get_num_bins(h0) == 3);
24 
25     start = width = count = 123;
26     assert(htgram_get_bin_data(h0, -1, &start, &width, &count) == false);
27     assert(start == 123);
28     assert(width == 123);
29     assert(count == 0);
30 
31     for (i = 0; i < (int) htgram_get_num_bins(h0); i++) {
32         start = width = count = 123;
33         assert(htgram_get_bin_data(h0, i, &start, &width, &count) == true);
34         assert(start == i * 5);
35         assert(width == 5);
36         assert(count == 0);
37 
38         htgram_incr(h0, (i * 5) + 0, 1);
39         htgram_incr(h0, (i * 5) + 1, 2);
40         htgram_incr(h0, (i * 5) + 2, 3);
41         htgram_incr(h0, (i * 5) + 5, 0);
42 
43         start = width = count = 123;
44         assert(htgram_get_bin_data(h0, i, &start, &width, &count) == true);
45         assert(start == i * 5);
46         assert(width == 5);
47         assert(count == 6);
48     }
49 
50     start = width = count = 123;
51     assert(htgram_get_bin_data(h0, i, &start, &width, &count) == false);
52     assert(start == 123);
53     assert(width == 123);
54     assert(count == 0);
55 
56     htgram_reset(h0);
57 
58     start = width = count = 123;
59     assert(htgram_get_bin_data(h0, -1, &start, &width, &count) == false);
60     assert(start == 123);
61     assert(width == 123);
62     assert(count == 0);
63 
64     for (i = 0; i < (int) htgram_get_num_bins(h0); i++) {
65         start = width = count = 123;
66         assert(htgram_get_bin_data(h0, i, &start, &width, &count) == true);
67         assert(start == i * 5);
68         assert(width == 5);
69         assert(count == 0);
70     }
71 
72     start = width = count = 123;
73     assert(htgram_get_bin_data(h0, i, &start, &width, &count) == false);
74     assert(start == 123);
75     assert(width == 123);
76     assert(count == 0);
77 
78     htgram_incr(h0, -10000, 111);
79     htgram_incr(h0, 10000, 222);
80 
81     start = width = count = 123;
82     assert(htgram_get_bin_data(h0, -1, &start, &width, &count) == false);
83     assert(start == 123);
84     assert(width == 123);
85     assert(count == 111);
86 
87     start = width = count = 123;
88     assert(htgram_get_bin_data(h0, i, &start, &width, &count) == false);
89     assert(start == 123);
90     assert(width == 123);
91     assert(count == 222);
92 
93     htgram_destroy(h0);
94 }
95 
testChained(void)96 static void testChained(void) {
97     HTGRAM_HANDLE h0, h1;
98 
99     int64_t start;
100     int64_t width;
101     uint64_t count;
102     int i;
103 
104     /* Have 200 bins from [0 to 2000), with bin widths of 10. */
105     /* Have 36 bins from 2000 onwards, with bin width growing at 1.5, chained. */
106     h1 = htgram_mk(2000, 10, 1.5, 36, NULL);
107     h0 = htgram_mk(0, 10, 1.0, 200, h1);
108 
109     for (i = 0; i < (int) (htgram_get_num_bins(h0) + htgram_get_num_bins(h1)); i++) {
110         assert(htgram_get_bin_data(h0, i, &start, &width, &count) == true);
111         /* printf("%d %d %d %d\n", i, start, width, count); */
112     }
113 
114     htgram_incr(h0, 28000000, 111);
115 
116     assert(htgram_get_bin_data(h0, 200 + 36 - 1, &start, &width, &count) == true);
117     assert(start == 27692301);
118     assert(width == 13845150);
119     assert(count == 111);
120 
121     htgram_reset(h0);
122 
123     assert(htgram_get_bin_data(h0, 200 + 36 - 1, &start, &width, &count) == true);
124     assert(start == 27692301);
125     assert(width == 13845150);
126     assert(count == 0);
127 }
128 
main(void)129 int main(void) {
130     testSimple();
131     testChained();
132 
133     return 0;
134 }
135