-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdeck-tests.py
432 lines (381 loc) · 14.6 KB
/
deck-tests.py
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import unittest
import deck
from deck import Deck, Card, Hand
class DeckTests(unittest.TestCase):
def test_patch(self):
import collections
assert collections.deck is Deck
def test_count(self):
d = Deck()
assert len(d) == 54
def test_count_no_jokers(self):
d = Deck(include_jokers=False)
assert len(d) == 52
def test_count_multiple(self):
with self.assertRaises(ValueError):
Deck(decks=-1)
for i in range(100):
d = Deck(decks=i)
assert len(d) == 54 * i
def test_count_no_jokers_multiple(self):
for i in range(100):
d = Deck(include_jokers=False, decks=i)
assert len(d) == 52 * i
class CardTests(unittest.TestCase):
def test_init(self):
c = Card("♥", 9)
assert c.suit == deck.Suit.Hearts
assert c.value == 9
c = Card("spades", "ace")
assert c.suit == deck.Suit.Spades
assert c.value == 1
def test_eq(self):
c1 = Card("Diamonds", "King")
c2 = Card("♦", 13)
assert c1 == c2
c1 = Card(joker=True)
c2.joker = True
assert c1 == c2
assert c1.value == deck.Value.Joker
def test_format(self):
c1 = Card("♥", 9)
c2 = Card("Diamonds", "King")
c3 = Card(joker=True)
assert format(c1) == "9♥"
assert format(c2) == "K♦"
assert format(c3) == "Jok"
assert format(c1, "<4") == "9♥ "
assert format(c2, "<4") == "King♦"
assert format(c3, "<4") == "Joker"
assert format(c1, ">7") == " 9♥"
assert format(c2, ">7") == " King♦"
assert format(c3, ">7") == " Joker"
assert format(c1, "<5.5") == "9♥ "
assert format(c2, "<5.5") == "King♦"
assert format(c3, "<5.5") == "Joker"
class PokerHandTests(unittest.TestCase):
def test_high_card(self):
cards = [
Card("Spades", 2),
Card("Clubs", 4),
Card("Hearts", 5),
Card("Diamonds", 7),
Card("Spades", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.HighCard, 9, 7, 5, 4, 2)
def test_high_card_ace(self):
cards = [
Card("Spades", 2),
Card("Clubs", 4),
Card("Hearts", 5),
Card("Diamonds", 7),
Card("Spades", "Ace"),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.HighCard, 14, 7, 5, 4, 2)
def test_pair(self):
cards = [
Card("Spades", 2),
Card("Clubs", 4),
Card("Hearts", 5),
Card("Diamonds", 9),
Card("Spades", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.Pair, 9, 5)
def test_two_pair(self):
cards = [
Card("Spades", 2),
Card("Clubs", 5),
Card("Hearts", 5),
Card("Diamonds", 9),
Card("Spades", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.TwoPair, 9, 5)
def test_triples(self):
cards = [
Card("Spades", 2),
Card("Clubs", 4),
Card("Hearts", 9),
Card("Diamonds", 9),
Card("Spades", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.ThreeOfAKind, 9, 4)
def test_straight(self):
cards = [
Card("Spades", 2),
Card("Clubs", 3),
Card("Hearts", 4),
Card("Diamonds", 5),
Card("Spades", 6),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.Straight, 6)
def test_straight_with_low_ace(self):
cards = [
Card("Spades", 1),
Card("Spades", 2),
Card("Clubs", 3),
Card("Hearts", 4),
Card("Diamonds", 5),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.Straight, 5)
def test_flush(self):
cards = [
Card("Clubs", 2),
Card("Clubs", 4),
Card("Clubs", 5),
Card("Clubs", 7),
Card("Clubs", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.Flush, 9)
def test_full_house(self):
cards = [
Card("Spades", 4),
Card("Clubs", 4),
Card("Hearts", 9),
Card("Diamonds", 9),
Card("Spades", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.FullHouse, 9)
def test_fours(self):
cards = [
Card("Spades", 2),
Card("Clubs", 9),
Card("Hearts", 9),
Card("Diamonds", 9),
Card("Spades", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.FourOfAKind, 9)
def test_straight_flush(self):
cards = [
Card("Clubs", 5),
Card("Clubs", 6),
Card("Clubs", 7),
Card("Clubs", 8),
Card("Clubs", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.StraightFlush, 9)
def test_straight_flush_with_low_ace(self):
cards = [
Card("Clubs", 1),
Card("Clubs", 2),
Card("Clubs", 3),
Card("Clubs", 4),
Card("Clubs", 5),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.StraightFlush, 5)
def test_compare_1(self):
cards = [
Card("Spades", 2),
Card("Clubs", 4),
Card("Hearts", 5),
Card("Diamonds", 7),
]
h1 = deck.get_poker_hand(cards + [Card("Spades", "Ace")])
h2 = deck.get_poker_hand(cards + [Card("Spades", "King")])
assert h1 > h2
def test_compare_2(self):
cards = [
Card("Spades", 6),
Card("Hearts", 2),
Card("Clubs", "King"),
]
h1 = deck.get_poker_hand(cards + [Card("Diamonds", 8), Card("Diamonds", "Ace")])
h2 = deck.get_poker_hand(cards + [Card("Spades", 9), Card("Diamonds", 5)])
assert h1 > h2
def test_compare_3(self):
cards = [
Card("Diamonds", 6),
Card("Spades", 9),
Card("Clubs", "King"),
]
h1 = deck.get_poker_hand(
cards + [Card("Diamonds", 7), Card("Diamonds", "Queen")]
)
h2 = deck.get_poker_hand(cards + [Card("Hearts", 2), Card("Diamonds", "Jack")])
assert h1 > h2
class HandTests(unittest.TestCase):
HAND1 = Hand(
[Card("♥", 2), Card("♦", 11), Card("♥", 3), Card(joker=True), Card("♠", 11)]
)
HAND2 = Hand(
[Card("♦", 2), Card("♥", 11), Card("♠", 3), Card(joker=True), Card("♠", 11)]
)
def test_type_checks(self):
try:
h = Hand(["!not a card!"])
except Exception as ex:
assert False, "expected no error"
try:
h.check_contents()
except ValueError as ex:
assert "!not a card!" in str(ex)
else:
assert False, "expected runtime error"
def test_deal(self):
deck = Deck()
hands = deck.deal_hands()
assert len(hands) == 2
assert len(hands[0]) == 5
assert len(hands[1]) == 5
def test_default_sort(self):
hand = Hand(self.HAND1)
hand.sort(deck.HandSort.Default)
assert [c.value.value for c in hand] == [2, 3, 11, 11, 15]
hand.sort(deck.HandSort.Default, reverse=True)
assert [c.value.value for c in hand] == [15, 11, 11, 3, 2]
def test_sort_suit(self):
hand = Hand([Card("♦", 2), Card("♠", 2), Card("♣", 2), Card("♥", 2)])
hand.sort(deck.HandSort.Default)
assert "".join(c.suit.value for c in hand) == "♣♦♥♠"
hand.sort(deck.HandSort.Default, reverse=True)
assert "".join(c.suit.value for c in hand) == "♠♥♦♣"
def test_poker_sort(self):
hand = Hand(self.HAND1)
hand.sort(deck.HandSort.Poker)
assert [c.value.value for c in hand] == [2, 3, 15, 11, 11]
hand.sort(deck.HandSort.Poker, reverse=True)
assert [c.value.value for c in hand] == [11, 11, 15, 3, 2]
def test_contains(self):
hand1 = self.HAND1
assert hand1.index(hand1[1]) == 1
assert hand1.index(hand1[1].suit) == 1
assert hand1.index(hand1[1].value) == 1
assert hand1.index(hand1[2]) == 2
assert hand1.index(hand1[2].suit) == 0
assert hand1.index(hand1[2].suit, start=1) == 2
assert hand1.index(hand1[2].value) == 2
with Hand.default_comparison(deck.HandComparison.Suits):
assert hand1.index(hand1[2]) == 0
with Hand.default_comparison(deck.HandComparison.Values):
assert hand1.index(hand1[2]) == 2
def test_count(self):
hand1 = self.HAND1
assert hand1.count(hand1[1]) == 1
assert hand1.count(hand1[1].suit) == 1
assert hand1.count(hand1[1].value) == 2
assert hand1.count(hand1[2]) == 1
assert hand1.count(hand1[2].suit) == 2
assert hand1.count(hand1[2].value) == 1
with Hand.default_comparison(deck.HandComparison.Suits):
assert hand1.count(hand1[2]) == 2
with Hand.default_comparison(deck.HandComparison.Values):
assert hand1.count(hand1[2]) == 1
def test_intersect_exact(self):
hand1, hand2 = self.HAND1, self.HAND2
# Self-intersect should be the complete set
assert set(hand1.intersect(hand1, deck.HandComparison.Exact)) == set(hand1)
assert set(hand2.intersect(hand2, deck.HandComparison.Exact)) == set(hand2)
# Only exact matches should remain after an intersect
assert set(hand1.intersect(hand2, deck.HandComparison.Exact)) == {
Card(joker=True),
Card("♠", 11),
}
def test_intersect_value(self):
hand1, hand2 = self.HAND1, self.HAND2
hand3 = Hand([Card("♥", 11)])
expect_hand3 = {Card("♦", 11), Card("♠", 11)}
hand4 = Hand([Card(joker=True)])
with Hand.default_comparison(deck.HandComparison.Values):
# Self-intersect should be the complete set
assert set(hand1.intersect(hand1)) == set(hand1)
assert set(hand2.intersect(hand2)) == set(hand2)
# All values exist between hand1 and hand2 and so we get all of them
assert set(hand1.intersect(hand2)) == set(hand1)
# Only Jacks are retained
assert set(hand1.intersect(hand3)) == expect_hand3
# Only Jokers are retained
assert set(hand1.intersect(hand4)) == set(hand4)
# Check bitwise operator
assert set(hand1 & hand3) == expect_hand3
# Check in-place operator
h1 = Hand(hand1)
h1 &= hand3
assert set(h1) == expect_hand3
assert len(hand1) != len(h1)
def test_intersect_suit(self):
hand1, hand2 = self.HAND1, self.HAND2
hand3 = Hand([Card("♥", 11)])
expect_hand3 = {Card("♥", 2), Card("♥", 3)}
hand4 = Hand([Card(joker=True)])
with Hand.default_comparison(deck.HandComparison.Suits):
# Self-intersect should be the complete set
assert set(hand1.intersect(hand1)) == set(hand1)
assert set(hand2.intersect(hand2)) == set(hand2)
# All suits exist between hand1 and hand2 and so we get all of them
assert set(hand1.intersect(hand2)) == set(hand1)
# Only Hearts are retained
assert set(hand1.intersect(hand3)) == expect_hand3
# Only Jokers are retained
assert set(hand1.intersect(hand4)) == set(hand4)
# Check bitwise operator
assert set(hand1 & hand3) == expect_hand3
# Check in-place operator
h1 = Hand(hand1)
h1 &= hand3
assert set(h1) == expect_hand3
assert len(hand1) != len(h1)
def test_union_exact(self):
hand1, hand2 = self.HAND1, self.HAND2
# Self-union should be the original hand
assert list(hand1.union(hand1, deck.HandComparison.Exact)) == list(hand1)
assert list(hand2.union(hand2, deck.HandComparison.Exact)) == list(hand2)
# Exact matches should not duplicate after a union
assert list(hand1.union(hand2, deck.HandComparison.Exact)) == [
*hand1,
*hand2[:3],
]
def test_union_value(self):
hand1, hand2 = self.HAND1, self.HAND2
hand3 = Hand([Card("♥", 10), Card("♥", 11)])
expect_hand3 = [*hand1, hand3[0]]
hand4 = Hand([Card(joker=True)])
with Hand.default_comparison(deck.HandComparison.Values):
# Self-union should be the complete list
assert list(hand1.union(hand1)) == list(hand1)
assert list(hand2.union(hand2)) == list(hand2)
# All values exist between hand1 and hand2 and so we get all of them
assert list(hand1.union(hand2)) == list(hand1)
# Only Tens are added
assert list(hand1.union(hand3)) == expect_hand3
# Nothing is added
assert list(hand1.union(hand4)) == list(hand1)
# Check bitwise operator
assert list(hand1 | hand3) == expect_hand3
# Check in-place operator
h1 = Hand(c for c in hand1 if c.value == 3)
h1 |= hand1
assert set(h1) == set(hand1)
def test_union_suit(self):
hand1, hand2 = self.HAND1, self.HAND2
hand3 = Hand([Card("♥", 10), Card("♥", 11)])
expect_hand3 = [*hand1, *hand3]
hand4 = Hand([Card(joker=True)])
with Hand.default_comparison(deck.HandComparison.Suits):
# Self-union should be the complete hand
assert list(hand1.union(hand1)) == list(hand1)
assert list(hand2.union(hand2)) == list(hand2)
# All suits exist between hand1 and hand2 and so we get all of them
assert list(hand1.union(hand2)) == list(hand1)
# Nothing is added
assert list(hand1.union(hand3)) == list(hand1)
# Nothing is added
assert list(hand1.union(hand4)) == list(hand1)
# Check bitwise operator
assert list(hand1 | hand3) == list(hand1)
# Check in-place operator
h1 = Hand(c for c in hand1 if c.suit != deck.Suit.Hearts)
h1 |= hand1
assert set(h1) == set(hand1)
if __name__ == "__main__":
unittest.main()