-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathengine_test.go
More file actions
426 lines (384 loc) · 11.1 KB
/
Copy pathengine_test.go
File metadata and controls
426 lines (384 loc) · 11.1 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
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, false)
// 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, 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, false)
if len(moves) == 0 && p.name != "Empty" {
b.Fatal("unexpected zero moves")
}
}
})
}
}
func TestMoveTags(t *testing.T) {
tests := []struct {
name string
move Move
want MoveTag
fen string
}{
{
name: "move with queen side castle",
move: Move{s1: E8, s2: C8},
want: QueenSideCastle,
fen: "r3kb1r/p2nqppp/5n2/1B2p1B1/4P3/1Q6/PPP2PPP/R3K2R b KQkq - 1 12",
},
{
name: "move with king side castle",
move: Move{s1: E1, s2: G1},
want: KingSideCastle | Check,
fen: "r4b1r/ppp3pp/8/4p3/2Pq4/3P4/PP2QPPP/2k1K2R w K - 0 18",
},
{
name: "move with king side castle and check",
move: Move{s1: E1, s2: G1},
want: KingSideCastle | Check,
fen: "r4b1r/ppp3pp/8/4p3/2Pq4/3P1Q2/PP3PPP/1k2K2R w K - 2 19",
},
{
name: "move with check",
move: Move{s1: D7, s2: A4},
want: Check,
fen: "rn2k1r1/ppqb4/4p1n1/3pPp1Q/8/P1PP4/4NPPP/R1BK1B1R b q - 0 14",
},
{
name: "move leaves king in check",
move: Move{s1: G6, s2: F8},
want: inCheck,
fen: "r3k1r1/ppq5/2n1p1n1/3p1pBQ/b2P3P/P1P5/4NPP1/R3KB1R b q - 0 18",
},
{
name: "capture move",
move: Move{s1: G2, s2: G3},
want: Capture,
fen: "8/7p/3k2p1/8/2p2P2/R5bP/6K1/4r3 w - - 0 44",
},
{
name: "normal move without tags",
move: Move{s1: D6, s2: D5},
want: 0,
fen: "8/7p/3k2p1/8/2p2P2/R5KP/8/4r3 b - - 0 44",
},
{
name: "en passant move",
move: Move{s1: E4, s2: F3},
want: EnPassant | Check,
fen: "r3k2r/pbppqpb1/1pn3p1/7p/1N2pPn1/1PP4N/PB1P2PP/2QRKR2 b kq f3 0 1",
},
{
name: "normal move without tags",
move: Move{s1: B7, s2: A6},
want: 0,
fen: "r3k2r/pbppqpb1/1pn3p1/7p/1N2pPn1/1PP4N/PB1P2PP/2QRKR2 b kq f3 0 1",
},
{
name: "en passant move with check",
move: Move{s1: E4, s2: F3},
want: EnPassant | Check,
fen: "r3k1r1/pbppqpb1/1pn3p1/7p/1N2pPn1/1PP4N/PB1P2PP/2QRK1R1 b q f3 0 2",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.move.tags = moveTags(test.move, mustPosition(test.fen))
if test.move.tags != test.want {
t.Errorf("fen: %s | move: %s\ntags(%d) == expected_tags(%d)", test.fen, test.move.String(), test.move.tags, test.want)
}
})
}
}
func TestUnsafeMoves_StartingPosition(t *testing.T) {
pos := mustPosition("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
moves := engine{}.UnsafeMoves(pos)
if len(moves) != 0 {
t.Errorf("expected 0 unsafe moves in starting position, got %d", len(moves))
}
}
func TestPromotionCheckTagIsolation(t *testing.T) {
// FEN from issue #112: k7/4P3/8/8/8/8/8/K7 w - - 0 1
// White pawn on E7 can promote to E8. Only Queen and Rook give check
// to the black king on A8. Bishop and Knight should NOT have Check.
pos := mustPosition("k7/4P3/8/8/8/8/8/K7 w - - 0 1")
moves := pos.ValidMoves()
// Verify total move count (3 king moves + 4 promotions = 7)
if len(moves) != 7 {
t.Fatalf("expected 7 moves, got %d", len(moves))
}
// Helper: find move by s1, s2, promo fields (order-independent)
findMove := func(s1, s2 Square, promo PieceType) (Move, bool) {
for _, m := range moves {
if m.s1 == s1 && m.s2 == s2 && m.promo == promo {
return m, true
}
}
return Move{}, false
}
tests := []struct {
name string
promo PieceType
wantCheck bool
}{
{"queen promotion with check", Queen, true},
{"rook promotion with check", Rook, true},
{"bishop promotion without check", Bishop, false},
{"knight promotion without check", Knight, false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
m, ok := findMove(E7, E8, test.promo)
if !ok {
t.Fatalf("expected to find E7-E8=%s promotion move", test.promo.String())
}
gotCheck := m.HasTag(Check)
if gotCheck != test.wantCheck {
t.Errorf("E7-E8=%s: Check=%v, want %v", test.promo.String(), gotCheck, test.wantCheck)
}
})
}
}
func TestPromotionNoCheck(t *testing.T) {
// Position where no promotion gives check against an existing black king.
// Black king on A6 is not attacked by any promoted piece on E8.
pos := mustPosition("8/4P3/k7/8/8/8/8/7K w - - 0 1")
moves := pos.ValidMoves()
for _, m := range moves {
if m.s1 == E7 && m.s2 == E8 {
if m.HasTag(Check) {
t.Errorf("E7-E8=%s should NOT have Check tag", m.promo.String())
}
}
}
}
func TestUnsafeMoves_PinnedKnight(t *testing.T) {
pos := mustPosition("4k3/8/8/8/1b6/2N5/8/4K3 w - - 0 1")
moves := engine{}.UnsafeMoves(pos)
if len(moves) != 8 {
t.Fatalf("expected 8 unsafe moves for pinned knight, got %d", len(moves))
}
for _, m := range moves {
if m.s1 != C3 {
t.Errorf("expected unsafe move from C3, got %s", m.s1.String())
}
if !m.HasTag(inCheck) {
t.Errorf("expected unsafe move to have inCheck tag: %s", m.String())
}
}
}
func TestUnsafeMoves_KingIntoCheck(t *testing.T) {
pos := mustPosition("8/8/8/8/8/3r4/8/4K3 w - - 0 1")
moves := engine{}.UnsafeMoves(pos)
if len(moves) != 2 {
t.Fatalf("expected 2 unsafe moves, got %d", len(moves))
}
expected := map[string]bool{"d1": true, "d2": true}
for _, m := range moves {
if m.s1 != E1 {
t.Errorf("expected move from E1, got %s", m.s1.String())
}
if !expected[m.s2.String()] {
t.Errorf("unexpected unsafe move to %s", m.s2.String())
}
}
}
func TestDiscoveredCheck(t *testing.T) {
// White rook on e1 is blocked by white knight on e4 from checking black king on e8
// Moving Ne4-f6 reveals the check
pos := mustPosition("4k3/8/8/8/4N3/8/8/4R1K1 w - - 0 1")
moves := pos.ValidMoves()
foundDiscoveredCheck := false
for _, m := range moves {
if m.s1 == E4 && m.HasTag(Check) {
foundDiscoveredCheck = true
break
}
}
if !foundDiscoveredCheck {
t.Error("expected at least one move from e4 with discovered check")
}
}
func TestEnPassantDiscoveredCheck(t *testing.T) {
// White king on e1, black rook on e8, black pawn on e4 blocks the rook.
// White pawn on d2 moves to d4. Black captures e4xd3 en passant.
// After capture, pawn is on d3 (not on e-file), rook gives check.
pos := mustPosition("4r3/8/8/8/4p3/8/3P4/4K1k1 w - - 0 1")
// White plays d2-d4
moves := pos.ValidMoves()
var d2d4 Move
found := false
for _, m := range moves {
if m.s1 == D2 && m.s2 == D4 {
d2d4 = m
found = true
break
}
}
if !found {
t.Fatal("expected d2-d4 move")
}
// Apply the move and get the new position
pos2 := pos.Update(&d2d4)
// Black to move, en passant on d3
moves2 := pos2.ValidMoves()
foundEPCheck := false
for _, m := range moves2 {
if m.HasTag(EnPassant) && m.HasTag(Check) {
foundEPCheck = true
break
}
}
if !foundEPCheck {
t.Error("expected en passant move with discovered check")
}
}
func TestDoubleCheck(t *testing.T) {
// Black king on e8 is in double check from white queen on h5 and knight on f6
// Clear diagonal h5-e8 by removing pawn on f7: white knight on f6, queen on h5
// The position itself should have inCheck=true, and only king moves should be legal
pos := mustPosition("rnb1kbnr/pppp2pp/5N2/6pQ/8/8/PPPPPPPP/RNB1KB1R b KQkq - 0 1")
// Verify the position itself has the black king in check
if !pos.inCheck {
t.Error("expected black king to be in check in this position")
}
moves := pos.ValidMoves()
// In double check, only king moves are legal
for _, m := range moves {
if m.s1 != E8 {
t.Errorf("in double check, only king moves should be legal, got %s", m.String())
}
}
}
func TestKingMoveEscapesCheck(t *testing.T) {
// Black king on e8 is in check from white rook on e1
// Ke8-d8 should escape check
pos := mustPosition("4k3/8/8/8/8/8/8/4R3 b - - 0 1")
moves := pos.ValidMoves()
findMove := func(s1, s2 Square) (Move, bool) {
for _, m := range moves {
if m.s1 == s1 && m.s2 == s2 {
return m, true
}
}
return Move{}, false
}
m, ok := findMove(E8, D8)
if !ok {
t.Fatal("expected to find Ke8-d8 move")
}
if m.HasTag(inCheck) {
t.Error("Ke8-d8 should not leave king in check")
}
}
func TestKingMoveWalksIntoCheck(t *testing.T) {
// White king on e1, black rook on e8
// Ke1-e2 should be illegal (walks into check)
pos := mustPosition("4r3/8/8/8/8/8/8/4K3 w - - 0 1")
moves := engine{}.UnsafeMoves(pos)
findMove := func(s1, s2 Square) (Move, bool) {
for _, m := range moves {
if m.s1 == s1 && m.s2 == s2 {
return m, true
}
}
return Move{}, false
}
m, ok := findMove(E1, E2)
if !ok {
t.Fatal("expected to find Ke1-e2 unsafe move")
}
if !m.HasTag(inCheck) {
t.Error("Ke1-e2 should have inCheck tag")
}
}
func BenchmarkMoveTags(b *testing.B) {
pos := mustPosition("r1bqk2r/pppp1ppp/2n2n2/2b1p3/2B1P3/2N2N2/PPPP1PPP/R1BQK2R w KQkq - 0 1")
moves := engine{}.CalcMoves(pos, false)
if len(moves) == 0 {
b.Fatal("expected moves")
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, m := range moves {
_ = moveTags(m, pos)
}
}
}
// 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
}