#include "tests/test_framework.hpp" #include "engine/address.hpp" #include "engine/workbook.hpp" using namespace htab; TEST_CASE("Phase2: Incremental DAG recalculation and cycle detection") { Workbook wb; wb.set_cell_input(0, 0, 0, "5"); // A1 wb.set_cell_input(0, 1, 0, "=A1 * 2"); // A2 = 10 wb.set_cell_input(0, 2, 0, "=A1 + A2"); // A3 = 15 (diamond) wb.set_cell_input(0, 3, 0, "=A3 * 10"); // A4 = 150 CHECK_NEAR(wb.get_scalar_value(0, 3, 0).as_number(), 150.0, 1e-12); wb.set_cell_input(0, 0, 0, "10"); // A1 = 10 => A2=20, A3=30, A4=300 CHECK_NEAR(wb.get_scalar_value(0, 1, 0).as_number(), 20.0, 1e-12); CHECK_NEAR(wb.get_scalar_value(0, 2, 0).as_number(), 30.0, 1e-12); CHECK_NEAR(wb.get_scalar_value(0, 3, 0).as_number(), 300.0, 1e-12); // Direct self-cycle wb.set_cell_input(0, 0, 1, "=B1"); CHECK(wb.get_scalar_value(0, 0, 1).is_error()); CHECK(wb.get_scalar_value(0, 0, 1).as_error() == ErrorCode::Cycle); // Multi-node cycle: C1 -> D1 -> E1 -> C1 and downstream F1 = E1 + 1 wb.set_cell_input(0, 0, 2, "=E1 + 1"); // C1 wb.set_cell_input(0, 0, 3, "=C1 + 1"); // D1 wb.set_cell_input(0, 0, 4, "=D1 + 1"); // E1 wb.set_cell_input(0, 0, 5, "=E1 + 10"); // F1 (downstream of cycle) wb.set_cell_input(0, 0, 6, "=IF(FALSE, E1, 99)"); // G1 (short-circuits past cycle) CHECK(wb.get_scalar_value(0, 0, 2).as_error() == ErrorCode::Cycle); CHECK(wb.get_scalar_value(0, 0, 3).as_error() == ErrorCode::Cycle); CHECK(wb.get_scalar_value(0, 0, 4).as_error() == ErrorCode::Cycle); CHECK(wb.get_scalar_value(0, 0, 5).as_error() == ErrorCode::Cycle); CHECK(wb.get_scalar_value(0, 0, 6).is_number()); CHECK_NEAR(wb.get_scalar_value(0, 0, 6).as_number(), 99.0, 1e-12); // Break cycle wb.set_cell_input(0, 0, 2, "100"); CHECK_NEAR(wb.get_scalar_value(0, 0, 3).as_number(), 101.0, 1e-12); CHECK_NEAR(wb.get_scalar_value(0, 0, 4).as_number(), 102.0, 1e-12); CHECK_NEAR(wb.get_scalar_value(0, 0, 5).as_number(), 112.0, 1e-12); } TEST_CASE("Phase2: Structural row/col edits, sheet ops, and Undo/Redo") { Workbook wb; wb.set_cell_input(0, 0, 0, "10"); // A1 wb.set_cell_input(0, 1, 0, "20"); // A2 wb.set_cell_input(0, 2, 0, "30"); // A3 wb.set_cell_input(0, 3, 0, "=SUM(A1:A3)"); // A4 = 60 wb.set_cell_input(0, 4, 0, "=$A$2 + A3"); // A5 = 50 int s2 = wb.add_sheet("Data"); wb.set_cell_input(s2, 0, 0, "=Sheet1!$A$2 + SUM(Sheet1!A1:A3)"); // 20 + 60 = 80 CHECK_NEAR(wb.get_scalar_value(s2, 0, 0).as_number(), 80.0, 1e-12); // Insert row inside A1:A3 (at index 1, before row 2) -> A1:A3 expands to A1:A4 wb.insert_rows(0, 1, 1); // Old A2 shifted to A3; A4 shifted to A5; A5 shifted to A6 CHECK_EQ(wb.get_cell(0, 4, 0)->raw_input, std::string("=SUM(A1:A4)")); CHECK_EQ(wb.get_cell(0, 5, 0)->raw_input, std::string("=$A$3 + A4")); CHECK_EQ(wb.get_cell(s2, 0, 0)->raw_input, std::string("=Sheet1!$A$3 + SUM(Sheet1!A1:A4)")); CHECK_NEAR(wb.get_scalar_value(s2, 0, 0).as_number(), 80.0, 1e-12); // Set newly inserted A2 = 5 -> sums update to 65 and Data!A1 = 85 wb.set_cell_input(0, 1, 0, "5"); CHECK_NEAR(wb.get_scalar_value(0, 4, 0).as_number(), 65.0, 1e-12); CHECK_NEAR(wb.get_scalar_value(s2, 0, 0).as_number(), 85.0, 1e-12); // Delete row 2 (index 2, which holds 20 and is referenced directly by $A$3!) wb.delete_rows(0, 2, 1); CHECK_EQ(wb.get_cell(0, 3, 0)->raw_input, std::string("=SUM(A1:A3)")); CHECK_EQ(wb.get_cell(0, 4, 0)->raw_input, std::string("=#REF! + A3")); CHECK(wb.get_scalar_value(0, 4, 0).is_error()); CHECK(wb.get_scalar_value(0, 4, 0).as_error() == ErrorCode::Ref); CHECK(wb.get_scalar_value(s2, 0, 0).as_error() == ErrorCode::Ref); // Undo row deletion restores #REF! formula back to exact working formula CHECK(wb.undo()); CHECK_EQ(wb.get_cell(0, 5, 0)->raw_input, std::string("=$A$3 + A4")); CHECK_NEAR(wb.get_scalar_value(s2, 0, 0).as_number(), 85.0, 1e-12); // Redo re-applies deletion CHECK(wb.redo()); CHECK(wb.get_scalar_value(0, 4, 0).as_error() == ErrorCode::Ref); // Undo back to clean state, test column insert/delete and sheet rename/delete CHECK(wb.undo()); CHECK(wb.rename_sheet(0, "Main Sheet")); CHECK_EQ(wb.get_cell(s2, 0, 0)->raw_input, std::string("='Main Sheet'!$A$3 + SUM('Main Sheet'!A1:A4)")); CHECK_NEAR(wb.get_scalar_value(s2, 0, 0).as_number(), 85.0, 1e-12); // Insert column at index 0 on Main Sheet -> A becomes B wb.insert_cols(0, 0, 1); CHECK_EQ(wb.get_cell(s2, 0, 0)->raw_input, std::string("='Main Sheet'!$B$3 + SUM('Main Sheet'!B1:B4)")); CHECK_NEAR(wb.get_scalar_value(s2, 0, 0).as_number(), 85.0, 1e-12); // Delete entire range B1:B4 by deleting col B -> range becomes #REF! wb.delete_cols(0, 1, 1); CHECK(wb.get_scalar_value(s2, 0, 0).as_error() == ErrorCode::Ref); CHECK(wb.undo()); CHECK_NEAR(wb.get_scalar_value(s2, 0, 0).as_number(), 85.0, 1e-12); // Delete Main Sheet -> Data!A1 becomes #REF! CHECK(wb.delete_sheet(0)); CHECK_EQ(wb.sheet_count(), 1); CHECK(wb.get_scalar_value(0, 0, 0).as_error() == ErrorCode::Ref); CHECK(wb.undo()); CHECK_EQ(wb.sheet_count(), 2); CHECK_NEAR(wb.get_scalar_value(1, 0, 0).as_number(), 85.0, 1e-12); } TEST_CASE("Phase2: Copy/paste relative anchors and autofill") { Workbook wb; wb.set_cell_input(0, 0, 0, "2"); wb.set_cell_input(0, 1, 0, "4"); wb.set_cell_input(0, 0, 1, "10"); wb.set_cell_input(0, 1, 1, "20"); wb.set_cell_input(0, 0, 2, "=A1 + $A$1 + $B1 + B$1"); auto clip = wb.copy_range(0, CellRange{0, 0, 2, 0, 2}); wb.paste_range(0, 1, 3, clip, true); // Paste at D2 (drow=1, dcol=1) // A1 -> B2, $A$1 -> $A$1, $B1 -> $B2, B$1 -> C$1 CHECK_EQ(wb.get_cell(0, 1, 3)->raw_input, std::string("=B2 + $A$1 + $B2 + C$1")); // Autofill numeric series A1:A2 (2, 4) -> A1:A5 (2, 4, 6, 8, 10) wb.autofill_range(0, CellRange{0, 0, 0, 1, 0}, CellRange{0, 0, 0, 4, 0}); CHECK_NEAR(wb.get_scalar_value(0, 2, 0).as_number(), 6.0, 1e-12); CHECK_NEAR(wb.get_scalar_value(0, 3, 0).as_number(), 8.0, 1e-12); CHECK_NEAR(wb.get_scalar_value(0, 4, 0).as_number(), 10.0, 1e-12); } TEST_CASE("Phase2: Dynamic array spilling, blocking, unblocking, and downstream DAG") { Workbook wb; wb.set_cell_input(0, 0, 0, "1"); wb.set_cell_input(0, 1, 0, "2"); wb.set_cell_input(0, 2, 0, "3"); // Put blocker in B2 before entering spill formula in B1 wb.set_cell_input(0, 1, 1, "BLOCKER"); wb.set_cell_input(0, 0, 1, "=A1:A3 * 10"); // B1 must be #SPILL!, B2 must still preserve "BLOCKER" CHECK(wb.get_scalar_value(0, 0, 1).is_error()); CHECK(wb.get_scalar_value(0, 0, 1).as_error() == ErrorCode::Spill); CHECK_EQ(wb.get_scalar_value(0, 1, 1).as_string(), std::string("BLOCKER")); // Downstream formula referencing B1:B3 wb.set_cell_input(0, 0, 2, "=SUM(B1:B3)"); // C1 // Clear blocker in B2 -> B1 must automatically spill 10, 20, 30 and C1 must become 60! wb.set_cell_input(0, 1, 1, ""); CHECK(wb.get_scalar_value(0, 0, 1).is_number()); CHECK_NEAR(wb.get_scalar_value(0, 0, 1).as_number(), 10.0, 1e-12); CHECK_NEAR(wb.get_scalar_value(0, 1, 1).as_number(), 20.0, 1e-12); CHECK_NEAR(wb.get_scalar_value(0, 2, 1).as_number(), 30.0, 1e-12); CHECK(wb.is_spilled_cell(0, 1, 1)); CHECK(wb.is_spilled_cell(0, 2, 1)); CHECK_NEAR(wb.get_scalar_value(0, 0, 2).as_number(), 60.0, 1e-12); // Edit A2 from 2 to 5 -> B2 updates to 50 and C1 updates to 90 wb.set_cell_input(0, 1, 0, "5"); CHECK_NEAR(wb.get_scalar_value(0, 1, 1).as_number(), 50.0, 1e-12); CHECK_NEAR(wb.get_scalar_value(0, 0, 2).as_number(), 90.0, 1e-12); // Typing into spilled cell B3 triggers #SPILL! on B1 and preserves user's value in B3 wb.set_cell_input(0, 2, 1, "999"); CHECK(wb.get_scalar_value(0, 0, 1).as_error() == ErrorCode::Spill); CHECK_NEAR(wb.get_scalar_value(0, 2, 1).as_number(), 999.0, 1e-12); CHECK(!wb.is_spilled_cell(0, 1, 1)); }