You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
6.3 KiB

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