1 /*
2  *  Copyright (c) 2021 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/agc2/saturation_protector_buffer.h"
12 
13 #include "test/gmock.h"
14 #include "test/gtest.h"
15 
16 namespace webrtc {
17 namespace {
18 
19 using ::testing::Eq;
20 using ::testing::Optional;
21 
TEST(GainController2SaturationProtectorBuffer,Init)22 TEST(GainController2SaturationProtectorBuffer, Init) {
23   SaturationProtectorBuffer b;
24   EXPECT_EQ(b.Size(), 0);
25   EXPECT_FALSE(b.Front().has_value());
26 }
27 
TEST(GainController2SaturationProtectorBuffer,PushBack)28 TEST(GainController2SaturationProtectorBuffer, PushBack) {
29   SaturationProtectorBuffer b;
30   constexpr float kValue = 123.0f;
31   b.PushBack(kValue);
32   EXPECT_EQ(b.Size(), 1);
33   EXPECT_THAT(b.Front(), Optional(Eq(kValue)));
34 }
35 
TEST(GainController2SaturationProtectorBuffer,Reset)36 TEST(GainController2SaturationProtectorBuffer, Reset) {
37   SaturationProtectorBuffer b;
38   b.PushBack(123.0f);
39   b.Reset();
40   EXPECT_EQ(b.Size(), 0);
41   EXPECT_FALSE(b.Front().has_value());
42 }
43 
44 // Checks that the front value does not change until the ring buffer gets full.
TEST(GainController2SaturationProtectorBuffer,FrontUntilBufferIsFull)45 TEST(GainController2SaturationProtectorBuffer, FrontUntilBufferIsFull) {
46   SaturationProtectorBuffer b;
47   constexpr float kValue = 123.0f;
48   b.PushBack(kValue);
49   for (int i = 1; i < b.Capacity(); ++i) {
50     SCOPED_TRACE(i);
51     EXPECT_THAT(b.Front(), Optional(Eq(kValue)));
52     b.PushBack(kValue + i);
53   }
54 }
55 
56 // Checks that when the buffer is full it behaves as a shift register.
TEST(GainController2SaturationProtectorBuffer,FrontIsDelayed)57 TEST(GainController2SaturationProtectorBuffer, FrontIsDelayed) {
58   SaturationProtectorBuffer b;
59   // Fill the buffer.
60   for (int i = 0; i < b.Capacity(); ++i) {
61     b.PushBack(i);
62   }
63   // The ring buffer should now behave as a shift register with a delay equal to
64   // its capacity.
65   for (int i = b.Capacity(); i < 2 * b.Capacity() + 1; ++i) {
66     SCOPED_TRACE(i);
67     EXPECT_THAT(b.Front(), Optional(Eq(i - b.Capacity())));
68     b.PushBack(i);
69   }
70 }
71 
72 }  // namespace
73 }  // namespace webrtc
74