283 lines
12 KiB
C++
283 lines
12 KiB
C++
#include <Void/Text/Diff.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <functional>
|
|
#include <limits>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
using namespace Void::Diff;
|
|
|
|
namespace
|
|
{
|
|
std::string join_lines(const std::vector<std::string>& lines)
|
|
{
|
|
std::string out;
|
|
for (std::size_t i = 0; i < lines.size(); ++i) {
|
|
if (i != 0) out += '\n';
|
|
out += lines[i];
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::string reconstruct_lines(const std::vector<LineDiff>& edits)
|
|
{
|
|
return join_lines(apply(edits));
|
|
}
|
|
} // namespace
|
|
|
|
// ── empty / equal inputs ──────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bounded_diff_empty_and_equal_inputs()
|
|
{
|
|
const std::vector<int> empty;
|
|
const auto both_empty = diff_bounded(empty, empty);
|
|
Void::Testing::require(both_empty.has_value());
|
|
Void::Testing::expect(both_empty->empty());
|
|
|
|
const std::vector<int> same{1, 2, 3, 4};
|
|
const auto equal = diff_bounded(same, same);
|
|
Void::Testing::require(equal.has_value());
|
|
Void::Testing::expect_eq(count_op(*equal, Op::Insert), std::size_t{0});
|
|
Void::Testing::expect_eq(count_op(*equal, Op::Delete), std::size_t{0});
|
|
Void::Testing::expect_eq(count_op(*equal, Op::Equal), std::size_t{4});
|
|
Void::Testing::expect_eq(apply(*equal), same);
|
|
|
|
// Zero elements are allowed when the budget is exactly zero.
|
|
const auto zero_budget =
|
|
diff_bounded(empty, empty, DiffLimits{.max_input_elements = 0});
|
|
Void::Testing::require(zero_budget.has_value());
|
|
Void::Testing::expect(zero_budget->empty());
|
|
}
|
|
|
|
// ── unrelated sequences ──────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bounded_diff_unrelated_sequences()
|
|
{
|
|
const std::vector<int> a{1, 2, 3};
|
|
const std::vector<int> b{4, 5, 6};
|
|
const auto edits = diff_bounded(a, b);
|
|
Void::Testing::require(edits.has_value());
|
|
Void::Testing::expect_eq(count_op(*edits, Op::Equal), std::size_t{0});
|
|
Void::Testing::expect_eq(count_op(*edits, Op::Delete), std::size_t{3});
|
|
Void::Testing::expect_eq(count_op(*edits, Op::Insert), std::size_t{3});
|
|
Void::Testing::expect_eq(apply(*edits), b);
|
|
}
|
|
|
|
// ── repetitive lines ─────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bounded_diff_repetitive_lines()
|
|
{
|
|
std::string a;
|
|
for (int i = 0; i < 64; ++i) a += "repeat\n";
|
|
|
|
std::string b = a;
|
|
b.replace(0, 7, "changed\n");
|
|
b += "tail\n";
|
|
|
|
const auto edits = line_diff_bounded(a, b);
|
|
Void::Testing::require(edits.has_value());
|
|
Void::Testing::expect_eq(reconstruct_lines(*edits), b);
|
|
|
|
// A limit that still fits the line count succeeds.
|
|
const std::size_t lines = 65 + 66;
|
|
const auto limited = line_diff_bounded(
|
|
a, b, DiffLimits{.max_input_elements = lines,
|
|
.max_trace_memory = std::numeric_limits<std::size_t>::max(),
|
|
.max_work = std::numeric_limits<std::size_t>::max()});
|
|
Void::Testing::require(limited.has_value());
|
|
Void::Testing::expect_eq(reconstruct_lines(*limited), b);
|
|
}
|
|
|
|
// ── input and arithmetic boundaries ─────────────────────────────────────────
|
|
|
|
Void::Testing::Test bounded_diff_input_and_arithmetic_boundaries()
|
|
{
|
|
const std::vector<int> one{1};
|
|
const std::vector<int> two{2};
|
|
|
|
const auto too_many = diff_bounded(one, two, DiffLimits{.max_input_elements = 1});
|
|
Void::Testing::expect(!too_many.has_value());
|
|
Void::Testing::expect(too_many.error() == DiffFailure::InputTooLarge);
|
|
|
|
const auto exact = diff_bounded(one, two, DiffLimits{.max_input_elements = 2});
|
|
Void::Testing::require(exact.has_value());
|
|
Void::Testing::expect_eq(apply(*exact), two);
|
|
|
|
const auto maximum =
|
|
diff_bounded(one, two,
|
|
DiffLimits{.max_input_elements = std::numeric_limits<std::size_t>::max(),
|
|
.max_input_bytes = std::numeric_limits<std::size_t>::max(),
|
|
.max_trace_memory = std::numeric_limits<std::size_t>::max(),
|
|
.max_output = std::numeric_limits<std::size_t>::max(),
|
|
.max_work = std::numeric_limits<std::size_t>::max()});
|
|
Void::Testing::require(maximum.has_value());
|
|
Void::Testing::expect_eq(apply(*maximum), two);
|
|
|
|
// Line diff checks the combined byte count with checked arithmetic.
|
|
const auto bytes = line_diff_bounded("abc", "abc",
|
|
DiffLimits{.max_input_bytes = 5});
|
|
Void::Testing::expect(!bytes.has_value());
|
|
Void::Testing::expect(bytes.error() == DiffFailure::InputTooLarge);
|
|
|
|
const auto bytes_exact = line_diff_bounded("abc", "abc",
|
|
DiffLimits{.max_input_bytes = 6});
|
|
Void::Testing::require(bytes_exact.has_value());
|
|
Void::Testing::expect_eq(reconstruct_lines(*bytes_exact), std::string{"abc"});
|
|
}
|
|
|
|
// ── trace memory budget ─────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bounded_diff_trace_memory_budget()
|
|
{
|
|
const std::vector<int> a{1, 2};
|
|
const std::vector<int> b{3, 4};
|
|
|
|
// max_d = 4, v_size = 9, one snapshot = 9 * sizeof(ptrdiff_t) = 72 bytes.
|
|
constexpr std::size_t kSnapshot = 9 * sizeof(std::ptrdiff_t);
|
|
|
|
const auto none = diff_bounded(a, b, DiffLimits{.max_trace_memory = 0});
|
|
Void::Testing::expect(!none.has_value());
|
|
Void::Testing::expect(none.error() == DiffFailure::TraceMemoryExhausted);
|
|
|
|
const auto short_by_one =
|
|
diff_bounded(a, b, DiffLimits{.max_trace_memory = kSnapshot - 1});
|
|
Void::Testing::expect(!short_by_one.has_value());
|
|
Void::Testing::expect(short_by_one.error() == DiffFailure::TraceMemoryExhausted);
|
|
|
|
// Unrelated sequences need d = 4, i.e. five snapshots.
|
|
const auto exact = diff_bounded(a, b, DiffLimits{.max_trace_memory = 5 * kSnapshot});
|
|
Void::Testing::require(exact.has_value());
|
|
Void::Testing::expect_eq(apply(*exact), b);
|
|
}
|
|
|
|
// ── output budget ──────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bounded_diff_output_budget()
|
|
{
|
|
const std::vector<int> a{1, 2, 3};
|
|
const std::vector<int> b{4, 5, 6};
|
|
|
|
const auto too_small = diff_bounded(a, b, DiffLimits{.max_output = 5});
|
|
Void::Testing::expect(!too_small.has_value());
|
|
Void::Testing::expect(too_small.error() == DiffFailure::OutputTooLarge);
|
|
|
|
const auto exact = diff_bounded(a, b, DiffLimits{.max_output = 6});
|
|
Void::Testing::require(exact.has_value());
|
|
Void::Testing::expect_eq(exact->size(), std::size_t{6});
|
|
Void::Testing::expect_eq(apply(*exact), b);
|
|
}
|
|
|
|
// ── work budget ────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bounded_diff_work_budget()
|
|
{
|
|
const std::vector<int> a{1, 2, 3};
|
|
const std::vector<int> b{1, 2, 3};
|
|
|
|
const auto no_work = diff_bounded(a, b, DiffLimits{.max_work = 0});
|
|
Void::Testing::expect(!no_work.has_value());
|
|
Void::Testing::expect(no_work.error() == DiffFailure::WorkExhausted);
|
|
|
|
const auto one_unit = diff_bounded(a, b, DiffLimits{.max_work = 1});
|
|
Void::Testing::expect(!one_unit.has_value());
|
|
Void::Testing::expect(one_unit.error() == DiffFailure::WorkExhausted);
|
|
|
|
const auto enough =
|
|
diff_bounded(a, b, DiffLimits{.max_work = std::numeric_limits<std::size_t>::max()});
|
|
Void::Testing::require(enough.has_value());
|
|
Void::Testing::expect_eq(apply(*enough), b);
|
|
}
|
|
|
|
// ── cancellation ───────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bounded_diff_cancellation()
|
|
{
|
|
const std::vector<int> a{1, 2, 3, 4};
|
|
const std::vector<int> b{1, 3, 4, 5};
|
|
|
|
const auto immediate =
|
|
diff_bounded(a, b, {}, std::equal_to<int>{}, [] { return true; });
|
|
Void::Testing::expect(!immediate.has_value());
|
|
Void::Testing::expect(immediate.error() == DiffFailure::Cancelled);
|
|
|
|
int calls = 0;
|
|
const auto after_one = diff_bounded(a, b, {}, std::equal_to<int>{}, [&calls] {
|
|
return ++calls > 1;
|
|
});
|
|
Void::Testing::expect(!after_one.has_value());
|
|
Void::Testing::expect(after_one.error() == DiffFailure::Cancelled);
|
|
Void::Testing::expect(calls >= 2);
|
|
|
|
bool cancelled = false;
|
|
const auto not_cancelled =
|
|
diff_bounded(a, b, {}, std::equal_to<int>{}, [&cancelled] { return cancelled; });
|
|
Void::Testing::require(not_cancelled.has_value());
|
|
Void::Testing::expect_eq(apply(*not_cancelled), b);
|
|
}
|
|
|
|
// ── reconstruction properties ──────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bounded_diff_reconstruction_properties()
|
|
{
|
|
const std::vector<std::vector<int>> int_cases{
|
|
{},
|
|
{1},
|
|
{1, 2, 3},
|
|
{1, 2, 3, 4, 5},
|
|
{2, 1, 2, 1, 2},
|
|
{9, 8, 7, 6},
|
|
};
|
|
for (const auto& a : int_cases) {
|
|
for (const auto& b : int_cases) {
|
|
const auto edits = diff_bounded(a, b);
|
|
Void::Testing::require(edits.has_value());
|
|
Void::Testing::expect_eq(apply(*edits), b);
|
|
Void::Testing::expect(edits->size() <= a.size() + b.size());
|
|
}
|
|
}
|
|
|
|
const std::vector<std::pair<std::string, std::string>> line_cases{
|
|
{"", ""},
|
|
{"a", ""},
|
|
{"", "a"},
|
|
{"a\nb\nc", "a\nb\nc"},
|
|
{"a\nb\nc", "a\nx\nc"},
|
|
{"x\ny\nx\ny", "y\nx\ny\nx"},
|
|
{"same\nsame\nsame", "same\nsame"},
|
|
};
|
|
for (const auto& [a, b] : line_cases) {
|
|
const auto edits = line_diff_bounded(a, b);
|
|
Void::Testing::require(edits.has_value());
|
|
Void::Testing::expect_eq(reconstruct_lines(*edits), b);
|
|
}
|
|
}
|
|
|
|
// ── line diff budgets ──────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bounded_line_diff_budgets()
|
|
{
|
|
const auto line_limit = line_diff_bounded(
|
|
"a\nb", "a\nb", DiffLimits{.max_input_elements = 3});
|
|
Void::Testing::expect(!line_limit.has_value());
|
|
Void::Testing::expect(line_limit.error() == DiffFailure::InputTooLarge);
|
|
|
|
const auto work_limit = line_diff_bounded(
|
|
"a\nb\nc", "x\ny\nz", DiffLimits{.max_work = 0});
|
|
Void::Testing::expect(!work_limit.has_value());
|
|
Void::Testing::expect(work_limit.error() == DiffFailure::WorkExhausted);
|
|
|
|
const auto output_limit = line_diff_bounded(
|
|
"a\nb\nc", "x\ny\nz", DiffLimits{.max_output = 5});
|
|
Void::Testing::expect(!output_limit.has_value());
|
|
Void::Testing::expect(output_limit.error() == DiffFailure::OutputTooLarge);
|
|
|
|
const auto cancelled = line_diff_bounded(
|
|
"a\nb", "a\nb", {}, [] { return true; });
|
|
Void::Testing::expect(!cancelled.has_value());
|
|
Void::Testing::expect(cancelled.error() == DiffFailure::Cancelled);
|
|
}
|