summaryrefslogtreecommitdiff
path: root/src/unit_tests.cc
blob: 54269074dff0a84af54813cb877655dc6213a1b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "unit_tests.hh"

#include "assert.hh"
#include "diff.hh"
#include "utf8.hh"
#include "string.hh"

namespace Kakoune
{

UnitTest test_utf8{[]()
{
    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);
}};

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;
    };

    {
        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);
    }
}};

UnitTest* UnitTest::list = nullptr;

void UnitTest::run_all_tests()
{
    for (const UnitTest* test = UnitTest::list; test; test = test->next)
        test->func();
}

}