summaryrefslogtreecommitdiff
path: root/src/engine/fen.c
diff options
context:
space:
mode:
authorAargh Rai <aargh.rai+git@gmail.com>2026-05-31 13:56:43 +0530
committerAargh Rai <aargh.rai+git@gmail.com>2026-05-31 13:56:43 +0530
commitd786ac8fc49391e4efc69891b135979aa2848701 (patch)
tree011f6ae51da54494495c21a8a1240233a6d3f642 /src/engine/fen.c
parent8671756d141292d4ec9747ec8de6e9cf3324777d (diff)
replaced asserts with test failures
Diffstat (limited to 'src/engine/fen.c')
-rw-r--r--src/engine/fen.c51
1 files changed, 31 insertions, 20 deletions
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 <assert.h>
+#include <stdbool.h>
#include <stdio.h>
+#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 <stdbool.h>
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 |