summaryrefslogtreecommitdiff
path: root/tests/get_level_device.cc
blob: 95e51ad100b08f455ee91821ea4115c4685d8da1 (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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)));
			});
		}

	});

});