-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Math; | ||
|
||
use Psl; | ||
|
||
/** | ||
* Returns a number whose value is limited to the given range. | ||
* | ||
* @template TNum of float|int | ||
* | ||
* @param TNum $number | ||
* @param TNum $min | ||
* @param TNum $max | ||
* | ||
* @return TNum | ||
* @throws Psl\Exception\InvariantViolationException If min is bigger than max | ||
* | ||
* @psalm-pure | ||
*/ | ||
function clamp($number, $min, $max) | ||
{ | ||
Psl\invariant($min <= $max, 'Expected %s to be lower or equal to %s.', $min, $max); | ||
|
||
if ($number < $min) { | ||
return $min; | ||
} | ||
|
||
if ($number > $max) { | ||
return $max; | ||
} | ||
|
||
return $number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Math; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Exception; | ||
use Psl\Math; | ||
|
||
final class ClampTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function testClamp($expected, $number, $min, $max): void | ||
{ | ||
static::assertSame($expected, Math\clamp($number, $min, $max)); | ||
} | ||
|
||
public function testInvalidMinMax(): void | ||
{ | ||
$this->expectException(Exception\InvariantViolationException::class); | ||
$this->expectExceptionMessage('Expected 20 to be lower or equal to 10.'); | ||
|
||
Math\clamp(10, 20, 10); | ||
} | ||
|
||
public function provideData(): array | ||
{ | ||
return [ | ||
[ | ||
'expected' => 10, | ||
'number' => 10, | ||
'min' => 2, | ||
'max' => 20 | ||
], | ||
[ | ||
'expected' => 10, | ||
'number' => 20, | ||
'min' => 1, | ||
'max' => 10 | ||
], | ||
[ | ||
'expected' => 10, | ||
'number' => 5, | ||
'min' => 10, | ||
'max' => 20 | ||
], | ||
[ | ||
'expected' => 10, | ||
'number' => 10, | ||
'min' => 10, | ||
'max' => 20 | ||
], | ||
[ | ||
'expected' => 10, | ||
'number' => 10, | ||
'min' => 1, | ||
'max' => 10 | ||
], | ||
[ | ||
'expected' => 10, | ||
'number' => 20, | ||
'min' => 10, | ||
'max' => 10 | ||
], | ||
[ | ||
'expected' => 10.0, | ||
'number' => 10.0, | ||
'min' => 2.0, | ||
'max' => 20.0 | ||
], | ||
]; | ||
} | ||
} |