This source file includes following definitions.
- getFreeListSize
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "config.h"
#include "platform/PODFreeListArena.h"
#include "platform/testing/ArenaTestHelpers.h"
#include "wtf/FastMalloc.h"
#include "wtf/RefPtr.h"
#include "wtf/Vector.h"
#include <gtest/gtest.h>
namespace WebCore {
using ArenaTestHelpers::TrackedAllocator;
namespace {
struct TestClass1 {
TestClass1()
: x(0), y(0), z(0), w(1) { }
float x, y, z, w;
};
struct TestClass2 {
TestClass2()
: padding(0)
{
static int TestIds = 0;
id = TestIds++;
}
int id;
int padding;
};
}
class PODFreeListArenaTest : public testing::Test {
protected:
int getFreeListSize(const PassRefPtr<PODFreeListArena<TestClass1> > arena) const
{
return arena->getFreeListSizeForTesting();
}
};
TEST_F(PODFreeListArenaTest, CanAllocateFromMoreThanOneRegion)
{
RefPtr<TrackedAllocator> allocator = TrackedAllocator::create();
RefPtr<PODFreeListArena<TestClass1> > arena = PODFreeListArena<TestClass1>::create(allocator);
int numIterations = 10 * PODArena::DefaultChunkSize / sizeof(TestClass1);
for (int i = 0; i < numIterations; ++i)
arena->allocateObject();
EXPECT_GT(allocator->numRegions(), 1);
}
TEST_F(PODFreeListArenaTest, FreesAllAllocatedRegions)
{
RefPtr<TrackedAllocator> allocator = TrackedAllocator::create();
{
RefPtr<PODFreeListArena<TestClass1> > arena = PODFreeListArena<TestClass1>::create(allocator);
for (int i = 0; i < 3; i++)
arena->allocateObject();
EXPECT_GT(allocator->numRegions(), 0);
}
EXPECT_TRUE(allocator->isEmpty());
}
TEST_F(PODFreeListArenaTest, RunsConstructorsOnNewObjects)
{
RefPtr<PODFreeListArena<TestClass1> > arena = PODFreeListArena<TestClass1>::create();
for (int i = 0; i < 10000; i++) {
TestClass1* tc1 = arena->allocateObject();
EXPECT_EQ(0, tc1->x);
EXPECT_EQ(0, tc1->y);
EXPECT_EQ(0, tc1->z);
EXPECT_EQ(1, tc1->w);
}
}
TEST_F(PODFreeListArenaTest, RunsConstructorsOnReusedObjects)
{
std::set<TestClass1*> objects;
RefPtr<PODFreeListArena<TestClass1> > arena = PODFreeListArena<TestClass1>::create();
for (int i = 0; i < 100; i++) {
TestClass1* tc1 = arena->allocateObject();
tc1->x = 100;
tc1->y = 101;
tc1->z = 102;
tc1->w = 103;
objects.insert(tc1);
}
for (std::set<TestClass1*>::iterator it = objects.begin(); it != objects.end(); ++it) {
arena->freeObject(*it);
}
for (int i = 0; i < 100; i++) {
TestClass1* cur = arena->allocateObject();
EXPECT_TRUE(objects.find(cur) != objects.end());
EXPECT_EQ(0, cur->x);
EXPECT_EQ(0, cur->y);
EXPECT_EQ(0, cur->z);
EXPECT_EQ(1, cur->w);
objects.erase(cur);
}
}
TEST_F(PODFreeListArenaTest, AddsFreedObjectsToFreedList)
{
std::vector<TestClass1*> objects;
RefPtr<PODFreeListArena<TestClass1> > arena = PODFreeListArena<TestClass1>::create();
for (int i = 0; i < 100; i++) {
objects.push_back(arena->allocateObject());
}
for (std::vector<TestClass1*>::iterator it = objects.begin(); it != objects.end(); ++it) {
arena->freeObject(*it);
}
EXPECT_EQ(100, getFreeListSize(arena));
}
TEST_F(PODFreeListArenaTest, ReusesPreviouslyFreedObjects)
{
std::set<TestClass2*> objects;
RefPtr<PODFreeListArena<TestClass2> > arena = PODFreeListArena<TestClass2>::create();
for (int i = 0; i < 100; i++) {
objects.insert(arena->allocateObject());
}
for (std::set<TestClass2*>::iterator it = objects.begin(); it != objects.end(); ++it) {
arena->freeObject(*it);
}
for (int i = 0; i < 100; i++) {
TestClass2* cur = arena->allocateObject();
EXPECT_TRUE(objects.find(cur) != objects.end());
EXPECT_TRUE(cur->id >= 100 && cur->id < 200);
objects.erase(cur);
}
}
}