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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
#include "diff.h"
#include <library/cpp/testing/unittest/registar.h>
using namespace NDiff;
struct TDiffTester {
TStringStream Res;
TVector<TChunk<char>> Chunks;
TStringBuf Special(const TStringBuf& str) const {
return str;
}
TStringBuf Common(const TConstArrayRef<const char>& str) const {
return TStringBuf(str.begin(), str.end());
}
TStringBuf Left(const TConstArrayRef<const char>& str) const {
return TStringBuf(str.begin(), str.end());
}
TStringBuf Right(const TConstArrayRef<const char>& str) const {
return TStringBuf(str.begin(), str.end());
}
void Test(const TStringBuf& a, const TStringBuf& b, const TString& delims = " \t\n") {
Chunks.clear();
InlineDiff(Chunks, a, b, delims);
Res.clear();
PrintChunks(Res, *this, Chunks);
}
const TString& Result() const {
return Res.Str();
}
};
Y_UNIT_TEST_SUITE(DiffTokens) {
Y_UNIT_TEST(ReturnValue) {
TVector<TChunk<char>> res;
UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "aaa", "aaa"), 0);
UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "aaa", "aa"), 1);
UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "aaa", "a"), 2);
UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "aaa", "abc"), 2);
UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "aaa", "aba"), 1);
UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "", "aba"), 3);
UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "aaa", "aaaa"), 1);
UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "abc", "xyz"), 3);
}
Y_UNIT_TEST(EqualStringsOneToken) {
TDiffTester tester;
tester.Test("aaa", "aaa");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "aaa");
}
Y_UNIT_TEST(NonCrossingStringsOneToken) {
TDiffTester tester;
tester.Test("aaa", "bbb");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|bbb)");
tester.Test("aaa", "bbbb");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|bbbb)");
}
Y_UNIT_TEST(Simple) {
TDiffTester tester;
tester.Test("aaa", "abb", "");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "a(aa|bb)");
tester.Test("aac", "abc", "");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "a(a|b)c");
tester.Test("123", "133", "");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "1(2|3)3");
tester.Test("[1, 2, 3]", "[1, 3, 3]", "");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "[1, (2|3), 3]");
}
Y_UNIT_TEST(CommonCharOneToken) {
TDiffTester tester;
tester.Test("abcde", "accfg");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(abcde|accfg)");
}
Y_UNIT_TEST(EqualStringsTwoTokens) {
TDiffTester tester;
TStringBuf str("aaa bbb");
tester.Test(str, str);
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "aaa bbb");
}
Y_UNIT_TEST(NonCrossingStringsTwoTokens) {
TDiffTester tester;
tester.Test("aaa bbb", "ccc ddd");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|ccc) (bbb|ddd)");
tester.Test("aaa bbb", "c d");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|c) (bbb|d)");
}
Y_UNIT_TEST(SimpleTwoTokens) {
TDiffTester tester;
tester.Test("aaa ccd", "abb cce");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|abb) (ccd|cce)");
tester.Test("aac cbb", "aa bb");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aac|aa) (cbb|bb)");
}
Y_UNIT_TEST(MixedTwoTokens) {
TDiffTester tester;
tester.Test("aaa bbb", "bbb aaa");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(|bbb )aaa( bbb|)");
tester.Test("aaa bbb", " bbb aaa");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|) bbb(| aaa)");
tester.Test(" aaa bbb ", " bbb aaa ");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(| bbb) aaa (bbb |)");
tester.Test("aaa bb", " bbb aa");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|) (bb|bbb aa)");
}
Y_UNIT_TEST(TwoTokensInOneString) {
TDiffTester tester;
tester.Test("aaa bbb", "aaa");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "aaa( bbb|)");
tester.Test("aaa bbb", "aaa ");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "aaa (bbb|)");
tester.Test("aaa bbb", " bbb");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|) bbb");
tester.Test("aaa bbb", "bbb");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa |)bbb");
}
Y_UNIT_TEST(Multiline) {
TDiffTester tester;
tester.Test("aaa\nabc\nbbb", "aaa\nacc\nbbb");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "aaa\n(abc|acc)\nbbb");
tester.Test("aaa\nabc\nbbb", "aaa\nac\nbbb");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "aaa\n(abc|ac)\nbbb");
}
Y_UNIT_TEST(DifferentDelimiters) {
TDiffTester tester;
tester.Test("aaa bbb", "aaa\tbbb");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "aaa( |\t)bbb");
tester.Test(" aaa\tbbb\n", "\taaa\nbbb ");
//~ Cerr << tester.Result() << Endl;
UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "( |\t)aaa(\t|\n)bbb(\n| )");
}
}
|