Chromium Code Reviews| Index: webrtc/common_audio/swap_queue_unittest.cc |
| diff --git a/webrtc/common_audio/swap_queue_unittest.cc b/webrtc/common_audio/swap_queue_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2778c889c498e5a918e0da6e15a3ebd9e398cdfb |
| --- /dev/null |
| +++ b/webrtc/common_audio/swap_queue_unittest.cc |
| @@ -0,0 +1,231 @@ |
| +/* |
| + * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include <algorithm> |
| +#include <map> |
| +#include <tuple> |
| +#include <vector> |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "webrtc/common_audio/swap_queue.h" |
| +#include "webrtc/modules/interface/module_common_types.h" |
| +#include "webrtc/system_wrappers/interface/event_wrapper.h" |
| +#include "webrtc/system_wrappers/interface/sleep.h" |
| +#include "webrtc/system_wrappers/interface/thread_wrapper.h" |
| + |
| +namespace webrtc { |
| + |
| +namespace { |
| + |
| +class ThreadSafeRandomNumberGenerator { |
|
the sun
2015/10/26 15:16:21
Not used, remove!
peah-webrtc
2015/10/26 22:27:02
Done.
|
| + public: |
| + ThreadSafeRandomNumberGenerator() : ThreadSafeRandomNumberGenerator(42) {} |
| + |
| + explicit ThreadSafeRandomNumberGenerator(int seed) { srand(seed); } |
| + |
| + int operator()() { |
| + rtc::CritScope cs(&crit_); |
| + return rand(); |
| + } |
| + |
| + private: |
| + rtc::CriticalSection crit_; |
| +}; |
| + |
| +// The type for a message. |
| +class SwapQueueTestMessage { |
| + public: |
| + SwapQueueTestMessage() { |
| + id_ = -1; |
| + info_ = -1; |
| + } |
| + |
| + SwapQueueTestMessage(const SwapQueueTestMessage& m) |
| + : id_(m.id_), info_(m.info_) {} |
| + |
| + SwapQueueTestMessage(int id, int info) { |
| + id_ = id; |
| + info_ = info; |
| + } |
| + |
| + bool Compare(const SwapQueueTestMessage& m) { |
| + return ((id_ == m.id_) && (info_ == m.info_)); |
| + } |
| + |
| + void swap(SwapQueueTestMessage& m) { |
| + std::swap(id_, m.id_); |
| + std::swap(info_, m.info_); |
| + } |
| + |
| + private: |
| + int id_; |
| + int info_; |
| +}; |
| + |
| +// Test parameter for the basic sample based SwapQueue Tests. |
| +const size_t kChunkSize = 3; |
| + |
| +// Item invariance check function for the InvarianceVerification |
| +// test. |
| +bool InvarianceVerifierFunction(const std::vector<int>& v) { |
| + RTC_CHECK(v.size() == kChunkSize); |
| + return true; |
| +} |
| + |
| +// Queue item verifier for the FunctorVerification test. |
| +class IntVerifier { |
| + public: |
| + explicit IntVerifier(int threshold) : threshold_(threshold) {} |
| + |
| + bool operator()(const int& i) const { return i > threshold_; } |
| + |
| + private: |
| + int threshold_; |
| +}; |
| + |
| +} // anonymous namespace |
| + |
| +TEST(SwapQueueTest, FunctorVerification) { |
| + IntVerifier verifier(-2); |
| + SwapQueue<int, IntVerifier> queue(2, verifier); |
| + |
| + int valid_value = 1; |
| + int invalid_value = -4; |
| + EXPECT_TRUE(queue.Insert(&valid_value)); |
| + EXPECT_TRUE(queue.Remove(&valid_value)); |
| + EXPECT_DEATH(queue.Insert(&invalid_value), ""); |
| +} |
| + |
| +TEST(SwapQueueTest, SuccessfulInvarianceVerification) { |
| + std::vector<int> template_element(kChunkSize); |
| + SwapQueue< |
| + std::vector<int>, |
| + SwapQueueItemVerifier<std::vector<int>, &InvarianceVerifierFunction>> |
| + queue(2, template_element); |
| + std::vector<int> valid_chunk(kChunkSize, 0); |
| + |
| + EXPECT_TRUE(queue.Insert(&valid_chunk)); |
| + EXPECT_EQ(valid_chunk.size(), kChunkSize); |
| + EXPECT_TRUE(queue.Remove(&valid_chunk)); |
| + EXPECT_EQ(valid_chunk.size(), kChunkSize); |
| +} |
| + |
| +#if defined(GTEST_HAS_DEATH_TEST) |
| +TEST(SwapQueueTest, UnSuccessfulInvarianceVerification1) { |
| + std::vector<int> template_element(kChunkSize); |
| + SwapQueue< |
| + std::vector<int>, |
| + SwapQueueItemVerifier<std::vector<int>, &InvarianceVerifierFunction>> |
| + queue(2, template_element); |
| + std::vector<int> invalid_chunk(kChunkSize - 1, 0); |
| + EXPECT_DEATH(queue.Insert(&invalid_chunk), ""); |
| +} |
| + |
| +TEST(SwapQueueTest, UnSuccessfulInvarianceVerification2) { |
| + std::vector<int> template_element(kChunkSize); |
| + SwapQueue< |
| + std::vector<int>, |
| + SwapQueueItemVerifier<std::vector<int>, &InvarianceVerifierFunction>> |
| + queue(2, template_element); |
| + std::vector<int> invalid_chunk(kChunkSize - 1, 0); |
| + std::vector<int> valid_chunk(kChunkSize, 0); |
| + |
| + EXPECT_TRUE(queue.Insert(&valid_chunk)); |
| + EXPECT_EQ(valid_chunk.size(), kChunkSize); |
| + EXPECT_DEATH(queue.Remove(&invalid_chunk), ""); |
| +} |
| +#endif |
| + |
| +TEST(SwapQueueTest, MessageTest) { |
| + const size_t kQueueSize = 200; |
| + std::vector<SwapQueueTestMessage> messages(kQueueSize); |
| + std::vector<SwapQueueTestMessage> messages_read(kQueueSize); |
| + SwapQueue<SwapQueueTestMessage> queue(kQueueSize); |
| + |
| + for (size_t k = 0; k < kQueueSize; k++) { |
| + messages[k] = SwapQueueTestMessage(k % 7, k % 17); |
| + } |
| + |
| + for (size_t k = 0; k < kQueueSize; k++) { |
| + SwapQueueTestMessage m(messages[k]); |
| + EXPECT_TRUE(queue.Insert(&m)); |
| + } |
| + |
| + for (size_t k = 0; k < kQueueSize; k++) { |
| + SwapQueueTestMessage m; |
| + EXPECT_TRUE(queue.Remove(&m)); |
| + EXPECT_TRUE(m.Compare(messages[k])); |
| + } |
| +} |
| + |
| +TEST(SwapQueueTest, VectorTest) { |
| + const size_t kQueueSize = 10; |
| + const size_t kFrameLength = 160; |
| + const size_t kDataLength = kQueueSize * kFrameLength; |
| + std::vector<int16_t> buffer_reader(kFrameLength, 0); |
| + std::vector<int16_t> buffer_writer(kFrameLength, 0); |
| + std::vector<int16_t> template_queue_element(kFrameLength); |
| + SwapQueue<std::vector<int16_t>> queue(kQueueSize, template_queue_element); |
| + std::vector<int16_t> samples(kDataLength); |
| + |
| + for (size_t k = 0; k < kDataLength; k++) { |
| + samples[k] = k % 9; |
| + } |
| + |
| + for (size_t k = 0; k < kQueueSize; k++) { |
| + memcpy(&buffer_writer[0], &samples[k * kFrameLength], |
| + kFrameLength * sizeof(samples[0])); |
| + EXPECT_TRUE(queue.Insert(&buffer_writer)); |
| + } |
| + |
| + for (size_t k = 0; k < kQueueSize; k++) { |
| + queue.Remove(&buffer_reader); |
| + |
| + for (size_t j = 0; j < buffer_reader.size(); j++) { |
| + EXPECT_EQ(buffer_reader[j], samples[k * kFrameLength + j]); |
| + } |
| + } |
| +} |
| + |
| +TEST(SwapQueueTest, FullQueue) { |
| + SwapQueue<int> queue(2); |
| + int i = 0; |
| + EXPECT_TRUE(queue.Insert(&i)); |
| + EXPECT_TRUE(queue.Insert(&i)); |
| + EXPECT_FALSE(queue.Insert(&i)); |
| + EXPECT_TRUE(queue.Remove(&i)); |
| + EXPECT_TRUE(queue.Insert(&i)); |
| + EXPECT_FALSE(queue.Insert(&i)); |
| +} |
| + |
| +TEST(SwapQueueTest, EmptyQueue) { |
| + SwapQueue<int> queue(2); |
| + int i = 0; |
| + EXPECT_FALSE(queue.Remove(&i)); |
| + EXPECT_TRUE(queue.Insert(&i)); |
| + EXPECT_TRUE(queue.Remove(&i)); |
| + EXPECT_FALSE(queue.Remove(&i)); |
| +} |
| + |
| +TEST(SwapQueueTest, InitializeTest) { |
| + std::vector<int> i(kChunkSize, 0); |
| + SwapQueue<std::vector<int>> queue(2, i); |
| + |
| + EXPECT_TRUE(queue.Insert(&i)); |
| + EXPECT_EQ(i.size(), kChunkSize); |
| + EXPECT_TRUE(queue.Insert(&i)); |
| + EXPECT_EQ(i.size(), kChunkSize); |
| + EXPECT_TRUE(queue.Remove(&i)); |
| + EXPECT_EQ(i.size(), kChunkSize); |
| + EXPECT_TRUE(queue.Remove(&i)); |
| + EXPECT_EQ(i.size(), kChunkSize); |
| +} |
| + |
| +} // namespace webrtc |