blob: 844ea4ac6ad1ca4ead67d038c5afc041352e63f9 [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;
26 EXPECT_THAT(counter.Avg(kMinSamples), Eq(rtc::nullopt));
27 EXPECT_THAT(counter.Avg(kMinSamples), Eq(rtc::nullopt));
28 EXPECT_THAT(counter.Max(), Eq(rtc::nullopt));
29}
30
31TEST(SampleCounterTest, NotEnoughSamples) {
32 constexpr int kMinSamples = 6;
33 SampleCounter counter;
34 for (int value : {1, 2, 3, 4, 5}) {
35 counter.Add(value);
36 }
37 EXPECT_THAT(counter.Avg(kMinSamples), Eq(rtc::nullopt));
38 EXPECT_THAT(counter.Avg(kMinSamples), Eq(rtc::nullopt));
39 EXPECT_THAT(counter.Max(), Eq(5));
40}
41
42TEST(SampleCounterTest, EnoughSamples) {
43 constexpr int kMinSamples = 5;
44 SampleCounter counter;
45 for (int value : {1, 2, 3, 4, 5}) {
46 counter.Add(value);
47 }
48 EXPECT_THAT(counter.Avg(kMinSamples), Eq(3));
49 EXPECT_THAT(counter.Variance(kMinSamples), Eq(2));
50 EXPECT_THAT(counter.Max(), Eq(5));
51}
52
53} // namespace rtc