blob: 494312383b175a92ee51c7b49799f526d361df89 [file] [log] [blame]
Ilya Nikolaevskiyed23be92017-10-12 12:38:01 +02001/*
2 * Copyright 2017 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/histogram_percentile_counter.h"
12
13#include <utility>
14#include <vector>
15
16#include "test/gtest.h"
17
18TEST(HistogramPercentileCounterTest, ReturnsCorrectPercentiles) {
19 rtc::HistogramPercentileCounter counter(10);
20 const std::vector<int> kTestValues = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
21 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
22
23 EXPECT_FALSE(counter.GetPercentile(0.5f));
24 // Pairs of {fraction, percentile value} computed by hand
25 // for |kTestValues|.
26 const std::vector<std::pair<float, uint32_t>> kTestPercentiles = {
27 {0.0f, 1}, {0.01f, 1}, {0.5f, 10}, {0.9f, 18},
28 {0.95f, 19}, {0.99f, 20}, {1.0f, 20}};
29 for (int value : kTestValues) {
30 counter.Add(value);
31 }
32 for (const auto& test_percentile : kTestPercentiles) {
33 EXPECT_EQ(test_percentile.second,
34 counter.GetPercentile(test_percentile.first).value_or(0));
35 }
36}
37
38TEST(HistogramPercentileCounterTest, HandlesEmptySequence) {
39 rtc::HistogramPercentileCounter counter(10);
40 EXPECT_FALSE(counter.GetPercentile(0.5f));
41 counter.Add(1u);
42 EXPECT_TRUE(counter.GetPercentile(0.5f));
43}