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
|
// Copyright (c) 2016 Florian Fischer. All rights reserved.
// Use of this source code is governed by a MIT license found in the LICENSE file.
package logic
import "testing"
func TestScore(t *testing.T) {
s := NewScore()
s = []int{1, 2, 3, 4, 5, 6, 23, 27, 25, 30, 40, -1, 26}
if s.Score() != 192 {
t.Errorf("Score of \n%v should be 192", s)
}
s = []int{3, 6, 9, 12, 15, 18, 23, 27, 25, 30, 40, -1, 26}
if s.Score() != 269 {
t.Errorf("Score of \n%v should be 269", s)
}
s = []int{1, 8, 6, 8, 15, 12, 23, 15, -1, 30, 40, -1, 26}
if s.Score() != 184 {
t.Errorf("Score of \n%v should be 184", s)
}
}
func TestInsert(t *testing.T) {
s := NewScore()
d := Dice{1, 2, 3, 4, 5}
p, err := s.Insert(d, 0)
if p != 1 || err != nil {
t.Errorf("Inserting %v as aces should return 1", d)
}
d = Dice{1, 2, 2, 3, 4}
p, err = s.Insert(d, 1)
if p != 4 || err != nil {
t.Errorf("Inserting %v as twos should return 4", d)
}
p, err = s.Insert(d, 1)
if p != 0 || err == nil {
t.Errorf("Inserting %v as twos again should fail", d)
}
d = Dice{1, 2, 2, 4, 4}
p, err = s.Insert(d, 2)
if p != 0 || err != nil {
t.Errorf("Inserting %v as threes should return 0", d)
}
d = Dice{1, 2, 2, 6, 6}
p, err = s.Insert(d, 5)
if p != 12 || err != nil {
t.Errorf("Inserting %v as sixes should return 12", d)
}
d = Dice{1, 2, 6, 6, 6}
p, err = s.Insert(d, 6)
if p != 21 || err != nil {
t.Errorf("Inserting %v as three of a kind should return 21", d)
}
d = Dice{1, 1, 2, 2, 2}
p, err = s.Insert(d, 7)
if p != 0 || err == nil {
t.Errorf("Inserting %v as four of a kind should fail", d)
}
d = Dice{1, 2, 2, 2, 2}
p, err = s.Insert(d, 7)
if p != 9 || err != nil {
t.Errorf("Inserting %v as four of a kind should return 9", d)
}
d = Dice{1, 1, 2, 2, 2}
p, err = s.Insert(d, 8)
if p != 25 || err != nil {
t.Errorf("Inserting %v as full house should return 25", d)
}
d = Dice{1, 2, 3, 4, 5}
p, err = s.Insert(d, 9)
if p != 30 || err != nil {
t.Errorf("Inserting %v as small straight should return 30", d)
}
d = Dice{1, 2, 3, 4, 5}
p, err = s.Insert(d, 10)
if p != 40 || err != nil {
t.Errorf("Inserting %v as large straight should return 40", d)
}
d = Dice{2, 2, 3, 4, 5}
p, err = s.Insert(d, 10)
if p != 0 || err == nil {
t.Errorf("Inserting %v as large straight should fail", d)
}
d = Dice{2, 2, 2, 2, 2}
p, err = s.Insert(d, 11)
if p != 50 || err != nil {
t.Errorf("Inserting %v as yahtzee should return 50", d)
}
d = Dice{1, 3, 3, 5, 6}
p, err = s.Insert(d, 12)
if p != 18 || err != nil {
t.Errorf("Inserting %v as chance should return 18", d)
}
p, err = s.Insert(d, 200)
if p != 0 || err == nil {
t.Errorf("Inserting %v at index 200 should fail", d)
}
}
func TestCancel(t *testing.T) {
s := NewScore()
err := s.Cancel(1)
if err != nil {
t.Errorf("Cancelling Aces should not fail: %v", err)
}
err = s.Cancel(1)
if err == nil {
t.Errorf("Cancelling Aces twice should fail")
}
err = s.Cancel(12)
if err != nil {
t.Errorf("Cancelling Yahtzee should not fail: %v", err)
}
err = s.Cancel(22)
if err == nil {
t.Errorf("Cancelling 22 should fail")
}
}
|