diff --git a/storm/Core.cpp b/storm/Core.cpp new file mode 100644 index 0000000..11eb35a --- /dev/null +++ b/storm/Core.cpp @@ -0,0 +1,27 @@ +#include "Core.hpp" +#include "Event.hpp" + +int32_t STORMAPI StormDestroy() { + // Combined list of all calls from every game + + // SErrSetBlizzardErrorFunction(nullptr); + // SDlgDestroy(); + // SGdiDestroy(); + // SVidDestroy(); + // SDrawDestroy(); + // SRgnDestroy(); + // SMsgDestroy(); + // SNetDestroy(); + SEvtDestroy(); + // SBltDestroy(); + // SCodeDestroy(); + // SCmdDestroy(); + // SFileDestroy(); + // SCompDestroy(); + // SStrDestroy(); + // SRegDestroy(); + // SErrDestroy(); + // SLogDestroy(); + // STransDestroy(); + return 1; +} diff --git a/storm/Core.hpp b/storm/Core.hpp index be2a0e8..613eae3 100644 --- a/storm/Core.hpp +++ b/storm/Core.hpp @@ -13,5 +13,6 @@ #endif #endif +int32_t STORMAPI StormDestroy(); #endif diff --git a/test/Big.cpp b/test/Big.cpp index 82b9e1d..82fbcef 100644 --- a/test/Big.cpp +++ b/test/Big.cpp @@ -1,5 +1,4 @@ -#include "storm/Big.hpp" -#include "test/Test.hpp" +#include "BigTest.hpp" #include TEST_CASE("SBigAdd", "[big]") { diff --git a/test/BigTest.hpp b/test/BigTest.hpp new file mode 100644 index 0000000..3e57cb3 --- /dev/null +++ b/test/BigTest.hpp @@ -0,0 +1,18 @@ +#include "Test.hpp" +#include "storm/Big.hpp" + +class BigData; + +// Fixture for repetitive handling of BigData objects. +struct BigDataTest { + using BigDataPtr = BigData*; + + BigData* num; + + BigDataTest() { SBigNew(&num); } + ~BigDataTest() { SBigDel(num); } + + BigData** operator &() { return # } + operator BigDataPtr() const { return num; } + BigData* operator->() const { return num; } +}; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7e07469..1f3ba04 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,7 +2,9 @@ if(WHOA_TEST_STORMDLL) set(TEST_SOURCES #Big.cpp - #Event.cpp + Core.cpp + Event.cpp + EventTest.cpp Memory.cpp Region.cpp String.cpp diff --git a/test/Core.cpp b/test/Core.cpp new file mode 100644 index 0000000..c6703d1 --- /dev/null +++ b/test/Core.cpp @@ -0,0 +1,40 @@ +#include "test/Test.hpp" +#include "EventTest.hpp" +#include "storm/Core.hpp" + + +TEST_CASE("StormDestroy", "[core]") { + SECTION("always returns 1") { + CHECK(StormDestroy() == 1); + } + + SECTION("SEvt") { + EventHandlerTest test; + + SECTION("destroys all event handlers") { + SEvtRegisterHandler(1, 1, 1, 0, &TestEventHandler1); + CHECK(SEvtDispatch(1, 1, 1, nullptr) == 1); + CHECK(test.NumCalls() == 1); + + CHECK(SEvtDispatch(1, 1, 1, nullptr) == 1); + CHECK(test.NumCalls() == 2); + + CHECK(StormDestroy() == 1); + + // Can't increment calls since the handler was destroyed + CHECK(SEvtDispatch(1, 1, 1, nullptr) == 0); + CHECK(test.NumCalls() == 2); + } + + SECTION("doesn't destroy break data") { + // not ideal but it's official behaviour + SEvtBreakHandlerChain(nullptr); + + CHECK(StormDestroy() == 1); + + SEvtRegisterHandler(0, 0, 0, 0, &TestEventHandler1); + CHECK(SEvtDispatch(0, 0, 0, nullptr) == 0); + CHECK(SEvtDispatch(0, 0, 0, nullptr) == 1); + } + } +} diff --git a/test/Event.cpp b/test/Event.cpp index dbd34cb..0b8a687 100644 --- a/test/Event.cpp +++ b/test/Event.cpp @@ -10,8 +10,7 @@ static void STORMAPI TestBreakEventHandlerSelf(void* data) { static void STORMAPI TestBreakEventHandlerOther(void* data) { EventHandlerTest::RegisterCall(10, data); - int bunk = 0; - CHECK(SEvtBreakHandlerChain(&bunk) == 1); + CHECK(SEvtBreakHandlerChain(nullptr) == 1); } TEST_CASE("SEvtBreakHandlerChain", "[event]") { @@ -22,6 +21,7 @@ TEST_CASE("SEvtBreakHandlerChain", "[event]") { SEvtRegisterHandler(7357, 1, 0, 0, &TestEventHandler1); CHECK(SEvtDispatch(7357, 1, 0, nullptr) == 0); + CHECK(SEvtDispatch(7357, 1, 0, nullptr) == 1); } SECTION("causes SEvtDispatch to break early if one of many data matches") { @@ -34,6 +34,11 @@ TEST_CASE("SEvtBreakHandlerChain", "[event]") { SEvtRegisterHandler(7357, 1, 0, 0, &TestEventHandler1); CHECK(SEvtDispatch(7357, 1, 0, &data2) == 0); + + // CLEANUP - SEvtDestroy doesn't erase registered breaks, avoid polluting other tests + CHECK(SEvtDispatch(7357, 1, 0, nullptr) == 0); + CHECK(SEvtDispatch(7357, 1, 0, &data1) == 0); + CHECK(SEvtDispatch(7357, 1, 0, &data3) == 0); } SECTION("doesn't break SEvtDispatch if no data matches") { @@ -46,6 +51,12 @@ TEST_CASE("SEvtBreakHandlerChain", "[event]") { SEvtRegisterHandler(7357, 1, 0, 0, &TestEventHandler1); CHECK(SEvtDispatch(7357, 1, 0, &test) == 1); + + // CLEANUP - SEvtDestroy doesn't erase registered breaks, avoid polluting other tests + CHECK(SEvtDispatch(7357, 1, 0, nullptr) == 0); + CHECK(SEvtDispatch(7357, 1, 0, &data1) == 0); + CHECK(SEvtDispatch(7357, 1, 0, &data2) == 0); + CHECK(SEvtDispatch(7357, 1, 0, &data3) == 0); } SECTION("deduplicates multiple same-data breaks") { @@ -75,15 +86,20 @@ TEST_CASE("SEvtBreakHandlerChain", "[event]") { SEvtRegisterHandler(0, 0, 0, 0, &TestBreakEventHandlerOther); SEvtRegisterHandler(0, 0, 0, 0, &TestEventHandler2); - CHECK(SEvtDispatch(0, 0, 0, nullptr) == 1); + int data = 42; + CHECK(SEvtDispatch(0, 0, 0, &data) == 1); CHECK(test.NumCalls() == 3); // Calls are reverse order, TestEventHandler1 doesn't get called - CHECK_THAT(test.CallResult(), MatchesCall({ 2, nullptr })); - CHECK_THAT(test.CallResult(), MatchesCall({ 10, nullptr })); - CHECK_THAT(test.CallResult(), MatchesCall({ 1, nullptr })); + CHECK_THAT(test.CallResult(), MatchesCall({ 2, &data })); + CHECK_THAT(test.CallResult(), MatchesCall({ 10, &data })); + CHECK_THAT(test.CallResult(), MatchesCall({ 1, &data })); + + // CLEANUP - SEvtDestroy doesn't erase registered breaks, avoid polluting other tests + CHECK(SEvtDispatch(0, 0, 0, nullptr) == 0); } } +#if !defined(WHOA_TEST_STORMDLL) TEST_CASE("SEvtDestroy", "[event]") { EventHandlerTest test; @@ -117,6 +133,7 @@ TEST_CASE("SEvtDestroy", "[event]") { CHECK(SEvtDispatch(0, 0, 0, nullptr) == 1); } } +#endif static void STORMAPI TestNestedDispatchEventHandler(void* data) { EventHandlerTest::RegisterCall(20, data); diff --git a/test/EventTest.cpp b/test/EventTest.cpp new file mode 100644 index 0000000..b76fa21 --- /dev/null +++ b/test/EventTest.cpp @@ -0,0 +1,29 @@ +#include "EventTest.hpp" + +std::deque EventHandlerCallResults; + + +void STORMAPI TestEventHandler1(void* data) { + EventHandlerTest::RegisterCall(1, data); +} + +void STORMAPI TestEventHandler2(void* data) { + EventHandlerTest::RegisterCall(2, data); +} + +void STORMAPI TestEventHandler3(void* data) { + EventHandlerTest::RegisterCall(3, data); +} + +void STORMAPI TestEventHandler4(void* data) { + EventHandlerTest::RegisterCall(4, data); +} + +std::ostream& operator <<(std::ostream& os, EventHandlerCalledWith const& value) { + os << "{ TestEventHandler" << value.handler << ", " << value.data << " }"; + return os; +} + +EventHandlerCalledWithMatcher MatchesCall(EventHandlerCalledWith arg) { + return { arg }; +} diff --git a/test/EventTest.hpp b/test/EventTest.hpp index 45b5069..ec64c25 100644 --- a/test/EventTest.hpp +++ b/test/EventTest.hpp @@ -1,25 +1,22 @@ #include "test/Test.hpp" +#include "storm/Core.hpp" #include "storm/Event.hpp" #include #include -void SEvtCleanExtraDataForTests(); - - struct EventHandlerCalledWith { int handler; void* data; }; -static std::deque EventHandlerCallResults; +extern std::deque EventHandlerCallResults; struct EventHandlerTest { EventHandlerTest() { EventHandlerCallResults.clear(); - SEvtDestroy(); - SEvtCleanExtraDataForTests(); + StormDestroy(); } static void RegisterCall(int handler, void* data) { @@ -43,28 +40,14 @@ struct EventHandlerTest { }; -static void STORMAPI TestEventHandler1(void* data) { - EventHandlerTest::RegisterCall(1, data); -} - -static void STORMAPI TestEventHandler2(void* data) { - EventHandlerTest::RegisterCall(2, data); -} - -static void STORMAPI TestEventHandler3(void* data) { - EventHandlerTest::RegisterCall(3, data); -} - -static void STORMAPI TestEventHandler4(void* data) { - EventHandlerTest::RegisterCall(4, data); -} +void STORMAPI TestEventHandler1(void* data); +void STORMAPI TestEventHandler2(void* data); +void STORMAPI TestEventHandler3(void* data); +void STORMAPI TestEventHandler4(void* data); // Helpers for comparing EventHandlerCalledWith structs -std::ostream& operator <<(std::ostream& os, EventHandlerCalledWith const& value) { - os << "{ TestEventHandler" << value.handler << ", " << value.data << " }"; - return os; -} +std::ostream& operator <<(std::ostream& os, EventHandlerCalledWith const& value); template class EventHandlerCalledWithMatcher : public Catch::MatcherBase { @@ -85,6 +68,4 @@ public: } }; -EventHandlerCalledWithMatcher MatchesCall(EventHandlerCalledWith arg) { - return { arg }; -} +EventHandlerCalledWithMatcher MatchesCall(EventHandlerCalledWith arg); diff --git a/test/Test.cpp b/test/Test.cpp index fae4b8c..c5af3d8 100644 --- a/test/Test.cpp +++ b/test/Test.cpp @@ -1,6 +1,2 @@ -#define CATCH_CONFIG_MAIN -#include "test/Test.hpp" -#include "storm/Big.hpp" - -BigDataTest::BigDataTest() { SBigNew(&num); } -BigDataTest::~BigDataTest() { SBigDel(num); } +#define CATCH_CONFIG_MAIN +#include "test/Test.hpp" diff --git a/test/Test.hpp b/test/Test.hpp index 990c842..da3dbbd 100644 --- a/test/Test.hpp +++ b/test/Test.hpp @@ -1,17 +1 @@ #include "vendor/catch-2.13.10/catch.hpp" - -class BigData; - -// Fixture for repetitive handling of BigData objects. -struct BigDataTest { - using BigDataPtr = BigData*; - - BigData* num; - - BigDataTest(); - ~BigDataTest(); - - BigData** operator &() { return # } - operator BigDataPtr() const { return num; } - BigData* operator->() const { return num; } -}; diff --git a/test/big/Ops.cpp b/test/big/Ops.cpp index e2e7501..5a6a29c 100644 --- a/test/big/Ops.cpp +++ b/test/big/Ops.cpp @@ -1,6 +1,5 @@ -#include "storm/Big.hpp" +#include "test/BigTest.hpp" #include "storm/big/Ops.hpp" -#include "test/Test.hpp" #include TEST_CASE("Add", "[big]") { diff --git a/test/stormdll/storm.def b/test/stormdll/storm.def index f3744eb..45a6f4a 100644 --- a/test/stormdll/storm.def +++ b/test/stormdll/storm.def @@ -128,7 +128,7 @@ EXPORTS ; Main ; This is the only *Destroy function exported, tests will need to use it instead - ;StormDestroy @301 NONAME + StormDestroy @301 NONAME ;StormGetInstance @302 NONAME ;StormGetOption @303 NONAME ;StormSetOption @304 NONAME @@ -176,7 +176,7 @@ EXPORTS ;SDrawUpdatePalette @357 NONAME ;SDrawUpdateScreen @358 NONAME ;SDrawWaitForVerticalBlank @359 NONAME - + ; Event ;SEvtDestroy @371 NONAME SEvtDispatch @372 NONAME diff --git a/test/stormdll/stormstubs.cpp b/test/stormdll/stormstubs.cpp index 0c56037..feca531 100644 --- a/test/stormdll/stormstubs.cpp +++ b/test/stormdll/stormstubs.cpp @@ -1,3 +1,7 @@ +#include + +int32_t STORMAPI StormDestroy() { return 0; } + #include void STORMAPI SBigAdd(BigData*, BigData*, BigData*) {} @@ -40,7 +44,6 @@ void STORMAPI SErrSetLastError(uint32_t) {} uint32_t STORMAPI SErrGetLastError() { return 0; } void STORMAPI SErrSuppressErrors(uint32_t) {} - #include int32_t STORMAPI SEvtBreakHandlerChain(void*) { return 0; }