Ensure DataTransformer::testSet samples array is not empty (#204)

This commit is contained in:
Marcin Michalski 2018-02-25 22:56:36 +01:00 committed by Arkadiusz Kondas
parent 4562f1dfc9
commit 9e375ca544
2 changed files with 15 additions and 0 deletions

View File

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Phpml\SupportVectorMachine;
use Phpml\Exception\InvalidArgumentException;
class DataTransformer
{
public static function trainingSet(array $samples, array $labels, bool $targets = false): string
@ -24,6 +26,10 @@ class DataTransformer
public static function testSet(array $samples): string
{
if (empty($samples)) {
throw InvalidArgumentException::arrayCantBeEmpty();
}
if (!is_array($samples[0])) {
$samples = [$samples];
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Phpml\Tests\SupportVectorMachine;
use Phpml\Exception\InvalidArgumentException;
use Phpml\SupportVectorMachine\DataTransformer;
use PHPUnit\Framework\TestCase;
@ -78,4 +79,12 @@ class DataTransformerTest extends TestCase
$this->assertEquals($probabilities, DataTransformer::probabilities($rawPredictions, $labels));
}
public function testThrowExceptionWhenTestSetIsEmpty(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The array has zero elements');
DataTransformer::testSet([]);
}
}