Skip to content

Commit

Permalink
🧹 Replace #if DEBUG with asserts whenever possible (#1154)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio authored Nov 12, 2024
1 parent 7c5d1e3 commit 266357d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 70 deletions.
56 changes: 10 additions & 46 deletions src/Lynx/MoveGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ internal static Move[] GenerateAllMoves(Position position, bool capturesOnly = f
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<Move> GenerateAllMoves(Position position, Span<Move> movePool)
{
#if DEBUG
if (position.Side == Side.Both)
{
return [];
}
#endif
Debug.Assert(position.Side != Side.Both);

int localIndex = 0;

Expand All @@ -96,12 +91,7 @@ public static Span<Move> GenerateAllMoves(Position position, Span<Move> movePool
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Move[] GenerateAllCaptures(Position position, Move[] movePool)
{
#if DEBUG
if (position.Side == Side.Both)
{
return [];
}
#endif
Debug.Assert(position.Side != Side.Both);

int localIndex = 0;

Expand All @@ -127,12 +117,8 @@ public static Move[] GenerateAllCaptures(Position position, Move[] movePool)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<Move> GenerateAllCaptures(Position position, Span<Move> movePool)
{
#if DEBUG
if (position.Side == Side.Both)
{
return [];
}
#endif
Debug.Assert(position.Side != Side.Both);


int localIndex = 0;

Expand Down Expand Up @@ -166,13 +152,7 @@ internal static void GenerateAllPawnMoves(ref int localIndex, Span<Move> movePoo

var sourceRank = (sourceSquare >> 3) + 1;

#if DEBUG
if (sourceRank == 1 || sourceRank == 8)
{
_logger.Warn("There's a non-promoted {0} pawn in rank {1}", position.Side, sourceRank);
continue;
}
#endif
Debug.Assert(sourceRank != 1 && sourceRank != 8, $"There's a non-promoted {position.Side} pawn in rank {sourceRank})");

// Pawn pushes
var singlePushSquare = sourceSquare + pawnPush;
Expand Down Expand Up @@ -252,13 +232,7 @@ internal static void GeneratePawnCapturesAndPromotions(ref int localIndex, Span<

var sourceRank = (sourceSquare >> 3) + 1;

#if DEBUG
if (sourceRank == 1 || sourceRank == 8)
{
_logger.Warn("There's a non-promoted {0} pawn in rank {1}", position.Side, sourceRank);
continue;
}
#endif
Debug.Assert(sourceRank != 1 && sourceRank != 8, $"There's a non-promoted {position.Side} pawn in rank {sourceRank})");

// Pawn pushes
var singlePushSquare = sourceSquare + pawnPush;
Expand Down Expand Up @@ -464,12 +438,7 @@ internal static void GeneratePieceCaptures(ref int localIndex, Span<Move> movePo
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CanGenerateAtLeastAValidMove(Position position)
{
#if DEBUG
if (position.Side == Side.Both)
{
return false;
}
#endif
Debug.Assert(position.Side != Side.Both);

var offset = Utils.PieceOffset(position.Side);

Expand All @@ -488,7 +457,7 @@ public static bool CanGenerateAtLeastAValidMove(Position position)
}
catch (Exception e)
{
Debug.Fail($"Error in {nameof(CanGenerateAtLeastAValidMove)}", e.StackTrace);
_logger.Error(e, $"Error in {nameof(CanGenerateAtLeastAValidMove)}");
return false;
}
#endif
Expand All @@ -511,13 +480,8 @@ private static bool IsAnyPawnMoveValid(Position position, int offset)

var sourceRank = (sourceSquare >> 3) + 1;

#if DEBUG
if (sourceRank == 1 || sourceRank == 8)
{
_logger.Warn("There's a non-promoted {0} pawn in rank {1}", position.Side, sourceRank);
continue;
}
#endif
Debug.Assert(sourceRank != 1 && sourceRank != 8, $"There's a non-promoted {position.Side} pawn in rank {sourceRank})");

// Pawn pushes
var singlePushSquare = sourceSquare + pawnPush;
if (!position.OccupancyBitBoards[2].GetBit(singlePushSquare))
Expand Down
6 changes: 3 additions & 3 deletions src/Lynx/UCIHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,17 @@ static ReadOnlySpan<char> ExtractCommandItems(string rawCommand)

private void HandlePosition(ReadOnlySpan<char> command)
{
var sw = Stopwatch.StartNew();
#if DEBUG
var sw = Stopwatch.StartNew();
_engine.Game.CurrentPosition.Print();
#endif

_engine.AdjustPosition(command);

#if DEBUG
_engine.Game.CurrentPosition.Print();
#endif

_logger.Info("Position parsing took {0}ms", sw.ElapsedMilliseconds);
#endif
}

private void HandleStop() => _engine.StopSearching();
Expand Down
9 changes: 3 additions & 6 deletions src/Lynx/ZobristTable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lynx.Model;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace Lynx;
Expand Down Expand Up @@ -35,12 +36,8 @@ public static ulong EnPassantHash(int enPassantSquare)
return default;
}

#if DEBUG
if (Constants.EnPassantCaptureSquares.Length <= enPassantSquare || Constants.EnPassantCaptureSquares[enPassantSquare] == 0)
{
throw new ArgumentException($"{Constants.Coordinates[enPassantSquare]} is not a valid en-passant square");
}
#endif
Debug.Assert(Constants.EnPassantCaptureSquares.Length > enPassantSquare && Constants.EnPassantCaptureSquares[enPassantSquare] != 0,
$"{Constants.Coordinates[enPassantSquare]} is not a valid en-passant square");

var file = enPassantSquare & 0x07; // enPassantSquare % 8

Expand Down
4 changes: 0 additions & 4 deletions tests/Lynx.Test/MoveGeneration/GeneratePawnMovesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,6 @@ public void DoubleEnPassant(string fen)
Assert.AreEqual(2, moves.Count(m => m.IsEnPassant() && m.IsCapture()));
}

#if DEBUG
[TestCase("PPPPPPPP/8/8/8/8/8/8/8 w - - 0 1", Description = "Last rank")]
[TestCase("8/8/8/8/8/8/8/pppppppp b - - 0 1", Description = "Last rank")]
#endif
[TestCase("8/8/8/p1p1p1p1/PpPpPpPp/1P1P1P1P/8/8 w - - 0 1", Description = "Blocked position")]
[TestCase("8/8/8/p1p1p1p1/PpPpPpPp/1P1P1P1P/8/8 b - - 0 1", Description = "Blocked position")]
[TestCase("8/8/8/N7/P7/1p6/8/8 w - - 0 1", Description = "Backwards/inverse capture")]
Expand Down
11 changes: 0 additions & 11 deletions tests/Lynx.Test/ZobristTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,6 @@ public void EnPassantHash()
}

Assert.AreEqual(16, enPassantSquares.Count());

#if DEBUG
for (int square = 0; square < 64; ++square)
{
var rank = square / 8;
if (rank != 2 && rank != 5)
{
Assert.Throws<ArgumentException>(() => ZobristTable.EnPassantHash(square));
}
}
#endif
}

[Test]
Expand Down

0 comments on commit 266357d

Please sign in to comment.