Playing card library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.2 KiB

package joker
import (
type cardsTestCase struct {
Cards Cards
RemovedFirst Cards
RemovedLast Cards
Reversed Cards
Sorted Cards
Low Card
High Card
var cardsTestCases = []*cardsTestCase{
Cards: Cards{Card{FaceAce, SuitHearts}, Card{Face4, SuitDiamonds}, Card{Face2, SuitClubs}, Card{Face3, SuitSpades}},
RemovedFirst: Cards{Card{Face4, SuitDiamonds}, Card{Face2, SuitClubs}, Card{Face3, SuitSpades}},
RemovedLast: Cards{Card{FaceAce, SuitHearts}, Card{Face4, SuitDiamonds}, Card{Face2, SuitClubs}},
Reversed: Cards{Card{Face3, SuitSpades}, Card{Face2, SuitClubs}, Card{Face4, SuitDiamonds}, Card{FaceAce, SuitHearts}},
Sorted: Cards{Card{FaceAce, SuitHearts}, Card{Face2, SuitClubs}, Card{Face3, SuitSpades}, Card{Face4, SuitDiamonds}},
Low: Card{FaceAce, SuitHearts},
High: Card{Face4, SuitDiamonds},
Cards: Cards{Card{Face2, SuitClubs}, Card{Face4, SuitSpades}},
RemovedFirst: Cards{Card{Face4, SuitSpades}},
RemovedLast: Cards{Card{Face2, SuitClubs}},
Reversed: Cards{Card{Face4, SuitSpades}, Card{Face2, SuitClubs}},
Sorted: Cards{Card{Face2, SuitClubs}, Card{Face4, SuitSpades}},
Low: Card{Face2, SuitClubs},
High: Card{Face4, SuitSpades},
Cards: Cards{Card{FaceQueen, SuitHearts}},
RemovedFirst: Cards{},
RemovedLast: Cards{},
Reversed: Cards{Card{FaceQueen, SuitHearts}},
Sorted: Cards{Card{FaceQueen, SuitHearts}},
Low: Card{FaceQueen, SuitHearts},
High: Card{FaceQueen, SuitHearts},
func TestCards(t *testing.T) {
for _, c := range cardsTestCases {
copied := c.Cards.Copy()
if !reflect.DeepEqual(copied, c.Cards) {
t.Errorf("failed to copy cards: expected %s, got %s", c.Cards, copied)
removedFirst := c.Cards.RemoveIndex(0)
if !reflect.DeepEqual(removedFirst, c.RemovedFirst) {
t.Errorf("failed to remove first card: expected %s, got %s", c.RemovedFirst, removedFirst)
removedLast := c.Cards.Remove(copied[len(c.Cards)-1])
if !reflect.DeepEqual(removedLast, c.RemovedLast) {
t.Errorf("failed to remove first card: expected %s, got %s", c.RemovedLast, removedLast)
reversed := c.Cards.Reversed()
if !reflect.DeepEqual(reversed, c.Reversed) {
t.Errorf("failed to reverse cards: expected %s, got %s", c.Reversed, reversed)
sorted := c.Cards.Sorted()
if !reflect.DeepEqual(sorted, c.Sorted) {
t.Errorf("failed to sort cards: expected %s, got %s", c.Sorted, sorted)
permutations := c.Cards.Permutations()
if len(permutations) != factorial(len(c.Cards)) {
t.Errorf("failed to generate permutations: expected %d, got %d: %s", factorial(len(c.Cards)), len(permutations), permutations)
low := c.Cards.Low()
if !low.Equal(c.Low) {
t.Errorf("failed to get low card: expected %s, got %s", c.Low, low)
high := c.Cards.High()
if !high.Equal(c.High) {
t.Errorf("failed to get high card: expected %s, got %s", c.High, high)
func BenchmarkCardsSort(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, c := range cardsTestCases {
func factorial(i int) int {
if i <= 1 {
return 1
return i * factorial(i-1)