#include #include #include #include #include using namespace Void::Diff; // ── integer sequence diff ───────────────────────────────────────────────────── Void::Testing::Test diff_identical_sequences() { const std::vector a{1, 2, 3}; const auto edits = diff(a, a); Void::Testing::expect_eq(count_op(edits, Op::Delete), std::size_t{0}); Void::Testing::expect_eq(count_op(edits, Op::Insert), std::size_t{0}); Void::Testing::expect_eq(count_op(edits, Op::Equal), std::size_t{3}); } Void::Testing::Test diff_empty_to_nonempty() { const std::vector a{}; const std::vector b{1, 2, 3}; const auto edits = diff(a, b); Void::Testing::expect_eq(count_op(edits, Op::Insert), std::size_t{3}); Void::Testing::expect_eq(count_op(edits, Op::Delete), std::size_t{0}); } Void::Testing::Test diff_nonempty_to_empty() { const std::vector a{1, 2}; const std::vector b{}; const auto edits = diff(a, b); Void::Testing::expect_eq(count_op(edits, Op::Delete), std::size_t{2}); Void::Testing::expect_eq(count_op(edits, Op::Insert), std::size_t{0}); } Void::Testing::Test diff_single_insert() { const std::vector a{1, 3}; const std::vector b{1, 2, 3}; const auto edits = diff(a, b); Void::Testing::expect_eq(count_op(edits, Op::Insert), std::size_t{1}); Void::Testing::expect_eq(count_op(edits, Op::Equal), std::size_t{2}); } Void::Testing::Test diff_single_delete() { const std::vector a{1, 2, 3}; const std::vector b{1, 3}; const auto edits = diff(a, b); Void::Testing::expect_eq(count_op(edits, Op::Delete), std::size_t{1}); Void::Testing::expect_eq(count_op(edits, Op::Equal), std::size_t{2}); } Void::Testing::Test diff_substitution() { const std::vector a{1, 2, 3}; const std::vector b{1, 9, 3}; const auto edits = diff(a, b); Void::Testing::expect_eq(count_op(edits, Op::Delete), std::size_t{1}); Void::Testing::expect_eq(count_op(edits, Op::Insert), std::size_t{1}); Void::Testing::expect_eq(count_op(edits, Op::Equal), std::size_t{2}); } Void::Testing::Test diff_apply_reconstructs_b() { const std::vector a{1, 2, 3, 4, 5}; const std::vector b{1, 3, 5, 6}; const auto edits = diff(a, b); const auto result = apply(edits); Void::Testing::expect_eq(result, b); } Void::Testing::Test diff_completely_different() { const std::vector a{1, 2, 3}; const std::vector b{4, 5, 6}; const auto edits = diff(a, b); Void::Testing::expect_eq(count_op(edits, Op::Equal), std::size_t{0}); } Void::Testing::Test diff_empty_both() { const std::vector a{}, b{}; const auto edits = diff(a, b); Void::Testing::expect(edits.empty()); } // ── string sequence diff ────────────────────────────────────────────────────── Void::Testing::Test diff_string_chars() { const std::vector a{'a', 'b', 'c'}; const std::vector b{'a', 'x', 'c'}; const auto edits = diff(a, b); Void::Testing::expect_eq(count_op(edits, Op::Equal), std::size_t{2}); Void::Testing::expect_eq(count_op(edits, Op::Delete), std::size_t{1}); Void::Testing::expect_eq(count_op(edits, Op::Insert), std::size_t{1}); } // ── line diff ───────────────────────────────────────────────────────────────── Void::Testing::Test diff_line_diff_identical() { const auto edits = line_diff("a\nb\nc", "a\nb\nc"); Void::Testing::expect_eq(count_op(edits, Op::Insert), std::size_t{0}); Void::Testing::expect_eq(count_op(edits, Op::Delete), std::size_t{0}); } Void::Testing::Test diff_line_diff_insert_line() { const auto edits = line_diff("a\nc", "a\nb\nc"); Void::Testing::expect_eq(count_op(edits, Op::Insert), std::size_t{1}); } Void::Testing::Test diff_format_patch() { const auto edits = line_diff("a\nb", "a\nc"); const auto patch = format_patch(edits); Void::Testing::expect(patch.find('-') != std::string::npos); Void::Testing::expect(patch.find('+') != std::string::npos); } Void::Testing::Test diff_apply_line_reconstructs_b() { const std::string a = "foo\nbar\nbaz"; const std::string b = "foo\nqux\nbaz"; const auto edits = line_diff(a, b); const auto result_lines = apply(edits); Void::Testing::expect_eq(result_lines.size(), std::size_t{3}); Void::Testing::expect_eq(result_lines[1], std::string{"qux"}); }