summaryrefslogtreecommitdiffstats
path: root/ml/Tests.cc
blob: 7d369d48d4a1a6e50a8f6bd1b774f4b7ef729326 (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// SPDX-License-Identifier: GPL-3.0-or-later

#include "BitBufferCounter.h"
#include "BitRateWindow.h"

#include "gtest/gtest.h"

using namespace ml;

TEST(BitBufferCounterTest, Cap_4) {
    size_t Capacity = 4;
    BitBufferCounter BBC(Capacity);

    // No bits set
    EXPECT_EQ(BBC.numSetBits(), 0);

    // All ones
    for (size_t Idx = 0; Idx != (2 * Capacity); Idx++) {
        BBC.insert(true);

        EXPECT_EQ(BBC.numSetBits(), std::min(Idx + 1, Capacity));
    }

    // All zeroes
    for (size_t Idx = 0; Idx != Capacity; Idx++) {
        BBC.insert(false);

        if (Idx < Capacity)
            EXPECT_EQ(BBC.numSetBits(), Capacity - (Idx + 1));
        else
            EXPECT_EQ(BBC.numSetBits(), 0);
    }

    // Even ones/zeroes
    for (size_t Idx = 0; Idx != (2 * Capacity); Idx++)
        BBC.insert(Idx % 2 == 0);
    EXPECT_EQ(BBC.numSetBits(), Capacity / 2);
}

using State = BitRateWindow::State;
using Edge = BitRateWindow::Edge;
using Result = std::pair<Edge, size_t>;

TEST(BitRateWindowTest, Cycles) {
    /* Test the FSM by going through its two cycles:
     *  1) NotFilled -> AboveThreshold -> Idle -> NotFilled
     *  2) NotFilled -> BelowThreshold -> AboveThreshold -> Idle -> NotFilled
     *
     * Check the window's length on every new state transition.
     */

    size_t MinLength = 4, MaxLength = 6, IdleLength = 5;
    size_t SetBitsThreshold = 3;

    Result R;
    BitRateWindow BRW(MinLength, MaxLength, IdleLength, SetBitsThreshold);

    /*
     * 1st cycle
     */

    // NotFilled -> AboveThreshold
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::AboveThreshold));
    EXPECT_EQ(R.second, MinLength);

    // AboveThreshold -> Idle
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::AboveThreshold));
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::AboveThreshold));

    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::Idle));
    EXPECT_EQ(R.second, MaxLength);


    // Idle -> NotFilled
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::Idle, State::NotFilled));
    EXPECT_EQ(R.second, 1);

    // NotFilled -> AboveThreshold
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::AboveThreshold));
    EXPECT_EQ(R.second, MinLength);

    /*
     * 2nd cycle
     */

    BRW = BitRateWindow(MinLength, MaxLength, IdleLength, SetBitsThreshold);

    // NotFilled -> BelowThreshold
    R = BRW.insert(false);
    EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
    R = BRW.insert(false);
    EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
    R = BRW.insert(false);
    EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
    R = BRW.insert(false);
    EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::BelowThreshold));
    EXPECT_EQ(R.second, MinLength);

    // BelowThreshold -> BelowThreshold:
    //      Check the state's self loop by adding set bits that will keep the
    //      bit buffer below the specified threshold.
    //
    for (size_t Idx = 0; Idx != 2 * MaxLength; Idx++) {
        R = BRW.insert(Idx % 2 == 0);
        EXPECT_EQ(R.first, std::make_pair(State::BelowThreshold, State::BelowThreshold));
        EXPECT_EQ(R.second, MinLength);
    }

    // Verify that at the end of the loop the internal bit buffer contains
    // "1010". Do so by adding one set bit and checking that we remain below
    // the specified threshold.
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::BelowThreshold, State::BelowThreshold));
    EXPECT_EQ(R.second, MinLength);

    // BelowThreshold -> AboveThreshold
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::BelowThreshold, State::AboveThreshold));
    EXPECT_EQ(R.second, MinLength);

    // AboveThreshold -> Idle:
    //      Do the transition without filling the max window size this time.
    R = BRW.insert(false);
    EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::Idle));
    EXPECT_EQ(R.second, MinLength);

    // Idle -> NotFilled
    R = BRW.insert(false);
    EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
    R = BRW.insert(false);
    EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
    R = BRW.insert(false);
    EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
    R = BRW.insert(false);
    EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
    R = BRW.insert(false);
    EXPECT_EQ(R.first, std::make_pair(State::Idle, State::NotFilled));
    EXPECT_EQ(R.second, 1);

    // NotFilled -> AboveThreshold
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::NotFilled));
    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::AboveThreshold));
    EXPECT_EQ(R.second, MinLength);
}

TEST(BitRateWindowTest, ConsecutiveOnes) {
    size_t MinLength = 120, MaxLength = 240, IdleLength = 30;
    size_t SetBitsThreshold = 30;

    Result R;
    BitRateWindow BRW(MinLength, MaxLength, IdleLength, SetBitsThreshold);

    for (size_t Idx = 0; Idx != MaxLength; Idx++)
        R = BRW.insert(false);
    EXPECT_EQ(R.first, std::make_pair(State::BelowThreshold, State::BelowThreshold));
    EXPECT_EQ(R.second, MinLength);

    for (size_t Idx = 0; Idx != SetBitsThreshold; Idx++) {
        EXPECT_EQ(R.first, std::make_pair(State::BelowThreshold, State::BelowThreshold));
        R = BRW.insert(true);
    }
    EXPECT_EQ(R.first, std::make_pair(State::BelowThreshold, State::AboveThreshold));
    EXPECT_EQ(R.second, MinLength);

    // At this point the window's buffer contains:
    //      (MinLength - SetBitsThreshold = 90) 0s, followed by
    //                  (SetBitsThreshold = 30) 1s.
    //
    // To go below the threshold, we need to add (90 + 1) more 0s in the window's
    // buffer. At that point, the the window's buffer will contain:
    //                  (SetBitsThreshold = 29) 1s, followed by
    //      (MinLength - SetBitsThreshold = 91) 0s.
    //
    // Right before adding the last 0, we expect the window's length to be equal to 210,
    // because the bit buffer has gone through these bits:
    //      (MinLength - SetBitsThreshold = 90) 0s, followed by
    //                  (SetBitsThreshold = 30) 1s, followed by
    //      (MinLength - SetBitsThreshold = 90) 0s.

    for (size_t Idx = 0; Idx != (MinLength - SetBitsThreshold); Idx++) {
        R = BRW.insert(false);
        EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::AboveThreshold));
    }
    EXPECT_EQ(R.second, 2 * MinLength - SetBitsThreshold);
    R = BRW.insert(false);
    EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::Idle));

    // Continue with the Idle -> NotFilled edge.
    for (size_t Idx = 0; Idx != IdleLength - 1; Idx++) {
        R = BRW.insert(false);
        EXPECT_EQ(R.first, std::make_pair(State::Idle, State::Idle));
    }
    R = BRW.insert(false);
    EXPECT_EQ(R.first, std::make_pair(State::Idle, State::NotFilled));
    EXPECT_EQ(R.second, 1);
}

TEST(BitRateWindowTest, WithHoles) {
    size_t MinLength = 120, MaxLength = 240, IdleLength = 30;
    size_t SetBitsThreshold = 30;

    Result R;
    BitRateWindow BRW(MinLength, MaxLength, IdleLength, SetBitsThreshold);

    for (size_t Idx = 0; Idx != MaxLength; Idx++)
        R = BRW.insert(false);

    for (size_t Idx = 0; Idx != SetBitsThreshold / 3; Idx++)
        R = BRW.insert(true);
    for (size_t Idx = 0; Idx != SetBitsThreshold / 3; Idx++)
        R = BRW.insert(false);
    for (size_t Idx = 0; Idx != SetBitsThreshold / 3; Idx++)
        R = BRW.insert(true);
    for (size_t Idx = 0; Idx != SetBitsThreshold / 3; Idx++)
        R = BRW.insert(false);
    for (size_t Idx = 0; Idx != SetBitsThreshold / 3; Idx++)
        R = BRW.insert(true);

    EXPECT_EQ(R.first, std::make_pair(State::BelowThreshold, State::AboveThreshold));
    EXPECT_EQ(R.second, MinLength);

    // The window's bit buffer contains:
    //      70 0s, 10 1s, 10 0s, 10 1s, 10 0s, 10 1s.
    // Where: 70 = MinLength - (5 / 3) * SetBitsThresholds, ie. we need
    // to add (70 + 1) more zeros to make the bit buffer go below the
    // threshold and then the window's length should be:
    //      70 + 50 + 70 = 190.

    BitRateWindow::Edge E;
    do {
        R = BRW.insert(false);
        E = R.first;
    } while (E.first != State::AboveThreshold || E.second != State::Idle);
    EXPECT_EQ(R.second, 2 * MinLength - (5 * SetBitsThreshold) / 3);
}

TEST(BitRateWindowTest, MinWindow) {
    size_t MinLength = 120, MaxLength = 240, IdleLength = 30;
    size_t SetBitsThreshold = 30;

    Result R;
    BitRateWindow BRW(MinLength, MaxLength, IdleLength, SetBitsThreshold);

    BRW.insert(true);
    BRW.insert(false);
    for (size_t Idx = 2; Idx != SetBitsThreshold; Idx++)
        BRW.insert(true);
    for (size_t Idx = SetBitsThreshold; Idx != MinLength - 1; Idx++)
        BRW.insert(false);

    R = BRW.insert(true);
    EXPECT_EQ(R.first, std::make_pair(State::NotFilled, State::AboveThreshold));
    EXPECT_EQ(R.second, MinLength);

    R = BRW.insert(false);
    EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::Idle));
}

TEST(BitRateWindowTest, MaxWindow) {
    size_t MinLength = 100, MaxLength = 200, IdleLength = 30;
    size_t SetBitsThreshold = 50;

    Result R;
    BitRateWindow BRW(MinLength, MaxLength, IdleLength, SetBitsThreshold);

    for (size_t Idx = 0; Idx != MaxLength; Idx++)
        R = BRW.insert(Idx % 2 == 0);
    EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::AboveThreshold));
    EXPECT_EQ(R.second, MaxLength);

    R = BRW.insert(false);
    EXPECT_EQ(R.first, std::make_pair(State::AboveThreshold, State::Idle));
}