-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_card.c
228 lines (199 loc) · 5.92 KB
/
test_card.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
#include <stdio.h>
#include <head-unit.h>
#include "card.h"
static struct card_t tenH = { TEN, HEARTS };
static struct card_t sixC = { SIX, CLUBS };
static struct card_t threeD = { THREE, DIAMONDS };
static struct card_t threeH = { THREE, HEARTS };
static struct card_t threeS = { THREE, SPADES };
static struct card_t threeC = { THREE, CLUBS };
static struct card_t twoH = { TWO, HEARTS };
static struct card_t fiveD = { FIVE, DIAMONDS };
static struct card_t jackD = { JACK, DIAMONDS };
static struct card_t sevenH = { SEVEN, HEARTS };
static struct card_t twoD = { TWO, DIAMONDS };
static struct card_t sevenD = { SEVEN, DIAMONDS };
static struct card_t tenD = { TEN, DIAMONDS };
static struct card_t aceH = { ACE, HEARTS };
static struct card_t fourC = { FOUR, CLUBS };
static struct card_t nineH = { NINE, HEARTS };
static struct card_t sevenS = { SEVEN, SPADES };
static struct card_t jackC = { JACK, CLUBS };
static struct card_t aceD = { ACE, DIAMONDS };
static struct card_t twoS = { TWO, SPADES };
static struct card_t kingC = { KING, CLUBS };
static struct card_t eightS = { EIGHT, SPADES };
static void test_three_lowest_when_last(void)
{
struct card_t cards[] = { tenH, sixC, threeD };
struct card_t lowest = find_lowest_card(cards, 3);
assert_int_equals(THREE, lowest.rank);
}
static void test_three_lowest_when_middle(void)
{
struct card_t cards[] = { tenH, threeD, sixC };
struct card_t lowest = find_lowest_card(cards, 3);
assert_int_equals(THREE, lowest.rank);
}
static void test_three_lowest_when_first(void)
{
struct card_t cards[] = { threeD, tenH, sixC };
struct card_t lowest = find_lowest_card(cards, 3);
assert_int_equals(THREE, lowest.rank);
}
static void test_lowest_one_card(void)
{
struct card_t cards[] = { tenH };
struct card_t lowest = find_lowest_card(cards, 1);
assert_int_equals(TEN, lowest.rank);
}
static void test_three_lower_than_two(void)
{
struct card_t cards[] = { threeD, twoH };
struct card_t lowest = find_lowest_card(cards, 2);
assert_int_equals(THREE, lowest.rank);
}
static void test_five_lower_than_two(void)
{
struct card_t cards[] = { fiveD, twoH };
struct card_t lowest = find_lowest_card(cards, 2);
assert_int_equals(FIVE, lowest.rank);
}
static void test_jack_lower_than_seven(void)
{
struct card_t cards[] = { jackD, sevenH };
struct card_t lowest = find_lowest_card(cards, 2);
assert_int_equals(JACK, lowest.rank);
}
static void test_two_special(void)
{
assert_true(special_card(&twoH));
}
static void test_seven_special(void)
{
assert_true(special_card(&sevenD));
}
static void test_ten_special(void)
{
assert_true(special_card(&tenD));
}
static void test_ranks_equal(void)
{
assert_true(ranks_equal(&tenD, &tenH));
}
static void test_ranks_not_equal(void)
{
assert_false(ranks_equal(&tenD, &aceH));
}
static void test_all_ranks_equal(void)
{
struct card_t cards[4] = { threeD, threeS, threeH, threeC };
assert_true(all_ranks_equal(cards, 4));
}
static void test_all_ranks_not_equal(void)
{
struct card_t cards[4] = { threeD, threeS, threeH, fourC };
assert_false(all_ranks_equal(cards, 4));
}
static void test_remove_card_from_cards_decrements_size(void)
{
int num_cards = 5;
struct card_t cards[num_cards];
cards[0] = threeD;
cards[1] = twoD;
cards[2] = nineH;
cards[3] = sevenS;
cards[4] = jackC;
remove_card_from_cards(cards, &num_cards, &nineH);
assert_int_equals(4, num_cards);
}
static void test_remove_card_from_cards_removes_card(void)
{
int num_cards = 5;
struct card_t cards[num_cards];
cards[0] = threeD;
cards[1] = twoD;
cards[2] = nineH;
cards[3] = sevenS;
cards[4] = jackC;
remove_card_from_cards(cards, &num_cards, &nineH);
int i;
int found = 0;
for (i = 0; i < num_cards; i++) {
if (cards_equal(&cards[i], &nineH)) {
found = 1;
break;
}
}
assert_false(found);
}
static void test_add_card_to_cards_increments_size(void)
{
int num_cards = 5;
struct card_t cards[num_cards];
cards[0] = threeD;
cards[1] = twoD;
cards[2] = nineH;
cards[3] = sevenS;
cards[4] = jackC;
add_card_to_cards(cards, &num_cards, aceH);
assert_int_equals(6, num_cards);
}
static void test_add_card_to_cards_adds_card(void)
{
int num_cards = 5;
struct card_t cards[num_cards];
cards[0] = threeD;
cards[1] = twoD;
cards[2] = nineH;
cards[3] = sevenS;
cards[4] = jackC;
add_card_to_cards(cards, &num_cards, twoS);
int i;
int found = 0;
for (i = 0 ; i < num_cards ; i++) {
if (cards_equal(&cards[i], &twoS)) {
found = 1;
break;
}
}
assert_true(found);
}
static void test_sort_cards(void) {
int num_cards = 5;
struct card_t cards[num_cards];
cards[0] = aceD;
cards[1] = threeH;
cards[2] = twoS;
cards[3] = kingC;
cards[4] = eightS;
sort_cards(cards, num_cards);
assert_true(cards_equal(&cards[0], &threeH) &&
cards_equal(&cards[1], &eightS) &&
cards_equal(&cards[2], &kingC) &&
cards_equal(&cards[3], &aceD) &&
cards_equal(&cards[4], &twoS));
}
void register_card_tests()
{
TEST_MODULE("test_card");
TEST(test_three_lowest_when_last);
TEST(test_three_lowest_when_middle);
TEST(test_three_lowest_when_first);
TEST(test_lowest_one_card);
TEST(test_three_lower_than_two);
TEST(test_five_lower_than_two) ;
TEST(test_jack_lower_than_seven) ;
TEST(test_two_special) ;
TEST(test_seven_special) ;
TEST(test_ten_special);
TEST(test_ranks_equal);
TEST(test_ranks_not_equal);
TEST(test_all_ranks_equal);
TEST(test_all_ranks_not_equal);
TEST(test_remove_card_from_cards_decrements_size);
TEST(test_remove_card_from_cards_removes_card);
TEST(test_add_card_to_cards_increments_size);
TEST(test_add_card_to_cards_adds_card);
TEST(test_sort_cards);
}