1 /*
2 * Copyright (c) 2013 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/transient/dyadic_decimator.h"
12
13 #include "test/gtest.h"
14
15 namespace webrtc {
16
17 static const size_t kEvenBufferLength = 6;
18 static const size_t kOddBufferLength = 5;
19 static const size_t kOutBufferLength = 3;
20
21 int16_t const test_buffer_even_len[] = {0, 1, 2, 3, 4, 5};
22 int16_t const test_buffer_odd_len[] = {0, 1, 2, 3, 4};
23 int16_t test_buffer_out[kOutBufferLength];
24
TEST(DyadicDecimatorTest,GetOutLengthToDyadicDecimate)25 TEST(DyadicDecimatorTest, GetOutLengthToDyadicDecimate) {
26 EXPECT_EQ(3u, GetOutLengthToDyadicDecimate(6, false));
27 EXPECT_EQ(3u, GetOutLengthToDyadicDecimate(6, true));
28 EXPECT_EQ(3u, GetOutLengthToDyadicDecimate(5, false));
29 EXPECT_EQ(2u, GetOutLengthToDyadicDecimate(5, true));
30 }
31
TEST(DyadicDecimatorTest,DyadicDecimateErrorValues)32 TEST(DyadicDecimatorTest, DyadicDecimateErrorValues) {
33 size_t out_samples = 0;
34
35 out_samples = DyadicDecimate(static_cast<int16_t*>(NULL), kEvenBufferLength,
36 false, // Even sequence.
37 test_buffer_out, kOutBufferLength);
38 EXPECT_EQ(0u, out_samples);
39
40 out_samples = DyadicDecimate(test_buffer_even_len, kEvenBufferLength,
41 false, // Even sequence.
42 static_cast<int16_t*>(NULL), kOutBufferLength);
43 EXPECT_EQ(0u, out_samples);
44
45 // Less than required `out_length`.
46 out_samples = DyadicDecimate(test_buffer_even_len, kEvenBufferLength,
47 false, // Even sequence.
48 test_buffer_out, 2);
49 EXPECT_EQ(0u, out_samples);
50 }
51
TEST(DyadicDecimatorTest,DyadicDecimateEvenLengthEvenSequence)52 TEST(DyadicDecimatorTest, DyadicDecimateEvenLengthEvenSequence) {
53 size_t expected_out_samples =
54 GetOutLengthToDyadicDecimate(kEvenBufferLength, false);
55
56 size_t out_samples = DyadicDecimate(test_buffer_even_len, kEvenBufferLength,
57 false, // Even sequence.
58 test_buffer_out, kOutBufferLength);
59
60 EXPECT_EQ(expected_out_samples, out_samples);
61
62 EXPECT_EQ(0, test_buffer_out[0]);
63 EXPECT_EQ(2, test_buffer_out[1]);
64 EXPECT_EQ(4, test_buffer_out[2]);
65 }
66
TEST(DyadicDecimatorTest,DyadicDecimateEvenLengthOddSequence)67 TEST(DyadicDecimatorTest, DyadicDecimateEvenLengthOddSequence) {
68 size_t expected_out_samples =
69 GetOutLengthToDyadicDecimate(kEvenBufferLength, true);
70
71 size_t out_samples = DyadicDecimate(test_buffer_even_len, kEvenBufferLength,
72 true, // Odd sequence.
73 test_buffer_out, kOutBufferLength);
74
75 EXPECT_EQ(expected_out_samples, out_samples);
76
77 EXPECT_EQ(1, test_buffer_out[0]);
78 EXPECT_EQ(3, test_buffer_out[1]);
79 EXPECT_EQ(5, test_buffer_out[2]);
80 }
81
TEST(DyadicDecimatorTest,DyadicDecimateOddLengthEvenSequence)82 TEST(DyadicDecimatorTest, DyadicDecimateOddLengthEvenSequence) {
83 size_t expected_out_samples =
84 GetOutLengthToDyadicDecimate(kOddBufferLength, false);
85
86 size_t out_samples = DyadicDecimate(test_buffer_odd_len, kOddBufferLength,
87 false, // Even sequence.
88 test_buffer_out, kOutBufferLength);
89
90 EXPECT_EQ(expected_out_samples, out_samples);
91
92 EXPECT_EQ(0, test_buffer_out[0]);
93 EXPECT_EQ(2, test_buffer_out[1]);
94 EXPECT_EQ(4, test_buffer_out[2]);
95 }
96
TEST(DyadicDecimatorTest,DyadicDecimateOddLengthOddSequence)97 TEST(DyadicDecimatorTest, DyadicDecimateOddLengthOddSequence) {
98 size_t expected_out_samples =
99 GetOutLengthToDyadicDecimate(kOddBufferLength, true);
100
101 size_t out_samples = DyadicDecimate(test_buffer_odd_len, kOddBufferLength,
102 true, // Odd sequence.
103 test_buffer_out, kOutBufferLength);
104
105 EXPECT_EQ(expected_out_samples, out_samples);
106
107 EXPECT_EQ(1, test_buffer_out[0]);
108 EXPECT_EQ(3, test_buffer_out[1]);
109 }
110
111 } // namespace webrtc
112