xref: /6.0.3/moxi/tests/moxi/htgram_test.c (revision d0366df5)
1/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
3#include "src/config.h"
4#include <platform/cbassert.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8
9#include <src/htgram.h>
10
11static 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    cb_assert(h0 != NULL);
20    cb_assert(htgram_get_bin_start(h0) == 0);
21    cb_assert(htgram_get_bin_start_width(h0) == 5);
22    cb_assert(htgram_get_bin_width_growth(h0) == 1.0);
23    cb_assert(htgram_get_num_bins(h0) == 3);
24
25    start = width = count = 123;
26    cb_assert(htgram_get_bin_data(h0, -1, &start, &width, &count) == false);
27    cb_assert(start == 123);
28    cb_assert(width == 123);
29    cb_assert(count == 0);
30
31    for (i = 0; i < (int) htgram_get_num_bins(h0); i++) {
32        start = width = count = 123;
33        cb_assert(htgram_get_bin_data(h0, i, &start, &width, &count) == true);
34        cb_assert(start == i * 5);
35        cb_assert(width == 5);
36        cb_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        cb_assert(htgram_get_bin_data(h0, i, &start, &width, &count) == true);
45        cb_assert(start == i * 5);
46        cb_assert(width == 5);
47        cb_assert(count == 6);
48    }
49
50    start = width = count = 123;
51    cb_assert(htgram_get_bin_data(h0, i, &start, &width, &count) == false);
52    cb_assert(start == 123);
53    cb_assert(width == 123);
54    cb_assert(count == 0);
55
56    htgram_reset(h0);
57
58    start = width = count = 123;
59    cb_assert(htgram_get_bin_data(h0, -1, &start, &width, &count) == false);
60    cb_assert(start == 123);
61    cb_assert(width == 123);
62    cb_assert(count == 0);
63
64    for (i = 0; i < (int) htgram_get_num_bins(h0); i++) {
65        start = width = count = 123;
66        cb_assert(htgram_get_bin_data(h0, i, &start, &width, &count) == true);
67        cb_assert(start == i * 5);
68        cb_assert(width == 5);
69        cb_assert(count == 0);
70    }
71
72    start = width = count = 123;
73    cb_assert(htgram_get_bin_data(h0, i, &start, &width, &count) == false);
74    cb_assert(start == 123);
75    cb_assert(width == 123);
76    cb_assert(count == 0);
77
78    htgram_incr(h0, -10000, 111);
79    htgram_incr(h0, 10000, 222);
80
81    start = width = count = 123;
82    cb_assert(htgram_get_bin_data(h0, -1, &start, &width, &count) == false);
83    cb_assert(start == 123);
84    cb_assert(width == 123);
85    cb_assert(count == 111);
86
87    start = width = count = 123;
88    cb_assert(htgram_get_bin_data(h0, i, &start, &width, &count) == false);
89    cb_assert(start == 123);
90    cb_assert(width == 123);
91    cb_assert(count == 222);
92
93    htgram_destroy(h0);
94}
95
96static 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        cb_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    cb_assert(htgram_get_bin_data(h0, 200 + 36 - 1, &start, &width, &count) == true);
117    cb_assert(start == 27692301);
118    cb_assert(width == 13845150);
119    cb_assert(count == 111);
120
121    htgram_reset(h0);
122
123    cb_assert(htgram_get_bin_data(h0, 200 + 36 - 1, &start, &width, &count) == true);
124    cb_assert(start == 27692301);
125    cb_assert(width == 13845150);
126    cb_assert(count == 0);
127}
128
129int main(void) {
130    testSimple();
131    testChained();
132
133    return 0;
134}
135