php-ml/tests/Math/Distance/EuclideanTest.php

64 lines
1.5 KiB
PHP
Raw Normal View History

<?php
2016-04-04 22:49:54 +02:00
2016-11-20 22:53:17 +01:00
declare(strict_types=1);
namespace Phpml\Tests\Math\Distance;
use Phpml\Exception\InvalidArgumentException;
2016-04-20 23:56:33 +02:00
use Phpml\Math\Distance\Euclidean;
2017-02-03 12:58:25 +01:00
use PHPUnit\Framework\TestCase;
2017-02-03 12:58:25 +01:00
class EuclideanTest extends TestCase
{
/**
* @var Euclidean
*/
private $distanceMetric;
public function setUp(): void
{
$this->distanceMetric = new Euclidean();
}
public function testThrowExceptionOnInvalidArguments(): void
{
$this->expectException(InvalidArgumentException::class);
2016-04-04 22:48:07 +02:00
$a = [0, 1, 2];
$b = [0, 2];
$this->distanceMetric->distance($a, $b);
2016-04-04 22:48:07 +02:00
}
public function testCalculateDistanceForOneDimension(): void
2016-04-04 22:48:07 +02:00
{
$a = [4];
$b = [2];
$expectedDistance = 2;
$actualDistance = $this->distanceMetric->distance($a, $b);
2016-04-04 22:48:07 +02:00
2016-04-05 21:35:06 +02:00
$this->assertEquals($expectedDistance, $actualDistance);
2016-04-04 22:48:07 +02:00
}
public function testCalculateDistanceForTwoDimensions(): void
2016-04-04 22:48:07 +02:00
{
$a = [4, 6];
$b = [2, 5];
$expectedDistance = 2.2360679774998;
$actualDistance = $this->distanceMetric->distance($a, $b);
2016-04-04 22:48:07 +02:00
2016-04-05 21:35:06 +02:00
$this->assertEquals($expectedDistance, $actualDistance);
2016-04-11 21:44:48 +02:00
}
2016-04-04 22:48:07 +02:00
public function testCalculateDistanceForThreeDimensions(): void
2016-04-11 21:44:48 +02:00
{
2016-04-04 22:48:07 +02:00
$a = [6, 10, 3];
$b = [2, 5, 5];
$expectedDistance = 6.7082039324993694;
$actualDistance = $this->distanceMetric->distance($a, $b);
2016-04-04 22:48:07 +02:00
2016-04-05 21:35:06 +02:00
$this->assertEquals($expectedDistance, $actualDistance);
}
}