// Characterizes annual-budget parity and the pure proportional monthly operating allocator. #include "sc2k/sim/budget_categories.hpp" #include "sc2k/sim/operating_budget.hpp" #include "test_harness.hpp" #include #include #include #include using namespace sc2k; static void check_monthly_matches_annual(int record, std::int64_t rate, std::int64_t value) { std::int64_t carry = 0; std::int64_t monthlyCash = 0; for (int month = 0; month < 12; ++month) { const sim::MonthlyCashConversion converted = sim::convert_monthly_budget_cash(carry, rate, value, kBudgetDivisor[record]); CHECK(converted); carry = converted.remainderUnits; monthlyCash += converted.cashDelta; } const std::int64_t annualUnits = 12 * rate * value; const std::int64_t annualCash = annualUnits / (12 * kBudgetDivisor[record]); std::array legacyRecords{}; legacyRecords[static_cast(record)].revenue = static_cast(annualUnits); long legacyFunds = 0; apply_year_end(legacyRecords, legacyFunds); const std::int64_t signedAnnualUnits = kBudgetDivisor[record] < 0 ? -annualUnits : annualUnits; const std::int64_t denominator = 12 * (kBudgetDivisor[record] < 0 ? -kBudgetDivisor[record] : kBudgetDivisor[record]); CHECK(monthlyCash == annualCash); CHECK(monthlyCash == legacyFunds); CHECK(carry == signedAnnualUnits - annualCash * denominator); } static void exhaustive_small_proportional_allocations() { for (std::int64_t first = 0; first <= 8; ++first) for (std::int64_t second = 0; second <= 8; ++second) for (std::int64_t third = 0; third <= 8; ++third) { const std::int64_t requestedTotal = first + second + third; std::array requested{}; requested[5] = first; requested[6] = second; requested[7] = third; for (std::int64_t available = 0; available <= requestedTotal + 2; ++available) { const sim::OperatingAllocation allocation = sim::allocate_operating_cash(requested, available); CHECK(allocation); if (!allocation) continue; const std::int64_t expectedPaid = std::min(available, requestedTotal); CHECK(allocation.requestedTotal == requestedTotal); CHECK(allocation.paidTotal == expectedPaid); CHECK(allocation.paidByRecord[5] + allocation.paidByRecord[6] + allocation.paidByRecord[7] == expectedPaid); CHECK(allocation.paidByRecord[5] <= first); CHECK(allocation.paidByRecord[6] <= second); CHECK(allocation.paidByRecord[7] <= third); const std::int32_t expectedCoverage = requestedTotal == 0 ? sim::kFullOperatingCoverageBasisPoints : static_cast( expectedPaid * sim::kFullOperatingCoverageBasisPoints / requestedTotal); CHECK(allocation.coverageBasisPoints == expectedCoverage); if (requestedTotal == 0 || expectedPaid == requestedTotal) continue; for (int record = 5; record <= 7; ++record) { const std::int64_t exactNumerator = requested[static_cast(record)] * expectedPaid; const std::int64_t floorShare = exactNumerator / requestedTotal; CHECK(allocation.paidByRecord[static_cast( record)] == floorShare || allocation.paidByRecord[static_cast( record)] == floorShare + 1); } } } } int main() { // Revenue, service expense, infrastructure expense, and a net-negative ordinance record all retain // the legacy annual integer result when streamed through twelve monthly boundaries. check_monthly_matches_annual(0, 7, 100); check_monthly_matches_annual(5, 100, 1); check_monthly_matches_annual(10, 83, 12345); check_monthly_matches_annual(3, 1, -437); exhaustive_small_proportional_allocations(); { std::array requested{}; requested[5] = 100; requested[6] = 60; requested[7] = 40; const sim::OperatingAllocation allocation = sim::allocate_operating_cash(requested, 101); CHECK(allocation); CHECK(allocation.requestedTotal == 200); CHECK(allocation.paidTotal == 101); CHECK(allocation.coverageBasisPoints == 5050); CHECK(allocation.paidByRecord[5] == 51); CHECK(allocation.paidByRecord[6] == 30); CHECK(allocation.paidByRecord[7] == 20); CHECK(allocation.paidByRecord[5] + allocation.paidByRecord[6] + allocation.paidByRecord[7] == allocation.paidTotal); } // Equal fractional remainders are resolved by stable budget-record order. { std::array requested{}; requested[5] = 50; requested[6] = 50; const sim::OperatingAllocation allocation = sim::allocate_operating_cash(requested, 1); CHECK(allocation); CHECK(allocation.paidByRecord[5] == 1); CHECK(allocation.paidByRecord[6] == 0); CHECK(allocation.coverageBasisPoints == 100); } { std::array none{}; const sim::OperatingAllocation allocation = sim::allocate_operating_cash(none, 0); CHECK(allocation); CHECK(allocation.coverageBasisPoints == 10000); CHECK(allocation.paidTotal == 0); } { std::array requested{}; requested[5] = 10; const sim::OperatingAllocation shutdown = sim::allocate_operating_cash(requested, 0); CHECK(shutdown); CHECK(shutdown.coverageBasisPoints == 0); CHECK(sim::classify_operating_funding(0) == sim::OperatingFundingBand::Shutdown); CHECK(sim::classify_operating_funding(4999) == sim::OperatingFundingBand::Critical); CHECK(sim::classify_operating_funding(5000) == sim::OperatingFundingBand::Rationed); CHECK(sim::classify_operating_funding(9999) == sim::OperatingFundingBand::Rationed); CHECK(sim::classify_operating_funding(10000) == sim::OperatingFundingBand::Funded); } { std::array invalid{}; invalid[5] = -1; CHECK(!sim::allocate_operating_cash(invalid, 1)); CHECK(!sim::allocate_operating_cash({}, -1)); invalid = {}; invalid[5] = std::numeric_limits::max(); invalid[6] = 1; CHECK(!sim::allocate_operating_cash(invalid, 1)); CHECK(!sim::convert_monthly_budget_cash(0, 1, 1, 0)); CHECK(!sim::convert_monthly_budget_cash(0, std::numeric_limits::max(), 2, 1)); } // At full coverage the effectiveness seam is an identity over every operating appropriation. { std::array planned{}; for (int record = 5; record < 16; ++record) planned[record] = 100 - record; const sim::OperatingFundingSnapshot funding = sim::make_operating_funding_snapshot( [&](int record) { return planned[static_cast(record)]; }, 10000); CHECK(funding.band == sim::OperatingFundingBand::Funded); for (int record = 5; record < 16; ++record) CHECK(funding.rate(record) == planned[static_cast(record)]); const sim::OperatingFundingSnapshot rationed = sim::make_operating_funding_snapshot( [&](int record) { return planned[static_cast(record)]; }, 5000); CHECK(rationed.band == sim::OperatingFundingBand::Rationed); CHECK(rationed.rate(5) == planned[5] / 2); CHECK(rationed.funding_basis_points(5) == planned[5] * 50); planned[5] = 150; const sim::OperatingFundingSnapshot overfunded = sim::make_operating_funding_snapshot( [&](int record) { return planned[static_cast(record)]; }, 10000); CHECK(overfunded.rate(5) == 150); CHECK(overfunded.funding_basis_points(5) == 15000); CHECK(overfunded.efficiency_basis_points(5) == 11852); const sim::OperatingFundingSnapshot fundedWater = sim::make_operating_funding_snapshot( [&](int record) { return planned[static_cast(record)]; }, 5000, 150); CHECK(fundedWater.funding_basis_points( sim::kWaterOperationsBudgetRecord) == 7500); CHECK(fundedWater.efficiency_basis_points( sim::kWaterOperationsBudgetRecord) == 7500); } if (failures == 0) std::printf("operating_budget: all checks passed\n"); else std::printf("operating_budget: %d FAILED\n", failures); return failures ? 1 : 0; }