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

Side by Side Diff: webrtc/base/buffer_unittest.cc

Issue 1717273002: Added functional variants of Buffer::SetData and Buffer::AppendData. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 10 months 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
« webrtc/base/buffer.h ('K') | « webrtc/base/buffer.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
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 buf.SetData(kTestData, 15); 182 buf.SetData(kTestData, 15);
183 EXPECT_EQ(buf.size(), 15u); 183 EXPECT_EQ(buf.size(), 15u);
184 EXPECT_EQ(buf.capacity(), 15u); 184 EXPECT_EQ(buf.capacity(), 15u);
185 const char *data = buf.data<char>(); 185 const char *data = buf.data<char>();
186 buf.Clear(); 186 buf.Clear();
187 EXPECT_EQ(buf.size(), 0u); 187 EXPECT_EQ(buf.size(), 0u);
188 EXPECT_EQ(buf.capacity(), 15u); // Hasn't shrunk. 188 EXPECT_EQ(buf.capacity(), 15u); // Hasn't shrunk.
189 EXPECT_EQ(buf.data<char>(), data); // No reallocation. 189 EXPECT_EQ(buf.data<char>(), data); // No reallocation.
190 } 190 }
191 191
192 TEST(BufferTest, TestLambdaSetAppend) {
193 auto setter = [] (rtc::ArrayView<uint8_t> av) {
194 for (int i = 0; i != 15; ++i)
195 av[i] = kTestData[i];
196 return 15;
197 };
198
199 Buffer buf1;
200 buf1.SetData(kTestData, 15);
201 buf1.AppendData(kTestData, 15);
202
203 Buffer buf2;
204 EXPECT_EQ(buf2.SetData(15, setter), 15u);
205 EXPECT_EQ(buf2.AppendData(15, setter), 15u);
206 EXPECT_EQ(buf1, buf2);
207 EXPECT_EQ(buf1.capacity(), buf2.capacity());
208 }
209
210 TEST(BufferTest, TestLambdaSetAppendSigned) {
211 auto setter = [] (rtc::ArrayView<int8_t> av) {
212 for (int i = 0; i != 15; ++i)
213 av[i] = kTestData[i];
214 return 15;
215 };
216
217 Buffer buf1;
218 buf1.SetData(kTestData, 15);
219 buf1.AppendData(kTestData, 15);
220
221 Buffer buf2;
222 EXPECT_EQ(buf2.SetData<int8_t>(15, setter), 15u);
223 EXPECT_EQ(buf2.AppendData<int8_t>(15, setter), 15u);
224 EXPECT_EQ(buf1, buf2);
225 EXPECT_EQ(buf1.capacity(), buf2.capacity());
226 }
227
228 TEST(BufferTest, TestLambdaAppendEmpty) {
229 auto setter = [] (rtc::ArrayView<uint8_t> av) {
230 for (int i = 0; i != 15; ++i)
231 av[i] = kTestData[i];
232 return 15;
233 };
234
235 Buffer buf1;
236 buf1.SetData(kTestData, 15);
237
238 Buffer buf2;
239 EXPECT_EQ(buf2.AppendData(15, setter), 15u);
240 EXPECT_EQ(buf1, buf2);
241 EXPECT_EQ(buf1.capacity(), buf2.capacity());
242 }
243
244 TEST(BufferTest, TestLambdaAppendPartial) {
245 auto setter = [] (rtc::ArrayView<uint8_t> av) {
246 for (int i = 0; i != 7; ++i)
247 av[i] = kTestData[i];
248 return 7;
249 };
250
251 Buffer buf;
252 EXPECT_EQ(buf.AppendData(15, setter), 7u);
253 EXPECT_EQ(buf.size(), 7u); // Size is exactly what we wrote.
254 EXPECT_GE(buf.capacity(), 7u); // Capacity is valid.
255 EXPECT_NE(buf.data<char>(), nullptr); // Data is actually stored.
256 }
257
kwiberg-webrtc 2016/02/22 13:27:27 Suggestion: also try a mutable lambda (that should
kwiberg-webrtc 2016/02/23 10:06:10 Did you want to do this?
ossu 2016/02/23 10:11:39 I'm not sure why I missed this comment. :/ I'll ad
192 } // namespace rtc 258 } // namespace rtc
OLDNEW
« webrtc/base/buffer.h ('K') | « webrtc/base/buffer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698