TimeEfficientImplementationTest.php (5472B)
1 <?php 2 /* 3 * This file is part of the Diff package. 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 * Some of these tests are volontary stressfull, in order to give some approximative benchmark hints. 17 */ 18 class TimeEfficientImplementationTest extends PHPUnit_Framework_TestCase 19 { 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() 47 { 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 } 174 } 175 }