-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearch_test.go
More file actions
130 lines (106 loc) · 2.8 KB
/
Copy pathsearch_test.go
File metadata and controls
130 lines (106 loc) · 2.8 KB
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
package chester_test
import (
"testing"
"time"
"github.com/bluescreen10/chester"
)
func TestSearchBestMove_Tactics(t *testing.T) {
tests := []struct {
name string
fen string
depth int
wantMove string // algebraic notation
}{
{
name: "Mate in 1 - Back Rank",
fen: "6k1/5ppp/8/8/8/8/5PPP/3R2K1 w - - 0 1",
depth: 2,
wantMove: "d1d8",
},
{
name: "Mate in 1 - Scholar's Mate",
fen: "r1bqkb1r/pppp1ppp/2n2n2/4p2Q/2B1P3/8/PPPP1PPP/RNB1K1NR w KQkq - 0 1",
depth: 2,
wantMove: "h5f7",
},
{
name: "Take Hanging Queen",
fen: "rnb1kbnr/pppp1ppp/8/4p3/4P3/5q2/PPPP1PPP/RNBQKBNR w KQkq - 0 1",
depth: 2,
wantMove: "g1f3", // or d1f3, but f3 is the destination
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
p, _ := chester.ParseFEN(test.fen)
opts := &chester.SearchOptions{
MaxDepth: test.depth,
MaxTime: time.Second * 2,
}
ch, _ := chester.SearchBestMove(p, opts)
var lastEval chester.Evaluation
for e := range ch {
lastEval = e
}
if lastEval.Best.String() != test.wantMove {
t.Errorf("%s: got move %s, want %s", test.name, lastEval.Best, test.wantMove)
}
})
}
}
func TestSearchBestMove_Cancellation(t *testing.T) {
p, _ := chester.ParseFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
opts := &chester.SearchOptions{
MaxDepth: 20,
MaxTime: 20 * time.Millisecond,
}
start := time.Now()
ch, _ := chester.SearchBestMove(p, opts)
var finalDepth int
for e := range ch {
finalDepth = e.Depth
}
elapsed := time.Since(start)
if finalDepth >= 20 {
t.Errorf("Search did not cancel, reached depth %d", finalDepth)
}
if elapsed > 200*time.Millisecond {
t.Errorf("Search took too long to cancel: %v", elapsed)
}
}
func TestSearchBestMove_MateScore(t *testing.T) {
p, _ := chester.ParseFEN("6k1/5ppp/8/8/8/8/5PPP/3R2K1 w - - 0 1")
opts := &chester.SearchOptions{
MaxDepth: 2,
}
ch, _ := chester.SearchBestMove(p, opts)
var lastEval chester.Evaluation
for e := range ch {
lastEval = e
}
expectedMate := chester.MateScore - 1
if lastEval.Score != expectedMate {
t.Errorf("Expected mate score %d, got %d", expectedMate, lastEval.Score)
}
}
func TestSearchWithTT(t *testing.T) {
p, _ := chester.ParseFEN("6k1/5ppp/8/8/8/8/5PPP/3R2K1 w - - 0 1")
tt := chester.NewTranspositionTable(1024)
opts := &chester.SearchOptions{
MaxDepth: 4,
TranspositionTable: tt,
}
start := time.Now()
ch, _ := chester.SearchBestMove(p, opts)
for range ch {
}
elapsedFirst := time.Since(start)
start = time.Now()
ch, _ = chester.SearchBestMove(p, opts)
for range ch {
}
elapsedSecond := time.Since(start)
if elapsedSecond > elapsedFirst {
t.Errorf("Transposition table failed t1 (%s) < t2 (%s)", elapsedFirst, elapsedSecond)
}
}