diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 7ee85203a..154284413 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -11925,7 +11925,10 @@ bool TMoverParameters::readWiperList(std::string const &line) WriteLog("Read WiperList: number of entries exceeded capacity of the data table"); return false; } - parser >> WiperList[idx].byteSum >> WiperList[idx].WiperSpeed >> WiperList[idx].interval >> WiperList[idx].outBackDelay; + // the mask of the wipers is a number: read into the uint8_t directly, the parser would take only its first character + int bytesum{0}; + parser >> bytesum >> WiperList[idx].WiperSpeed >> WiperList[idx].interval >> WiperList[idx].outBackDelay; + WiperList[idx].byteSum = static_cast(std::clamp(bytesum, 0, 255)); return true; } diff --git a/tests/unit/test_wipers.cpp b/tests/unit/test_wipers.cpp new file mode 100644 index 000000000..cc6c7042e --- /dev/null +++ b/tests/unit/test_wipers.cpp @@ -0,0 +1,50 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +using test::definition; +using test::vehicle; + +// the first number of a WiperList row is the mask of the wipers working in that position of the wiper switch, +// bit n for the wiper n. it's a number: read as a character "5" was 53 and "12" was '1', the wiper 0 alone + +namespace { + +definition wiper_list(nlohmann::ordered_json const &Rows) { + return definition::load("cab_vehicle") + .set("WiperList", "rows", Rows) + .set("WiperList", "Size", static_cast(Rows.size())) + .set("WiperList", "Angle", 58); +} + +} // namespace + +TEST_CASE("wipers: the mask of the wipers of a WiperList row is read as a number") { + vehicle cab{wiper_list(nlohmann::ordered_json::array({ + {"0", "0.7", "0.0", "0.5"}, + {"5", "0.7", "7.0", "0.5"}, + {"12", "0.6", "0.7", "0.0"}, + {"15", "0.5", "0.5", "0.25"}, + }))}; + REQUIRE(cab->WiperListSize == 4); + + CHECK(cab->WiperList[0].byteSum == 0); + CHECK(cab->WiperList[1].byteSum == 5); + CHECK(cab->WiperList[2].byteSum == 12); + CHECK(cab->WiperList[3].byteSum == 15); + + // and the numbers after it still land in their own fields + CHECK(cab->WiperList[1].interval == doctest::Approx(7.0)); + CHECK(cab->WiperList[2].WiperSpeed == doctest::Approx(0.6)); + CHECK(cab->WiperList[3].outBackDelay == doctest::Approx(0.25)); +} + +TEST_CASE("wipers: a mask out of the range of a byte is clamped to it") { + vehicle cab{wiper_list(nlohmann::ordered_json::array({ + {"-1", "0.7", "0.0", "0.5"}, + {"300", "0.7", "0.0", "0.5"}, + }))}; + + CHECK(cab->WiperList[0].byteSum == 0); + CHECK(cab->WiperList[1].byteSum == 255); +}