// Known-answer test for municipal bond issuance. // Verifies the transcribed state machine: each bond adds $10,000 + its rate, the rate array fills in // issue order, the running sum tracks, and the 50-bond cap rejects further issues with no side effect. #include "sc2k/sim/bonds.hpp" #include "sc2k/sim/terrain_gen.hpp" #include #include #include "test_harness.hpp" int main() { using namespace sc2k; Bonds b; long funds = 0; // First bond: +$10,000, rate recorded at index 0. CHECK(issue_bond(b, funds, 5)); CHECK(funds == 10000); CHECK(b.count == 1); CHECK(b.rateSum == 5); CHECK(b.rate[0] == 5); // Second bond at a different rate: principal and sum accumulate, array keeps issue order. CHECK(issue_bond(b, funds, 8)); CHECK(funds == 20000); CHECK(b.count == 2); CHECK(b.rateSum == 13); CHECK(b.rate[1] == 8); // aggregate (+548) = 10000·rateSum/count = 10000·13/2 = 65000 CHECK(bond_interest_aggregate(b) == 65000); // Fill to the 50-bond cap. while (b.count < kMaxBonds) CHECK(issue_bond(b, funds, 7)); CHECK(b.count == kMaxBonds); const long fundsAtCap = funds; const int sumAtCap = b.rateSum; // The 51st issue is rejected and leaves every field untouched. CHECK(!issue_bond(b, funds, 7)); CHECK(funds == fundsAtCap); CHECK(b.rateSum == sumAtCap); CHECK(b.count == kMaxBonds); // Treasury raised exactly count·$10,000 across all issues. CHECK(funds == static_cast(kMaxBonds) * kBondPrincipal); // --- City valuation: weighted infra histogram over codes [14,112). --- { TileCensus cen{}; cen.count[30] = 10; // road (29-43) weight 10 → 100 cen.count[50] = 4; // rail (44-62) weight 25 → 100 cen.count[110] = 2; // subway junc (108-111) w 250 → 500 CHECK(city_valuation(cen) == 700); CHECK(city_valuation(cen, 50) == 650); // minus the subway-tile counter } // --- Debt ceiling + try_issue_bond (the original checks the ceiling before the 50-cap). --- { Bonds b2; long f2 = 0; CHECK(try_issue_bond(b2, f2, 3, 100000) == BondIssue::Issued); // wealthy city → issue at 3+1=4 CHECK(b2.count == 1 && b2.rate[0] == 4 && f2 == 10000); Bonds b3; long f3 = 0; b3.count = 3; // poor city carrying debt CHECK(try_issue_bond(b3, f3, 3, 1000) == BondIssue::OverCeiling);// 2500·3/1001 = 7 ≥ 6 → refuse CHECK(f3 == 0 && b3.count == 3); // no side effect } // --- Loan rating: 25000·count/(value+1) clamped to [0,6] → AAA..F (value 24999 makes index == count). --- { const long v = 24999; // value + 1 = 25000, so the index equals the bond count CHECK(loan_rating_index(0, v) == 0); CHECK(loan_rating_index(1, v) == 1); CHECK(loan_rating_index(3, v) == 3); CHECK(loan_rating_index(6, v) == 6); CHECK(loan_rating_index(9, v) == 6); // clamped at the bottom of the scale CHECK(std::string(loan_rating_grade(0)) == "AAA"); CHECK(std::string(loan_rating_grade(1)) == "AA"); CHECK(std::string(loan_rating_grade(2)) == "A"); CHECK(std::string(loan_rating_grade(3)) == "B"); CHECK(std::string(loan_rating_grade(4)) == "C"); CHECK(std::string(loan_rating_grade(5)) == "D"); CHECK(std::string(loan_rating_grade(6)) == "F"); // A debt-free (AAA) city borrows at bankRate+1; a poorer rating pays a matching premium. CHECK(next_bond_interest(5, 0) == 6); CHECK(next_bond_interest(5, 2) == 8); // Coherence: once the ceiling blocks borrowing, the rating has already bottomed out at F. CHECK(bond_over_debt_ceiling(3, 1000)); CHECK(loan_rating_index(3, 1000) == 6); } // --- Repay removes the OLDEST bond (the original pays principal and shifts the array down). --- { Bonds b4; long f4 = 100000; issue_bond(b4, f4, 5); issue_bond(b4, f4, 8); issue_bond(b4, f4, 6); // rates [5,8,6]; f4 = 130000 CHECK(repay_bond(b4, f4)); CHECK(b4.count == 2 && b4.rate[0] == 8 && b4.rate[1] == 6); // oldest (5) gone, rest shifted CHECK(b4.rateSum == 14 && f4 == 120000); // −$10,000 Bonds b5; long f5 = 5000; CHECK(!repay_bond(b5, f5)); // no bonds → refused b5.count = 1; b5.rate[0] = 5; b5.rateSum = 5; CHECK(!repay_bond(b5, f5) && f5 == 5000); // funds < $10,000 → refused, unchanged } // --- Hard difficulty seeds the national rate (3) AND starts 1 bond (the original's Hard case). --- { City hard = make_new_city(0x1234u, 0 /*1900*/, 3 /*hard*/, 20, 20, 20, 1, 1); CHECK(hard.next_bond_rate() == kInitialBondRate); // 3 CHECK(hard.bond_count() == 1 && hard.bond_rate(0) == kInitialBondRate); City easy = make_new_city(0x1234u, 0, 1 /*easy*/, 20, 20, 20, 1, 1); CHECK(easy.next_bond_rate() == kInitialBondRate); // rate seeded regardless of difficulty CHECK(easy.bond_count() == 0); // easy/medium start debt-free } if (failures == 0) { std::printf("bonds: issuance checks passed\n"); return 0; } std::printf("bonds: %d failure(s)\n", failures); return 1; }