InsertionSortTest.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * @file
  4. * InsertionSortTest.php
  5. *
  6. * @author: marand
  7. *
  8. * @license General Public License version 2 or later
  9. */
  10. namespace OSInet\Sort\Tests;
  11. use OSInet\Sort\Sort;
  12. class InsertionSortTest extends \PHPUnit_Framework_TestCase {
  13. /**
  14. * @var \OSInet\Sort\InsertionSort
  15. */
  16. protected $sort;
  17. protected $expected;
  18. public function setUp() {
  19. $this->sort = Sort::create('insertion');
  20. $this->assertInstanceOf('OSInet\Sort\SortInterface', $this->sort);
  21. // Roughly 4 seconds on my 2011 MacBook Pro
  22. $this->expected = range(1, 1000);
  23. }
  24. protected function genericTest($source, $expected = NULL) {
  25. $actual = $this->sort->sort($source, function ($a, $b) {
  26. return $a - $b;
  27. });
  28. if (!isset($expected)) {
  29. $expected = $this->expected;
  30. }
  31. $this->assertEquals($expected, $actual);
  32. }
  33. public function testEmpty() {
  34. $this->genericTest(array(), array());
  35. }
  36. public function testSorted() {
  37. $source = $this->expected;
  38. $this->genericTest($source);
  39. }
  40. public function testReverted() {
  41. $source = array_reverse($this->expected);
  42. $this->genericTest($source);
  43. }
  44. public function testRandom() {
  45. $passes = 10;
  46. $source = $this->expected;
  47. for ($i = 0 ; $i < $passes ; $i++) {
  48. shuffle($source);
  49. $this->genericTest($source);
  50. }
  51. }
  52. }