|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* (c) Steve Nebes <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace SN\RangeDifferencer; |
|
12
|
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
use SN\RangeDifferencer\Tag\TextAtom; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* TextLineLCS Tests |
|
18
|
|
|
*/ |
|
19
|
|
|
class DiffTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
public function testLineAdditions(): void |
|
22
|
|
|
{ |
|
23
|
|
|
$s1 = 'abc' . PHP_EOL . 'def' . PHP_EOL . 'xyz'; |
|
24
|
|
|
$s2 = 'abc' . PHP_EOL . 'def' . PHP_EOL . '123' . PHP_EOL . 'xyz'; |
|
25
|
|
|
|
|
26
|
|
|
$l1 = TextLineLCS::getTextLines($s1); |
|
27
|
|
|
$l2 = TextLineLCS::getTextLines($s2); |
|
28
|
|
|
$lcs = new TextLineLCS($l1, $l2); |
|
29
|
|
|
$lcs->longestCommonSubsequence(); |
|
30
|
|
|
|
|
31
|
|
|
/** @var TextLine[][] $result */ |
|
32
|
|
|
$result = $lcs->getResult(); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertSame(0, $result[0][0]->getLineNumber()); |
|
35
|
|
|
$this->assertSame(0, $result[1][0]->getLineNumber()); |
|
36
|
|
|
$this->assertSame(1, $result[0][1]->getLineNumber()); |
|
37
|
|
|
$this->assertSame(1, $result[1][1]->getLineNumber()); |
|
38
|
|
|
$this->assertSame(2, $result[0][2]->getLineNumber()); |
|
39
|
|
|
$this->assertSame(3, $result[1][2]->getLineNumber()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testLineDeletion(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$s1 = 'abc' . PHP_EOL . 'def' . PHP_EOL . '123' . PHP_EOL . 'xyz'; |
|
45
|
|
|
$s2 = 'abc' . PHP_EOL . 'def' . PHP_EOL . 'xyz'; |
|
46
|
|
|
|
|
47
|
|
|
$l1 = TextLineLCS::getTextLines($s1); |
|
48
|
|
|
$l2 = TextLineLCS::getTextLines($s2); |
|
49
|
|
|
$lcs = new TextLineLCS($l1, $l2); |
|
50
|
|
|
$lcs->longestCommonSubsequence(); |
|
51
|
|
|
|
|
52
|
|
|
/** @var TextLine[][] $result */ |
|
53
|
|
|
$result = $lcs->getResult(); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertTrue(\count($result[0]) === \count($result[1])); |
|
56
|
|
|
|
|
57
|
|
|
for ($i = 0; $i < \count($result[0]); $i++) { |
|
|
|
|
|
|
58
|
|
|
$this->assertTrue($result[0][$i]->isSameText($result[1][$i])); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->assertSame(0, $result[0][0]->getLineNumber()); |
|
62
|
|
|
$this->assertSame(1, $result[0][1]->getLineNumber()); |
|
63
|
|
|
$this->assertSame(3, $result[0][2]->getLineNumber()); |
|
64
|
|
|
$this->assertSame(0, $result[1][0]->getLineNumber()); |
|
65
|
|
|
$this->assertSame(1, $result[1][1]->getLineNumber()); |
|
66
|
|
|
$this->assertSame(2, $result[1][2]->getLineNumber()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testLineAppendEnd(): void |
|
70
|
|
|
{ |
|
71
|
|
|
$s1 = 'abc' . PHP_EOL . 'def'; |
|
72
|
|
|
$s2 = 'abc' . PHP_EOL . 'def' . PHP_EOL . '123'; |
|
73
|
|
|
|
|
74
|
|
|
$l1 = TextLineLCS::getTextLines($s1); |
|
75
|
|
|
$l2 = TextLineLCS::getTextLines($s2); |
|
76
|
|
|
$lcs = new TextLineLCS($l1, $l2); |
|
77
|
|
|
$lcs->longestCommonSubsequence(); |
|
78
|
|
|
|
|
79
|
|
|
/** @var TextLine[][] $result */ |
|
80
|
|
|
$result = $lcs->getResult(); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertSame(2, \count($result[0])); |
|
83
|
|
|
$this->assertSame(2, \count($result[1])); |
|
84
|
|
|
|
|
85
|
|
|
for ($i = 0; $i < \count($result[0]); $i++) { |
|
|
|
|
|
|
86
|
|
|
$this->assertTrue($result[0][$i]->isSameText($result[1][$i])); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->assertSame(0, $result[0][0]->getLineNumber()); |
|
90
|
|
|
$this->assertSame(0, $result[1][0]->getLineNumber()); |
|
91
|
|
|
$this->assertSame(1, $result[0][1]->getLineNumber()); |
|
92
|
|
|
$this->assertSame(1, $result[1][1]->getLineNumber()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testLineDeleteEnd(): void |
|
96
|
|
|
{ |
|
97
|
|
|
$s1 = 'abc' . PHP_EOL . 'def' . PHP_EOL . '123'; |
|
98
|
|
|
$s2 = 'abc' . PHP_EOL . 'def'; |
|
99
|
|
|
|
|
100
|
|
|
$l1 = TextLineLCS::getTextLines($s1); |
|
101
|
|
|
$l2 = TextLineLCS::getTextLines($s2); |
|
102
|
|
|
$lcs = new TextLineLCS($l1, $l2); |
|
103
|
|
|
$lcs->longestCommonSubsequence(); |
|
104
|
|
|
|
|
105
|
|
|
/** @var TextLine[][] $result */ |
|
106
|
|
|
$result = $lcs->getResult(); |
|
107
|
|
|
|
|
108
|
|
|
$this->assertSame(2, \count($result[0])); |
|
109
|
|
|
$this->assertSame(2, \count($result[1])); |
|
110
|
|
|
|
|
111
|
|
|
for ($i = 0; $i < \count($result[0]); $i++) { |
|
|
|
|
|
|
112
|
|
|
$this->assertTrue($result[0][$i]->isSameText($result[1][$i])); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$this->assertSame(0, $result[0][0]->getLineNumber()); |
|
116
|
|
|
$this->assertSame(0, $result[1][0]->getLineNumber()); |
|
117
|
|
|
$this->assertSame(1, $result[0][1]->getLineNumber()); |
|
118
|
|
|
$this->assertSame(1, $result[1][1]->getLineNumber()); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function testLineAppendStart(): void |
|
122
|
|
|
{ |
|
123
|
|
|
$s1 = 'abc' . PHP_EOL . 'def'; |
|
124
|
|
|
$s2 = '123' . PHP_EOL . 'abc' . PHP_EOL . 'def'; |
|
125
|
|
|
|
|
126
|
|
|
$l1 = TextLineLCS::getTextLines($s1); |
|
127
|
|
|
$l2 = TextLineLCS::getTextLines($s2); |
|
128
|
|
|
$lcs = new TextLineLCS($l1, $l2); |
|
129
|
|
|
$lcs->longestCommonSubsequence(); |
|
130
|
|
|
|
|
131
|
|
|
/** @var TextLine[][] $result */ |
|
132
|
|
|
$result = $lcs->getResult(); |
|
133
|
|
|
|
|
134
|
|
|
$this->assertSame(2, \count($result[0])); |
|
135
|
|
|
$this->assertSame(2, \count($result[1])); |
|
136
|
|
|
|
|
137
|
|
|
for ($i = 0; $i < \count($result[0]); $i++) { |
|
|
|
|
|
|
138
|
|
|
$this->assertTrue($result[0][$i]->isSameText($result[1][$i])); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$this->assertSame(0, $result[0][0]->getLineNumber()); |
|
142
|
|
|
$this->assertSame(1, $result[1][0]->getLineNumber()); |
|
143
|
|
|
$this->assertSame(1, $result[0][1]->getLineNumber()); |
|
144
|
|
|
$this->assertSame(2, $result[1][1]->getLineNumber()); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function testLineDeleteStart(): void |
|
148
|
|
|
{ |
|
149
|
|
|
$s1 = '123' . PHP_EOL . 'abc' . PHP_EOL . 'def'; |
|
150
|
|
|
$s2 = 'abc' . PHP_EOL . 'def'; |
|
151
|
|
|
|
|
152
|
|
|
$l1 = TextLineLCS::getTextLines($s1); |
|
153
|
|
|
$l2 = TextLineLCS::getTextLines($s2); |
|
154
|
|
|
$lcs = new TextLineLCS($l1, $l2); |
|
155
|
|
|
$lcs->longestCommonSubsequence(); |
|
156
|
|
|
|
|
157
|
|
|
/** @var TextLine[][] $result */ |
|
158
|
|
|
$result = $lcs->getResult(); |
|
159
|
|
|
|
|
160
|
|
|
$this->assertSame(2, \count($result[0])); |
|
161
|
|
|
$this->assertSame(2, \count($result[1])); |
|
162
|
|
|
|
|
163
|
|
|
for ($i = 0; $i < \count($result[0]); $i++) { |
|
|
|
|
|
|
164
|
|
|
$this->assertTrue($result[0][$i]->isSameText($result[1][$i])); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$this->assertSame(1, $result[0][0]->getLineNumber()); |
|
168
|
|
|
$this->assertSame(0, $result[1][0]->getLineNumber()); |
|
169
|
|
|
$this->assertSame(2, $result[0][1]->getLineNumber()); |
|
170
|
|
|
$this->assertSame(1, $result[1][1]->getLineNumber()); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function testEmtpy(): void |
|
174
|
|
|
{ |
|
175
|
|
|
$l1 = TextLineLCS::getTextLines('abc'); |
|
176
|
|
|
$lcs = new TextLineLCS($l1, []); |
|
177
|
|
|
$lcs->longestCommonSubsequence(); |
|
178
|
|
|
|
|
179
|
|
|
$this->assertTrue(true); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function testLong(): void |
|
183
|
|
|
{ |
|
184
|
|
|
$s1 = str_repeat('a' . PHP_EOL, 100); |
|
185
|
|
|
$l1 = TextLineLCS::getTextLines($s1); |
|
186
|
|
|
$l2 = TextLineLCS::getTextLines($s1 . 'test'); |
|
187
|
|
|
$lcs = new TextLineLCS($l1, $l2); |
|
188
|
|
|
$lcs->longestCommonSubsequence(); |
|
189
|
|
|
|
|
190
|
|
|
$this->assertTrue(true); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function testOneDiff(): void |
|
194
|
|
|
{ |
|
195
|
|
|
$s1 = str_repeat('a' . PHP_EOL, 100); |
|
196
|
|
|
$l1 = TextLineLCS::getTextLines($s1); |
|
197
|
|
|
$l2 = TextLineLCS::getTextLines($s1); |
|
198
|
|
|
$l2[50] = new TextLine(50, 'diff'); |
|
199
|
|
|
$lcs = new TextLineLCS($l1, $l2); |
|
200
|
|
|
$lcs->longestCommonSubsequence(); |
|
201
|
|
|
|
|
202
|
|
|
$this->assertTrue(true); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: