1 /*
2  *  Copyright (c) 2017 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_processing/echo_detector/moving_max.h"
12 
13 #include "test/gtest.h"
14 
15 namespace webrtc {
16 
17 // Test if the maximum is correctly found.
TEST(MovingMaxTests,SimpleTest)18 TEST(MovingMaxTests, SimpleTest) {
19   MovingMax test_moving_max(5);
20   test_moving_max.Update(1.0f);
21   test_moving_max.Update(1.1f);
22   test_moving_max.Update(1.9f);
23   test_moving_max.Update(1.87f);
24   test_moving_max.Update(1.89f);
25   EXPECT_EQ(1.9f, test_moving_max.max());
26 }
27 
28 // Test if values fall out of the window when expected.
TEST(MovingMaxTests,SlidingWindowTest)29 TEST(MovingMaxTests, SlidingWindowTest) {
30   MovingMax test_moving_max(5);
31   test_moving_max.Update(1.0f);
32   test_moving_max.Update(1.9f);
33   test_moving_max.Update(1.7f);
34   test_moving_max.Update(1.87f);
35   test_moving_max.Update(1.89f);
36   test_moving_max.Update(1.3f);
37   test_moving_max.Update(1.2f);
38   EXPECT_LT(test_moving_max.max(), 1.9f);
39 }
40 
41 // Test if Clear() works as expected.
TEST(MovingMaxTests,ClearTest)42 TEST(MovingMaxTests, ClearTest) {
43   MovingMax test_moving_max(5);
44   test_moving_max.Update(1.0f);
45   test_moving_max.Update(1.1f);
46   test_moving_max.Update(1.9f);
47   test_moving_max.Update(1.87f);
48   test_moving_max.Update(1.89f);
49   EXPECT_EQ(1.9f, test_moving_max.max());
50   test_moving_max.Clear();
51   EXPECT_EQ(0.f, test_moving_max.max());
52 }
53 
54 // Test the decay of the estimated maximum.
TEST(MovingMaxTests,DecayTest)55 TEST(MovingMaxTests, DecayTest) {
56   MovingMax test_moving_max(1);
57   test_moving_max.Update(1.0f);
58   float previous_value = 1.0f;
59   for (int i = 0; i < 500; i++) {
60     test_moving_max.Update(0.0f);
61     EXPECT_LT(test_moving_max.max(), previous_value);
62     EXPECT_GT(test_moving_max.max(), 0.0f);
63     previous_value = test_moving_max.max();
64   }
65   EXPECT_LT(test_moving_max.max(), 0.01f);
66 }
67 
68 }  // namespace webrtc
69