Skip to content

Commit

Permalink
Ensure DataTransformer::testSet samples array is not empty (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
marmichalski authored and akondas committed Feb 25, 2018
1 parent 4562f1d commit 9e375ca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/SupportVectorMachine/DataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Phpml\SupportVectorMachine;

use Phpml\Exception\InvalidArgumentException;

class DataTransformer
{
public static function trainingSet(array $samples, array $labels, bool $targets = false): string
Expand All @@ -24,6 +26,10 @@ public static function trainingSet(array $samples, array $labels, bool $targets

public static function testSet(array $samples): string
{
if (empty($samples)) {
throw InvalidArgumentException::arrayCantBeEmpty();
}

if (!is_array($samples[0])) {
$samples = [$samples];
}
Expand Down
9 changes: 9 additions & 0 deletions tests/SupportVectorMachine/DataTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Phpml\Tests\SupportVectorMachine;

use Phpml\Exception\InvalidArgumentException;
use Phpml\SupportVectorMachine\DataTransformer;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -78,4 +79,12 @@ public function testProbabilities(): void

$this->assertEquals($probabilities, DataTransformer::probabilities($rawPredictions, $labels));
}

public function testThrowExceptionWhenTestSetIsEmpty(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The array has zero elements');

DataTransformer::testSet([]);
}
}

0 comments on commit 9e375ca

Please sign in to comment.