blob: e30a2956dc5404e51e28206f45c0e6a128fdc8e7 [file] [log] [blame]
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +01001/*
2 * Copyright (c) 2019 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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "modules/audio_coding/neteq/histogram.h"
12
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010013#include <cmath>
14
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010015#include "test/gtest.h"
16
17namespace webrtc {
18
19TEST(HistogramTest, Initialization) {
20 Histogram histogram(65, 32440);
21 histogram.Reset();
22 const auto& buckets = histogram.buckets();
23 double sum = 0.0;
24 for (size_t i = 0; i < buckets.size(); i++) {
25 EXPECT_NEAR(ldexp(std::pow(0.5, static_cast<int>(i + 1)), 30), buckets[i],
26 65537);
27 // Tolerance 65537 in Q30 corresponds to a delta of approximately 0.00006.
28 sum += buckets[i];
29 }
30 EXPECT_EQ(1 << 30, static_cast<int>(sum)); // Should be 1 in Q30.
31}
32
33TEST(HistogramTest, Add) {
34 Histogram histogram(10, 32440);
35 histogram.Reset();
36 const std::vector<int> before = histogram.buckets();
37 const int index = 5;
38 histogram.Add(index);
39 const std::vector<int> after = histogram.buckets();
40 EXPECT_GT(after[index], before[index]);
41 int sum = 0;
42 for (int bucket : after) {
43 sum += bucket;
44 }
45 EXPECT_EQ(1 << 30, sum);
46}
47
48TEST(HistogramTest, ForgetFactor) {
49 Histogram histogram(10, 32440);
50 histogram.Reset();
51 const std::vector<int> before = histogram.buckets();
52 const int index = 4;
53 histogram.Add(index);
54 const std::vector<int> after = histogram.buckets();
55 for (int i = 0; i < histogram.NumBuckets(); ++i) {
56 if (i != index) {
57 EXPECT_LT(after[i], before[i]);
58 }
59 }
60}
61
Jakob Ivarssone9a2ee22019-05-22 16:54:09 +020062TEST(HistogramTest, ReachSteadyStateForgetFactor) {
63 static constexpr int kSteadyStateForgetFactor = (1 << 15) * 0.9993;
64 Histogram histogram(100, kSteadyStateForgetFactor, 1.0);
65 histogram.Reset();
66 int n = (1 << 15) / ((1 << 15) - kSteadyStateForgetFactor);
67 for (int i = 0; i < n; ++i) {
68 histogram.Add(0);
69 }
70 EXPECT_EQ(histogram.forget_factor_for_testing(), kSteadyStateForgetFactor);
71}
72
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010073} // namespace webrtc