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