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

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: Changes to code comments, unittest changes, annotation changes, various minor changes 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
« no previous file with comments | « webrtc/common_audio/swap_queue.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
the sun 2015/10/29 09:33:04 Include swap_queue.h first, since we don't have an
peah-webrtc 2015/10/29 12:32:43 Done.
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"
the sun 2015/10/29 09:33:04 Clean away unused includes, please.
peah-webrtc 2015/10/29 12:32:42 Done.
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 {
the sun 2015/10/29 09:33:04 nit: just TestMessage using the SwapQueue prefix m
peah-webrtc 2015/10/29 12:32:43 Good point! Renamed the class. Done.
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) {
the sun 2015/10/29 09:33:04 Total nit: This is C++, we can do "bool operator==
peah-webrtc 2015/10/29 12:32:42 Good point! Done.
44 return (id_ == m.id_ && info_ == m.info_);
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 // Queue item verification function for the vector test.
61 bool LengthVerifierFunction(const std::vector<int>& v) {
62 RTC_CHECK(v.size() == kChunkSize);
the sun 2015/10/29 09:33:04 Shouldn't use RTC_CHECK here. The verifier should
peah-webrtc 2015/10/29 12:32:42 Done.
63 return true;
64 }
65
66 // Queue item verifier for the vector test.
67 class LengthVerifierFunctor {
68 public:
69 explicit LengthVerifierFunctor(size_t length) : length_(length) {}
70
71 bool operator()(const std::vector<int>& v) const {
72 return v.size() == length_;
73 }
74
75 private:
76 size_t length_;
77 };
78
79 // Queue item verifier for the FunctorVerification test.
80 class IntVerifier {
81 public:
82 explicit IntVerifier(int threshold) : threshold_(threshold) {}
83
84 bool operator()(const int& i) const { return i > threshold_; }
85
86 private:
87 int threshold_;
88 };
89
90 } // anonymous namespace
91
92 TEST(SwapQueueTest, SuccessfulInvarianceVerification1) {
the sun 2015/10/29 09:33:04 Avoid numbering the tests when you could let the n
the sun 2015/10/29 09:33:04 nit: could you please rearrange the tests to start
peah-webrtc 2015/10/29 12:32:42 Done.
peah-webrtc 2015/10/29 12:32:43 Done.
93 std::vector<int> template_element(kChunkSize);
94 SwapQueue<std::vector<int>,
95 SwapQueueItemVerifier<std::vector<int>, LengthVerifierFunction>>
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 TEST(SwapQueueTest, SuccessfulInvarianceVerification2) {
the sun 2015/10/29 09:33:04 nit: for consistency, "SuccessfulItemVerifyFunctor
peah-webrtc 2015/10/29 12:32:42 Done.
106 std::vector<int> template_element(kChunkSize);
107 LengthVerifierFunctor verifier(kChunkSize);
108 SwapQueue<std::vector<int>, LengthVerifierFunctor> queue(2, verifier,
109 template_element);
110 std::vector<int> valid_chunk(kChunkSize, 0);
111
112 EXPECT_TRUE(queue.Insert(&valid_chunk));
113 EXPECT_EQ(valid_chunk.size(), kChunkSize);
114 EXPECT_TRUE(queue.Remove(&valid_chunk));
115 EXPECT_EQ(valid_chunk.size(), kChunkSize);
116 }
117
118 #if defined(GTEST_HAS_DEATH_TEST)
the sun 2015/10/29 09:33:05 Karl use a different condition to enable death tes
peah-webrtc 2015/10/29 12:32:42 Done.
119 TEST(SwapQueueTest, FunctorVerification) {
the sun 2015/10/29 09:33:04 UnsuccessfulItemVerifyFunctor
peah-webrtc 2015/10/29 12:32:42 Done.
120 IntVerifier verifier(-2);
the sun 2015/10/29 09:33:04 IntVerifier is only used in this test - can you mo
peah-webrtc 2015/10/29 12:32:42 Done.
121 SwapQueue<int, IntVerifier> queue(2, verifier);
122
123 int valid_value = 1;
124 int invalid_value = -4;
125 EXPECT_TRUE(queue.Insert(&valid_value));
126 EXPECT_TRUE(queue.Remove(&valid_value));
127 bool result;
128 EXPECT_DEATH(result = queue.Insert(&invalid_value), "");
129 }
130
131 TEST(SwapQueueTest, UnSuccessfulInvarianceVerification1) {
the sun 2015/10/29 09:33:04 UnsuccessfulItemVerifyInsert
peah-webrtc 2015/10/29 12:32:43 Done.
132 std::vector<int> template_element(kChunkSize);
133 SwapQueue<std::vector<int>,
134 SwapQueueItemVerifier<std::vector<int>, &LengthVerifierFunction>>
135 queue(2, template_element);
136 std::vector<int> invalid_chunk(kChunkSize - 1, 0);
137 bool result;
138 EXPECT_DEATH(result = queue.Insert(&invalid_chunk), "");
139 }
140
141 TEST(SwapQueueTest, UnSuccessfulInvarianceVerification2) {
the sun 2015/10/29 09:33:04 UnsuccessfulItemVerifyRemove
peah-webrtc 2015/10/29 12:32:42 Done.
142 std::vector<int> template_element(kChunkSize);
143 SwapQueue<std::vector<int>,
144 SwapQueueItemVerifier<std::vector<int>, &LengthVerifierFunction>>
145 queue(2, template_element);
146 std::vector<int> invalid_chunk(kChunkSize - 1, 0);
147 std::vector<int> valid_chunk(kChunkSize, 0);
148 EXPECT_TRUE(queue.Insert(&valid_chunk));
149 EXPECT_EQ(valid_chunk.size(), kChunkSize);
150 bool result;
151 EXPECT_DEATH(result = queue.Remove(&invalid_chunk), "");
152 }
153 #endif
154
155 TEST(SwapQueueTest, MessageTest) {
156 const size_t kQueueSize = 200;
157 std::vector<SwapQueueTestMessage> messages;
158 SwapQueue<SwapQueueTestMessage> queue(kQueueSize);
159
160 for (size_t k = 0; k < kQueueSize; k++) {
161 messages.push_back(SwapQueueTestMessage(k % 7, k % 17));
162 }
163
164 for (size_t k = 0; k < kQueueSize; k++) {
165 SwapQueueTestMessage m(messages[k]);
166 EXPECT_TRUE(queue.Insert(&m));
167 }
168
169 for (size_t k = 0; k < kQueueSize; k++) {
170 SwapQueueTestMessage m;
171 EXPECT_TRUE(queue.Remove(&m));
172 EXPECT_TRUE(m.Compare(messages[k]));
173 }
174 }
175
176 TEST(SwapQueueTest, VectorTest) {
177 const size_t kQueueSize = 10;
178 const size_t kFrameLength = 160;
179 const size_t kDataLength = kQueueSize * kFrameLength;
180 std::vector<int16_t> buffer_reader(kFrameLength, 0);
181 std::vector<int16_t> buffer_writer(kFrameLength, 0);
182 std::vector<int16_t> template_queue_element(kFrameLength);
the sun 2015/10/29 09:33:04 Just make this inline, in the queue instantiation:
peah-webrtc 2015/10/29 12:32:42 Done.
183 SwapQueue<std::vector<int16_t>> queue(kQueueSize, template_queue_element);
184 std::vector<int16_t> samples(kDataLength);
185
186 for (size_t k = 0; k < kDataLength; k++) {
187 samples[k] = k % 9;
188 }
189
190 for (size_t k = 0; k < kQueueSize; k++) {
191 memcpy(&buffer_writer[0], &samples[k * kFrameLength],
192 kFrameLength * sizeof(samples[0]));
193 EXPECT_TRUE(queue.Insert(&buffer_writer));
194 }
195
196 for (size_t k = 0; k < kQueueSize; k++) {
197 EXPECT_TRUE(queue.Remove(&buffer_reader));
198
199 for (size_t j = 0; j < buffer_reader.size(); j++) {
200 EXPECT_EQ(buffer_reader[j], samples[k * kFrameLength + j]);
201 }
202 }
203 }
204
205 TEST(SwapQueueTest, FullQueue) {
the sun 2015/10/29 09:33:05 It would be a good test to verify the Remove()d va
peah-webrtc 2015/10/29 12:32:42 Done.
206 SwapQueue<int> queue(2);
207 int i = 0;
208 EXPECT_TRUE(queue.Insert(&i));
209 EXPECT_TRUE(queue.Insert(&i));
210 EXPECT_FALSE(queue.Insert(&i));
211 EXPECT_TRUE(queue.Remove(&i));
212 EXPECT_TRUE(queue.Insert(&i));
213 EXPECT_FALSE(queue.Insert(&i));
214 }
215
216 TEST(SwapQueueTest, EmptyQueue) {
217 SwapQueue<int> queue(2);
218 int i = 0;
219 EXPECT_FALSE(queue.Remove(&i));
220 EXPECT_TRUE(queue.Insert(&i));
221 EXPECT_TRUE(queue.Remove(&i));
222 EXPECT_FALSE(queue.Remove(&i));
223 }
224
225 TEST(SwapQueueTest, InitializeTest) {
226 std::vector<int> i(kChunkSize, 0);
227 SwapQueue<std::vector<int>> queue(2, i);
228
229 EXPECT_TRUE(queue.Insert(&i));
230 EXPECT_EQ(i.size(), kChunkSize);
231 EXPECT_TRUE(queue.Insert(&i));
232 EXPECT_EQ(i.size(), kChunkSize);
233 EXPECT_TRUE(queue.Remove(&i));
234 EXPECT_EQ(i.size(), kChunkSize);
235 EXPECT_TRUE(queue.Remove(&i));
236 EXPECT_EQ(i.size(), kChunkSize);
237 }
238
the sun 2015/10/29 09:33:04 I didn't see a test for Clear().
peah-webrtc 2015/10/29 12:32:42 Added another test case for that. Done.
239 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/common_audio/swap_queue.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698