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 LogTest extends TestCase
|
|
|
|
{
|
2019-12-26 21:03:29 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider provideData
|
|
|
|
*/
|
|
|
|
public function testLog(float $expected, float $number, ?float $base = null): void
|
|
|
|
{
|
|
|
|
self::assertSame($expected, Math\log($number, $base));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideData(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
1.6863989535702288,
|
|
|
|
5.4,
|
|
|
|
null
|
|
|
|
],
|
|
|
|
|
|
|
|
[
|
|
|
|
0.6574784600188808,
|
|
|
|
5.4,
|
|
|
|
13
|
|
|
|
],
|
|
|
|
|
|
|
|
[
|
|
|
|
1.7323937598229686,
|
|
|
|
54.0,
|
|
|
|
10
|
|
|
|
],
|
|
|
|
|
|
|
|
[
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
null
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNegativeInputThrows(): void
|
|
|
|
{
|
|
|
|
$this->expectException(Exception\InvariantViolationException::class);
|
2020-07-09 00:28:29 +02:00
|
|
|
$this->expectExceptionMessage('Expected a non-negative number.');
|
2019-12-26 21:03:29 +01:00
|
|
|
|
|
|
|
Math\log(-45);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNonPositiveBaseThrows(): void
|
|
|
|
{
|
|
|
|
$this->expectException(Exception\InvariantViolationException::class);
|
2020-07-09 00:28:29 +02:00
|
|
|
$this->expectExceptionMessage('Expected a non-negative base.');
|
2019-12-26 21:03:29 +01:00
|
|
|
|
|
|
|
Math\log(4.4, 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBaseOneThrowsForUndefinedLogarithm(): void
|
|
|
|
{
|
|
|
|
$this->expectException(Exception\InvariantViolationException::class);
|
2020-07-09 00:28:29 +02:00
|
|
|
$this->expectExceptionMessage('Logarithm undefined for base 1.');
|
2019-12-26 21:03:29 +01:00
|
|
|
|
|
|
|
Math\log(4.4, 1.0);
|
|
|
|
}
|
2019-12-24 01:52:07 +01:00
|
|
|
}
|