Guard optional environment values explicitly in tests
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
@@ -18,6 +19,14 @@ void check(bool ok, std::source_location at = std::source_location::current()) {
|
||||
throw std::runtime_error("environment view line " + std::to_string(at.line()));
|
||||
}
|
||||
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
||||
|
||||
const EnvironmentQuantity&
|
||||
required_field(const std::optional<EnvironmentQuantity>& field,
|
||||
std::source_location at = std::source_location::current()) {
|
||||
if (!field.has_value())
|
||||
throw std::runtime_error("missing environment field at line " + std::to_string(at.line()));
|
||||
return *field;
|
||||
}
|
||||
template <typename T, typename E> bool refused(const std::expected<T, E>& r, E e) {
|
||||
return !r && r.error() == e;
|
||||
}
|
||||
@@ -74,11 +83,13 @@ void values_and_order() {
|
||||
CHECK(s.measure == HabitatMeasure{{200}, {600}});
|
||||
CHECK(s.terrain.material() == MaterialId{1} && s.terrain.fill() == fill_scale);
|
||||
CHECK(!s.fields[1] && !s.fields[3]); // missing requested N; unrequested organic C
|
||||
CHECK(std::get<EnvironmentMass>(*s.fields[2]) == EnvironmentMass{{0}, {0, 1}});
|
||||
CHECK(std::get<evo::accounting::Picojoules>(*s.fields[7]).value == 72);
|
||||
CHECK(std::get<EnvironmentMass>(required_field(s.fields[2])) ==
|
||||
EnvironmentMass{{0}, {0, 1}});
|
||||
CHECK(std::get<evo::accounting::Picojoules>(required_field(s.fields[7])).value == 72);
|
||||
CHECK(s.chunk_revision == f.terrain->view(s.chunk)->revision());
|
||||
}
|
||||
CHECK(std::get<EnvironmentMass>(*q->samples()[0].fields[0]) == EnvironmentMass{{6}, {1, 100}});
|
||||
CHECK(std::get<EnvironmentMass>(required_field(q->samples()[0].fields[0])) ==
|
||||
EnvironmentMass{{6}, {1, 100}});
|
||||
CHECK(q->samples()[0].moisture_status == MoistureStatus::available);
|
||||
CHECK(q->samples()[0].moisture == WaterSaturation{3, 50});
|
||||
CHECK(q->samples()[1].habitat.substrate == MaterialId{2}); // no silent substrate reconciliation
|
||||
@@ -217,9 +228,11 @@ void maximum_footprint() {
|
||||
for (std::size_t k = 0; k < field_count; ++k) {
|
||||
CHECK(s.fields[k]);
|
||||
if (k < 7)
|
||||
CHECK(std::get<EnvironmentMass>(*s.fields[k]) == EnvironmentMass{{4}, {4, 1}});
|
||||
CHECK(std::get<EnvironmentMass>(required_field(s.fields[k])) ==
|
||||
EnvironmentMass{{4}, {4, 1}});
|
||||
else
|
||||
CHECK(std::get<evo::accounting::Picojoules>(*s.fields[k]).value == 4);
|
||||
CHECK(std::get<evo::accounting::Picojoules>(required_field(s.fields[k])).value ==
|
||||
4);
|
||||
}
|
||||
CHECK(s.moisture == WaterSaturation{1, 25});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user