Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: webrtc/common_audio/swap_queue_unittest.cc

Issue 1398473004: Changed queue implementation to the proposed vector-based solution. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@lock_unittest_CL
Patch Set: Changed thread-based unittests to be fully deterministic Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 class ThreadSafeRandomNumberGenerator {
the sun 2015/10/26 15:16:21 Not used, remove!
peah-webrtc 2015/10/26 22:27:02 Done.
28 public:
29 ThreadSafeRandomNumberGenerator() : ThreadSafeRandomNumberGenerator(42) {}
30
31 explicit ThreadSafeRandomNumberGenerator(int seed) { srand(seed); }
32
33 int operator()() {
34 rtc::CritScope cs(&crit_);
35 return rand();
36 }
37
38 private:
39 rtc::CriticalSection crit_;
40 };
41
42 // The type for a message.
43 class SwapQueueTestMessage {
44 public:
45 SwapQueueTestMessage() {
46 id_ = -1;
47 info_ = -1;
48 }
49
50 SwapQueueTestMessage(const SwapQueueTestMessage& m)
51 : id_(m.id_), info_(m.info_) {}
52
53 SwapQueueTestMessage(int id, int info) {
54 id_ = id;
55 info_ = info;
56 }
57
58 bool Compare(const SwapQueueTestMessage& m) {
59 return ((id_ == m.id_) && (info_ == m.info_));
60 }
61
62 void swap(SwapQueueTestMessage& m) {
63 std::swap(id_, m.id_);
64 std::swap(info_, m.info_);
65 }
66
67 private:
68 int id_;
69 int info_;
70 };
71
72 // Test parameter for the basic sample based SwapQueue Tests.
73 const size_t kChunkSize = 3;
74
75 // Item invariance check function for the InvarianceVerification
76 // test.
77 bool InvarianceVerifierFunction(const std::vector<int>& v) {
78 RTC_CHECK(v.size() == kChunkSize);
79 return true;
80 }
81
82 // Queue item verifier for the FunctorVerification test.
83 class IntVerifier {
84 public:
85 explicit IntVerifier(int threshold) : threshold_(threshold) {}
86
87 bool operator()(const int& i) const { return i > threshold_; }
88
89 private:
90 int threshold_;
91 };
92
93 } // anonymous namespace
94
95 TEST(SwapQueueTest, FunctorVerification) {
96 IntVerifier verifier(-2);
97 SwapQueue<int, IntVerifier> queue(2, verifier);
98
99 int valid_value = 1;
100 int invalid_value = -4;
101 EXPECT_TRUE(queue.Insert(&valid_value));
102 EXPECT_TRUE(queue.Remove(&valid_value));
103 EXPECT_DEATH(queue.Insert(&invalid_value), "");
104 }
105
106 TEST(SwapQueueTest, SuccessfulInvarianceVerification) {
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> valid_chunk(kChunkSize, 0);
113
114 EXPECT_TRUE(queue.Insert(&valid_chunk));
115 EXPECT_EQ(valid_chunk.size(), kChunkSize);
116 EXPECT_TRUE(queue.Remove(&valid_chunk));
117 EXPECT_EQ(valid_chunk.size(), kChunkSize);
118 }
119
120 #if defined(GTEST_HAS_DEATH_TEST)
121 TEST(SwapQueueTest, UnSuccessfulInvarianceVerification1) {
122 std::vector<int> template_element(kChunkSize);
123 SwapQueue<
124 std::vector<int>,
125 SwapQueueItemVerifier<std::vector<int>, &InvarianceVerifierFunction>>
126 queue(2, template_element);
127 std::vector<int> invalid_chunk(kChunkSize - 1, 0);
128 EXPECT_DEATH(queue.Insert(&invalid_chunk), "");
129 }
130
131 TEST(SwapQueueTest, UnSuccessfulInvarianceVerification2) {
132 std::vector<int> template_element(kChunkSize);
133 SwapQueue<
134 std::vector<int>,
135 SwapQueueItemVerifier<std::vector<int>, &InvarianceVerifierFunction>>
136 queue(2, template_element);
137 std::vector<int> invalid_chunk(kChunkSize - 1, 0);
138 std::vector<int> valid_chunk(kChunkSize, 0);
139
140 EXPECT_TRUE(queue.Insert(&valid_chunk));
141 EXPECT_EQ(valid_chunk.size(), kChunkSize);
142 EXPECT_DEATH(queue.Remove(&invalid_chunk), "");
143 }
144 #endif
145
146 TEST(SwapQueueTest, MessageTest) {
147 const size_t kQueueSize = 200;
148 std::vector<SwapQueueTestMessage> messages(kQueueSize);
149 std::vector<SwapQueueTestMessage> messages_read(kQueueSize);
150 SwapQueue<SwapQueueTestMessage> queue(kQueueSize);
151
152 for (size_t k = 0; k < kQueueSize; k++) {
153 messages[k] = SwapQueueTestMessage(k % 7, k % 17);
154 }
155
156 for (size_t k = 0; k < kQueueSize; k++) {
157 SwapQueueTestMessage m(messages[k]);
158 EXPECT_TRUE(queue.Insert(&m));
159 }
160
161 for (size_t k = 0; k < kQueueSize; k++) {
162 SwapQueueTestMessage m;
163 EXPECT_TRUE(queue.Remove(&m));
164 EXPECT_TRUE(m.Compare(messages[k]));
165 }
166 }
167
168 TEST(SwapQueueTest, VectorTest) {
169 const size_t kQueueSize = 10;
170 const size_t kFrameLength = 160;
171 const size_t kDataLength = kQueueSize * kFrameLength;
172 std::vector<int16_t> buffer_reader(kFrameLength, 0);
173 std::vector<int16_t> buffer_writer(kFrameLength, 0);
174 std::vector<int16_t> template_queue_element(kFrameLength);
175 SwapQueue<std::vector<int16_t>> queue(kQueueSize, template_queue_element);
176 std::vector<int16_t> samples(kDataLength);
177
178 for (size_t k = 0; k < kDataLength; k++) {
179 samples[k] = k % 9;
180 }
181
182 for (size_t k = 0; k < kQueueSize; k++) {
183 memcpy(&buffer_writer[0], &samples[k * kFrameLength],
184 kFrameLength * sizeof(samples[0]));
185 EXPECT_TRUE(queue.Insert(&buffer_writer));
186 }
187
188 for (size_t k = 0; k < kQueueSize; k++) {
189 queue.Remove(&buffer_reader);
190
191 for (size_t j = 0; j < buffer_reader.size(); j++) {
192 EXPECT_EQ(buffer_reader[j], samples[k * kFrameLength + j]);
193 }
194 }
195 }
196
197 TEST(SwapQueueTest, FullQueue) {
198 SwapQueue<int> queue(2);
199 int i = 0;
200 EXPECT_TRUE(queue.Insert(&i));
201 EXPECT_TRUE(queue.Insert(&i));
202 EXPECT_FALSE(queue.Insert(&i));
203 EXPECT_TRUE(queue.Remove(&i));
204 EXPECT_TRUE(queue.Insert(&i));
205 EXPECT_FALSE(queue.Insert(&i));
206 }
207
208 TEST(SwapQueueTest, EmptyQueue) {
209 SwapQueue<int> queue(2);
210 int i = 0;
211 EXPECT_FALSE(queue.Remove(&i));
212 EXPECT_TRUE(queue.Insert(&i));
213 EXPECT_TRUE(queue.Remove(&i));
214 EXPECT_FALSE(queue.Remove(&i));
215 }
216
217 TEST(SwapQueueTest, InitializeTest) {
218 std::vector<int> i(kChunkSize, 0);
219 SwapQueue<std::vector<int>> queue(2, i);
220
221 EXPECT_TRUE(queue.Insert(&i));
222 EXPECT_EQ(i.size(), kChunkSize);
223 EXPECT_TRUE(queue.Insert(&i));
224 EXPECT_EQ(i.size(), kChunkSize);
225 EXPECT_TRUE(queue.Remove(&i));
226 EXPECT_EQ(i.size(), kChunkSize);
227 EXPECT_TRUE(queue.Remove(&i));
228 EXPECT_EQ(i.size(), kChunkSize);
229 }
230
231 } // namespace webrtc
OLDNEW
« webrtc/common_audio/swap_queue.h ('K') | « webrtc/common_audio/swap_queue.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698