OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2014 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 | 58 |
59 PurgeableVector purgeableVector(GetParam()); | 59 PurgeableVector purgeableVector(GetParam()); |
60 purgeableVector.append(testData.data(), testData.size()); | 60 purgeableVector.append(testData.data(), testData.size()); |
61 EXPECT_EQ(testData.size(), purgeableVector.size()); | 61 EXPECT_EQ(testData.size(), purgeableVector.size()); |
62 | 62 |
63 purgeableVector.clear(); | 63 purgeableVector.clear(); |
64 EXPECT_EQ(0U, purgeableVector.size()); | 64 EXPECT_EQ(0U, purgeableVector.size()); |
65 EXPECT_EQ(0, purgeableVector.data()); | 65 EXPECT_EQ(0, purgeableVector.data()); |
66 } | 66 } |
67 | 67 |
68 TEST_P(PurgeableVectorTestWithDiscardableMemory, clearDoesNotResetLockCounter) | |
69 { | |
70 PurgeableVector purgeableVector(GetParam()); | |
71 purgeableVector.clear(); | |
72 EXPECT_TRUE(purgeableVector.isLocked()); | |
73 purgeableVector.unlock(); | |
74 EXPECT_FALSE(purgeableVector.isLocked()); | |
75 } | |
76 | |
77 TEST_P(PurgeableVectorTestWithDiscardableMemory, reserveCapacityDoesNotChangeSiz
e) | 68 TEST_P(PurgeableVectorTestWithDiscardableMemory, reserveCapacityDoesNotChangeSiz
e) |
78 { | 69 { |
79 PurgeableVector purgeableVector(GetParam()); | 70 PurgeableVector purgeableVector(GetParam()); |
80 EXPECT_EQ(0U, purgeableVector.size()); | 71 EXPECT_EQ(0U, purgeableVector.size()); |
81 purgeableVector.reserveCapacity(kTestSize); | 72 purgeableVector.reserveCapacity(kTestSize); |
82 EXPECT_EQ(0U, purgeableVector.size()); | 73 EXPECT_EQ(0U, purgeableVector.size()); |
83 } | 74 } |
84 | 75 |
85 TEST_P(PurgeableVectorTestWithDiscardableMemory, multipleAppends) | 76 TEST_P(PurgeableVectorTestWithDiscardableMemory, multipleAppends) |
86 { | 77 { |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 PurgeableVector purgeableVector(PurgeableVector::NotPurgeable); | 200 PurgeableVector purgeableVector(PurgeableVector::NotPurgeable); |
210 static const char smallString[] = "hello"; | 201 static const char smallString[] = "hello"; |
211 purgeableVector.append(smallString, sizeof(smallString)); | 202 purgeableVector.append(smallString, sizeof(smallString)); |
212 ASSERT_EQ(0, memcmp(purgeableVector.data(), smallString, sizeof(smallString)
)); | 203 ASSERT_EQ(0, memcmp(purgeableVector.data(), smallString, sizeof(smallString)
)); |
213 | 204 |
214 purgeableVector.adopt(testData); | 205 purgeableVector.adopt(testData); |
215 EXPECT_EQ(testData.size(), purgeableVector.size()); | 206 EXPECT_EQ(testData.size(), purgeableVector.size()); |
216 ASSERT_EQ(0, memcmp(purgeableVector.data(), testData.data(), testData.size()
)); | 207 ASSERT_EQ(0, memcmp(purgeableVector.data(), testData.data(), testData.size()
)); |
217 } | 208 } |
218 | 209 |
219 TEST(PurgeableVectorTest, unlockWithoutHintAtConstruction) | |
220 { | |
221 Vector<char> testData(30000); | |
222 std::generate(testData.begin(), testData.end(), &std::rand); | |
223 | |
224 unsigned length = testData.size(); | |
225 PurgeableVector purgeableVector(PurgeableVector::NotPurgeable); | |
226 purgeableVector.append(testData.data(), length); | |
227 ASSERT_EQ(length, purgeableVector.size()); | |
228 const char* data = purgeableVector.data(); | |
229 | |
230 purgeableVector.unlock(); | |
231 | |
232 // Note that the purgeable vector must be locked before calling data(). | |
233 const bool wasPurged = !purgeableVector.lock(); | |
234 | |
235 // The implementation of purgeable memory used for testing always purges dat
a upon unlock(). | |
236 EXPECT_TRUE(wasPurged); | |
237 | |
238 // The data should have been moved from the heap-allocated vector to a purge
able buffer. | |
239 ASSERT_NE(data, purgeableVector.data()); | |
240 | |
241 if (!wasPurged) | |
242 ASSERT_EQ(0, memcmp(purgeableVector.data(), testData.data(), length)); | |
243 } | |
244 | |
245 TEST(PurgeableVectorTest, unlockOnEmptyPurgeableVector) | |
246 { | |
247 PurgeableVector purgeableVector; | |
248 ASSERT_EQ(0U, purgeableVector.size()); | |
249 purgeableVector.unlock(); | |
250 ASSERT_FALSE(purgeableVector.isLocked()); | |
251 } | |
252 | |
253 TEST(PurgeableVectorTest, unlockOnPurgeableVectorWithPurgeableHint) | |
254 { | |
255 Vector<char> testData(kTestSize); | |
256 std::generate(testData.begin(), testData.end(), &std::rand); | |
257 | |
258 PurgeableVector purgeableVector; | |
259 purgeableVector.append(testData.data(), kTestSize); | |
260 const char* const data = purgeableVector.data(); | |
261 | |
262 // unlock() should happen in place, i.e. without causing any reallocation. | |
263 // Note that the instance must be locked when data() is called. | |
264 purgeableVector.unlock(); | |
265 EXPECT_FALSE(purgeableVector.isLocked()); | |
266 purgeableVector.lock(); | |
267 EXPECT_TRUE(purgeableVector.isLocked()); | |
268 EXPECT_EQ(data, purgeableVector.data()); | |
269 } | |
270 | |
271 TEST(PurgeableVectorTest, lockingUsesACounter) | |
272 { | |
273 Vector<char> testData(kTestSize); | |
274 std::generate(testData.begin(), testData.end(), &std::rand); | |
275 | |
276 PurgeableVector purgeableVector(PurgeableVector::NotPurgeable); | |
277 purgeableVector.append(testData.data(), testData.size()); | |
278 ASSERT_EQ(testData.size(), purgeableVector.size()); | |
279 | |
280 ASSERT_TRUE(purgeableVector.isLocked()); // PurgeableVector is locked at cre
ation. | |
281 ASSERT_TRUE(purgeableVector.lock()); // Add an extra lock. | |
282 ASSERT_TRUE(purgeableVector.isLocked()); | |
283 | |
284 purgeableVector.unlock(); | |
285 ASSERT_TRUE(purgeableVector.isLocked()); | |
286 | |
287 purgeableVector.unlock(); | |
288 ASSERT_FALSE(purgeableVector.isLocked()); | |
289 | |
290 if (purgeableVector.lock()) | |
291 ASSERT_EQ(0, memcmp(purgeableVector.data(), testData.data(), testData.si
ze())); | |
292 } | |
293 | |
294 // Instantiates all the unit tests using the PurgeableVectorTestWithDiscardableM
emory | 210 // Instantiates all the unit tests using the PurgeableVectorTestWithDiscardableM
emory |
295 // fixture both with and without using discardable memory support. | 211 // fixture both with and without using discardable memory support. |
296 INSTANTIATE_TEST_CASE_P(, PurgeableVectorTestWithDiscardableMemory, | 212 INSTANTIATE_TEST_CASE_P(, PurgeableVectorTestWithDiscardableMemory, |
297 ::testing::Values(PurgeableVector::NotPurgeable, PurgeableVector::Purgeable)
); | 213 ::testing::Values(PurgeableVector::NotPurgeable, PurgeableVector::Purgeable)
); |
298 | 214 |
299 } // namespace blink | 215 } // namespace blink |
OLD | NEW |