TimeEfficientImplementationTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. namespace SebastianBergmann\Diff\LCS;
  11. use PHPUnit_Framework_TestCase;
  12. /**
  13. * Some of these tests are volontary stressfull, in order to give some approximative benchmark hints.
  14. */
  15. class TimeEfficientImplementationTest extends PHPUnit_Framework_TestCase
  16. {
  17. private $implementation;
  18. private $memory_limit;
  19. private $stress_sizes = array(1, 2, 3, 100, 500, 1000, 2000);
  20. protected function setUp()
  21. {
  22. $this->memory_limit = ini_get('memory_limit');
  23. ini_set('memory_limit', '256M');
  24. $this->implementation = new TimeEfficientImplementation;
  25. }
  26. protected function tearDown()
  27. {
  28. ini_set('memory_limit', $this->memory_limit);
  29. }
  30. public function testBothEmpty()
  31. {
  32. $from = array();
  33. $to = array();
  34. $common = $this->implementation->calculate($from, $to);
  35. $this->assertEquals(array(), $common);
  36. }
  37. public function testIsStrictComparison()
  38. {
  39. $from = array(
  40. false, 0, 0.0, '', null, array(),
  41. true, 1, 1.0, 'foo', array('foo', 'bar'), array('foo' => 'bar')
  42. );
  43. $to = $from;
  44. $common = $this->implementation->calculate($from, $to);
  45. $this->assertEquals($from, $common);
  46. $to = array(
  47. false, false, false, false, false, false,
  48. true, true, true, true, true, true
  49. );
  50. $expected = array(
  51. false,
  52. true,
  53. );
  54. $common = $this->implementation->calculate($from, $to);
  55. $this->assertEquals($expected, $common);
  56. }
  57. public function testEqualSequences()
  58. {
  59. foreach ($this->stress_sizes as $size) {
  60. $range = range(1, $size);
  61. $from = $range;
  62. $to = $range;
  63. $common = $this->implementation->calculate($from, $to);
  64. $this->assertEquals($range, $common);
  65. }
  66. }
  67. public function testDistinctSequences()
  68. {
  69. $from = array('A');
  70. $to = array('B');
  71. $common = $this->implementation->calculate($from, $to);
  72. $this->assertEquals(array(), $common);
  73. $from = array('A', 'B', 'C');
  74. $to = array('D', 'E', 'F');
  75. $common = $this->implementation->calculate($from, $to);
  76. $this->assertEquals(array(), $common);
  77. foreach ($this->stress_sizes as $size) {
  78. $from = range(1, $size);
  79. $to = range($size + 1, $size * 2);
  80. $common = $this->implementation->calculate($from, $to);
  81. $this->assertEquals(array(), $common);
  82. }
  83. }
  84. public function testCommonSubsequence()
  85. {
  86. $from = array('A', 'C', 'E', 'F', 'G');
  87. $to = array('A', 'B', 'D', 'E', 'H');
  88. $expected = array('A', 'E');
  89. $common = $this->implementation->calculate($from, $to);
  90. $this->assertEquals($expected, $common);
  91. $from = array('A', 'C', 'E', 'F', 'G');
  92. $to = array('B', 'C', 'D', 'E', 'F', 'H');
  93. $expected = array('C', 'E', 'F');
  94. $common = $this->implementation->calculate($from, $to);
  95. $this->assertEquals($expected, $common);
  96. foreach ($this->stress_sizes as $size) {
  97. $from = $size < 2 ? array(1) : range(1, $size + 1, 2);
  98. $to = $size < 3 ? array(1) : range(1, $size + 1, 3);
  99. $expected = $size < 6 ? array(1) : range(1, $size + 1, 6);
  100. $common = $this->implementation->calculate($from, $to);
  101. $this->assertEquals($expected, $common);
  102. }
  103. }
  104. public function testSingleElementSubsequenceAtStart()
  105. {
  106. foreach ($this->stress_sizes as $size) {
  107. $from = range(1, $size);
  108. $to = array_slice($from, 0, 1);
  109. $common = $this->implementation->calculate($from, $to);
  110. $this->assertEquals($to, $common);
  111. }
  112. }
  113. public function testSingleElementSubsequenceAtMiddle()
  114. {
  115. foreach ($this->stress_sizes as $size) {
  116. $from = range(1, $size);
  117. $to = array_slice($from, (int) $size / 2, 1);
  118. $common = $this->implementation->calculate($from, $to);
  119. $this->assertEquals($to, $common);
  120. }
  121. }
  122. public function testSingleElementSubsequenceAtEnd()
  123. {
  124. foreach ($this->stress_sizes as $size) {
  125. $from = range(1, $size);
  126. $to = array_slice($from, $size - 1, 1);
  127. $common = $this->implementation->calculate($from, $to);
  128. $this->assertEquals($to, $common);
  129. }
  130. }
  131. public function testReversedSequences()
  132. {
  133. $from = array('A', 'B');
  134. $to = array('B', 'A');
  135. $expected = array('A');
  136. $common = $this->implementation->calculate($from, $to);
  137. $this->assertEquals($expected, $common);
  138. foreach ($this->stress_sizes as $size) {
  139. $from = range(1, $size);
  140. $to = array_reverse($from);
  141. $common = $this->implementation->calculate($from, $to);
  142. $this->assertEquals(array(1), $common);
  143. }
  144. }
  145. }