feat(hash): add memory recycling hash table

This commit is contained in:
fallenoak 2023-03-26 15:49:41 -05:00 committed by GitHub
parent 06dbb7d1c8
commit 8a232d34f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 158 additions and 5 deletions

View file

@ -2,7 +2,7 @@
#include "test/Test.hpp"
struct TestHashObject : TSHashObject<TestHashObject, HASHKEY_STRI> {
uint32_t index = 0;
uint32_t index = 255;
};
TEST_CASE("TSHashTable", "[hash]") {
@ -34,3 +34,44 @@ TEST_CASE("TSHashTable::Clear", "[hash]") {
REQUIRE(hashTable.Head() == nullptr);
}
}
TEST_CASE("TSHashTableReuse", "[hash]") {
SECTION("constructs correctly") {
TSHashTableReuse<TestHashObject, HASHKEY_STRI> hashTable;
REQUIRE(hashTable.Head() == nullptr);
}
}
TEST_CASE("TSHashTableReuse::New", "[hash]") {
SECTION("allocates new object correctly") {
TSHashTableReuse<TestHashObject, HASHKEY_STRI> hashTable;
auto object = hashTable.New("testKey1", 0, 0x0);
REQUIRE(object != nullptr);
REQUIRE(object->index == 255);
}
SECTION("recycles memory correctly") {
TSHashTableReuse<TestHashObject, HASHKEY_STRI> hashTable;
auto object1 = hashTable.New("testKey1", 0, 0x0);
auto object1Ptr = reinterpret_cast<uintptr_t>(object1);
hashTable.Clear();
auto object2 = hashTable.New("testKey2", 0, 0x0);
auto object2Ptr = reinterpret_cast<uintptr_t>(object2);
hashTable.Clear();
auto object3 = hashTable.New("testKey3", 0, 0x0);
auto object3Ptr = reinterpret_cast<uintptr_t>(object3);
REQUIRE(object1Ptr == object2Ptr);
REQUIRE(object1Ptr == object3Ptr);
REQUIRE(hashTable.Ptr("testKey1") == nullptr);
REQUIRE(hashTable.Ptr("testKey2") == nullptr);
REQUIRE(hashTable.Ptr("testKey3") != nullptr);
REQUIRE(hashTable.Ptr("testKey3")->index == 255);
}
}