File: levenshteintest.cpp

package info (click to toggle)
libkomparediff2 4%3A22.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,768 kB
  • sloc: cpp: 5,405; sh: 9; makefile: 3
file content (142 lines) | stat: -rw-r--r-- 5,492 bytes parent folder | download | duplicates (2)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
SPDX-FileCopyrightText: 2011 Dmitry Risenberg <dmitry.risenberg@gmail.com>

SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "levenshteintest.h"

#include <QTest>
#include "difference.h"
#include "differencestringpair.h"
#include "levenshteintable.h"
#include "stringlistpair.h"

using namespace Diff2;

namespace QTest {

template<> char* toString(const Marker& marker)
{
    QByteArray result = "Marker(";
    if (marker.type() == Marker::Start) {
        result += "Start, ";
    } else {
        result += "End, ";
    }
    result += QByteArray::number(marker.offset());
    result += ")";
    return qstrdup(result.data());
}

}

void LevenshteinTest::testFirstEmptyString()
{
    DifferenceString* string1 = new DifferenceString(QString("12345"));
    DifferenceString* string2 = new DifferenceString(QString());
    DifferenceStringPair* pair = new DifferenceStringPair(string1, string2);
    LevenshteinTable<DifferenceStringPair> table;
    table.createTable(pair);
    table.createListsOfMarkers();
    MarkerList markersFirstExpected;
    markersFirstExpected << new Marker(Marker::Start, 0) << new Marker(Marker::End, 5);
    for (int i = 0; i < markersFirstExpected.size(); ++i) {
        QCOMPARE(*string1->markerList()[i], *markersFirstExpected[i]);
    }
    MarkerList markersSecondExpected;
    markersSecondExpected << new Marker(Marker::Start, 0) << new Marker(Marker::End, 0);
    for (int i = 0; i < markersSecondExpected.size(); ++i) {
        QCOMPARE(*string2->markerList()[i], *markersSecondExpected[i]);
    }
}

void LevenshteinTest::testSecondEmptyString()
{
    DifferenceString* string1 = new DifferenceString(QString());
    DifferenceString* string2 = new DifferenceString(QString("12345"));
    DifferenceStringPair* pair = new DifferenceStringPair(string1, string2);
    LevenshteinTable<DifferenceStringPair> table;
    table.createTable(pair);
    table.createListsOfMarkers();
    MarkerList markersFirstExpected;
    markersFirstExpected << new Marker(Marker::Start, 0) << new Marker(Marker::End, 0);
    for (int i = 0; i < markersFirstExpected.size(); ++i) {
        QCOMPARE(*string1->markerList()[i], *markersFirstExpected[i]);
    }
    MarkerList markersSecondExpected;
    markersSecondExpected << new Marker(Marker::Start, 0) << new Marker(Marker::End, 5);
    for (int i = 0; i < markersSecondExpected.size(); ++i) {
        QCOMPARE(*string2->markerList()[i], *markersSecondExpected[i]);
    }
}


void LevenshteinTest::testDifferenceStrings()
{
    DifferenceString* string1 = new DifferenceString(QString("aaabcddefghik"));
    DifferenceString* string2 = new DifferenceString(QString("aabcefghijk"));
    DifferenceStringPair* pair = new DifferenceStringPair(string1, string2);
    LevenshteinTable<DifferenceStringPair> table;
    table.createTable(pair);
    table.createListsOfMarkers();
    MarkerList markersFirstExpected;
    markersFirstExpected << new Marker(Marker::Start, 2) << new Marker(Marker::End, 3) << new Marker(Marker::Start, 5) << new Marker(Marker::End, 7);
    for (int i = 0; i < markersFirstExpected.size(); ++i) {
        QCOMPARE(*string1->markerList()[i], *markersFirstExpected[i]);
    }
    MarkerList markersSecondExpected;
    markersSecondExpected << new Marker(Marker::Start, 9) << new Marker(Marker::End, 10);
    for (int i = 0; i < markersSecondExpected.size(); ++i) {
        QCOMPARE(*string2->markerList()[i], *markersSecondExpected[i]);
    }
}

void LevenshteinTest::testStringLists()
{
    QStringList list1;
    list1 << "delete1" << "line1" << "line2" << "line3" << "delete2" << "delete3" << "line4";
    QStringList list2;
    list2 << "line1" << "line2" << "line3" << "insert1" << "line4" << "insert2";
    StringListPair* pair = new StringListPair(list1, list2);
    LevenshteinTable<StringListPair> table;
    table.createTable(pair);
    table.createListsOfMarkers();

    MarkerList markersFirstExpected;
    markersFirstExpected << new Marker(Marker::Start, 0) << new Marker(Marker::End, 1) << new Marker(Marker::Start, 4) << new Marker(Marker::End, 6);
    for (int i = 0; i < markersFirstExpected.size(); ++i) {
        QCOMPARE(*pair->markerListFirst()[i], *markersFirstExpected[i]);
    }
    MarkerList markersSecondExpected;
    markersSecondExpected << new Marker(Marker::Start, 3) << new Marker(Marker::End, 4) << new Marker(Marker::Start, 5) << new Marker(Marker::End, 6);
    for (int i = 0; i < markersSecondExpected.size(); ++i) {
        QCOMPARE(*pair->markerListSecond()[i], *markersSecondExpected[i]);
    }
}

void LevenshteinTest::testSmth()
{
    QStringList list1;
    list1 << "insert1\n" << "newline1\n" << "newline2\n" << "insert2\n";;
    QStringList list2;
    list2 << "delete1\n" << "efgh\n" << "delete2\n";
    StringListPair* pair = new StringListPair(list1, list2);
    LevenshteinTable<StringListPair> table;
    table.createTable(pair);
    table.createListsOfMarkers();

    MarkerList markersFirstExpected;
    markersFirstExpected << new Marker(Marker::Start, 0) << new Marker(Marker::End, 4);
    for (int i = 0; i < markersFirstExpected.size(); ++i) {
        QCOMPARE(*pair->markerListFirst()[i], *markersFirstExpected[i]);
    }
    MarkerList markersSecondExpected;
    markersSecondExpected << new Marker(Marker::Start, 0)  << new Marker(Marker::End, 3);
    for (int i = 0; i < markersSecondExpected.size(); ++i) {
        QCOMPARE(*pair->markerListSecond()[i], *markersSecondExpected[i]);
    }
}


QTEST_GUILESS_MAIN(LevenshteinTest);