mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-24 13:09:16 +01:00
Merge branch 'master' into feat/renderers-cleanup
This commit is contained in:
@@ -34,6 +34,8 @@ add_executable(${UNIT_TESTS_NAME}
|
||||
"${CMAKE_SOURCE_DIR}/editor/editorFouling.cpp"
|
||||
# which textures the shadow maps draw without the alpha test
|
||||
"${CMAKE_SOURCE_DIR}/rendering/common/texturealpha.cpp"
|
||||
# and the http client the editor fetches its aids with
|
||||
"${CMAKE_SOURCE_DIR}/editor/editorHttp.cpp"
|
||||
${UNIT_TEST_CASES})
|
||||
|
||||
# the core library brings the game's build settings along
|
||||
|
||||
195
tests/unit/test_braking_curve.cpp
Normal file
195
tests/unit/test_braking_curve.cpp
Normal file
@@ -0,0 +1,195 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
|
||||
#include "vehicle/brakingcurve.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
|
||||
// the deceleration the AI asks for on its way down to a stop or a lower speed ahead, outside of the final approach
|
||||
// (TController::adjust_desired_speed_for_braking_curve). in shunting the brake table of the consist isn't worked out
|
||||
// and stays at zero; planned from it, the curve asked for next to no braking, and shunting engines ran through the
|
||||
// signals they were to stop at.
|
||||
|
||||
namespace {
|
||||
|
||||
auto constexpr tablesize{20};
|
||||
using brake_values = std::array<double, tablesize + 1>;
|
||||
|
||||
// what the AI holds in shunting: nothing worked out
|
||||
struct zeroed_table {
|
||||
brake_values a0{};
|
||||
brake_values a1{};
|
||||
|
||||
braking_curve::brake_table table() const { return {a0.data(), a1.data(), tablesize, 100.0}; }
|
||||
};
|
||||
|
||||
// a train brake that gives the same deceleration at every speed, full service 0.05 + 12 * 0.1 = 1.25 m/s2
|
||||
struct flat_table {
|
||||
brake_values a0{};
|
||||
brake_values a1{};
|
||||
|
||||
flat_table() {
|
||||
a0.fill(0.05);
|
||||
a1.fill(0.1);
|
||||
}
|
||||
braking_curve::brake_table table() const { return {a0.data(), a1.data(), tablesize, 120.0}; }
|
||||
};
|
||||
|
||||
braking_curve::approach stop_at(double const Speed, double const Distance) {
|
||||
braking_curve::approach approach{};
|
||||
approach.speed = Speed;
|
||||
approach.target = 0.0;
|
||||
approach.distance = Distance;
|
||||
approach.offset = 0.0; // the braking point of a stop, outside of a train, is the stop itself (VelNext * multiplier)
|
||||
approach.speedup_horizon = 30.0 / 3.6;
|
||||
approach.fill_horizon = 30.0 / 3.6;
|
||||
return approach;
|
||||
}
|
||||
|
||||
// the AI in shunting leaves the brake alone for a demand weaker than this (TController::fAccThreshold)
|
||||
auto constexpr shunting_brake_threshold{-0.2};
|
||||
|
||||
struct shunting_run {
|
||||
double speed; // [km/h]
|
||||
double left; // to the stop point [m]
|
||||
bool braked_at_once;
|
||||
};
|
||||
|
||||
// a shunting engine braking to a stop point the way the AI drives it: the brake follows the demand once it is past
|
||||
// the threshold, the engine coasts otherwise. the braking curve gives way near the point to the final approach, at
|
||||
// walking pace (10 km/h, 5 km/h in the last 10 m)
|
||||
shunting_run brake_to_point(double const Speed, double const Distance, zeroed_table const &Table) {
|
||||
auto const table{Table.table()};
|
||||
auto const timestep{0.1};
|
||||
shunting_run run{Speed, Distance, false};
|
||||
run.braked_at_once = braking_curve::demand(stop_at(run.speed, run.left), &table) < shunting_brake_threshold;
|
||||
while ((run.speed > (run.left > 10.0 ? 10.0 : 5.0)) && (run.left > -50.0)) {
|
||||
auto const demand{braking_curve::demand(stop_at(run.speed, run.left), &table)};
|
||||
auto const acceleration{demand < shunting_brake_threshold ? demand : 0.0};
|
||||
run.speed = std::max(0.0, run.speed + 3.6 * acceleration * timestep);
|
||||
run.left -= run.speed / 3.6 * timestep;
|
||||
}
|
||||
return run;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("braking curve: a brake table left at zero isn't known, one worked out is") {
|
||||
CHECK_FALSE(zeroed_table{}.table().known());
|
||||
CHECK(flat_table{}.table().known());
|
||||
}
|
||||
|
||||
TEST_CASE("braking curve: shunting to a point asks for the constant deceleration over the distance left") {
|
||||
zeroed_table const shunting;
|
||||
auto const table{shunting.table()};
|
||||
auto approach{stop_at(33.0, 186.0)};
|
||||
SUBCASE("coasting") { approach.acceleration = 0.0; }
|
||||
SUBCASE("still speeding up") { approach.acceleration = 0.2; }
|
||||
|
||||
auto const demand{braking_curve::demand(approach, &table)};
|
||||
|
||||
CHECK(demand == doctest::Approx(-33.0 * 33.0 / (25.92 * 186.0 + 0.1)));
|
||||
CHECK(demand == doctest::Approx(braking_curve::demand(approach, nullptr)));
|
||||
// enough for the AI to put the brake on
|
||||
CHECK(demand < shunting_brake_threshold);
|
||||
}
|
||||
|
||||
TEST_CASE("braking curve: a shunting engine braking to a point gets down to walking pace short of it") {
|
||||
zeroed_table const shunting;
|
||||
auto speed{0.0};
|
||||
auto distance{0.0};
|
||||
SUBCASE("33 km/h, 186 m") {
|
||||
speed = 33.0;
|
||||
distance = 186.0;
|
||||
}
|
||||
SUBCASE("40 km/h, 186 m") {
|
||||
speed = 40.0;
|
||||
distance = 186.0;
|
||||
}
|
||||
SUBCASE("33 km/h, 120 m") {
|
||||
speed = 33.0;
|
||||
distance = 120.0;
|
||||
}
|
||||
SUBCASE("25 km/h, 60 m") {
|
||||
speed = 25.0;
|
||||
distance = 60.0;
|
||||
}
|
||||
CAPTURE(speed);
|
||||
CAPTURE(distance);
|
||||
|
||||
auto const run{brake_to_point(speed, distance, shunting)};
|
||||
|
||||
CHECK(run.braked_at_once);
|
||||
CHECK(run.left > 0.0);
|
||||
CHECK(run.speed <= 10.0);
|
||||
}
|
||||
|
||||
TEST_CASE("braking curve: a train with a worked out brake table plans along the shape of its brake") {
|
||||
flat_table const train;
|
||||
auto const table{train.table()};
|
||||
auto approach{stop_at(80.0, 600.0)};
|
||||
approach.offset = 20.0;
|
||||
approach.fill_horizon = 6.0;
|
||||
approach.speedup_horizon = 5.0;
|
||||
|
||||
SUBCASE("before the brake is applied, the room it takes to fill is kept") {
|
||||
auto const room{580.0 - 80.0 / 3.6 * 6.0};
|
||||
auto const demand{braking_curve::demand(approach, &table)};
|
||||
CHECK(demand == doctest::Approx(braking_curve::shaped_demand(80.0, 0.0, room + 0.1 / 25.92, 0.0, table)));
|
||||
// with the same deceleration at every speed the shape is the constant deceleration over that room
|
||||
CHECK(demand == doctest::Approx(-(80.0 / 3.6) * (80.0 / 3.6) / (2.0 * room)).epsilon(0.01));
|
||||
}
|
||||
SUBCASE("once the brake is applied, the whole distance is used") {
|
||||
approach.brake_applied = true;
|
||||
auto const demand{braking_curve::demand(approach, &table)};
|
||||
CHECK(demand == doctest::Approx(braking_curve::shaped_demand(80.0, 0.0, 580.0 + 0.1 / 25.92, 0.0, table)));
|
||||
}
|
||||
SUBCASE("still speeding up, it plans from the speed reached once the brake acts") {
|
||||
approach.acceleration = 0.3;
|
||||
auto const reached{80.0 + 3.6 * 5.0 * 0.3};
|
||||
auto const distance{600.0 - (80.0 + reached) * 0.5 / 3.6 * 5.0};
|
||||
auto const demand{braking_curve::demand(approach, &table)};
|
||||
CHECK(demand == doctest::Approx(braking_curve::shaped_demand(reached, 0.0, distance - 20.0 + 0.1 / 25.92, 0.0, table)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("braking curve: a train gets the demand it got before the curve moved out of the AI") {
|
||||
// a table that grips harder as the train slows, on a gradient; the demands were worked out with the
|
||||
// code of TController::adjust_desired_speed_for_braking_curve before the move (origin/master 80fd5aad)
|
||||
brake_values a0{};
|
||||
brake_values a1{};
|
||||
for (auto i{1}; i <= tablesize; ++i) {
|
||||
a0[i] = 0.12 - 0.003 * i;
|
||||
a1[i] = 0.06 - 0.001 * i;
|
||||
}
|
||||
braking_curve::brake_table const table{a0.data(), a1.data(), tablesize, 120.0};
|
||||
REQUIRE(table.full_braking(80.0) == doctest::Approx(0.645));
|
||||
REQUIRE(table.full_braking(10.0) == doctest::Approx(0.825));
|
||||
|
||||
auto approach{stop_at(80.0, 600.0)};
|
||||
approach.offset = 20.0;
|
||||
approach.gravity = 0.1;
|
||||
approach.speedup_horizon = 5.0;
|
||||
approach.fill_horizon = 6.0;
|
||||
auto expected{0.0};
|
||||
SUBCASE("downhill, coasting, the brake still to be applied") { expected = -0.4926656801067293; }
|
||||
SUBCASE("downhill, the brake applied") {
|
||||
approach.brake_applied = true;
|
||||
expected = -0.37739905908238147;
|
||||
}
|
||||
SUBCASE("downhill, still speeding up") {
|
||||
approach.acceleration = 0.3;
|
||||
expected = -0.5330117415264249;
|
||||
}
|
||||
SUBCASE("uphill, down to 40 km/h") {
|
||||
approach = stop_at(100.0, 900.0);
|
||||
approach.target = 40.0;
|
||||
approach.gravity = -0.08;
|
||||
approach.speedup_horizon = 5.0;
|
||||
approach.fill_horizon = 6.0;
|
||||
expected = -0.40930710539221765;
|
||||
}
|
||||
|
||||
CHECK(braking_curve::demand(approach, &table) == doctest::Approx(expected).epsilon(1e-7));
|
||||
}
|
||||
89
tests/unit/test_crashsignals.cpp
Normal file
89
tests/unit/test_crashsignals.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
#include "scratch.h"
|
||||
|
||||
#include "utilities/crashsignals.h"
|
||||
|
||||
// the stack of a crash on Linux (utilities/crashsignals.h): written to the log and to the standard error, after which
|
||||
// the signal ends the process as it always did. the crash happens in a child process, which the test watches
|
||||
#if defined(__linux__)
|
||||
|
||||
#include <array>
|
||||
#include <csignal>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
|
||||
#include <sys/resource.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace {
|
||||
|
||||
std::string read_all(int const File) {
|
||||
std::string text;
|
||||
std::array<char, 4096> buffer{};
|
||||
for (auto count{::read(File, buffer.data(), buffer.size())}; count > 0; count = ::read(File, buffer.data(), buffer.size()))
|
||||
text.append(buffer.data(), static_cast<std::size_t>(count));
|
||||
return text;
|
||||
}
|
||||
|
||||
std::string read_file(std::string const &Path) {
|
||||
std::ifstream file{Path, std::ios::binary};
|
||||
return {std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
|
||||
}
|
||||
|
||||
// the child: the report goes to the pipe in place of the standard error, no core dump is left behind
|
||||
[[noreturn]] void crash_in_child(std::array<int, 2> const &Pipe, std::string const &Log) {
|
||||
// a child stuck before the crash (a lock some other thread held at the fork) ends anyway, and the test with it
|
||||
alarm(10);
|
||||
dup2(Pipe[1], STDERR_FILENO);
|
||||
close(Pipe[0]);
|
||||
close(Pipe[1]);
|
||||
rlimit const nocore{0, 0};
|
||||
setrlimit(RLIMIT_CORE, &nocore);
|
||||
|
||||
crash_signals::install("test version 1.2");
|
||||
crash_signals::set_log_path(Log);
|
||||
raise(SIGSEGV);
|
||||
_exit(0); // not reached: the signal ends the child
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("crash signals: a crash writes its stack to the log and to the standard error, and still ends by its signal") {
|
||||
test::scratch_folder const folder{"crash_signals"};
|
||||
auto const log{folder.write("log.txt", "the last line of the log\n")};
|
||||
|
||||
std::array<int, 2> pipe_ends{};
|
||||
REQUIRE(pipe(pipe_ends.data()) == 0);
|
||||
auto const child{fork()};
|
||||
REQUIRE(child >= 0);
|
||||
if (child == 0)
|
||||
crash_in_child(pipe_ends, log);
|
||||
|
||||
close(pipe_ends[1]);
|
||||
auto const errors{read_all(pipe_ends[0])};
|
||||
close(pipe_ends[0]);
|
||||
int status{0};
|
||||
REQUIRE(waitpid(child, &status, 0) == child);
|
||||
|
||||
CAPTURE(errors);
|
||||
// the process ends as it would without the handler
|
||||
CHECK(WIFSIGNALED(status));
|
||||
CHECK(WTERMSIG(status) == SIGSEGV);
|
||||
// the report on the standard error, which the starter shows
|
||||
CHECK(errors.find("Crash: signal 11 (SIGSEGV) at address 0x") != std::string::npos);
|
||||
CHECK(errors.find("Version: test version 1.2") != std::string::npos);
|
||||
CHECK(errors.find("Stack (addr2line") != std::string::npos);
|
||||
// with the frames of the program that crashed
|
||||
CHECK(errors.find("eu07_unit_tests") != std::string::npos);
|
||||
// and the same at the end of the log, after what it held
|
||||
auto const logtext{read_file(log)};
|
||||
CAPTURE(logtext);
|
||||
CHECK(logtext.starts_with("the last line of the log\n"));
|
||||
CHECK(logtext.find("Crash: signal 11 (SIGSEGV)") != std::string::npos);
|
||||
CHECK(logtext.find("eu07_unit_tests") != std::string::npos);
|
||||
}
|
||||
|
||||
#endif
|
||||
138
tests/unit/test_editor_http.cpp
Normal file
138
tests/unit/test_editor_http.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
#include "editor/editorHttp.hpp"
|
||||
|
||||
#include <asio.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <format>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
// a service on this machine. with no reply it takes the connection and the request and never answers, as geoportal.gov.pl
|
||||
// does now and then
|
||||
class local_server {
|
||||
public:
|
||||
explicit local_server(std::string Reply = {}) :
|
||||
m_reply(std::move(Reply)) {
|
||||
m_acceptor.open(asio::ip::tcp::v4());
|
||||
m_acceptor.bind({asio::ip::address_v4::loopback(), 0});
|
||||
m_acceptor.listen();
|
||||
m_port = m_acceptor.local_endpoint().port();
|
||||
m_thread = std::jthread{[this]() { serve(); }};
|
||||
}
|
||||
~local_server() {
|
||||
m_done = true;
|
||||
// a connection of its own wakes the server waiting for one
|
||||
asio::ip::tcp::socket wake{m_context};
|
||||
asio::error_code ignored;
|
||||
wake.connect({asio::ip::address_v4::loopback(), m_port}, ignored);
|
||||
wake.close(ignored);
|
||||
m_thread.join();
|
||||
}
|
||||
local_server(local_server const &) = delete;
|
||||
local_server &operator=(local_server const &) = delete;
|
||||
|
||||
std::string url() const {
|
||||
return std::format("http://127.0.0.1:{}/wcs?REQUEST=GetCapabilities", m_port);
|
||||
}
|
||||
|
||||
private:
|
||||
void serve() {
|
||||
while (false == m_done) {
|
||||
asio::ip::tcp::socket socket{m_context};
|
||||
asio::error_code error;
|
||||
m_acceptor.accept(socket, error);
|
||||
if (error)
|
||||
return;
|
||||
if (false == m_reply.empty()) {
|
||||
answer(socket);
|
||||
continue;
|
||||
}
|
||||
// the request is left without an answer, the connection open, until the server goes
|
||||
while (false == m_done)
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
}
|
||||
|
||||
// the reply once the whole request is read, then the connection closed
|
||||
void answer(asio::ip::tcp::socket &Socket) const {
|
||||
asio::error_code error;
|
||||
asio::streambuf request;
|
||||
asio::read_until(Socket, request, "\r\n\r\n", error);
|
||||
if (!error)
|
||||
asio::write(Socket, asio::buffer(m_reply), error);
|
||||
Socket.shutdown(asio::ip::tcp::socket::shutdown_both, error);
|
||||
}
|
||||
|
||||
std::string const m_reply;
|
||||
asio::io_context m_context;
|
||||
asio::ip::tcp::acceptor m_acceptor{m_context};
|
||||
unsigned short m_port{0};
|
||||
std::atomic<bool> m_done{false};
|
||||
std::jthread m_thread;
|
||||
};
|
||||
|
||||
// the requests to the local server would go to a proxy named in the environment, which can't reach it
|
||||
bool proxied() {
|
||||
for (char const *variable : {"http_proxy", "HTTP_PROXY", "all_proxy", "ALL_PROXY"}) {
|
||||
if (char const *value{std::getenv(variable)}; value != nullptr && *value != '\0')
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("editor http: a request a service doesn't answer is given up as soon as a stop is asked for") {
|
||||
if (false == editor_http::available() || proxied())
|
||||
return; // a build with no http client, or a proxy in the way
|
||||
local_server server;
|
||||
auto const start{std::chrono::steady_clock::now()};
|
||||
auto const elapsed = [start]() { return std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count(); };
|
||||
editor_http::client client;
|
||||
std::vector<std::uint8_t> data;
|
||||
std::string error;
|
||||
// the stop comes while the client waits for the answer, long before any of its timeouts
|
||||
auto const got{client.get(server.url(), data, error, [&elapsed]() { return elapsed() > 0.3; })};
|
||||
CHECK_FALSE(got);
|
||||
CHECK_FALSE(error.empty());
|
||||
// the receive timeout is 20 s; the transfer ends within a moment of the stop (libcurl looks at it about once a second)
|
||||
CHECK(elapsed() < 5.0);
|
||||
}
|
||||
|
||||
TEST_CASE("editor http: the whole body of a response is read") {
|
||||
if (false == editor_http::available() || proxied())
|
||||
return;
|
||||
std::string body(100000, '\0');
|
||||
for (std::size_t i = 0; i < body.size(); ++i)
|
||||
body[i] = static_cast<char>('a' + i % 26);
|
||||
local_server server{std::format("HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", body.size(), body)};
|
||||
editor_http::client client;
|
||||
std::vector<std::uint8_t> data;
|
||||
std::string error;
|
||||
CHECK(client.get(server.url(), data, error, []() { return false; }));
|
||||
CHECK(error.empty());
|
||||
CHECK(std::string(data.begin(), data.end()) == body);
|
||||
}
|
||||
|
||||
TEST_CASE("editor http: the body of an error is kept with the status in the error") {
|
||||
if (false == editor_http::available() || proxied())
|
||||
return;
|
||||
// the terrain model service tells a place outside of its coverage in the body of an error
|
||||
std::string const body{"<ServiceException>outside requested coverage</ServiceException>"};
|
||||
local_server server{std::format("HTTP/1.1 400 Bad Request\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", body.size(), body)};
|
||||
editor_http::client client;
|
||||
std::vector<std::uint8_t> data;
|
||||
std::string error;
|
||||
CHECK_FALSE(client.get(server.url(), data, error, []() { return false; }));
|
||||
CHECK(error == "HTTP 400");
|
||||
CHECK(std::string(data.begin(), data.end()) == body);
|
||||
}
|
||||
@@ -63,7 +63,8 @@ TEST_CASE("uneven track: the cab keeps what the ride on the track doesn't stand
|
||||
TEST_CASE("uneven track: measures as the Id-14 limits for its value")
|
||||
{
|
||||
// appendix 1, class C at 80 km/h: 18 mm vertically, 17 mm sideways, 16 mm of twist on 5 m, gauge +10/-8 mm, 20 mm of cross level, J = 5.3
|
||||
auto const amplitude{track_uneven::stray_per_mm * 18.0 * 1000.0}; // mm
|
||||
auto const amplitude{track_uneven::stray_up_per_mm * 18.0 * 1000.0}; // mm
|
||||
auto const sideways{track_uneven::stray_side_per_mm * 18.0 * 1000.0}; // mm
|
||||
double const gauge{1.435};
|
||||
double const step{0.5};
|
||||
int const steps{4000};
|
||||
@@ -76,6 +77,7 @@ TEST_CASE("uneven track: measures as the Id-14 limits for its value")
|
||||
double level{0.0};
|
||||
double j{0.0};
|
||||
double away{0.0};
|
||||
double aside{0.0};
|
||||
for (int line = 0; line < lines; ++line)
|
||||
{
|
||||
auto const heading{glm::pi<double>() * (line + 0.37) / lines};
|
||||
@@ -85,6 +87,7 @@ TEST_CASE("uneven track: measures as the Id-14 limits for its value")
|
||||
std::array<std::vector<double>, 2> up;
|
||||
std::array<std::vector<double>, 2> side;
|
||||
std::vector<double> middle;
|
||||
std::vector<double> across_middle;
|
||||
for (int i = 0; i < steps; ++i)
|
||||
{
|
||||
auto const point{start + along * (i * step)};
|
||||
@@ -93,9 +96,11 @@ TEST_CASE("uneven track: measures as the Id-14 limits for its value")
|
||||
auto const sign{rail == 0 ? 1.0 : -1.0};
|
||||
auto const stray{track_uneven::field(point + across * (sign * 0.5 * gauge))};
|
||||
up[rail].push_back((stray.y - sign * 0.5 * track_uneven::level(point, across)) * amplitude);
|
||||
side[rail].push_back(glm::dot(stray, across) * amplitude);
|
||||
side[rail].push_back(glm::dot(stray, across) * sideways);
|
||||
}
|
||||
middle.push_back(track_uneven::field(point).y * amplitude);
|
||||
auto const centre{track_uneven::field(point)};
|
||||
middle.push_back(centre.y * amplitude);
|
||||
across_middle.push_back(glm::dot(centre, across) * sideways);
|
||||
}
|
||||
std::vector<double> verticals;
|
||||
std::vector<double> horizontals;
|
||||
@@ -120,6 +125,7 @@ TEST_CASE("uneven track: measures as the Id-14 limits for its value")
|
||||
wander += peak(gauges) / lines;
|
||||
level += peak(levels) / lines;
|
||||
away += peak(middle) / lines;
|
||||
aside += peak(across_middle) / lines;
|
||||
j += (deviation(verticals) + deviation(horizontals) + deviation(twists) + 0.5 * deviation(gauges)) / 3.5 / lines;
|
||||
}
|
||||
CHECK(vertical == doctest::Approx(18.0).epsilon(0.1));
|
||||
@@ -128,7 +134,11 @@ TEST_CASE("uneven track: measures as the Id-14 limits for its value")
|
||||
CHECK(wander == doctest::Approx(10.0).epsilon(0.2));
|
||||
CHECK(level == doctest::Approx(20.0).epsilon(0.1));
|
||||
CHECK(j == doctest::Approx(5.3).epsilon(0.2));
|
||||
CHECK(away < 40.0); // the long waves go past the 15 mm off the survey marks of appendix 1a
|
||||
// the long waves go past the 15 mm off the survey marks of appendix 1a: most of the height of a track spectrum is in them, the 10 m chord
|
||||
// hardly sees them
|
||||
CHECK(away < 60.0);
|
||||
// and sideways: about 4.5 cm, which a platform edge or the panels of a level crossing laid on the design are off the rails by
|
||||
CHECK(aside < 50.0);
|
||||
}
|
||||
|
||||
TEST_CASE("uneven track: a place strays the same every time")
|
||||
@@ -136,11 +146,11 @@ TEST_CASE("uneven track: a place strays the same every time")
|
||||
// no random numbers: changing these changes how every uneven track in every scenery looks
|
||||
glm::dvec3 const place{1234.5, 0.0, -678.9};
|
||||
auto const stray{track_uneven::field(place)};
|
||||
CHECK(stray.x == doctest::Approx(0.309777548158).epsilon(1e-9));
|
||||
CHECK(stray.y == doctest::Approx(0.311350299745).epsilon(1e-9));
|
||||
CHECK(stray.z == doctest::Approx(-0.328429734019).epsilon(1e-9));
|
||||
CHECK(track_uneven::level(place, {1.0, 0.0, 0.0}) == doctest::Approx(0.53 * -0.178598957983).epsilon(1e-9));
|
||||
CHECK(track_uneven::level(place, {0.0, 0.0, 1.0}) == doctest::Approx(0.53 * -0.868291889902).epsilon(1e-9));
|
||||
CHECK(stray.x == doctest::Approx(0.158385385505).epsilon(1e-9));
|
||||
CHECK(stray.y == doctest::Approx(0.356190053667).epsilon(1e-9));
|
||||
CHECK(stray.z == doctest::Approx(0.200372098150).epsilon(1e-9));
|
||||
CHECK(track_uneven::level(place, {1.0, 0.0, 0.0}) == doctest::Approx(0.35 * -0.178598957983).epsilon(1e-9));
|
||||
CHECK(track_uneven::level(place, {0.0, 0.0, 1.0}) == doctest::Approx(0.35 * -0.868291889902).epsilon(1e-9));
|
||||
}
|
||||
|
||||
TEST_CASE("uneven track: tracks laid towards each other meet at one cant")
|
||||
|
||||
103
tests/unit/test_uphill_start.cpp
Normal file
103
tests/unit/test_uphill_start.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
|
||||
#include "vehicle/uphillstart.h"
|
||||
|
||||
// starting uphill, the train brake is applied further, a step at a time, until the brake of the unpowered vehicles
|
||||
// holds the train without the locomotive brake (eu07/maszyna#396). the numbers are those of an ST43 with 37 empty
|
||||
// wagons on 12.5 per mille: 1040 t, 128.6 kN of grade force, the wagons on G with about 22 s to apply
|
||||
|
||||
using uphill_start::brake_top_up;
|
||||
using uphill_start::top_up;
|
||||
using uphill_start::top_up_action;
|
||||
|
||||
namespace {
|
||||
|
||||
double const grade{128600.0}; // [N]
|
||||
double const application{22.0}; // [s]
|
||||
|
||||
// the train standing with the brake handle at Position, held there long enough for the cylinders to fill, while
|
||||
// each step adds Gain of brake force (as a multiple of the grade force) to the Start it had. runs the top-up until it
|
||||
// lets the locomotive brake off, and returns the handle position it stopped at and the number of steps
|
||||
struct outcome {
|
||||
double position;
|
||||
int steps;
|
||||
};
|
||||
outcome run_top_up(double Start, double Gain, double Position = 1.25) {
|
||||
top_up state;
|
||||
auto force{Start * grade};
|
||||
auto steps{0};
|
||||
for (auto guard{0}; guard < 100; ++guard) {
|
||||
auto const action{brake_top_up(state, force, grade, Position, application, application)};
|
||||
if (action == top_up_action::none) {
|
||||
break;
|
||||
}
|
||||
REQUIRE(action == top_up_action::step);
|
||||
Position += 0.25;
|
||||
force += Gain * grade;
|
||||
++steps;
|
||||
}
|
||||
return {Position, steps};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("uphill start: a brake that holds the train isn't applied further") {
|
||||
top_up state;
|
||||
CHECK(brake_top_up(state, 1.1 * grade, grade, 1.25, application, application) == top_up_action::none);
|
||||
CHECK(state.last_force < 0.0);
|
||||
}
|
||||
|
||||
TEST_CASE("uphill start: each step waits the full application time of the brake") {
|
||||
top_up state;
|
||||
CHECK(brake_top_up(state, 0.9 * grade, grade, 1.25, 0.5 * application, application) == top_up_action::wait);
|
||||
CHECK(brake_top_up(state, 0.9 * grade, grade, 1.25, application, application) == top_up_action::step);
|
||||
// the handle has just moved
|
||||
CHECK(brake_top_up(state, 0.95 * grade, grade, 1.5, 0.0, application) == top_up_action::wait);
|
||||
CHECK(brake_top_up(state, 0.95 * grade, grade, 1.5, application - 0.1, application) == top_up_action::wait);
|
||||
}
|
||||
|
||||
TEST_CASE("uphill start: a light application is topped up until the brake holds the train") {
|
||||
// before Nowy Zagorz on linia_107_objazdy: 0.64 bar in the wagons, 128 kN against 129 kN
|
||||
auto const result{run_top_up(0.995, 0.15)};
|
||||
CHECK(result.steps == 1);
|
||||
CHECK(result.position == doctest::Approx(1.5));
|
||||
}
|
||||
|
||||
TEST_CASE("uphill start: a brake that doesn't get stronger ends the top-up after one more step") {
|
||||
// all the wagon brakes cut out: no step adds anything
|
||||
auto const result{run_top_up(0.0, 0.0)};
|
||||
CHECK(result.steps == 1);
|
||||
CHECK(result.position < uphill_start::top_up_position);
|
||||
}
|
||||
|
||||
TEST_CASE("uphill start: a brake that gets stronger by too little ends the top-up") {
|
||||
// a step adds 1% of the grade force, below the 2% that counts
|
||||
auto const result{run_top_up(0.3, 0.01)};
|
||||
CHECK(result.steps == 1);
|
||||
SUBCASE("and one that gets stronger by enough goes on") {
|
||||
auto const going{run_top_up(0.3, 0.05)};
|
||||
CHECK(going.steps > 1);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("uphill start: the top-up goes no further than full service") {
|
||||
// every step adds enough to go on, but never enough to hold the train
|
||||
auto const result{run_top_up(0.0, 0.03)};
|
||||
CHECK(result.position == doctest::Approx(uphill_start::top_up_position));
|
||||
CHECK(result.steps == 11);
|
||||
top_up state;
|
||||
CHECK(brake_top_up(state, 0.0, grade, uphill_start::top_up_position, application, application) == top_up_action::none);
|
||||
}
|
||||
|
||||
TEST_CASE("uphill start: once ended, the top-up stays ended for the start") {
|
||||
top_up state;
|
||||
REQUIRE(brake_top_up(state, 0.2 * grade, grade, 1.25, application, application) == top_up_action::step);
|
||||
REQUIRE(brake_top_up(state, 0.2 * grade, grade, 1.5, application, application) == top_up_action::none);
|
||||
CHECK(state.done);
|
||||
// the brake gets stronger later all the same: the start has gone on without the top-up
|
||||
CHECK(brake_top_up(state, 0.5 * grade, grade, 1.5, application, application) == top_up_action::none);
|
||||
// a new start begins with a new top-up
|
||||
state = {};
|
||||
CHECK(brake_top_up(state, 0.5 * grade, grade, 1.5, application, application) == top_up_action::step);
|
||||
}
|
||||
@@ -2,9 +2,15 @@
|
||||
#include "doctest.h"
|
||||
#include "scratch.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
#include "utilities/Globals.h"
|
||||
#include "utilities/utilities.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <type_traits>
|
||||
|
||||
// the helpers of utilities/utilities.cpp and utilities.h as they behave now: the numbers and texts they give, their
|
||||
// oddities included. the ones which look like mistakes are listed in COVERAGE.md. the helpers which read from a
|
||||
// parser (LoadPoint, the sets, the map) are in test_parser.cpp
|
||||
@@ -112,16 +118,31 @@ TEST_CASE("utilities: clearing a flag needs all of its bits set") {
|
||||
TEST_CASE("utilities: random whole numbers are within the bounds, both included, given in any order") {
|
||||
std::set<int> seen;
|
||||
for (auto draw{0}; draw < 200; ++draw) {
|
||||
auto const value{Random(3, 7)};
|
||||
auto const value{RandomInt(3, 7)};
|
||||
CHECK(value >= 3);
|
||||
CHECK(value <= 7);
|
||||
seen.insert(value);
|
||||
auto const swapped{Random(7, 3)};
|
||||
auto const swapped{RandomInt(7, 3)};
|
||||
CHECK(swapped >= 3);
|
||||
CHECK(swapped <= 7);
|
||||
}
|
||||
CHECK(seen.size() == 5);
|
||||
CHECK(Random(5, 5) == 5);
|
||||
CHECK(RandomInt(5, 5) == 5);
|
||||
}
|
||||
|
||||
TEST_CASE("utilities: whole-number bounds still give a real number in between") {
|
||||
// the wind, the wheel flats, the truck hunting and the tachometer draw with whole-number bounds
|
||||
static_assert(std::is_same_v<decltype(Random(0, 4)), double>);
|
||||
auto fractions{0};
|
||||
for (auto draw{0}; draw < 50; ++draw) {
|
||||
auto const value{Random(0, 4)};
|
||||
CHECK(value >= 0.0);
|
||||
CHECK(value <= 4.0);
|
||||
if (value != std::floor(value)) {
|
||||
++fractions;
|
||||
}
|
||||
}
|
||||
CHECK(fractions > 0);
|
||||
}
|
||||
|
||||
TEST_CASE("utilities: random numbers are within the bounds given in any order") {
|
||||
@@ -154,17 +175,37 @@ TEST_CASE("utilities: random numbers are within the bounds given in any order")
|
||||
}
|
||||
|
||||
TEST_CASE("utilities: the same seed gives the same random numbers") {
|
||||
std::vector<int> first;
|
||||
std::vector<double> first;
|
||||
Global.random_engine.seed(42);
|
||||
for (auto draw{0}; draw < 5; ++draw) {
|
||||
first.push_back(Random(0, 1000000));
|
||||
first.push_back(Random(0.0, 1000000.0));
|
||||
}
|
||||
std::vector<int> second;
|
||||
std::vector<double> second;
|
||||
Global.random_engine.seed(42);
|
||||
for (auto draw{0}; draw < 5; ++draw) {
|
||||
second.push_back(Random(0, 1000000));
|
||||
second.push_back(Random(0.0, 1000000.0));
|
||||
}
|
||||
CHECK(first == second);
|
||||
std::vector<int> localfirst;
|
||||
Global.local_random_engine.seed(42);
|
||||
for (auto draw{0}; draw < 5; ++draw) {
|
||||
localfirst.push_back(RandomInt(0, 1000000));
|
||||
}
|
||||
std::vector<int> localsecond;
|
||||
Global.local_random_engine.seed(42);
|
||||
for (auto draw{0}; draw < 5; ++draw) {
|
||||
localsecond.push_back(RandomInt(0, 1000000));
|
||||
}
|
||||
CHECK(localfirst == localsecond);
|
||||
}
|
||||
|
||||
TEST_CASE("utilities: the whole numbers are drawn from the local engine, not from that of the run's seed") {
|
||||
// the trivia of the loading screen is drawn before a network client gets the seed of the session
|
||||
Global.random_engine.seed(5);
|
||||
auto const first{Random()};
|
||||
Global.random_engine.seed(5);
|
||||
RandomInt(0, 9);
|
||||
CHECK(Random() == first);
|
||||
}
|
||||
|
||||
TEST_CASE("utilities: the local random numbers have an engine of their own") {
|
||||
@@ -681,12 +722,30 @@ TEST_CASE("utilities: the sign of a number, and a number rounded to a whole one"
|
||||
CHECK(Round(-2.5) == -2);
|
||||
}
|
||||
|
||||
TEST_CASE("utilities: a value is kept within bounds, given in any order") {
|
||||
TEST_CASE("utilities: a value is kept within bounds; inverted bounds give the upper one, as the old clamp did") {
|
||||
CHECK(safe_clamp(5, 0, 3) == 3);
|
||||
CHECK(safe_clamp(5, 3, 0) == 3);
|
||||
CHECK(safe_clamp(-1, 3, 0) == 0);
|
||||
CHECK(safe_clamp(2.0, 3.0, 1.0) == 2.0);
|
||||
CHECK(safe_clamp(-1, 0, 3) == 0);
|
||||
CHECK(safe_clamp(2, 0, 3) == 2);
|
||||
CHECK(safe_clamp(2.0, 2.0, 2.0) == 2.0);
|
||||
// inverted bounds: always the upper bound, whatever the value
|
||||
CHECK(safe_clamp(5, 3, 0) == 0);
|
||||
CHECK(safe_clamp(-1, 3, 0) == 0);
|
||||
CHECK(safe_clamp(2.0, 3.0, 1.0) == 1.0);
|
||||
// NaN as the old clamp had it: a NaN value stays NaN, a NaN bound doesn't clamp
|
||||
auto const notanumber{std::numeric_limits<double>::quiet_NaN()};
|
||||
CHECK(std::isnan(safe_clamp(notanumber, 0.0, 1.0)));
|
||||
CHECK(safe_clamp(2.0, 0.0, notanumber) == 2.0);
|
||||
CHECK(safe_clamp(-1.0, notanumber, 1.0) == -1.0);
|
||||
// what the helper gives for arguments in the order and of the signs of two calls (these checks don't call
|
||||
// them): the AI braking test, whose demand is the stronger of the two (negative) bounds, as up to 26.05.1
|
||||
double const threshold{-0.45};
|
||||
CHECK(safe_clamp(0.3, threshold * 1.01, threshold * 1.21) == doctest::Approx(threshold * 1.21));
|
||||
CHECK(safe_clamp(-0.6, threshold * 1.01, threshold * 1.21) == doctest::Approx(threshold * 1.21));
|
||||
// and the bump at a rail joint, 0 in place of the value as up to 26.05.1: the figure is capped from above
|
||||
// only, and above 68% of the top speed it is negative
|
||||
CHECK(safe_clamp(0.0, 160.0, 160.0 - (150.0 + 160.0 * 0.32)) == doctest::Approx(-41.2));
|
||||
CHECK(safe_clamp(0.0, 4.0, -0.5) == -0.5);
|
||||
CHECK(safe_clamp(0.0, 4.0, 5.0) == 4.0);
|
||||
}
|
||||
|
||||
TEST_CASE("utilities: a value is wrapped into a range, as an angle into the full circle") {
|
||||
|
||||
Reference in New Issue
Block a user