diff --git a/src/Lynx/MoveGenerator.cs b/src/Lynx/MoveGenerator.cs index 245bcf99d..b0986c3e2 100644 --- a/src/Lynx/MoveGenerator.cs +++ b/src/Lynx/MoveGenerator.cs @@ -65,12 +65,7 @@ internal static Move[] GenerateAllMoves(Position position, bool capturesOnly = f [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span GenerateAllMoves(Position position, Span movePool) { -#if DEBUG - if (position.Side == Side.Both) - { - return []; - } -#endif + Debug.Assert(position.Side != Side.Both); int localIndex = 0; @@ -96,12 +91,7 @@ public static Span GenerateAllMoves(Position position, Span 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; @@ -127,12 +117,8 @@ public static Move[] GenerateAllCaptures(Position position, Move[] movePool) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span GenerateAllCaptures(Position position, Span movePool) { -#if DEBUG - if (position.Side == Side.Both) - { - return []; - } -#endif + Debug.Assert(position.Side != Side.Both); + int localIndex = 0; @@ -166,13 +152,7 @@ internal static void GenerateAllPawnMoves(ref int localIndex, Span 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; @@ -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; @@ -464,12 +438,7 @@ internal static void GeneratePieceCaptures(ref int localIndex, Span 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); @@ -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 @@ -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)) diff --git a/src/Lynx/UCIHandler.cs b/src/Lynx/UCIHandler.cs index dbd94a23c..e604481dd 100644 --- a/src/Lynx/UCIHandler.cs +++ b/src/Lynx/UCIHandler.cs @@ -131,17 +131,17 @@ static ReadOnlySpan ExtractCommandItems(string rawCommand) private void HandlePosition(ReadOnlySpan 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(); diff --git a/src/Lynx/ZobristTable.cs b/src/Lynx/ZobristTable.cs index 09d56cfe3..f173d0e7b 100644 --- a/src/Lynx/ZobristTable.cs +++ b/src/Lynx/ZobristTable.cs @@ -1,4 +1,5 @@ using Lynx.Model; +using System.Diagnostics; using System.Runtime.CompilerServices; namespace Lynx; @@ -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 diff --git a/tests/Lynx.Test/MoveGeneration/GeneratePawnMovesTest.cs b/tests/Lynx.Test/MoveGeneration/GeneratePawnMovesTest.cs index 8ac288c7b..44a2a6b87 100644 --- a/tests/Lynx.Test/MoveGeneration/GeneratePawnMovesTest.cs +++ b/tests/Lynx.Test/MoveGeneration/GeneratePawnMovesTest.cs @@ -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")] diff --git a/tests/Lynx.Test/ZobristTableTest.cs b/tests/Lynx.Test/ZobristTableTest.cs index 339a69c02..98378f3ab 100644 --- a/tests/Lynx.Test/ZobristTableTest.cs +++ b/tests/Lynx.Test/ZobristTableTest.cs @@ -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(() => ZobristTable.EnPassantHash(square)); - } - } -#endif } [Test]