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

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

Issue 2078873005: rtc::Buffer: Grow capacity by at least 1.5x to prevent quadratic behavior (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 6 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
« webrtc/base/buffer.h ('K') | « webrtc/base/buffer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 Buffer buf(kTestData); 61 Buffer buf(kTestData);
62 EXPECT_EQ(buf.size(), 16u); 62 EXPECT_EQ(buf.size(), 16u);
63 EXPECT_EQ(buf.capacity(), 16u); 63 EXPECT_EQ(buf.capacity(), 16u);
64 EXPECT_EQ(0, memcmp(buf.data(), kTestData, 16)); 64 EXPECT_EQ(0, memcmp(buf.data(), kTestData, 16));
65 } 65 }
66 66
67 TEST(BufferTest, TestSetData) { 67 TEST(BufferTest, TestSetData) {
68 Buffer buf(kTestData + 4, 7); 68 Buffer buf(kTestData + 4, 7);
69 buf.SetData(kTestData, 9); 69 buf.SetData(kTestData, 9);
70 EXPECT_EQ(buf.size(), 9u); 70 EXPECT_EQ(buf.size(), 9u);
71 EXPECT_EQ(buf.capacity(), 9u); 71 EXPECT_EQ(buf.capacity(), 7u * 3 / 2);
72 EXPECT_EQ(0, memcmp(buf.data(), kTestData, 9)); 72 EXPECT_EQ(0, memcmp(buf.data(), kTestData, 9));
73 } 73 }
74 74
75 TEST(BufferTest, TestAppendData) { 75 TEST(BufferTest, TestAppendData) {
76 Buffer buf(kTestData + 4, 3); 76 Buffer buf(kTestData + 4, 3);
77 buf.AppendData(kTestData + 10, 2); 77 buf.AppendData(kTestData + 10, 2);
78 const int8_t exp[] = {0x4, 0x5, 0x6, 0xa, 0xb}; 78 const int8_t exp[] = {0x4, 0x5, 0x6, 0xa, 0xb};
79 EXPECT_EQ(buf, Buffer(exp)); 79 EXPECT_EQ(buf, Buffer(exp));
80 } 80 }
81 81
82 TEST(BufferTest, TestSetSizeSmaller) { 82 TEST(BufferTest, TestSetSizeSmaller) {
83 Buffer buf; 83 Buffer buf;
84 buf.SetData(kTestData, 15); 84 buf.SetData(kTestData, 15);
85 buf.SetSize(10); 85 buf.SetSize(10);
86 EXPECT_EQ(buf.size(), 10u); 86 EXPECT_EQ(buf.size(), 10u);
87 EXPECT_EQ(buf.capacity(), 15u); // Hasn't shrunk. 87 EXPECT_EQ(buf.capacity(), 15u); // Hasn't shrunk.
88 EXPECT_EQ(buf, Buffer(kTestData, 10)); 88 EXPECT_EQ(buf, Buffer(kTestData, 10));
89 } 89 }
90 90
91 TEST(BufferTest, TestSetSizeLarger) { 91 TEST(BufferTest, TestSetSizeLarger) {
92 Buffer buf; 92 Buffer buf;
93 buf.SetData(kTestData, 15); 93 buf.SetData(kTestData, 15);
94 EXPECT_EQ(buf.size(), 15u); 94 EXPECT_EQ(buf.size(), 15u);
95 EXPECT_EQ(buf.capacity(), 15u); 95 EXPECT_EQ(buf.capacity(), 15u);
96 buf.SetSize(20); 96 buf.SetSize(20);
97 EXPECT_EQ(buf.size(), 20u); 97 EXPECT_EQ(buf.size(), 20u);
98 EXPECT_EQ(buf.capacity(), 20u); // Has grown. 98 EXPECT_EQ(buf.capacity(), 15u * 3 / 2); // Has grown.
99 EXPECT_EQ(0, memcmp(buf.data(), kTestData, 15)); 99 EXPECT_EQ(0, memcmp(buf.data(), kTestData, 15));
100 } 100 }
101 101
102 TEST(BufferTest, TestEnsureCapacitySmaller) { 102 TEST(BufferTest, TestEnsureCapacitySmaller) {
103 Buffer buf(kTestData); 103 Buffer buf(kTestData);
104 const char* data = buf.data<char>(); 104 const char* data = buf.data<char>();
105 buf.EnsureCapacity(4); 105 buf.EnsureCapacity(4);
106 EXPECT_EQ(buf.capacity(), 16u); // Hasn't shrunk. 106 EXPECT_EQ(buf.capacity(), 16u); // Hasn't shrunk.
107 EXPECT_EQ(buf.data<char>(), data); // No reallocation. 107 EXPECT_EQ(buf.data<char>(), data); // No reallocation.
108 EXPECT_EQ(buf, Buffer(kTestData)); 108 EXPECT_EQ(buf, Buffer(kTestData));
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 BufferT<BloodStone*> buf2(4); 351 BufferT<BloodStone*> buf2(4);
352 for (size_t i = 0; i < buf2.size(); ++i) { 352 for (size_t i = 0; i < buf2.size(); ++i) {
353 buf2[i] = &buf[i]; 353 buf2[i] = &buf[i];
354 } 354 }
355 static const char kObsidian[] = "obsidian"; 355 static const char kObsidian[] = "obsidian";
356 buf2[2]->stone = kObsidian; 356 buf2[2]->stone = kObsidian;
357 EXPECT_EQ(kObsidian, buf[2].stone); 357 EXPECT_EQ(kObsidian, buf[2].stone);
358 } 358 }
359 359
360 } // namespace rtc 360 } // namespace rtc
OLDNEW
« webrtc/base/buffer.h ('K') | « webrtc/base/buffer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698