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/test/echo_canceller_test_tools.h"
12
13 #include <vector>
14
15 #include "api/array_view.h"
16 #include "rtc_base/checks.h"
17 #include "rtc_base/random.h"
18 #include "test/gtest.h"
19
20 namespace webrtc {
21
TEST(EchoCancellerTestTools,FloatDelayBuffer)22 TEST(EchoCancellerTestTools, FloatDelayBuffer) {
23 constexpr size_t kDelay = 10;
24 DelayBuffer<float> delay_buffer(kDelay);
25 std::vector<float> v(1000, 0.f);
26 for (size_t k = 0; k < v.size(); ++k) {
27 v[k] = k;
28 }
29 std::vector<float> v_delayed = v;
30 constexpr size_t kBlockSize = 50;
31 for (size_t k = 0; k < rtc::CheckedDivExact(v.size(), kBlockSize); ++k) {
32 delay_buffer.Delay(
33 rtc::ArrayView<const float>(&v[k * kBlockSize], kBlockSize),
34 rtc::ArrayView<float>(&v_delayed[k * kBlockSize], kBlockSize));
35 }
36 for (size_t k = kDelay; k < v.size(); ++k) {
37 EXPECT_EQ(v[k - kDelay], v_delayed[k]);
38 }
39 }
40
TEST(EchoCancellerTestTools,IntDelayBuffer)41 TEST(EchoCancellerTestTools, IntDelayBuffer) {
42 constexpr size_t kDelay = 10;
43 DelayBuffer<int> delay_buffer(kDelay);
44 std::vector<int> v(1000, 0);
45 for (size_t k = 0; k < v.size(); ++k) {
46 v[k] = k;
47 }
48 std::vector<int> v_delayed = v;
49 const size_t kBlockSize = 50;
50 for (size_t k = 0; k < rtc::CheckedDivExact(v.size(), kBlockSize); ++k) {
51 delay_buffer.Delay(
52 rtc::ArrayView<const int>(&v[k * kBlockSize], kBlockSize),
53 rtc::ArrayView<int>(&v_delayed[k * kBlockSize], kBlockSize));
54 }
55 for (size_t k = kDelay; k < v.size(); ++k) {
56 EXPECT_EQ(v[k - kDelay], v_delayed[k]);
57 }
58 }
59
TEST(EchoCancellerTestTools,RandomizeSampleVector)60 TEST(EchoCancellerTestTools, RandomizeSampleVector) {
61 Random random_generator(42U);
62 std::vector<float> v(50, 0.f);
63 std::vector<float> v_ref = v;
64 RandomizeSampleVector(&random_generator, v);
65 EXPECT_NE(v, v_ref);
66 v_ref = v;
67 RandomizeSampleVector(&random_generator, v);
68 EXPECT_NE(v, v_ref);
69 }
70
TEST(EchoCancellerTestTools,RandomizeSampleVectorWithAmplitude)71 TEST(EchoCancellerTestTools, RandomizeSampleVectorWithAmplitude) {
72 Random random_generator(42U);
73 std::vector<float> v(50, 0.f);
74 RandomizeSampleVector(&random_generator, v, 1000.f);
75 EXPECT_GE(1000.f, *std::max_element(v.begin(), v.end()));
76 EXPECT_LE(-1000.f, *std::min_element(v.begin(), v.end()));
77 RandomizeSampleVector(&random_generator, v, 100.f);
78 EXPECT_GE(100.f, *std::max_element(v.begin(), v.end()));
79 EXPECT_LE(-100.f, *std::min_element(v.begin(), v.end()));
80 }
81
82 } // namespace webrtc
83