-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdeck_test.go
161 lines (139 loc) · 4.91 KB
/
deck_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
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
package deck
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSmallDeckToString(t *testing.T) {
deck, _ := New(Empty)
deck.Cards = append(deck.Cards, NewCard(ACE, HEART), NewCard(KING, HEART))
result := fmt.Sprintf("%s", deck)
assert.Equal(t, "A♥\nK♥\n", result, "These should be equal")
}
func TestDeckToString(t *testing.T) {
deck, _ := New(Unshuffled)
result := fmt.Sprintf("%s", deck)
assert.Equal(t, "A♣\n2♣\n3♣\n4♣\n5♣\n6♣\n7♣\n8♣\n9♣\nT♣\nJ♣\nQ♣\nK♣\n"+
"A♦\n2♦\n3♦\n4♦\n5♦\n6♦\n7♦\n8♦\n9♦\nT♦\nJ♦\nQ♦\nK♦\n"+
"A♥\n2♥\n3♥\n4♥\n5♥\n6♥\n7♥\n8♥\n9♥\nT♥\nJ♥\nQ♥\nK♥\n"+
"A♠\n2♠\n3♠\n4♠\n5♠\n6♠\n7♠\n8♠\n9♠\nT♠\nJ♠\nQ♠\nK♠\n", result, "These should be equal")
}
func TestDeckFromSignature(t *testing.T) {
deck, _ := New(FromSignature("02c290"), Unshuffled)
result := fmt.Sprintf("%s", deck)
assert.Equal(t, "A♥\nK♥\nT♣\n", result, "These should be equal")
}
func TestDeckSignature(t *testing.T) {
deck, _ := New(Empty)
deck.Cards = append(deck.Cards, NewCard(ACE, HEART), NewCard(KING, HEART), NewCard(TEN, CLUB))
result := deck.GetSignature()
assert.Equal(t, "02c290", result, "These should be equal")
}
func TestCompleteDeckSignature(t *testing.T) {
deck, _ := New(Unshuffled)
result := deck.GetSignature()
assert.Equal(t, "00102030405060708090a0b0c001112131415161718191a1b1c102122232425262728292a2b2c203132333435363738393a3b3c3", result, "These should be equal")
}
func TestNewTinyDeck(t *testing.T) {
deck, _ := New(Unshuffled, Suits(SPADE))
assert.Equal(t, 13, deck.NumberOfCards())
}
func TestNewSmallDeck(t *testing.T) {
deck, _ := New(Unshuffled, Suits(SPADE, HEART))
assert.Equal(t, 26, deck.NumberOfCards())
}
func TestDeal(t *testing.T) {
deck, _ := New(Unshuffled, Suits(SPADE), Faces(FACES...))
assert.Equal(t, deck.NumberOfCards(), 13)
hand1, _ := New(Empty)
hand2, _ := New(Empty)
deck.Deal(5, hand1, hand2)
assert.Equal(t, deck.NumberOfCards(), 3)
assert.Equal(t, hand1.NumberOfCards(), 5)
assert.Equal(t, hand2.NumberOfCards(), 5)
assert.Equal(t, hand1.String(), "A♠\n3♠\n5♠\n7♠\n9♠\n")
assert.Equal(t, hand2.String(), "2♠\n4♠\n6♠\n8♠\nT♠\n")
}
func TestShuffle(t *testing.T) {
expected, _ := New(Unshuffled)
deck, _ := New(Unshuffled)
deck.Shuffle()
assert.NotEqual(t, expected.GetSignature(), deck.GetSignature())
}
func TestShufflePerm(t *testing.T) {
expected, _ := New(Unshuffled)
deck, _ := New(Unshuffled)
deck.ShufflePerm()
assert.NotEqual(t, expected.GetSignature(), deck.GetSignature())
}
func TestWithCards(t *testing.T) {
cards := []Card{
NewCard(ACE, HEART),
NewCard(ACE, SPADE),
NewCard(ACE, DIAMOND),
NewCard(ACE, CLUB),
}
deck, _ := New(WithCards(cards...), Unshuffled)
assert.Equal(t, deck.NumberOfCards(), 4)
assert.Equal(t, deck.Cards[0].String(), "A♥")
assert.Equal(t, deck.Cards[1].String(), "A♠")
assert.Equal(t, deck.Cards[2].String(), "A♦")
assert.Equal(t, deck.Cards[3].String(), "A♣")
}
func BenchmarkTinyDeckShuffle(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
deck, _ := New(Unshuffled, Suits(SPADE))
deck.Shuffle()
}
}
func BenchmarkSmallDeckShuffle(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
deck, _ := New(Unshuffled, Faces(FACES...), Suits(SPADE, HEART))
deck.Shuffle()
}
}
func BenchmarkMediumDeckShuffle(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
deck, _ := New(Unshuffled, Faces(FACES...), Suits(SPADE, HEART, DIAMOND))
deck.Shuffle()
}
}
func BenchmarkDeckShuffle(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
deck, _ := New(Unshuffled, Faces(FACES...), Suits(SUITS...))
deck.Shuffle()
}
}
func BenchmarkLargeDeckShuffle(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
deck, _ := New(Unshuffled, Decks(10), Faces(FACES...), Suits(SUITS...))
deck.Shuffle()
}
}
// func BenchmarkLargeDeckShuffleAlt(b *testing.B) {
// b.ReportAllocs()
// deck, _ := New(Unshuffled, Decks(10), Faces(FACES...), Suits(SUITS...))
// for n := 0; n < b.N; n++ {
// deck.ShufflePerm()
// }
// }
func TestShoeToString(t *testing.T) {
shoe, _ := New(Unshuffled, Decks(1))
result := fmt.Sprintf("%s", shoe)
assert.Equal(t, "A♣\n2♣\n3♣\n4♣\n5♣\n6♣\n7♣\n8♣\n9♣\nT♣\nJ♣\nQ♣\nK♣\nA♦\n2♦\n3♦\n4♦\n5♦\n6♦\n7♦\n8♦\n9♦\nT♦\nJ♦\nQ♦\nK♦\nA♥\n2♥\n3♥\n4♥\n5♥\n6♥\n7♥\n8♥\n9♥\nT♥\nJ♥\nQ♥\nK♥\nA♠\n2♠\n3♠\n4♠\n5♠\n6♠\n7♠\n8♠\n9♠\nT♠\nJ♠\nQ♠\nK♠\n", result, "These should be equal")
}
func TestNewSpecificShoe(t *testing.T) {
shoe, _ := New(Unshuffled, Decks(2), Faces(ACE), Suits(HEART))
result := fmt.Sprintf("%s", shoe)
assert.Equal(t, "A♥\nA♥\n", result, "These should be equal")
}
func TestEmptyShoe(t *testing.T) {
shoe, _ := New(Decks(0))
result := shoe.NumberOfDecks
assert.Equal(t, 0, result, "These should be equal")
}