From e5cb857ea8bb418ba30b4f7354776694710e3db6 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Tue, 31 Dec 2019 10:39:20 +0100 Subject: [PATCH] Tests and tweaks for the Nature Library. --- src/Library/Natures/Nature.hpp | 13 +++- src/Library/Natures/NatureLibrary.hpp | 20 +++--- tests/LibraryTests/NatureLibraryTests.cpp | 82 +++++++++++++++++++++++ 3 files changed, 105 insertions(+), 10 deletions(-) create mode 100644 tests/LibraryTests/NatureLibraryTests.cpp diff --git a/src/Library/Natures/Nature.hpp b/src/Library/Natures/Nature.hpp index 3a6518c..dce6e6e 100644 --- a/src/Library/Natures/Nature.hpp +++ b/src/Library/Natures/Nature.hpp @@ -5,12 +5,21 @@ namespace PkmnLib::Library { class Nature { private: - float _increaseModifier; - float _decreaseModifier; CreatureLib::Core::Statistic _increaseStat; CreatureLib::Core::Statistic _decreaseStat; + float _increaseModifier; + float _decreaseModifier; public: + Nature() + : _increaseStat(CreatureLib::Core::Statistic::Health), _decreaseStat(CreatureLib::Core::Statistic::Health), + _increaseModifier(1.0f), _decreaseModifier(1.0f) {} + + Nature(CreatureLib::Core::Statistic increasedStat, CreatureLib::Core::Statistic decreasedStat, + float increasedModifier = 1.1f, float decreasedModifier = 0.9f) + : _increaseStat(increasedStat), _decreaseStat(decreasedStat), _increaseModifier(increasedModifier), + _decreaseModifier(decreasedModifier) {} + [[nodiscard]] float GetIncreaseModifier() const { return _increaseModifier; } [[nodiscard]] float GetDecreaseModifier() const { return _decreaseModifier; } diff --git a/src/Library/Natures/NatureLibrary.hpp b/src/Library/Natures/NatureLibrary.hpp index 223b2c2..cd70cbe 100644 --- a/src/Library/Natures/NatureLibrary.hpp +++ b/src/Library/Natures/NatureLibrary.hpp @@ -10,16 +10,23 @@ namespace PkmnLib::Library { private: std::unordered_map _keyLookup; std::vector _natures; + uint8_t _current = 0; public: - void LoadNature(const std::string& name, const Nature& nature) { + NatureLibrary(uint8_t size = 32) + : _keyLookup(std::unordered_map(size)), _natures(std::vector(size)) {} + + uint8_t LoadNature(const std::string& name, const Nature& nature) { auto find = _keyLookup.find(name); if (find != _keyLookup.end()) { auto key = _keyLookup[name]; _natures[key] = nature; + return key; } else { - _keyLookup[name] = _natures.size(); - _natures.push_back(nature); + auto key = _current; + _keyLookup[name] = key; + _natures[_current++] = nature; + return key; } } @@ -31,17 +38,14 @@ namespace PkmnLib::Library { return _natures[_keyLookup.at(name)]; } - const Nature& GetNature(uint8_t id) const{ - return _natures[id]; - } + const Nature& GetNature(uint8_t id) const { return _natures[id]; } - uint8_t GetNatureId(const std::string& name) const{ + uint8_t GetNatureIdByName(const std::string& name) const { auto find = _keyLookup.find(name); if (find == _keyLookup.end()) { throw CreatureException("Invalid nature name."); } return _keyLookup.at(name); - } }; } diff --git a/tests/LibraryTests/NatureLibraryTests.cpp b/tests/LibraryTests/NatureLibraryTests.cpp new file mode 100644 index 0000000..87f4bb2 --- /dev/null +++ b/tests/LibraryTests/NatureLibraryTests.cpp @@ -0,0 +1,82 @@ +#ifdef TESTS_BUILD +#include "../../extern/catch.hpp" +#include "../../src/Library/Natures/NatureLibrary.hpp" +#include "../../src/Library/Statistic.hpp" + +using namespace PkmnLib::Library; + +TEST_CASE("Able to create and delete nature library", "library") { + auto lib = new NatureLibrary(); + delete lib; +} + +TEST_CASE("Able to insert into nature library", "library") { + auto lib = new NatureLibrary(); + lib->LoadNature("testNature", Nature(Statistic::PhysicalAttack, Statistic::Speed)); + delete lib; +} + +TEST_CASE("Able to retrieve nature by name", "library") { + auto lib = new NatureLibrary(); + lib->LoadNature("testNature", Nature(Statistic::PhysicalAttack, Statistic::Speed)); + + auto nature = lib->GetNatureByName("testNature"); + REQUIRE(nature.GetIncreasedStat() == Statistic::PhysicalAttack); + REQUIRE(nature.GetDecreasedStat() == Statistic::Speed); + REQUIRE(nature.GetIncreaseModifier() == 1.1f); + REQUIRE(nature.GetDecreaseModifier() == 0.9f); + + delete lib; +} + +TEST_CASE("Able to retrieve nature by id", "library") { + auto lib = new NatureLibrary(); + auto id = lib->LoadNature("testNature", Nature(Statistic::PhysicalAttack, Statistic::Speed)); + + auto nature = lib->GetNature(id); + REQUIRE(nature.GetIncreasedStat() == Statistic::PhysicalAttack); + REQUIRE(nature.GetDecreasedStat() == Statistic::Speed); + REQUIRE(nature.GetIncreaseModifier() == 1.1f); + REQUIRE(nature.GetDecreaseModifier() == 0.9f); + + delete lib; +} + +TEST_CASE("Able to retrieve nature id by name", "library") { + auto lib = new NatureLibrary(); + lib->LoadNature("testNature", Nature(Statistic::PhysicalAttack, Statistic::Speed)); + auto id = lib->GetNatureIdByName("testNature"); + REQUIRE(id == 0); + auto nature = lib->GetNature(id); + REQUIRE(nature.GetIncreasedStat() == Statistic::PhysicalAttack); + REQUIRE(nature.GetDecreasedStat() == Statistic::Speed); + REQUIRE(nature.GetIncreaseModifier() == 1.1f); + REQUIRE(nature.GetDecreaseModifier() == 0.9f); + + delete lib; +} + +TEST_CASE("Able to insert and retrieve multiple natures", "library") { + auto lib = new NatureLibrary(); + auto id = lib->LoadNature("testNature", Nature(Statistic::PhysicalAttack, Statistic::Speed)); + REQUIRE(id == 0); + auto id2 = lib->LoadNature("testNature2", Nature(Statistic::Speed, Statistic::PhysicalAttack)); + REQUIRE(id2 == 1); + auto nature = lib->GetNature(id); + REQUIRE(nature.GetIncreasedStat() == Statistic::PhysicalAttack); + REQUIRE(nature.GetDecreasedStat() == Statistic::Speed); + REQUIRE(nature.GetIncreaseModifier() == 1.1f); + REQUIRE(nature.GetDecreaseModifier() == 0.9f); + nature = lib->GetNature(id2); + REQUIRE(nature.GetIncreasedStat() == Statistic::Speed); + REQUIRE(nature.GetDecreasedStat() == Statistic::PhysicalAttack); + REQUIRE(nature.GetIncreaseModifier() == 1.1f); + REQUIRE(nature.GetDecreaseModifier() == 0.9f); + + delete lib; +} + + + + +#endif \ No newline at end of file