Skip to content

Commit

Permalink
test: add tests for timecontrol (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
gahtan-syarif authored Sep 28, 2024
1 parent 8cf6822 commit 6a42db9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ SRC_FILES_TEST := \
$(TESTDIR)/player.cpp \
$(TESTDIR)/scoreboard_test.cpp \
$(TESTDIR)/sprt_test.cpp \
$(TESTDIR)/timecontrol_test.cpp \
$(TESTDIR)/uci_engine_test.cpp \
$(TESTDIR)/epd_reader_test.cpp \
$(TESTDIR)/match_test.cpp
Expand Down
60 changes: 60 additions & 0 deletions app/tests/timecontrol_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <time/timecontrol.hpp>

#include "doctest/doctest.hpp"

using namespace fastchess;

TEST_SUITE("TimeControl") {
TEST_CASE("moves/time+increment") {
TimeControl::Limits limits;
limits.moves = 3;
limits.time = 10000;
limits.increment = 100;
limits.timemargin = 100;

TimeControl tc(limits);

CHECK(tc.updateTime(5555) == true);
CHECK(tc.getTimeLeft() == limits.time + limits.increment - 5555 + limits.increment);
CHECK(tc.getMovesLeft() == 2);

CHECK(tc.updateTime(4745) == true);
CHECK(tc.getTimeLeft() == limits.increment);
CHECK(tc.getMovesLeft() == 1);

CHECK(tc.updateTime(50) == true);
CHECK(tc.getTimeLeft() == 100 + limits.time - 50 + limits.increment);
CHECK(tc.getMovesLeft() == 3);

CHECK(tc.updateTime(10251) == false);
CHECK(tc.getTimeLeft() == 10150 - 10251);
CHECK(tc.getMovesLeft() == 2);
}

TEST_CASE("Fixed time") {
TimeControl::Limits limits;
limits.fixed_time = 5000;
limits.timemargin = 200;

TimeControl tc(limits);


CHECK(tc.updateTime(limits.fixed_time + limits.timemargin - 1) == true);
CHECK(tc.getTimeLeft() == limits.fixed_time);

CHECK(tc.updateTime(limits.fixed_time + limits.timemargin) == true);
CHECK(tc.getTimeLeft() == limits.fixed_time);

CHECK(tc.updateTime(limits.fixed_time + limits.timemargin + 1) == false);
CHECK(tc.getTimeLeft() == limits.fixed_time - (limits.fixed_time + limits.timemargin + 1));
}

TEST_CASE("Fixed depth/nodes") {
TimeControl::Limits limits;

TimeControl tc(limits);

CHECK(tc.updateTime(523199) == true);
CHECK(tc.getTimeLeft() == 0);
}
}

0 comments on commit 6a42db9

Please sign in to comment.