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

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

Issue 1785713005: Use CopyOnWriteBuffer instead of Buffer to avoid unnecessary copies. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fix errors on bots (java, objc). 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);
kwiberg-webrtc 2016/03/14 11:53:49 Do you need the static_cast here?
joachim 2016/03/17 20:27:21 Yes, otherwise compilation fails with error: no m
kwiberg-webrtc 2016/03/18 05:27:11 Right. Thanks.
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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
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));
128 } 135 }
129 136
137 TEST(CopyOnWriteBufferTest, TestSetDataEmpty) {
138 CopyOnWriteBuffer buf;
139 buf.SetData(static_cast<const uint8_t*>(nullptr), 0u);
kwiberg-webrtc 2016/03/14 11:53:49 ...and here?
joachim 2016/03/17 20:27:21 see above
kwiberg-webrtc 2016/03/18 05:27:11 Acknowledged.
140 EXPECT_EQ(buf.size(), 0u);
141 EXPECT_EQ(buf.capacity(), 0u);
142 EXPECT_EQ(buf.data(), nullptr);
143 }
144
130 TEST(CopyOnWriteBufferTest, TestEnsureCapacity) { 145 TEST(CopyOnWriteBufferTest, TestEnsureCapacity) {
131 CopyOnWriteBuffer buf1(kTestData, 3, 10); 146 CopyOnWriteBuffer buf1(kTestData, 3, 10);
132 CopyOnWriteBuffer buf2(buf1); 147 CopyOnWriteBuffer buf2(buf1);
133 148
134 // Smaller than existing capacity -> no change and still same contents. 149 // Smaller than existing capacity -> no change and still same contents.
135 buf2.EnsureCapacity(8); 150 buf2.EnsureCapacity(8);
136 EnsureBuffersShareData(buf1, buf2); 151 EnsureBuffersShareData(buf1, buf2);
137 EXPECT_EQ(buf1.size(), 3u); 152 EXPECT_EQ(buf1.size(), 3u);
138 EXPECT_EQ(buf1.capacity(), 10u); 153 EXPECT_EQ(buf1.capacity(), 10u);
139 EXPECT_EQ(buf2.size(), 3u); 154 EXPECT_EQ(buf2.size(), 3u);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 const uint8_t* data1 = buf1.data(); 205 const uint8_t* data1 = buf1.data();
191 const uint8_t* data2 = buf2.data(); 206 const uint8_t* data2 = buf2.data();
192 EXPECT_NE(data1, data2); 207 EXPECT_NE(data1, data2);
193 // buf1 was cloned above. 208 // buf1 was cloned above.
194 EXPECT_NE(data1, cdata1); 209 EXPECT_NE(data1, cdata1);
195 // Therefore buf2 was no longer sharing data and was not cloned. 210 // Therefore buf2 was no longer sharing data and was not cloned.
196 EXPECT_EQ(data2, cdata1); 211 EXPECT_EQ(data2, cdata1);
197 } 212 }
198 213
199 } // namespace rtc 214 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698