php-ml/tests/Phpml/Metric/Distance/EuclideanTest.php

65 lines
1.4 KiB
PHP
Raw Normal View History

<?php
2016-04-04 20:49:54 +00:00
declare (strict_types = 1);
namespace tests\Phpml\Metric;
use Phpml\Metric\Distance\Euclidean;
class EuclideanTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Euclidean
*/
private $distanceMetric;
public function setUp()
{
$this->distanceMetric = new Euclidean();
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidArguments()
{
2016-04-04 20:48:07 +00:00
$a = [0, 1, 2];
$b = [0, 2];
$this->distanceMetric->distance($a, $b);
2016-04-04 20:48:07 +00:00
}
2016-04-11 19:44:48 +00:00
public function testCalculateDistanceForOneDimension()
2016-04-04 20:48:07 +00:00
{
$a = [4];
$b = [2];
$expectedDistance = 2;
$actualDistance = $this->distanceMetric->distance($a, $b);
2016-04-04 20:48:07 +00:00
2016-04-05 19:35:06 +00:00
$this->assertEquals($expectedDistance, $actualDistance);
2016-04-04 20:48:07 +00:00
}
2016-04-11 19:44:48 +00:00
public function testCalculateDistanceForTwoDimensions()
2016-04-04 20:48:07 +00:00
{
$a = [4, 6];
$b = [2, 5];
$expectedDistance = 2.2360679774998;
$actualDistance = $this->distanceMetric->distance($a, $b);
2016-04-04 20:48:07 +00:00
2016-04-05 19:35:06 +00:00
$this->assertEquals($expectedDistance, $actualDistance);
2016-04-11 19:44:48 +00:00
}
2016-04-04 20:48:07 +00:00
2016-04-11 19:44:48 +00:00
public function testCalculateDistanceForThreeDimensions()
{
2016-04-04 20:48:07 +00:00
$a = [6, 10, 3];
$b = [2, 5, 5];
$expectedDistance = 6.7082039324993694;
$actualDistance = $this->distanceMetric->distance($a, $b);
2016-04-04 20:48:07 +00:00
2016-04-05 19:35:06 +00:00
$this->assertEquals($expectedDistance, $actualDistance);
}
}