Skip to content

Commit

Permalink
Add Cluster tests (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
marmichalski authored and akondas committed Mar 4, 2018
1 parent 33efab2 commit 55749c7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Clustering/KMeans/Cluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function toArray(): array
public function attach(Point $point): Point
{
if ($point instanceof self) {
throw new LogicException('cannot attach a cluster to another');
throw new LogicException('Cannot attach a cluster to another');
}

$this->points->attach($point);
Expand Down
49 changes: 49 additions & 0 deletions tests/Clustering/KMeans/ClusterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Phpml\Tests\Clustering\KMeans;

use LogicException;
use Phpml\Clustering\KMeans\Cluster;
use Phpml\Clustering\KMeans\Point;
use Phpml\Clustering\KMeans\Space;
use PHPUnit\Framework\TestCase;

class ClusterTest extends TestCase
{
public function testThrowExceptionWhenAttachingToCluster(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Cannot attach a cluster to another');

$cluster = new Cluster(new Space(1), []);
$cluster->attach(clone $cluster);
}

public function testToArray(): void
{
$cluster = new Cluster(new Space(2), [1, 2]);
$cluster->attach(new Point([1, 1]));

$this->assertSame([
'centroid' => [1, 2],
'points' => [
[1, 1],
],
], $cluster->toArray());
}

public function testDetach(): void
{
$cluster = new Cluster(new Space(2), []);
$cluster->attach(new Point([1, 2]));
$cluster->attach($point = new Point([1, 1]));

$detachedPoint = $cluster->detach($point);

$this->assertSame($detachedPoint, $point);
$this->assertNotContains($point, $cluster->getPoints());
$this->assertCount(1, $cluster);
}
}

0 comments on commit 55749c7

Please sign in to comment.