1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
|
/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/buffer.h"
#include <cstdint>
#include <utility>
#include "absl/strings/string_view.h"
#include "api/array_view.h"
#include "test/gmock.h"
#include "test/gtest.h"
namespace rtc {
namespace {
using ::testing::ElementsAre;
using ::testing::ElementsAreArray;
// clang-format off
const uint8_t kTestData[] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
// clang-format on
void TestBuf(const Buffer& b1, size_t size, size_t capacity) {
EXPECT_EQ(b1.size(), size);
EXPECT_EQ(b1.capacity(), capacity);
}
} // namespace
TEST(BufferTest, TestConstructEmpty) {
TestBuf(Buffer(), 0, 0);
TestBuf(Buffer(Buffer()), 0, 0);
TestBuf(Buffer(0), 0, 0);
// We can't use a literal 0 for the first argument, because C++ will allow
// that to be considered a null pointer, which makes the call ambiguous.
TestBuf(Buffer(0 + 0, 10), 0, 10);
TestBuf(Buffer(kTestData, 0), 0, 0);
TestBuf(Buffer(kTestData, 0, 20), 0, 20);
}
TEST(BufferTest, TestConstructData) {
Buffer buf(kTestData, 7);
EXPECT_EQ(buf.size(), 7u);
EXPECT_EQ(buf.capacity(), 7u);
EXPECT_FALSE(buf.empty());
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 7));
}
TEST(BufferTest, TestConstructDataWithCapacity) {
Buffer buf(kTestData, 7, 14);
EXPECT_EQ(buf.size(), 7u);
EXPECT_EQ(buf.capacity(), 14u);
EXPECT_FALSE(buf.empty());
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 7));
}
TEST(BufferTest, TestConstructArray) {
Buffer buf(kTestData);
EXPECT_EQ(buf.size(), 16u);
EXPECT_EQ(buf.capacity(), 16u);
EXPECT_FALSE(buf.empty());
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 16));
}
TEST(BufferTest, TestStringViewConversion) {
Buffer buf(kTestData);
absl::string_view view = buf;
EXPECT_EQ(view,
absl::string_view(reinterpret_cast<const char*>(kTestData), 16u));
}
TEST(BufferTest, TestSetData) {
Buffer buf(kTestData + 4, 7);
buf.SetData(kTestData, 9);
EXPECT_EQ(buf.size(), 9u);
EXPECT_EQ(buf.capacity(), 7u * 3 / 2);
EXPECT_FALSE(buf.empty());
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 9));
Buffer buf2;
buf2.SetData(buf);
EXPECT_EQ(buf.size(), 9u);
EXPECT_EQ(buf.capacity(), 7u * 3 / 2);
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 9));
}
TEST(BufferTest, TestAppendData) {
Buffer buf(kTestData + 4, 3);
buf.AppendData(kTestData + 10, 2);
const int8_t exp[] = {0x4, 0x5, 0x6, 0xa, 0xb};
EXPECT_EQ(buf, Buffer(exp));
Buffer buf2;
buf2.AppendData(buf);
buf2.AppendData(rtc::ArrayView<uint8_t>(buf));
const int8_t exp2[] = {0x4, 0x5, 0x6, 0xa, 0xb, 0x4, 0x5, 0x6, 0xa, 0xb};
EXPECT_EQ(buf2, Buffer(exp2));
}
TEST(BufferTest, TestSetAndAppendWithUnknownArg) {
struct TestDataContainer {
size_t size() const { return 3; }
const uint8_t* data() const { return kTestData; }
};
Buffer buf;
buf.SetData(TestDataContainer());
EXPECT_EQ(3u, buf.size());
EXPECT_EQ(Buffer(kTestData, 3), buf);
EXPECT_THAT(buf, ElementsAre(0, 1, 2));
buf.AppendData(TestDataContainer());
EXPECT_EQ(6u, buf.size());
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 3));
EXPECT_EQ(0, memcmp(buf.data() + 3, kTestData, 3));
EXPECT_THAT(buf, ElementsAre(0, 1, 2, 0, 1, 2));
}
TEST(BufferTest, TestSetSizeSmaller) {
Buffer buf;
buf.SetData(kTestData, 15);
buf.SetSize(10);
EXPECT_EQ(buf.size(), 10u);
EXPECT_EQ(buf.capacity(), 15u); // Hasn't shrunk.
EXPECT_FALSE(buf.empty());
EXPECT_EQ(buf, Buffer(kTestData, 10));
}
TEST(BufferTest, TestSetSizeLarger) {
Buffer buf;
buf.SetData(kTestData, 15);
EXPECT_EQ(buf.size(), 15u);
EXPECT_EQ(buf.capacity(), 15u);
EXPECT_FALSE(buf.empty());
buf.SetSize(20);
EXPECT_EQ(buf.size(), 20u);
EXPECT_EQ(buf.capacity(), 15u * 3 / 2); // Has grown.
EXPECT_FALSE(buf.empty());
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 15));
}
TEST(BufferTest, TestEnsureCapacitySmaller) {
Buffer buf(kTestData);
const char* data = buf.data<char>();
buf.EnsureCapacity(4);
EXPECT_EQ(buf.capacity(), 16u); // Hasn't shrunk.
EXPECT_EQ(buf.data<char>(), data); // No reallocation.
EXPECT_FALSE(buf.empty());
EXPECT_EQ(buf, Buffer(kTestData));
}
TEST(BufferTest, TestEnsureCapacityLarger) {
Buffer buf(kTestData, 5);
buf.EnsureCapacity(10);
const int8_t* data = buf.data<int8_t>();
EXPECT_EQ(buf.capacity(), 10u);
buf.AppendData(kTestData + 5, 5);
EXPECT_EQ(buf.data<int8_t>(), data); // No reallocation.
EXPECT_FALSE(buf.empty());
EXPECT_EQ(buf, Buffer(kTestData, 10));
}
TEST(BufferTest, TestMoveConstruct) {
Buffer buf1(kTestData, 3, 40);
const uint8_t* data = buf1.data();
Buffer buf2(std::move(buf1));
EXPECT_EQ(buf2.size(), 3u);
EXPECT_EQ(buf2.capacity(), 40u);
EXPECT_EQ(buf2.data(), data);
EXPECT_FALSE(buf2.empty());
buf1.Clear();
EXPECT_EQ(buf1.size(), 0u);
EXPECT_EQ(buf1.capacity(), 0u);
EXPECT_EQ(buf1.data(), nullptr);
EXPECT_TRUE(buf1.empty());
}
TEST(BufferTest, TestMoveAssign) {
Buffer buf1(kTestData, 3, 40);
const uint8_t* data = buf1.data();
Buffer buf2(kTestData);
buf2 = std::move(buf1);
EXPECT_EQ(buf2.size(), 3u);
EXPECT_EQ(buf2.capacity(), 40u);
EXPECT_EQ(buf2.data(), data);
EXPECT_FALSE(buf2.empty());
buf1.Clear();
EXPECT_EQ(buf1.size(), 0u);
EXPECT_EQ(buf1.capacity(), 0u);
EXPECT_EQ(buf1.data(), nullptr);
EXPECT_TRUE(buf1.empty());
}
TEST(BufferTest, TestMoveAssignSelf) {
// Move self-assignment isn't required to produce a meaningful state, but
// should not leave the object in an inconsistent state. (Such inconsistent
// state could be caught by the DCHECKs and/or by the leak checker.) We need
// to be sneaky when testing this; if we're doing a too-obvious
// move-assign-to-self, clang's -Wself-move triggers at compile time.
Buffer buf(kTestData, 3, 40);
Buffer* buf_ptr = &buf;
buf = std::move(*buf_ptr);
}
TEST(BufferTest, TestSwap) {
Buffer buf1(kTestData, 3);
Buffer buf2(kTestData, 6, 40);
uint8_t* data1 = buf1.data();
uint8_t* data2 = buf2.data();
using std::swap;
swap(buf1, buf2);
EXPECT_EQ(buf1.size(), 6u);
EXPECT_EQ(buf1.capacity(), 40u);
EXPECT_EQ(buf1.data(), data2);
EXPECT_FALSE(buf1.empty());
EXPECT_EQ(buf2.size(), 3u);
EXPECT_EQ(buf2.capacity(), 3u);
EXPECT_EQ(buf2.data(), data1);
EXPECT_FALSE(buf2.empty());
}
TEST(BufferTest, TestClear) {
Buffer buf;
buf.SetData(kTestData, 15);
EXPECT_EQ(buf.size(), 15u);
EXPECT_EQ(buf.capacity(), 15u);
EXPECT_FALSE(buf.empty());
const char* data = buf.data<char>();
buf.Clear();
EXPECT_EQ(buf.size(), 0u);
EXPECT_EQ(buf.capacity(), 15u); // Hasn't shrunk.
EXPECT_EQ(buf.data<char>(), data); // No reallocation.
EXPECT_TRUE(buf.empty());
}
TEST(BufferTest, TestLambdaSetAppend) {
auto setter = [](rtc::ArrayView<uint8_t> av) {
for (int i = 0; i != 15; ++i)
av[i] = kTestData[i];
return 15;
};
Buffer buf1;
buf1.SetData(kTestData, 15);
buf1.AppendData(kTestData, 15);
Buffer buf2;
EXPECT_EQ(buf2.SetData(15, setter), 15u);
EXPECT_EQ(buf2.AppendData(15, setter), 15u);
EXPECT_EQ(buf1, buf2);
EXPECT_EQ(buf1.capacity(), buf2.capacity());
EXPECT_FALSE(buf1.empty());
EXPECT_FALSE(buf2.empty());
}
TEST(BufferTest, TestLambdaSetAppendSigned) {
auto setter = [](rtc::ArrayView<int8_t> av) {
for (int i = 0; i != 15; ++i)
av[i] = kTestData[i];
return 15;
};
Buffer buf1;
buf1.SetData(kTestData, 15);
buf1.AppendData(kTestData, 15);
Buffer buf2;
EXPECT_EQ(buf2.SetData<int8_t>(15, setter), 15u);
EXPECT_EQ(buf2.AppendData<int8_t>(15, setter), 15u);
EXPECT_EQ(buf1, buf2);
EXPECT_EQ(buf1.capacity(), buf2.capacity());
EXPECT_FALSE(buf1.empty());
EXPECT_FALSE(buf2.empty());
}
TEST(BufferTest, TestLambdaAppendEmpty) {
auto setter = [](rtc::ArrayView<uint8_t> av) {
for (int i = 0; i != 15; ++i)
av[i] = kTestData[i];
return 15;
};
Buffer buf1;
buf1.SetData(kTestData, 15);
Buffer buf2;
EXPECT_EQ(buf2.AppendData(15, setter), 15u);
EXPECT_EQ(buf1, buf2);
EXPECT_EQ(buf1.capacity(), buf2.capacity());
EXPECT_FALSE(buf1.empty());
EXPECT_FALSE(buf2.empty());
}
TEST(BufferTest, TestLambdaAppendPartial) {
auto setter = [](rtc::ArrayView<uint8_t> av) {
for (int i = 0; i != 7; ++i)
av[i] = kTestData[i];
return 7;
};
Buffer buf;
EXPECT_EQ(buf.AppendData(15, setter), 7u);
EXPECT_EQ(buf.size(), 7u); // Size is exactly what we wrote.
EXPECT_GE(buf.capacity(), 7u); // Capacity is valid.
EXPECT_NE(buf.data<char>(), nullptr); // Data is actually stored.
EXPECT_FALSE(buf.empty());
}
TEST(BufferTest, TestMutableLambdaSetAppend) {
uint8_t magic_number = 17;
auto setter = [magic_number](rtc::ArrayView<uint8_t> av) mutable {
for (int i = 0; i != 15; ++i) {
av[i] = magic_number;
++magic_number;
}
return 15;
};
EXPECT_EQ(magic_number, 17);
Buffer buf;
EXPECT_EQ(buf.SetData(15, setter), 15u);
EXPECT_EQ(buf.AppendData(15, setter), 15u);
EXPECT_EQ(buf.size(), 30u); // Size is exactly what we wrote.
EXPECT_GE(buf.capacity(), 30u); // Capacity is valid.
EXPECT_NE(buf.data<char>(), nullptr); // Data is actually stored.
EXPECT_FALSE(buf.empty());
for (uint8_t i = 0; i != buf.size(); ++i) {
EXPECT_EQ(buf.data()[i], magic_number + i);
}
}
TEST(BufferTest, TestBracketRead) {
Buffer buf(kTestData, 7);
EXPECT_EQ(buf.size(), 7u);
EXPECT_EQ(buf.capacity(), 7u);
EXPECT_NE(buf.data(), nullptr);
EXPECT_FALSE(buf.empty());
for (size_t i = 0; i != 7u; ++i) {
EXPECT_EQ(buf[i], kTestData[i]);
}
}
TEST(BufferTest, TestBracketReadConst) {
Buffer buf(kTestData, 7);
EXPECT_EQ(buf.size(), 7u);
EXPECT_EQ(buf.capacity(), 7u);
EXPECT_NE(buf.data(), nullptr);
EXPECT_FALSE(buf.empty());
const Buffer& cbuf = buf;
for (size_t i = 0; i != 7u; ++i) {
EXPECT_EQ(cbuf[i], kTestData[i]);
}
}
TEST(BufferTest, TestBracketWrite) {
Buffer buf(7);
EXPECT_EQ(buf.size(), 7u);
EXPECT_EQ(buf.capacity(), 7u);
EXPECT_NE(buf.data(), nullptr);
EXPECT_FALSE(buf.empty());
for (size_t i = 0; i != 7u; ++i) {
buf[i] = kTestData[i];
}
EXPECT_THAT(buf, ElementsAreArray(kTestData, 7));
}
TEST(BufferTest, TestBeginEnd) {
const Buffer cbuf(kTestData);
Buffer buf(kTestData);
auto* b1 = cbuf.begin();
for (auto& x : buf) {
EXPECT_EQ(*b1, x);
++b1;
++x;
}
EXPECT_EQ(cbuf.end(), b1);
auto* b2 = buf.begin();
for (auto& y : cbuf) {
EXPECT_EQ(*b2, y + 1);
++b2;
}
EXPECT_EQ(buf.end(), b2);
}
TEST(BufferTest, TestInt16) {
static constexpr int16_t test_data[] = {14, 15, 16, 17, 18};
BufferT<int16_t> buf(test_data);
EXPECT_EQ(buf.size(), 5u);
EXPECT_EQ(buf.capacity(), 5u);
EXPECT_NE(buf.data(), nullptr);
EXPECT_FALSE(buf.empty());
EXPECT_THAT(buf, ElementsAreArray(test_data));
BufferT<int16_t> buf2(test_data);
EXPECT_EQ(buf, buf2);
buf2[0] = 9;
EXPECT_NE(buf, buf2);
}
TEST(BufferTest, TestFloat) {
static constexpr float test_data[] = {14, 15, 16, 17, 18};
BufferT<float> buf;
EXPECT_EQ(buf.size(), 0u);
EXPECT_EQ(buf.capacity(), 0u);
EXPECT_EQ(buf.data(), nullptr);
EXPECT_TRUE(buf.empty());
buf.SetData(test_data);
EXPECT_EQ(buf.size(), 5u);
EXPECT_EQ(buf.capacity(), 5u);
EXPECT_NE(buf.data(), nullptr);
EXPECT_FALSE(buf.empty());
float* p1 = buf.data();
while (buf.data() == p1) {
buf.AppendData(test_data);
}
EXPECT_EQ(buf.size(), buf.capacity());
EXPECT_GT(buf.size(), 5u);
EXPECT_EQ(buf.size() % 5, 0u);
EXPECT_NE(buf.data(), nullptr);
for (size_t i = 0; i != buf.size(); ++i) {
EXPECT_EQ(test_data[i % 5], buf[i]);
}
}
TEST(BufferTest, TestStruct) {
struct BloodStone {
bool blood;
const char* stone;
};
BufferT<BloodStone> buf(4);
EXPECT_EQ(buf.size(), 4u);
EXPECT_EQ(buf.capacity(), 4u);
EXPECT_NE(buf.data(), nullptr);
EXPECT_FALSE(buf.empty());
BufferT<BloodStone*> buf2(4);
for (size_t i = 0; i < buf2.size(); ++i) {
buf2[i] = &buf[i];
}
static const char kObsidian[] = "obsidian";
buf2[2]->stone = kObsidian;
EXPECT_EQ(kObsidian, buf[2].stone);
}
TEST(BufferDeathTest, DieOnUseAfterMove) {
Buffer buf(17);
Buffer buf2 = std::move(buf);
EXPECT_EQ(buf2.size(), 17u);
#if RTC_DCHECK_IS_ON
#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
EXPECT_DEATH(buf.empty(), "");
#endif
#else
EXPECT_TRUE(buf.empty());
#endif
}
TEST(ZeroOnFreeBufferTest, TestZeroOnSetData) {
ZeroOnFreeBuffer<uint8_t> buf(kTestData, 7);
const uint8_t* old_data = buf.data();
const size_t old_capacity = buf.capacity();
const size_t old_size = buf.size();
constexpr size_t offset = 1;
buf.SetData(kTestData + offset, 2);
// Sanity checks to make sure the underlying heap memory was not reallocated.
EXPECT_EQ(old_data, buf.data());
EXPECT_EQ(old_capacity, buf.capacity());
// The first two elements have been overwritten, and the remaining five have
// been zeroed.
EXPECT_EQ(kTestData[offset], buf[0]);
EXPECT_EQ(kTestData[offset + 1], buf[1]);
for (size_t i = 2; i < old_size; i++) {
EXPECT_EQ(0, old_data[i]);
}
}
TEST(ZeroOnFreeBufferTest, TestZeroOnSetDataFromSetter) {
static constexpr size_t offset = 1;
const auto setter = [](rtc::ArrayView<uint8_t> av) {
for (int i = 0; i != 2; ++i)
av[i] = kTestData[offset + i];
return 2;
};
ZeroOnFreeBuffer<uint8_t> buf(kTestData, 7);
const uint8_t* old_data = buf.data();
const size_t old_capacity = buf.capacity();
const size_t old_size = buf.size();
buf.SetData(2, setter);
// Sanity checks to make sure the underlying heap memory was not reallocated.
EXPECT_EQ(old_data, buf.data());
EXPECT_EQ(old_capacity, buf.capacity());
// The first two elements have been overwritten, and the remaining five have
// been zeroed.
EXPECT_EQ(kTestData[offset], buf[0]);
EXPECT_EQ(kTestData[offset + 1], buf[1]);
for (size_t i = 2; i < old_size; i++) {
EXPECT_EQ(0, old_data[i]);
}
}
TEST(ZeroOnFreeBufferTest, TestZeroOnSetSize) {
ZeroOnFreeBuffer<uint8_t> buf(kTestData, 7);
const uint8_t* old_data = buf.data();
const size_t old_capacity = buf.capacity();
const size_t old_size = buf.size();
buf.SetSize(2);
// Sanity checks to make sure the underlying heap memory was not reallocated.
EXPECT_EQ(old_data, buf.data());
EXPECT_EQ(old_capacity, buf.capacity());
// The first two elements have not been modified and the remaining five have
// been zeroed.
EXPECT_EQ(kTestData[0], buf[0]);
EXPECT_EQ(kTestData[1], buf[1]);
for (size_t i = 2; i < old_size; i++) {
EXPECT_EQ(0, old_data[i]);
}
}
TEST(ZeroOnFreeBufferTest, TestZeroOnClear) {
ZeroOnFreeBuffer<uint8_t> buf(kTestData, 7);
const uint8_t* old_data = buf.data();
const size_t old_capacity = buf.capacity();
const size_t old_size = buf.size();
buf.Clear();
// Sanity checks to make sure the underlying heap memory was not reallocated.
EXPECT_EQ(old_data, buf.data());
EXPECT_EQ(old_capacity, buf.capacity());
// The underlying memory was not released but cleared.
for (size_t i = 0; i < old_size; i++) {
EXPECT_EQ(0, old_data[i]);
}
}
} // namespace rtc
|