From 2d0613b40bd3e99a3cf7f2233677ced0defbb1a7 Mon Sep 17 00:00:00 2001 From: AlexVinS Date: Sun, 9 Jul 2017 22:15:06 +0300 Subject: [PATCH] Added some CHealth tests. --- lib/CStack.cpp | 6 +- lib/CStack.h | 1 + test/CMakeLists.txt | 1 + test/battle/CHealthTest.cpp | 150 ++++++++++++++++++++++++++++++++++++ 4 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 test/battle/CHealthTest.cpp diff --git a/lib/CStack.cpp b/lib/CStack.cpp index 3b3ca7a57..f6d765701 100644 --- a/lib/CStack.cpp +++ b/lib/CStack.cpp @@ -202,10 +202,10 @@ void CHealth::heal(int32_t & amount, EHealLevel level, EHealPower power) if(amount == 0) return; - int64_t totalHealth = total(); + int64_t availableHealth = available(); - totalHealth += amount; - setFromTotal(totalHealth); + availableHealth += amount; + setFromTotal(availableHealth); if(power == EHealPower::ONE_BATTLE) addResurrected(getCount() - oldCount); diff --git a/lib/CStack.h b/lib/CStack.h index 101fd6895..17a40f2af 100644 --- a/lib/CStack.h +++ b/lib/CStack.h @@ -7,6 +7,7 @@ * Full text of license available in license.txt file, in main folder * */ + #pragma once #include "battle/BattleHex.h" #include "CCreatureHandler.h" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ee6f4ed7d..9b37330e2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -13,6 +13,7 @@ set(test_SRCS CMemoryBufferTest.cpp CVcmiTestConfig.cpp MapComparer.cpp + battle/CHealthTest.cpp ) set(test_HEADERS diff --git a/test/battle/CHealthTest.cpp b/test/battle/CHealthTest.cpp new file mode 100644 index 000000000..c752a8b7d --- /dev/null +++ b/test/battle/CHealthTest.cpp @@ -0,0 +1,150 @@ +/* + * CHealthTest.cpp, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ + +#include "StdInc.h" +#include +#include "../../lib/CStack.h" + +class CUnitHealthInfoMock : public IUnitHealthInfo +{ +public: + CUnitHealthInfoMock(): + maxHealth(123), + baseAmount(300) + {} + + int32_t maxHealth; + int32_t baseAmount; + + int32_t unitMaxHealth() const override + { + return maxHealth; + }; + + int32_t unitBaseAmount() const override + { + return baseAmount; + }; +}; + +static void checkTotal(const CHealth & health, const CUnitHealthInfoMock & mock) +{ + BOOST_CHECK_EQUAL(health.total(), mock.maxHealth * mock.baseAmount); +} + +static void checkEmptyHealth(const CHealth & health, const CUnitHealthInfoMock & mock) +{ + checkTotal(health, mock); + BOOST_CHECK_EQUAL(health.getCount(), 0); + BOOST_CHECK_EQUAL(health.getFirstHPleft(), 0); + BOOST_CHECK_EQUAL(health.getResurrected(), 0); + BOOST_CHECK_EQUAL(health.available(), 0); +} + +static void checkFullHealth(const CHealth & health, const CUnitHealthInfoMock & mock) +{ + checkTotal(health, mock); + BOOST_CHECK_EQUAL(health.getCount(), mock.baseAmount); + BOOST_CHECK_EQUAL(health.getFirstHPleft(), mock.maxHealth); + BOOST_CHECK_EQUAL(health.getResurrected(), 0); + BOOST_CHECK_EQUAL(health.available(), mock.maxHealth * mock.baseAmount); +} + +static void checkDamage(CHealth & health, const int32_t initialDamage, const int32_t expectedDamage) +{ + int32_t damage = initialDamage; + health.damage(damage); + BOOST_CHECK_EQUAL(damage, expectedDamage); +} + +static void checkNormalDamage(CHealth & health, const int32_t initialDamage) +{ + checkDamage(health, initialDamage, initialDamage); +} + +static void checkNoDamage(CHealth & health, const int32_t initialDamage) +{ + checkDamage(health, initialDamage, 0); +} + +static void checkHeal(CHealth & health, EHealLevel level, EHealPower power, const int32_t initialHeal, const int32_t expectedHeal) +{ + int32_t heal = initialHeal; + health.heal(heal, level, power); + BOOST_CHECK_EQUAL(heal, expectedHeal); +} + +BOOST_AUTO_TEST_SUITE(CHealthTest_Suite) + +BOOST_AUTO_TEST_CASE(empty) +{ + CUnitHealthInfoMock uhi; + CHealth health(&uhi); + checkEmptyHealth(health, uhi); + + health.init(); + checkFullHealth(health, uhi); + + health.reset(); + checkEmptyHealth(health, uhi); +} + +BOOST_AUTO_TEST_CASE(damage) +{ + CUnitHealthInfoMock uhi; + CHealth health(&uhi); + health.init(); + + checkNormalDamage(health, 0); + checkFullHealth(health, uhi); + + checkNormalDamage(health, uhi.maxHealth - 1); + BOOST_CHECK_EQUAL(health.getCount(), uhi.baseAmount); + BOOST_CHECK_EQUAL(health.getFirstHPleft(), 1); + BOOST_CHECK_EQUAL(health.getResurrected(), 0); + + checkNormalDamage(health, 1); + BOOST_CHECK_EQUAL(health.getCount(), uhi.baseAmount - 1); + BOOST_CHECK_EQUAL(health.getFirstHPleft(), uhi.maxHealth); + BOOST_CHECK_EQUAL(health.getResurrected(), 0); + + checkNormalDamage(health, uhi.maxHealth * (uhi.baseAmount - 1)); + checkEmptyHealth(health, uhi); + + checkNoDamage(health, 1337); + checkEmptyHealth(health, uhi); +} + +BOOST_AUTO_TEST_CASE(heal) +{ + CUnitHealthInfoMock uhi; + CHealth health(&uhi); + health.init(); + checkNormalDamage(health, 99); + BOOST_CHECK_EQUAL(health.getCount(), uhi.baseAmount); + BOOST_CHECK_EQUAL(health.getFirstHPleft(), uhi.maxHealth-99); + BOOST_CHECK_EQUAL(health.getResurrected(), 0); + + checkHeal(health, EHealLevel::HEAL, EHealPower::PERMANENT, 9, 9); + BOOST_CHECK_EQUAL(health.getCount(), uhi.baseAmount); + BOOST_CHECK_EQUAL(health.getFirstHPleft(), uhi.maxHealth-90); + BOOST_CHECK_EQUAL(health.getResurrected(), 0); + + checkHeal(health, EHealLevel::RESURRECT, EHealPower::ONE_BATTLE, 40, 40); + BOOST_CHECK_EQUAL(health.getCount(), uhi.baseAmount); + BOOST_CHECK_EQUAL(health.getFirstHPleft(), uhi.maxHealth-50); + BOOST_CHECK_EQUAL(health.getResurrected(), 0); + + checkHeal(health, EHealLevel::OVERHEAL, EHealPower::PERMANENT, 50, 50); + checkFullHealth(health, uhi); +} + +BOOST_AUTO_TEST_SUITE_END() +