henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "modules/audio_coding/neteq/audio_multi_vector.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 12 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | |
Philipp Hancke | c4fe825 | 2025-05-06 22:39:54 | [diff] [blame] | 15 | #include <cstdint> |
| 16 | #include <cstring> |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 17 | #include <vector> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 18 | |
Philipp Hancke | c4fe825 | 2025-05-06 22:39:54 | [diff] [blame] | 19 | #include "modules/audio_coding/neteq/audio_vector.h" |
Karl Wiberg | e40468b | 2017-11-22 09:42:26 | [diff] [blame] | 20 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 21 | #include "test/gtest.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | // This is a value-parameterized test. The test cases are instantiated with |
| 26 | // different values for the test parameter, which is used to determine the |
| 27 | // number of channels in the AudioMultiBuffer. Note that it is not possible |
| 28 | // to combine typed testing with value-parameterized testing, and since the |
| 29 | // tests for AudioVector already covers a number of different type parameters, |
| 30 | // this test focuses on testing different number of channels, and keeping the |
| 31 | // value type constant. |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 32 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 33 | class AudioMultiVectorTest : public ::testing::TestWithParam<size_t> { |
| 34 | protected: |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 35 | AudioMultiVectorTest() |
| 36 | : num_channels_(GetParam()), // Get the test parameter. |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 37 | array_interleaved_(num_channels_ * array_length()) {} |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 38 | |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 39 | ~AudioMultiVectorTest() = default; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 40 | |
| 41 | virtual void SetUp() { |
| 42 | // Populate test arrays. |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 43 | for (size_t i = 0; i < array_length(); ++i) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 44 | array_[i] = static_cast<int16_t>(i); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 45 | } |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 46 | int16_t* ptr = array_interleaved_.data(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 47 | // Write 100, 101, 102, ... for first channel. |
| 48 | // Write 200, 201, 202, ... for second channel. |
| 49 | // And so on. |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 50 | for (size_t i = 0; i < array_length(); ++i) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 51 | for (size_t j = 1; j <= num_channels_; ++j) { |
Evan Shrubsole | ef95b20 | 2025-02-24 14:55:04 | [diff] [blame] | 52 | *ptr = checked_cast<int16_t>(j * 100 + i); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 53 | ++ptr; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 58 | size_t array_length() const { return sizeof(array_) / sizeof(array_[0]); } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 59 | |
| 60 | const size_t num_channels_; |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 61 | int16_t array_[10]; |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 62 | std::vector<int16_t> array_interleaved_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | // Create and destroy AudioMultiVector objects, both empty and with a predefined |
| 66 | // length. |
| 67 | TEST_P(AudioMultiVectorTest, CreateAndDestroy) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 68 | AudioMultiVector vec1(num_channels_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 69 | EXPECT_TRUE(vec1.Empty()); |
| 70 | EXPECT_EQ(num_channels_, vec1.Channels()); |
| 71 | EXPECT_EQ(0u, vec1.Size()); |
| 72 | |
| 73 | size_t initial_size = 17; |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 74 | AudioMultiVector vec2(num_channels_, initial_size); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 75 | EXPECT_FALSE(vec2.Empty()); |
| 76 | EXPECT_EQ(num_channels_, vec2.Channels()); |
| 77 | EXPECT_EQ(initial_size, vec2.Size()); |
| 78 | } |
| 79 | |
| 80 | // Test the subscript operator [] for getting and setting. |
| 81 | TEST_P(AudioMultiVectorTest, SubscriptOperator) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 82 | AudioMultiVector vec(num_channels_, array_length()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 83 | for (size_t channel = 0; channel < num_channels_; ++channel) { |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 84 | for (size_t i = 0; i < array_length(); ++i) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 85 | vec[channel][i] = static_cast<int16_t>(i); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 86 | // Make sure to use the const version. |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 | [diff] [blame] | 87 | const AudioVector& audio_vec = vec[channel]; |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 88 | EXPECT_EQ(static_cast<int16_t>(i), audio_vec[i]); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Test the PushBackInterleaved method and the CopyFrom method. The Clear |
| 94 | // method is also invoked. |
| 95 | TEST_P(AudioMultiVectorTest, PushBackInterleavedAndCopy) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 96 | AudioMultiVector vec(num_channels_); |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 97 | vec.PushBackInterleaved(array_interleaved_); |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 98 | AudioMultiVector vec_copy(num_channels_); |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 99 | vec.CopyTo(&vec_copy); // Copy from `vec` to `vec_copy`. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 100 | ASSERT_EQ(num_channels_, vec.Channels()); |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 101 | ASSERT_EQ(array_length(), vec.Size()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 102 | ASSERT_EQ(num_channels_, vec_copy.Channels()); |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 103 | ASSERT_EQ(array_length(), vec_copy.Size()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 104 | for (size_t channel = 0; channel < vec.Channels(); ++channel) { |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 105 | for (size_t i = 0; i < array_length(); ++i) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 106 | EXPECT_EQ(static_cast<int16_t>((channel + 1) * 100 + i), vec[channel][i]); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 107 | EXPECT_EQ(vec[channel][i], vec_copy[channel][i]); |
| 108 | } |
| 109 | } |
| 110 | |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 111 | // Clear `vec` and verify that it is empty. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 112 | vec.Clear(); |
| 113 | EXPECT_TRUE(vec.Empty()); |
| 114 | |
| 115 | // Now copy the empty vector and verify that the copy becomes empty too. |
henrik.lundin@webrtc.org | f6ab6f8 | 2014-09-04 10:58:43 | [diff] [blame] | 116 | vec.CopyTo(&vec_copy); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 117 | EXPECT_TRUE(vec_copy.Empty()); |
| 118 | } |
| 119 | |
| 120 | // Try to copy to a NULL pointer. Nothing should happen. |
| 121 | TEST_P(AudioMultiVectorTest, CopyToNull) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 122 | AudioMultiVector vec(num_channels_); |
| 123 | AudioMultiVector* vec_copy = NULL; |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 124 | vec.PushBackInterleaved(array_interleaved_); |
henrik.lundin@webrtc.org | f6ab6f8 | 2014-09-04 10:58:43 | [diff] [blame] | 125 | vec.CopyTo(vec_copy); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // Test the PushBack method with another AudioMultiVector as input argument. |
| 129 | TEST_P(AudioMultiVectorTest, PushBackVector) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 130 | AudioMultiVector vec1(num_channels_, array_length()); |
| 131 | AudioMultiVector vec2(num_channels_, array_length()); |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 132 | // Set the first vector to [0, 1, ..., array_length() - 1] + |
| 133 | // 100 * channel_number. |
| 134 | // Set the second vector to [array_length(), array_length() + 1, ..., |
| 135 | // 2 * array_length() - 1] + 100 * channel_number. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 136 | for (size_t channel = 0; channel < num_channels_; ++channel) { |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 137 | for (size_t i = 0; i < array_length(); ++i) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 138 | vec1[channel][i] = static_cast<int16_t>(i + 100 * channel); |
| 139 | vec2[channel][i] = |
| 140 | static_cast<int16_t>(i + 100 * channel + array_length()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | // Append vec2 to the back of vec1. |
| 144 | vec1.PushBack(vec2); |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 145 | ASSERT_EQ(2u * array_length(), vec1.Size()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 146 | for (size_t channel = 0; channel < num_channels_; ++channel) { |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 147 | for (size_t i = 0; i < 2 * array_length(); ++i) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 148 | EXPECT_EQ(static_cast<int16_t>(i + 100 * channel), vec1[channel][i]); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Test the PushBackFromIndex method. |
| 154 | TEST_P(AudioMultiVectorTest, PushBackFromIndex) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 155 | AudioMultiVector vec1(num_channels_); |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 156 | vec1.PushBackInterleaved(array_interleaved_); |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 157 | AudioMultiVector vec2(num_channels_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 158 | |
| 159 | // Append vec1 to the back of vec2 (which is empty). Read vec1 from the second |
| 160 | // last element. |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 161 | vec2.PushBackFromIndex(vec1, array_length() - 2); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 162 | ASSERT_EQ(2u, vec2.Size()); |
| 163 | for (size_t channel = 0; channel < num_channels_; ++channel) { |
| 164 | for (size_t i = 0; i < 2; ++i) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 165 | EXPECT_EQ(array_interleaved_[channel + |
| 166 | num_channels_ * (array_length() - 2 + i)], |
| 167 | vec2[channel][i]); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // Starts with pushing some values to the vector, then test the Zeros method. |
| 173 | TEST_P(AudioMultiVectorTest, Zeros) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 174 | AudioMultiVector vec(num_channels_); |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 175 | vec.PushBackInterleaved(array_interleaved_); |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 176 | vec.Zeros(2 * array_length()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 177 | ASSERT_EQ(num_channels_, vec.Channels()); |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 178 | ASSERT_EQ(2u * array_length(), vec.Size()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 179 | for (size_t channel = 0; channel < num_channels_; ++channel) { |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 180 | for (size_t i = 0; i < 2 * array_length(); ++i) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 181 | EXPECT_EQ(0, vec[channel][i]); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // Test the ReadInterleaved method |
| 187 | TEST_P(AudioMultiVectorTest, ReadInterleaved) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 188 | AudioMultiVector vec(num_channels_); |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 189 | vec.PushBackInterleaved(array_interleaved_); |
| 190 | int16_t* output = new int16_t[array_interleaved_.size()]; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 191 | // Read 5 samples. |
| 192 | size_t read_samples = 5; |
| 193 | EXPECT_EQ(num_channels_ * read_samples, |
| 194 | vec.ReadInterleaved(read_samples, output)); |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 195 | EXPECT_EQ(0, memcmp(array_interleaved_.data(), output, |
| 196 | read_samples * sizeof(int16_t))); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 197 | |
| 198 | // Read too many samples. Expect to get all samples from the vector. |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 199 | EXPECT_EQ(array_interleaved_.size(), |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 200 | vec.ReadInterleaved(array_length() + 1, output)); |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 201 | EXPECT_EQ(0, memcmp(array_interleaved_.data(), output, |
| 202 | read_samples * sizeof(int16_t))); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 203 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 204 | delete[] output; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 205 | } |
| 206 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 207 | // Test the PopFront method. |
| 208 | TEST_P(AudioMultiVectorTest, PopFront) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 209 | AudioMultiVector vec(num_channels_); |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 210 | vec.PushBackInterleaved(array_interleaved_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 211 | vec.PopFront(1); // Remove one element from each channel. |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 212 | ASSERT_EQ(array_length() - 1u, vec.Size()); |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 213 | // Let `ptr` point to the second element of the first channel in the |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 214 | // interleaved array. |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 215 | int16_t* ptr = &array_interleaved_[num_channels_]; |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 216 | for (size_t i = 0; i < array_length() - 1; ++i) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 217 | for (size_t channel = 0; channel < num_channels_; ++channel) { |
| 218 | EXPECT_EQ(*ptr, vec[channel][i]); |
| 219 | ++ptr; |
| 220 | } |
| 221 | } |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 222 | vec.PopFront(array_length()); // Remove more elements than vector size. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 223 | EXPECT_EQ(0u, vec.Size()); |
| 224 | } |
| 225 | |
| 226 | // Test the PopBack method. |
| 227 | TEST_P(AudioMultiVectorTest, PopBack) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 228 | AudioMultiVector vec(num_channels_); |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 229 | vec.PushBackInterleaved(array_interleaved_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 230 | vec.PopBack(1); // Remove one element from each channel. |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 231 | ASSERT_EQ(array_length() - 1u, vec.Size()); |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 232 | // Let `ptr` point to the first element of the first channel in the |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 233 | // interleaved array. |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 234 | int16_t* ptr = array_interleaved_.data(); |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 235 | for (size_t i = 0; i < array_length() - 1; ++i) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 236 | for (size_t channel = 0; channel < num_channels_; ++channel) { |
| 237 | EXPECT_EQ(*ptr, vec[channel][i]); |
| 238 | ++ptr; |
| 239 | } |
| 240 | } |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 241 | vec.PopBack(array_length()); // Remove more elements than vector size. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 242 | EXPECT_EQ(0u, vec.Size()); |
| 243 | } |
| 244 | |
| 245 | // Test the AssertSize method. |
| 246 | TEST_P(AudioMultiVectorTest, AssertSize) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 247 | AudioMultiVector vec(num_channels_, array_length()); |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 248 | EXPECT_EQ(array_length(), vec.Size()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 249 | // Start with asserting with smaller sizes than already allocated. |
| 250 | vec.AssertSize(0); |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 251 | vec.AssertSize(array_length() - 1); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 252 | // Nothing should have changed. |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 253 | EXPECT_EQ(array_length(), vec.Size()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 254 | // Assert with one element longer than already allocated. |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 255 | vec.AssertSize(array_length() + 1); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 256 | // Expect vector to have grown. |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 257 | EXPECT_EQ(array_length() + 1, vec.Size()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 258 | // Also check the individual AudioVectors. |
| 259 | for (size_t channel = 0; channel < vec.Channels(); ++channel) { |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 260 | EXPECT_EQ(array_length() + 1u, vec[channel].Size()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | // Test the PushBack method with another AudioMultiVector as input argument. |
| 265 | TEST_P(AudioMultiVectorTest, OverwriteAt) { |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 266 | AudioMultiVector vec1(num_channels_); |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 267 | vec1.PushBackInterleaved(array_interleaved_); |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 | [diff] [blame] | 268 | AudioMultiVector vec2(num_channels_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 269 | vec2.Zeros(3); // 3 zeros in each channel. |
| 270 | // Overwrite vec2 at position 5. |
| 271 | vec1.OverwriteAt(vec2, 3, 5); |
| 272 | // Verify result. |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 273 | // Length remains the same. |
| 274 | ASSERT_EQ(array_length(), vec1.Size()); |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 275 | int16_t* ptr = array_interleaved_.data(); |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 | [diff] [blame] | 276 | for (size_t i = 0; i < array_length() - 1; ++i) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 277 | for (size_t channel = 0; channel < num_channels_; ++channel) { |
| 278 | if (i >= 5 && i <= 7) { |
| 279 | // Elements 5, 6, 7 should have been replaced with zeros. |
| 280 | EXPECT_EQ(0, vec1[channel][i]); |
| 281 | } else { |
| 282 | EXPECT_EQ(*ptr, vec1[channel][i]); |
| 283 | } |
| 284 | ++ptr; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
henrik.lundin@webrtc.org | 7825b1a | 2014-09-04 07:39:21 | [diff] [blame] | 289 | // Test the CopyChannel method, when the test is instantiated with at least two |
| 290 | // channels. |
| 291 | TEST_P(AudioMultiVectorTest, CopyChannel) { |
| 292 | if (num_channels_ < 2) |
| 293 | return; |
| 294 | |
| 295 | AudioMultiVector vec(num_channels_); |
Henrik Lundin | 00eb12a | 2018-09-05 16:14:52 | [diff] [blame] | 296 | vec.PushBackInterleaved(array_interleaved_); |
henrik.lundin@webrtc.org | 7825b1a | 2014-09-04 07:39:21 | [diff] [blame] | 297 | // Create a reference copy. |
| 298 | AudioMultiVector ref(num_channels_); |
| 299 | ref.PushBack(vec); |
| 300 | // Copy from first to last channel. |
| 301 | vec.CopyChannel(0, num_channels_ - 1); |
| 302 | // Verify that the first and last channels are identical; the others should |
| 303 | // be left untouched. |
| 304 | for (size_t i = 0; i < array_length(); ++i) { |
| 305 | // Verify that all but the last channel are untouched. |
| 306 | for (size_t channel = 0; channel < num_channels_ - 1; ++channel) { |
| 307 | EXPECT_EQ(ref[channel][i], vec[channel][i]); |
| 308 | } |
| 309 | // Verify that the last and the first channels are identical. |
| 310 | EXPECT_EQ(vec[0][i], vec[num_channels_ - 1][i]); |
| 311 | } |
| 312 | } |
| 313 | |
Jakob Ivarsson | 136ef25 | 2022-09-23 20:03:09 | [diff] [blame] | 314 | TEST_P(AudioMultiVectorTest, PushBackEmptyArray) { |
| 315 | AudioMultiVector vec(num_channels_); |
| 316 | vec.PushBackInterleaved({}); |
| 317 | EXPECT_TRUE(vec.Empty()); |
| 318 | } |
| 319 | |
Mirko Bonadei | c84f661 | 2019-01-31 11:20:57 | [diff] [blame] | 320 | INSTANTIATE_TEST_SUITE_P(TestNumChannels, |
| 321 | AudioMultiVectorTest, |
| 322 | ::testing::Values(static_cast<size_t>(1), |
| 323 | static_cast<size_t>(2), |
| 324 | static_cast<size_t>(5))); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 325 | } // namespace webrtc |