blob: 57812fcb4e67053a565add10d09c5265e581ccd5 [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
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <initializer_list>
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020014
15#include "test/gmock.h"
16#include "test/gtest.h"
17
18using testing::Eq;
19
20namespace rtc {
21
22TEST(SampleCounterTest, ProcessesNoSamples) {
23 constexpr int kMinSamples = 1;
24 SampleCounter counter;
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020025 EXPECT_THAT(counter.Avg(kMinSamples), Eq(absl::nullopt));
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020026 EXPECT_THAT(counter.Max(), Eq(absl::nullopt));
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020027}
28
29TEST(SampleCounterTest, NotEnoughSamples) {
30 constexpr int kMinSamples = 6;
31 SampleCounter counter;
32 for (int value : {1, 2, 3, 4, 5}) {
33 counter.Add(value);
34 }
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020035 EXPECT_THAT(counter.Avg(kMinSamples), Eq(absl::nullopt));
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020036 EXPECT_THAT(counter.Max(), Eq(5));
37}
38
39TEST(SampleCounterTest, EnoughSamples) {
40 constexpr int kMinSamples = 5;
41 SampleCounter counter;
42 for (int value : {1, 2, 3, 4, 5}) {
43 counter.Add(value);
44 }
45 EXPECT_THAT(counter.Avg(kMinSamples), Eq(3));
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020046 EXPECT_THAT(counter.Max(), Eq(5));
47}
48
Ilya Nikolaevskiy8c688452018-09-11 13:46:22 +020049TEST(SampleCounterTest, ComputesVariance) {
50 constexpr int kMinSamples = 5;
51 SampleCounterWithVariance counter;
52 for (int value : {1, 2, 3, 4, 5}) {
53 counter.Add(value);
54 }
55 EXPECT_THAT(counter.Variance(kMinSamples), Eq(2));
56}
57
58TEST(SampleCounterTest, AggregatesTwoCounters) {
59 constexpr int kMinSamples = 5;
60 SampleCounterWithVariance counter1;
61 for (int value : {1, 2, 3}) {
62 counter1.Add(value);
63 }
64 SampleCounterWithVariance counter2;
65 for (int value : {4, 5}) {
66 counter2.Add(value);
67 }
68 // Before aggregation there is not enough samples.
69 EXPECT_THAT(counter1.Avg(kMinSamples), Eq(absl::nullopt));
70 EXPECT_THAT(counter1.Variance(kMinSamples), Eq(absl::nullopt));
71 // Aggregate counter2 in counter1.
72 counter1.Add(counter2);
73 EXPECT_THAT(counter1.Avg(kMinSamples), Eq(3));
74 EXPECT_THAT(counter1.Max(), Eq(5));
75 EXPECT_THAT(counter1.Variance(kMinSamples), Eq(2));
76}
77
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020078} // namespace rtc