// Known-answer checks for the atomic monthly operating-budget proposal. #include "sc2k/sim/monthly_operating_budget.hpp" #include "test_harness.hpp" #include #include #include #include using namespace sc2k; static std::array two_equal_service_requests() { std::array records{}; // divisor -1: 12 * 10 / 12 = 10 cash requested per record. records[5].rate = 12; records[5].value = 10; records[6].rate = 12; records[6].value = 10; return records; } int main() { // The native water-department record participates in the same proportional monthly allocation. { sim::OperatingBudgetState state; state.waterFundingPercent = 100; state.waterOperatingValue = 1200; // $1,200 per year at full funding = $100 this month const sim::MonthlyOperatingBudgetQuoteResult result = sim::quote_monthly_operating_budget({}, state, 0, 60, 0); CHECK(result.status == sim::MonthlyOperatingBudgetQuoteStatus::Ready); CHECK(result.quote.has_value()); if (result.quote) { CHECK(result.quote->requestedByRecord[sim::kWaterOperationsBudgetRecord] == 100); CHECK(result.quote->operatingAllocation.paidByRecord[sim::kWaterOperationsBudgetRecord] == 60); CHECK(result.quote->operatingAllocation.coverageBasisPoints == 6000); CHECK(result.quote->nextState.waterPaidYearToDate == 60); CHECK(result.quote->treasuryAfter == 0); } } // Revenue is available before debt; debt is senior to proportional operating allocations. { auto records = two_equal_service_requests(); // divisor +75: 75 * 120 / 900 = 10 cash revenue. records[0].rate = 75; records[0].value = 120; const sim::OperatingBudgetState state; const sim::MonthlyOperatingBudgetQuoteResult result = sim::quote_monthly_operating_budget(records, state, 123, 15, 10); CHECK(result.status == sim::MonthlyOperatingBudgetQuoteStatus::Ready); CHECK(result.quote.has_value()); CHECK(!result.pendingState); if (result.quote) { const auto& quote = *result.quote; CHECK(quote.revenueCash == 10); CHECK(quote.treasuryAfterRevenue == 25); CHECK(quote.mandatoryDebtPayment == 10); CHECK(quote.treasuryAfterDebt == 15); CHECK(quote.operatingAllocation.requestedTotal == 20); CHECK(quote.operatingAllocation.paidTotal == 15); CHECK(quote.operatingAllocation.paidByRecord[5] == 8); CHECK(quote.operatingAllocation.paidByRecord[6] == 7); CHECK(quote.operatingAllocation.coverageBasisPoints == 7500); CHECK(quote.treasuryAfter == 0); CHECK(quote.nextState.lastProcessedPeriod == 123); CHECK(quote.nextState.consecutiveDistressMonths == 0); } } // The first Critical month pauses without advancing any authoritative accounting state. sim::OperatingBudgetState acceptedCriticalState; { auto records = two_equal_service_requests(); // A fractional request that produces no whole cash still advances only the proposed carry. records[7].rate = 1; records[7].value = 1; const sim::OperatingBudgetState state; const sim::MonthlyOperatingBudgetQuoteResult paused = sim::quote_monthly_operating_budget(records, state, 0, 8, 0); CHECK(paused.status == sim::MonthlyOperatingBudgetQuoteStatus::AwaitingCriticalDecision); CHECK(paused.quote.has_value()); CHECK(paused.pendingState.has_value()); if (paused.quote) { CHECK(paused.quote->operatingAllocation.coverageBasisPoints == 4000); CHECK(paused.quote->nextState.lastProcessedPeriod == 0); CHECK(paused.quote->nextState.consecutiveDistressMonths == 1); CHECK(paused.quote->nextState.conversionRemainderUnits[7] == -1); CHECK(paused.quote->treasuryAfter == 0); } if (paused.pendingState) { CHECK(paused.pendingState->pendingDecisionPeriod == 0); CHECK(!paused.pendingState->lastProcessedPeriod); CHECK(paused.pendingState->coverageBasisPoints == 10000); CHECK(paused.pendingState->consecutiveDistressMonths == 0); for (const std::int64_t carry : paused.pendingState->conversionRemainderUnits) CHECK(carry == 0); const sim::MonthlyOperatingBudgetQuoteResult accepted = sim::quote_monthly_operating_budget( records, *paused.pendingState, 0, 8, 0, true); CHECK(accepted.status == sim::MonthlyOperatingBudgetQuoteStatus::Ready); CHECK(accepted.quote.has_value()); if (accepted.quote) { acceptedCriticalState = accepted.quote->nextState; CHECK(!acceptedCriticalState.pendingDecisionPeriod); CHECK(acceptedCriticalState.lastProcessedPeriod == 0); CHECK(acceptedCriticalState.coverageBasisPoints == 4000); CHECK(acceptedCriticalState.consecutiveDistressMonths == 1); CHECK(acceptedCriticalState.conversionRemainderUnits[7] == -1); } } } // A net-negative ordinance is an operating request, not negative revenue or mandatory debt. { std::array records{}; records[3].rate = 1; records[3].value = -900; const sim::MonthlyOperatingBudgetQuoteResult result = sim::quote_monthly_operating_budget( records, sim::OperatingBudgetState{}, 0, 1, 0); CHECK(result.status == sim::MonthlyOperatingBudgetQuoteStatus::Ready); CHECK(result.quote.has_value()); if (result.quote) { CHECK(result.quote->revenueCash == 0); CHECK(result.quote->requestedByRecord[3] == 1); CHECK(result.quote->operatingAllocation.paidByRecord[3] == 1); CHECK(result.quote->treasuryAfter == 0); } } // Continued distress does not pause every month; the bounded streak reaches support eligibility. { const auto records = two_equal_service_requests(); const sim::MonthlyOperatingBudgetQuoteResult second = sim::quote_monthly_operating_budget( records, acceptedCriticalState, 1, 0, 0); CHECK(second.status == sim::MonthlyOperatingBudgetQuoteStatus::Ready); CHECK(second.quote.has_value()); if (second.quote) { CHECK(second.quote->operatingAllocation.coverageBasisPoints == 0); CHECK(second.quote->nextState.consecutiveDistressMonths == sim::kOperatingDistressSupportThresholdMonths); const sim::MonthlyOperatingBudgetQuoteResult recovered = sim::quote_monthly_operating_budget( records, second.quote->nextState, 2, 20, 0); CHECK(recovered.status == sim::MonthlyOperatingBudgetQuoteStatus::Ready); CHECK(recovered.quote.has_value()); if (recovered.quote) { CHECK(recovered.quote->operatingAllocation.coverageBasisPoints == 10000); CHECK(recovered.quote->nextState.consecutiveDistressMonths == 0); } } } // Debt shortfall is non-committable even when this month's revenue narrows the gap. { std::array records{}; records[0].rate = 75; records[0].value = 120; const sim::MonthlyOperatingBudgetQuoteResult shortfall = sim::quote_monthly_operating_budget( records, sim::OperatingBudgetState{}, 0, 0, 11); CHECK(shortfall.status == sim::MonthlyOperatingBudgetQuoteStatus::InsufficientForDebt); CHECK(shortfall.quote.has_value()); CHECK(!shortfall.pendingState); if (shortfall.quote) { CHECK(shortfall.quote->revenueCash == 10); CHECK(shortfall.quote->treasuryAfterRevenue == 10); CHECK(shortfall.quote->treasuryAfterDebt == -1); } } // Taking a loan while paused can make the proposal non-critical and clear the pending decision. { const auto records = two_equal_service_requests(); sim::OperatingBudgetState pending; pending.pendingDecisionPeriod = 9; const sim::MonthlyOperatingBudgetQuoteResult recovered = sim::quote_monthly_operating_budget(records, pending, 9, 20, 0); CHECK(recovered.status == sim::MonthlyOperatingBudgetQuoteStatus::Ready); CHECK(recovered.quote && !recovered.quote->nextState.pendingDecisionPeriod); const sim::MonthlyOperatingBudgetQuoteResult staleAcceptance = sim::quote_monthly_operating_budget( records, pending, 9, 20, 0, true); CHECK(staleAcceptance.status == sim::MonthlyOperatingBudgetQuoteStatus::StaleDecision); } // Period order, mode, record signs, and arithmetic boundaries fail closed. { const auto records = two_equal_service_requests(); sim::OperatingBudgetState processed; processed.lastProcessedPeriod = 5; CHECK(sim::quote_monthly_operating_budget(records, processed, 5, 20, 0).status == sim::MonthlyOperatingBudgetQuoteStatus::AlreadyProcessed); CHECK(sim::quote_monthly_operating_budget(records, processed, 7, 20, 0).status == sim::MonthlyOperatingBudgetQuoteStatus::InvalidState); CHECK(sim::quote_monthly_operating_budget(records, processed, 6, 20, 0, true).status == sim::MonthlyOperatingBudgetQuoteStatus::StaleDecision); sim::OperatingBudgetState legacy; legacy.mode = sim::OperatingBudgetMode::LegacyAnnual; legacy.activationPeriod = 12; CHECK(sim::quote_monthly_operating_budget(records, legacy, 0, 20, 0).status == sim::MonthlyOperatingBudgetQuoteStatus::InvalidState); auto invalidRecords = records; invalidRecords[5].rate = -1; CHECK(sim::quote_monthly_operating_budget( invalidRecords, sim::OperatingBudgetState{}, 0, 20, 0) .status == sim::MonthlyOperatingBudgetQuoteStatus::InvalidState); std::array revenue{}; revenue[0].rate = 75; revenue[0].value = 120; CHECK(sim::quote_monthly_operating_budget(revenue, sim::OperatingBudgetState{}, 0, std::numeric_limits::max(), 0) .status == sim::MonthlyOperatingBudgetQuoteStatus::Overflow); } // Identical snapshots always produce byte-for-byte-equivalent proposals. { const auto records = two_equal_service_requests(); const sim::OperatingBudgetState state; const auto first = sim::quote_monthly_operating_budget(records, state, 42, 17, 3); const auto second = sim::quote_monthly_operating_budget(records, state, 42, 17, 3); CHECK(first == second); } if (failures == 0) std::printf("monthly_operating_budget: all checks passed\n"); else std::printf("monthly_operating_budget: %d FAILED\n", failures); return failures ? 1 : 0; }