1 package joker
2
3 import (
4 "reflect"
5 "sort"
6 "testing"
7 )
8
9 type cardsTestCase struct {
10 Cards Cards
11 RemovedFirst Cards
12 RemovedLast Cards
13 Reversed Cards
14 Sorted Cards
15 Low Card
16 High Card
17 }
18
19 var cardsTestCases = []*cardsTestCase{
20 {
21 Cards: Cards{Card{FaceAce, SuitHearts}, Card{Face4, SuitDiamonds}, Card{Face2, SuitClubs}, Card{Face3, SuitSpades}},
22 RemovedFirst: Cards{Card{Face4, SuitDiamonds}, Card{Face2, SuitClubs}, Card{Face3, SuitSpades}},
23 RemovedLast: Cards{Card{FaceAce, SuitHearts}, Card{Face4, SuitDiamonds}, Card{Face2, SuitClubs}},
24 Reversed: Cards{Card{Face3, SuitSpades}, Card{Face2, SuitClubs}, Card{Face4, SuitDiamonds}, Card{FaceAce, SuitHearts}},
25 Sorted: Cards{Card{FaceAce, SuitHearts}, Card{Face2, SuitClubs}, Card{Face3, SuitSpades}, Card{Face4, SuitDiamonds}},
26 Low: Card{FaceAce, SuitHearts},
27 High: Card{Face4, SuitDiamonds},
28 },
29 {
30 Cards: Cards{Card{Face2, SuitClubs}, Card{Face4, SuitSpades}},
31 RemovedFirst: Cards{Card{Face4, SuitSpades}},
32 RemovedLast: Cards{Card{Face2, SuitClubs}},
33 Reversed: Cards{Card{Face4, SuitSpades}, Card{Face2, SuitClubs}},
34 Sorted: Cards{Card{Face2, SuitClubs}, Card{Face4, SuitSpades}},
35 Low: Card{Face2, SuitClubs},
36 High: Card{Face4, SuitSpades},
37 },
38 {
39 Cards: Cards{Card{FaceQueen, SuitHearts}},
40 RemovedFirst: Cards{},
41 RemovedLast: Cards{},
42 Reversed: Cards{Card{FaceQueen, SuitHearts}},
43 Sorted: Cards{Card{FaceQueen, SuitHearts}},
44 Low: Card{FaceQueen, SuitHearts},
45 High: Card{FaceQueen, SuitHearts},
46 },
47 }
48
49 func TestCards(t *testing.T) {
50 for _, c := range cardsTestCases {
51 copied := c.Cards.Copy()
52 if !reflect.DeepEqual(copied, c.Cards) {
53 t.Errorf("failed to copy cards: expected %s, got %s", c.Cards, copied)
54 }
55
56 removedFirst := c.Cards.RemoveIndex(0)
57 if !reflect.DeepEqual(removedFirst, c.RemovedFirst) {
58 t.Errorf("failed to remove first card: expected %s, got %s", c.RemovedFirst, removedFirst)
59 }
60
61 removedLast := c.Cards.Remove(copied[len(c.Cards)-1])
62 if !reflect.DeepEqual(removedLast, c.RemovedLast) {
63 t.Errorf("failed to remove first card: expected %s, got %s", c.RemovedLast, removedLast)
64 }
65
66 reversed := c.Cards.Reversed()
67 if !reflect.DeepEqual(reversed, c.Reversed) {
68 t.Errorf("failed to reverse cards: expected %s, got %s", c.Reversed, reversed)
69 }
70
71 sorted := c.Cards.Sorted()
72 if !reflect.DeepEqual(sorted, c.Sorted) {
73 t.Errorf("failed to sort cards: expected %s, got %s", c.Sorted, sorted)
74 }
75
76 permutations := c.Cards.Permutations()
77 if len(permutations) != factorial(len(c.Cards)) {
78 t.Errorf("failed to generate permutations: expected %d, got %d: %s", factorial(len(c.Cards)), len(permutations), permutations)
79 }
80
81 low := c.Cards.Low()
82 if !low.Equal(c.Low) {
83 t.Errorf("failed to get low card: expected %s, got %s", c.Low, low)
84 }
85
86 high := c.Cards.High()
87 if !high.Equal(c.High) {
88 t.Errorf("failed to get high card: expected %s, got %s", c.High, high)
89 }
90 }
91 }
92
93 func BenchmarkCardsSort(b *testing.B) {
94 for i := 0; i < b.N; i++ {
95 for _, c := range cardsTestCases {
96 sort.Sort(c.Cards)
97 }
98 }
99 }
100
101 func factorial(i int) int {
102 if i <= 1 {
103 return 1
104 }
105 return i * factorial(i-1)
106 }
107
View as plain text