/
hand_test.go
87 lines (74 loc) · 3.11 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
/*
* Copyright 2011 Colin Patrick McCabe
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"testing"
)
func expectHand(t *testing.T, c CardSlice, eTy int, eVal [2]int, eFlushSuit int) {
aHand := MakeHand(c)
eHand := &Hand { eTy, eVal, eFlushSuit, c }
if (!aHand.Identical(eHand)) {
t.Errorf("expected MakeHand to create: %s.\n" +
"Instead, it created: %s", eHand, aHand)
}
}
func cvalToString(cval int) string {
switch (cval) {
case -1:
return "less than"
case 0:
return "equal to"
case 1:
return "greater than"
}
panic(fmt.Sprintf("unknown cval %d", cval))
}
func compareHands(t *testing.T, eRes int, a *Hand, b *Hand) {
aRes := a.Compare(b)
if (aRes != eRes) {
t.Errorf("expected hand:\n%s\nto be %s hand:\n%s, but it was " +
"marked as %s", a, cvalToString(eRes), b, cvalToString(aRes))
}
}
func TestHand1(t *testing.T) {
c1 := CardSlice { &Card{2, DIAMONDS}, &Card{2, CLUBS}, &Card{2, HEARTS},
&Card{QUEEN_VAL, SPADES}, &Card{KING_VAL, SPADES} }
expectHand(t, c1, THREE_OF_A_KIND, [2]int{2, -1}, -1)
c2 := CardSlice { &Card{8, DIAMONDS}, &Card{9, DIAMONDS}, &Card{10, DIAMONDS},
&Card{QUEEN_VAL, DIAMONDS}, &Card{JACK_VAL, DIAMONDS} }
expectHand(t, c2, STRAIGHT_FLUSH, [2]int{QUEEN_VAL, -1}, DIAMONDS)
c3 := CardSlice { &Card{8, DIAMONDS}, &Card{KING_VAL, DIAMONDS}, &Card{10, DIAMONDS},
&Card{QUEEN_VAL, DIAMONDS}, &Card{JACK_VAL, DIAMONDS} }
expectHand(t, c3, FLUSH, [2]int{-1, -1}, DIAMONDS)
c4 := CardSlice { &Card{8, CLUBS}, &Card{8, DIAMONDS}, &Card{10, HEARTS},
&Card{4, DIAMONDS}, &Card{10, DIAMONDS} }
expectHand(t, c4, TWO_PAIR, [2]int{8, 10}, -1)
c5 := CardSlice { &Card{8, CLUBS}, &Card{9, DIAMONDS}, &Card{2, HEARTS},
&Card{KING_VAL, DIAMONDS}, &Card{ACE_VAL, CLUBS} }
expectHand(t, c5, HIGH_CARD, [2]int{-1, -1}, -1)
c6 := CardSlice { &Card{2, DIAMONDS}, &Card{2, CLUBS}, &Card{2, HEARTS},
&Card{3, DIAMONDS}, &Card{ACE_VAL, DIAMONDS} }
expectHand(t, c6, THREE_OF_A_KIND, [2]int{2, -1}, -1)
compareHands(t, 1, MakeHand(CardSlice { &Card{8, DIAMONDS}, &Card{9, DIAMONDS},
&Card{10, DIAMONDS}, &Card{QUEEN_VAL, DIAMONDS}, &Card{JACK_VAL, DIAMONDS} }),
MakeHand(CardSlice { &Card{8, DIAMONDS}, &Card{7, DIAMONDS},
&Card{10, DIAMONDS}, &Card{QUEEN_VAL, DIAMONDS}, &Card{JACK_VAL, DIAMONDS} }))
compareHands(t, -1, MakeHand(CardSlice { &Card{10, DIAMONDS}, &Card{KING_VAL, DIAMONDS},
&Card{10, CLUBS}, &Card{QUEEN_VAL, DIAMONDS}, &Card{JACK_VAL, DIAMONDS} }),
MakeHand(CardSlice { &Card{8, DIAMONDS}, &Card{7, DIAMONDS},
&Card{10, DIAMONDS}, &Card{QUEEN_VAL, DIAMONDS}, &Card{JACK_VAL, DIAMONDS} }))
}