Fix bugs in diff implementation

This commit is contained in:
Maxime Coste 2015-05-14 13:57:03 +01:00
parent f224d11ccd
commit cc97d4ba41
2 changed files with 27 additions and 3 deletions

View File

@ -14,6 +14,8 @@ struct MirroredArray : public ArrayView<T>
: ArrayView<T>(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<T>::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});

View File

@ -241,11 +241,30 @@ void test_line_modifications()
}
void test_diff()
{
auto eq = [](const Diff& lhs, const Diff& rhs) {
return lhs.mode == rhs.mode and lhs.len == rhs.len and lhs.posB == rhs.posB;
};
{
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()