Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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/samples_stats_counter.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "test/gtest.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | namespace { |
| 20 | SamplesStatsCounter CreateStatsFilledWithIntsFrom1ToN(int n) { |
| 21 | std::vector<double> data; |
| 22 | for (int i = 1; i <= n; i++) { |
| 23 | data.push_back(i); |
| 24 | } |
| 25 | std::random_shuffle(data.begin(), data.end()); |
| 26 | |
| 27 | SamplesStatsCounter stats; |
| 28 | for (double v : data) { |
| 29 | stats.AddSample(v); |
| 30 | } |
| 31 | return stats; |
| 32 | } |
| 33 | } // namespace |
| 34 | |
| 35 | TEST(SamplesStatsCounter, FullSimpleTest) { |
| 36 | SamplesStatsCounter stats = CreateStatsFilledWithIntsFrom1ToN(100); |
| 37 | |
| 38 | ASSERT_TRUE(!stats.IsEmpty()); |
| 39 | ASSERT_DOUBLE_EQ(stats.GetMin(), 1.0); |
| 40 | ASSERT_DOUBLE_EQ(stats.GetMax(), 100.0); |
| 41 | ASSERT_DOUBLE_EQ(stats.GetAverage(), 50.5); |
| 42 | ASSERT_DOUBLE_EQ(stats.GetPercentile(0), 1); |
| 43 | for (int i = 1; i <= 100; i++) { |
| 44 | double p = i / 100.0; |
| 45 | ASSERT_GE(stats.GetPercentile(p), i); |
| 46 | ASSERT_LT(stats.GetPercentile(p), i + 1); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | TEST(SamplesStatsCounter, FractionPercentile) { |
| 51 | SamplesStatsCounter stats = CreateStatsFilledWithIntsFrom1ToN(5); |
| 52 | |
| 53 | ASSERT_DOUBLE_EQ(stats.GetPercentile(0.5), 3); |
| 54 | } |
| 55 | |
| 56 | TEST(SamplesStatsCounter, TestBorderValues) { |
| 57 | SamplesStatsCounter stats = CreateStatsFilledWithIntsFrom1ToN(5); |
| 58 | |
| 59 | ASSERT_GE(stats.GetPercentile(0.01), 1); |
| 60 | ASSERT_LT(stats.GetPercentile(0.01), 2); |
| 61 | ASSERT_DOUBLE_EQ(stats.GetPercentile(1.0), 5); |
| 62 | } |
| 63 | |
| 64 | } // namespace webrtc |