blob: 4085370037d9ca0ffdaac2f0b6d59a684ee74e28 [file] [log] [blame]
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +02001/*
2 * Copyright 2018 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/sample_counter.h"
12
13#include <utility>
14#include <vector>
15
16#include "test/gmock.h"
17#include "test/gtest.h"
18
19using testing::Eq;
20
21namespace rtc {
22
23TEST(SampleCounterTest, ProcessesNoSamples) {
24 constexpr int kMinSamples = 1;
25 SampleCounter counter;
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020026 EXPECT_THAT(counter.Avg(kMinSamples), Eq(absl::nullopt));
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020027 EXPECT_THAT(counter.Max(), Eq(absl::nullopt));
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020028}
29
30TEST(SampleCounterTest, NotEnoughSamples) {
31 constexpr int kMinSamples = 6;
32 SampleCounter counter;
33 for (int value : {1, 2, 3, 4, 5}) {
34 counter.Add(value);
35 }
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020036 EXPECT_THAT(counter.Avg(kMinSamples), Eq(absl::nullopt));
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020037 EXPECT_THAT(counter.Max(), Eq(5));
38}
39
40TEST(SampleCounterTest, EnoughSamples) {
41 constexpr int kMinSamples = 5;
42 SampleCounter counter;
43 for (int value : {1, 2, 3, 4, 5}) {
44 counter.Add(value);
45 }
46 EXPECT_THAT(counter.Avg(kMinSamples), Eq(3));
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020047 EXPECT_THAT(counter.Max(), Eq(5));
48}
49
Ilya Nikolaevskiy8c688452018-09-11 13:46:22 +020050TEST(SampleCounterTest, ComputesVariance) {
51 constexpr int kMinSamples = 5;
52 SampleCounterWithVariance counter;
53 for (int value : {1, 2, 3, 4, 5}) {
54 counter.Add(value);
55 }
56 EXPECT_THAT(counter.Variance(kMinSamples), Eq(2));
57}
58
59TEST(SampleCounterTest, AggregatesTwoCounters) {
60 constexpr int kMinSamples = 5;
61 SampleCounterWithVariance counter1;
62 for (int value : {1, 2, 3}) {
63 counter1.Add(value);
64 }
65 SampleCounterWithVariance counter2;
66 for (int value : {4, 5}) {
67 counter2.Add(value);
68 }
69 // Before aggregation there is not enough samples.
70 EXPECT_THAT(counter1.Avg(kMinSamples), Eq(absl::nullopt));
71 EXPECT_THAT(counter1.Variance(kMinSamples), Eq(absl::nullopt));
72 // Aggregate counter2 in counter1.
73 counter1.Add(counter2);
74 EXPECT_THAT(counter1.Avg(kMinSamples), Eq(3));
75 EXPECT_THAT(counter1.Max(), Eq(5));
76 EXPECT_THAT(counter1.Variance(kMinSamples), Eq(2));
77}
78
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020079} // namespace rtc