Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 11 #include <algorithm> | |
| 12 #include <map> | |
| 13 #include <tuple> | |
| 14 #include <vector> | |
| 15 | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 #include "webrtc/common_audio/swap_queue.h" | |
| 18 #include "webrtc/modules/interface/module_common_types.h" | |
| 19 #include "webrtc/system_wrappers/interface/event_wrapper.h" | |
| 20 #include "webrtc/system_wrappers/interface/sleep.h" | |
| 21 #include "webrtc/system_wrappers/interface/thread_wrapper.h" | |
| 22 | |
| 23 namespace webrtc { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // The type for a message. | |
| 28 class SwapQueueTestMessage { | |
| 29 public: | |
| 30 SwapQueueTestMessage() { | |
| 31 id_ = -1; | |
| 32 info_ = -1; | |
| 33 } | |
| 34 | |
| 35 SwapQueueTestMessage(const SwapQueueTestMessage& m) | |
| 36 : id_(m.id_), info_(m.info_) {} | |
| 37 | |
| 38 SwapQueueTestMessage(int id, int info) { | |
| 39 id_ = id; | |
| 40 info_ = info; | |
| 41 } | |
| 42 | |
| 43 bool Compare(const SwapQueueTestMessage& m) { | |
| 44 return ((id_ == m.id_) && (info_ == m.info_)); | |
|
kwiberg-webrtc
2015/10/27 12:05:13
Cut down on the number of parentheses?
peah-webrtc
2015/10/29 06:15:15
Done.
| |
| 45 } | |
| 46 | |
| 47 void swap(SwapQueueTestMessage& m) { | |
| 48 std::swap(id_, m.id_); | |
| 49 std::swap(info_, m.info_); | |
| 50 } | |
| 51 | |
| 52 private: | |
| 53 int id_; | |
| 54 int info_; | |
| 55 }; | |
| 56 | |
| 57 // Test parameter for the basic sample based SwapQueue Tests. | |
| 58 const size_t kChunkSize = 3; | |
| 59 | |
| 60 // Item invariance check function for the InvarianceVerification | |
| 61 // test. | |
| 62 bool InvarianceVerifierFunction(const std::vector<int>& v) { | |
|
the sun
2015/10/27 16:18:48
You call this ItemVerifier elsewhere. Can we stick
peah-webrtc
2015/10/29 06:15:14
Done.
| |
| 63 RTC_CHECK(v.size() == kChunkSize); | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 // Queue item verifier for the FunctorVerification test. | |
| 68 class IntVerifier { | |
| 69 public: | |
| 70 explicit IntVerifier(int threshold) : threshold_(threshold) {} | |
| 71 | |
| 72 bool operator()(const int& i) const { return i > threshold_; } | |
| 73 | |
| 74 private: | |
| 75 int threshold_; | |
| 76 }; | |
| 77 | |
| 78 } // anonymous namespace | |
| 79 | |
|
the sun
2015/10/27 16:18:48
AFAICT you don't test instantiating the queue usin
peah-webrtc
2015/10/29 06:15:15
Good point! And found an error that indeed did :-)
| |
| 80 TEST(SwapQueueTest, FunctorVerification) { | |
| 81 IntVerifier verifier(-2); | |
| 82 SwapQueue<int, IntVerifier> queue(2, verifier); | |
| 83 | |
| 84 int valid_value = 1; | |
| 85 int invalid_value = -4; | |
| 86 EXPECT_TRUE(queue.Insert(&valid_value)); | |
| 87 EXPECT_TRUE(queue.Remove(&valid_value)); | |
| 88 EXPECT_DEATH(queue.Insert(&invalid_value), ""); | |
|
kwiberg-webrtc
2015/10/27 12:05:13
Protect this with #ifdefs, since some of our platf
peah-webrtc
2015/10/29 06:15:15
Done.
| |
| 89 } | |
| 90 | |
| 91 TEST(SwapQueueTest, SuccessfulInvarianceVerification) { | |
| 92 std::vector<int> template_element(kChunkSize); | |
| 93 SwapQueue< | |
| 94 std::vector<int>, | |
| 95 SwapQueueItemVerifier<std::vector<int>, &InvarianceVerifierFunction>> | |
| 96 queue(2, template_element); | |
| 97 std::vector<int> valid_chunk(kChunkSize, 0); | |
| 98 | |
| 99 EXPECT_TRUE(queue.Insert(&valid_chunk)); | |
| 100 EXPECT_EQ(valid_chunk.size(), kChunkSize); | |
| 101 EXPECT_TRUE(queue.Remove(&valid_chunk)); | |
| 102 EXPECT_EQ(valid_chunk.size(), kChunkSize); | |
| 103 } | |
| 104 | |
| 105 #if defined(GTEST_HAS_DEATH_TEST) | |
| 106 TEST(SwapQueueTest, UnSuccessfulInvarianceVerification1) { | |
| 107 std::vector<int> template_element(kChunkSize); | |
| 108 SwapQueue< | |
| 109 std::vector<int>, | |
| 110 SwapQueueItemVerifier<std::vector<int>, &InvarianceVerifierFunction>> | |
| 111 queue(2, template_element); | |
| 112 std::vector<int> invalid_chunk(kChunkSize - 1, 0); | |
| 113 EXPECT_DEATH(queue.Insert(&invalid_chunk), ""); | |
| 114 } | |
| 115 | |
| 116 TEST(SwapQueueTest, UnSuccessfulInvarianceVerification2) { | |
| 117 std::vector<int> template_element(kChunkSize); | |
| 118 SwapQueue< | |
| 119 std::vector<int>, | |
| 120 SwapQueueItemVerifier<std::vector<int>, &InvarianceVerifierFunction>> | |
| 121 queue(2, template_element); | |
| 122 std::vector<int> invalid_chunk(kChunkSize - 1, 0); | |
| 123 std::vector<int> valid_chunk(kChunkSize, 0); | |
| 124 | |
| 125 EXPECT_TRUE(queue.Insert(&valid_chunk)); | |
| 126 EXPECT_EQ(valid_chunk.size(), kChunkSize); | |
| 127 EXPECT_DEATH(queue.Remove(&invalid_chunk), ""); | |
| 128 } | |
| 129 #endif | |
| 130 | |
| 131 TEST(SwapQueueTest, MessageTest) { | |
| 132 const size_t kQueueSize = 200; | |
| 133 std::vector<SwapQueueTestMessage> messages(kQueueSize); | |
| 134 std::vector<SwapQueueTestMessage> messages_read(kQueueSize); | |
| 135 SwapQueue<SwapQueueTestMessage> queue(kQueueSize); | |
| 136 | |
| 137 for (size_t k = 0; k < kQueueSize; k++) { | |
| 138 messages[k] = SwapQueueTestMessage(k % 7, k % 17); | |
| 139 } | |
|
kwiberg-webrtc
2015/10/27 12:05:13
Initialize the vector empty, and use push_back to
peah-webrtc
2015/10/29 06:15:15
Done.
| |
| 140 | |
| 141 for (size_t k = 0; k < kQueueSize; k++) { | |
| 142 SwapQueueTestMessage m(messages[k]); | |
| 143 EXPECT_TRUE(queue.Insert(&m)); | |
| 144 } | |
| 145 | |
| 146 for (size_t k = 0; k < kQueueSize; k++) { | |
| 147 SwapQueueTestMessage m; | |
| 148 EXPECT_TRUE(queue.Remove(&m)); | |
| 149 EXPECT_TRUE(m.Compare(messages[k])); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 TEST(SwapQueueTest, VectorTest) { | |
| 154 const size_t kQueueSize = 10; | |
| 155 const size_t kFrameLength = 160; | |
| 156 const size_t kDataLength = kQueueSize * kFrameLength; | |
| 157 std::vector<int16_t> buffer_reader(kFrameLength, 0); | |
| 158 std::vector<int16_t> buffer_writer(kFrameLength, 0); | |
| 159 std::vector<int16_t> template_queue_element(kFrameLength); | |
| 160 SwapQueue<std::vector<int16_t>> queue(kQueueSize, template_queue_element); | |
| 161 std::vector<int16_t> samples(kDataLength); | |
| 162 | |
| 163 for (size_t k = 0; k < kDataLength; k++) { | |
| 164 samples[k] = k % 9; | |
| 165 } | |
| 166 | |
| 167 for (size_t k = 0; k < kQueueSize; k++) { | |
| 168 memcpy(&buffer_writer[0], &samples[k * kFrameLength], | |
| 169 kFrameLength * sizeof(samples[0])); | |
| 170 EXPECT_TRUE(queue.Insert(&buffer_writer)); | |
| 171 } | |
| 172 | |
| 173 for (size_t k = 0; k < kQueueSize; k++) { | |
| 174 queue.Remove(&buffer_reader); | |
| 175 | |
| 176 for (size_t j = 0; j < buffer_reader.size(); j++) { | |
| 177 EXPECT_EQ(buffer_reader[j], samples[k * kFrameLength + j]); | |
| 178 } | |
| 179 } | |
| 180 } | |
| 181 | |
| 182 TEST(SwapQueueTest, FullQueue) { | |
| 183 SwapQueue<int> queue(2); | |
| 184 int i = 0; | |
| 185 EXPECT_TRUE(queue.Insert(&i)); | |
| 186 EXPECT_TRUE(queue.Insert(&i)); | |
| 187 EXPECT_FALSE(queue.Insert(&i)); | |
| 188 EXPECT_TRUE(queue.Remove(&i)); | |
| 189 EXPECT_TRUE(queue.Insert(&i)); | |
| 190 EXPECT_FALSE(queue.Insert(&i)); | |
| 191 } | |
| 192 | |
| 193 TEST(SwapQueueTest, EmptyQueue) { | |
| 194 SwapQueue<int> queue(2); | |
| 195 int i = 0; | |
| 196 EXPECT_FALSE(queue.Remove(&i)); | |
| 197 EXPECT_TRUE(queue.Insert(&i)); | |
| 198 EXPECT_TRUE(queue.Remove(&i)); | |
| 199 EXPECT_FALSE(queue.Remove(&i)); | |
| 200 } | |
| 201 | |
| 202 TEST(SwapQueueTest, InitializeTest) { | |
| 203 std::vector<int> i(kChunkSize, 0); | |
| 204 SwapQueue<std::vector<int>> queue(2, i); | |
| 205 | |
| 206 EXPECT_TRUE(queue.Insert(&i)); | |
| 207 EXPECT_EQ(i.size(), kChunkSize); | |
| 208 EXPECT_TRUE(queue.Insert(&i)); | |
| 209 EXPECT_EQ(i.size(), kChunkSize); | |
| 210 EXPECT_TRUE(queue.Remove(&i)); | |
| 211 EXPECT_EQ(i.size(), kChunkSize); | |
| 212 EXPECT_TRUE(queue.Remove(&i)); | |
| 213 EXPECT_EQ(i.size(), kChunkSize); | |
| 214 } | |
| 215 | |
| 216 } // namespace webrtc | |
| OLD | NEW |