Skip to content

Commit 6342754

Browse files
authored
fix: improve tests and example for dnd character exercise (#813)
[no important files changed]
1 parent 425a050 commit 6342754

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "dnd_character.h"
22

3+
#include <algorithm>
34
#include <cmath>
45
#include <cstdlib>
6+
#include <numeric>
57

68
namespace dnd_character {
79
int modifier(int score) {
@@ -10,8 +12,10 @@ int modifier(int score) {
1012

1113
int dice_roll() { return 1 + std::rand() / ((RAND_MAX + 1u) / 6); }
1214

13-
// Throwing three dice is not the same as selecting a random number between 3
14-
// and 18.
15-
int ability() { return dice_roll() + dice_roll() + dice_roll(); }
15+
int ability() {
16+
auto rolls = {dice_roll(), dice_roll(), dice_roll(), dice_roll()};
17+
auto discard = std::min(rolls);
1618

19+
return std::accumulate(rolls.begin(), rolls.end(), 0) - discard;
20+
}
1721
} // namespace dnd_character

exercises/practice/dnd-character/.meta/tests.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,5 @@ include = false
7070
[dca2b2ec-f729-4551-84b9-078876bb4808]
7171
description = "each ability is only calculated once"
7272
reimplements = "2ca77b9b-c099-46c3-a02c-0d0f68ffa0fe"
73+
include = false
74+
comment = "changes for member variable test not suitable for C++"

exercises/practice/dnd-character/dnd_character_test.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@
55
#include "test/catch.hpp"
66
#endif
77

8+
#include <sstream>
9+
10+
template <typename T>
11+
class IsBetweenMatcher : public Catch::MatcherBase<T> {
12+
T m_begin, m_end;
13+
public:
14+
IsBetweenMatcher(T begin, T end) :
15+
m_begin(begin), m_end(end) {
16+
}
17+
bool match(T const& in) const override {
18+
return in >= m_begin && in <= m_end;
19+
}
20+
std::string describe() const override {
21+
std::ostringstream ss;
22+
ss << "should be between " << m_begin << " and " << m_end;
23+
return ss.str();
24+
}
25+
};
26+
827
TEST_CASE("ability modifier for score 3 is -4", "[1e9ae1dc-35bd-43ba-aa08-e4b94c20fa37]") {
928
REQUIRE(-4 == dnd_character::modifier(3));
1029
}
@@ -73,29 +92,18 @@ TEST_CASE("ability modifier for score 18 is +4", "[bafd997a-e852-4e56-9f65-14b60
7392

7493
TEST_CASE("random ability is within range", "[4f28f19c-2e47-4453-a46a-c0d365259c14]") {
7594
int result{dnd_character::ability()};
76-
//REQUIRE("score >= 3 && score <= 18" == dnd_character::ability());
77-
REQUIRE((result >= 3 && result <= 18));
95+
CHECK_THAT(result, IsBetweenMatcher(3, 18));
7896
}
7997

8098
TEST_CASE("random character is valid", "[385d7e72-864f-4e88-8279-81a7d75b04ad]") {
8199
dnd_character::Character character;
82-
REQUIRE((character.strength >= 3 && character.strength <= 18));
83-
REQUIRE((character.dexterity >= 3 && character.dexterity <= 18));
84-
REQUIRE((character.constitution >= 3 && character.constitution <= 18));
85-
REQUIRE((character.intelligence >= 3 && character.intelligence <= 18));
86-
REQUIRE((character.wisdom >= 3 && character.wisdom <= 18));
87-
REQUIRE((character.charisma >= 3 && character.charisma <= 18));
100+
CHECK_THAT(character.strength, IsBetweenMatcher(3, 18));
101+
CHECK_THAT(character.dexterity, IsBetweenMatcher(3, 18));
102+
CHECK_THAT(character.constitution, IsBetweenMatcher(3, 18));
103+
CHECK_THAT(character.intelligence, IsBetweenMatcher(3, 18));
104+
CHECK_THAT(character.wisdom, IsBetweenMatcher(3, 18));
105+
CHECK_THAT(character.charisma, IsBetweenMatcher(3, 18));
88106
REQUIRE(character.hitpoints == 10 + dnd_character::modifier(character.constitution));
89107
}
90108

91-
TEST_CASE("each ability is only calculated once", "[dca2b2ec-f729-4551-84b9-078876bb4808]") {
92-
dnd_character::Character character;
93-
REQUIRE(character.strength == character.strength);
94-
REQUIRE(character.dexterity == character.dexterity);
95-
REQUIRE(character.constitution == character.constitution);
96-
REQUIRE(character.intelligence == character.intelligence);
97-
REQUIRE(character.wisdom == character.wisdom);
98-
REQUIRE(character.charisma == character.charisma);
99-
}
100-
101109
#endif

0 commit comments

Comments
 (0)