Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame^] | 1 | /* |
| 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 | |
| 19 | using testing::Eq; |
| 20 | |
| 21 | namespace rtc { |
| 22 | |
| 23 | TEST(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 | |
| 31 | TEST(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 | |
| 42 | TEST(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 |