diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/flag_set.cc | 110 | ||||
-rw-r--r-- | tests/get_level_device.cc | 184 | ||||
-rw-r--r-- | tests/grid.cc | 52 | ||||
-rw-r--r-- | tests/harness.cc | 7 | ||||
-rw-r--r-- | tests/lua_get_level.cc | 135 |
5 files changed, 488 insertions, 0 deletions
diff --git a/tests/flag_set.cc b/tests/flag_set.cc new file mode 100644 index 00000000..fe1ad4a4 --- /dev/null +++ b/tests/flag_set.cc @@ -0,0 +1,110 @@ +#include "flag_set.hpp" +#include <bandit/bandit.h> +using namespace bandit; + +// +// Tests +// + +go_bandit([]() { + + describe("flag_set", []() { + + // Convenience typedef + typedef flag_set<2> fs_t; + + it("make function should handle tier=0, index=31 properly", [&] { + // Setup + fs_t fs = fs_t::make(0, 31); + // Exercise + auto result = fs; + // Verify + AssertThat(result[0], Equals(2147483648UL)); + AssertThat(result[1], Equals(0UL)); + }); + + it("make function should handle tier=1, index=31 properly", [&] { + // Setup + fs_t fs = fs_t::make(1, 31); + // Exercise + auto result = fs; + // Verify + AssertThat(result[0], Equals(0UL)); + AssertThat(result[1], Equals(2147483648UL)); + }); + + it("make function should respect the tier and index", [&] { + // Exercise + fs_t fs = fs_t::make(1, 7); + // Verify + AssertThat(fs[0], Equals(0UL)); + AssertThat(fs[1], Equals(128UL)); + }); + + it("bool conversion should return false for zero flags", [&] { + // Setup + fs_t fs = fs_t(); + // Exercise + auto result = bool(fs); + // Verify + AssertThat(result, Equals(false)); + }); + + it("bool conversion should return true for non-zero flags", [&] { + // Setup + fs_t fs = fs_t::make(1, 3); + // Exercise + auto result = bool(fs); + // Verify + AssertThat(result, Equals(true)); + }); + + it("== operator should compare equals as equals", [&] { + // Setup + fs_t fs1 = fs_t::make(1, 3); + fs_t fs2 = fs_t::make(1, 3); + // Exercise + auto result = (fs1 == fs2); + // Verify + AssertThat(result, Equals(true)); + }); + + it("| operator should respect the tier and index", [&] { + // Setup + fs_t fs1 = fs_t::make(0, 31); + fs_t fs2 = fs_t::make(1, 3); + // Exercise + fs_t fs = fs1 | fs2; + // Verify + AssertThat(fs[0], Equals(2147483648UL)); + AssertThat(fs[1], Equals(8UL)); + }); + + it("& operator should respect the tier and index", [&] { + // Setup + fs_t fs = fs_t::make(0, 31) | fs_t::make(1, 3); + // Exercise + fs_t result = fs & fs; + // Verify + AssertThat(result[0], Equals(2147483648UL)); + AssertThat(result[1], Equals(8UL)); + }); + + it("make_bit(5) should set the 5th bit in the 1st tier", [&] { + // Exercise + fs_t result = fs_t::make_bit(5); + // Verify + AssertThat(result[0], Equals(32L)); + AssertThat(result[1], Equals(0L)); + }); + + it("make_bit(37) should set the 5th bit in the 2nd tier", [&] { + // Exercise + fs_t result = fs_t::make_bit(37); + // Verify + AssertThat(result[0], Equals(0L)); + AssertThat(result[1], Equals(32L)); + }); + }); + +}); 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/grid.cc b/tests/grid.cc new file mode 100644 index 00000000..ff618088 --- /dev/null +++ b/tests/grid.cc @@ -0,0 +1,52 @@ +#include "grid.hpp" +#include <bandit/bandit.h> +using namespace bandit; + +go_bandit([]() { + describe("grid<>", []() { + + auto w0 = size_t { 123 }; + auto h0 = size_t { 42 }; + + it("width(...) properly sets returned width", [&](){ + // Setup + grid<int> g; + // Exercise + g.width(w0); + // Verify + AssertThat(g.width(), Equals(w0)); + }); + + it("height(...) properly sets returned height", [&](){ + // Setup + grid<int> g; + // Exercise + g.height(h0); + // Verify + AssertThat(g.height(), Equals(h0)); + }); + + it("resize(w, h) properly sets returned width and height", [&](){ + // Setup + grid<int> g; + // Exercise + g.resize(w0, h0); + // Verify + AssertThat(g.width(), Equals(w0)); + AssertThat(g.height(), Equals(h0)); + }); + + it("operator () can access ((w-1), (h-1)) element", [&](){ + // Class with 'magic' default value + struct X { int magic = 1001; }; + // Setup + grid<X> g; + g.resize(w0 + 1, h0 + 1); + // Exercise + auto x = g(w0, h0); + // Verify + AssertThat(x.magic, Equals(1001)); + }); + + }); +}); 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)); + }); + + }); +}); |