From 0483e635eab0c056e15abe8662b8b769c1bbe0f3 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 7 Dec 2019 13:45:44 +0100 Subject: [PATCH] Reworks test suite, tweaks to Cmake config for Windows. --- CMakeLists.txt | 10 ++--- src/Battling/Models/Battle.cpp | 4 +- src/Battling/Models/Battle.hpp | 1 - tests/BattleTests/ChoiceSetTests.cpp | 15 ++++---- tests/BattleTests/TurnOrderTests.cpp | 11 +++--- tests/Integration/BattleIntegrations.cpp | 12 +++--- tests/LibraryTests/CreatureTests.cpp | 17 ++++---- tests/LibraryTests/TestLibraryTests.cpp | 15 ++++---- tests/TestLibrary/TestLibrary.cpp | 49 +++++++++++------------- tests/TestLibrary/TestLibrary.hpp | 24 ++++++++++++ 10 files changed, 90 insertions(+), 68 deletions(-) create mode 100644 tests/TestLibrary/TestLibrary.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 42d7793..83c4557 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,12 +39,12 @@ target_link_libraries(CreatureLibTests CreatureLibLibrary) target_link_libraries(CreatureLibTests CreatureLibBattling) if (WINDOWS) - set (CMAKE_CXX_FLAGS "-Wl,-allow-multiple-definition") + set(CMAKE_CXX_FLAGS "-m64 -Wa,-mbig-obj") # Statically link libraries we need in Windows. - target_link_libraries(CreatureLibCore -static -static-libgcc -static-libstdc++) - target_link_libraries(CreatureLibLibrary -static -static-libgcc -static-libstdc++) - target_link_libraries(CreatureLibBattling -static -static-libgcc -static-libstdc++) - target_link_libraries(CreatureLibTests -static -static-libgcc -static-libstdc++) +# target_link_libraries(CreatureLibCore -static -static-libgcc -static-libstdc++) +# target_link_libraries(CreatureLibLibrary -static -static-libgcc -static-libstdc++) +# target_link_libraries(CreatureLibBattling -static -static-libgcc -static-libstdc++) +# target_link_libraries(CreatureLibTests -static -static-libgcc -static-libstdc++) endif(WINDOWS) # Add a definition for the test library diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index a5767a6..a80772f 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -81,13 +81,13 @@ bool Battle::CreatureInField(const Creature* creature) const { void Battle::ForceRecall(uint8_t side, uint8_t index) { _sides[side]->SetCreature(nullptr, index); } -void Battle::FillEmptySlot(uint8_t side, uint8_t index, Creature* c) { _sides[side]->SetCreature(c, index); } - void Battle::GetActiveScripts(std::vector& scripts) { scripts.emplace_back(&_volatile); } + void Battle::SwitchCreature(uint8_t sideIndex, uint8_t index, Creature* c) { auto side = this->_sides[sideIndex]; side->SetCreature(c, index); } + bool Battle::CanSlotBeFilled(uint8_t side, uint8_t index) const { for (const auto& party : _parties) { if (party.IsResponsibleForIndex(side, index)) { diff --git a/src/Battling/Models/Battle.hpp b/src/Battling/Models/Battle.hpp index 9325b4a..aa1d3a1 100644 --- a/src/Battling/Models/Battle.hpp +++ b/src/Battling/Models/Battle.hpp @@ -54,7 +54,6 @@ namespace CreatureLib::Battling { } void ForceRecall(uint8_t side, uint8_t index); - void FillEmptySlot(uint8_t side, uint8_t index, Creature* c); void SwitchCreature(uint8_t side, uint8_t index, Creature* c); bool CanSlotBeFilled(uint8_t side, uint8_t index) const; diff --git a/tests/BattleTests/ChoiceSetTests.cpp b/tests/BattleTests/ChoiceSetTests.cpp index 1f42bfe..3143999 100644 --- a/tests/BattleTests/ChoiceSetTests.cpp +++ b/tests/BattleTests/ChoiceSetTests.cpp @@ -1,15 +1,16 @@ #ifdef TESTS_BUILD +#include "../../extern/catch.hpp" +#include "../TestLibrary/TestLibrary.hpp" #include "../../src/Battling/Models/BattleSide.hpp" #include "../../src/Battling/Models/CreateCreature.hpp" #include "../../src/Battling/TurnChoices/PassTurnChoice.hpp" -#include "../TestLibrary/TestLibrary.cpp" using namespace CreatureLib::Battling; TEST_CASE("Set Choice one-sized side", "[Battling]") { auto side = BattleSide(0, nullptr, 1); - auto c = CreateCreature(GetLibrary(), "testSpecies1", 5).Create(); + auto c = CreateCreature(TestLibrary::Get(), "testSpecies1", 5).Create(); side.SetCreature(c, 0); auto choice = new PassTurnChoice(c); side.SetChoice(choice); @@ -19,7 +20,7 @@ TEST_CASE("Set Choice one-sized side", "[Battling]") { TEST_CASE("Set Choice one-sized side, validate all choices set", "[Battling]") { auto side = BattleSide(0, nullptr, 1); - auto c = CreateCreature(GetLibrary(), "testSpecies1", 5).Create(); + auto c = CreateCreature(TestLibrary::Get(), "testSpecies1", 5).Create(); side.SetCreature(c, 0); auto choice = new PassTurnChoice(c); REQUIRE_FALSE(side.AllChoicesSet()); @@ -31,8 +32,8 @@ TEST_CASE("Set Choice one-sized side, validate all choices set", "[Battling]") { TEST_CASE("Set Choice two-sized side", "[Battling]") { auto side = BattleSide(0, nullptr, 2); - auto c1 = CreateCreature(GetLibrary(), "testSpecies1", 5).Create(); - auto c2 = CreateCreature(GetLibrary(), "testSpecies1", 5).Create(); + auto c1 = CreateCreature(TestLibrary::Get(), "testSpecies1", 5).Create(); + auto c2 = CreateCreature(TestLibrary::Get(), "testSpecies1", 5).Create(); side.SetCreature(c1, 0); side.SetCreature(c2, 1); auto choice1 = new PassTurnChoice(c1); @@ -47,8 +48,8 @@ TEST_CASE("Set Choice two-sized side", "[Battling]") { TEST_CASE("Set Choice two-sized side, validate all choices set", "[Battling]") { auto side = BattleSide(0, nullptr, 2); - auto c1 = CreateCreature(GetLibrary(), "testSpecies1", 5).Create(); - auto c2 = CreateCreature(GetLibrary(), "testSpecies1", 5).Create(); + auto c1 = CreateCreature(TestLibrary::Get(), "testSpecies1", 5).Create(); + auto c2 = CreateCreature(TestLibrary::Get(), "testSpecies1", 5).Create(); side.SetCreature(c1, 0); side.SetCreature(c2, 1); auto choice1 = new PassTurnChoice(c1); diff --git a/tests/BattleTests/TurnOrderTests.cpp b/tests/BattleTests/TurnOrderTests.cpp index 861b312..564fa87 100644 --- a/tests/BattleTests/TurnOrderTests.cpp +++ b/tests/BattleTests/TurnOrderTests.cpp @@ -1,9 +1,10 @@ #ifdef TESTS_BUILD +#include "../../extern/catch.hpp" #include "../../src/Battling/Flow/TurnOrdering.hpp" #include "../../src/Battling/TurnChoices/AttackTurnChoice.hpp" #include "../../src/Battling/TurnChoices/PassTurnChoice.hpp" -#include "../TestLibrary/TestLibrary.cpp" +#include "../TestLibrary/TestLibrary.hpp" using namespace CreatureLib; using namespace CreatureLib::Battling; @@ -26,7 +27,7 @@ TEST_CASE("Turn ordering: Attack before pass", "[Battling]") { } TEST_CASE("Turn ordering: High priority goes before no priority", "[Battling]") { - auto l = GetLibrary()->GetAttackLibrary(); + auto l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->GetAttack("standard"), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->GetAttack("highPriority"), AttackLearnMethod::Unknown); auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); @@ -48,7 +49,7 @@ TEST_CASE("Turn ordering: High priority goes before no priority", "[Battling]") } TEST_CASE("Turn ordering: Higher priority goes before high priority", "[Battling]") { - auto l = GetLibrary()->GetAttackLibrary(); + auto l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->GetAttack("highPriority"), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->GetAttack("higherPriority"), AttackLearnMethod::Unknown); auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); @@ -70,7 +71,7 @@ TEST_CASE("Turn ordering: Higher priority goes before high priority", "[Battling } TEST_CASE("Turn ordering: High priority goes before low priority", "[Battling]") { - auto l = GetLibrary()->GetAttackLibrary(); + auto l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->GetAttack("lowPriority"), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->GetAttack("higherPriority"), AttackLearnMethod::Unknown); auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); @@ -92,7 +93,7 @@ TEST_CASE("Turn ordering: High priority goes before low priority", "[Battling]") } TEST_CASE("Turn ordering: No priority goes before low priority", "[Battling]") { - auto l = GetLibrary()->GetAttackLibrary(); + auto l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->GetAttack("lowPriority"), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->GetAttack("standard"), AttackLearnMethod::Unknown); auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); diff --git a/tests/Integration/BattleIntegrations.cpp b/tests/Integration/BattleIntegrations.cpp index 23f15c9..124cc79 100644 --- a/tests/Integration/BattleIntegrations.cpp +++ b/tests/Integration/BattleIntegrations.cpp @@ -5,13 +5,13 @@ #include "../../src/Battling/Models/CreateCreature.hpp" #include "../../src/Battling/TurnChoices/AttackTurnChoice.hpp" #include "../../src/Battling/TurnChoices/PassTurnChoice.hpp" -#include "../TestLibrary/TestLibrary.cpp" +#include "../TestLibrary/TestLibrary.hpp" using namespace CreatureLib; using namespace Battling; TEST_CASE("Create Party", "[Integrations]") { - auto library = GetLibrary(); + auto library = TestLibrary::Get(); auto c1 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard", AttackLearnMethod::Unknown)->Create(); CreatureParty party1{c1}; auto battleParty = BattleParty(&party1, {CreatureIndex(0, 0)}); @@ -19,7 +19,7 @@ TEST_CASE("Create Party", "[Integrations]") { } TEST_CASE("Create Battle", "[Integrations]") { - auto library = GetLibrary(); + auto library = TestLibrary::Get(); auto c1 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard", AttackLearnMethod::Unknown)->Create(); CreatureParty party1{c1}; auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)}); @@ -31,7 +31,7 @@ TEST_CASE("Create Battle", "[Integrations]") { } TEST_CASE("Use damaging move", "[Integrations]") { - auto library = GetLibrary(); + auto library = TestLibrary::Get(); auto c1 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard", AttackLearnMethod::Unknown)->Create(); CreatureParty party1{c1}; auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)}); @@ -41,8 +41,8 @@ TEST_CASE("Use damaging move", "[Integrations]") { auto battle = Battle(library, {battleParty1, battleParty2}); - battle.FillEmptySlot(0, 0, c1); - battle.FillEmptySlot(1, 0, c2); + battle.SwitchCreature(0, 0, c1); + battle.SwitchCreature(1, 0, c2); battle.TrySetChoice(new AttackTurnChoice(c1, c1->GetAttacks()[0], CreatureIndex(1, 0))); battle.TrySetChoice(new PassTurnChoice(c2)); diff --git a/tests/LibraryTests/CreatureTests.cpp b/tests/LibraryTests/CreatureTests.cpp index 5e0c7cb..f20d37a 100644 --- a/tests/LibraryTests/CreatureTests.cpp +++ b/tests/LibraryTests/CreatureTests.cpp @@ -1,44 +1,45 @@ #ifdef TESTS_BUILD +#include "../../extern/catch.hpp" #include "../../src/Battling/Models/CreateCreature.hpp" -#include "../TestLibrary/TestLibrary.cpp" +#include "../TestLibrary/TestLibrary.hpp" using namespace CreatureLib::Library; TEST_CASE("Create basic creature", "[Library]") { - auto library = GetLibrary(); + auto library = TestLibrary::Get(); auto creature = CreateCreature(library, "testSpecies1", 1).Create(); delete creature; } TEST_CASE("Get creature species", "[Library]") { - auto library = GetLibrary(); + auto library = TestLibrary::Get(); auto creature = CreateCreature(library, "testSpecies1", 1).Create(); REQUIRE(creature->GetSpecies()->GetName() == "testSpecies1"); delete creature; } TEST_CASE("Get creature level", "[Library]") { - auto library = GetLibrary(); + auto library = TestLibrary::Get(); auto creature = CreateCreature(library, "testSpecies1", 1).Create(); REQUIRE(creature->GetLevel() == 1); delete creature; } TEST_CASE("Get creature variant when unset", "[Library]") { - auto library = GetLibrary(); + auto library = TestLibrary::Get(); auto creature = CreateCreature(library, "testSpecies1", 1).Create(); REQUIRE(creature->GetVariant()->GetName() == "default"); delete creature; } TEST_CASE("Get creature nickname when unset", "[Library]") { - auto library = GetLibrary(); + auto library = TestLibrary::Get(); auto creature = CreateCreature(library, "testSpecies1", 1).Create(); REQUIRE(creature->GetNickname() == "testSpecies1"); delete creature; } TEST_CASE("Get creature stat potentials when unset", "[Library]") { - auto library = GetLibrary(); + auto library = TestLibrary::Get(); auto creature = CreateCreature(library, "testSpecies1", 1).Create(); auto potentials = creature->GetStatPotential(); REQUIRE(potentials.GetHealth() == 0); @@ -51,7 +52,7 @@ TEST_CASE("Get creature stat potentials when unset", "[Library]") { } TEST_CASE("Get creature stat experience when unset", "[Library]") { - auto library = GetLibrary(); + auto library = TestLibrary::Get(); auto creature = CreateCreature(library, "testSpecies1", 1).Create(); auto experiences = creature->GetStatExperience(); REQUIRE(experiences.GetHealth() == 0); diff --git a/tests/LibraryTests/TestLibraryTests.cpp b/tests/LibraryTests/TestLibraryTests.cpp index 73be1c8..b11f888 100644 --- a/tests/LibraryTests/TestLibraryTests.cpp +++ b/tests/LibraryTests/TestLibraryTests.cpp @@ -2,36 +2,35 @@ #define CATCH_CONFIG_MAIN #include "../../extern/catch.hpp" -#include "../TestLibrary/TestLibrary.cpp" +#include "../TestLibrary/TestLibrary.hpp" TEST_CASE("Can Create Species Library", "[Library]") { - auto l = BuildSpeciesLibrary(); + auto l = TestLibrary::BuildSpeciesLibrary(); REQUIRE(l != nullptr); delete l; } TEST_CASE("Can Create Attack Library", "[Library]") { - auto l = BuildAttackLibrary(); + auto l = TestLibrary::BuildAttackLibrary(); REQUIRE(l != nullptr); delete l; } TEST_CASE("Can Create Item Library", "[Library]") { - auto l = BuildItemLibrary(); + auto l = TestLibrary::BuildItemLibrary(); REQUIRE(l != nullptr); delete l; } -TEST_CASE("Can Create Growthrate Library", "[Library]") { - auto l = BuildGrowthRateLibrary(); +TEST_CASE("Can Create Growth Rate Library", "[Library]") { + auto l = TestLibrary::BuildGrowthRateLibrary(); REQUIRE(l != nullptr); delete l; } TEST_CASE("Can Create Data Library", "[Library]") { - auto l = BuildLibrary(); + auto l = TestLibrary::Get(); REQUIRE(l != nullptr); - delete l; } #endif \ No newline at end of file diff --git a/tests/TestLibrary/TestLibrary.cpp b/tests/TestLibrary/TestLibrary.cpp index 84b22b3..9217541 100644 --- a/tests/TestLibrary/TestLibrary.cpp +++ b/tests/TestLibrary/TestLibrary.cpp @@ -1,24 +1,35 @@ #ifdef TESTS_BUILD -#include "../../extern/catch.hpp" -#include "../../src/Battling/Library/BattleLibrary.hpp" + +#include "TestLibrary.hpp" using namespace CreatureLib::Core; using namespace CreatureLib::Library; using namespace CreatureLib::Battling; -static BattleLibrary* __library = nullptr; +BattleLibrary* TestLibrary::_library = nullptr; -static SpeciesLibrary* BuildSpeciesLibrary() { +BattleLibrary *TestLibrary::Get() { + if (TestLibrary::_library == nullptr){ + auto l = new DataLibrary(LibrarySettings(100, 4), BuildSpeciesLibrary(), BuildAttackLibrary(), BuildItemLibrary(), + BuildGrowthRateLibrary(), BuildTypeLibrary()); + auto statCalc = new BattleStatCalculator(); + auto battleLib = new BattleLibrary(l, statCalc, new DamageLibrary(), new CriticalLibrary(), new ScriptResolver()); + TestLibrary::_library = battleLib; + } + return TestLibrary::_library; +} + +SpeciesLibrary *TestLibrary::BuildSpeciesLibrary() { auto l = new SpeciesLibrary(); l->LoadSpecies("testSpecies1", new CreatureSpecies( - 0, "testSpecies1", - new SpeciesVariant("default", 1, 1, 10, {0, 1}, StatisticSet(10, 10, 10, 10, 10, 10), - {"testTalent"}, {"testSecretTalent"}, new LearnableAttacks(100)), - 0.5f, "testGrowthRate", 5, 100)); + 0, "testSpecies1", + new SpeciesVariant("default", 1, 1, 10, {0, 1}, StatisticSet(10, 10, 10, 10, 10, 10), + {"testTalent"}, {"testSecretTalent"}, new LearnableAttacks(100)), + 0.5f, "testGrowthRate", 5, 100)); return l; } -static AttackLibrary* BuildAttackLibrary() { +AttackLibrary *TestLibrary::BuildAttackLibrary() { auto l = new AttackLibrary(); l->LoadAttack("standard", new AttackData("standard", "normal", AttackCategory::Physical, 20, 100, 30, AttackTarget::AdjacentOpponent, 0, {})); @@ -31,17 +42,17 @@ static AttackLibrary* BuildAttackLibrary() { return l; } -static ItemLibrary* BuildItemLibrary() { +ItemLibrary *TestLibrary::BuildItemLibrary() { auto l = new ItemLibrary(); return l; } -static GrowthRateLibrary* BuildGrowthRateLibrary() { +GrowthRateLibrary *TestLibrary::BuildGrowthRateLibrary() { auto l = new GrowthRateLibrary(); return l; } -static TypeLibrary* BuildTypeLibrary() { +TypeLibrary *TestLibrary::BuildTypeLibrary() { auto l = new TypeLibrary(); l->RegisterType("testType1"); l->RegisterType("testType2"); @@ -49,18 +60,4 @@ static TypeLibrary* BuildTypeLibrary() { return l; } -static BattleLibrary* BuildLibrary() { - auto l = new DataLibrary(LibrarySettings(100, 4), BuildSpeciesLibrary(), BuildAttackLibrary(), BuildItemLibrary(), - BuildGrowthRateLibrary(), BuildTypeLibrary()); - auto statCalc = new BattleStatCalculator(); - auto battleLib = new BattleLibrary(l, statCalc, new DamageLibrary(), new CriticalLibrary(), new ScriptResolver()); - return battleLib; -} - -[[maybe_unused]] static BattleLibrary* GetLibrary() { - if (__library == nullptr) { - __library = BuildLibrary(); - } - return __library; -} #endif \ No newline at end of file diff --git a/tests/TestLibrary/TestLibrary.hpp b/tests/TestLibrary/TestLibrary.hpp new file mode 100644 index 0000000..9adb7a5 --- /dev/null +++ b/tests/TestLibrary/TestLibrary.hpp @@ -0,0 +1,24 @@ + + +#ifndef CREATURELIB_TESTLIBRARY_HPP +#define CREATURELIB_TESTLIBRARY_HPP + +#include "../../src/Battling/Library/BattleLibrary.hpp" + +using namespace CreatureLib::Core; +using namespace CreatureLib::Library; +using namespace CreatureLib::Battling; + +class TestLibrary{ + static BattleLibrary* _library; +public: + static SpeciesLibrary* BuildSpeciesLibrary(); + static AttackLibrary* BuildAttackLibrary(); + static ItemLibrary* BuildItemLibrary(); + static GrowthRateLibrary* BuildGrowthRateLibrary(); + static TypeLibrary* BuildTypeLibrary(); + + static BattleLibrary* Get(); +}; + +#endif //CREATURELIB_TESTLIBRARY_HPP