Foundation / checks (deploy) (push) Successful in 3m27s
Foundation / checks (ecs) (push) Successful in 1m51s
Foundation / checks (client) (push) Successful in 12m31s
Foundation / checks (network) (push) Successful in 13m30s
Foundation / checks (tsan) (push) Canceled after 8m42s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 27m37s
Foundation / checks (postgres) (push) Canceled after 15m9s
Rates are stored per second in their integer base units and converted at the tick boundary with no carried state: costs round up, capacities down (maintainer decision), error below one unit per tick. per_second_from_per_tick is the migration's exact inverse (x20 at 50 ms) and refuses inexact values. New rate-contracts test. No biology uses it yet; genome v2, development v2 and process fields follow. Refresh the dev session window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
92 lines
3.8 KiB
C++
92 lines
3.8 KiB
C++
#include "evo/accounting/rates.hpp"
|
|
|
|
#include <cstdint>
|
|
#include <iostream>
|
|
#include <limits>
|
|
#include <stdexcept>
|
|
|
|
namespace {
|
|
using namespace evo::accounting;
|
|
|
|
void check(bool condition) {
|
|
if (!condition)
|
|
throw std::runtime_error("rate contract failed");
|
|
}
|
|
#define CHECK(expression) check(static_cast<bool>(expression))
|
|
|
|
constexpr std::uint64_t tick_50ms = 50'000;
|
|
constexpr auto u64_max = std::numeric_limits<std::uint64_t>::max();
|
|
|
|
bool is(const std::expected<std::uint64_t, RateError>& r, std::uint64_t value) {
|
|
return r.has_value() && *r == value;
|
|
}
|
|
bool fails(const std::expected<std::uint64_t, RateError>& r, RateError e) {
|
|
return !r.has_value() && r.error() == e;
|
|
}
|
|
|
|
// At the ratified 50 ms tick, every per-tick value migrates exactly (x20) and converts
|
|
// back unchanged under either rounding: migration preserves behaviour.
|
|
void migration_is_exact_at_50ms() {
|
|
for (const std::uint64_t v :
|
|
{std::uint64_t{0}, std::uint64_t{1}, std::uint64_t{7}, std::uint64_t{100'000},
|
|
std::uint64_t{1'000'000'000}, u64_max / 1'000'000}) {
|
|
const auto per_second = per_second_from_per_tick(v, tick_50ms);
|
|
CHECK(is(per_second, v * 20));
|
|
CHECK(exact_per_tick(*per_second, tick_50ms));
|
|
CHECK(is(per_tick(*per_second, tick_50ms, Rounding::down), v));
|
|
CHECK(is(per_tick(*per_second, tick_50ms, Rounding::up), v));
|
|
}
|
|
}
|
|
|
|
// A tick that does not divide the rate rounds by direction, by less than one unit.
|
|
void directional_rounding() {
|
|
// 10 per second at 1/3 s: 3.33 per tick.
|
|
CHECK(!exact_per_tick(10, 333'333));
|
|
CHECK(is(per_tick(10, 333'333, Rounding::down), 3));
|
|
CHECK(is(per_tick(10, 333'333, Rounding::up), 4));
|
|
// Below one unit per tick: a capacity rounds to nothing, a cost to one unit.
|
|
CHECK(is(per_tick(1, tick_50ms, Rounding::down), 0));
|
|
CHECK(is(per_tick(1, tick_50ms, Rounding::up), 1));
|
|
// Whole seconds and the fraction combine: 1'000'001 per second over 1 s and 0.5 s.
|
|
CHECK(is(per_tick(1'000'001, 1'000'000, Rounding::down), 1'000'001));
|
|
CHECK(is(per_tick(1'000'001, 500'000, Rounding::down), 500'000));
|
|
CHECK(is(per_tick(1'000'001, 500'000, Rounding::up), 500'001));
|
|
// The two directions bracket the exact amount, one unit apart.
|
|
const std::uint64_t rate = 123'457, tick = 16'667;
|
|
const auto down = *per_tick(rate, tick, Rounding::down);
|
|
const auto up = *per_tick(rate, tick, Rounding::up);
|
|
const auto exact_millionths = rate * tick;
|
|
CHECK(down * 1'000'000 < exact_millionths && exact_millionths < up * 1'000'000);
|
|
CHECK(up - down == 1);
|
|
}
|
|
|
|
void bounds() {
|
|
CHECK(fails(per_tick(1, 0, Rounding::down), RateError::invalid_tick));
|
|
CHECK(fails(per_tick(1, max_tick_microseconds + 1, Rounding::up), RateError::invalid_tick));
|
|
CHECK(fails(per_second_from_per_tick(1, 0), RateError::invalid_tick));
|
|
CHECK(!exact_per_tick(0, 0));
|
|
// A one-second tick passes the rate through; the largest rate still converts.
|
|
CHECK(is(per_tick(u64_max, max_tick_microseconds, Rounding::down), u64_max));
|
|
CHECK(is(per_tick(u64_max, max_tick_microseconds, Rounding::up), u64_max));
|
|
CHECK(is(per_tick(u64_max, tick_50ms, Rounding::down), u64_max / 20));
|
|
CHECK(is(per_tick(u64_max, tick_50ms, Rounding::up), u64_max / 20 + 1));
|
|
// The migration refuses what it cannot represent rather than rounding it.
|
|
CHECK(fails(per_second_from_per_tick(u64_max, tick_50ms), RateError::overflow));
|
|
CHECK(fails(per_second_from_per_tick(1, 333'333), RateError::inexact));
|
|
CHECK(is(per_second_from_per_tick(3, 3), 1'000'000));
|
|
}
|
|
} // namespace
|
|
|
|
int main() {
|
|
try {
|
|
migration_is_exact_at_50ms();
|
|
directional_rounding();
|
|
bounds();
|
|
std::cout << "rate contracts passed\n";
|
|
return 0;
|
|
} catch (const std::exception& e) {
|
|
std::cerr << e.what() << '\n';
|
|
return 1;
|
|
}
|
|
}
|