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

Unified Diff: webrtc/base/copyonwritebuffer_unittest.cc

Issue 1697743003: Add CopyOnWriteBuffer class (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Added CopyOnWriteBuffer class Created 4 years, 10 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 side-by-side diff with in-line comments
Download patch
« webrtc/base/copyonwritebuffer.cc ('K') | « webrtc/base/copyonwritebuffer.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/base/copyonwritebuffer_unittest.cc
diff --git a/webrtc/base/copyonwritebuffer_unittest.cc b/webrtc/base/copyonwritebuffer_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ca2aa7275ff7662b3be01e5d602c2ded4468bf03
--- /dev/null
+++ b/webrtc/base/copyonwritebuffer_unittest.cc
@@ -0,0 +1,131 @@
+/*
+ * Copyright 2016 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 "webrtc/base/copyonwritebuffer.h"
+#include "webrtc/base/gunit.h"
+
+namespace rtc {
+
+namespace {
+
+// 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
+
+} // namespace
+
+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.
+ const CopyOnWriteBuffer& buf2) {
+ // Data is shared between buffers.
+ EXPECT_EQ(buf1.size(), buf2.size());
+ EXPECT_EQ(buf1.capacity(), buf2.capacity());
+ const uint8_t* data1 = buf1.data();
+ const uint8_t* data2 = buf2.data();
+ EXPECT_EQ(data1, data2);
+ EXPECT_EQ(buf1, buf2);
+}
+
+// Move comparison to own function to have const buffers and prevent cloning
+// 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.
+void EnsureBufferDataNotEqual(const CopyOnWriteBuffer& buf1,
kwiberg-webrtc 2016/02/19 12:36:21 EnsureBuffersDontShareData?
joachim 2016/02/19 20:37:42 Done.
+ const CopyOnWriteBuffer& buf2) {
+ // Data is not shared between buffers.
+ const uint8_t* data1 = buf1.data();
+ const uint8_t* data2 = buf2.data();
+ EXPECT_NE(data1, data2);
+}
+
+TEST(CopyOnWriteBufferTest, TestAppendData) {
+ CopyOnWriteBuffer buf1(kTestData, 3, 10);
+ CopyOnWriteBuffer buf2(buf1);
+
+ EnsureBuffersEqual(buf1, buf2);
+
+ // AppendData copies the underlying buffer.
+ buf2.AppendData("foo");
+ EXPECT_EQ(buf2.size(), buf1.size() + 4); // "foo" + trailing 0x00
+ EXPECT_EQ(buf2.capacity(), buf1.capacity());
+ EXPECT_NE(buf2.data(), buf1.data());
+
+ EXPECT_EQ(buf1, CopyOnWriteBuffer(kTestData, 3));
+ const int8_t exp[] = {0x0, 0x1, 0x2, 'f', 'o', 'o', 0x0};
+ EXPECT_EQ(buf2, CopyOnWriteBuffer(exp));
+}
+
+TEST(CopyOnWriteBufferTest, TestSetData) {
+ CopyOnWriteBuffer buf1(kTestData, 3, 10);
+ CopyOnWriteBuffer buf2;
+
+ buf2.SetData(buf1);
+ // buf2 shares the same data as buf1 now.
+ EnsureBuffersEqual(buf1, buf2);
+
+ CopyOnWriteBuffer buf3(buf1);
+ // buf3 is re-allocated with new data, existing buffers are not modified.
+ buf3.SetData("foo");
+ EXPECT_EQ(buf1, CopyOnWriteBuffer(kTestData, 3));
+ EnsureBuffersEqual(buf1, buf2);
+ EnsureBufferDataNotEqual(buf1, buf3);
+ const int8_t exp[] = {'f', 'o', 'o', 0x0};
+ EXPECT_EQ(buf3, CopyOnWriteBuffer(exp));
+}
+
+TEST(CopyOnWriteBufferTest, TestEnsureCapacity) {
+ CopyOnWriteBuffer buf1(kTestData, 3, 10);
+ CopyOnWriteBuffer buf2(buf1);
+
+ // Smaller than existing capacity -> no change and still same contents.
+ buf2.EnsureCapacity(8);
+ EnsureBuffersEqual(buf1, buf2);
+ EXPECT_EQ(buf1.size(), 3u);
+ EXPECT_EQ(buf1.capacity(), 10u);
+ EXPECT_EQ(buf2.size(), 3u);
+ EXPECT_EQ(buf2.capacity(), 10u);
+
+ // Lager than existing capacity -> data is cloned.
+ buf2.EnsureCapacity(16);
+ EnsureBufferDataNotEqual(buf1, buf2);
+ EXPECT_EQ(buf1.size(), 3u);
+ EXPECT_EQ(buf1.capacity(), 10u);
+ EXPECT_EQ(buf2.size(), 3u);
+ EXPECT_EQ(buf2.capacity(), 16u);
+ // The size and contents are still the same.
+ EXPECT_EQ(buf1, buf2);
+}
+
+TEST(CopyOnWriteBufferTest, TestSetSize) {
+ CopyOnWriteBuffer buf1(kTestData, 3, 10);
+ CopyOnWriteBuffer buf2(buf1);
+
+ buf2.SetSize(16);
+ EnsureBufferDataNotEqual(buf1, buf2);
+ EXPECT_EQ(buf1.size(), 3u);
+ EXPECT_EQ(buf1.capacity(), 10u);
+ EXPECT_EQ(buf2.size(), 16u);
+ EXPECT_EQ(buf2.capacity(), 16u);
+ // The contents got cloned.
+ EXPECT_EQ(0, memcmp(buf2.data(), kTestData, 3));
+}
+
+TEST(CopyOnWriteBufferTest, TestClear) {
+ CopyOnWriteBuffer buf1(kTestData, 3, 10);
+ CopyOnWriteBuffer buf2(buf1);
+
+ buf2.Clear();
+ EnsureBufferDataNotEqual(buf1, buf2);
+ EXPECT_EQ(buf1.size(), 3u);
+ EXPECT_EQ(buf1.capacity(), 10u);
+ EXPECT_EQ(0, memcmp(buf1.data(), kTestData, 3));
+ EXPECT_EQ(buf2.size(), 0u);
+ EXPECT_EQ(buf2.capacity(), 0u);
+}
+
+} // namespace rtc
« webrtc/base/copyonwritebuffer.cc ('K') | « webrtc/base/copyonwritebuffer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698