Guard optional environment values explicitly in tests
This commit is contained in:
@@ -1876,3 +1876,18 @@ Linux/UBSan/TSan or cluster result is claimed.
|
||||
Initial native static analysis flagged an unspecified enum base and integer widening
|
||||
in a size constant; both corrected before the passing analysis runs. No disk/host-loss
|
||||
recovery, biological maintenance, reproduction or player/offline progression added.
|
||||
|
||||
## Environment-view optional guards - 2026-09-27
|
||||
|
||||
The supplied Linux clang-tidy-19 log rejected five optional field accesses in
|
||||
`tests/environment_views_tests.cpp` with bugprone-unchecked-optional-access. Three
|
||||
had no presence check; two relied on CHECK, whose throwing helper is not recognized
|
||||
as a control-flow guard by this analysis. All five now use required_field, which
|
||||
explicitly throws on absence before returning a reference. The rule remains enabled;
|
||||
production code, formats, expected values and NDEBUG behavior are unchanged.
|
||||
|
||||
Executed native analysis, Release and ASan builds plus the targeted
|
||||
`environment-views-contracts` CTest in each preset; all passed. Release format check
|
||||
and git diff --check passed. Full suites were not repeated for this test-only fix.
|
||||
Linux clang-tidy/libstdc++ confirmation remains with the next Gitea run; native
|
||||
analysis is not claimed as a reproduction of the Linux diagnostics.
|
||||
|
||||
@@ -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