Complete M6.1 environmental units and coupling contract
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
#include "evo/world/environment_units.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
namespace {
|
||||
using namespace evo::world;
|
||||
using evo::accounting::Femtograms;
|
||||
using evo::accounting::Picojoules;
|
||||
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
|
||||
static_assert(!std::is_convertible_v<CubicMicrometres, Femtograms>);
|
||||
static_assert(!std::is_convertible_v<MassConcentration, Femtograms>);
|
||||
static_assert(!std::is_convertible_v<WaterSaturation, Femtograms>);
|
||||
static_assert(!std::is_convertible_v<Femtograms, Picojoules>);
|
||||
void check(bool ok, std::source_location at = std::source_location::current()) {
|
||||
if (!ok)
|
||||
throw std::runtime_error("environment units line " + std::to_string(at.line()));
|
||||
}
|
||||
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
||||
template <typename T>
|
||||
bool refused(const std::expected<T, EnvironmentUnitError>& value, EnvironmentUnitError error) {
|
||||
return !value && value.error() == error;
|
||||
}
|
||||
void geometry_and_ratios() {
|
||||
CHECK(measure_habitat({{0}, {0}, {0}, {1000}, {2000}, {10}}) ==
|
||||
HabitatMeasure{{2'000'000}, {20'000'000}});
|
||||
CHECK(measure_habitat({{0}, {0}, {0}, {1'000'000}, {1'000'000}, {1'000'000}}) ==
|
||||
HabitatMeasure{{1'000'000'000'000}, {habitat_volume_limit_um3}});
|
||||
CHECK(measure_habitat({{999999}, {999999}, {999999}, {1}, {1}, {1}}) ==
|
||||
HabitatMeasure{{1}, {1}});
|
||||
for (std::size_t axis = 0; axis < 3; ++axis) {
|
||||
for (const auto invalid : {std::uint64_t{0}, std::uint64_t{1'000'001}, maximum}) {
|
||||
HabitatBox box{{0}, {0}, {0}, {1}, {1}, {1}};
|
||||
auto* extent = axis == 0 ? &box.width : (axis == 1 ? &box.height : &box.depth);
|
||||
extent->value = invalid;
|
||||
CHECK(refused(measure_habitat(box), EnvironmentUnitError::invalid_extent));
|
||||
}
|
||||
for (const auto invalid : {std::uint64_t{1'000'000}, maximum}) {
|
||||
HabitatBox box{{0}, {0}, {0}, {1}, {1}, {1}};
|
||||
auto* origin = axis == 0 ? &box.x : (axis == 1 ? &box.y : &box.z);
|
||||
origin->value = invalid;
|
||||
CHECK(refused(measure_habitat(box), EnvironmentUnitError::outside_voxel));
|
||||
}
|
||||
}
|
||||
CHECK(refused(measure_habitat({{999999}, {0}, {0}, {2}, {1}, {1}}),
|
||||
EnvironmentUnitError::outside_voxel));
|
||||
CHECK(mass_concentration({200}, {20'000'000}) == MassConcentration{1, 100000});
|
||||
CHECK(mass_concentration({0}, {1}) == MassConcentration{0, 1});
|
||||
CHECK(mass_concentration({maximum}, {1}) == MassConcentration{maximum, 1});
|
||||
CHECK(refused(mass_concentration({1}, {0}), EnvironmentUnitError::invalid_volume));
|
||||
CHECK(refused(mass_concentration({1}, {maximum}), EnvironmentUnitError::invalid_volume));
|
||||
CHECK(water_saturation({5'000'000}, {20'000'000}) == WaterSaturation{1, 4});
|
||||
CHECK(water_saturation({0}, {maximum}) == WaterSaturation{0, 1});
|
||||
CHECK(water_saturation({maximum}, {maximum}) == WaterSaturation{1, 1});
|
||||
CHECK(refused(water_saturation({0}, {0}), EnvironmentUnitError::zero_capacity));
|
||||
CHECK(refused(water_saturation({maximum}, {maximum - 1}), EnvironmentUnitError::excess_water));
|
||||
}
|
||||
void interval_accounting() {
|
||||
CHECK(integrate_energy({5, 3}, 6, 0) == EnergyIntegral{{10}, 0});
|
||||
CHECK(integrate_energy({0, 3}, maximum, 2) == EnergyIntegral{{0}, 2});
|
||||
CHECK(integrate_energy({maximum, 3}, 0, 2) == EnergyIntegral{{0}, 2});
|
||||
CHECK(integrate_energy({maximum, 1}, 1, 0) == EnergyIntegral{{maximum}, 0});
|
||||
CHECK(refused(integrate_energy({1, 0}, 0, 0), EnvironmentUnitError::invalid_rate));
|
||||
CHECK(refused(integrate_energy({1, 3}, 0, 3), EnvironmentUnitError::invalid_remainder));
|
||||
CHECK(refused(integrate_energy({maximum, 2}, 2, 0), EnvironmentUnitError::overflow));
|
||||
CHECK(refused(integrate_energy({maximum, 2}, 1, 1), EnvironmentUnitError::overflow));
|
||||
CHECK(integrate_energy({maximum - 1, 2}, 1, 1) == EnergyIntegral{{maximum / 2}, 1});
|
||||
// Independent repeated-addition oracle: count whole units at each denominator crossing.
|
||||
for (std::uint64_t numerator = 0; numerator <= 17; ++numerator) {
|
||||
for (std::uint64_t denominator = 1; denominator <= 19; ++denominator) {
|
||||
for (std::uint64_t initial = 0; initial < denominator; ++initial) {
|
||||
std::uint64_t whole{}, residual = initial, split_whole{}, split_residual = initial;
|
||||
for (std::uint64_t tick = 1; tick <= 23; ++tick) {
|
||||
residual += numerator;
|
||||
while (residual >= denominator) {
|
||||
residual -= denominator;
|
||||
++whole;
|
||||
}
|
||||
const auto part = integrate_energy({numerator, denominator}, 1, split_residual);
|
||||
CHECK(part);
|
||||
split_whole += part->amount.value;
|
||||
split_residual = part->remainder;
|
||||
CHECK(integrate_energy({numerator, denominator}, tick, initial) ==
|
||||
EnergyIntegral{{whole}, residual});
|
||||
CHECK(split_whole == whole && split_residual == residual);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void closed_box_example() {
|
||||
using namespace evo::accounting;
|
||||
const Inventory opening[]{{{100}, {{{0}, {200}, {0}, {20'000'000}}}}, {}, {}, {{7}, {}}};
|
||||
auto ledger = Ledger::create(opening);
|
||||
CHECK(ledger);
|
||||
const auto totals = ledger->conserved_totals();
|
||||
const Transfer capture[]{{{1}, {2}, {{30}, {}}}, {{1}, {3}, {{20}, {}}}};
|
||||
CHECK(ledger->transfer(1, capture));
|
||||
const Transfer uptake[]{{{1}, {2}, {{0}, {{{0}, {50}, {0}, {5'000'000}}}}}};
|
||||
CHECK(ledger->transfer(2, uptake));
|
||||
CHECK(ledger->conserved_totals() == totals);
|
||||
CHECK(ledger->balances()[0].energy.value == 50);
|
||||
CHECK(mass_concentration(ledger->balances()[0].matter[1], {20'000'000}) ==
|
||||
MassConcentration{3, 400000});
|
||||
// Refusal preserves every inventory: a computed proposal cannot mint missing energy.
|
||||
const Transfer overdraft[]{{{1}, {2}, {{51}, {}}}};
|
||||
const std::array before{ledger->balances()[0], ledger->balances()[1], ledger->balances()[2],
|
||||
ledger->balances()[3]};
|
||||
CHECK(!ledger->transfer(3, overdraft));
|
||||
CHECK(ledger->conserved_totals() == totals && ledger->balances()[0].energy.value == 50);
|
||||
for (std::size_t i = 0; i < before.size(); ++i)
|
||||
CHECK(ledger->balances()[i] == before[i]);
|
||||
// Reservoir 4 is outside the selected habitat boundary, but inside the ledger.
|
||||
// This models a recorded import without adding a mint operation to the ledger.
|
||||
const Transfer boundary_import[]{{{4}, {1}, {{7}, {}}}};
|
||||
CHECK(ledger->transfer(3, boundary_import));
|
||||
Inventory independent_sum{};
|
||||
for (const auto& inventory : ledger->balances().first(3)) {
|
||||
independent_sum.energy.value += inventory.energy.value;
|
||||
for (std::size_t channel = 0; channel < independent_sum.matter.size(); ++channel)
|
||||
independent_sum.matter[channel].value += inventory.matter[channel].value;
|
||||
}
|
||||
CHECK(independent_sum == Inventory{{107}, {{{0}, {200}, {0}, {20'000'000}}}});
|
||||
CHECK(ledger->balances()[3] == Inventory{});
|
||||
}
|
||||
} // namespace
|
||||
int main() try {
|
||||
geometry_and_ratios();
|
||||
interval_accounting();
|
||||
closed_box_example();
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::fprintf(stderr, "%s\n", e.what());
|
||||
return 1;
|
||||
} catch (...) {
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user