Files
cards/src/game/rule/operation/hand_test.go
Bel LaPointe 117250f5c1 hands to int64
2021-03-17 13:36:58 -05:00

506 lines
9.5 KiB
Go

package operation
import (
"local/sandbox/cards/src/entity"
"testing"
)
type testHandCase struct {
public []entity.Card
private []entity.Card
want int
}
func TestStraight(t *testing.T) {
cases := map[string]testHandCase{
"no cards": {
want: 0,
},
"straight big 3": {
public: []entity.Card{
entity.Card{Value: 11},
entity.Card{Value: 12},
entity.Card{Value: 13},
},
want: 13,
},
"straight sm 3": {
public: []entity.Card{
entity.Card{Value: 1},
entity.Card{Value: 2},
entity.Card{Value: 3},
},
want: 3,
},
"straight sm 2": {
public: []entity.Card{
entity.Card{Value: 1},
entity.Card{Value: 2},
},
want: 2,
},
"not a straight": {
public: []entity.Card{
entity.Card{Value: 1},
entity.Card{Value: 3},
},
want: 0,
},
}
for name, d := range cases {
testHand(t, name, straight, d.public, d.private, d.want)
}
}
func TestFlush(t *testing.T) {
cases := map[string]testHandCase{
"no cards": {
want: 0,
},
"not a flush": {
public: []entity.Card{
entity.Card{Value: 1, Suit: 0},
entity.Card{Value: 2, Suit: 1},
},
want: 0,
},
"big, big, small, small flush": {
public: []entity.Card{
entity.Card{Value: 12},
entity.Card{Value: 10},
entity.Card{Value: 2},
entity.Card{Value: 1},
},
want: 12100201,
},
"small, small flush": {
public: []entity.Card{
entity.Card{Value: 2},
entity.Card{Value: 1},
},
want: 201,
},
"big, small flush": {
public: []entity.Card{
entity.Card{Value: 11},
entity.Card{Value: 1},
},
want: 1101,
},
"big, big flush": {
public: []entity.Card{
entity.Card{Value: 11},
entity.Card{Value: 12},
},
want: 1211,
},
}
for name, d := range cases {
testHand(t, name, flush, d.public, d.private, d.want)
}
}
func TestHighCard(t *testing.T) {
cases := map[string]testHandCase{
"no cards": {
want: 0,
},
"seven card": {
public: []entity.Card{
entity.Card{Value: 13},
entity.Card{Value: 11},
entity.Card{Value: 11},
entity.Card{Value: 11},
entity.Card{Value: 11},
entity.Card{Value: 3},
entity.Card{Value: 4},
},
want: 13111111110403,
},
"four card": {
public: []entity.Card{
entity.Card{Value: 13},
entity.Card{Value: 11},
entity.Card{Value: 3},
entity.Card{Value: 4},
},
want: 13110403,
},
"three card": {
public: []entity.Card{
entity.Card{Value: 13},
entity.Card{Value: 3},
entity.Card{Value: 4},
},
want: 130403,
},
"two card": {
public: []entity.Card{
entity.Card{Value: 3},
entity.Card{Value: 4},
},
want: 403,
},
"one card": {
public: []entity.Card{
entity.Card{Value: 3},
},
want: 3,
},
}
for name, d := range cases {
testHand(t, name, highCard, d.public, d.private, d.want)
}
}
func TestTwoPair(t *testing.T) {
cases := map[string]testHandCase{
"no cards": {
want: 0,
},
"no pair": {
public: []entity.Card{
entity.Card{Value: 11},
entity.Card{Value: 10},
},
want: 0,
},
"one pair": {
public: []entity.Card{
entity.Card{Value: 11},
entity.Card{Value: 11},
},
want: 0,
},
"no second pair": {
public: []entity.Card{
entity.Card{Value: 12},
entity.Card{Value: 12},
entity.Card{Value: 11},
entity.Card{Value: 10},
},
want: 0,
},
"two pair diff": {
public: []entity.Card{
entity.Card{Value: 11},
entity.Card{Value: 11},
entity.Card{Value: 10},
entity.Card{Value: 10},
},
want: 111000,
},
"two pair same": {
public: []entity.Card{
entity.Card{Value: 11},
entity.Card{Value: 11},
entity.Card{Value: 11},
entity.Card{Value: 11},
},
want: 111100,
},
"two pair high": {
public: []entity.Card{
entity.Card{Value: 11},
entity.Card{Value: 11},
entity.Card{Value: 10},
entity.Card{Value: 10},
entity.Card{Value: 13},
},
want: 111013,
},
}
for name, d := range cases {
testHand(t, name, twoPair, d.public, d.private, d.want)
}
}
func TestFullHouse(t *testing.T) {
cases := map[string]testHandCase{
"no cards": {
want: 0,
},
"10 > 11": {
public: []entity.Card{
entity.Card{Value: 11},
entity.Card{Value: 11},
entity.Card{Value: 10},
entity.Card{Value: 10},
entity.Card{Value: 10},
},
want: 1011,
},
"five of a kind": {
public: []entity.Card{
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
},
want: 0,
},
"two pair": {
public: []entity.Card{
entity.Card{Value: 4},
entity.Card{Value: 4},
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 6},
},
want: 0,
},
}
for name, d := range cases {
testHand(t, name, fullHouse, d.public, d.private, d.want)
}
}
func TestNOfAKind(t *testing.T) {
cases4 := map[string]testHandCase{
"no cards": {
want: 0,
},
"one card": {
public: []entity.Card{
entity.Card{Value: 5},
},
want: 0,
},
"two cards": {
public: []entity.Card{
entity.Card{Value: 5},
entity.Card{Value: 5},
},
want: 0,
},
"three cards": {
public: []entity.Card{
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
},
want: 0,
},
"four cards": {
public: []entity.Card{
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
},
want: 500,
},
"five cards": {
public: []entity.Card{
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
},
want: 505,
},
}
for name, d := range cases4 {
testHand(t, name, fourOfAKind, d.public, d.private, d.want)
}
cases2 := map[string]testHandCase{
"no cards": {
want: 0,
},
"one card": {
public: []entity.Card{
entity.Card{Value: 5},
},
want: 0,
},
"two cards": {
public: []entity.Card{
entity.Card{Value: 5},
entity.Card{Value: 5},
},
want: 500,
},
"three cards": {
public: []entity.Card{
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
},
want: 505,
},
"four cards, two diff pairs": {
public: []entity.Card{
entity.Card{Value: 4},
entity.Card{Value: 4},
entity.Card{Value: 5},
entity.Card{Value: 5},
},
want: 504,
},
"four cards": {
public: []entity.Card{
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
},
want: 505,
},
"five cards": {
public: []entity.Card{
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
},
want: 505,
},
}
for name, d := range cases2 {
testHand(t, name, pair, d.public, d.private, d.want)
}
cases3 := map[string]testHandCase{
"no cards": {
want: 0,
},
"one card": {
public: []entity.Card{
entity.Card{Value: 5},
},
want: 0,
},
"two cards": {
public: []entity.Card{
entity.Card{Value: 5},
entity.Card{Value: 5},
},
want: 0,
},
"three cards": {
public: []entity.Card{
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
},
want: 500,
},
"four cards": {
public: []entity.Card{
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
},
want: 505,
},
"five cards": {
public: []entity.Card{
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
entity.Card{Value: 5},
},
want: 505,
},
}
for name, d := range cases3 {
testHand(t, name, threeOfAKind, d.public, d.private, d.want)
}
}
func TestStraightFlush(t *testing.T) {
cases := map[string]testHandCase{
"no cards": {
want: 0,
},
"one nonroyal cards": {
public: []entity.Card{entity.Card{Value: 5}},
want: 0,
},
"one royal cards": {
public: []entity.Card{entity.Card{Value: 12}},
want: 0,
},
"two nonroyal cards": {
public: []entity.Card{entity.Card{Value: 5}, entity.Card{Value: 6}},
want: 6,
},
"twe royal cards": {
public: []entity.Card{entity.Card{Value: 12}, entity.Card{Value: 13}},
want: 13,
},
"twe mix cards": {
public: []entity.Card{entity.Card{Value: 11}, entity.Card{Value: 10}},
want: 11,
},
}
for name, d := range cases {
testHand(t, name, straightFlush, d.public, d.private, d.want)
}
}
func TestRoyalFlush(t *testing.T) {
cases := map[string]testHandCase{
"no cards": {
want: 0,
},
"one nonroyal cards": {
public: []entity.Card{entity.Card{Value: 5}},
want: 0,
},
"one royal cards": {
public: []entity.Card{entity.Card{Value: 12}},
want: 0,
},
"two nonroyal cards": {
public: []entity.Card{entity.Card{Value: 5}, entity.Card{Value: 6}},
want: 0,
},
"twe royal cards": {
public: []entity.Card{entity.Card{Value: 12}, entity.Card{Value: 13}},
want: 1,
},
"twe mix cards": {
public: []entity.Card{entity.Card{Value: 11}, entity.Card{Value: 10}},
want: 0,
},
}
for name, d := range cases {
testHand(t, name, royalFlush, d.public, d.private, d.want)
}
}
func testHand(t *testing.T, name string, foo Int, public []entity.Card, private []entity.Card, want int) {
t.Run(name, func(t *testing.T) {
hand := entity.Hand{
Public: public,
Private: private,
}
game := &entity.Game{
Players: []entity.Player{
entity.Player{Hand: hand},
},
}
got := foo(game, nil)
if got != int64(want) {
t.Fatal(want, got)
}
})
}