forked from notnil/chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notation_test.go
145 lines (131 loc) · 3.55 KB
/
notation_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
package chess
import (
"encoding/json"
"os"
"strings"
"testing"
)
type validNotationTest struct {
Pos1 *Position
Pos2 *Position
AlgText string
LongAlgText string
UCIText string
Description string
}
func TestValidDecoding(t *testing.T) {
f, err := os.Open("fixtures/valid_notation_tests.json")
if err != nil {
t.Fatal(err)
return
}
validTests := []validNotationTest{}
if err := json.NewDecoder(f).Decode(&validTests); err != nil {
t.Fatal(err)
return
}
for _, test := range validTests {
for i, n := range []Notation{SANNotation, LongAlgebraicNotation, UCINotation} {
var moveText string
switch i {
case 0:
moveText = test.AlgText
case 1:
moveText = test.LongAlgText
case 2:
moveText = test.UCIText
}
m, err := test.Pos1.DecodeMove(moveText, n)
if err != nil {
movesStrList := []string{}
for _, m := range test.Pos1.ValidMoves() {
s := test.Pos1.EncodeMove(m, n)
movesStrList = append(movesStrList, s)
}
t.Fatalf("starting from board \n%s\n expected move to be valid error - %s %s\n", test.Pos1.board.Draw(), err, strings.Join(movesStrList, ","))
}
postPos := test.Pos1.Update(m)
if test.Pos2.String() != postPos.String() {
t.Fatalf("starting from board \n%s\n after move %s\n (%s, %d) expected board to be %s\n%s\n but was %s\n%s\n",
test.Pos1.board.Draw(), m.String(), moveText, i, test.Pos2.String(),
test.Pos2.board.Draw(), postPos.String(), postPos.board.Draw())
}
}
}
}
type notationDecodeTest struct {
N Notation
Pos *Position
Text string
PostPos *Position
MoveText string
}
var (
invalidDecodeTests = []notationDecodeTest{
{
// opening for white
N: SANNotation,
Pos: unsafeFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"),
Text: "e5",
},
{
// http://en.lichess.org/W91M4jms#14
N: SANNotation,
Pos: unsafeFEN("rn1qkb1r/pp3ppp/2p1pn2/3p4/2PP4/2NQPN2/PP3PPP/R1B1K2R b KQkq - 0 7"),
Text: "Nd7",
},
{
// http://en.lichess.org/W91M4jms#17
N: SANNotation,
Pos: unsafeFEN("r2qk2r/pp1n1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P4PPP/R1B1K2R w KQkq - 1 9"),
Text: "O-O-O-O",
PostPos: unsafeFEN("r2qk2r/pp1n1ppp/2pbpn2/3p4/2PP4/1PNQPN2/P4PPP/R1B2RK1 b kq - 0 9"),
},
{
// http://en.lichess.org/W91M4jms#23
N: SANNotation,
Pos: unsafeFEN("3r1rk1/pp1nqppp/2pbpn2/3p4/2PP4/1PNQPN2/PB3PPP/3RR1K1 b - - 5 12"),
Text: "dx4",
},
{
// should not assume pawn for unknown piece type "n"
N: SANNotation,
Pos: unsafeFEN("rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2"),
Text: "nf3",
},
{
// disambiguation should not allow for this since it is not a capture
N: SANNotation,
Pos: unsafeFEN("rnbqkbnr/ppp1pppp/8/3p4/3P4/8/PPP1PPPP/RNBQKBNR w KQkq - 0 2"),
Text: "bf4",
},
}
)
func TestInvalidDecoding(t *testing.T) {
for _, test := range invalidDecodeTests {
if _, err := test.Pos.DecodeMove(test.Text, test.N); err == nil {
t.Fatalf("starting from board\n%s\n expected move notation %s to be invalid", test.Pos.board.Draw(), test.Text)
}
}
}
func BenchmarkValidAlgebraicDecoding(b *testing.B) {
f, err := os.Open("fixtures/valid_notation_tests.json")
if err != nil {
b.Fatal(err)
return
}
validTests := []validNotationTest{}
if err := json.NewDecoder(f).Decode(&validTests); err != nil {
b.Fatal(err)
return
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
for _, test := range validTests {
_, err := test.Pos1.DecodeMove(test.AlgText, SANNotation)
if err != nil {
b.Fatal(err)
}
}
}
}