-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_game_rules.c
245 lines (211 loc) · 5.96 KB
/
test_game_rules.c
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
#include <stdio.h>
#include <string.h>
#include <head-unit.h>
#include "game.h"
#include "player.h"
#include "game_rules.h"
static char names[MAX_NUM_PLAYERS][MAX_NAME_LEN];
static char types[MAX_NUM_PLAYERS];
static struct game_t game;
static struct player_t *player;
static int choices[2];
static int num_choices;
static struct card_t threeS;
static struct card_t threeH;
static struct card_t nineH;
static struct card_t fourS;
static struct card_t sevenD;
static struct card_t fourH;
static struct card_t twoH;
static struct card_t twoD;
static struct card_t nineS;
static struct card_t sevenC;
static struct card_t queenS;
static struct card_t sevenH;
static struct card_t twoS;
static struct card_t tenC;
static struct card_t jackD;
static struct card_t sixH;
static struct card_t threeD;
static struct card_t fiveH;
static void setup(void)
{
threeS = make_card(THREE, SPADES);
threeH = make_card(THREE, HEARTS);
nineH = make_card(NINE, HEARTS);
fourS = make_card(FOUR, SPADES);
sevenD = make_card(SEVEN, DIAMONDS);
fourH = make_card(FOUR, HEARTS);
twoH = make_card(TWO, HEARTS);
twoD = make_card(TWO, DIAMONDS);
nineS = make_card(NINE, SPADES);
sevenC = make_card(SEVEN, CLUBS);
queenS = make_card(QUEEN, SPADES);
sevenH = make_card(SEVEN, HEARTS);
twoS = make_card(TWO, SPADES);
tenC = make_card(TEN, CLUBS);
jackD = make_card(JACK, DIAMONDS);
sixH = make_card(SIX, HEARTS);
threeD = make_card(THREE, DIAMONDS);
fiveH = make_card(FIVE, HEARTS);
}
static void setup_game(void)
{
strcpy(names[0],"James");
types[0] = 'h';
game = make_game(1, names, types, 3);
game.current_player = 0;
player = &game.players[game.current_player];
}
// helper tests
static void test_valid_card_on_card(struct card_t test_card, struct card_t pile_card)
{
setup_game();
num_choices = 1;
game.pile[game.pile_size++] = pile_card;
player->hand[player->hand_size++] = test_card;
choices[0] = 0;
assert_true(valid_move(&game, choices, num_choices));
}
static void test_not_valid_card_on_card(struct card_t test_card, struct card_t pile_card)
{
setup_game();
num_choices = 1;
game.pile[game.pile_size++] = pile_card;
player->hand[player->hand_size++] = test_card;
choices[0] = 0;
assert_false(valid_move(&game, choices, num_choices));
}
// basic tests
static void test_valid_on_no_cards(void)
{
setup_game();
num_choices = 1;
player->hand[player->hand_size++] = threeS;
assert_true(valid_move(&game, choices, num_choices));
}
static void test_not_valid_move_no_cards(void)
{
setup_game();
num_choices = 1;
game.pile[game.pile_size++] = nineH;
player->hand[player->hand_size++] = fourS;
assert_false(valid_move(&game, choices, num_choices));
}
static void test_not_valid_more_cards_than_hand(void)
{
setup_game();
num_choices = 2;
game.pile[game.pile_size++] = threeH;
player->hand[player->hand_size++] = fourS;
choices[0] = 0;
choices[1] = 1;
assert_false(valid_move(&game, choices, num_choices));
}
static void test_not_valid_when_rank_not_equal(void)
{
setup_game();
num_choices = 2;
player->hand[player->hand_size++] = fourS;
player->hand[player->hand_size++] = sevenD;
choices[0] = 0;
choices[1] = 1;
assert_false(valid_move(&game, choices, num_choices));
}
static void test_not_valid_when_not_in_hand(void)
{
setup_game();
num_choices = 2;
game.pile[game.pile_size++] = threeH;
player->hand[player->hand_size++] = fourS;
player->hand[player->hand_size++] = sevenD;
choices[0] = 0;
choices[1] = 4;
assert_false(valid_move(&game, choices, num_choices));
}
// normal order tests
static void test_valid_four_on_three(void)
{
test_valid_card_on_card(fourS, threeH);
}
static void test_valid_three_on_three(void)
{
test_valid_card_on_card(threeS, threeH);
}
static void test_not_valid_three_on_four(void)
{
test_not_valid_card_on_card(threeS, fourH);
}
static void test_valid_three_on_two(void)
{
test_valid_card_on_card(threeS, twoH);
}
// test two on anything
static void test_valid_two_on_nine(void)
{
test_valid_card_on_card(twoD, nineS);
}
static void test_valid_two_on_seven(void)
{
test_valid_card_on_card(twoH, sevenC);
}
// test seven on anything
static void test_valid_seven_on_queen(void)
{
test_valid_card_on_card(sevenD, queenS);
}
static void test_valid_seven_on_two(void)
{
test_valid_card_on_card(sevenH, twoS);
}
//test ten on anything
static void test_valid_ten_on_jack(void)
{
test_valid_card_on_card(tenC, jackD);
}
static void test_valid_ten_on_two(void)
{
test_valid_card_on_card(tenC, twoD);
}
// test laying on invisible
static void test_valid_six_on_seven_on_nothing(void)
{
setup_game();
num_choices = 1;
game.pile[game.pile_size++] = sevenH;
player->hand[player->hand_size++] = sixH;
choices[0] = 0;
assert_true(valid_move(&game, choices, num_choices));
}
static void test_valid_five_on_seven_on_three(void)
{
setup_game();
num_choices = 1;
game.pile[game.pile_size++] = threeD;
game.pile[game.pile_size++] = sevenH;
player->hand[player->hand_size++] = fiveH;
choices[0] = 0;
assert_true(valid_move(&game, choices, num_choices));
}
void register_game_rules_tests(void)
{
TEST_MODULE("test_game_rules");
SETUP(setup);
TEST(test_valid_on_no_cards);
TEST(test_not_valid_move_no_cards);
TEST(test_not_valid_more_cards_than_hand);
TEST(test_not_valid_when_rank_not_equal);
TEST(test_not_valid_when_not_in_hand);
TEST(test_valid_four_on_three);
TEST(test_valid_three_on_three);
TEST(test_valid_three_on_two);
TEST(test_not_valid_three_on_four);
TEST(test_valid_two_on_nine);
TEST(test_valid_seven_on_queen);
TEST(test_valid_seven_on_two);
TEST(test_valid_ten_on_jack);
TEST(test_valid_ten_on_two);
TEST(test_valid_two_on_seven);
TEST(test_valid_six_on_seven_on_nothing);
TEST(test_valid_five_on_seven_on_three);
}