Tests and tweaks for the Nature Library.
This commit is contained in:
parent
bb1791b3ae
commit
e5cb857ea8
|
@ -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; }
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue