endtoend-test-psl/tests/Psl/Math/FromBaseTest.php

82 lines
1.9 KiB
PHP
Raw Normal View History

2019-12-24 01:52:07 +01:00
<?php
declare(strict_types=1);
namespace Psl\Tests\Math;
use PHPUnit\Framework\TestCase;
2019-12-26 21:03:29 +01:00
use Psl\Exception;
use Psl\Math;
2019-12-24 01:52:07 +01:00
class FromBaseTest extends TestCase
{
2019-12-26 21:03:29 +01:00
/**
* @dataProvider provideData
*/
public function testFromBase(int $expected, string $value, int $from_base): void
{
self::assertSame($expected, Math\from_base($value, $from_base));
}
public function provideData(): array
{
return [
[
5497,
'1010101111001',
2
],
[
2014587925987,
'pphlmw9v',
36
],
[
Math\INT32_MAX,
'zik0zj',
36
],
[
15,
'F',
16
]
];
}
public function testInvalidDigitThrows(): void
{
$this->expectException(Exception\InvariantViolationException::class);
$this->expectExceptionMessage('Invalid digit Z in base 16');
Math\from_base('Z', 16);
}
public function testSpecialCharThrows(): void
{
$this->expectException(Exception\InvariantViolationException::class);
$this->expectExceptionMessage('Invalid digit * in base 16');
Math\from_base('*', 16);
}
public function testEmptyValueThrows(): void
{
$this->expectException(Exception\InvariantViolationException::class);
$this->expectExceptionMessage('Unexpected empty string, expected number in base 16');
Math\from_base('', 16);
}
public function testInvalidFromBaseThrows(): void
{
$this->expectException(Exception\InvariantViolationException::class);
$this->expectExceptionMessage('Expected $from_base to be between 2 and 36, got 64');
Math\from_base('z', 64);
}
2019-12-24 01:52:07 +01:00
}