Move several classes from Core to Arbutils.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2020-02-26 13:51:16 +01:00
parent 428b318baf
commit 5e6572aca5
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
24 changed files with 51 additions and 384 deletions

View File

@ -8,12 +8,24 @@ project(CreatureLib)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if (EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
else ()
message(WARNING "The file conanbuildinfo.cmake doesn't exist, you have to run conan install first")
if (NOT EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
message(WARNING "The file conanbuildinfo.cmake doesn't exist, running conan install.")
string(REPLACE "." ";" VERSION_LIST "${CMAKE_C_COMPILER_VERSION}")
list(GET VERSION_LIST 0 VERSION)
list(GET VERSION_LIST 1 MINOR)
if (NOT MINOR MATCHES 0)
SET(VERSION ${VERSION}.${MINOR})
endif ()
if (NOT WINDOWS)
execute_process(COMMAND conan install ${CMAKE_SOURCE_DIR} --install-folder=${CMAKE_BINARY_DIR} --build missing
-s compiler=clang -s compiler.libcxx=libstdc++11 -s compiler.version=${VERSION})
else ()
execute_process(COMMAND conan install ${CMAKE_SOURCE_DIR} --install-folder=${CMAKE_BINARY_DIR} --build missing
-s compiler=gcc -s compiler.libcxx=libstdc++11 -s compiler.version=${VERSION} -s os=Windows)
endif ()
endif ()
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
if (WINDOWS)
ADD_DEFINITIONS(-D WINDOWS=1)
@ -22,6 +34,7 @@ endif (WINDOWS)
# Create Core library with files in src/Core
file(GLOB_RECURSE CORE_SRC_FILES "src/Core/*.cpp" "src/Core/*.hpp")
add_library(CreatureLibCore SHARED ${CORE_SRC_FILES})
set_target_properties(CreatureLibCore PROPERTIES LINKER_LANGUAGE CXX)
# Create main Library library with files in src/Library
file(GLOB_RECURSE LIBRARY_SRC_FILES "src/Library/*.cpp" "src/Library/*.hpp")
@ -44,6 +57,11 @@ target_link_libraries(CreatureLibBattling CreatureLibCore)
# Link the library data to the Battling library
target_link_libraries(CreatureLibBattling CreatureLibLibrary)
target_link_libraries(CreatureLibCore Arbutils)
target_link_libraries(CreatureLibLibrary Arbutils)
target_link_libraries(CreatureLibBattling Arbutils)
if (NOT DEFINED CONAN_EXPORTED)
target_link_libraries(CreatureLibTests CreatureLibLibrary)
target_link_libraries(CreatureLibTests CreatureLibBattling)

View File

@ -33,3 +33,6 @@ class CreatureLibConan(ConanFile):
def package_info(self):
self.cpp_info.libs = ["CreatureLibCore", "CreatureLibLibrary", "CreatureLibBattling"]
def requirements(self):
self.requires("Arbutils/latest@epsilon/master")

View File

@ -6,7 +6,7 @@
using namespace CreatureLib;
using namespace Battling;
bool ___ChoiceOrderFunc(const BaseTurnChoice* a, const BaseTurnChoice* b, Core::Random& rand) {
bool ___ChoiceOrderFunc(const BaseTurnChoice* a, const BaseTurnChoice* b, Arbutils::Random& rand) {
auto aKind = a->GetKind();
auto bKind = b->GetKind();
if (aKind != bKind)
@ -26,7 +26,7 @@ bool ___ChoiceOrderFunc(const BaseTurnChoice* a, const BaseTurnChoice* b, Core::
return randomValue == 0;
}
void TurnOrdering::OrderChoices(std::vector<BaseTurnChoice*>& vec, Core::Random& rand) {
void TurnOrdering::OrderChoices(std::vector<BaseTurnChoice*>& vec, Arbutils::Random& rand) {
auto comp = [&](const BaseTurnChoice* a, const BaseTurnChoice* b) -> bool {
return ___ChoiceOrderFunc(a, b, rand);
};

View File

@ -1,14 +1,14 @@
#ifndef CREATURELIB_TURNORDERING_HPP
#define CREATURELIB_TURNORDERING_HPP
#include <Arbutils/Random.hpp>
#include <vector>
#include "../../Core/Random.hpp"
#include "../TurnChoices/BaseTurnChoice.hpp"
namespace CreatureLib::Battling {
class TurnOrdering {
public:
static void OrderChoices(std::vector<BaseTurnChoice*>& vec, CreatureLib::Core::Random& rand);
static void OrderChoices(std::vector<BaseTurnChoice*>& vec, Arbutils::Random& rand);
};
}

View File

@ -1,7 +1,7 @@
#ifndef CREATURELIB_BATTLERANDOM_HPP
#define CREATURELIB_BATTLERANDOM_HPP
#include "../../Core/Random.hpp"
#include <Arbutils/Random.hpp>
namespace CreatureLib::Battling {
class ExecutingAttack;
@ -9,7 +9,7 @@ namespace CreatureLib::Battling {
class BattleRandom {
private:
Core::Random _random;
Arbutils::Random _random;
public:
BattleRandom() : _random() {}
@ -19,7 +19,7 @@ namespace CreatureLib::Battling {
int32_t Get() { return _random.Get(); }
int32_t Get(int32_t max) { return _random.Get(max); }
int32_t Get(int32_t min, int32_t max) { return _random.Get(min, max); }
Core::Random& GetRNG() { return _random; }
Arbutils::Random& GetRNG() { return _random; }
};
}

View File

@ -30,7 +30,7 @@ CreateCreature* CreateCreature::WithAttack(const std::string& attackName, Attack
}
Creature* CreateCreature::Create() {
auto rand = Core::Random();
auto rand = Arbutils::Random();
auto species = this->_library->GetSpeciesLibrary()->Get(this->_species.c_str());
auto variant = species->GetVariant(this->_variant);
int8_t talent;

View File

@ -1,8 +1,8 @@
#ifndef CREATURELIB_SCRIPTRESOLVER_HPP
#define CREATURELIB_SCRIPTRESOLVER_HPP
#include <Arbutils/Enum.hpp>
#include <string>
#include "../../Core/Enum.hpp"
#include "Script.hpp"
#include "ScriptCategory.hpp"

View File

@ -1,73 +0,0 @@
#include <algorithm>
#include <sstream>
#include <vector>
#include "Exceptions/NotReachableException.hpp"
#include "MacroUtils.hpp"
#define ENUM_CASE(x, name) \
case name::x: \
return #x;
#
#define ENUM_PARSE_CASE(x, name) \
case ConstHash(#x): \
return name::x;
#
#define ENUM_TRY_PARSE_CASE(x, name) \
case ConstHash(#x): \
out = name::x; \
return true;
#
#define ENUM_PARSE_CASE_INSENSITIVE(x, name) \
case ConstHashCI(#x): \
return name::x;
#
#define ENUM_TRY_PARSE_CASE_INSENSITIVE(x, name) \
case ConstHashCI(#x): \
out = name::x; \
return true;
#
#define ARRAY_NAME(x, name) name::x,
#define ENUM(name, type, values...) \
enum class name : type { values }; \
class name##Helper { \
inline static uint32_t constexpr ConstHash(char const* input) { \
return *input ? static_cast<uint32_t>(*input) + 33 * ConstHash(input + 1) : 5381; \
} \
inline static constexpr char charToLower(const char c) { \
return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c; \
} \
inline static uint32_t constexpr ConstHashCI(char const* input) { \
return charToLower(*input) ? static_cast<uint32_t>(charToLower(*input)) + 33 * ConstHashCI(input + 1) \
: 5381; \
} \
\
public: \
constexpr static const char* ToString(name value) { \
switch (value) { FOR_EACH(ENUM_CASE, name, values) } \
throw NotReachableException(); \
} \
constexpr static name Parse(const char* input, bool caseInsensitive = false) { \
if (caseInsensitive) \
return ParseCaseInsensitive(input); \
switch (ConstHash(input)) { FOR_EACH(ENUM_PARSE_CASE, name, values) } \
throw CreatureException("Invalid AttackCategory string."); \
} \
constexpr static bool TryParse(const char* input, name& out, bool caseInsensitive = false) { \
if (caseInsensitive) \
return TryParseCaseInsensitive(input, out); \
switch (ConstHash(input)) { FOR_EACH(ENUM_TRY_PARSE_CASE, name, values) } \
return false; \
} \
static std::vector<name> GetValues() { return {FOR_EACH(ARRAY_NAME, name, values)}; } \
\
private: \
static name ParseCaseInsensitive(const char* input) { \
switch (ConstHashCI(input)) { FOR_EACH(ENUM_PARSE_CASE_INSENSITIVE, name, values) } \
throw CreatureException("Invalid AttackCategory string."); \
} \
static bool TryParseCaseInsensitive(const char* input, name& out) { \
switch (ConstHashCI(input)) { FOR_EACH(ENUM_TRY_PARSE_CASE_INSENSITIVE, name, values) } \
return false; \
} \
};

View File

@ -1,43 +0,0 @@
#define FE_0(FUNC, arg)
#define FE_1(FUNC, arg, X) FUNC(X, arg)
#define FE_2(FUNC, arg, X, ...) FUNC(X, arg) FE_1(FUNC, arg, __VA_ARGS__)
#define FE_3(FUNC, arg, X, ...) FUNC(X, arg) FE_2(FUNC, arg, __VA_ARGS__)
#define FE_4(FUNC, arg, X, ...) FUNC(X, arg) FE_3(FUNC, arg, __VA_ARGS__)
#define FE_5(FUNC, arg, X, ...) FUNC(X, arg) FE_4(FUNC, arg, __VA_ARGS__)
#define FE_6(FUNC, arg, X, ...) FUNC(X, arg) FE_5(FUNC, arg, __VA_ARGS__)
#define FE_7(FUNC, arg, X, ...) FUNC(X, arg) FE_6(FUNC, arg, __VA_ARGS__)
#define FE_8(FUNC, arg, X, ...) FUNC(X, arg) FE_7(FUNC, arg, __VA_ARGS__)
#define FE_9(FUNC, arg, X, ...) FUNC(X, arg) FE_8(FUNC, arg, __VA_ARGS__)
#define FE_10(FUNC, arg, X, ...) FUNC(X, arg) FE_9(FUNC, arg, __VA_ARGS__)
#define FE_11(FUNC, arg, X, ...) FUNC(X, arg) FE_10(FUNC, arg, __VA_ARGS__)
#define FE_12(FUNC, arg, X, ...) FUNC(X, arg) FE_11(FUNC, arg, __VA_ARGS__)
#define FE_13(FUNC, arg, X, ...) FUNC(X, arg) FE_12(FUNC, arg, __VA_ARGS__)
#define FE_14(FUNC, arg, X, ...) FUNC(X, arg) FE_13(FUNC, arg, __VA_ARGS__)
#define FE_15(FUNC, arg, X, ...) FUNC(X, arg) FE_14(FUNC, arg, __VA_ARGS__)
#define FE_16(FUNC, arg, X, ...) FUNC(X, arg) FE_15(FUNC, arg, __VA_ARGS__)
#define FE_17(FUNC, arg, X, ...) FUNC(X, arg) FE_16(FUNC, arg, __VA_ARGS__)
#define FE_18(FUNC, arg, X, ...) FUNC(X, arg) FE_17(FUNC, arg, __VA_ARGS__)
#define FE_19(FUNC, arg, X, ...) FUNC(X, arg) FE_18(FUNC, arg, __VA_ARGS__)
#define FE_20(FUNC, arg, X, ...) FUNC(X, arg) FE_19(FUNC, arg, __VA_ARGS__)
#define FE_21(FUNC, arg, X, ...) FUNC(X, arg) FE_20(FUNC, arg, __VA_ARGS__)
#define FE_22(FUNC, arg, X, ...) FUNC(X, arg) FE_21(FUNC, arg, __VA_ARGS__)
#define FE_23(FUNC, arg, X, ...) FUNC(X, arg) FE_22(FUNC, arg, __VA_ARGS__)
#define FE_24(FUNC, arg, X, ...) FUNC(X, arg) FE_23(FUNC, arg, __VA_ARGS__)
#define FE_25(FUNC, arg, X, ...) FUNC(X, arg) FE_24(FUNC, arg, __VA_ARGS__)
#define FE_26(FUNC, arg, X, ...) FUNC(X, arg) FE_25(FUNC, arg, __VA_ARGS__)
#define FE_27(FUNC, arg, X, ...) FUNC(X, arg) FE_26(FUNC, arg, __VA_ARGS__)
#define FE_28(FUNC, arg, X, ...) FUNC(X, arg) FE_27(FUNC, arg, __VA_ARGS__)
#define FE_29(FUNC, arg, X, ...) FUNC(X, arg) FE_28(FUNC, arg, __VA_ARGS__)
#define FE_30(FUNC, arg, X, ...) FUNC(X, arg) FE_29(FUNC, arg, __VA_ARGS__)
#define FE_31(FUNC, arg, X, ...) FUNC(X, arg) FE_30(FUNC, arg, __VA_ARGS__)
#define FE_32(FUNC, arg, X, ...) FUNC(X, arg) FE_31(FUNC, arg, __VA_ARGS__)
#define FE_33(FUNC, arg, X, ...) THE_FOREACH_MACRO_CURRENTLY_ONLY_SUPPORTS_UP_TO_32_VALUES FE_32(FUNC, arg, __VA_ARGS__)
#define GET_MACRO(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, \
_22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, NAME, ...) \
NAME
#define FOR_EACH(action, arg, ...) \
GET_MACRO(_0, __VA_ARGS__, FE_33, FE_32, FE_31, FE_30, FE_29, FE_28, FE_27, FE_26, FE_25, FE_24, FE_23, FE_22, \
FE_21, FE_20, FE_19, FE_18, FE_17, FE_16, FE_15, FE_14, FE_13, FE_12, FE_11, FE_10, FE_9, FE_8, FE_7, \
FE_6, FE_5, FE_4, FE_3, FE_2, FE_1, FE_0) \
(action, arg, __VA_ARGS__)

View File

@ -1,26 +0,0 @@
#include "Random.hpp"
#include <limits>
// Seed parameterless constructor with current milliseconds since epoch.
CreatureLib::Core::Random::Random()
: _rng(std::mt19937(duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count())) {}
float CreatureLib::Core::Random::GetFloat() {
return static_cast<float>(_rng()) / (static_cast<float>(std::mt19937::max() - std::mt19937::min()) - 0.5f);
}
double CreatureLib::Core::Random::GetDouble() {
return static_cast<double>(_rng()) / ((static_cast<double>(std::mt19937::max()) - std::mt19937::min()) - 0.5);
}
int32_t CreatureLib::Core::Random::Get() {
return static_cast<int32_t>(GetDouble() * static_cast<float>(std::numeric_limits<int32_t>::max()));
}
int32_t CreatureLib::Core::Random::Get(int32_t max) {
return static_cast<int32_t>(GetDouble() * static_cast<float>(max));
}
int32_t CreatureLib::Core::Random::Get(int32_t min, int32_t max) {
return static_cast<int32_t>(GetDouble() * static_cast<float>(max - min) + min);
}

View File

@ -1,24 +0,0 @@
#ifndef CREATURELIB_RANDOM_HPP
#define CREATURELIB_RANDOM_HPP
#include <chrono>
#include <cstdint>
#include <random>
using namespace std::chrono;
namespace CreatureLib::Core {
class Random {
private:
std::mt19937 _rng;
public:
Random();
explicit Random(int32_t seed) : _rng(seed){};
[[nodiscard]] float GetFloat();
[[nodiscard]] double GetDouble();
[[nodiscard]] int32_t Get();
[[nodiscard]] int32_t Get(int32_t max);
[[nodiscard]] int32_t Get(int32_t min, int32_t max);
};
}
#endif // CREATURELIB_RANDOM_HPP

View File

@ -1,8 +1,8 @@
#ifndef CREATURELIB_STATISTIC_HPP
#define CREATURELIB_STATISTIC_HPP
#include <Arbutils/Enum.hpp>
#include <cstdint>
#include "Enum.hpp"
namespace CreatureLib::Core {
ENUM(Statistic, uint8_t, Health, PhysicalAttack, PhysicalDefense, MagicalAttack, MagicalDefense, Speed)

View File

@ -1,8 +1,8 @@
#ifndef CREATURELIB_ATTACKCATEGORY_HPP
#define CREATURELIB_ATTACKCATEGORY_HPP
#include <Arbutils/Enum.hpp>
#include <cstdint>
#include "../../Core/Enum.hpp"
namespace CreatureLib::Library {
ENUM(AttackCategory, uint8_t, Physical, Magical, Status)

View File

@ -1,8 +1,8 @@
#ifndef CREATURELIB_ATTACKTARGET_HPP
#define CREATURELIB_ATTACKTARGET_HPP
#include <Arbutils/Enum.hpp>
#include <cstdint>
#include "../../Core/Enum.hpp"
namespace CreatureLib::Library {
ENUM(AttackTarget, uint8_t, Adjacent, AdjacentAlly, AdjacentAllySelf, AdjacentOpponent,

View File

@ -40,7 +40,7 @@ void CreatureSpecies::SetVariant(const std::string& name, const SpeciesVariant*
_variants[key] = variant;
}
Gender CreatureSpecies::GetRandomGender(CreatureLib::Core::Random& rand) const {
Gender CreatureSpecies::GetRandomGender(Arbutils::Random& rand) const {
// TODO: Genderless creatures
auto val = rand.GetDouble();
if (val >= this->_genderRate)

View File

@ -39,7 +39,7 @@ namespace CreatureLib::Library {
[[nodiscard]] bool HasVariant(const std::string& key) const;
[[nodiscard]] bool TryGetVariant(const std::string& name, const SpeciesVariant*& out) const;
[[nodiscard]] const SpeciesVariant* GetVariant(const std::string& key) const;
[[nodiscard]] Gender GetRandomGender(Core::Random& rand) const;
[[nodiscard]] Gender GetRandomGender(Arbutils::Random& rand) const;
[[nodiscard]] const std::string& GetName() const;
void SetVariant(const std::string& name, const SpeciesVariant* variant);

View File

@ -36,7 +36,7 @@ int8_t CreatureLib::Library::SpeciesVariant::GetTalentIndex(const std::string& t
throw CreatureException("The given talent is not a valid talent for this creature.");
}
int8_t CreatureLib::Library::SpeciesVariant::GetRandomTalent(CreatureLib::Core::Random* rand) const {
int8_t CreatureLib::Library::SpeciesVariant::GetRandomTalent(Arbutils::Random* rand) const {
return rand->Get(_talents.size());
}

View File

@ -1,9 +1,9 @@
#ifndef CREATURELIB_SPECIESVARIANT_HPP
#define CREATURELIB_SPECIESVARIANT_HPP
#include <Arbutils/Random.hpp>
#include <string>
#include <vector>
#include "../../Core/Random.hpp"
#include "../../Core/StatisticSet.hpp"
#include "CreatureMoves.hpp"
#include "LearnableAttacks.hpp"
@ -46,7 +46,7 @@ namespace CreatureLib::Library {
[[nodiscard]] const std::string& GetTalent(int32_t index) const;
[[nodiscard]] const LearnableAttacks* GetLearnableAttacks() const;
[[nodiscard]] int8_t GetTalentIndex(const std::string& talent) const;
[[nodiscard]] int8_t GetRandomTalent(Core::Random* rand) const;
[[nodiscard]] int8_t GetRandomTalent(Arbutils::Random* rand) const;
[[nodiscard]] inline const std::vector<std::string>& GetTalents() const { return _talents; }
[[nodiscard]] inline const std::vector<std::string>& GetSecretTalents() const { return _secretTalents; }
};

View File

@ -1,8 +1,8 @@
#ifndef CREATURELIB_GENDER_HPP
#define CREATURELIB_GENDER_HPP
#include <Arbutils/Enum.hpp>
#include <cstdint>
#include "../Core/Enum.hpp"
namespace CreatureLib::Library {
/*!

View File

@ -1,8 +1,8 @@
#ifndef CREATURELIB_BATTLEITEMCATEGORY_HPP
#define CREATURELIB_BATTLEITEMCATEGORY_HPP
#include <Arbutils/Enum.hpp>
#include <cstdint>
#include "../../Core/Enum.hpp"
namespace CreatureLib::Library {
ENUM(BattleItemCategory, uint8_t, None, Healing, StatusHealing, CaptureDevice, MiscBattleItem)

View File

@ -1,8 +1,8 @@
#ifndef CREATURELIB_ITEMCATEGORY_HPP
#define CREATURELIB_ITEMCATEGORY_HPP
#include <Arbutils/Enum.hpp>
#include <cstdint>
#include "../../Core/Enum.hpp"
namespace CreatureLib::Library {
ENUM(ItemCategory, uint8_t, MiscItem, CaptureDevice, Medicine, Berry, MoveLearner, VariantChanger, KeyItem, Mail)

View File

@ -13,7 +13,7 @@ TEST_CASE("Turn ordering: Attack before pass", "[Battling]") {
auto choice1 = new PassTurnChoice(nullptr);
auto choice2 = new AttackTurnChoice(nullptr, nullptr, CreatureIndex(0, 0));
auto vec = std::vector<BaseTurnChoice*>{choice1, choice2};
auto rand = Core::Random();
auto rand = Arbutils::Random();
TurnOrdering::OrderChoices(vec, rand);
CHECK(vec[0] == choice2);
CHECK(vec[1] == choice1);
@ -33,7 +33,7 @@ TEST_CASE("Turn ordering: High priority goes before no priority", "[Battling]")
auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0));
auto choice2 = new AttackTurnChoice(nullptr, a2, CreatureIndex(0, 0));
auto vec = std::vector<BaseTurnChoice*>{choice1, choice2};
auto rand = Core::Random();
auto rand = Arbutils::Random();
TurnOrdering::OrderChoices(vec, rand);
CHECK(vec[0] == choice2);
CHECK(vec[1] == choice1);
@ -55,7 +55,7 @@ TEST_CASE("Turn ordering: Higher priority goes before high priority", "[Battling
auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0));
auto choice2 = new AttackTurnChoice(nullptr, a2, CreatureIndex(0, 0));
auto vec = std::vector<BaseTurnChoice*>{choice1, choice2};
auto rand = Core::Random();
auto rand = Arbutils::Random();
TurnOrdering::OrderChoices(vec, rand);
CHECK(vec[0] == choice2);
CHECK(vec[1] == choice1);
@ -77,7 +77,7 @@ TEST_CASE("Turn ordering: High priority goes before low priority", "[Battling]")
auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0));
auto choice2 = new AttackTurnChoice(nullptr, a2, CreatureIndex(0, 0));
auto vec = std::vector<BaseTurnChoice*>{choice1, choice2};
auto rand = Core::Random();
auto rand = Arbutils::Random();
TurnOrdering::OrderChoices(vec, rand);
CHECK(vec[0] == choice2);
CHECK(vec[1] == choice1);
@ -99,7 +99,7 @@ TEST_CASE("Turn ordering: No priority goes before low priority", "[Battling]") {
auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0));
auto choice2 = new AttackTurnChoice(nullptr, a2, CreatureIndex(0, 0));
auto vec = std::vector<BaseTurnChoice*>{choice1, choice2};
auto rand = Core::Random();
auto rand = Arbutils::Random();
TurnOrdering::OrderChoices(vec, rand);
CHECK(vec[0] == choice2);
CHECK(vec[1] == choice1);

View File

@ -1,69 +0,0 @@
#ifdef TESTS_BUILD
#include <cstring>
#include "../extern/catch.hpp"
#include "../src/Core/Enum.hpp"
ENUM(TestEnum, uint8_t, Val1, Val2, Val3)
TEST_CASE("Parse Enum case sensitive", "[Utilities]") {
CHECK(TestEnumHelper::Parse("Val1") == TestEnum::Val1);
CHECK(TestEnumHelper::Parse("Val2") == TestEnum::Val2);
CHECK(TestEnumHelper::Parse("Val3") == TestEnum::Val3);
CHECK_THROWS(TestEnumHelper::Parse("Val4"));
CHECK_THROWS(TestEnumHelper::Parse("val1"));
}
TEST_CASE("Try Parse Enum case sensitive", "[Utilities]") {
TestEnum v = static_cast<TestEnum>(255);
REQUIRE(TestEnumHelper::TryParse("Val1", v));
CHECK(v == TestEnum::Val1);
REQUIRE(TestEnumHelper::TryParse("Val2", v));
CHECK(v == TestEnum::Val2);
REQUIRE(TestEnumHelper::TryParse("Val3", v));
CHECK(v == TestEnum::Val3);
CHECK_FALSE(TestEnumHelper::TryParse("Val4", v));
CHECK_FALSE(TestEnumHelper::TryParse("val1", v));
}
TEST_CASE("Parse Enum case insensitive", "[Utilities]") {
CHECK(TestEnumHelper::Parse("Val1", true) == TestEnum::Val1);
CHECK(TestEnumHelper::Parse("Val2", true) == TestEnum::Val2);
CHECK(TestEnumHelper::Parse("Val3", true) == TestEnum::Val3);
CHECK(TestEnumHelper::Parse("val1", true) == TestEnum::Val1);
CHECK(TestEnumHelper::Parse("vAL2", true) == TestEnum::Val2);
CHECK(TestEnumHelper::Parse("VaL3", true) == TestEnum::Val3);
CHECK_THROWS(TestEnumHelper::Parse("Val4", true));
}
TEST_CASE("Try Parse Enum case insensitive", "[Utilities]") {
TestEnum v = static_cast<TestEnum>(255);
REQUIRE(TestEnumHelper::TryParse("Val1", v, true));
CHECK(v == TestEnum::Val1);
REQUIRE(TestEnumHelper::TryParse("Val2", v, true));
CHECK(v == TestEnum::Val2);
REQUIRE(TestEnumHelper::TryParse("Val3", v, true));
CHECK(v == TestEnum::Val3);
REQUIRE(TestEnumHelper::TryParse("val1", v, true));
CHECK(v == TestEnum::Val1);
REQUIRE(TestEnumHelper::TryParse("vAL2", v, true));
CHECK(v == TestEnum::Val2);
REQUIRE(TestEnumHelper::TryParse("VaL3", v, true));
CHECK(v == TestEnum::Val3);
CHECK_FALSE(TestEnumHelper::TryParse("Val4", v, true));
}
TEST_CASE("Enum To String", "[Utilities]") {
CHECK(strcmp(TestEnumHelper::ToString(TestEnum::Val1), "Val1") == 0);
CHECK(strcmp(TestEnumHelper::ToString(TestEnum::Val2), "Val2") == 0);
CHECK(strcmp(TestEnumHelper::ToString(TestEnum::Val3), "Val3") == 0);
}
TEST_CASE("Enum Get Values", "[Utilities]") {
auto vec = TestEnumHelper::GetValues();
REQUIRE(vec.size() == 3);
CHECK(vec[0] == TestEnum::Val1);
CHECK(vec[1] == TestEnum::Val2);
CHECK(vec[2] == TestEnum::Val3);
}
#endif

View File

@ -1,119 +0,0 @@
#ifdef TESTS_BUILD
#include "../extern/catch.hpp"
#include "../src/Core/Exceptions/CreatureException.hpp"
#include "../src/Core/Random.hpp"
TEST_CASE("Random ints", "[Utilities]") {
auto rand = CreatureLib::Core::Random(10);
CHECK(rand.Get() == 1656398469);
CHECK(rand.Get() == 641584702);
CHECK(rand.Get() == 44564466);
CHECK(rand.Get() == 1062123783);
CHECK(rand.Get() == 1360749216);
CHECK(rand.Get() == 951367352);
CHECK(rand.Get() == 1608044094);
CHECK(rand.Get() == 1786516046);
CHECK(rand.Get() == 1070535660);
CHECK(rand.Get() == 1252673902);
}
TEST_CASE("Random ints with limit", "[Utilities]") {
auto rand = CreatureLib::Core::Random(10);
CHECK(rand.Get(10) == 7);
CHECK(rand.Get(10) == 2);
CHECK(rand.Get(10) == 0);
CHECK(rand.Get(10) == 4);
CHECK(rand.Get(10) == 6);
CHECK(rand.Get(10) == 4);
CHECK(rand.Get(10) == 7);
CHECK(rand.Get(10) == 8);
CHECK(rand.Get(10) == 4);
CHECK(rand.Get(10) == 5);
CHECK(rand.Get(2) == 0);
CHECK(rand.Get(2) == 0);
CHECK(rand.Get(2) == 0);
CHECK(rand.Get(2) == 1);
CHECK(rand.Get(2) == 1);
CHECK(rand.Get(2) == 0);
CHECK(rand.Get(2) == 0);
CHECK(rand.Get(2) == 0);
CHECK(rand.Get(2) == 0);
}
TEST_CASE("Random ints with upper and bottom", "[Utilities]") {
auto rand = CreatureLib::Core::Random(10);
CHECK(rand.Get(10, 30) == 25);
CHECK(rand.Get(10, 30) == 15);
CHECK(rand.Get(10, 30) == 10);
CHECK(rand.Get(10, 30) == 19);
CHECK(rand.Get(10, 30) == 22);
CHECK(rand.Get(10, 30) == 18);
CHECK(rand.Get(10, 30) == 24);
CHECK(rand.Get(10, 30) == 26);
CHECK(rand.Get(10, 30) == 19);
CHECK(rand.Get(10, 30) == 21);
}
TEST_CASE("Random distribution (max 0, min 1)", "[Utilities]") {
auto rand = CreatureLib::Core::Random(10);
const int size = 100000;
int arr[size];
for (size_t i = 0; i < size; i++) {
arr[i] = rand.Get(0, 1);
}
for (size_t i = 0; i < size; i++) {
if (arr[i] != 0)
throw CreatureException("We expected a value of 0 here, but got a " + std::to_string(arr[i]));
}
}
TEST_CASE("Random distribution (max 0, min 2)", "[Utilities]") {
auto rand = CreatureLib::Core::Random(10);
const int size = 100000;
int arr[size];
for (size_t i = 0; i < size; i++) {
arr[i] = rand.Get(0, 2);
}
auto numZeros = 0;
auto numOnes = 0;
for (size_t i = 0; i < size; i++) {
if (arr[i] != 0 && arr[i] != 1)
throw CreatureException("We expected a value of 0 or 1 here, but got a " + std::to_string(arr[i]));
if (arr[i] == 0)
numZeros++;
else
numOnes++;
}
auto div = static_cast<float>(numZeros) / static_cast<float>(numOnes);
INFO("Distribution: " << numZeros << "/" << numOnes);
CHECK(Approx(div).margin(0.01) == 1);
}
TEST_CASE("Random distribution (max 0, min 3)", "[Utilities]") {
auto rand = CreatureLib::Core::Random(10);
const int size = 100000;
int arr[size];
for (size_t i = 0; i < size; i++) {
arr[i] = rand.Get(0, 3);
}
auto numZeros = 0;
auto numOnes = 0;
auto numTwos = 0;
for (size_t i = 0; i < size; i++) {
if (arr[i] != 0 && arr[i] != 1 && arr[i] != 2)
throw CreatureException("We expected a value between 0 and 2 here, but got a " + std::to_string(arr[i]));
if (arr[i] == 0)
numZeros++;
else if (arr[i] == 1)
numOnes++;
else
numTwos++;
}
INFO("Distribution: " << numZeros << "/" << numOnes << "/" << numTwos);
CHECK(Approx(static_cast<float>(numZeros) / static_cast<float>(numOnes)).margin(0.01) == 1);
CHECK(Approx(static_cast<float>(numZeros) / static_cast<float>(numTwos)).margin(0.01) == 1);
CHECK(Approx(static_cast<float>(numOnes) / static_cast<float>(numTwos)).margin(0.01) == 1);
}
#endif