OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 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 "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 #include "webrtc/modules/audio_processing/utility/mean_calculator.h" |
| 14 |
| 15 namespace webrtc { |
| 16 |
| 17 TEST(MeanCalculatorTest, Correctness) { |
| 18 const size_t kWindowLength = 10; |
| 19 MeanCalculator mean_calculator(kWindowLength); |
| 20 size_t i = 0; |
| 21 float reference = 0.0; |
| 22 |
| 23 for (; i < kWindowLength; ++i) { |
| 24 EXPECT_EQ(mean_calculator.GetNumberNewSamples(), i % kWindowLength); |
| 25 mean_calculator.AddSample(static_cast<float>(i)); |
| 26 } |
| 27 for (; i < 3 * kWindowLength; ++i) { |
| 28 const size_t new_samples = i % kWindowLength; |
| 29 if (new_samples == 0) { |
| 30 // Sum of (i - kWindowLength) ... (i - 1) |
| 31 reference = i - 0.5 * (1 + kWindowLength); |
| 32 } |
| 33 EXPECT_EQ(mean_calculator.GetNumberNewSamples(), new_samples); |
| 34 EXPECT_EQ(reference, mean_calculator.GetLatestMean()); |
| 35 mean_calculator.AddSample(static_cast<float>(i)); |
| 36 } |
| 37 } |
| 38 |
| 39 TEST(MeanCalculatorTest, Reset) { |
| 40 const size_t kWindowLength = 10; |
| 41 MeanCalculator mean_calculator(kWindowLength); |
| 42 for (size_t i = 0; i < kWindowLength - 1; ++i) { |
| 43 mean_calculator.AddSample(static_cast<float>(i)); |
| 44 } |
| 45 EXPECT_EQ(mean_calculator.GetNumberNewSamples(), kWindowLength - 1); |
| 46 mean_calculator.Reset(); |
| 47 for (size_t i = 0; i < kWindowLength; ++i) { |
| 48 EXPECT_EQ(mean_calculator.GetNumberNewSamples(), i); |
| 49 mean_calculator.AddSample(static_cast<float>(i)); |
| 50 } |
| 51 EXPECT_EQ(mean_calculator.GetNumberNewSamples(), 0u); |
| 52 EXPECT_EQ(mean_calculator.GetLatestMean(), 0.5 * (kWindowLength - 1)); |
| 53 } |
| 54 |
| 55 } // namespace webrtc |
OLD | NEW |