MyersDiffTest::testFirstEmpty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Fisharebest\Tests\Algorithm;
4
5
use Fisharebest\Algorithm\MyersDiff;
6
7
/**
8
 * @author    Greg Roach <[email protected]>
9
 * @copyright (c) 2021 Greg Roach <[email protected]>
10
 * @license   GPL-3.0+
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation, either version 3 of the License, or
15
 * (at your option) any later version.
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU General Public License for more details.
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses>.
22
 */
23
class MyersDiffTest extends BaseTestCase
24
{
25
    /**
26
     * Test empty sequences.
27
     *
28
     * @return void
29
     *
30
     * @covers \Fisharebest\Algorithm\MyersDiff
31
     */
32
    public function testBothEmpty()
33
    {
34
        $algorithm = new MyersDiff();
35
        $x = array();
36
        $y = array();
37
        $diff = array();
38
39
        $this->assertSame($diff, $algorithm->calculate($x, $y));
40
    }
41
42
    /**
43
     * Test one empty sequence.
44
     *
45
     * @return void
46
     *
47
     * @covers \Fisharebest\Algorithm\MyersDiff
48
     */
49
    public function testFirstEmpty()
50
    {
51
        $algorithm = new MyersDiff();
52
        $x = array();
53
        $y = array('a', 'b', 'c');
54
        $diff = array(
55
            array('a', MyersDiff::INSERT),
56
            array('b', MyersDiff::INSERT),
57
            array('c', MyersDiff::INSERT),
58
        );
59
60
        $this->assertSame($diff, $algorithm->calculate($x, $y));
61
    }
62
63
    /**
64
     * Test one empty sequence.
65
     *
66
     * @return void
67
     *
68
     * @covers \Fisharebest\Algorithm\MyersDiff
69
     */
70
    public function testSecondEmpty()
71
    {
72
        $algorithm = new MyersDiff();
73
        $x = array('a', 'b', 'c');
74
        $y = array();
75
        $diff = array(
76
            array('a', MyersDiff::DELETE),
77
            array('b', MyersDiff::DELETE),
78
            array('c', MyersDiff::DELETE),
79
        );
80
81
        $this->assertSame($diff, $algorithm->calculate($x, $y));
82
    }
83
84
    /**
85
     * Test identical sequences containing one token.
86
     *
87
     * @return void
88
     *
89
     * @covers \Fisharebest\Algorithm\MyersDiff
90
     */
91
    public function testIdenticalOne()
92
    {
93
        $algorithm = new MyersDiff();
94
        $x = array('a');
95
        $y = array('a');
96
        $diff = array(
97
            array('a', MyersDiff::KEEP),
98
        );
99
100
        $this->assertSame($diff, $algorithm->calculate($x, $y));
101
    }
102
103
    /**
104
     * Test identical sequences containing two tokens.
105
     *
106
     * @return void
107
     *
108
     * @covers \Fisharebest\Algorithm\MyersDiff
109
     */
110
    public function testIdenticalTwo()
111
    {
112
        $algorithm = new MyersDiff();
113
        $x = array('a', 'b');
114
        $y = array('a', 'b');
115
        $diff = array(
116
            array('a', MyersDiff::KEEP),
117
            array('b', MyersDiff::KEEP),
118
        );
119
120
        $this->assertSame($diff, $algorithm->calculate($x, $y));
121
    }
122
123
    /**
124
     * Test identical sequences containing three tokens.
125
     *
126
     * @return void
127
     *
128
     * @covers \Fisharebest\Algorithm\MyersDiff
129
     */
130
    public function testIdenticalThree()
131
    {
132
        $algorithm = new MyersDiff();
133
        $x = array('a', 'b', 'c');
134
        $y = array('a', 'b', 'c');
135
        $diff = array(
136
            array('a', MyersDiff::KEEP),
137
            array('b', MyersDiff::KEEP),
138
            array('c', MyersDiff::KEEP),
139
        );
140
141
        $this->assertSame($diff, $algorithm->calculate($x, $y));
142
    }
143
144
    /**
145
     * Test different strings containing one token.
146
     *
147
     * @return void
148
     *
149
     * @covers \Fisharebest\Algorithm\MyersDiff
150
     */
151
    public function testSingleDifferentONe()
152
    {
153
        $algorithm = new MyersDiff();
154
        $x = array('a');
155
        $y = array('x');
156
        $diff = array(
157
            array('a', MyersDiff::DELETE),
158
            array('x', MyersDiff::INSERT),
159
        );
160
161
        $this->assertSame($diff, $algorithm->calculate($x, $y));
162
    }
163
164
    /**
165
     * Test different strings containing two token.
166
     *
167
     * @return void
168
     *
169
     * @covers \Fisharebest\Algorithm\MyersDiff
170
     */
171
    public function testSingleDifferentTwo()
172
    {
173
        $algorithm = new MyersDiff();
174
        $x = array('a', 'b');
175
        $y = array('x', 'y');
176
        $diff = array(
177
            array('a', MyersDiff::DELETE),
178
            array('b', MyersDiff::DELETE),
179
            array('x', MyersDiff::INSERT),
180
            array('y', MyersDiff::INSERT),
181
        );
182
183
        $this->assertSame($diff, $algorithm->calculate($x, $y));
184
    }
185
186
    /**
187
     * Test different strings containing three token.
188
     *
189
     * @return void
190
     *
191
     * @covers \Fisharebest\Algorithm\MyersDiff
192
     */
193
    public function testSingleDifferentThree()
194
    {
195
        $algorithm = new MyersDiff();
196
        $x = array('a', 'b', 'c');
197
        $y = array('x', 'y', 'z');
198
        $diff = array(
199
            array('a', MyersDiff::DELETE),
200
            array('b', MyersDiff::DELETE),
201
            array('c', MyersDiff::DELETE),
202
            array('x', MyersDiff::INSERT),
203
            array('y', MyersDiff::INSERT),
204
            array('z', MyersDiff::INSERT),
205
        );
206
207
        $this->assertSame($diff, $algorithm->calculate($x, $y));
208
    }
209
210
    /**
211
     * Test two non-empty sequences.
212
     *
213
     * @return void
214
     *
215
     * @covers \Fisharebest\Algorithm\MyersDiff
216
     */
217
    public function testBothNonEmpty()
218
    {
219
        $algorithm = new MyersDiff();
220
        $x = array('a', 'b', 'c', 'a', 'b', 'b', 'a');
221
        $y = array('c', 'b', 'a', 'b', 'a', 'c');
222
        $diff = array(
223
            array('a', MyersDiff::DELETE),
224
            array('b', MyersDiff::DELETE),
225
            array('c', MyersDiff::KEEP),
226
            array('b', MyersDiff::INSERT),
227
            array('a', MyersDiff::KEEP),
228
            array('b', MyersDiff::KEEP),
229
            array('b', MyersDiff::DELETE),
230
            array('a', MyersDiff::KEEP),
231
            array('c', MyersDiff::INSERT),
232
        );
233
234
        $this->assertSame($diff, $algorithm->calculate($x, $y));
235
    }
236
237
    /**
238
     * Test delete-before-insert.  Delete/insert gives the same
239
     * result as insert/delete.  Our algorithm consistently
240
     * deletes first.
241
     *
242
     * @return void
243
     *
244
     * @covers \Fisharebest\Algorithm\MyersDiff
245
     */
246
    public function testDeleteBeforeInsert()
247
    {
248
        $algorithm = new MyersDiff();
249
        $x = array('a', 'b', 'c');
250
        $y = array('a', 'd', 'c');
251
        $diff = array(
252
            array('a', MyersDiff::KEEP),
253
            array('b', MyersDiff::DELETE),
254
            array('d', MyersDiff::INSERT),
255
            array('c', MyersDiff::KEEP),
256
        );
257
258
        $this->assertSame($diff, $algorithm->calculate($x, $y));
259
    }
260
261
    /**
262
     * Test custom token comparison.
263
     *
264
     * @return void
265
     *
266
     * @covers \Fisharebest\Algorithm\MyersDiff
267
     */
268
    public function testCustomCompare()
269
    {
270
        $algorithm = new MyersDiff();
271
        $ignorecase = function ($x, $y) {
272
            return strtolower($x) === strtolower($y);
273
        };
274
        $x = array('a', 'b', 'c');
275
        $y = array('A', 'B', 'C');
276
        $diff = array(
277
            array('a', MyersDiff::KEEP),
278
            array('b', MyersDiff::KEEP),
279
            array('c', MyersDiff::KEEP),
280
        );
281
282
        $this->assertSame($diff, $algorithm->calculate($x, $y, $ignorecase));
283
    }
284
}
285