Skip to content

Commit

Permalink
Fix: phpunit include tests path (#230)
Browse files Browse the repository at this point in the history
* Fix phpunit include path

* Add tests for Covariance
  • Loading branch information
akondas authored Feb 11, 2018
1 parent 53f8a89 commit 52c9ba8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
enforceTimeLimit="true"
>
<testsuite name="PHP-ML Test Suite">
<directory>tests/*</directory>
<directory>tests</directory>
</testsuite>

<filter>
Expand Down
3 changes: 1 addition & 2 deletions src/Math/Statistic/Covariance.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Phpml\Math\Statistic;

use Exception;
use Phpml\Exception\InvalidArgumentException;

class Covariance
Expand Down Expand Up @@ -64,7 +63,7 @@ public static function fromDataset(array $data, int $i, int $k, bool $sample = t
}

if ($i < 0 || $k < 0 || $i >= $n || $k >= $n) {
throw new Exception('Given indices i and k do not match with the dimensionality of data');
throw new InvalidArgumentException('Given indices i and k do not match with the dimensionality of data');
}

if ($meanX === null || $meanY === null) {
Expand Down
43 changes: 43 additions & 0 deletions tests/Math/Statistic/CovarianceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Phpml\Tests\Math\Statistic;

use Phpml\Exception\InvalidArgumentException;
use Phpml\Math\Statistic\Covariance;
use Phpml\Math\Statistic\Mean;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -59,4 +60,46 @@ public function testSimpleCovariance(): void
$covariance = Covariance::covarianceMatrix($matrix, [$meanX, $meanY]);
$this->assertEquals($knownCovariance, $covariance, '', $epsilon);
}

public function testThrowExceptionOnEmptyX(): void
{
$this->expectException(InvalidArgumentException::class);
Covariance::fromXYArrays([], [1, 2, 3]);
}

public function testThrowExceptionOnEmptyY(): void
{
$this->expectException(InvalidArgumentException::class);
Covariance::fromXYArrays([1, 2, 3], []);
}

public function testThrowExceptionOnToSmallArrayIfSample(): void
{
$this->expectException(InvalidArgumentException::class);
Covariance::fromXYArrays([1], [2], true);
}

public function testThrowExceptionIfEmptyDataset(): void
{
$this->expectException(InvalidArgumentException::class);
Covariance::fromDataset([], 0, 1);
}

public function testThrowExceptionOnToSmallDatasetIfSample(): void
{
$this->expectException(InvalidArgumentException::class);
Covariance::fromDataset([1], 0, 1);
}

public function testThrowExceptionWhenKIndexIsOutOfBound(): void
{
$this->expectException(InvalidArgumentException::class);
Covariance::fromDataset([1, 2, 3], 2, 5);
}

public function testThrowExceptionWhenIIndexIsOutOfBound(): void
{
$this->expectException(InvalidArgumentException::class);
Covariance::fromDataset([1, 2, 3], 5, 2);
}
}

0 comments on commit 52c9ba8

Please sign in to comment.