From cc97d4ba41169a3a365922b05a30fdab88dbe688 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 14 May 2015 13:57:03 +0100 Subject: [PATCH] Fix bugs in diff implementation --- src/diff.hh | 5 +++++ src/unit_tests.cc | 25 ++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/diff.hh b/src/diff.hh index bc9717083..e68ec10c4 100644 --- a/src/diff.hh +++ b/src/diff.hh @@ -14,6 +14,8 @@ struct MirroredArray : public ArrayView : ArrayView(data), size(size) { kak_assert(2 * size + 1 <= data.size()); + for (int i = -size; i <= size; ++i) + (*this)[i] = 0; } T& operator[](int n) { return ArrayView::operator[](n + size); } @@ -118,6 +120,7 @@ void find_diff_rec(Iterator a, int offA, int lenA, if (lenA > 0 and lenB > 0) { auto middle_snake = find_middle_snake(a + offA, lenA, b + offB, lenB, data1, data2, eq); + kak_assert(middle_snake.u <= lenA and middle_snake.v <= lenB); if (middle_snake.d > 1) { find_diff_rec(a, offA, middle_snake.x, @@ -145,6 +148,8 @@ void find_diff_rec(Iterator a, int offA, int lenA, else diffs.push_back({Diff::Remove, 1, 0}); } + else if (int len = middle_snake.u - middle_snake.x) + diffs.push_back({Diff::Keep, len, 0}); } else if (lenB > 0) diffs.push_back({Diff::Add, lenB, offB}); diff --git a/src/unit_tests.cc b/src/unit_tests.cc index e054ee411..29fd90f6c 100644 --- a/src/unit_tests.cc +++ b/src/unit_tests.cc @@ -242,10 +242,29 @@ void test_line_modifications() void test_diff() { - StringView s1 = "mais que fais la police"; - StringView s2 = "mais ou va la police"; + auto eq = [](const Diff& lhs, const Diff& rhs) { + return lhs.mode == rhs.mode and lhs.len == rhs.len and lhs.posB == rhs.posB; + }; - auto diff = find_diff(s1.begin(), (int)s1.length(), s2.begin(), (int)s2.length()); + { + StringView s1 = "mais que fais la police"; + StringView s2 = "mais ou va la police"; + + auto diff = find_diff(s1.begin(), (int)s1.length(), s2.begin(), (int)s2.length()); + kak_assert(diff.size() == 10); + } + + { + StringView s1 = "a?"; + StringView s2 = "!"; + + auto diff = find_diff(s1.begin(), (int)s1.length(), s2.begin(), (int)s2.length()); + + kak_assert(diff.size() == 3 and + eq(diff[0], {Diff::Remove, 1, 0}) and + eq(diff[1], {Diff::Add, 1, 0}) and + eq(diff[2], {Diff::Remove, 1, 0})); + } } void run_unit_tests()