diff --git a/src/Battling/EventHooks/EventHook.hpp b/src/Battling/EventHooks/EventHook.hpp index 353029e..7e41b8c 100644 --- a/src/Battling/EventHooks/EventHook.hpp +++ b/src/Battling/EventHooks/EventHook.hpp @@ -24,24 +24,21 @@ namespace CreatureLib::Battling { public: EventHook() : _offset(0), _capacity(defaultSize) { - // auto ptr = calloc(defaultSize, 1); - // if (ptr == nullptr) { - // THROW_CREATURE("Out of memory."); - // } - // _memory = static_cast(ptr); + auto ptr = calloc(defaultSize, 1); + if (ptr == nullptr) { + THROW_CREATURE("Out of memory."); + } + _memory = static_cast(ptr); } EventHook(const EventHook&) = delete; EventHook& operator=(const EventHook&) = delete; - ~EventHook() { - // free(_memory); - } + ~EventHook() { free(_memory); } size_t GetPosition() const noexcept { return _offset; } size_t GetCapacity() const noexcept { return _capacity; } template void Trigger(parameters... args) { - return; if (_listeners.size() == 0) return; if (_offset + sizeof(T) >= _capacity) { diff --git a/tests/BattleTests/EventHookTests.cpp b/tests/BattleTests/EventHookTests.cpp index ee3c391..1568cfe 100644 --- a/tests/BattleTests/EventHookTests.cpp +++ b/tests/BattleTests/EventHookTests.cpp @@ -6,38 +6,38 @@ using namespace CreatureLib::Battling; -// TEST_CASE("Build and use event hook", "[Battling]") { -// auto eventHook = EventHook(); -// std::vector events; -// eventHook.RegisterListener([&](const EventData* evt) mutable -> void { events.push_back(evt); }); -// for (size_t i = 0; i < 10; i++) { -// eventHook.Trigger(nullptr, DamageSource::AttackDamage, 0, 0); -// } -// REQUIRE(events.size() == 10); -// REQUIRE(events[0]->GetKind() == EventDataKind::Damage); -//} -// -// TEST_CASE("Build and use event hook a lot", "[Battling]") { -// auto eventHook = EventHook(); -// std::vector events; -// eventHook.RegisterListener([&](const EventData* evt) mutable -> void { events.push_back(evt); }); -// for (size_t i = 0; i < 10000; i++) { -// eventHook.Trigger(nullptr, DamageSource::AttackDamage, 0, 0); -// } -// REQUIRE(events.size() == 10000); -//} -// -// TEST_CASE("Build and use event hook with different types", "[Battling]") { -// auto eventHook = EventHook(); -// std::vector events; -// eventHook.RegisterListener([&](const EventData* evt) mutable -> void { events.push_back(evt); }); -// eventHook.Trigger(nullptr, DamageSource::AttackDamage, 0, 0); -// eventHook.Trigger(nullptr); -// eventHook.Trigger(nullptr, DamageSource::AttackDamage, 0, 0); -// eventHook.Trigger(nullptr); -// eventHook.Trigger(nullptr, DamageSource::AttackDamage, 0, 0); -// eventHook.Trigger(nullptr); -// eventHook.Trigger(nullptr, DamageSource::AttackDamage, 0, 0); -// eventHook.Trigger(nullptr); -//} +TEST_CASE("Build and use event hook", "[Battling]") { + auto eventHook = EventHook(); + std::vector events; + eventHook.RegisterListener([&](const EventData* evt) mutable -> void { events.push_back(evt); }); + for (size_t i = 0; i < 10; i++) { + eventHook.Trigger(nullptr, DamageSource::AttackDamage, 0, 0); + } + REQUIRE(events.size() == 10); + REQUIRE(events[0]->GetKind() == EventDataKind::Damage); +} + +TEST_CASE("Build and use event hook a lot", "[Battling]") { + auto eventHook = EventHook(); + std::vector events; + eventHook.RegisterListener([&](const EventData* evt) mutable -> void { events.push_back(evt); }); + for (size_t i = 0; i < 10000; i++) { + eventHook.Trigger(nullptr, DamageSource::AttackDamage, 0, 0); + } + REQUIRE(events.size() == 10000); +} + +TEST_CASE("Build and use event hook with different types", "[Battling]") { + auto eventHook = EventHook(); + std::vector events; + eventHook.RegisterListener([&](const EventData* evt) mutable -> void { events.push_back(evt); }); + eventHook.Trigger(nullptr, DamageSource::AttackDamage, 0, 0); + eventHook.Trigger(nullptr); + eventHook.Trigger(nullptr, DamageSource::AttackDamage, 0, 0); + eventHook.Trigger(nullptr); + eventHook.Trigger(nullptr, DamageSource::AttackDamage, 0, 0); + eventHook.Trigger(nullptr); + eventHook.Trigger(nullptr, DamageSource::AttackDamage, 0, 0); + eventHook.Trigger(nullptr); +} #endif \ No newline at end of file