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