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

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

Issue 1823503002: Reland Use CopyOnWriteBuffer instead of Buffer to avoid unnecessary copies. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 9 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
OLDNEW
1 /* 1 /*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2016 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 23 matching lines...) Expand all
34 } 34 }
35 35
36 void EnsureBuffersDontShareData(const CopyOnWriteBuffer& buf1, 36 void EnsureBuffersDontShareData(const CopyOnWriteBuffer& buf1,
37 const CopyOnWriteBuffer& buf2) { 37 const CopyOnWriteBuffer& buf2) {
38 // Data is not shared between buffers. 38 // Data is not shared between buffers.
39 const uint8_t* data1 = buf1.cdata(); 39 const uint8_t* data1 = buf1.cdata();
40 const uint8_t* data2 = buf2.cdata(); 40 const uint8_t* data2 = buf2.cdata();
41 EXPECT_NE(data1, data2); 41 EXPECT_NE(data1, data2);
42 } 42 }
43 43
44 TEST(CopyOnWriteBufferTest, TestCreateEmptyData) {
45 CopyOnWriteBuffer buf(static_cast<const uint8_t*>(nullptr), 0);
46 EXPECT_EQ(buf.size(), 0u);
47 EXPECT_EQ(buf.capacity(), 0u);
48 EXPECT_EQ(buf.data(), nullptr);
49 }
50
44 TEST(CopyOnWriteBufferTest, TestMoveConstruct) { 51 TEST(CopyOnWriteBufferTest, TestMoveConstruct) {
45 CopyOnWriteBuffer buf1(kTestData, 3, 10); 52 CopyOnWriteBuffer buf1(kTestData, 3, 10);
46 size_t buf1_size = buf1.size(); 53 size_t buf1_size = buf1.size();
47 size_t buf1_capacity = buf1.capacity(); 54 size_t buf1_capacity = buf1.capacity();
48 const uint8_t* buf1_data = buf1.cdata(); 55 const uint8_t* buf1_data = buf1.cdata();
49 56
50 CopyOnWriteBuffer buf2(std::move(buf1)); 57 CopyOnWriteBuffer buf2(std::move(buf1));
51 EXPECT_EQ(buf1.size(), 0u); 58 EXPECT_EQ(buf1.size(), 0u);
52 EXPECT_EQ(buf1.capacity(), 0u); 59 EXPECT_EQ(buf1.capacity(), 0u);
53 EXPECT_EQ(buf1.data(), nullptr); 60 EXPECT_EQ(buf1.data(), nullptr);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 EnsureBuffersShareData(buf1, buf2); 125 EnsureBuffersShareData(buf1, buf2);
119 126
120 CopyOnWriteBuffer buf3(buf1); 127 CopyOnWriteBuffer buf3(buf1);
121 // buf3 is re-allocated with new data, existing buffers are not modified. 128 // buf3 is re-allocated with new data, existing buffers are not modified.
122 buf3.SetData("foo"); 129 buf3.SetData("foo");
123 EXPECT_EQ(buf1, CopyOnWriteBuffer(kTestData, 3)); 130 EXPECT_EQ(buf1, CopyOnWriteBuffer(kTestData, 3));
124 EnsureBuffersShareData(buf1, buf2); 131 EnsureBuffersShareData(buf1, buf2);
125 EnsureBuffersDontShareData(buf1, buf3); 132 EnsureBuffersDontShareData(buf1, buf3);
126 const int8_t exp[] = {'f', 'o', 'o', 0x0}; 133 const int8_t exp[] = {'f', 'o', 'o', 0x0};
127 EXPECT_EQ(buf3, CopyOnWriteBuffer(exp)); 134 EXPECT_EQ(buf3, CopyOnWriteBuffer(exp));
135
136 buf2.SetData(static_cast<const uint8_t*>(nullptr), 0u);
137 EnsureBuffersDontShareData(buf1, buf2);
138 EXPECT_EQ(buf1.size(), 3u);
139 EXPECT_EQ(buf1.capacity(), 10u);
140 EXPECT_EQ(buf2.size(), 0u);
141 EXPECT_EQ(buf2.capacity(), 0u);
142 }
143
144 TEST(CopyOnWriteBufferTest, TestSetDataEmpty) {
145 CopyOnWriteBuffer buf;
146 buf.SetData(static_cast<const uint8_t*>(nullptr), 0u);
147 EXPECT_EQ(buf.size(), 0u);
148 EXPECT_EQ(buf.capacity(), 0u);
149 EXPECT_EQ(buf.data(), nullptr);
128 } 150 }
129 151
130 TEST(CopyOnWriteBufferTest, TestEnsureCapacity) { 152 TEST(CopyOnWriteBufferTest, TestEnsureCapacity) {
131 CopyOnWriteBuffer buf1(kTestData, 3, 10); 153 CopyOnWriteBuffer buf1(kTestData, 3, 10);
132 CopyOnWriteBuffer buf2(buf1); 154 CopyOnWriteBuffer buf2(buf1);
133 155
134 // Smaller than existing capacity -> no change and still same contents. 156 // Smaller than existing capacity -> no change and still same contents.
135 buf2.EnsureCapacity(8); 157 buf2.EnsureCapacity(8);
136 EnsureBuffersShareData(buf1, buf2); 158 EnsureBuffersShareData(buf1, buf2);
137 EXPECT_EQ(buf1.size(), 3u); 159 EXPECT_EQ(buf1.size(), 3u);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 // Non-const .data() clones data if shared. 211 // Non-const .data() clones data if shared.
190 const uint8_t* data1 = buf1.data(); 212 const uint8_t* data1 = buf1.data();
191 const uint8_t* data2 = buf2.data(); 213 const uint8_t* data2 = buf2.data();
192 EXPECT_NE(data1, data2); 214 EXPECT_NE(data1, data2);
193 // buf1 was cloned above. 215 // buf1 was cloned above.
194 EXPECT_NE(data1, cdata1); 216 EXPECT_NE(data1, cdata1);
195 // Therefore buf2 was no longer sharing data and was not cloned. 217 // Therefore buf2 was no longer sharing data and was not cloned.
196 EXPECT_EQ(data2, cdata1); 218 EXPECT_EQ(data2, cdata1);
197 } 219 }
198 220
221 TEST(CopyOnWriteBufferTest, TestBacketRead) {
222 CopyOnWriteBuffer buf1(kTestData, 3, 10);
223 CopyOnWriteBuffer buf2(buf1);
224
225 EnsureBuffersShareData(buf1, buf2);
226 // Non-const reads clone the data if shared.
227 for (size_t i = 0; i != 3u; ++i) {
228 EXPECT_EQ(buf1[i], kTestData[i]);
229 }
230 EnsureBuffersDontShareData(buf1, buf2);
231 }
232
233 TEST(CopyOnWriteBufferTest, TestBacketReadConst) {
234 CopyOnWriteBuffer buf1(kTestData, 3, 10);
235 CopyOnWriteBuffer buf2(buf1);
236
237 EnsureBuffersShareData(buf1, buf2);
238 const CopyOnWriteBuffer& cbuf1 = buf1;
239 for (size_t i = 0; i != 3u; ++i) {
240 EXPECT_EQ(cbuf1[i], kTestData[i]);
241 }
242 EnsureBuffersShareData(buf1, buf2);
243 }
244
245 TEST(CopyOnWriteBufferTest, TestBacketWrite) {
246 CopyOnWriteBuffer buf1(kTestData, 3, 10);
247 CopyOnWriteBuffer buf2(buf1);
248
249 EnsureBuffersShareData(buf1, buf2);
250 for (size_t i = 0; i != 3u; ++i) {
251 buf1[i] = kTestData[i] + 1;
252 }
253 EXPECT_EQ(buf1.size(), 3u);
254 EXPECT_EQ(buf1.capacity(), 10u);
255 EXPECT_EQ(buf2.size(), 3u);
256 EXPECT_EQ(buf2.capacity(), 10u);
257 EXPECT_EQ(0, memcmp(buf2.cdata(), kTestData, 3));
258 }
259
199 } // namespace rtc 260 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698