/* * pooltest.cpp * * Created on: Feb 11, 2018 * Author: gregor */ #include #include "GLTB/pool.h" TEST(PoolRegion, allocate) { gltb::PoolRegion poolRegion(64); ASSERT_EQ(64, poolRegion.size()); ASSERT_TRUE(poolRegion.empty()); ASSERT_FALSE(poolRegion.full()); std::uint32_t *element = poolRegion.allocate(); ASSERT_NE(nullptr, element); // brute force a potential crash if element allocation is not correct for(int i = 0; i < 4; i++) { element[i] = i; } ASSERT_FALSE(poolRegion.empty()); ASSERT_FALSE(poolRegion.full()); } TEST(PoolRegion, allocateWhenFull) { gltb::PoolRegion poolRegion(64); // allocate all elements in pool for(int i = 0; i < 64; i++) { std::uint32_t *element = poolRegion.allocate(); ASSERT_NE(nullptr, element); ASSERT_EQ(i + 1, poolRegion.elements()); } ASSERT_TRUE(poolRegion.full()); // check that last allocation fails std::uint32_t *element = poolRegion.allocate(); ASSERT_EQ(nullptr, element); } TEST(PoolRegion, free) { gltb::PoolRegion poolRegion(64); ASSERT_EQ(64, poolRegion.size()); ASSERT_TRUE(poolRegion.empty()); std::uint32_t *element = poolRegion.allocate(); ASSERT_NE(nullptr, element); poolRegion.free(element); ASSERT_EQ(0, poolRegion.elements()); ASSERT_TRUE(poolRegion.empty()); } TEST(PoolRegion, reallocateAll) { // allocate, then free, then allocate all again gltb::PoolRegion poolRegion(64); // allocate all elements in pool std::uint32_t *elements[64]; for(int i = 0; i < 64; i++) { elements[i] = poolRegion.allocate(); ASSERT_NE(nullptr, elements[i]); ASSERT_EQ(i + 1, poolRegion.elements()); } // free all elements for(int i = 0; i < 64; i++) { poolRegion.free(elements[i]); ASSERT_EQ(64 - i - 1, poolRegion.elements()); } // allocate all elements in pool again and see if successful for(int i = 0; i < 64; i++) { std::uint32_t *element = poolRegion.allocate(); ASSERT_NE(nullptr, element); ASSERT_EQ(i + 1, poolRegion.elements()); } } TEST(PoolRegion, containsWhenTrue) { gltb::PoolRegion poolRegion(64); ASSERT_EQ(64, poolRegion.size()); std::uint32_t *element = poolRegion.allocate(); ASSERT_TRUE(poolRegion.contains(element)); } TEST(PoolRegion, containsWhenFalse) { gltb::PoolRegion poolRegion(64); ASSERT_EQ(64, poolRegion.size()); std::uint32_t *element = new std::uint32_t[4]; ASSERT_FALSE(poolRegion.contains(element)); delete[] element; } TEST(Pool, allocate) { gltb::Pool pool; std::uint32_t *element = pool.allocate(); ASSERT_NE(nullptr, element); for(int i = 0; i < 4; i++) { element[i] = i; } for(int i = 0; i < 4; i++) { ASSERT_EQ(i, element[i]); } } TEST(Pool, free) { gltb::Pool pool; std::uint32_t *element = pool.allocate(); ASSERT_NE(nullptr, element); pool.free(element); // TODO figure out how to test if element was really freed from pool region } int main(int argc, char *argv[]) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }