From d786ac8fc49391e4efc69891b135979aa2848701 Mon Sep 17 00:00:00 2001 From: Aargh Rai Date: Sun, 31 May 2026 13:56:43 +0530 Subject: replaced asserts with test failures --- src/engine/bitboard.c | 17 +++++++++++------ src/engine/bitboard.h | 2 ++ src/engine/fen.c | 51 +++++++++++++++++++++++++++++++-------------------- src/engine/fen.h | 6 +++++- 4 files changed, 49 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/engine/bitboard.c b/src/engine/bitboard.c index 2d123ea..6e4f1b6 100644 --- a/src/engine/bitboard.c +++ b/src/engine/bitboard.c @@ -28,16 +28,21 @@ position_t position_starting() { } void assert_valid_position(position_t position) { - assert(position.castling <= + assert(check_valid_position(position) == true); +} + +bool check_valid_position(position_t position) { + if (position.castling > ( WHITE_SHORT_CASTLE | WHITE_LONG_CASTLE | BLACK_SHORT_CASTLE | BLACK_LONG_CASTLE ) - ); - assert(position.turn == WHITE_TURN || position.turn == BLACK_TURN); - assert(position.passantable_file <= 8); // 0 means no passant - assert(position.bitboards[WHITE_KING] != 0); - assert(position.bitboards[BLACK_KING] != 0); + ) return false; + if (position.turn != WHITE_TURN && position.turn != BLACK_TURN) return false; + if (position.passantable_file > 8) return false; // 0 means no passant + if (position.bitboards[WHITE_KING] == 0) return false; + if (position.bitboards[BLACK_KING] == 0) return false; + return true; } bitboard_t whites(position_t position) { diff --git a/src/engine/bitboard.h b/src/engine/bitboard.h index b137101..8515f5d 100644 --- a/src/engine/bitboard.h +++ b/src/engine/bitboard.h @@ -2,6 +2,7 @@ #define BITBOARD_H #include "ints.h" +#include typedef u64 bitboard_t; @@ -63,6 +64,7 @@ typedef struct { position_t position_starting(); void assert_valid_position(position_t position); +bool check_valid_position(position_t position); bitboard_t whites(position_t position); bitboard_t blacks(position_t position); diff --git a/src/engine/fen.c b/src/engine/fen.c index 187271e..a375b30 100644 --- a/src/engine/fen.c +++ b/src/engine/fen.c @@ -1,20 +1,21 @@ -#include "fen.h" -#include "bitboard.h" #include +#include #include +#include "fen.h" +#include "bitboard.h" int get_castling(char c) { if (c == 'K') return WHITE_SHORT_CASTLE; if (c == 'Q') return WHITE_LONG_CASTLE; if (c == 'k') return BLACK_SHORT_CASTLE; if (c == 'q') return BLACK_LONG_CASTLE; - assert(0); + return -1; } int get_turn(char c) { if (c == 'w') return WHITE_TURN; if (c == 'b') return BLACK_TURN; - assert(0); + return -1; } int get_piece_enum_item(char c) { @@ -32,7 +33,7 @@ int get_piece_enum_item(char c) { if (c == 'q') return BLACK_QUEEN; if (c == 'k') return BLACK_KING; - assert(0); + return -1; } /* @@ -78,7 +79,7 @@ int get_piece_enum_item(char c) { * * e.g. 1B6/2n5/p1N1P2R/P1K3N1/4Pk2/1Q2p2p/6nP/1B4R1 w - - 0 1 */ -position_t load_fen(const char* fen) { +struct fen_load load_fen(const char* fen) { position_t position = {0}; int square = 56; @@ -86,7 +87,7 @@ position_t load_fen(const char* fen) { char c; while ((c = fen[++i]) != ' ') { if (c >= '0' && c <= '9') { - assert(c != '9'); + if (c == '9') return (struct fen_load) { true, position }; square += c - '0'; continue; } @@ -96,57 +97,67 @@ position_t load_fen(const char* fen) { } int piece_type = get_piece_enum_item(c); + if (piece_type == -1) return (struct fen_load) { true, position }; position.bitboards[piece_type] |= (bitboard_t)1 << square; } c = fen[++i]; - position.turn = get_turn(c); - assert(fen[++i] == ' '); + int turn = get_turn(c); + if (turn == -1) return (struct fen_load) { true, position }; + position.turn = turn; + if (fen[++i] != ' ') return (struct fen_load) { true, position }; while ((c = fen[++i]) != ' ') { if (c == '-') continue; - position.castling |= get_castling(c); + int castling = get_castling(c); + if (castling == -1) return (struct fen_load) { true, position }; + position.castling |= castling; } c = fen[++i]; if (c >= 'a' && c <= 'z') { - assert(c <= 'h'); + if (c > 'h') return (struct fen_load) { true, position }; position.passantable_file = c - 'a' + 1; c = fen[++i]; - assert(c >= '1' && c <= '8'); + if (c < '1' && c > '8') return (struct fen_load) { true, position }; } else { position.passantable_file = 0; } - assert(fen[++i] == ' '); + if (fen[++i] != ' ') return (struct fen_load) { true, position }; while ((c = fen[++i]) != ' ') { - assert(c >= '0' && c <= '9'); + if (c < '0' || c > '9') return (struct fen_load) { true, position }; position.halfmove_clock *= 10; position.halfmove_clock += c - '0'; } while ((c = fen[++i]) != 0) { - assert(c >= '0' && c <= '9'); + if (c < '0' || c > '9') return (struct fen_load) { true, position }; position.fullmove_clock *= 10; position.fullmove_clock += c - '0'; } - assert_valid_position(position); - return position; + bool error = !check_valid_position(position); + return (struct fen_load) { error, position }; } #ifdef TEST_MOD #include "bitboard.c" -#include bool test_fen_no_passant() { - position_t p = load_fen("1B6/2n5/p1N1P2R/P1K3N1/4Pk2/1Q2p2p/6nP/1B4R1 w - - 0 1"); + struct fen_load r = load_fen("1B6/2n5/p1N1P2R/P1K3N1/4Pk2/1Q2p2p/6nP/1B4R1 w - - 0 1"); + if (r.failed) return false; + + position_t p = r.position; if (p.castling != 0) return false; if (p.passantable_file != 0) return false; return true; } bool test_fen_passant() { - position_t p = load_fen("rnbqkbnr/ppp1p1pp/8/3pPp2/8/8/PPPP1PPP/RNBQKBNR w KQkq f6 0 3"); + struct fen_load r = load_fen("rnbqkbnr/ppp1p1pp/8/3pPp2/8/8/PPPP1PPP/RNBQKBNR w KQkq f6 0 3"); + if (r.failed) return false; + + position_t p = r.position; if ( p.castling != ( WHITE_LONG_CASTLE | WHITE_SHORT_CASTLE | diff --git a/src/engine/fen.h b/src/engine/fen.h index c1444c1..3e5cecf 100644 --- a/src/engine/fen.h +++ b/src/engine/fen.h @@ -1,3 +1,7 @@ #include "bitboard.h" -position_t load_fen(const char* fen); +struct fen_load { + bool failed; + position_t position; +}; +struct fen_load load_fen(const char* fen); -- cgit v1.2.3