This source file includes following definitions.
- SetUp
- TearDown
- id_allocator
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "gpu/command_buffer/common/id_allocator.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gpu {
class IdAllocatorTest : public testing::Test {
protected:
virtual void SetUp() {}
virtual void TearDown() {}
IdAllocator* id_allocator() { return &id_allocator_; }
private:
IdAllocator id_allocator_;
};
TEST_F(IdAllocatorTest, TestBasic) {
IdAllocator *allocator = id_allocator();
EXPECT_FALSE(allocator->InUse(1));
ResourceId id1 = allocator->AllocateID();
EXPECT_TRUE(allocator->InUse(id1));
ResourceId id2 = allocator->AllocateID();
EXPECT_TRUE(allocator->InUse(id2));
EXPECT_NE(id1, id2);
allocator->FreeID(id1);
EXPECT_FALSE(allocator->InUse(id1));
allocator->FreeID(id2);
EXPECT_FALSE(allocator->InUse(id2));
}
TEST_F(IdAllocatorTest, TestAdvanced) {
IdAllocator *allocator = id_allocator();
allocator->AllocateIDAtOrAbove(-1);
const unsigned int kNumResources = 100;
ResourceId ids[kNumResources];
for (unsigned int i = 0; i < kNumResources; ++i) {
ids[i] = allocator->AllocateID();
EXPECT_TRUE(allocator->InUse(ids[i]));
}
ResourceId id1 = ids[kNumResources / 2];
allocator->FreeID(id1);
EXPECT_FALSE(allocator->InUse(id1));
ResourceId id2 = allocator->AllocateID();
EXPECT_TRUE(allocator->InUse(id2));
EXPECT_EQ(id1, id2);
}
TEST_F(IdAllocatorTest, MarkAsUsed) {
IdAllocator* allocator = id_allocator();
ResourceId id = allocator->AllocateID();
allocator->FreeID(id);
EXPECT_FALSE(allocator->InUse(id));
EXPECT_TRUE(allocator->MarkAsUsed(id));
EXPECT_TRUE(allocator->InUse(id));
ResourceId id2 = allocator->AllocateID();
EXPECT_NE(id, id2);
EXPECT_TRUE(allocator->MarkAsUsed(id2 + 1));
ResourceId id3 = allocator->AllocateID();
EXPECT_EQ(id3, id2 + 2);
}
TEST_F(IdAllocatorTest, AllocateIdAtOrAbove) {
const ResourceId kOffset = 123456;
IdAllocator* allocator = id_allocator();
ResourceId id1 = allocator->AllocateIDAtOrAbove(kOffset);
EXPECT_EQ(kOffset, id1);
ResourceId id2 = allocator->AllocateIDAtOrAbove(kOffset);
EXPECT_GT(id2, kOffset);
ResourceId id3 = allocator->AllocateIDAtOrAbove(kOffset);
EXPECT_GT(id3, kOffset);
}
TEST_F(IdAllocatorTest, AllocateIdAtOrAboveWrapsAround) {
const ResourceId kMaxPossibleOffset = -1;
IdAllocator* allocator = id_allocator();
ResourceId id1 = allocator->AllocateIDAtOrAbove(kMaxPossibleOffset);
EXPECT_EQ(kMaxPossibleOffset, id1);
ResourceId id2 = allocator->AllocateIDAtOrAbove(kMaxPossibleOffset);
EXPECT_EQ(1u, id2);
ResourceId id3 = allocator->AllocateIDAtOrAbove(kMaxPossibleOffset);
EXPECT_EQ(2u, id3);
}
TEST_F(IdAllocatorTest, RedundantFreeIsIgnored) {
IdAllocator* allocator = id_allocator();
ResourceId id1 = allocator->AllocateID();
allocator->FreeID(0);
allocator->FreeID(id1);
allocator->FreeID(id1);
allocator->FreeID(id1 + 1);
ResourceId id2 = allocator->AllocateID();
ResourceId id3 = allocator->AllocateID();
EXPECT_NE(id2, id3);
EXPECT_NE(kInvalidResource, id2);
EXPECT_NE(kInvalidResource, id3);
}
}