3 * This file is part of sebastian/diff.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace SebastianBergmann\Diff\LCS;
13 use PHPUnit\Framework\TestCase;
15 abstract class LongestCommonSubsequenceTest extends TestCase
18 * @var LongestCommonSubsequence
20 private $implementation;
30 private $stress_sizes = array(1, 2, 3, 100, 500, 1000, 2000);
32 protected function setUp()
34 $this->memoryLimit = \ini_get('memory_limit');
35 \ini_set('memory_limit', '256M');
37 $this->implementation = $this->createImplementation();
41 * @return LongestCommonSubsequence
43 abstract protected function createImplementation();
45 protected function tearDown()
47 \ini_set('memory_limit', $this->memoryLimit);
50 public function testBothEmpty()
54 $common = $this->implementation->calculate($from, $to);
56 $this->assertEquals(array(), $common);
59 public function testIsStrictComparison()
62 false, 0, 0.0, '', null, array(),
63 true, 1, 1.0, 'foo', array('foo', 'bar'), array('foo' => 'bar')
66 $common = $this->implementation->calculate($from, $to);
68 $this->assertEquals($from, $common);
71 false, false, false, false, false, false,
72 true, true, true, true, true, true
80 $common = $this->implementation->calculate($from, $to);
82 $this->assertEquals($expected, $common);
85 public function testEqualSequences()
87 foreach ($this->stress_sizes as $size) {
88 $range = \range(1, $size);
91 $common = $this->implementation->calculate($from, $to);
93 $this->assertEquals($range, $common);
97 public function testDistinctSequences()
101 $common = $this->implementation->calculate($from, $to);
102 $this->assertEquals(array(), $common);
104 $from = array('A', 'B', 'C');
105 $to = array('D', 'E', 'F');
106 $common = $this->implementation->calculate($from, $to);
107 $this->assertEquals(array(), $common);
109 foreach ($this->stress_sizes as $size) {
110 $from = \range(1, $size);
111 $to = \range($size + 1, $size * 2);
112 $common = $this->implementation->calculate($from, $to);
113 $this->assertEquals(array(), $common);
117 public function testCommonSubsequence()
119 $from = array('A', 'C', 'E', 'F', 'G');
120 $to = array('A', 'B', 'D', 'E', 'H');
121 $expected = array('A', 'E');
122 $common = $this->implementation->calculate($from, $to);
123 $this->assertEquals($expected, $common);
125 $from = array('A', 'C', 'E', 'F', 'G');
126 $to = array('B', 'C', 'D', 'E', 'F', 'H');
127 $expected = array('C', 'E', 'F');
128 $common = $this->implementation->calculate($from, $to);
129 $this->assertEquals($expected, $common);
131 foreach ($this->stress_sizes as $size) {
132 $from = $size < 2 ? array(1) : \range(1, $size + 1, 2);
133 $to = $size < 3 ? array(1) : \range(1, $size + 1, 3);
134 $expected = $size < 6 ? array(1) : \range(1, $size + 1, 6);
135 $common = $this->implementation->calculate($from, $to);
137 $this->assertEquals($expected, $common);
141 public function testSingleElementSubsequenceAtStart()
143 foreach ($this->stress_sizes as $size) {
144 $from = \range(1, $size);
145 $to = \array_slice($from, 0, 1);
146 $common = $this->implementation->calculate($from, $to);
148 $this->assertEquals($to, $common);
152 public function testSingleElementSubsequenceAtMiddle()
154 foreach ($this->stress_sizes as $size) {
155 $from = \range(1, $size);
156 $to = \array_slice($from, (int) $size / 2, 1);
157 $common = $this->implementation->calculate($from, $to);
159 $this->assertEquals($to, $common);
163 public function testSingleElementSubsequenceAtEnd()
165 foreach ($this->stress_sizes as $size) {
166 $from = \range(1, $size);
167 $to = \array_slice($from, $size - 1, 1);
168 $common = $this->implementation->calculate($from, $to);
170 $this->assertEquals($to, $common);
174 public function testReversedSequences()
176 $from = array('A', 'B');
177 $to = array('B', 'A');
178 $expected = array('A');
179 $common = $this->implementation->calculate($from, $to);
180 $this->assertEquals($expected, $common);
182 foreach ($this->stress_sizes as $size) {
183 $from = \range(1, $size);
184 $to = \array_reverse($from);
185 $common = $this->implementation->calculate($from, $to);
187 $this->assertEquals(array(1), $common);
191 public function testStrictTypeCalculate()
193 $diff = $this->implementation->calculate(array('5'), array('05'));
195 $this->assertInternalType('array', $diff);
196 $this->assertCount(0, $diff);