diff --git a/Makefile b/Makefile index 7fbfd41..1747b22 100644 --- a/Makefile +++ b/Makefile @@ -286,7 +286,7 @@ memcheck: targets ##################################### test binaries .PHONY: testing test -TEST :=piece-test fen-test bitboard-test movegen-test attack-test +TEST := piece-test fen-test bitboard-test movegen-test attack-test movedo-test PIECE_OBJS := piece.o FEN_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \ @@ -297,6 +297,8 @@ MOVEGEN_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintesse attack.o move.o move-gen.o ATTACK_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \ attack.o move.o move-gen.o +MOVEDO_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \ + attack.o move.o move-gen.o move-do.o TEST := $(addprefix $(BINDIR)/,$(TEST)) @@ -305,6 +307,7 @@ FEN_OBJS := $(addprefix $(OBJDIR)/,$(FEN_OBJS)) BB_OBJS := $(addprefix $(OBJDIR)/,$(BB_OBJS)) MOVEGEN_OBJS := $(addprefix $(OBJDIR)/,$(MOVEGEN_OBJS)) ATTACK_OBJS := $(addprefix $(OBJDIR)/,$(ATTACK_OBJS)) +MOVEDO_OBJS := $(addprefix $(OBJDIR)/,$(MOVEDO_OBJS)) test: echo TEST=$(TEST) @@ -332,6 +335,10 @@ bin/attack-test: test/attack-test.c test/common-test.h $(ATTACK_OBJS) @echo compiling $@ test executable. @$(CC) $(ALL_CFLAGS) $< $(ATTACK_OBJS) $(ALL_LDFLAGS) -o $@ +bin/movedo-test: test/movedo-test.c test/common-test.h $(MOVEDO_OBJS) + @echo compiling $@ test executable. + @$(CC) $(ALL_CFLAGS) $< $(MOVEDO_OBJS) $(ALL_LDFLAGS) -o $@ + ##################################### Makefile debug .PHONY: showflags wft diff --git a/test/common-test.h b/test/common-test.h index 3064379..9486ca5 100644 --- a/test/common-test.h +++ b/test/common-test.h @@ -14,11 +14,12 @@ #include #include "chessdefs.h" /* when below FENs are in a struct with selection per test */ -#define NOTEST 0 -#define FEN 1 -#define BITBOARD 2 -#define MOVEGEN 4 -#define ATTACK 8 +#define NOTEST 0 +#define FEN 1 +#define BITBOARD 2 +#define MOVEGEN 4 +#define ATTACK 8 +#define MOVEDO 16 struct fentest { uint modules; @@ -367,7 +368,18 @@ struct fentest { "illegal, SF crash", "2r1k3/3P4/8/8/8/8/8/4K3 w - - 0 1" }, - + { MOVEDO, + "simple movedo/undo: only 2 W knights", + "8/1k6/8/8/8/8/6K1/1NN5 w - - 0 1" + }, + { MOVEDO, + "simple movedo/undo: only 2 W knights", + "8/1k6/8/8/8/8/6K1/1NN5 w - - 0 1" + }, + { MOVEDO, + "simple movedo/undo: only 2 W knights", + "5n2/1k6/8/8/5K2/8/P7/1N6 w - - 0 1" + }, { 0, NULL, NULL } }; diff --git a/test/movedo-test.c b/test/movedo-test.c new file mode 100644 index 0000000..c498514 --- /dev/null +++ b/test/movedo-test.c @@ -0,0 +1,80 @@ +/* movedo-test.c - basic movedo/undo tests. + * + * Copyright (C) 2024 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include +#include +#include + +#include "chessdefs.h" +#include "fen.h" +#include "position.h" +#include "move.h" +#include "move-do.h" +#include "move-gen.h" + +#include "common-test.h" + +int main(int __unused ac, __unused char**av) +{ + int i = 1; + char *fen, movebuf[8];; + pos_t *pos, *savepos; + move_t move; + + setlinebuf(stdout); /* line-buffered stdout */ + + bitboard_init(); + hyperbola_init(); + + while ((fen = next_fen(MOVEGEN | MOVEDO))) { + if (!(pos = fen2pos(NULL, fen))) { + printf("wrong fen %d: [%s]\n", i, fen); + continue; + } + pos_gen_pseudomoves(pos); + savepos = pos_dup(pos); + if (pos_cmp(pos, savepos) != true) { + printf("*** positions differ 1\n"); + exit(0); + } + int tmp = 0, j = 1; + while ((move = pos_next_legal(pos, &tmp)) != MOVE_NONE) { + state_t state; + + pos_print(pos); + printf("i=%d j=%d turn=%d move=[%s]\n", i, j, pos->turn, + move_str(movebuf, move, 0)); + //move_p + move_do(pos, move, &state); + pos_print(pos); + fflush(stdout); + pos_check(pos, true); + printf("%d/%d move_do check ok\n", i, j); + move_undo(pos, move, &state); + if (pos_cmp(pos, savepos) != true) { + printf("*** positions differ 2\n"); + exit(0); + } + fflush(stdout); + pos_check(pos, true); + printf("%d/%d move_undo check ok\n", i, j); + + if (j++ == 1000) + exit(0); + } + pos_del(savepos); + pos_del(pos); + i++; + } + return 0; +}