<?php
/**
 * @file
 *   InsertionSortTest.php
 *
 * @author: marand
 *
 * @license General Public License version 2 or later
 */

namespace OSInet\Sort\Tests;


use OSInet\Sort\Sort;

class InsertionSortTest extends \PHPUnit_Framework_TestCase {

  /**
   * @var \OSInet\Sort\InsertionSort
   */
  protected $sort;

  protected $expected;

  public function setUp() {
    $this->sort = Sort::create('insertion');
    $this->assertInstanceOf('OSInet\Sort\SortInterface', $this->sort);
    // Roughly 4 seconds on my 2011 MacBook Pro
    $this->expected = range(1, 1000);
  }

  protected function genericTest($source, $expected = NULL) {
    $actual = $this->sort->sort($source, function ($a, $b) {
      return $a - $b;
    });
    if (!isset($expected)) {
      $expected = $this->expected;
    }
    $this->assertEquals($expected, $actual);
  }

  public function testEmpty() {
    $this->genericTest(array(), array());
  }

  public function testSorted() {
    $source = $this->expected;
    $this->genericTest($source);
  }

  public function testReverted() {
    $source = array_reverse($this->expected);
    $this->genericTest($source);
  }

  public function testRandom() {
    $passes = 10;

    $source = $this->expected;
    for ($i = 0 ; $i < $passes ; $i++) {
      shuffle($source);
      $this->genericTest($source);
    }
  }
}