2015-05-22 14:58:56 +02:00
|
|
|
#include "unit_tests.hh"
|
|
|
|
|
2012-03-21 20:27:36 +01:00
|
|
|
#include "assert.hh"
|
2015-05-13 00:41:35 +02:00
|
|
|
#include "diff.hh"
|
2015-05-22 14:58:56 +02:00
|
|
|
#include "utf8.hh"
|
|
|
|
#include "string.hh"
|
2012-03-21 20:27:36 +01:00
|
|
|
|
2015-05-22 14:58:56 +02:00
|
|
|
namespace Kakoune
|
2012-03-21 20:27:36 +01:00
|
|
|
{
|
2014-01-16 23:07:42 +01:00
|
|
|
|
2015-05-22 14:58:56 +02:00
|
|
|
UnitTest test_utf8{[]()
|
2012-10-22 01:05:56 +02:00
|
|
|
{
|
2015-05-22 14:58:56 +02:00
|
|
|
StringView str = "maïs mélange bientôt";
|
|
|
|
kak_assert(utf8::distance(std::begin(str), std::end(str)) == 20);
|
|
|
|
kak_assert(utf8::codepoint(std::begin(str) + 2, std::end(str)) == 0x00EF);
|
|
|
|
}};
|
2013-01-30 19:16:36 +01:00
|
|
|
|
2015-05-22 14:58:56 +02:00
|
|
|
UnitTest test_diff{[]()
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
2020-03-20 10:26:05 +01:00
|
|
|
struct Diff{DiffOp op; int len;};
|
2019-11-30 00:46:42 +01:00
|
|
|
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(),
|
2020-03-20 10:26:05 +01:00
|
|
|
[&](DiffOp op, int len) {
|
2019-11-30 00:46:42 +01:00
|
|
|
kak_assert(count < diffs.size());
|
|
|
|
auto& d = diffs.begin()[count++];
|
2020-03-20 10:26:05 +01:00
|
|
|
kak_assert(d.op == op and d.len == len);
|
2019-11-30 00:46:42 +01:00
|
|
|
});
|
|
|
|
kak_assert(count == diffs.size());
|
2015-05-14 14:57:03 +02:00
|
|
|
};
|
2020-02-18 10:20:09 +01:00
|
|
|
check_diff("a?", "!", {{DiffOp::Remove, 1}, {DiffOp::Add, 1}, {DiffOp::Remove, 1}});
|
|
|
|
check_diff("abcde", "cd", {{DiffOp::Remove, 2}, {DiffOp::Keep, 2}, {DiffOp::Remove, 1}});
|
2020-03-20 10:26:05 +01:00
|
|
|
check_diff("abcd", "cdef", {{DiffOp::Remove, 2}, {DiffOp::Keep, 2}, {DiffOp::Add, 2}});
|
2019-11-30 00:46:42 +01:00
|
|
|
|
|
|
|
check_diff("mais que fais la police", "mais ou va la police",
|
2020-03-20 10:26:05 +01:00
|
|
|
{{DiffOp::Keep, 5}, {DiffOp::Remove, 1}, {DiffOp::Add, 1}, {DiffOp::Keep, 1},
|
|
|
|
{DiffOp::Remove, 1}, {DiffOp::Keep, 1}, {DiffOp::Add, 1}, {DiffOp::Remove, 1},
|
2020-02-18 10:20:09 +01:00
|
|
|
{DiffOp::Keep, 1}, {DiffOp::Remove, 2}, {DiffOp::Keep, 10}} );
|
|
|
|
|
|
|
|
check_diff("abcdefghijk", "1cdef2hij34",
|
2020-03-20 10:26:05 +01:00
|
|
|
{{DiffOp::Remove, 2}, {DiffOp::Add, 1}, {DiffOp::Keep, 4}, {DiffOp::Remove, 1},
|
|
|
|
{DiffOp::Add, 1}, {DiffOp::Keep, 3}, {DiffOp::Add, 2}, {DiffOp::Remove, 1}});
|
2020-02-18 10:20:09 +01:00
|
|
|
|
2015-05-22 14:58:56 +02:00
|
|
|
}};
|
|
|
|
|
2015-05-24 23:34:05 +02:00
|
|
|
UnitTest* UnitTest::list = nullptr;
|
2015-05-13 00:41:35 +02:00
|
|
|
|
2015-05-24 23:34:05 +02:00
|
|
|
void UnitTest::run_all_tests()
|
2012-03-21 20:27:36 +01:00
|
|
|
{
|
2015-05-24 23:34:05 +02:00
|
|
|
for (const UnitTest* test = UnitTest::list; test; test = test->next)
|
2015-05-22 14:58:56 +02:00
|
|
|
test->func();
|
|
|
|
}
|
|
|
|
|
2012-03-21 20:27:36 +01:00
|
|
|
}
|