Skip to content

Commit

Permalink
chess: add benchmarks for Position.IsKingInCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
e0ff committed Nov 26, 2023
1 parent 10c7adf commit aab5636
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions internal/chess/position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,44 @@ func BenchmarkMakeUciMove(b *testing.B) {
position.Undo()
}
}

func isKingInCheckBenchmark(b *testing.B, fen string, color Color) {
position, _ := NewPosition(fen)
for i := 0; i < b.N; i++ {
position.IsKingInCheck(color)
}
}

func BenchmarkIsKingInCheck(b *testing.B) {
cases := []struct {
Name string
Fen string
InCheck bool
Color Color
}{
{
Name: "NotInCheck",
Fen: StartingFen,
InCheck: false,
Color: White,
},
{
Name: "InCheck",
Fen: "rn2kbnr/ppp2ppp/3pb3/4p3/2B1q3/BPN5/P1PP1PPP/R2QK1NR w KQkq - 0 6",
InCheck: true,
Color: White,
},
{
Name: "DoubleCheck",
Fen: "rnbk1b1r/pp3ppp/2p5/4q1B1/4n3/8/PPP2PPP/2KR1BNR b - - 1 10",
InCheck: true,
Color: Black,
},
}

for _, c := range cases {
b.Run(c.Name, func(b *testing.B) {
isKingInCheckBenchmark(b, c.Fen, c.Color)
})
}
}

0 comments on commit aab5636

Please sign in to comment.