aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/histogram/hdr/histogram_iter_ut.cpp
blob: 9c291a2547bc0180c0d220566c94a85c711ea00b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "histogram_iter.h"

#include <library/cpp/testing/unittest/registar.h>

using namespace NHdr;

Y_UNIT_TEST_SUITE(THistogramIterTest) {
    Y_UNIT_TEST(RecordedValues) {
        THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
        UNIT_ASSERT(h.RecordValues(1000, 1000));
        UNIT_ASSERT(h.RecordValue(1000 * 1000));

        int index = 0;
        TRecordedValuesIterator it(h);

        while (it.Next()) {
            i64 countInBucket = it.GetCount();
            i64 countInStep = it.GetCountAddedInThisIterationStep();
            if (index == 0) {
                UNIT_ASSERT_EQUAL(countInBucket, 1000);
                UNIT_ASSERT_EQUAL(countInStep, 1000);
            } else if (index == 1) {
                UNIT_ASSERT_EQUAL(countInBucket, 1);
                UNIT_ASSERT_EQUAL(countInStep, 1);
            } else {
                UNIT_FAIL("unexpected index value: " << index);
            }

            index++;
        }

        UNIT_ASSERT_EQUAL(index, 2);
    }

    Y_UNIT_TEST(CorrectedRecordedValues) {
        THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
        UNIT_ASSERT(h.RecordValuesWithExpectedInterval(1000, 1000, 1000));
        UNIT_ASSERT(h.RecordValueWithExpectedInterval(1000 * 1000, 1000));

        int index = 0;
        i64 totalCount = 0;
        TRecordedValuesIterator it(h);

        while (it.Next()) {
            i64 countInBucket = it.GetCount();
            i64 countInStep = it.GetCountAddedInThisIterationStep();
            if (index == 0) {
                UNIT_ASSERT_EQUAL(countInBucket, 1001);
                UNIT_ASSERT_EQUAL(countInStep, 1001);
            } else {
                UNIT_ASSERT(countInBucket >= 1);
                UNIT_ASSERT(countInStep >= 1);
            }
            index++;
            totalCount += countInStep;
        }

        UNIT_ASSERT_EQUAL(index, 1000);
        UNIT_ASSERT_EQUAL(totalCount, 2000);
    }

    Y_UNIT_TEST(LinearValues) {
        THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
        UNIT_ASSERT(h.RecordValues(1000, 1000));
        UNIT_ASSERT(h.RecordValue(1000 * 1000));

        int index = 0;
        TLinearIterator it(h, 1000);

        while (it.Next()) {
            i64 countInBucket = it.GetCount();
            i64 countInStep = it.GetCountAddedInThisIterationStep();
            if (index == 0) {
                UNIT_ASSERT_EQUAL(countInBucket, 1000);
                UNIT_ASSERT_EQUAL(countInStep, 1000);
            } else if (index == 999) {
                UNIT_ASSERT_EQUAL(countInBucket, 1);
                UNIT_ASSERT_EQUAL(countInStep, 1);
            } else {
                UNIT_ASSERT_EQUAL(countInBucket, 0);
                UNIT_ASSERT_EQUAL(countInStep, 0);
            }

            index++;
        }

        UNIT_ASSERT_EQUAL(index, 1000);
    }

    Y_UNIT_TEST(CorrectLinearValues) {
        THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
        UNIT_ASSERT(h.RecordValuesWithExpectedInterval(1000, 1000, 1000));
        UNIT_ASSERT(h.RecordValueWithExpectedInterval(1000 * 1000, 1000));

        int index = 0;
        i64 totalCount = 0;
        TLinearIterator it(h, 1000);

        while (it.Next()) {
            i64 countInBucket = it.GetCount();
            i64 countInStep = it.GetCountAddedInThisIterationStep();

            if (index == 0) {
                UNIT_ASSERT_EQUAL(countInBucket, 1001);
                UNIT_ASSERT_EQUAL(countInStep, 1001);
            } else {
                UNIT_ASSERT_EQUAL(countInBucket, 1);
                UNIT_ASSERT_EQUAL(countInStep, 1);
            }

            index++;
            totalCount += countInStep;
        }

        UNIT_ASSERT_EQUAL(index, 1000);
        UNIT_ASSERT_EQUAL(totalCount, 2000);
    }

    Y_UNIT_TEST(LogarithmicValues) {
        THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
        UNIT_ASSERT(h.RecordValues(1000, 1000));
        UNIT_ASSERT(h.RecordValue(1000 * 1000));

        int index = 0;
        i64 expectedValue = 1000;
        TLogarithmicIterator it(h, 1000, 2.0);

        while (it.Next()) {
            i64 value = it.GetValue();
            i64 countInBucket = it.GetCount();
            i64 countInStep = it.GetCountAddedInThisIterationStep();

            UNIT_ASSERT_EQUAL(value, expectedValue);

            if (index == 0) {
                UNIT_ASSERT_EQUAL(countInBucket, 1000);
                UNIT_ASSERT_EQUAL(countInStep, 1000);
            } else if (index == 10) {
                UNIT_ASSERT_EQUAL(countInBucket, 0);
                UNIT_ASSERT_EQUAL(countInStep, 1);
            } else {
                UNIT_ASSERT_EQUAL(countInBucket, 0);
                UNIT_ASSERT_EQUAL(countInStep, 0);
            }

            index++;
            expectedValue *= 2;
        }

        UNIT_ASSERT_EQUAL(index, 11);
    }

    Y_UNIT_TEST(CorrectedLogarithmicValues) {
        THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
        UNIT_ASSERT(h.RecordValuesWithExpectedInterval(1000, 1000, 1000));
        UNIT_ASSERT(h.RecordValueWithExpectedInterval(1000 * 1000, 1000));

        int index = 0;
        i64 totalCount = 0;
        i64 expectedValue = 1000;
        TLogarithmicIterator it(h, 1000, 2.0);

        while (it.Next()) {
            i64 value = it.GetValue();
            i64 countInBucket = it.GetCount();
            i64 countInStep = it.GetCountAddedInThisIterationStep();

            UNIT_ASSERT_EQUAL(value, expectedValue);

            if (index == 0) {
                UNIT_ASSERT_EQUAL(countInBucket, 1001);
                UNIT_ASSERT_EQUAL(countInStep, 1001);
            }

            index++;
            totalCount += countInStep;
            expectedValue *= 2;
        }

        UNIT_ASSERT_EQUAL(index, 11);
        UNIT_ASSERT_EQUAL(totalCount, 2000);
    }

    Y_UNIT_TEST(LinearIterBucketsCorrectly) {
        THistogram h(255, 2);
        UNIT_ASSERT(h.RecordValue(193));
        UNIT_ASSERT(h.RecordValue(255));
        UNIT_ASSERT(h.RecordValue(0));
        UNIT_ASSERT(h.RecordValue(1));
        UNIT_ASSERT(h.RecordValue(64));
        UNIT_ASSERT(h.RecordValue(128));

        int index = 0;
        i64 totalCount = 0;
        TLinearIterator it(h, 64);

        while (it.Next()) {
            if (index == 0) {
                // change after iterator was created
                UNIT_ASSERT(h.RecordValue(2));
            }

            index++;
            totalCount += it.GetCountAddedInThisIterationStep();
        }

        UNIT_ASSERT_EQUAL(index, 4);
        UNIT_ASSERT_EQUAL(totalCount, 6);
    }
}