OLD | NEW |
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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 buf2.AppendData("foo"); | 109 buf2.AppendData("foo"); |
110 EXPECT_EQ(buf2.size(), buf1.size() + 4); // "foo" + trailing 0x00 | 110 EXPECT_EQ(buf2.size(), buf1.size() + 4); // "foo" + trailing 0x00 |
111 EXPECT_EQ(buf2.capacity(), buf1.capacity()); | 111 EXPECT_EQ(buf2.capacity(), buf1.capacity()); |
112 EXPECT_NE(buf2.data(), buf1.data()); | 112 EXPECT_NE(buf2.data(), buf1.data()); |
113 | 113 |
114 EXPECT_EQ(buf1, CopyOnWriteBuffer(kTestData, 3)); | 114 EXPECT_EQ(buf1, CopyOnWriteBuffer(kTestData, 3)); |
115 const int8_t exp[] = {0x0, 0x1, 0x2, 'f', 'o', 'o', 0x0}; | 115 const int8_t exp[] = {0x0, 0x1, 0x2, 'f', 'o', 'o', 0x0}; |
116 EXPECT_EQ(buf2, CopyOnWriteBuffer(exp)); | 116 EXPECT_EQ(buf2, CopyOnWriteBuffer(exp)); |
117 } | 117 } |
118 | 118 |
119 TEST(CopyOnWriteBufferTest, TestSetData) { | 119 TEST(CopyOnWriteBufferTest, SetEmptyData) { |
| 120 CopyOnWriteBuffer buf(10); |
| 121 |
| 122 buf.SetData<uint8_t>(nullptr, 0); |
| 123 |
| 124 EXPECT_EQ(0u, buf.size()); |
| 125 } |
| 126 |
| 127 TEST(CopyOnWriteBufferTest, SetDataNoMoreThanCapacityDoesntCauseReallocation) { |
| 128 CopyOnWriteBuffer buf1(3, 10); |
| 129 const uint8_t* const original_allocation = buf1.cdata(); |
| 130 |
| 131 buf1.SetData(kTestData, 10); |
| 132 |
| 133 EXPECT_EQ(original_allocation, buf1.cdata()); |
| 134 EXPECT_EQ(buf1, CopyOnWriteBuffer(kTestData, 10)); |
| 135 } |
| 136 |
| 137 TEST(CopyOnWriteBufferTest, SetDataMakeReferenceCopy) { |
120 CopyOnWriteBuffer buf1(kTestData, 3, 10); | 138 CopyOnWriteBuffer buf1(kTestData, 3, 10); |
121 CopyOnWriteBuffer buf2; | 139 CopyOnWriteBuffer buf2; |
122 | 140 |
123 buf2.SetData(buf1); | 141 buf2.SetData(buf1); |
124 // buf2 shares the same data as buf1 now. | 142 |
| 143 EnsureBuffersShareData(buf1, buf2); |
| 144 } |
| 145 |
| 146 TEST(CopyOnWriteBufferTest, SetDataOnSharedKeepsOriginal) { |
| 147 const uint8_t data[] = "foo"; |
| 148 CopyOnWriteBuffer buf1(kTestData, 3, 10); |
| 149 const uint8_t* const original_allocation = buf1.cdata(); |
| 150 CopyOnWriteBuffer buf2(buf1); |
| 151 |
| 152 buf2.SetData(data); |
| 153 |
| 154 EnsureBuffersDontShareData(buf1, buf2); |
| 155 EXPECT_EQ(original_allocation, buf1.cdata()); |
| 156 EXPECT_EQ(buf1, CopyOnWriteBuffer(kTestData, 3)); |
| 157 EXPECT_EQ(buf2, CopyOnWriteBuffer(data)); |
| 158 } |
| 159 |
| 160 TEST(CopyOnWriteBufferTest, SetDataOnSharedKeepsCapacity) { |
| 161 CopyOnWriteBuffer buf1(kTestData, 3, 10); |
| 162 CopyOnWriteBuffer buf2(buf1); |
125 EnsureBuffersShareData(buf1, buf2); | 163 EnsureBuffersShareData(buf1, buf2); |
126 | 164 |
127 CopyOnWriteBuffer buf3(buf1); | 165 buf2.SetData(kTestData, 2); |
128 // buf3 is re-allocated with new data, existing buffers are not modified. | |
129 buf3.SetData("foo"); | |
130 EXPECT_EQ(buf1, CopyOnWriteBuffer(kTestData, 3)); | |
131 EnsureBuffersShareData(buf1, buf2); | |
132 EnsureBuffersDontShareData(buf1, buf3); | |
133 const int8_t exp[] = {'f', 'o', 'o', 0x0}; | |
134 EXPECT_EQ(buf3, CopyOnWriteBuffer(exp)); | |
135 | 166 |
136 buf2.SetData(static_cast<const uint8_t*>(nullptr), 0u); | |
137 EnsureBuffersDontShareData(buf1, buf2); | 167 EnsureBuffersDontShareData(buf1, buf2); |
138 EXPECT_EQ(buf1.size(), 3u); | 168 EXPECT_EQ(2u, buf2.size()); |
139 EXPECT_EQ(buf1.capacity(), 10u); | 169 EXPECT_EQ(10u, buf2.capacity()); |
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 } | 170 } |
151 | 171 |
152 TEST(CopyOnWriteBufferTest, TestEnsureCapacity) { | 172 TEST(CopyOnWriteBufferTest, TestEnsureCapacity) { |
153 CopyOnWriteBuffer buf1(kTestData, 3, 10); | 173 CopyOnWriteBuffer buf1(kTestData, 3, 10); |
154 CopyOnWriteBuffer buf2(buf1); | 174 CopyOnWriteBuffer buf2(buf1); |
155 | 175 |
156 // Smaller than existing capacity -> no change and still same contents. | 176 // Smaller than existing capacity -> no change and still same contents. |
157 buf2.EnsureCapacity(8); | 177 buf2.EnsureCapacity(8); |
158 EnsureBuffersShareData(buf1, buf2); | 178 EnsureBuffersShareData(buf1, buf2); |
159 EXPECT_EQ(buf1.size(), 3u); | 179 EXPECT_EQ(buf1.size(), 3u); |
160 EXPECT_EQ(buf1.capacity(), 10u); | 180 EXPECT_EQ(buf1.capacity(), 10u); |
161 EXPECT_EQ(buf2.size(), 3u); | 181 EXPECT_EQ(buf2.size(), 3u); |
162 EXPECT_EQ(buf2.capacity(), 10u); | 182 EXPECT_EQ(buf2.capacity(), 10u); |
163 | 183 |
164 // Lager than existing capacity -> data is cloned. | 184 // Lager than existing capacity -> data is cloned. |
165 buf2.EnsureCapacity(16); | 185 buf2.EnsureCapacity(16); |
166 EnsureBuffersDontShareData(buf1, buf2); | 186 EnsureBuffersDontShareData(buf1, buf2); |
167 EXPECT_EQ(buf1.size(), 3u); | 187 EXPECT_EQ(buf1.size(), 3u); |
168 EXPECT_EQ(buf1.capacity(), 10u); | 188 EXPECT_EQ(buf1.capacity(), 10u); |
169 EXPECT_EQ(buf2.size(), 3u); | 189 EXPECT_EQ(buf2.size(), 3u); |
170 EXPECT_EQ(buf2.capacity(), 16u); | 190 EXPECT_EQ(buf2.capacity(), 16u); |
171 // The size and contents are still the same. | 191 // The size and contents are still the same. |
172 EXPECT_EQ(buf1, buf2); | 192 EXPECT_EQ(buf1, buf2); |
173 } | 193 } |
174 | 194 |
175 TEST(CopyOnWriteBufferTest, TestSetSize) { | 195 TEST(CopyOnWriteBufferTest, SetSizeDoesntChangeOriginal) { |
| 196 CopyOnWriteBuffer buf1(kTestData, 3, 10); |
| 197 const uint8_t* const original_allocation = buf1.cdata(); |
| 198 CopyOnWriteBuffer buf2(buf1); |
| 199 |
| 200 buf2.SetSize(16); |
| 201 |
| 202 EnsureBuffersDontShareData(buf1, buf2); |
| 203 EXPECT_EQ(original_allocation, buf1.cdata()); |
| 204 EXPECT_EQ(3u, buf1.size()); |
| 205 EXPECT_EQ(10u, buf1.capacity()); |
| 206 } |
| 207 |
| 208 TEST(CopyOnWriteBufferTest, SetSizeCloneContent) { |
176 CopyOnWriteBuffer buf1(kTestData, 3, 10); | 209 CopyOnWriteBuffer buf1(kTestData, 3, 10); |
177 CopyOnWriteBuffer buf2(buf1); | 210 CopyOnWriteBuffer buf2(buf1); |
178 | 211 |
179 buf2.SetSize(16); | 212 buf2.SetSize(16); |
180 EnsureBuffersDontShareData(buf1, buf2); | 213 |
181 EXPECT_EQ(buf1.size(), 3u); | |
182 EXPECT_EQ(buf1.capacity(), 10u); | |
183 EXPECT_EQ(buf2.size(), 16u); | 214 EXPECT_EQ(buf2.size(), 16u); |
184 EXPECT_EQ(buf2.capacity(), 16u); | |
185 // The contents got cloned. | |
186 EXPECT_EQ(0, memcmp(buf2.data(), kTestData, 3)); | 215 EXPECT_EQ(0, memcmp(buf2.data(), kTestData, 3)); |
187 } | 216 } |
188 | 217 |
189 TEST(CopyOnWriteBufferTest, TestClear) { | 218 TEST(CopyOnWriteBufferTest, SetSizeMayIncreaseCapacity) { |
| 219 CopyOnWriteBuffer buf(kTestData, 3, 10); |
| 220 |
| 221 buf.SetSize(16); |
| 222 |
| 223 EXPECT_EQ(16u, buf.size()); |
| 224 EXPECT_EQ(16u, buf.capacity()); |
| 225 } |
| 226 |
| 227 TEST(CopyOnWriteBufferTest, SetSizeDoesntDecreaseCapacity) { |
| 228 CopyOnWriteBuffer buf1(kTestData, 5, 10); |
| 229 CopyOnWriteBuffer buf2(buf1); |
| 230 |
| 231 buf2.SetSize(2); |
| 232 |
| 233 EXPECT_EQ(2u, buf2.size()); |
| 234 EXPECT_EQ(10u, buf2.capacity()); |
| 235 } |
| 236 |
| 237 TEST(CopyOnWriteBufferTest, ClearDoesntChangeOriginal) { |
| 238 CopyOnWriteBuffer buf1(kTestData, 3, 10); |
| 239 const uint8_t* const original_allocation = buf1.cdata(); |
| 240 CopyOnWriteBuffer buf2(buf1); |
| 241 |
| 242 buf2.Clear(); |
| 243 |
| 244 EnsureBuffersDontShareData(buf1, buf2); |
| 245 EXPECT_EQ(3u, buf1.size()); |
| 246 EXPECT_EQ(10u, buf1.capacity()); |
| 247 EXPECT_EQ(original_allocation, buf1.cdata()); |
| 248 EXPECT_EQ(0u, buf2.size()); |
| 249 } |
| 250 |
| 251 TEST(CopyOnWriteBufferTest, ClearDoesntChangeCapacity) { |
190 CopyOnWriteBuffer buf1(kTestData, 3, 10); | 252 CopyOnWriteBuffer buf1(kTestData, 3, 10); |
191 CopyOnWriteBuffer buf2(buf1); | 253 CopyOnWriteBuffer buf2(buf1); |
192 | 254 |
193 buf2.Clear(); | 255 buf2.Clear(); |
194 EnsureBuffersDontShareData(buf1, buf2); | 256 |
195 EXPECT_EQ(buf1.size(), 3u); | 257 EXPECT_EQ(0u, buf2.size()); |
196 EXPECT_EQ(buf1.capacity(), 10u); | 258 EXPECT_EQ(10u, buf2.capacity()); |
197 EXPECT_EQ(0, memcmp(buf1.data(), kTestData, 3)); | |
198 EXPECT_EQ(buf2.size(), 0u); | |
199 EXPECT_EQ(buf2.capacity(), 0u); | |
200 } | 259 } |
201 | 260 |
202 TEST(CopyOnWriteBufferTest, TestConstDataAccessor) { | 261 TEST(CopyOnWriteBufferTest, TestConstDataAccessor) { |
203 CopyOnWriteBuffer buf1(kTestData, 3, 10); | 262 CopyOnWriteBuffer buf1(kTestData, 3, 10); |
204 CopyOnWriteBuffer buf2(buf1); | 263 CopyOnWriteBuffer buf2(buf1); |
205 | 264 |
206 // .cdata() doesn't clone data. | 265 // .cdata() doesn't clone data. |
207 const uint8_t* cdata1 = buf1.cdata(); | 266 const uint8_t* cdata1 = buf1.cdata(); |
208 const uint8_t* cdata2 = buf2.cdata(); | 267 const uint8_t* cdata2 = buf2.cdata(); |
209 EXPECT_EQ(cdata1, cdata2); | 268 EXPECT_EQ(cdata1, cdata2); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 buf1[i] = kTestData[i] + 1; | 310 buf1[i] = kTestData[i] + 1; |
252 } | 311 } |
253 EXPECT_EQ(buf1.size(), 3u); | 312 EXPECT_EQ(buf1.size(), 3u); |
254 EXPECT_EQ(buf1.capacity(), 10u); | 313 EXPECT_EQ(buf1.capacity(), 10u); |
255 EXPECT_EQ(buf2.size(), 3u); | 314 EXPECT_EQ(buf2.size(), 3u); |
256 EXPECT_EQ(buf2.capacity(), 10u); | 315 EXPECT_EQ(buf2.capacity(), 10u); |
257 EXPECT_EQ(0, memcmp(buf2.cdata(), kTestData, 3)); | 316 EXPECT_EQ(0, memcmp(buf2.cdata(), kTestData, 3)); |
258 } | 317 } |
259 | 318 |
260 } // namespace rtc | 319 } // namespace rtc |
OLD | NEW |