From 52c9ba8291cf4ea0a24e217282b28f7c7656cbf1 Mon Sep 17 00:00:00 2001 From: Arkadiusz Kondas Date: Sun, 11 Feb 2018 18:17:50 +0100 Subject: [PATCH] Fix: phpunit include tests path (#230) * Fix phpunit include path * Add tests for Covariance --- phpunit.xml | 2 +- src/Math/Statistic/Covariance.php | 3 +- tests/Math/Statistic/CovarianceTest.php | 43 +++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 4a74eb6..e0a91da 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -9,7 +9,7 @@ enforceTimeLimit="true" > - tests/* + tests diff --git a/src/Math/Statistic/Covariance.php b/src/Math/Statistic/Covariance.php index a669a7f..ac24a53 100644 --- a/src/Math/Statistic/Covariance.php +++ b/src/Math/Statistic/Covariance.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Phpml\Math\Statistic; -use Exception; use Phpml\Exception\InvalidArgumentException; class Covariance @@ -64,7 +63,7 @@ class Covariance } 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) { diff --git a/tests/Math/Statistic/CovarianceTest.php b/tests/Math/Statistic/CovarianceTest.php index 2b64854..43b775a 100644 --- a/tests/Math/Statistic/CovarianceTest.php +++ b/tests/Math/Statistic/CovarianceTest.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Phpml\Tests\Math\Statistic; +use Phpml\Exception\InvalidArgumentException; use Phpml\Math\Statistic\Covariance; use Phpml\Math\Statistic\Mean; use PHPUnit\Framework\TestCase; @@ -59,4 +60,46 @@ class CovarianceTest extends TestCase $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); + } }