Skip to content

Commit cf4120d

Browse files
Refactor LCS implementation tests
1 parent 773fe7e commit cf4120d

File tree

3 files changed

+215
-154
lines changed

3 files changed

+215
-154
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
/*
3+
* This file is part of sebastian/diff.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace SebastianBergmann\Diff\LCS;
12+
13+
use PHPUnit\Framework\TestCase;
14+
15+
abstract class LongestCommonSubsequenceTest extends TestCase
16+
{
17+
/**
18+
* @var LongestCommonSubsequence
19+
*/
20+
private $implementation;
21+
22+
/**
23+
* @var string
24+
*/
25+
private $memory_limit;
26+
27+
/**
28+
* @var int[]
29+
*/
30+
private $stress_sizes = array(1, 2, 3, 100, 500, 1000, 2000);
31+
32+
protected function setUp()
33+
{
34+
$this->memory_limit = \ini_get('memory_limit');
35+
\ini_set('memory_limit', '256M');
36+
37+
$this->implementation = $this->createImplementation();
38+
}
39+
40+
/**
41+
* @return LongestCommonSubsequence
42+
*/
43+
protected abstract function createImplementation();
44+
45+
protected function tearDown()
46+
{
47+
\ini_set('memory_limit', $this->memory_limit);
48+
}
49+
50+
public function testBothEmpty()
51+
{
52+
$from = array();
53+
$to = array();
54+
$common = $this->implementation->calculate($from, $to);
55+
56+
$this->assertEquals(array(), $common);
57+
}
58+
59+
public function testIsStrictComparison()
60+
{
61+
$from = array(
62+
false, 0, 0.0, '', null, array(),
63+
true, 1, 1.0, 'foo', array('foo', 'bar'), array('foo' => 'bar')
64+
);
65+
$to = $from;
66+
$common = $this->implementation->calculate($from, $to);
67+
68+
$this->assertEquals($from, $common);
69+
70+
$to = array(
71+
false, false, false, false, false, false,
72+
true, true, true, true, true, true
73+
);
74+
$expected = array(
75+
false,
76+
true,
77+
);
78+
$common = $this->implementation->calculate($from, $to);
79+
80+
$this->assertEquals($expected, $common);
81+
}
82+
83+
public function testEqualSequences()
84+
{
85+
foreach ($this->stress_sizes as $size) {
86+
$range = \range(1, $size);
87+
$from = $range;
88+
$to = $range;
89+
$common = $this->implementation->calculate($from, $to);
90+
91+
$this->assertEquals($range, $common);
92+
}
93+
}
94+
95+
public function testDistinctSequences()
96+
{
97+
$from = array('A');
98+
$to = array('B');
99+
$common = $this->implementation->calculate($from, $to);
100+
$this->assertEquals(array(), $common);
101+
102+
$from = array('A', 'B', 'C');
103+
$to = array('D', 'E', 'F');
104+
$common = $this->implementation->calculate($from, $to);
105+
$this->assertEquals(array(), $common);
106+
107+
foreach ($this->stress_sizes as $size) {
108+
$from = \range(1, $size);
109+
$to = \range($size + 1, $size * 2);
110+
$common = $this->implementation->calculate($from, $to);
111+
$this->assertEquals(array(), $common);
112+
}
113+
}
114+
115+
public function testCommonSubsequence()
116+
{
117+
$from = array('A', 'C', 'E', 'F', 'G');
118+
$to = array('A', 'B', 'D', 'E', 'H');
119+
$expected = array('A', 'E');
120+
$common = $this->implementation->calculate($from, $to);
121+
$this->assertEquals($expected, $common);
122+
123+
$from = array('A', 'C', 'E', 'F', 'G');
124+
$to = array('B', 'C', 'D', 'E', 'F', 'H');
125+
$expected = array('C', 'E', 'F');
126+
$common = $this->implementation->calculate($from, $to);
127+
$this->assertEquals($expected, $common);
128+
129+
foreach ($this->stress_sizes as $size) {
130+
$from = $size < 2 ? array(1) : \range(1, $size + 1, 2);
131+
$to = $size < 3 ? array(1) : \range(1, $size + 1, 3);
132+
$expected = $size < 6 ? array(1) : \range(1, $size + 1, 6);
133+
$common = $this->implementation->calculate($from, $to);
134+
135+
$this->assertEquals($expected, $common);
136+
}
137+
}
138+
139+
public function testSingleElementSubsequenceAtStart()
140+
{
141+
foreach ($this->stress_sizes as $size) {
142+
$from = \range(1, $size);
143+
$to = \array_slice($from, 0, 1);
144+
$common = $this->implementation->calculate($from, $to);
145+
146+
$this->assertEquals($to, $common);
147+
}
148+
}
149+
150+
public function testSingleElementSubsequenceAtMiddle()
151+
{
152+
foreach ($this->stress_sizes as $size) {
153+
$from = \range(1, $size);
154+
$to = \array_slice($from, (int) $size / 2, 1);
155+
$common = $this->implementation->calculate($from, $to);
156+
157+
$this->assertEquals($to, $common);
158+
}
159+
}
160+
161+
public function testSingleElementSubsequenceAtEnd()
162+
{
163+
foreach ($this->stress_sizes as $size) {
164+
$from = \range(1, $size);
165+
$to = \array_slice($from, $size - 1, 1);
166+
$common = $this->implementation->calculate($from, $to);
167+
168+
$this->assertEquals($to, $common);
169+
}
170+
}
171+
172+
public function testReversedSequences()
173+
{
174+
$from = array('A', 'B');
175+
$to = array('B', 'A');
176+
$expected = array('A');
177+
$common = $this->implementation->calculate($from, $to);
178+
$this->assertEquals($expected, $common);
179+
180+
foreach ($this->stress_sizes as $size) {
181+
$from = \range(1, $size);
182+
$to = \array_reverse($from);
183+
$common = $this->implementation->calculate($from, $to);
184+
185+
$this->assertEquals(array(1), $common);
186+
}
187+
}
188+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/*
3+
* This file is part of sebastian/diff.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace SebastianBergmann\Diff\LCS;
12+
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* @covers SebastianBergmann\Diff\LCS\MemoryEfficientImplementation
17+
*/
18+
class MemoryEfficientImplementationTest extends LongestCommonSubsequenceTest
19+
{
20+
protected function createImplementation()
21+
{
22+
return new MemoryEfficientImplementation;
23+
}
24+
}

tests/LCS/TimeEfficientImplementationTest.php

Lines changed: 3 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -15,161 +15,10 @@
1515
/**
1616
* @covers SebastianBergmann\Diff\LCS\TimeEfficientImplementation
1717
*/
18-
class TimeEfficientImplementationTest extends TestCase
18+
class TimeEfficientImplementationTest extends LongestCommonSubsequenceTest
1919
{
20-
private $implementation;
21-
private $memory_limit;
22-
private $stress_sizes = array(1, 2, 3, 100, 500, 1000, 2000);
23-
24-
protected function setUp()
25-
{
26-
$this->memory_limit = \ini_get('memory_limit');
27-
\ini_set('memory_limit', '256M');
28-
29-
$this->implementation = new TimeEfficientImplementation;
30-
}
31-
32-
protected function tearDown()
33-
{
34-
\ini_set('memory_limit', $this->memory_limit);
35-
}
36-
37-
public function testBothEmpty()
38-
{
39-
$from = array();
40-
$to = array();
41-
$common = $this->implementation->calculate($from, $to);
42-
43-
$this->assertEquals(array(), $common);
44-
}
45-
46-
public function testIsStrictComparison()
20+
protected function createImplementation()
4721
{
48-
$from = array(
49-
false, 0, 0.0, '', null, array(),
50-
true, 1, 1.0, 'foo', array('foo', 'bar'), array('foo' => 'bar')
51-
);
52-
$to = $from;
53-
$common = $this->implementation->calculate($from, $to);
54-
55-
$this->assertEquals($from, $common);
56-
57-
$to = array(
58-
false, false, false, false, false, false,
59-
true, true, true, true, true, true
60-
);
61-
$expected = array(
62-
false,
63-
true,
64-
);
65-
$common = $this->implementation->calculate($from, $to);
66-
67-
$this->assertEquals($expected, $common);
68-
}
69-
70-
public function testEqualSequences()
71-
{
72-
foreach ($this->stress_sizes as $size) {
73-
$range = \range(1, $size);
74-
$from = $range;
75-
$to = $range;
76-
$common = $this->implementation->calculate($from, $to);
77-
78-
$this->assertEquals($range, $common);
79-
}
80-
}
81-
82-
public function testDistinctSequences()
83-
{
84-
$from = array('A');
85-
$to = array('B');
86-
$common = $this->implementation->calculate($from, $to);
87-
$this->assertEquals(array(), $common);
88-
89-
$from = array('A', 'B', 'C');
90-
$to = array('D', 'E', 'F');
91-
$common = $this->implementation->calculate($from, $to);
92-
$this->assertEquals(array(), $common);
93-
94-
foreach ($this->stress_sizes as $size) {
95-
$from = \range(1, $size);
96-
$to = \range($size + 1, $size * 2);
97-
$common = $this->implementation->calculate($from, $to);
98-
$this->assertEquals(array(), $common);
99-
}
100-
}
101-
102-
public function testCommonSubsequence()
103-
{
104-
$from = array('A', 'C', 'E', 'F', 'G');
105-
$to = array('A', 'B', 'D', 'E', 'H');
106-
$expected = array('A', 'E');
107-
$common = $this->implementation->calculate($from, $to);
108-
$this->assertEquals($expected, $common);
109-
110-
$from = array('A', 'C', 'E', 'F', 'G');
111-
$to = array('B', 'C', 'D', 'E', 'F', 'H');
112-
$expected = array('C', 'E', 'F');
113-
$common = $this->implementation->calculate($from, $to);
114-
$this->assertEquals($expected, $common);
115-
116-
foreach ($this->stress_sizes as $size) {
117-
$from = $size < 2 ? array(1) : \range(1, $size + 1, 2);
118-
$to = $size < 3 ? array(1) : \range(1, $size + 1, 3);
119-
$expected = $size < 6 ? array(1) : \range(1, $size + 1, 6);
120-
$common = $this->implementation->calculate($from, $to);
121-
122-
$this->assertEquals($expected, $common);
123-
}
124-
}
125-
126-
public function testSingleElementSubsequenceAtStart()
127-
{
128-
foreach ($this->stress_sizes as $size) {
129-
$from = \range(1, $size);
130-
$to = \array_slice($from, 0, 1);
131-
$common = $this->implementation->calculate($from, $to);
132-
133-
$this->assertEquals($to, $common);
134-
}
135-
}
136-
137-
public function testSingleElementSubsequenceAtMiddle()
138-
{
139-
foreach ($this->stress_sizes as $size) {
140-
$from = \range(1, $size);
141-
$to = \array_slice($from, (int) $size / 2, 1);
142-
$common = $this->implementation->calculate($from, $to);
143-
144-
$this->assertEquals($to, $common);
145-
}
146-
}
147-
148-
public function testSingleElementSubsequenceAtEnd()
149-
{
150-
foreach ($this->stress_sizes as $size) {
151-
$from = \range(1, $size);
152-
$to = \array_slice($from, $size - 1, 1);
153-
$common = $this->implementation->calculate($from, $to);
154-
155-
$this->assertEquals($to, $common);
156-
}
157-
}
158-
159-
public function testReversedSequences()
160-
{
161-
$from = array('A', 'B');
162-
$to = array('B', 'A');
163-
$expected = array('A');
164-
$common = $this->implementation->calculate($from, $to);
165-
$this->assertEquals($expected, $common);
166-
167-
foreach ($this->stress_sizes as $size) {
168-
$from = \range(1, $size);
169-
$to = \array_reverse($from);
170-
$common = $this->implementation->calculate($from, $to);
171-
172-
$this->assertEquals(array(1), $common);
173-
}
22+
return new TimeEfficientImplementation;
17423
}
17524
}

0 commit comments

Comments
 (0)