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

Side by Side Diff: third_party/WebKit/Source/platform/PurgeableVectorTest.cpp

Issue 2247073007: Make SharedBuffer in Resource non-discardable and remove PurgeableVector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@SharedBuffer_DoNotUnlock
Patch Set: Rebase. Created 4 years, 3 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
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2014 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "platform/PurgeableVector.h"
32
33 #include "testing/gtest/include/gtest/gtest.h"
34 #include "wtf/Vector.h"
35 #include <algorithm>
36 #include <cstdlib>
37
38 namespace blink {
39
40 const size_t kTestSize = 32 * 1024;
41
42 class PurgeableVectorTestWithDiscardableMemory : public testing::TestWithParam<P urgeableVector::PurgeableOption> {
43 };
44
45 TEST_P(PurgeableVectorTestWithDiscardableMemory, grow)
46 {
47 PurgeableVector purgeableVector(GetParam());
48 purgeableVector.grow(kTestSize);
49 ASSERT_EQ(kTestSize, purgeableVector.size());
50 // Make sure the underlying buffer was actually (re)allocated.
51 memset(purgeableVector.data(), 0, purgeableVector.size());
52 }
53
54 TEST_P(PurgeableVectorTestWithDiscardableMemory, clear)
55 {
56 Vector<char> testData(kTestSize);
57 std::generate(testData.begin(), testData.end(), &std::rand);
58
59 PurgeableVector purgeableVector(GetParam());
60 purgeableVector.append(testData.data(), testData.size());
61 EXPECT_EQ(testData.size(), purgeableVector.size());
62
63 purgeableVector.clear();
64 EXPECT_EQ(0U, purgeableVector.size());
65 EXPECT_EQ(0, purgeableVector.data());
66 }
67
68 TEST_P(PurgeableVectorTestWithDiscardableMemory, reserveCapacityDoesNotChangeSiz e)
69 {
70 PurgeableVector purgeableVector(GetParam());
71 EXPECT_EQ(0U, purgeableVector.size());
72 purgeableVector.reserveCapacity(kTestSize);
73 EXPECT_EQ(0U, purgeableVector.size());
74 }
75
76 TEST_P(PurgeableVectorTestWithDiscardableMemory, multipleAppends)
77 {
78 Vector<char> testData(kTestSize);
79 std::generate(testData.begin(), testData.end(), &std::rand);
80
81 PurgeableVector purgeableVector(GetParam());
82 // Force an allocation.
83 const char kSmallString[] = "hello";
84 purgeableVector.append(kSmallString, sizeof(kSmallString));
85 const char* const data = purgeableVector.data();
86
87 // Append all the testing data in 4 iterations. The |data| pointer should
88 // have been changed at the end of the unit test due to reallocations.
89 const size_t kIterationCount = 4;
90 ASSERT_EQ(0U, testData.size() % kIterationCount);
91 for (size_t i = 0; i < kIterationCount; ++i) {
92 const char* const testDataStart = testData.data() + i * (testData.size() / kIterationCount);
93 purgeableVector.append(testDataStart, testData.size() / kIterationCount) ;
94 ASSERT_EQ((i + 1) * testData.size() / kIterationCount, purgeableVector.s ize() - sizeof(kSmallString));
95 }
96
97 ASSERT_EQ(sizeof(kSmallString) + testData.size(), purgeableVector.size());
98 EXPECT_NE(data, purgeableVector.data());
99 EXPECT_EQ(0, memcmp(purgeableVector.data() + sizeof(kSmallString), testData. data(), testData.size()));
100 }
101
102 TEST_P(PurgeableVectorTestWithDiscardableMemory, multipleAppendsAfterReserveCapa city)
103 {
104 Vector<char> testData(kTestSize);
105 std::generate(testData.begin(), testData.end(), &std::rand);
106
107 PurgeableVector purgeableVector(GetParam());
108 purgeableVector.reserveCapacity(testData.size());
109 const char* const data = purgeableVector.data();
110
111 // The |data| pointer should be unchanged at the end of the unit test
112 // meaning that there should not have been any reallocation.
113 const size_t kIterationCount = 4;
114 ASSERT_EQ(0U, testData.size() % kIterationCount);
115 for (size_t i = 0; i < kIterationCount; ++i) {
116 const char* const testDataStart = testData.data() + i * (testData.size() / kIterationCount);
117 purgeableVector.append(testDataStart, testData.size() / kIterationCount) ;
118 ASSERT_EQ((i + 1) * testData.size() / kIterationCount, purgeableVector.s ize());
119 }
120
121 ASSERT_EQ(testData.size(), purgeableVector.size());
122 EXPECT_EQ(data, purgeableVector.data());
123 EXPECT_EQ(0, memcmp(purgeableVector.data(), testData.data(), testData.size() ));
124 }
125
126 TEST_P(PurgeableVectorTestWithDiscardableMemory, reserveCapacityUsesExactCapacit yWhenVectorIsEmpty)
127 {
128 Vector<char> testData(kTestSize);
129 std::generate(testData.begin(), testData.end(), &std::rand);
130
131 PurgeableVector purgeableVector(GetParam());
132 purgeableVector.reserveCapacity(kTestSize);
133 const char* const data = purgeableVector.data();
134
135 purgeableVector.append(testData.data(), testData.size());
136 EXPECT_EQ(data, purgeableVector.data());
137 EXPECT_EQ(0, memcmp(purgeableVector.data(), testData.data(), testData.size() ));
138
139 // This test is not reliable if the PurgeableVector uses a plain WTF::Vector
140 // for storage, as it does if discardable memory is not supported; the vecto rs
141 // capacity will always be expanded to fill the PartitionAlloc bucket.
142 if (GetParam() == PurgeableVector::Purgeable) {
143 // Appending one extra byte should cause a reallocation since the first
144 // allocation happened while the purgeable vector was empty. This behavi or
145 // helps us guarantee that there is no memory waste on very small vector s
146 // (which SharedBuffer requires).
147 purgeableVector.append(testData.data(), 1);
148 EXPECT_NE(data, purgeableVector.data());
149 }
150 }
151
152 TEST_P(PurgeableVectorTestWithDiscardableMemory, appendReservesCapacityIfNeeded)
153 {
154 Vector<char> testData(kTestSize);
155 std::generate(testData.begin(), testData.end(), &std::rand);
156
157 PurgeableVector purgeableVector(GetParam());
158 // No reserveCapacity().
159 ASSERT_FALSE(purgeableVector.data());
160
161 purgeableVector.append(testData.data(), testData.size());
162 ASSERT_EQ(testData.size(), purgeableVector.size());
163 ASSERT_EQ(0, memcmp(purgeableVector.data(), testData.data(), testData.size() ));
164 }
165
166 TEST_P(PurgeableVectorTestWithDiscardableMemory, adopt)
167 {
168 Vector<char> testData(kTestSize);
169 std::generate(testData.begin(), testData.end(), &std::rand);
170 const Vector<char> testDataCopy(testData);
171 const char* const testDataPtr = testData.data();
172
173 PurgeableVector purgeableVector(GetParam());
174 purgeableVector.adopt(testData);
175 EXPECT_TRUE(testData.isEmpty());
176 EXPECT_EQ(kTestSize, purgeableVector.size());
177 ASSERT_EQ(0, memcmp(purgeableVector.data(), testDataCopy.data(), testDataCop y.size()));
178
179 if (GetParam() == PurgeableVector::Purgeable) {
180 // An extra discardable memory allocation + memcpy() should have happene d.
181 EXPECT_NE(testDataPtr, purgeableVector.data());
182 } else {
183 // Vector::swap() should have been used.
184 EXPECT_EQ(testDataPtr, purgeableVector.data());
185 }
186 }
187
188 TEST_P(PurgeableVectorTestWithDiscardableMemory, adoptEmptyVector)
189 {
190 Vector<char> testData;
191 PurgeableVector purgeableVector(GetParam());
192 purgeableVector.adopt(testData);
193 }
194
195 TEST(PurgeableVectorTest, adoptDiscardsPreviousData)
196 {
197 Vector<char> testData;
198 std::generate(testData.begin(), testData.end(), &std::rand);
199
200 PurgeableVector purgeableVector(PurgeableVector::NotPurgeable);
201 static const char smallString[] = "hello";
202 purgeableVector.append(smallString, sizeof(smallString));
203 ASSERT_EQ(0, memcmp(purgeableVector.data(), smallString, sizeof(smallString) ));
204
205 purgeableVector.adopt(testData);
206 EXPECT_EQ(testData.size(), purgeableVector.size());
207 ASSERT_EQ(0, memcmp(purgeableVector.data(), testData.data(), testData.size() ));
208 }
209
210 // Instantiates all the unit tests using the PurgeableVectorTestWithDiscardableM emory
211 // fixture both with and without using discardable memory support.
212 INSTANTIATE_TEST_CASE_P(, PurgeableVectorTestWithDiscardableMemory,
213 ::testing::Values(PurgeableVector::NotPurgeable, PurgeableVector::Purgeable) );
214
215 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/PurgeableVector.cpp ('k') | third_party/WebKit/Source/platform/SharedBuffer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698