Newer
Older
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//
// Created by Manuel Nowack on 11.04.21.
//
#include "gtest/gtest.h"
#include "../src/common/game_state/player/hand.h"
/* A test fixture allows to reuse the same configuration of objects for all
* tests in a test suite. The name of the fixture must match the test suite.
*
* For each test defined with TEST_F(), googletest will create a fresh test
* fixture at runtime, immediately initialize it via SetUp(), run the test,
* clean up by calling TearDown(), and then delete the test fixture.
* Note that different tests in the same test suite have different test fixture
* objects, and googletest always deletes a test fixture before it creates the
* next one. googletest does not reuse the same test fixture for multiple
* tests. Any changes one test makes to the fixture do not affect other tests.
*/
class HandTest : public ::testing::Test {
protected:
virtual void SetUp() {
cards.resize(8);
for (int i = 1; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
cards[i].push_back(new card(i));
}
}
}
/* Any object and subroutine declared here can be accessed in the tests */
// cards[i][j] holds a pointer to the j-th copy of a card of value i
std::vector<std::vector<card*>> cards;
hand player_hand;
std::string err;
};
// Adding one card to an empty hand must succeed
TEST_F(HandTest, AddOneCard) {
EXPECT_TRUE(player_hand.add_card(cards[1][0], err));
std::vector<card*> expected_hand = {cards[1][0]};
EXPECT_EQ(expected_hand, player_hand.get_cards());
}
// The initial state must be an empty hand
TEST_F(HandTest, AddNoCards) {
std::vector<card*> expected_hand = {};
EXPECT_EQ(expected_hand, player_hand.get_cards());
}
// Adding several cards with different values to an empty hand must succeed
TEST_F(HandTest, AddManyCards) {
EXPECT_TRUE(player_hand.add_card(cards[1][0], err));
EXPECT_TRUE(player_hand.add_card(cards[3][0], err));
EXPECT_TRUE(player_hand.add_card(cards[7][0], err));
std::vector<card*> expected_hand = {cards[1][0], cards[3][0], cards[7][0]};
EXPECT_EQ(expected_hand, player_hand.get_cards());
}
// Adding several cards with duplicate values to an empty hand must succeed
TEST_F(HandTest, AddManyCardsWithDuplicates) {
EXPECT_TRUE(player_hand.add_card(cards[1][0], err));
EXPECT_TRUE(player_hand.add_card(cards[1][1], err));
EXPECT_TRUE(player_hand.add_card(cards[1][2], err));
EXPECT_TRUE(player_hand.add_card(cards[3][0], err));
EXPECT_TRUE(player_hand.add_card(cards[3][1], err));
EXPECT_TRUE(player_hand.add_card(cards[7][0], err));
std::vector<card*> expected_hand = {cards[1][0], cards[1][1], cards[1][2],
cards[3][0], cards[3][1], cards[7][0]};
EXPECT_EQ(expected_hand, player_hand.get_cards());
}
// Removing one card from a hand of three cards must succeed
TEST_F(HandTest, RemoveOneCard) {
player_hand.add_card(cards[1][0], err);
player_hand.add_card(cards[3][0], err);
player_hand.add_card(cards[7][0], err);
card* to_remove = cards[1][0];
card* removed_card = nullptr;
EXPECT_TRUE(player_hand.remove_card(to_remove->get_id(), removed_card, err));
std::vector<card*> expected_hand = {cards[3][0], cards[7][0]};
EXPECT_EQ(to_remove, removed_card);
EXPECT_EQ(expected_hand, player_hand.get_cards());
}
// Removing a card from an empty hand must fail
// Removing a nonexistent card from a hand of three cards must fail
TEST_F(HandTest, RemoveNonexistentCards) {
card* to_remove = cards[1][0];
card* removed_card = cards[1][0];
EXPECT_FALSE(player_hand.remove_card(to_remove->get_id(), removed_card, err));
std::vector<card*> expected_hand = {};
EXPECT_EQ(nullptr, removed_card);
// Use ASSERT instead of EXPECT to abort the current function (test)
// because adding and removing additional cards in an already incorrect
// hand makes no sense.
ASSERT_EQ(expected_hand, player_hand.get_cards());
player_hand.add_card(cards[1][0], err);
player_hand.add_card(cards[3][0], err);
player_hand.add_card(cards[7][0], err);
to_remove = cards[1][1];
removed_card = cards[1][1];
EXPECT_FALSE(player_hand.remove_card(to_remove->get_id(), removed_card, err));
expected_hand = {cards[1][0], cards[3][0], cards[7][0]};
EXPECT_EQ(nullptr, removed_card);
EXPECT_EQ(expected_hand, player_hand.get_cards());
}
// Removing the final card from a hand must succeed
TEST_F(HandTest, RemoveAllCards) {
player_hand.add_card(cards[1][0], err);
card* to_remove = cards[1][0];
card* removed_card = nullptr;
EXPECT_TRUE(player_hand.remove_card(to_remove->get_id(), removed_card, err));
std::vector<card*> expected_hand = {};
EXPECT_EQ(to_remove, removed_card);
EXPECT_EQ(expected_hand, player_hand.get_cards());
}
// Removing several cards of different values from a hand of seven cards must succeed
TEST_F(HandTest, RemoveManyCards) {
player_hand.add_card(cards[1][0], err);
player_hand.add_card(cards[1][1], err);
player_hand.add_card(cards[1][2], err);
player_hand.add_card(cards[3][0], err);
player_hand.add_card(cards[3][1], err);
player_hand.add_card(cards[7][0], err);
card* to_remove = cards[1][0];
card* removed_card = nullptr;
EXPECT_TRUE(player_hand.remove_card(to_remove->get_id(), removed_card, err));
std::vector<card*> expected_hand = {cards[1][1], cards[1][2], cards[3][0],
cards[3][1], cards[7][0]};
EXPECT_EQ(to_remove, removed_card);
ASSERT_EQ(expected_hand, player_hand.get_cards());
to_remove = cards[3][0];
removed_card = nullptr;
EXPECT_TRUE(player_hand.remove_card(to_remove->get_id(), removed_card, err));
expected_hand = {cards[1][1], cards[1][2], cards[3][1], cards[7][0]};
EXPECT_EQ(to_remove, removed_card);
ASSERT_EQ(expected_hand, player_hand.get_cards());
to_remove = cards[7][0];
removed_card = nullptr;
EXPECT_TRUE(player_hand.remove_card(to_remove->get_id(), removed_card, err));
expected_hand = {cards[1][1], cards[1][2], cards[3][1]};
EXPECT_EQ(to_remove, removed_card);
EXPECT_EQ(expected_hand, player_hand.get_cards());
}
// Removing several cards of the same value from a hand of seven cards must succeed
TEST_F(HandTest, RemoveManyDuplicateCards) {
player_hand.add_card(cards[1][0], err);
player_hand.add_card(cards[1][1], err);
player_hand.add_card(cards[1][2], err);
player_hand.add_card(cards[3][0], err);
player_hand.add_card(cards[3][1], err);
player_hand.add_card(cards[7][0], err);
card* to_remove = cards[3][0];
card* removed_card = nullptr;
EXPECT_TRUE(player_hand.remove_card(to_remove->get_id(), removed_card, err));
std::vector<card*> expected_hand = {cards[1][0], cards[1][1], cards[1][2],
cards[3][1], cards[7][0]};
EXPECT_EQ(to_remove, removed_card);
ASSERT_EQ(expected_hand, player_hand.get_cards());
to_remove = cards[3][1];
removed_card = nullptr;
EXPECT_TRUE(player_hand.remove_card(to_remove->get_id(), removed_card, err));
expected_hand = {cards[1][0], cards[1][1], cards[1][2], cards[7][0]};
EXPECT_EQ(to_remove, removed_card);
EXPECT_EQ(expected_hand, player_hand.get_cards());
}
// The score of a hand with a single card of value one must be equal to 1
TEST_F(HandTest, ScoreOneCard) {
player_hand.add_card(cards[1][0], err);
EXPECT_EQ(1, player_hand.get_score());
}
// The score of a hand with a single Lama card must be equal to 10
TEST_F(HandTest, ScoreLama) {
player_hand.add_card(cards[7][0], err);
EXPECT_EQ(10, player_hand.get_score());
}
// The score of an empty hand must be zero
TEST_F(HandTest, ScoreNoCards) {
EXPECT_EQ(0, player_hand.get_score());
}
// Each addition of a card must increase the score by that card's value
TEST_F(HandTest, ScoreManyCards) {
player_hand.add_card(cards[1][0], err);
ASSERT_EQ(1, player_hand.get_score());
player_hand.add_card(cards[3][0], err);
ASSERT_EQ(4, player_hand.get_score());
player_hand.add_card(cards[7][0], err);
EXPECT_EQ(14, player_hand.get_score());
}
// Each addition of a card must increase the score by that card's value even
// if several cards have the same value
TEST_F(HandTest, ScoreManyCardsWithDuplicates) {
player_hand.add_card(cards[1][0], err);
ASSERT_EQ(1, player_hand.get_score());
player_hand.add_card(cards[1][1], err);
ASSERT_EQ(1, player_hand.get_score());
player_hand.add_card(cards[1][2], err);
ASSERT_EQ(1, player_hand.get_score());
player_hand.add_card(cards[3][0], err);
ASSERT_EQ(4, player_hand.get_score());
player_hand.add_card(cards[3][1], err);
ASSERT_EQ(4, player_hand.get_score());
player_hand.add_card(cards[7][0], err);
EXPECT_EQ(14, player_hand.get_score());
}
// A hand of one card must have count 1
TEST_F(HandTest, CountOneCard) {
player_hand.add_card(cards[1][0], err);
EXPECT_EQ(1, player_hand.get_nof_cards());
}
// An empty hand must have count 0
TEST_F(HandTest, CountNoCards) {
EXPECT_EQ(0, player_hand.get_nof_cards());
}
// Each addition of a card must increase the count by 1
TEST_F(HandTest, CountManyCards) {
player_hand.add_card(cards[1][0], err);
ASSERT_EQ(1, player_hand.get_nof_cards());
player_hand.add_card(cards[3][0], err);
ASSERT_EQ(2, player_hand.get_nof_cards());
player_hand.add_card(cards[7][0], err);
EXPECT_EQ(3, player_hand.get_nof_cards());
}
// Each addition of a card must increase the count by 1 even if several cards
// have the same value
TEST_F(HandTest, CountManyCardsWithDuplicates) {
player_hand.add_card(cards[1][0], err);
ASSERT_EQ(1, player_hand.get_nof_cards());
player_hand.add_card(cards[1][1], err);
ASSERT_EQ(2, player_hand.get_nof_cards());
player_hand.add_card(cards[1][2], err);
ASSERT_EQ(3, player_hand.get_nof_cards());
player_hand.add_card(cards[3][0], err);
ASSERT_EQ(4, player_hand.get_nof_cards());
player_hand.add_card(cards[3][1], err);
ASSERT_EQ(5, player_hand.get_nof_cards());
player_hand.add_card(cards[7][0], err);
EXPECT_EQ(6, player_hand.get_nof_cards());
}