Also fixes Diff/Memo/PriorityQueue from previous session: LineDiff is now an alias for Edit<string> so count_op/apply work on line diffs; Memoized uses std::apply instead of type-based get to handle duplicate arg types. 874/877 tests pass (3 are platform-gated plugin tests). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
135 lines
4.7 KiB
C++
135 lines
4.7 KiB
C++
#include <Void/Diff/Diff.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace Void::Diff;
|
|
|
|
// ── integer sequence diff ─────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test diff_identical_sequences()
|
|
{
|
|
const std::vector<int> 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<int> a{};
|
|
const std::vector<int> 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<int> a{1, 2};
|
|
const std::vector<int> 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<int> a{1, 3};
|
|
const std::vector<int> 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<int> a{1, 2, 3};
|
|
const std::vector<int> 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<int> a{1, 2, 3};
|
|
const std::vector<int> 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<int> a{1, 2, 3, 4, 5};
|
|
const std::vector<int> 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<int> a{1, 2, 3};
|
|
const std::vector<int> 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<int> 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<char> a{'a', 'b', 'c'};
|
|
const std::vector<char> 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"});
|
|
}
|