This source file includes following definitions.
- isDiscardableMemorySupported
- makeTestingPlatformSupportConfig
- makePurgeableOption
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST
- TEST_P
- TEST
- TEST_P
- TEST_P
#include "config.h"
#include "platform/PurgeableVector.h"
#include "platform/TestingPlatformSupport.h"
#include "public/platform/WebDiscardableMemory.h"
#include "wtf/Vector.h"
#include <algorithm>
#include <cstdlib>
#include <gtest/gtest.h>
using namespace WebCore;
namespace {
const size_t kTestSize = 32 * 1024;
enum DiscardableMemorySupport {
DontSupportDiscardableMemory,
SupportDiscardableMemory,
};
class PurgeableVectorTestWithPlatformSupport : public testing::TestWithParam<DiscardableMemorySupport> {
public:
PurgeableVectorTestWithPlatformSupport() : m_testingPlatformSupport(makeTestingPlatformSupportConfig()) { }
protected:
bool isDiscardableMemorySupported() const { return GetParam() == SupportDiscardableMemory; }
TestingPlatformSupport::Config makeTestingPlatformSupportConfig() const
{
TestingPlatformSupport::Config config;
config.hasDiscardableMemorySupport = isDiscardableMemorySupported();
return config;
}
PurgeableVector::PurgeableOption makePurgeableOption() const
{
return isDiscardableMemorySupported() ? PurgeableVector::Purgeable : PurgeableVector::NotPurgeable;
}
private:
TestingPlatformSupport m_testingPlatformSupport;
};
TEST_P(PurgeableVectorTestWithPlatformSupport, grow)
{
PurgeableVector purgeableVector(makePurgeableOption());
purgeableVector.grow(kTestSize);
ASSERT_EQ(kTestSize, purgeableVector.size());
memset(purgeableVector.data(), 0, purgeableVector.size());
}
TEST_P(PurgeableVectorTestWithPlatformSupport, clear)
{
Vector<char> testData(kTestSize);
std::generate(testData.begin(), testData.end(), &std::rand);
PurgeableVector purgeableVector(makePurgeableOption());
purgeableVector.append(testData.data(), testData.size());
EXPECT_EQ(testData.size(), purgeableVector.size());
purgeableVector.clear();
EXPECT_EQ(0U, purgeableVector.size());
EXPECT_EQ(0, purgeableVector.data());
}
TEST_P(PurgeableVectorTestWithPlatformSupport, clearDoesNotResetLockCounter)
{
PurgeableVector purgeableVector(makePurgeableOption());
purgeableVector.clear();
EXPECT_TRUE(purgeableVector.isLocked());
purgeableVector.unlock();
EXPECT_FALSE(purgeableVector.isLocked());
}
TEST_P(PurgeableVectorTestWithPlatformSupport, reserveCapacityDoesNotChangeSize)
{
PurgeableVector purgeableVector(makePurgeableOption());
EXPECT_EQ(0U, purgeableVector.size());
purgeableVector.reserveCapacity(kTestSize);
EXPECT_EQ(0U, purgeableVector.size());
}
TEST_P(PurgeableVectorTestWithPlatformSupport, multipleAppends)
{
Vector<char> testData(kTestSize);
std::generate(testData.begin(), testData.end(), &std::rand);
PurgeableVector purgeableVector(makePurgeableOption());
const char kSmallString[] = "hello";
purgeableVector.append(kSmallString, sizeof(kSmallString));
const char* const data = purgeableVector.data();
const size_t kIterationCount = 4;
ASSERT_EQ(0U, testData.size() % kIterationCount);
for (size_t i = 0; i < kIterationCount; ++i) {
const char* const testDataStart = testData.data() + i * (testData.size() / kIterationCount);
purgeableVector.append(testDataStart, testData.size() / kIterationCount);
ASSERT_EQ((i + 1) * testData.size() / kIterationCount, purgeableVector.size() - sizeof(kSmallString));
}
ASSERT_EQ(sizeof(kSmallString) + testData.size(), purgeableVector.size());
EXPECT_NE(data, purgeableVector.data());
EXPECT_EQ(0, memcmp(purgeableVector.data() + sizeof(kSmallString), testData.data(), testData.size()));
}
TEST_P(PurgeableVectorTestWithPlatformSupport, multipleAppendsAfterReserveCapacity)
{
Vector<char> testData(kTestSize);
std::generate(testData.begin(), testData.end(), &std::rand);
PurgeableVector purgeableVector(makePurgeableOption());
purgeableVector.reserveCapacity(testData.size());
const char* const data = purgeableVector.data();
const size_t kIterationCount = 4;
ASSERT_EQ(0U, testData.size() % kIterationCount);
for (size_t i = 0; i < kIterationCount; ++i) {
const char* const testDataStart = testData.data() + i * (testData.size() / kIterationCount);
purgeableVector.append(testDataStart, testData.size() / kIterationCount);
ASSERT_EQ((i + 1) * testData.size() / kIterationCount, purgeableVector.size());
}
ASSERT_EQ(testData.size(), purgeableVector.size());
EXPECT_EQ(data, purgeableVector.data());
EXPECT_EQ(0, memcmp(purgeableVector.data(), testData.data(), testData.size()));
}
TEST_P(PurgeableVectorTestWithPlatformSupport, reserveCapacityUsesExactCapacityWhenVectorIsEmpty)
{
Vector<char> testData(kTestSize);
std::generate(testData.begin(), testData.end(), &std::rand);
PurgeableVector purgeableVector(makePurgeableOption());
purgeableVector.reserveCapacity(kTestSize);
const char* const data = purgeableVector.data();
purgeableVector.append(testData.data(), testData.size());
EXPECT_EQ(data, purgeableVector.data());
EXPECT_EQ(0, memcmp(purgeableVector.data(), testData.data(), testData.size()));
if (isDiscardableMemorySupported()) {
purgeableVector.append(testData.data(), 1);
EXPECT_NE(data, purgeableVector.data());
}
}
TEST_P(PurgeableVectorTestWithPlatformSupport, appendReservesCapacityIfNeeded)
{
Vector<char> testData(kTestSize);
std::generate(testData.begin(), testData.end(), &std::rand);
PurgeableVector purgeableVector(makePurgeableOption());
ASSERT_FALSE(purgeableVector.data());
purgeableVector.append(testData.data(), testData.size());
ASSERT_EQ(testData.size(), purgeableVector.size());
ASSERT_EQ(0, memcmp(purgeableVector.data(), testData.data(), testData.size()));
}
TEST_P(PurgeableVectorTestWithPlatformSupport, adopt)
{
Vector<char> testData(kTestSize);
std::generate(testData.begin(), testData.end(), &std::rand);
const Vector<char> testDataCopy(testData);
const char* const testDataPtr = testData.data();
PurgeableVector purgeableVector(makePurgeableOption());
purgeableVector.adopt(testData);
EXPECT_TRUE(testData.isEmpty());
EXPECT_EQ(kTestSize, purgeableVector.size());
ASSERT_EQ(0, memcmp(purgeableVector.data(), testDataCopy.data(), testDataCopy.size()));
if (isDiscardableMemorySupported()) {
EXPECT_NE(testDataPtr, purgeableVector.data());
} else {
EXPECT_EQ(testDataPtr, purgeableVector.data());
}
}
TEST_P(PurgeableVectorTestWithPlatformSupport, adoptEmptyVector)
{
Vector<char> testData;
PurgeableVector purgeableVector(makePurgeableOption());
purgeableVector.adopt(testData);
}
TEST(PurgeableVectorTestWithPlatformSupport, adoptDiscardsPreviousData)
{
Vector<char> testData;
std::generate(testData.begin(), testData.end(), &std::rand);
PurgeableVector purgeableVector(PurgeableVector::NotPurgeable);
static const char smallString[] = "hello";
purgeableVector.append(smallString, sizeof(smallString));
ASSERT_EQ(0, memcmp(purgeableVector.data(), smallString, sizeof(smallString)));
purgeableVector.adopt(testData);
EXPECT_EQ(testData.size(), purgeableVector.size());
ASSERT_EQ(0, memcmp(purgeableVector.data(), testData.data(), testData.size()));
}
TEST_P(PurgeableVectorTestWithPlatformSupport, unlockWithoutHintAtConstruction)
{
Vector<char> testData(30000);
std::generate(testData.begin(), testData.end(), &std::rand);
unsigned length = testData.size();
PurgeableVector purgeableVector(PurgeableVector::NotPurgeable);
purgeableVector.append(testData.data(), length);
ASSERT_EQ(length, purgeableVector.size());
const char* data = purgeableVector.data();
purgeableVector.unlock();
const bool wasPurged = !purgeableVector.lock();
if (isDiscardableMemorySupported()) {
EXPECT_TRUE(wasPurged);
}
if (isDiscardableMemorySupported()) {
ASSERT_NE(data, purgeableVector.data());
} else {
ASSERT_EQ(data, purgeableVector.data());
}
if (!wasPurged)
ASSERT_EQ(0, memcmp(purgeableVector.data(), testData.data(), length));
}
TEST(PurgeableVectorTest, unlockOnEmptyPurgeableVector)
{
PurgeableVector purgeableVector;
ASSERT_EQ(0U, purgeableVector.size());
purgeableVector.unlock();
ASSERT_FALSE(purgeableVector.isLocked());
}
TEST_P(PurgeableVectorTestWithPlatformSupport, unlockOnPurgeableVectorWithPurgeableHint)
{
Vector<char> testData(kTestSize);
std::generate(testData.begin(), testData.end(), &std::rand);
PurgeableVector purgeableVector;
purgeableVector.append(testData.data(), kTestSize);
const char* const data = purgeableVector.data();
purgeableVector.unlock();
EXPECT_FALSE(purgeableVector.isLocked());
purgeableVector.lock();
EXPECT_TRUE(purgeableVector.isLocked());
EXPECT_EQ(data, purgeableVector.data());
}
TEST_P(PurgeableVectorTestWithPlatformSupport, lockingUsesACounter)
{
Vector<char> testData(kTestSize);
std::generate(testData.begin(), testData.end(), &std::rand);
PurgeableVector purgeableVector(PurgeableVector::NotPurgeable);
purgeableVector.append(testData.data(), testData.size());
ASSERT_EQ(testData.size(), purgeableVector.size());
ASSERT_TRUE(purgeableVector.isLocked());
ASSERT_TRUE(purgeableVector.lock());
ASSERT_TRUE(purgeableVector.isLocked());
purgeableVector.unlock();
ASSERT_TRUE(purgeableVector.isLocked());
purgeableVector.unlock();
ASSERT_FALSE(purgeableVector.isLocked());
if (purgeableVector.lock())
ASSERT_EQ(0, memcmp(purgeableVector.data(), testData.data(), testData.size()));
}
INSTANTIATE_TEST_CASE_P(testsWithPlatformSetUp, PurgeableVectorTestWithPlatformSupport,
::testing::Values(DontSupportDiscardableMemory, SupportDiscardableMemory));
}