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

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

Issue 1817753003: Revert of 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
« no previous file with comments | « webrtc/base/copyonwritebuffer.h ('k') | webrtc/base/sslfingerprint.h » ('j') | 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 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
51 TEST(CopyOnWriteBufferTest, TestMoveConstruct) { 44 TEST(CopyOnWriteBufferTest, TestMoveConstruct) {
52 CopyOnWriteBuffer buf1(kTestData, 3, 10); 45 CopyOnWriteBuffer buf1(kTestData, 3, 10);
53 size_t buf1_size = buf1.size(); 46 size_t buf1_size = buf1.size();
54 size_t buf1_capacity = buf1.capacity(); 47 size_t buf1_capacity = buf1.capacity();
55 const uint8_t* buf1_data = buf1.cdata(); 48 const uint8_t* buf1_data = buf1.cdata();
56 49
57 CopyOnWriteBuffer buf2(std::move(buf1)); 50 CopyOnWriteBuffer buf2(std::move(buf1));
58 EXPECT_EQ(buf1.size(), 0u); 51 EXPECT_EQ(buf1.size(), 0u);
59 EXPECT_EQ(buf1.capacity(), 0u); 52 EXPECT_EQ(buf1.capacity(), 0u);
60 EXPECT_EQ(buf1.data(), nullptr); 53 EXPECT_EQ(buf1.data(), nullptr);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 EnsureBuffersShareData(buf1, buf2); 118 EnsureBuffersShareData(buf1, buf2);
126 119
127 CopyOnWriteBuffer buf3(buf1); 120 CopyOnWriteBuffer buf3(buf1);
128 // buf3 is re-allocated with new data, existing buffers are not modified. 121 // buf3 is re-allocated with new data, existing buffers are not modified.
129 buf3.SetData("foo"); 122 buf3.SetData("foo");
130 EXPECT_EQ(buf1, CopyOnWriteBuffer(kTestData, 3)); 123 EXPECT_EQ(buf1, CopyOnWriteBuffer(kTestData, 3));
131 EnsureBuffersShareData(buf1, buf2); 124 EnsureBuffersShareData(buf1, buf2);
132 EnsureBuffersDontShareData(buf1, buf3); 125 EnsureBuffersDontShareData(buf1, buf3);
133 const int8_t exp[] = {'f', 'o', 'o', 0x0}; 126 const int8_t exp[] = {'f', 'o', 'o', 0x0};
134 EXPECT_EQ(buf3, CopyOnWriteBuffer(exp)); 127 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);
150 } 128 }
151 129
152 TEST(CopyOnWriteBufferTest, TestEnsureCapacity) { 130 TEST(CopyOnWriteBufferTest, TestEnsureCapacity) {
153 CopyOnWriteBuffer buf1(kTestData, 3, 10); 131 CopyOnWriteBuffer buf1(kTestData, 3, 10);
154 CopyOnWriteBuffer buf2(buf1); 132 CopyOnWriteBuffer buf2(buf1);
155 133
156 // Smaller than existing capacity -> no change and still same contents. 134 // Smaller than existing capacity -> no change and still same contents.
157 buf2.EnsureCapacity(8); 135 buf2.EnsureCapacity(8);
158 EnsureBuffersShareData(buf1, buf2); 136 EnsureBuffersShareData(buf1, buf2);
159 EXPECT_EQ(buf1.size(), 3u); 137 EXPECT_EQ(buf1.size(), 3u);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 // Non-const .data() clones data if shared. 189 // Non-const .data() clones data if shared.
212 const uint8_t* data1 = buf1.data(); 190 const uint8_t* data1 = buf1.data();
213 const uint8_t* data2 = buf2.data(); 191 const uint8_t* data2 = buf2.data();
214 EXPECT_NE(data1, data2); 192 EXPECT_NE(data1, data2);
215 // buf1 was cloned above. 193 // buf1 was cloned above.
216 EXPECT_NE(data1, cdata1); 194 EXPECT_NE(data1, cdata1);
217 // Therefore buf2 was no longer sharing data and was not cloned. 195 // Therefore buf2 was no longer sharing data and was not cloned.
218 EXPECT_EQ(data2, cdata1); 196 EXPECT_EQ(data2, cdata1);
219 } 197 }
220 198
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
260 } // namespace rtc 199 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/copyonwritebuffer.h ('k') | webrtc/base/sslfingerprint.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698