blob: 6fdaa364eb538298a582423541de43c8e2bc2dca [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
Karl Wiberg65c39222017-11-22 12:25:14 +010011#include "rtc_base/numerics/histogram_percentile_counter.h"
Ilya Nikolaevskiyed23be92017-10-12 12:38:01 +020012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <cstdint>
Ilya Nikolaevskiyed23be92017-10-12 12:38:01 +020014#include <utility>
15#include <vector>
16
17#include "test/gtest.h"
18
19TEST(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
39TEST(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}