xref: /aosp_15_r20/external/webrtc/modules/audio_coding/neteq/histogram_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #include "modules/audio_coding/neteq/histogram.h"
12 
13 #include <cmath>
14 
15 #include "test/gtest.h"
16 
17 namespace webrtc {
18 
TEST(HistogramTest,Initialization)19 TEST(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 
TEST(HistogramTest,Add)33 TEST(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 
TEST(HistogramTest,ForgetFactor)48 TEST(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 
TEST(HistogramTest,ReachSteadyStateForgetFactor)62 TEST(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 
73 }  // namespace webrtc
74