summaryrefslogtreecommitdiff
path: root/src/unit_tests.cc
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2019-11-30 10:46:42 +1100
committerMaxime Coste <mawww@kakoune.org>2019-11-30 11:29:36 +1100
commit4fdbf21ff8b042d48c9fb8a506bdc63018631453 (patch)
treec0414da81b2e87dba004e34eab9c6952f04d1452 /src/unit_tests.cc
parentb765fb4971db28bac608abc5cd856a9cb94fcfe1 (diff)
Refactor diff to make allocating a diff vector optional
The diff interface now goes through a for_each_diff function that uses a callback for each found diff.
Diffstat (limited to 'src/unit_tests.cc')
-rw-r--r--src/unit_tests.cc52
1 files changed, 18 insertions, 34 deletions
diff --git a/src/unit_tests.cc b/src/unit_tests.cc
index 54269074..694ad041 100644
--- a/src/unit_tests.cc
+++ b/src/unit_tests.cc
@@ -17,41 +17,25 @@ UnitTest test_utf8{[]()
UnitTest 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;
+ struct Diff{DiffOp op; int len; int posB;};
+ auto check_diff = [](StringView a, StringView b, std::initializer_list<Diff> diffs) {
+ size_t count = 0;
+ for_each_diff(a.begin(), (int)a.length(), b.begin(), (int)b.length(),
+ [&](DiffOp op, int len, int posB) {
+ kak_assert(count < diffs.size());
+ auto& d = diffs.begin()[count++];
+ kak_assert(d.op == op and d.len == len and d.posB == posB);
+ });
+ kak_assert(count == diffs.size());
};
-
- {
- auto diff = find_diff("a?", 2, "!", 1);
- 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}));
- }
-
- {
- auto diff = find_diff("abcde", 5, "cd", 2);
- kak_assert(diff.size() == 3 and
- eq(diff[0], {Diff::Remove, 2, 0}) and
- eq(diff[1], {Diff::Keep, 2, 0}) and
- eq(diff[2], {Diff::Remove, 1, 0}));
- }
-
- {
- auto diff = find_diff("abcd", 4, "cdef", 4);
- kak_assert(diff.size() == 3 and
- eq(diff[0], {Diff::Remove, 2, 0}) and
- eq(diff[1], {Diff::Keep, 2, 0}) and
- eq(diff[2], {Diff::Add, 2, 2}));
- }
-
- {
- 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() == 11);
- }
+ check_diff("a?", "!", {{DiffOp::Remove, 1, 0}, {DiffOp::Add, 1, 0}, {DiffOp::Remove, 1, 0}});
+ check_diff("abcde", "cd", {{DiffOp::Remove, 2, 0}, {DiffOp::Keep, 2, 0}, {DiffOp::Remove, 1, 0}});
+ check_diff("abcd", "cdef", {{DiffOp::Remove, 2, 0}, {DiffOp::Keep, 2, 0}, {DiffOp::Add, 2, 2}});
+
+ check_diff("mais que fais la police", "mais ou va la police",
+ {{DiffOp::Keep, 5, 0}, {DiffOp::Remove, 1, 0}, {DiffOp::Add, 1, 5}, {DiffOp::Keep, 1, 0},
+ {DiffOp::Remove, 1, 0}, {DiffOp::Keep, 1, 0}, {DiffOp::Add, 1, 8}, {DiffOp::Remove, 1, 0},
+ {DiffOp::Keep, 1, 0}, {DiffOp::Remove, 2, 0}, {DiffOp::Keep, 10, 0}} );
}};
UnitTest* UnitTest::list = nullptr;