-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhand_test.go
87 lines (82 loc) · 2.09 KB
/
hand_test.go
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
package poker
import (
"testing"
)
type THand struct {
cards [5]string
highcard string
pair bool
twopair bool
threeofakind bool
straight bool
flush bool
fullhouse bool
fourofakind bool
straightflush bool
royalflush bool
}
var hands = [...]THand{
// pair
{cards: [...]string{"2C", "2D", "4C", "3C", "5C"},
highcard: "6C",
pair: true,
},
// two pair
{cards: [...]string{"6D", "TS", "7C", "7D", "TC"},
highcard: "6C",
twopair: true,
pair: true,
},
// three of a kind
{cards: [...]string{"8H", "2S", "8C", "8D", "TC"},
highcard: "6C",
threeofakind: true,
},
// four of a kind
{cards: [...]string{"5H", "5S", "5C", "5D", "KC"},
highcard: "6C",
fourofakind: true,
},
// straight flush
{cards: [...]string{"2C", "6C", "4C", "3C", "5C"},
highcard: "6C",
straight: true,
flush: true,
straightflush: true},
// flush
{cards: [...]string{"2D", "6D", "TD", "3D", "AD"},
highcard: "AD",
flush: true,
},
}
func testHand(t *testing.T, h Hand, expected, got bool, handType string) {
if got && !expected {
t.Errorf("%v should not be %s", h, handType)
}
if !got && expected {
t.Errorf("%v should be %s", h, handType)
}
}
func TestHands(t *testing.T) {
for _, h := range hands {
var cards []Card
for _, c := range h.cards {
card, _ := NewCard(c)
cards = append(cards, *card)
}
hand := Hand{cards}
if hand.Len() != 5 {
t.Errorf("Expected hand length 5, got %d\n",
hand.Len())
}
testHand(t, hand, h.pair, hand.IsPair(), "pair")
testHand(t, hand, h.twopair, hand.IsTwoPair(), "twopair")
testHand(t, hand, h.threeofakind, hand.IsThreeOfAKind(), "threeofakind")
testHand(t, hand, h.straight, hand.IsStraight(), "straight")
testHand(t, hand, h.flush, hand.IsFlush(), "flush")
testHand(t, hand, h.fullhouse, hand.IsFullHouse(), "fullhouse")
testHand(t, hand, h.fourofakind, hand.IsFourOfAKind(), "fourofakind")
testHand(t, hand, h.straightflush, hand.IsStraightFlush(), "straightflush")
testHand(t, hand, h.royalflush, hand.IsRoyalFlush(), "royalflush")
}
}