diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/get_level_device.cc | 184 | ||||
-rw-r--r-- | tests/harness.cc | 7 | ||||
-rw-r--r-- | tests/lua_get_level.cc | 135 |
3 files changed, 326 insertions, 0 deletions
diff --git a/tests/get_level_device.cc b/tests/get_level_device.cc new file mode 100644 index 00000000..43dce624 --- /dev/null +++ b/tests/get_level_device.cc @@ -0,0 +1,184 @@ +#include "lua_bind.hpp" +#include "spell_type.hpp" +#include <bandit/bandit.h> +using namespace bandit; + +// +// Declarations for testing purposes: +// + +s32b get_level_device(spell_type *spell, s32b max, s32b min, s32b device_skill, std::function<s32b(spell_type *, s32b, s32b, s32b, s32b)> lua_get_level_ = lua_get_level); + +// +// Tests +// + +go_bandit([]() { + + describe("get_level_device", []() { + + s32b passed_in_max; + s32b passed_in_min; + + // Fake get_level function we can use to detect what's being passed to the real one. + auto fake_get_level = [&](struct spell_type *spell, s32b lvl, s32b max, s32b min, s32b bonus) -> s32b { + // Store the passed input values for verification purposes + passed_in_max = max; + passed_in_min = min; + // Return the input "lvl" unmodified. + return lvl; + }; + + before_each([&]() { + // Reset saved state + passed_in_max = -1; + passed_in_min = -1; + }); + + // Magic-Device skill levels that we've tested at. + const std::vector<s32b> device_skill_values { + 15300, // @15.3 + 35300, // @35.3 + 45300, // @45.3 + 50000 // @50.0 + }; + + // "Remove Curses" spell and expected result levels. + auto remove_curses_spell = spell_type_new("TEST: Remove Curses"); + spell_type_set_difficulty(remove_curses_spell, 10, 42 /* notused */); + + const std::map<s32b, s32b> remove_curses_expected_levels { + { 15300, 7 }, + { 35300, 15 }, + { 45300, 15 }, + { 50000, 15 } + }; + + // "Wish" spell and expected result levels. + auto wish_spell = spell_type_new("TEST: Wish"); + spell_type_set_difficulty(wish_spell, 50, 42 /* notused */); + + const std::map<s32b, s32b> wish_expected_levels { + { 15300, 1 }, + { 35300, 1 }, + { 45300, 1 }, + { 50000, 2 } + }; + + // "Heal Monster" spell and expected result levels. + auto heal_monster_spell = spell_type_new("TEST: Heal Monster"); + spell_type_set_difficulty(heal_monster_spell, 3, 42 /* notused */); + + const std::map<s32b, s32b> heal_monster_expected_levels { + { 15300, 108 }, + { 35300, 152 }, + { 45300, 152 }, + { 50000, 152 } + }; + + // "Teleport Away" spell and expected result levels. + auto teleport_away_spell = spell_type_new("TEST: Teleport Away"); + spell_type_set_difficulty(teleport_away_spell, 23, 42 /* notused */); + + const std::map<s32b, s32b> teleport_away_expected_levels { + { 15300, 1 }, + { 35300, 16 }, + { 45300, 20 }, + { 50000, 20 } + }; + + // + // Basic tests for "min <= 0" and "max <= 0". + // + + it("should clamp 'min' parameter to 1", [&]() { + // Setup + s32b device_skill = 100; /* doesn't matter for this test */ + get_level_max_stick = 1; /* doesn't matter for this test */ + get_level_use_stick = 1; /* doesn't matter for this test */ + auto spell = remove_curses_spell; /* doesn't matter for this test */ + s32b max = 100; /* doesn't matter for this test */ + s32b min = 0; + // Exercise + get_level_device(spell, max, min, device_skill, fake_get_level); + // Verify + AssertThat(passed_in_min, Equals(1)); + }); + + it("should use 50 as default for 'max' parameter if zero or less", [&]() { + // Setup + s32b device_skill = 100; /* doesn't matter for this test */ + get_level_max_stick = 1; /* doesn't matter for this test */ + get_level_use_stick = 1; /* doesn't matter for this test */ + auto spell = remove_curses_spell; /* doesn't matter for this test */ + s32b max = 0; + s32b min = 25; /* doesn't matter for this test */ + // Exercise + get_level_device(spell, max, min, device_skill, fake_get_level); + // Verify + AssertThat(passed_in_max, Equals(50)); + }); + + // + // Table-driven tests derived from empirical testing + // using printf. + // + + for (auto device_skill: device_skill_values) + { + it("calculates 'Remove Curses' staff level correctly for different magic device levels" , [&] { + // Setup: Device values for Remove Curses staff + get_level_use_stick = 1; + get_level_max_stick = 15; + // Setup: Max and min + s32b max = 50; + s32b min = 1; + // Exercise + s32b actualLevel = get_level_device(remove_curses_spell, max, min, device_skill); + // Verify: Check expected levels. + AssertThat(actualLevel, Equals(remove_curses_expected_levels.at(device_skill))); + }); + + it("calculates 'Wish' staff level correctly for different magic device levels", [&] { + // Setup: Device values for Wish staff + get_level_use_stick = 1; + get_level_max_stick = 1; + // Setup: Max and min + s32b max = 50; + s32b min = 1; + // Exercise + s32b actualLevel = get_level_device(wish_spell, max, min, device_skill); + // Verify: Check expected levels. + AssertThat(actualLevel, Equals(wish_expected_levels.at(device_skill))); + }); + + it("calculates 'Heal Monster' wand level correctly for different magic device levels", [&] { + // Setup: Device values for Heal Monster wand + get_level_use_stick = 1; + get_level_max_stick = 20; + // Setup: Max and min + s32b max = 380; + s32b min = 1; + // Exercise + s32b actualLevel = get_level_device(heal_monster_spell, max, min, device_skill); + // Verify: Check expected levels. + AssertThat(actualLevel, Equals(heal_monster_expected_levels.at(device_skill))); + }); + + it("calculates 'Teleport Away' wand level correctly for different magic device levels", [&] { + // Setup: Device values for Teleport Away wand + get_level_use_stick = 3; + get_level_max_stick = 20; + // Setup: Max and min + s32b max = 50; + s32b min = 1; + // Exercise + s32b actualLevel = get_level_device(teleport_away_spell, max, min, device_skill); + // Verify: Check expected levels. + AssertThat(actualLevel, Equals(teleport_away_expected_levels.at(device_skill))); + }); + } + + }); + +}); diff --git a/tests/harness.cc b/tests/harness.cc new file mode 100644 index 00000000..3a66ad55 --- /dev/null +++ b/tests/harness.cc @@ -0,0 +1,7 @@ +#include <bandit/bandit.h> +using namespace bandit; + +int main(int argc, char** argv) +{ + return bandit::run(argc, argv); +} diff --git a/tests/lua_get_level.cc b/tests/lua_get_level.cc new file mode 100644 index 00000000..a434903e --- /dev/null +++ b/tests/lua_get_level.cc @@ -0,0 +1,135 @@ +#include "lua_bind.hpp" +#include "spell_type.hpp" +#include <bandit/bandit.h> +using namespace bandit; + +go_bandit([]() { + describe("lua_get_level", []() { + + auto createEntsPotion = ([]() { + auto my_spell = spell_type_new("TEST: Ent's Potion"); + spell_type_set_difficulty(my_spell, 6, 35); // Copied from standard Ent's Potion spell. + return my_spell; + }); + + auto createSenseHidden = ([]() { + auto my_spell = spell_type_new("TEST: Sense Hidden"); + spell_type_set_difficulty(my_spell, 5, 25); // Copied from standard Sense Hidden spell. + return my_spell; + }); + + // + // Test cases derived from empirical testing of the code before refactoring. + // + + it("calculates \"Ent's Potion\" level appropriately for Sorcery@1.0", [&](){ + // Setup + auto my_spell = createEntsPotion(); + // Exercise + auto actualResult = lua_get_level(my_spell, 100, 50, -50, 0); + // Verify + AssertThat(actualResult, Equals(-4)); + }); + + it("calculates \"Ent's Potion\" cost appropriately for Sorcery@1.0", [&](){ + // Setup + auto my_spell = createEntsPotion(); + // Exercise + auto actualResult = lua_get_level(my_spell, 100, 15, 7, 0); + // Verify + AssertThat(actualResult, Equals(7)); + }); + + it("calculates \"Ent's Potion\" level appropriately for Sorcery@25.0", [&](){ + // Setup + auto my_spell = createEntsPotion(); + // Exercise + auto actualResult = lua_get_level(my_spell, 2500, 50, 1, 0); + // Verify + AssertThat(actualResult, Equals(20)); + }); + + it("calculates \"Ent's Potion\" cost appropriately for Sorcery@25.0", [&](){ + // Setup + auto my_spell = createEntsPotion(); + // Exercise + auto actualResult = lua_get_level(my_spell, 2500, 15, 7, 0); + // Verify + AssertThat(actualResult, Equals(7)); + }); + + it("calculates \"Ent's Potion\" level appropriately for Sorcery@25.0 with +3 equipment SP bonus", [&](){ + // Setup + auto my_spell = createEntsPotion(); + // Exercise + auto actualResult = lua_get_level(my_spell, 2500, 50, 1, 300); + // Verify + AssertThat(actualResult, Equals(23)); + }); + + it("calculates \"Ent's Potion\" cost appropriately for Sorcery@25.0 with +3 equipment SP bonus", [&](){ + // Setup + auto my_spell = createEntsPotion(); + // Exercise + auto actualResult = lua_get_level(my_spell, 2500, 15, 7, 300); + // Verify + AssertThat(actualResult, Equals(7)); + + }); + + it("calculates \"Sense Hidden\" level appropriately for Sorcery@1.0", [&](){ + // Setup + auto my_spell = createSenseHidden(); + // Exercise + auto actualResult = lua_get_level(my_spell, 100, 50, -50, 0); + // Verify + AssertThat(actualResult, Equals(-3)); + }); + + it("calculates \"Sense Hidden\" cost appropriately for Sorcery@1.0", [&](){ + // Setup + auto my_spell = createSenseHidden(); + // Exercise + auto actualResult = lua_get_level(my_spell, 100, 10, 2, 0); + // Verify + AssertThat(actualResult, Equals(2)); + }); + + it("calculates \"Sense Hidden\" level appropriately for Sorcery@25.0", [&](){ + // Setup + auto my_spell = createSenseHidden(); + // Exercise + auto actualResult = lua_get_level(my_spell, 2500, 50, 1, 0); + // Verify + AssertThat(actualResult, Equals(21)); + }); + + it("calculates \"Sense Hidden\" cost appropriately for Sorcery@25.0", [&](){ + // Setup + auto my_spell = createSenseHidden(); + // Exercise + auto actualResult = lua_get_level(my_spell, 2500, 10, 2, 0); + // Verify + AssertThat(actualResult, Equals(4)); + }); + + it("calculates \"Sense Hidden\" level appropriately for Sorcery@25.0 with +3 equipment SP bonus", [&](){ + // Setup + auto my_spell = createSenseHidden(); + // Exercise + auto actualResult = lua_get_level(my_spell, 2500, 50, 1, 300); + // Verify + AssertThat(actualResult, Equals(24)); + }); + + it("calculates \"Sense Hidden\" cost appropriately for Sorcery@25.0 with +3 equipment SP bonus", [&](){ + // Setup + auto my_spell = createSenseHidden(); + // Exercise + auto actualResult = lua_get_level(my_spell, 2500, 10, 2, 300); + // Verify + AssertThat(actualResult, Equals(4)); + }); + + }); +}); |