Tests and tweaks for the Nature Library.

This commit is contained in:
Deukhoofd 2019-12-31 10:39:20 +01:00
parent bb1791b3ae
commit e5cb857ea8
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
3 changed files with 105 additions and 10 deletions

View File

@ -5,12 +5,21 @@
namespace PkmnLib::Library { namespace PkmnLib::Library {
class Nature { class Nature {
private: private:
float _increaseModifier;
float _decreaseModifier;
CreatureLib::Core::Statistic _increaseStat; CreatureLib::Core::Statistic _increaseStat;
CreatureLib::Core::Statistic _decreaseStat; CreatureLib::Core::Statistic _decreaseStat;
float _increaseModifier;
float _decreaseModifier;
public: 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 GetIncreaseModifier() const { return _increaseModifier; }
[[nodiscard]] float GetDecreaseModifier() const { return _decreaseModifier; } [[nodiscard]] float GetDecreaseModifier() const { return _decreaseModifier; }

View File

@ -10,16 +10,23 @@ namespace PkmnLib::Library {
private: private:
std::unordered_map<std::string, uint8_t> _keyLookup; std::unordered_map<std::string, uint8_t> _keyLookup;
std::vector<Nature> _natures; std::vector<Nature> _natures;
uint8_t _current = 0;
public: public:
void LoadNature(const std::string& name, const Nature& nature) { NatureLibrary(uint8_t size = 32)
: _keyLookup(std::unordered_map<std::string, uint8_t>(size)), _natures(std::vector<Nature>(size)) {}
uint8_t LoadNature(const std::string& name, const Nature& nature) {
auto find = _keyLookup.find(name); auto find = _keyLookup.find(name);
if (find != _keyLookup.end()) { if (find != _keyLookup.end()) {
auto key = _keyLookup[name]; auto key = _keyLookup[name];
_natures[key] = nature; _natures[key] = nature;
return key;
} else { } else {
_keyLookup[name] = _natures.size(); auto key = _current;
_natures.push_back(nature); _keyLookup[name] = key;
_natures[_current++] = nature;
return key;
} }
} }
@ -31,17 +38,14 @@ namespace PkmnLib::Library {
return _natures[_keyLookup.at(name)]; return _natures[_keyLookup.at(name)];
} }
const Nature& GetNature(uint8_t id) const{ const Nature& GetNature(uint8_t id) const { return _natures[id]; }
return _natures[id];
}
uint8_t GetNatureId(const std::string& name) const{ uint8_t GetNatureIdByName(const std::string& name) const {
auto find = _keyLookup.find(name); auto find = _keyLookup.find(name);
if (find == _keyLookup.end()) { if (find == _keyLookup.end()) {
throw CreatureException("Invalid nature name."); throw CreatureException("Invalid nature name.");
} }
return _keyLookup.at(name); return _keyLookup.at(name);
} }
}; };
} }

View File

@ -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