-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine_test.go
105 lines (91 loc) · 2.55 KB
/
engine_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
package chess
import (
"testing"
)
// Common test positions
var (
// Starting position
startingPos = mustPosition("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
// Middle game position with lots of possible moves
middlePos = mustPosition("r1bqk2r/pppp1ppp/2n2n2/2b1p3/2B1P3/2N2N2/PPPP1PPP/R1BQK2R w KQkq - 0 1")
// Endgame position with few pieces
endPos = mustPosition("4k3/8/8/8/8/8/4P3/4K3 w - - 0 1")
// Position with multiple possible pawn promotions
promoPos = mustPosition("4k3/PPPP4/8/8/8/8/4pppp/4K3 w - - 0 1")
)
func BenchmarkStandardMoves(b *testing.B) {
benchmarks := []struct {
name string
pos *Position
wantFirst bool
}{
{"StartingPos_AllMoves", startingPos, false},
{"StartingPos_FirstMove", startingPos, true},
{"MiddleGame_AllMoves", middlePos, false},
{"MiddleGame_FirstMove", middlePos, true},
{"Endgame_AllMoves", endPos, false},
{"Endgame_FirstMove", endPos, true},
{"Promotions_AllMoves", promoPos, false},
{"Promotions_FirstMove", promoPos, true},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
// Reset timer to exclude setup
b.ResetTimer()
// Enable allocation tracking
b.ReportAllocs()
for i := 0; i < b.N; i++ {
moves := standardMoves(bm.pos, bm.wantFirst)
// Prevent compiler optimization
if len(moves) == 0 {
b.Fatal("unexpected zero moves")
}
}
})
}
}
// Benchmark specific scenarios
func BenchmarkStandardMoves_PawnPromotions(b *testing.B) {
pos := promoPos
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
moves := standardMoves(pos, false)
if len(moves) == 0 {
b.Fatal("unexpected zero moves")
}
}
}
// Benchmark with different board sizes to understand allocation scaling
func BenchmarkStandardMoves_BoardDensity(b *testing.B) {
positions := []struct {
name string
fen string
}{
{"Empty", "4k3/8/8/8/8/8/8/4K3 w - - 0 1"},
{"QuarterFull", "rnbqk3/pppp4/8/8/8/8/8/4K3 w - - 0 1"},
{"HalfFull", "rnbqkbnr/pppp4/8/8/8/8/8/4K3 w - - 0 1"},
{"Full", "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"},
}
for _, p := range positions {
pos := mustPosition(p.fen)
b.Run(p.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
moves := standardMoves(pos, false)
if len(moves) == 0 && p.name != "Empty" {
b.Fatal("unexpected zero moves")
}
}
})
}
}
// Helper function to convert FEN to Position
func mustPosition(fen string) *Position {
fenObject, err := FEN(fen)
pos := NewGame(fenObject).Position()
if err != nil {
panic(err)
}
return pos
}