php-ml/tests/NeuralNetwork/LayerTest.php

58 lines
1.5 KiB
PHP
Raw Normal View History

<?php
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
namespace Phpml\Tests\NeuralNetwork;
use Phpml\Exception\InvalidArgumentException;
use Phpml\NeuralNetwork\Layer;
use Phpml\NeuralNetwork\Node\Bias;
use Phpml\NeuralNetwork\Node\Neuron;
2017-02-03 11:58:25 +00:00
use PHPUnit\Framework\TestCase;
use stdClass;
2017-02-03 11:58:25 +00:00
class LayerTest extends TestCase
{
public function testLayerInitialization(): void
{
$layer = new Layer();
2018-10-28 06:44:52 +00:00
self::assertEquals([], $layer->getNodes());
}
public function testLayerInitializationWithDefaultNodesType(): void
{
$layer = new Layer($number = 5);
2018-10-28 06:44:52 +00:00
self::assertCount($number, $layer->getNodes());
foreach ($layer->getNodes() as $node) {
2018-10-28 06:44:52 +00:00
self::assertInstanceOf(Neuron::class, $node);
}
}
public function testLayerInitializationWithExplicitNodesType(): void
{
$layer = new Layer($number = 5, $class = Bias::class);
2018-10-28 06:44:52 +00:00
self::assertCount($number, $layer->getNodes());
foreach ($layer->getNodes() as $node) {
2018-10-28 06:44:52 +00:00
self::assertInstanceOf($class, $node);
}
}
public function testThrowExceptionOnInvalidNodeClass(): void
{
$this->expectException(InvalidArgumentException::class);
new Layer(1, stdClass::class);
}
public function testAddNodesToLayer(): void
{
$layer = new Layer();
$layer->addNode($node1 = new Neuron());
$layer->addNode($node2 = new Neuron());
2018-10-28 06:44:52 +00:00
self::assertEquals([$node1, $node2], $layer->getNodes());
}
}