Skip to content

Commit 87bf681

Browse files
authored
fix: duration parsing (#1065)
1 parent 4cff264 commit 87bf681

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

app/src/cli/cli.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ T parseScalar(std::string_view value) {
4141
try {
4242
if constexpr (std::is_floating_point_v<T>) {
4343
std::size_t parsed_length = 0;
44-
const auto parsed = std::stold(str, &parsed_length);
44+
const auto parsed = std::stold(str, &parsed_length);
4545
if (parsed_length != str.size() || !std::isfinite(parsed) ||
4646
parsed < std::numeric_limits<T>::lowest() || parsed > std::numeric_limits<T>::max()) {
4747
throw fastchess::fastchess_exception::format("Invalid numeric value: \"{}\"", str);
@@ -77,14 +77,15 @@ int64_t parseDuration(std::string_view value, int64_t multiplier) {
7777
value.remove_suffix(1);
7878
}
7979

80-
const auto duration = parseScalar<double>(value);
80+
const auto duration = parseScalar<long double>(value);
8181
const auto scaled = static_cast<long double>(duration) * multiplier;
82+
const auto rounded = std::round(scaled);
8283

83-
if (duration < 0 || scaled > std::numeric_limits<int64_t>::max()) {
84+
if (duration < 0 || rounded > std::numeric_limits<int64_t>::max()) {
8485
throw fastchess::fastchess_exception::format("Invalid time control duration: \"{}\"", value);
8586
}
8687

87-
return static_cast<int64_t>(scaled);
88+
return static_cast<int64_t>(rounded);
8889
}
8990

9091
// Parse a list of integers on the form 5,10,13-17,23 -> 5,10,13,14,15,16,17,23

app/tests/cli_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ TEST_SUITE("Option Parsing Tests") {
209209
CHECK(increment_tc.increment == 2);
210210
}
211211

212+
TEST_CASE("Time controls should round fractional seconds to milliseconds") {
213+
const auto tc = cli::OptionsParser{engineArgsWithTc("60+0.6")}.getEngineConfigs().front().limit.tc;
214+
215+
CHECK(tc.time == 60000);
216+
CHECK(tc.increment == 600);
217+
}
218+
212219
TEST_CASE("Numeric options should reject trailing characters and invalid ranges") {
213220
CHECK_THROWS_AS(cli::OptionsParser{baseArgs({"-rounds", "10junk"})}, fastchess_exception);
214221
CHECK_THROWS_AS(cli::OptionsParser{baseArgs({"-srand", "-1"})}, fastchess_exception);

0 commit comments

Comments
 (0)