Skip to content

Commit

Permalink
Modify tests to use the span variant as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio committed Dec 28, 2023
1 parent 91d9717 commit 5933eb9
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 31 deletions.
1 change: 1 addition & 0 deletions .github/workflows/perft.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ on:
divide:
description: 'Run also divide'
required: false
default: false
type: boolean

jobs:
Expand Down
1 change: 1 addition & 0 deletions src/Lynx/Model/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static Move Encode(
}

/// <summary>
/// TODO: try to make span-compatible
/// Returns the move from <paramref name="moveList"/> indicated by <paramref name="UCIString"/>
/// </summary>
/// <param name="UCIString"></param>
Expand Down
12 changes: 8 additions & 4 deletions src/Lynx/MoveGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ public static class MoveGenerator
/// <param name="capturesOnly">Filters out all moves but captures</param>
/// <returns></returns>
[Obsolete("dev and test only")]
internal static Move[] GenerateAllMoves(Position position, bool capturesOnly = false) =>
capturesOnly
? GenerateAllCaptures(position, new Move[Constants.MaxNumberOfPossibleMovesInAPosition])
: GenerateAllMoves(position, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]);
internal static Move[] GenerateAllMoves(Position position, bool capturesOnly = false)
{
Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];

return (capturesOnly
? GenerateAllCaptures(position, moves)
: GenerateAllMoves(position, moves)).ToArray();
}

/// <summary>
/// Generates all psuedo-legal moves from <paramref name="position"/>, ordered by <see cref="Move.Score(Position)"/>
Expand Down
6 changes: 4 additions & 2 deletions tests/Lynx.Test/MoveGeneration/GeneralMoveGeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ public class GeneralMoveGeneratorTest
public void DiscoveredCheckAfterEnPassantCapture()
{
var originalPosition = new Position("8/8/8/k1pP3R/8/8/8/n4K2 w - c6 0 1");
var enPassantMove = MoveGenerator.GenerateAllMoves(originalPosition, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]).Single(m => m.IsEnPassant());
Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
var enPassantMove = MoveGenerator.GenerateAllMoves(originalPosition, moves).ToArray().Single(m => m.IsEnPassant());
var positionAferEnPassant = new Position(originalPosition, enPassantMove);

foreach (var move in MoveGenerator.GenerateAllMoves(positionAferEnPassant, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]))
moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
foreach (var move in MoveGenerator.GenerateAllMoves(positionAferEnPassant, moves))
{
if (new Position(positionAferEnPassant, move).IsValid())
{
Expand Down
14 changes: 10 additions & 4 deletions tests/Lynx.Test/MoveGeneration/GenerateBishopMovesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ namespace Lynx.Test.MoveGeneration;

public class GenerateBishopMovesTest
{
private static IEnumerable<Move> GenerateBishopMoves(Position position) =>
MoveGenerator.GenerateAllMoves(position, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]).Where(m => m.Piece() == (int)Piece.B || m.Piece() == (int)Piece.b);
private static IEnumerable<Move> GenerateBishopMoves(Position position)
{
Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
return MoveGenerator.GenerateAllMoves(position, moves).ToArray().Where(m => m.Piece() == (int)Piece.B || m.Piece() == (int)Piece.b);
}

private static IEnumerable<Move> GenerateBishopCaptures(Position position) =>
MoveGenerator.GenerateAllCaptures(position, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]).Where(m => m.Piece() == (int)Piece.B || m.Piece() == (int)Piece.b);
private static IEnumerable<Move> GenerateBishopCaptures(Position position)
{
Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
return MoveGenerator.GenerateAllCaptures(position, moves).ToArray().Where(m => m.Piece() == (int)Piece.B || m.Piece() == (int)Piece.b);
}

[TestCase(Constants.EmptyBoardFEN, 0)]
[TestCase(Constants.InitialPositionFEN, 0)]
Expand Down
14 changes: 10 additions & 4 deletions tests/Lynx.Test/MoveGeneration/GenerateKingMovesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ namespace Lynx.Test.MoveGeneration;

public class GenerateKingMovesTest
{
private static IEnumerable<Move> GenerateKingMoves(Position position) =>
MoveGenerator.GenerateAllMoves(position, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]).Where(m => m.Piece() == (int)Piece.K || m.Piece() == (int)Piece.k);
private static IEnumerable<Move> GenerateKingMoves(Position position)
{
Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
return MoveGenerator.GenerateAllMoves(position, moves).ToArray().Where(m => m.Piece() == (int)Piece.K || m.Piece() == (int)Piece.k);
}

private static IEnumerable<Move> GenerateKingCaptures(Position position) =>
MoveGenerator.GenerateAllCaptures(position, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]).Where(m => m.Piece() == (int)Piece.K || m.Piece() == (int)Piece.k);
private static IEnumerable<Move> GenerateKingCaptures(Position position)
{
Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
return MoveGenerator.GenerateAllCaptures(position, moves).ToArray().Where(m => m.Piece() == (int)Piece.K || m.Piece() == (int)Piece.k);
}

[TestCase(Constants.InitialPositionFEN, 0)]
[TestCase("8/8/8/2PPP3/2PKP3/2P1P3/8/8 w - - 0 1", 1)]
Expand Down
14 changes: 10 additions & 4 deletions tests/Lynx.Test/MoveGeneration/GenerateKnightMovesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ namespace Lynx.Test.MoveGeneration;

public class GenerateKnightMovesTest
{
private static IEnumerable<Move> GenerateKnightMoves(Position position) =>
MoveGenerator.GenerateAllMoves(position, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]).Where(m => m.Piece() == (int)Piece.N || m.Piece() == (int)Piece.n);
private static IEnumerable<Move> GenerateKnightMoves(Position position)
{
Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
return MoveGenerator.GenerateAllMoves(position, moves).ToArray().Where(m => m.Piece() == (int)Piece.N || m.Piece() == (int)Piece.n);
}

private static IEnumerable<Move> GenerateKnightCaptures(Position position) =>
MoveGenerator.GenerateAllCaptures(position, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]).Where(m => m.Piece() == (int)Piece.N || m.Piece() == (int)Piece.n);
private static IEnumerable<Move> GenerateKnightCaptures(Position position)
{
Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
return MoveGenerator.GenerateAllCaptures(position, moves).ToArray().Where(m => m.Piece() == (int)Piece.N || m.Piece() == (int)Piece.n);
}

[TestCase(Constants.EmptyBoardFEN, 0)]
[TestCase(Constants.InitialPositionFEN, 4)]
Expand Down
14 changes: 10 additions & 4 deletions tests/Lynx.Test/MoveGeneration/GeneratePawnMovesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ public class GeneratePawnMovesTest
{
#pragma warning disable RCS1098 // Constant values should be placed on right side of comparisons.

private static IEnumerable<Move> GeneratePawnMoves(Position position) =>
MoveGenerator.GenerateAllMoves(position, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]).Where(m => m.Piece() % (int)Piece.p == 0);
private static IEnumerable<Move> GeneratePawnMoves(Position position)
{
Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
return MoveGenerator.GenerateAllMoves(position, moves).ToArray().Where(m => m.Piece() % (int)Piece.p == 0);
}

private static IEnumerable<Move> GeneratePawnCaptures(Position position) =>
MoveGenerator.GenerateAllCaptures(position, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]).Where(m => m.Piece() % (int)Piece.p == 0);
private static IEnumerable<Move> GeneratePawnCaptures(Position position)
{
Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
return MoveGenerator.GenerateAllCaptures(position, moves).ToArray().Where(m => m.Piece() % (int)Piece.p == 0);
}

[Test]
public void QuietMoves()
Expand Down
14 changes: 10 additions & 4 deletions tests/Lynx.Test/MoveGeneration/GenerateQueenMovesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ namespace Lynx.Test.MoveGeneration;

public class GenerateQueenMovesTest
{
private static IEnumerable<Move> GenerateQueenMoves(Position position) =>
MoveGenerator.GenerateAllMoves(position, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]).Where(m => m.Piece() == (int)Piece.Q || m.Piece() == (int)Piece.q);
private static IEnumerable<Move> GenerateQueenMoves(Position position)
{
Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
return MoveGenerator.GenerateAllMoves(position, moves).ToArray().Where(m => m.Piece() == (int)Piece.Q || m.Piece() == (int)Piece.q);
}

private static IEnumerable<Move> GenerateQueenCaptures(Position position) =>
MoveGenerator.GenerateAllCaptures(position, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]).Where(m => m.Piece() == (int)Piece.Q || m.Piece() == (int)Piece.q);
private static IEnumerable<Move> GenerateQueenCaptures(Position position)
{
Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
return MoveGenerator.GenerateAllCaptures(position, moves).ToArray().Where(m => m.Piece() == (int)Piece.Q || m.Piece() == (int)Piece.q);
}

[TestCase(Constants.EmptyBoardFEN, 0)]
[TestCase(Constants.InitialPositionFEN, 0)]
Expand Down
14 changes: 10 additions & 4 deletions tests/Lynx.Test/MoveGeneration/GenerateRookMovesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ namespace Lynx.Test.MoveGeneration;

public class GenerateRookMovesTest
{
private static IEnumerable<Move> GenerateRookMoves(Position position) =>
MoveGenerator.GenerateAllMoves(position, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]).Where(m => m.Piece() == (int)Piece.R || m.Piece() == (int)Piece.r);
private static IEnumerable<Move> GenerateRookMoves(Position position)
{
Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
return MoveGenerator.GenerateAllMoves(position, moves).ToArray().Where(m => m.Piece() == (int)Piece.R || m.Piece() == (int)Piece.r);
}

private static IEnumerable<Move> GenerateRookCaptures(Position position) =>
MoveGenerator.GenerateAllCaptures(position, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]).Where(m => m.Piece() == (int)Piece.R || m.Piece() == (int)Piece.r);
private static IEnumerable<Move> GenerateRookCaptures(Position position)
{
Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
return MoveGenerator.GenerateAllCaptures(position, moves).ToArray().Where(m => m.Piece() == (int)Piece.R || m.Piece() == (int)Piece.r);
}

[TestCase(Constants.EmptyBoardFEN, 0)]
[TestCase(Constants.InitialPositionFEN, 0)]
Expand Down
3 changes: 2 additions & 1 deletion tests/Lynx.Test/MoveGeneration/RegressionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public void AllMovesAreGenerated()
Assert.True(moves.Exists(m => m.PromotedPiece() != default && !m.IsCapture()));
Assert.True(moves.Exists(m => m.IsDoublePawnPush()));

var captures = MoveGenerator.GenerateAllCaptures(position, new Move[Constants.MaxNumberOfPossibleMovesInAPosition]).ToList();
Span<Move> moveSpan = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
var captures = MoveGenerator.GenerateAllCaptures(position, moveSpan).ToArray().ToList();

Assert.True(moves.Exists(m => m.IsShortCastle()));
Assert.True(moves.Exists(m => m.IsLongCastle()));
Expand Down

0 comments on commit 5933eb9

Please sign in to comment.