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

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: Added unittest for the Clear function. Did lots of renaming. Extended the test for full queue to en… 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 "webrtc/common_audio/swap_queue.h"
12
13 #include <vector>
14
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace webrtc {
18
19 namespace {
20
21 // The type for a message.
22 class TestMessage {
23 public:
24 TestMessage() {
25 id_ = -1;
26 info_ = -1;
27 }
28
29 TestMessage(const TestMessage& m) : id_(m.id_), info_(m.info_) {}
kwiberg-webrtc 2015/10/29 14:40:35 This constructor uses a member initializer list. T
peah-webrtc 2015/10/29 15:24:53 Done.
30
31 TestMessage(int id, int info) {
32 id_ = id;
33 info_ = info;
34 }
35
36 bool operator==(const TestMessage& m) const {
37 return (id_ == m.id_ && info_ == m.info_);
38 }
kwiberg-webrtc 2015/10/29 14:40:35 Style: I prefer using the non-member form of opera
peah-webrtc 2015/10/29 15:24:53 Done.
39
40 void swap(TestMessage& m) {
41 std::swap(id_, m.id_);
42 std::swap(info_, m.info_);
43 }
kwiberg-webrtc 2015/10/29 14:40:36 This swap function won't be found by the standard
peah-webrtc 2015/10/29 15:24:52 Done.
44
45 private:
46 int id_;
47 int info_;
48 };
kwiberg-webrtc 2015/10/29 14:40:35 Sooo... what are id id and info for? Couldn't you
peah-webrtc 2015/10/29 15:24:52 Good point! It feels rather redundant. Done.
49
50 // Test parameter for the basic sample based SwapQueue Tests.
51 const size_t kChunkSize = 3;
52
53 // Queue item verification function for the vector test.
54 bool LengthVerifierFunction(const std::vector<int>& v) {
55 return v.size() == kChunkSize;
56 }
57
58 // Queue item verifier for the vector test.
59 class LengthVerifierFunctor {
60 public:
61 explicit LengthVerifierFunctor(size_t length) : length_(length) {}
62
63 bool operator()(const std::vector<int>& v) const {
64 return v.size() == length_;
65 }
66
67 private:
68 size_t length_;
69 };
70
71 } // anonymous namespace
72
73 TEST(SwapQueueTest, BasicOperation) {
74 std::vector<int> i(kChunkSize, 0);
75 SwapQueue<std::vector<int>> queue(2, i);
76
77 EXPECT_TRUE(queue.Insert(&i));
78 EXPECT_EQ(i.size(), kChunkSize);
79 EXPECT_TRUE(queue.Insert(&i));
80 EXPECT_EQ(i.size(), kChunkSize);
81 EXPECT_TRUE(queue.Remove(&i));
82 EXPECT_EQ(i.size(), kChunkSize);
83 EXPECT_TRUE(queue.Remove(&i));
84 EXPECT_EQ(i.size(), kChunkSize);
85 }
86
87 TEST(SwapQueueTest, FullQueue) {
88 SwapQueue<int> queue(2);
89
90 // Fill the queue.
91 int i = 0;
92 EXPECT_TRUE(queue.Insert(&i));
93 i = 1;
94 EXPECT_TRUE(queue.Insert(&i));
95
96 // Ensure that the value is not swapped when doing an Insert
97 // on a full queue.
98 i = 2;
99 EXPECT_FALSE(queue.Insert(&i));
100 EXPECT_EQ(i, 2);
kwiberg-webrtc 2015/10/29 14:40:35 You should have initialized the empty slots to som
peah-webrtc 2015/10/29 15:24:53 That I don't really follow. There is no way to ini
101
102 // Ensure that the queue is not overwritten when doing an Insert
the sun 2015/10/29 12:48:18 ?? You're Remove()ing here.
peah-webrtc 2015/10/29 13:35:56 I changed the comment to make it more clear. Done
kwiberg-webrtc 2015/10/29 14:40:35 He's testing that the Insert on line 99 didn't ove
peah-webrtc 2015/10/29 15:24:52 Done.
103 // on a full queue.
104 EXPECT_TRUE(queue.Remove(&i));
105 EXPECT_EQ(i, 0);
kwiberg-webrtc 2015/10/29 14:40:35 To make the test watertight, you should probably e
peah-webrtc 2015/10/29 15:24:52 Good point! Done.
106 }
107
108 TEST(SwapQueueTest, EmptyQueue) {
109 SwapQueue<int> queue(2);
110 int i = 0;
111 EXPECT_FALSE(queue.Remove(&i));
112 EXPECT_TRUE(queue.Insert(&i));
113 EXPECT_TRUE(queue.Remove(&i));
114 EXPECT_FALSE(queue.Remove(&i));
115 }
116
117 TEST(SwapQueueTest, Clear) {
118 SwapQueue<int> queue(2);
119 int i = 0;
120
121 // Fill the queue.
122 EXPECT_TRUE(queue.Insert(&i));
123 EXPECT_TRUE(queue.Insert(&i));
124
125 // Ensure full queue.
126 EXPECT_FALSE(queue.Insert(&i));
kwiberg-webrtc 2015/10/29 14:40:35 Yeah, it doesn't have a size() method, does it? I
peah-webrtc 2015/10/29 15:24:53 Yep, no size check is present.
127
128 // Empty the queue.
129 queue.Clear();
130
131 // Ensure that the queue is no longer full.
132 EXPECT_TRUE(queue.Insert(&i));
kwiberg-webrtc 2015/10/29 14:40:35 Or even better, ensure that it's empty by failing
peah-webrtc 2015/10/29 15:24:53 I added that as well, and kept the former. It is a
133 }
134
135 TEST(SwapQueueTest, SuccessfulItemVerifyFunction) {
136 std::vector<int> template_element(kChunkSize);
137 SwapQueue<std::vector<int>,
138 SwapQueueItemVerifier<std::vector<int>, LengthVerifierFunction>>
139 queue(2, template_element);
140 std::vector<int> valid_chunk(kChunkSize, 0);
141
142 EXPECT_TRUE(queue.Insert(&valid_chunk));
143 EXPECT_EQ(valid_chunk.size(), kChunkSize);
144 EXPECT_TRUE(queue.Remove(&valid_chunk));
145 EXPECT_EQ(valid_chunk.size(), kChunkSize);
146 }
147
148 TEST(SwapQueueTest, SuccessfulItemVerifyFunctor) {
149 std::vector<int> template_element(kChunkSize);
150 LengthVerifierFunctor verifier(kChunkSize);
151 SwapQueue<std::vector<int>, LengthVerifierFunctor> queue(2, verifier,
152 template_element);
153 std::vector<int> valid_chunk(kChunkSize, 0);
154
155 EXPECT_TRUE(queue.Insert(&valid_chunk));
156 EXPECT_EQ(valid_chunk.size(), kChunkSize);
157 EXPECT_TRUE(queue.Remove(&valid_chunk));
158 EXPECT_EQ(valid_chunk.size(), kChunkSize);
159 }
160
161 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
162 TEST(SwapQueueTest, UnsuccessfulItemVerifyFunctor) {
163 // Queue item verifier for the test.
164 class IntVerifier {
165 public:
166 explicit IntVerifier(int threshold) : threshold_(threshold) {}
167
the sun 2015/10/29 12:48:18 total nit: could lose a blank line or two here
peah-webrtc 2015/10/29 13:35:56 Done.
168 bool operator()(const int& i) const { return i > threshold_; }
169
170 private:
171 int threshold_;
172 };
173
174 IntVerifier verifier(-2);
175 SwapQueue<int, IntVerifier> queue(2, verifier);
kwiberg-webrtc 2015/10/29 14:40:35 You should be able to do it like this (untested):
peah-webrtc 2015/10/29 15:24:52 Nice! Done.
176
177 int valid_value = 1;
178 int invalid_value = -4;
179 EXPECT_TRUE(queue.Insert(&valid_value));
180 EXPECT_TRUE(queue.Remove(&valid_value));
181 bool result;
182 EXPECT_DEATH(result = queue.Insert(&invalid_value), "");
183 }
184
185 TEST(SwapQueueTest, UnSuccessfulItemVerifyInsert) {
186 std::vector<int> template_element(kChunkSize);
187 SwapQueue<std::vector<int>,
188 SwapQueueItemVerifier<std::vector<int>, &LengthVerifierFunction>>
189 queue(2, template_element);
190 std::vector<int> invalid_chunk(kChunkSize - 1, 0);
191 bool result;
192 EXPECT_DEATH(result = queue.Insert(&invalid_chunk), "");
193 }
194
195 TEST(SwapQueueTest, UnSuccessfulItemVerifyRemove) {
196 std::vector<int> template_element(kChunkSize);
197 SwapQueue<std::vector<int>,
198 SwapQueueItemVerifier<std::vector<int>, &LengthVerifierFunction>>
199 queue(2, template_element);
200 std::vector<int> invalid_chunk(kChunkSize - 1, 0);
201 std::vector<int> valid_chunk(kChunkSize, 0);
202 EXPECT_TRUE(queue.Insert(&valid_chunk));
203 EXPECT_EQ(valid_chunk.size(), kChunkSize);
204 bool result;
205 EXPECT_DEATH(result = queue.Remove(&invalid_chunk), "");
206 }
207 #endif
208
209 TEST(SwapQueueTest, MessageContentTest) {
210 const size_t kQueueSize = 200;
211 std::vector<TestMessage> messages;
212 SwapQueue<TestMessage> queue(kQueueSize);
213
214 for (size_t k = 0; k < kQueueSize; k++) {
215 messages.push_back(TestMessage(k % 7, k % 17));
kwiberg-webrtc 2015/10/29 14:40:36 LCM(7,17) = 119 < kQueueSize, so you'll have dupli
peah-webrtc 2015/10/29 15:24:52 Removed this. Done.
216 }
217
218 for (size_t k = 0; k < kQueueSize; k++) {
219 TestMessage m(messages[k]);
220 EXPECT_TRUE(queue.Insert(&m));
221 }
222
223 for (size_t k = 0; k < kQueueSize; k++) {
224 TestMessage m;
225 EXPECT_TRUE(queue.Remove(&m));
226 EXPECT_TRUE(m == messages[k]);
227 }
228 }
229
230 TEST(SwapQueueTest, VectorContentTest) {
231 const size_t kQueueSize = 10;
232 const size_t kFrameLength = 160;
233 const size_t kDataLength = kQueueSize * kFrameLength;
234 std::vector<int16_t> buffer_reader(kFrameLength, 0);
235 std::vector<int16_t> buffer_writer(kFrameLength, 0);
236 std::vector<int16_t> template_queue_element(kFrameLength);
the sun 2015/10/29 12:48:18 unused, remove
peah-webrtc 2015/10/29 15:24:53 Done.
237 SwapQueue<std::vector<int16_t>> queue(kQueueSize,
238 std::vector<int16_t>(kFrameLength));
239 std::vector<int16_t> samples(kDataLength);
240
241 for (size_t k = 0; k < kDataLength; k++) {
242 samples[k] = k % 9;
243 }
244
245 for (size_t k = 0; k < kQueueSize; k++) {
246 memcpy(&buffer_writer[0], &samples[k * kFrameLength],
247 kFrameLength * sizeof(samples[0]));
kwiberg-webrtc 2015/10/29 14:40:36 buffer_writer.clear(); buffer_writer.insert(buff
peah-webrtc 2015/10/29 15:24:52 Done.
248 EXPECT_TRUE(queue.Insert(&buffer_writer));
249 }
250
251 for (size_t k = 0; k < kQueueSize; k++) {
252 EXPECT_TRUE(queue.Remove(&buffer_reader));
253
254 for (size_t j = 0; j < buffer_reader.size(); j++) {
255 EXPECT_EQ(buffer_reader[j], samples[k * kFrameLength + j]);
256 }
257 }
258 }
259
kwiberg-webrtc 2015/10/29 14:40:36 I'd like to see tests for 0- and 1-slot queues. Th
peah-webrtc 2015/10/29 15:24:53 Done.
260 } // 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