18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 11:47:06 +01:00
Files
maszyna/tests/unit/test_event_conditions.cpp
pawelw 1a12c7ef4f fix(events): say in the log when a condition passes because it can't test, and when a call is dropped (#448)
* fix(events): say in the log when a condition passes because it can't test, and when a call is dropped

A condition which can't do its test passes, and nothing told the scenery
author. Now the log says so when the scenery loads:

- trackfree / trackoccupied with no target, or with a target that isn't a
  track (an isolated section, a memory cell): the track test passes every
  time;
- a word in a condition the reader doesn't know, such as a name written
  after trackfree: it's skipped;
- a multiple comparing a memory cell that doesn't exist: besides the cell
  (already logged), that its whole memcompare condition is off and it
  always runs the events before its else.

And log.txt says when an event called while it's still in the queue isn't
added again, with the vehicle of the lost call and of the queued one.

Nothing changes in how events run.

* fix(events): log a lost call once per stay in the queue, set the activator of a joint event

A standing vehicle queues its event0 / eventall0 every frame, so an event
with a delay logged "EVENT ALREADY IN QUEUE" once a frame until it ran
(65 thousand lines in 70 s of a train standing on such a track). Now a lost
call is logged only when it comes from another vehicle than the one which
queued the event, and once per stay in the queue.

An instant event handing over to its joint (same-name) event queued that
event with the vehicle of its previous run. It gets the vehicle of the
call now, which it runs with and which the log names.

The message about a missing memory cell says that only the memcompare test
is off, and that the event always runs the events before its else only when
no track or probability test is left.

* fix(events): don't log a lost call from another vehicle of the same train

A standing train calls its eventall0 from every vehicle, so the second
vehicle's call was logged once per stay in the queue, and as the line is
written once per stay, it could hide a lost call of another train standing
on another track with the same event. A call from a vehicle coupled to the
one which queued the event isn't logged now.

* test(events): the unknown word in a condition is logged with the event and skipped

A name written after trackfree is read as a word the condition reader
doesn't know: one line in the log names the event and the word, and only
the track test is set. The known words of a condition log nothing.

* test(events): turn the log capture off when a case stops at a failed REQUIRE
2026-11-27 23:51:42 +01:00

173 lines
7.2 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "utilities/comparison.h"
#include "utilities/parser.h"
#include "world/Event.h"
#include <algorithm>
#include <cctype>
#include <sstream>
#include <string>
#include <vector>
// the lines logged while capture is on, kept by the engine stubs (engine_stubs.cpp)
void unit_tests_log_capture(bool Enabled);
std::vector<std::string> unit_tests_logged_lines();
// the condition block of the multiple and updatevalues events (world/eventconditions.cpp): the scenery editor writes
// the conditions of the events back with export_as_text(), and what it writes has to read back as the same conditions
namespace {
// the conditions are a part of the event, open to its kinds only
struct event_kind : basic_event {
using basic_event::event_conditions;
};
using conditions = event_kind::event_conditions;
// the log capture is on while the object lives, so a failed REQUIRE doesn't leave it on for the cases after it
class log_capture {
public:
log_capture() { unit_tests_log_capture(true); }
~log_capture() { unit_tests_log_capture(false); }
log_capture(log_capture const &) = delete;
log_capture &operator=(log_capture const &) = delete;
};
// the conditions of an event text, read the way the events read them: the parser stands on the word 'condition'
conditions read(std::string const &Text) {
cParser parser(Text, cParser::buffer_TEXT);
parser.getTokens();
REQUIRE(parser.peek() == "condition");
conditions result;
result.deserialize(parser, "test");
// the reading stops at the next keyword of the event and leaves it for the event
CHECK(parser.peek() == "endevent");
return result;
}
std::string write(conditions const &Conditions) {
std::ostringstream output;
Conditions.export_as_text(output);
return output.str();
}
// the conditions written and read again
conditions write_and_read(conditions const &Conditions) {
return read(write(Conditions) + "endevent");
}
void check_same(conditions const &Read, conditions const &Expected) {
CHECK(Read.flags == Expected.flags);
CHECK(Read.probability == Expected.probability);
CHECK(Read.memcompare_pass == Expected.memcompare_pass);
CHECK(Read.memcompare_text == Expected.memcompare_text);
CHECK(Read.memcompare_text_operator == Expected.memcompare_text_operator);
CHECK(Read.memcompare_value1 == Expected.memcompare_value1);
CHECK(Read.memcompare_value1_operator == Expected.memcompare_value1_operator);
CHECK(Read.memcompare_value2 == Expected.memcompare_value2);
CHECK(Read.memcompare_value2_operator == Expected.memcompare_value2_operator);
}
comparison_operator const operators[]{comparison_operator::equal, comparison_operator::not_equal, comparison_operator::lt,
comparison_operator::gt, comparison_operator::lt_eq, comparison_operator::gt_eq};
comparison_pass const passes[]{comparison_pass::all, comparison_pass::any, comparison_pass::none};
} // namespace
TEST_CASE("event conditions: each comparison operator and pass is written as the word that reads back as it") {
for (auto const op : operators) {
CAPTURE(to_string(op));
CHECK(comparison_operator_from_string(to_string(op)) == op);
}
CHECK(to_string(comparison_operator::gt_eq) == ">=");
for (auto const pass : passes) {
CAPTURE(to_string(pass));
CHECK(comparison_pass_from_string(to_string(pass)) == pass);
}
}
TEST_CASE("event conditions: memcompareex is written as the pass, then the operator before each compared value") {
auto const conditions{read("condition memcompareex any != Blokada >= 1 < 2.5 endevent")};
CHECK(conditions.memcompare_pass == comparison_pass::any);
CHECK(conditions.memcompare_text == "Blokada");
CHECK(conditions.memcompare_text_operator == comparison_operator::not_equal);
CHECK(conditions.memcompare_value1_operator == comparison_operator::gt_eq);
CHECK(conditions.memcompare_value2_operator == comparison_operator::lt);
CHECK(write(conditions) == "condition memcompareex any != Blokada >= 1.000000 < 2.500000 ");
check_same(write_and_read(conditions), conditions);
}
TEST_CASE("event conditions: memcompareex reads back the same for every pass, operator and left out value") {
for (auto const pass : passes) {
for (auto const op : operators) {
for (unsigned int used{0}; used < 8; ++used) {
std::string text{"condition memcompareex " + to_string(pass)};
text += (used & 1) ? " " + to_string(op) + " Zajety_tor" : std::string{" *"};
text += (used & 2) ? " " + to_string(op) + " -3.25" : std::string{" *"};
text += (used & 4) ? " " + to_string(op) + " 1250" : std::string{" *"};
text += " endevent";
CAPTURE(text);
auto const conditions{read(text)};
CHECK(conditions.flags == used);
if (used == 0) {
// nothing compared is no condition, and no condition is written
CHECK(write(conditions).empty());
continue;
}
check_same(write_and_read(conditions), conditions);
}
}
}
}
TEST_CASE("event conditions: the old memcompare is written as memcompareex all with == and reads back the same") {
auto const conditions{read("condition memcompare Wolny * 3 endevent")};
CHECK(write(conditions) == "condition memcompareex all == Wolny * == 3.000000 ");
check_same(write_and_read(conditions), conditions);
auto const all{read("condition memcompare Wolny 1 0 endevent")};
check_same(write_and_read(all), all);
}
TEST_CASE("event conditions: the track and probability conditions read back along with the comparison") {
auto const conditions{read("condition trackoccupied trackfree probability 0.25 memcompareex none <= 3 * > -1.5 endevent")};
CHECK(conditions.flags == (basic_event::flags::track_busy | basic_event::flags::track_free | basic_event::flags::probability |
basic_event::flags::text | basic_event::flags::value2));
check_same(write_and_read(conditions), conditions);
auto const probability{read("condition probability 0.5 endevent")};
CHECK(write(probability) == "condition probability 0.5 ");
check_same(write_and_read(probability), probability);
}
TEST_CASE("event conditions: an event without conditions writes none") {
CHECK(write(conditions{}).empty());
}
TEST_CASE("event conditions: a word the reader doesn't know is logged with the event and skipped") {
log_capture const capture;
// a name after trackfree: the track test takes the targets of the event, the name is skipped
auto const result{read("condition trackfree SomeTrack endevent")};
auto lines{unit_tests_logged_lines()};
CHECK(result.flags == basic_event::flags::track_free);
REQUIRE(lines.size() == 1);
// the parser may give the word in lower case
auto &line{lines.front()};
std::ranges::transform(line, line.begin(), [](unsigned char const Char) { return static_cast<char>(std::tolower(Char)); });
CHECK(line.find("\"test\"") != std::string::npos);
CHECK(line.find("\"sometrack\"") != std::string::npos);
CHECK(line.find("unknown word") != std::string::npos);
}
TEST_CASE("event conditions: the known words log nothing") {
log_capture const capture;
auto const result{read("condition trackoccupied probability 0.5 memcompareex any == 1 * * endevent")};
auto const lines{unit_tests_logged_lines()};
CHECK(result.flags == (basic_event::flags::track_busy | basic_event::flags::probability | basic_event::flags::text));
CHECK(lines.empty());
}