1 /*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "rtc_base/numerics/moving_max_counter.h"
12
13 #include "test/gtest.h"
14
TEST(MovingMaxCounter,ReportsMaximumInTheWindow)15 TEST(MovingMaxCounter, ReportsMaximumInTheWindow) {
16 rtc::MovingMaxCounter<int> counter(100);
17 counter.Add(1, 1);
18 EXPECT_EQ(counter.Max(1), 1);
19 counter.Add(2, 30);
20 EXPECT_EQ(counter.Max(30), 2);
21 counter.Add(100, 60);
22 EXPECT_EQ(counter.Max(60), 100);
23 counter.Add(4, 70);
24 EXPECT_EQ(counter.Max(70), 100);
25 counter.Add(5, 90);
26 EXPECT_EQ(counter.Max(90), 100);
27 }
28
TEST(MovingMaxCounter,IgnoresOldElements)29 TEST(MovingMaxCounter, IgnoresOldElements) {
30 rtc::MovingMaxCounter<int> counter(100);
31 counter.Add(1, 1);
32 counter.Add(2, 30);
33 counter.Add(100, 60);
34 counter.Add(4, 70);
35 counter.Add(5, 90);
36 EXPECT_EQ(counter.Max(160), 100);
37 // 100 is now out of the window. Next maximum is 5.
38 EXPECT_EQ(counter.Max(161), 5);
39 }
40
TEST(MovingMaxCounter,HandlesEmptyWindow)41 TEST(MovingMaxCounter, HandlesEmptyWindow) {
42 rtc::MovingMaxCounter<int> counter(100);
43 counter.Add(123, 1);
44 EXPECT_TRUE(counter.Max(101).has_value());
45 EXPECT_FALSE(counter.Max(102).has_value());
46 }
47
TEST(MovingMaxCounter,HandlesSamplesWithEqualTimestamps)48 TEST(MovingMaxCounter, HandlesSamplesWithEqualTimestamps) {
49 rtc::MovingMaxCounter<int> counter(100);
50 counter.Add(2, 30);
51 EXPECT_EQ(counter.Max(30), 2);
52 counter.Add(5, 30);
53 EXPECT_EQ(counter.Max(30), 5);
54 counter.Add(4, 30);
55 EXPECT_EQ(counter.Max(30), 5);
56 counter.Add(1, 90);
57 EXPECT_EQ(counter.Max(150), 1);
58 }
59