OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | |
3 * | |
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 | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include "webrtc/base/copyonwritebuffer.h" | |
12 #include "webrtc/base/gunit.h" | |
13 | |
14 namespace rtc { | |
15 | |
16 namespace { | |
17 | |
18 // clang-format off | |
19 const uint8_t kTestData[] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, | |
20 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}; | |
21 // clang-format on | |
22 | |
23 } // namespace | |
24 | |
25 void EnsureBuffersEqual(const CopyOnWriteBuffer& buf1, | |
kwiberg-webrtc
2016/02/19 12:36:21
Perhaps call this "EnsureBuffersShareData" or some
joachim
2016/02/19 20:37:42
Done.
| |
26 const CopyOnWriteBuffer& buf2) { | |
27 // Data is shared between buffers. | |
28 EXPECT_EQ(buf1.size(), buf2.size()); | |
29 EXPECT_EQ(buf1.capacity(), buf2.capacity()); | |
30 const uint8_t* data1 = buf1.data(); | |
31 const uint8_t* data2 = buf2.data(); | |
32 EXPECT_EQ(data1, data2); | |
33 EXPECT_EQ(buf1, buf2); | |
34 } | |
35 | |
36 // Move comparison to own function to have const buffers and prevent cloning | |
37 // from calling non-const "data()". | |
kwiberg-webrtc
2016/02/19 12:36:21
Hmm. I almost suggested a cdata method when review
joachim
2016/02/19 20:37:42
Done.
| |
38 void EnsureBufferDataNotEqual(const CopyOnWriteBuffer& buf1, | |
kwiberg-webrtc
2016/02/19 12:36:21
EnsureBuffersDontShareData?
joachim
2016/02/19 20:37:42
Done.
| |
39 const CopyOnWriteBuffer& buf2) { | |
40 // Data is not shared between buffers. | |
41 const uint8_t* data1 = buf1.data(); | |
42 const uint8_t* data2 = buf2.data(); | |
43 EXPECT_NE(data1, data2); | |
44 } | |
45 | |
46 TEST(CopyOnWriteBufferTest, TestAppendData) { | |
47 CopyOnWriteBuffer buf1(kTestData, 3, 10); | |
48 CopyOnWriteBuffer buf2(buf1); | |
49 | |
50 EnsureBuffersEqual(buf1, buf2); | |
51 | |
52 // AppendData copies the underlying buffer. | |
53 buf2.AppendData("foo"); | |
54 EXPECT_EQ(buf2.size(), buf1.size() + 4); // "foo" + trailing 0x00 | |
55 EXPECT_EQ(buf2.capacity(), buf1.capacity()); | |
56 EXPECT_NE(buf2.data(), buf1.data()); | |
57 | |
58 EXPECT_EQ(buf1, CopyOnWriteBuffer(kTestData, 3)); | |
59 const int8_t exp[] = {0x0, 0x1, 0x2, 'f', 'o', 'o', 0x0}; | |
60 EXPECT_EQ(buf2, CopyOnWriteBuffer(exp)); | |
61 } | |
62 | |
63 TEST(CopyOnWriteBufferTest, TestSetData) { | |
64 CopyOnWriteBuffer buf1(kTestData, 3, 10); | |
65 CopyOnWriteBuffer buf2; | |
66 | |
67 buf2.SetData(buf1); | |
68 // buf2 shares the same data as buf1 now. | |
69 EnsureBuffersEqual(buf1, buf2); | |
70 | |
71 CopyOnWriteBuffer buf3(buf1); | |
72 // buf3 is re-allocated with new data, existing buffers are not modified. | |
73 buf3.SetData("foo"); | |
74 EXPECT_EQ(buf1, CopyOnWriteBuffer(kTestData, 3)); | |
75 EnsureBuffersEqual(buf1, buf2); | |
76 EnsureBufferDataNotEqual(buf1, buf3); | |
77 const int8_t exp[] = {'f', 'o', 'o', 0x0}; | |
78 EXPECT_EQ(buf3, CopyOnWriteBuffer(exp)); | |
79 } | |
80 | |
81 TEST(CopyOnWriteBufferTest, TestEnsureCapacity) { | |
82 CopyOnWriteBuffer buf1(kTestData, 3, 10); | |
83 CopyOnWriteBuffer buf2(buf1); | |
84 | |
85 // Smaller than existing capacity -> no change and still same contents. | |
86 buf2.EnsureCapacity(8); | |
87 EnsureBuffersEqual(buf1, buf2); | |
88 EXPECT_EQ(buf1.size(), 3u); | |
89 EXPECT_EQ(buf1.capacity(), 10u); | |
90 EXPECT_EQ(buf2.size(), 3u); | |
91 EXPECT_EQ(buf2.capacity(), 10u); | |
92 | |
93 // Lager than existing capacity -> data is cloned. | |
94 buf2.EnsureCapacity(16); | |
95 EnsureBufferDataNotEqual(buf1, buf2); | |
96 EXPECT_EQ(buf1.size(), 3u); | |
97 EXPECT_EQ(buf1.capacity(), 10u); | |
98 EXPECT_EQ(buf2.size(), 3u); | |
99 EXPECT_EQ(buf2.capacity(), 16u); | |
100 // The size and contents are still the same. | |
101 EXPECT_EQ(buf1, buf2); | |
102 } | |
103 | |
104 TEST(CopyOnWriteBufferTest, TestSetSize) { | |
105 CopyOnWriteBuffer buf1(kTestData, 3, 10); | |
106 CopyOnWriteBuffer buf2(buf1); | |
107 | |
108 buf2.SetSize(16); | |
109 EnsureBufferDataNotEqual(buf1, buf2); | |
110 EXPECT_EQ(buf1.size(), 3u); | |
111 EXPECT_EQ(buf1.capacity(), 10u); | |
112 EXPECT_EQ(buf2.size(), 16u); | |
113 EXPECT_EQ(buf2.capacity(), 16u); | |
114 // The contents got cloned. | |
115 EXPECT_EQ(0, memcmp(buf2.data(), kTestData, 3)); | |
116 } | |
117 | |
118 TEST(CopyOnWriteBufferTest, TestClear) { | |
119 CopyOnWriteBuffer buf1(kTestData, 3, 10); | |
120 CopyOnWriteBuffer buf2(buf1); | |
121 | |
122 buf2.Clear(); | |
123 EnsureBufferDataNotEqual(buf1, buf2); | |
124 EXPECT_EQ(buf1.size(), 3u); | |
125 EXPECT_EQ(buf1.capacity(), 10u); | |
126 EXPECT_EQ(0, memcmp(buf1.data(), kTestData, 3)); | |
127 EXPECT_EQ(buf2.size(), 0u); | |
128 EXPECT_EQ(buf2.capacity(), 0u); | |
129 } | |
130 | |
131 } // namespace rtc | |
OLD | NEW |