peah | 48407f7 | 2015-11-09 13:24:50 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 "rtc_base/swap_queue.h" |
peah | 48407f7 | 2015-11-09 13:24:50 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <cstdint> |
peah | 48407f7 | 2015-11-09 13:24:50 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "test/gtest.h" |
peah | 48407f7 | 2015-11-09 13:24:50 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | // Test parameter for the basic sample based SwapQueue Tests. |
| 23 | const size_t kChunkSize = 3; |
| 24 | |
| 25 | // Queue item verification function for the vector test. |
| 26 | bool LengthVerifierFunction(const std::vector<int>& v) { |
| 27 | return v.size() == kChunkSize; |
| 28 | } |
| 29 | |
| 30 | // Queue item verifier for the vector test. |
| 31 | class LengthVerifierFunctor { |
| 32 | public: |
| 33 | explicit LengthVerifierFunctor(size_t length) : length_(length) {} |
| 34 | |
| 35 | bool operator()(const std::vector<int>& v) const { |
| 36 | return v.size() == length_; |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | size_t length_; |
| 41 | }; |
| 42 | |
| 43 | } // anonymous namespace |
| 44 | |
| 45 | TEST(SwapQueueTest, BasicOperation) { |
| 46 | std::vector<int> i(kChunkSize, 0); |
| 47 | SwapQueue<std::vector<int>> queue(2, i); |
| 48 | |
| 49 | EXPECT_TRUE(queue.Insert(&i)); |
| 50 | EXPECT_EQ(i.size(), kChunkSize); |
| 51 | EXPECT_TRUE(queue.Insert(&i)); |
| 52 | EXPECT_EQ(i.size(), kChunkSize); |
| 53 | EXPECT_TRUE(queue.Remove(&i)); |
| 54 | EXPECT_EQ(i.size(), kChunkSize); |
| 55 | EXPECT_TRUE(queue.Remove(&i)); |
| 56 | EXPECT_EQ(i.size(), kChunkSize); |
| 57 | } |
| 58 | |
| 59 | TEST(SwapQueueTest, FullQueue) { |
| 60 | SwapQueue<int> queue(2); |
| 61 | |
| 62 | // Fill the queue. |
| 63 | int i = 0; |
| 64 | EXPECT_TRUE(queue.Insert(&i)); |
| 65 | i = 1; |
| 66 | EXPECT_TRUE(queue.Insert(&i)); |
| 67 | |
| 68 | // Ensure that the value is not swapped when doing an Insert |
| 69 | // on a full queue. |
| 70 | i = 2; |
| 71 | EXPECT_FALSE(queue.Insert(&i)); |
| 72 | EXPECT_EQ(i, 2); |
| 73 | |
| 74 | // Ensure that the Insert didn't overwrite anything in the queue. |
| 75 | EXPECT_TRUE(queue.Remove(&i)); |
| 76 | EXPECT_EQ(i, 0); |
| 77 | EXPECT_TRUE(queue.Remove(&i)); |
| 78 | EXPECT_EQ(i, 1); |
| 79 | } |
| 80 | |
| 81 | TEST(SwapQueueTest, EmptyQueue) { |
| 82 | SwapQueue<int> queue(2); |
| 83 | int i = 0; |
| 84 | EXPECT_FALSE(queue.Remove(&i)); |
| 85 | EXPECT_TRUE(queue.Insert(&i)); |
| 86 | EXPECT_TRUE(queue.Remove(&i)); |
| 87 | EXPECT_FALSE(queue.Remove(&i)); |
| 88 | } |
| 89 | |
| 90 | TEST(SwapQueueTest, Clear) { |
| 91 | SwapQueue<int> queue(2); |
| 92 | int i = 0; |
| 93 | |
| 94 | // Fill the queue. |
| 95 | EXPECT_TRUE(queue.Insert(&i)); |
| 96 | EXPECT_TRUE(queue.Insert(&i)); |
| 97 | |
| 98 | // Ensure full queue. |
| 99 | EXPECT_FALSE(queue.Insert(&i)); |
| 100 | |
| 101 | // Empty the queue. |
| 102 | queue.Clear(); |
| 103 | |
| 104 | // Ensure that the queue is empty |
| 105 | EXPECT_FALSE(queue.Remove(&i)); |
| 106 | |
| 107 | // Ensure that the queue is no longer full. |
| 108 | EXPECT_TRUE(queue.Insert(&i)); |
| 109 | } |
| 110 | |
| 111 | TEST(SwapQueueTest, SuccessfulItemVerifyFunction) { |
| 112 | std::vector<int> template_element(kChunkSize); |
| 113 | SwapQueue<std::vector<int>, |
| 114 | SwapQueueItemVerifier<std::vector<int>, LengthVerifierFunction>> |
| 115 | queue(2, template_element); |
| 116 | std::vector<int> valid_chunk(kChunkSize, 0); |
| 117 | |
| 118 | EXPECT_TRUE(queue.Insert(&valid_chunk)); |
| 119 | EXPECT_EQ(valid_chunk.size(), kChunkSize); |
| 120 | EXPECT_TRUE(queue.Remove(&valid_chunk)); |
| 121 | EXPECT_EQ(valid_chunk.size(), kChunkSize); |
| 122 | } |
| 123 | |
| 124 | TEST(SwapQueueTest, SuccessfulItemVerifyFunctor) { |
| 125 | std::vector<int> template_element(kChunkSize); |
| 126 | LengthVerifierFunctor verifier(kChunkSize); |
| 127 | SwapQueue<std::vector<int>, LengthVerifierFunctor> queue(2, template_element, |
| 128 | verifier); |
| 129 | std::vector<int> valid_chunk(kChunkSize, 0); |
| 130 | |
| 131 | EXPECT_TRUE(queue.Insert(&valid_chunk)); |
| 132 | EXPECT_EQ(valid_chunk.size(), kChunkSize); |
| 133 | EXPECT_TRUE(queue.Remove(&valid_chunk)); |
| 134 | EXPECT_EQ(valid_chunk.size(), kChunkSize); |
| 135 | } |
| 136 | |
| 137 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
Tommi | a5e07cc | 2020-05-26 19:40:37 | [diff] [blame] | 138 | TEST(SwapQueueDeathTest, UnsuccessfulItemVerifyFunctor) { |
peah | 48407f7 | 2015-11-09 13:24:50 | [diff] [blame] | 139 | // Queue item verifier for the test. |
| 140 | auto minus_2_verifier = [](const int& i) { return i > -2; }; |
| 141 | SwapQueue<int, decltype(minus_2_verifier)> queue(2, minus_2_verifier); |
| 142 | |
| 143 | int valid_value = 1; |
| 144 | int invalid_value = -4; |
| 145 | EXPECT_TRUE(queue.Insert(&valid_value)); |
| 146 | EXPECT_TRUE(queue.Remove(&valid_value)); |
Mirko Bonadei | e5e78c4 | 2021-10-28 12:08:19 | [diff] [blame] | 147 | EXPECT_DEATH((void)queue.Insert(&invalid_value), ""); |
peah | 48407f7 | 2015-11-09 13:24:50 | [diff] [blame] | 148 | } |
| 149 | |
Tommi | a5e07cc | 2020-05-26 19:40:37 | [diff] [blame] | 150 | TEST(SwapQueueDeathTest, UnSuccessfulItemVerifyInsert) { |
peah | 48407f7 | 2015-11-09 13:24:50 | [diff] [blame] | 151 | std::vector<int> template_element(kChunkSize); |
| 152 | SwapQueue<std::vector<int>, |
| 153 | SwapQueueItemVerifier<std::vector<int>, &LengthVerifierFunction>> |
| 154 | queue(2, template_element); |
| 155 | std::vector<int> invalid_chunk(kChunkSize - 1, 0); |
Mirko Bonadei | e5e78c4 | 2021-10-28 12:08:19 | [diff] [blame] | 156 | EXPECT_DEATH((void)queue.Insert(&invalid_chunk), ""); |
peah | 48407f7 | 2015-11-09 13:24:50 | [diff] [blame] | 157 | } |
| 158 | |
Tommi | a5e07cc | 2020-05-26 19:40:37 | [diff] [blame] | 159 | TEST(SwapQueueDeathTest, UnSuccessfulItemVerifyRemove) { |
peah | 48407f7 | 2015-11-09 13:24:50 | [diff] [blame] | 160 | std::vector<int> template_element(kChunkSize); |
| 161 | SwapQueue<std::vector<int>, |
| 162 | SwapQueueItemVerifier<std::vector<int>, &LengthVerifierFunction>> |
| 163 | queue(2, template_element); |
| 164 | std::vector<int> invalid_chunk(kChunkSize - 1, 0); |
| 165 | std::vector<int> valid_chunk(kChunkSize, 0); |
| 166 | EXPECT_TRUE(queue.Insert(&valid_chunk)); |
| 167 | EXPECT_EQ(valid_chunk.size(), kChunkSize); |
Mirko Bonadei | e5e78c4 | 2021-10-28 12:08:19 | [diff] [blame] | 168 | EXPECT_DEATH((void)queue.Remove(&invalid_chunk), ""); |
peah | 48407f7 | 2015-11-09 13:24:50 | [diff] [blame] | 169 | } |
| 170 | #endif |
| 171 | |
| 172 | TEST(SwapQueueTest, VectorContentTest) { |
| 173 | const size_t kQueueSize = 10; |
| 174 | const size_t kFrameLength = 160; |
| 175 | const size_t kDataLength = kQueueSize * kFrameLength; |
| 176 | std::vector<int16_t> buffer_reader(kFrameLength, 0); |
| 177 | std::vector<int16_t> buffer_writer(kFrameLength, 0); |
| 178 | SwapQueue<std::vector<int16_t>> queue(kQueueSize, |
| 179 | std::vector<int16_t>(kFrameLength)); |
| 180 | std::vector<int16_t> samples(kDataLength); |
| 181 | |
| 182 | for (size_t k = 0; k < kDataLength; k++) { |
| 183 | samples[k] = k % 9; |
| 184 | } |
| 185 | |
| 186 | for (size_t k = 0; k < kQueueSize; k++) { |
| 187 | buffer_writer.clear(); |
| 188 | buffer_writer.insert(buffer_writer.end(), &samples[0] + k * kFrameLength, |
| 189 | &samples[0] + (k + 1) * kFrameLength); |
| 190 | |
| 191 | EXPECT_TRUE(queue.Insert(&buffer_writer)); |
| 192 | } |
| 193 | |
| 194 | for (size_t k = 0; k < kQueueSize; k++) { |
| 195 | EXPECT_TRUE(queue.Remove(&buffer_reader)); |
| 196 | |
| 197 | for (size_t j = 0; j < buffer_reader.size(); j++) { |
| 198 | EXPECT_EQ(buffer_reader[j], samples[k * kFrameLength + j]); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | TEST(SwapQueueTest, ZeroSlotQueue) { |
| 204 | SwapQueue<int> queue(0); |
| 205 | int i = 42; |
| 206 | EXPECT_FALSE(queue.Insert(&i)); |
| 207 | EXPECT_FALSE(queue.Remove(&i)); |
| 208 | EXPECT_EQ(i, 42); |
| 209 | } |
| 210 | |
| 211 | TEST(SwapQueueTest, OneSlotQueue) { |
| 212 | SwapQueue<int> queue(1); |
| 213 | int i = 42; |
| 214 | EXPECT_TRUE(queue.Insert(&i)); |
| 215 | i = 43; |
| 216 | EXPECT_FALSE(queue.Insert(&i)); |
| 217 | EXPECT_EQ(i, 43); |
| 218 | EXPECT_TRUE(queue.Remove(&i)); |
| 219 | EXPECT_EQ(i, 42); |
| 220 | EXPECT_FALSE(queue.Remove(&i)); |
| 221 | } |
| 222 | |
| 223 | } // namespace webrtc |