Index: webrtc/common_audio/smoothing_filter_unittest.cc |
diff --git a/webrtc/common_audio/smoothing_filter_unittest.cc b/webrtc/common_audio/smoothing_filter_unittest.cc |
index 9ffbf3c5f5aa9921439e7e372efb5e8a9e736c41..5075545f13d9ba969c25aa751098f9b4a1ad5617 100644 |
--- a/webrtc/common_audio/smoothing_filter_unittest.cc |
+++ b/webrtc/common_audio/smoothing_filter_unittest.cc |
@@ -11,6 +11,7 @@ |
#include <cmath> |
#include <memory> |
+#include "webrtc/base/fakeclock.h" |
#include "webrtc/common_audio/smoothing_filter.h" |
#include "webrtc/test/gtest.h" |
@@ -22,18 +23,14 @@ constexpr float kMaxAbsError = 1e-5f; |
constexpr int64_t kClockInitialTime = 123456; |
struct SmoothingFilterStates { |
- std::unique_ptr<SimulatedClock> simulated_clock; |
- std::unique_ptr<SmoothingFilterImpl> smoothing_filter; |
+ explicit SmoothingFilterStates(int init_time_ms) |
+ : smoothing_filter(init_time_ms) { |
+ fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(kClockInitialTime)); |
+ } |
+ rtc::ScopedFakeClock fake_clock; |
+ SmoothingFilterImpl smoothing_filter; |
}; |
-SmoothingFilterStates CreateSmoothingFilter(int init_time_ms) { |
- SmoothingFilterStates states; |
- states.simulated_clock.reset(new SimulatedClock(kClockInitialTime)); |
- states.smoothing_filter.reset( |
- new SmoothingFilterImpl(init_time_ms, states.simulated_clock.get())); |
- return states; |
-} |
- |
// This function does the following: |
// 1. Add a sample to filter at current clock, |
// 2. Advance the clock by |advance_time_ms|, |
@@ -43,9 +40,10 @@ void CheckOutput(SmoothingFilterStates* states, |
float sample, |
int advance_time_ms, |
float expected_ouput) { |
- states->smoothing_filter->AddSample(sample); |
- states->simulated_clock->AdvanceTimeMilliseconds(advance_time_ms); |
- auto output = states->smoothing_filter->GetAverage(); |
+ states->smoothing_filter.AddSample(sample); |
+ states->fake_clock.AdvanceTime( |
+ rtc::TimeDelta::FromMilliseconds(advance_time_ms)); |
+ auto output = states->smoothing_filter.GetAverage(); |
EXPECT_TRUE(output); |
EXPECT_NEAR(expected_ouput, *output, kMaxAbsError); |
} |
@@ -54,8 +52,8 @@ void CheckOutput(SmoothingFilterStates* states, |
TEST(SmoothingFilterTest, NoOutputWhenNoSampleAdded) { |
constexpr int kInitTimeMs = 100; |
- auto states = CreateSmoothingFilter(kInitTimeMs); |
- EXPECT_FALSE(states.smoothing_filter->GetAverage()); |
+ SmoothingFilterStates states(kInitTimeMs); |
+ EXPECT_FALSE(states.smoothing_filter.GetAverage()); |
} |
// Python script to calculate the reference values used in this test. |
@@ -104,7 +102,7 @@ TEST(SmoothingFilterTest, NoOutputWhenNoSampleAdded) { |
// print filter.state |
TEST(SmoothingFilterTest, CheckBehaviorAroundInitTime) { |
constexpr int kInitTimeMs = 795; |
- auto states = CreateSmoothingFilter(kInitTimeMs); |
+ SmoothingFilterStates states(kInitTimeMs); |
CheckOutput(&states, 1.0f, 500, 1.0f); |
CheckOutput(&states, 0.5f, 100, 0.680562264029f); |
CheckOutput(&states, 1.0f, 100, 0.794207139813f); |
@@ -116,50 +114,51 @@ TEST(SmoothingFilterTest, CheckBehaviorAroundInitTime) { |
TEST(SmoothingFilterTest, InitTimeEqualsZero) { |
constexpr int kInitTimeMs = 0; |
- auto states = CreateSmoothingFilter(kInitTimeMs); |
+ SmoothingFilterStates states(kInitTimeMs); |
CheckOutput(&states, 1.0f, 1, 1.0f); |
CheckOutput(&states, 0.5f, 1, 0.5f); |
} |
TEST(SmoothingFilterTest, InitTimeEqualsOne) { |
constexpr int kInitTimeMs = 1; |
- auto states = CreateSmoothingFilter(kInitTimeMs); |
+ SmoothingFilterStates states(kInitTimeMs); |
CheckOutput(&states, 1.0f, 1, 1.0f); |
CheckOutput(&states, 0.5f, 1, 1.0f * exp(-1.0f) + (1.0f - exp(-1.0f)) * 0.5f); |
} |
TEST(SmoothingFilterTest, GetAverageOutputsEmptyBeforeFirstSample) { |
constexpr int kInitTimeMs = 100; |
- auto states = CreateSmoothingFilter(kInitTimeMs); |
- EXPECT_FALSE(states.smoothing_filter->GetAverage()); |
+ SmoothingFilterStates states(kInitTimeMs); |
+ EXPECT_FALSE(states.smoothing_filter.GetAverage()); |
constexpr float kFirstSample = 1.2345f; |
- states.smoothing_filter->AddSample(kFirstSample); |
+ states.smoothing_filter.AddSample(kFirstSample); |
EXPECT_EQ(rtc::Optional<float>(kFirstSample), |
- states.smoothing_filter->GetAverage()); |
+ states.smoothing_filter.GetAverage()); |
} |
TEST(SmoothingFilterTest, CannotChangeTimeConstantDuringInitialization) { |
constexpr int kInitTimeMs = 100; |
- auto states = CreateSmoothingFilter(kInitTimeMs); |
- states.smoothing_filter->AddSample(0.0); |
+ SmoothingFilterStates states(kInitTimeMs); |
+ states.smoothing_filter.AddSample(0.0); |
// During initialization, |SetTimeConstantMs| does not take effect. |
- states.simulated_clock->AdvanceTimeMilliseconds(kInitTimeMs - 1); |
- states.smoothing_filter->AddSample(0.0); |
+ states.fake_clock.AdvanceTime( |
+ rtc::TimeDelta::FromMilliseconds(kInitTimeMs - 1)); |
+ states.smoothing_filter.AddSample(0.0); |
- EXPECT_FALSE(states.smoothing_filter->SetTimeConstantMs(kInitTimeMs * 2)); |
- EXPECT_NE(exp(-1.0f / (kInitTimeMs * 2)), states.smoothing_filter->alpha()); |
+ EXPECT_FALSE(states.smoothing_filter.SetTimeConstantMs(kInitTimeMs * 2)); |
+ EXPECT_NE(exp(-1.0f / (kInitTimeMs * 2)), states.smoothing_filter.alpha()); |
- states.simulated_clock->AdvanceTimeMilliseconds(1); |
- states.smoothing_filter->AddSample(0.0); |
+ states.fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1)); |
+ states.smoothing_filter.AddSample(0.0); |
// When initialization finishes, the time constant should be come |
// |kInitTimeConstantMs|. |
- EXPECT_FLOAT_EQ(exp(-1.0f / kInitTimeMs), states.smoothing_filter->alpha()); |
+ EXPECT_FLOAT_EQ(exp(-1.0f / kInitTimeMs), states.smoothing_filter.alpha()); |
// After initialization, |SetTimeConstantMs| takes effect. |
- EXPECT_TRUE(states.smoothing_filter->SetTimeConstantMs(kInitTimeMs * 2)); |
+ EXPECT_TRUE(states.smoothing_filter.SetTimeConstantMs(kInitTimeMs * 2)); |
EXPECT_FLOAT_EQ(exp(-1.0f / (kInitTimeMs * 2)), |
- states.smoothing_filter->alpha()); |
+ states.smoothing_filter.alpha()); |
} |
} // namespace webrtc |