histogram_unittest.cc (2228B)
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 #include <cstddef> 15 #include <vector> 16 17 #include "test/gtest.h" 18 19 namespace webrtc { 20 21 TEST(HistogramTest, Initialization) { 22 Histogram histogram(65, 32440); 23 histogram.Reset(); 24 const auto& buckets = histogram.buckets(); 25 double sum = 0.0; 26 for (size_t i = 0; i < buckets.size(); i++) { 27 EXPECT_NEAR(ldexp(std::pow(0.5, static_cast<int>(i + 1)), 30), buckets[i], 28 65537); 29 // Tolerance 65537 in Q30 corresponds to a delta of approximately 0.00006. 30 sum += buckets[i]; 31 } 32 EXPECT_EQ(1 << 30, static_cast<int>(sum)); // Should be 1 in Q30. 33 } 34 35 TEST(HistogramTest, Add) { 36 Histogram histogram(10, 32440); 37 histogram.Reset(); 38 const std::vector<int> before = histogram.buckets(); 39 const int index = 5; 40 histogram.Add(index); 41 const std::vector<int> after = histogram.buckets(); 42 EXPECT_GT(after[index], before[index]); 43 int sum = 0; 44 for (int bucket : after) { 45 sum += bucket; 46 } 47 EXPECT_EQ(1 << 30, sum); 48 } 49 50 TEST(HistogramTest, ForgetFactor) { 51 Histogram histogram(10, 32440); 52 histogram.Reset(); 53 const std::vector<int> before = histogram.buckets(); 54 const int index = 4; 55 histogram.Add(index); 56 const std::vector<int> after = histogram.buckets(); 57 for (int i = 0; i < histogram.NumBuckets(); ++i) { 58 if (i != index) { 59 EXPECT_LT(after[i], before[i]); 60 } 61 } 62 } 63 64 TEST(HistogramTest, ReachSteadyStateForgetFactor) { 65 static constexpr int kSteadyStateForgetFactor = (1 << 15) * 0.9993; 66 Histogram histogram(100, kSteadyStateForgetFactor, 1.0); 67 histogram.Reset(); 68 int n = (1 << 15) / ((1 << 15) - kSteadyStateForgetFactor); 69 for (int i = 0; i < n; ++i) { 70 histogram.Add(0); 71 } 72 EXPECT_EQ(histogram.forget_factor_for_testing(), kSteadyStateForgetFactor); 73 } 74 75 } // namespace webrtc