123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727 |
- <?php
- namespace MathPHP\Tests\Functions;
- use MathPHP\Functions\Support;
- use MathPHP\Exception;
- class SupportTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @test checkLimits on lower limit
- * @dataProvider dataProviderForCheckLimitsLowerLimit
- * @param array $limits
- * @param array $params
- * @throws \Exception
- */
- public function testCheckLimitsLowerLimit(array $limits, array $params)
- {
- // When
- $withinLimits = Support::checkLimits($limits, $params);
- // Then
- $this->assertTrue($withinLimits);
- }
- /**
- * @return array
- */
- public function dataProviderForCheckLimitsLowerLimit(): array
- {
- return [
- [
- ['x' => '[0,∞]'],
- ['x' => 0],
- ],
- [
- ['x' => '[0,∞]'],
- ['x' => 0.1],
- ],
- [
- ['x' => '[0,∞]'],
- ['x' => 1],
- ],
- [
- ['x' => '[0,∞]'],
- ['x' => 4934],
- ],
- [
- ['x' => '(0,∞]'],
- ['x' => 0.1],
- ],
- [
- ['x' => '(0,∞]'],
- ['x' => 1],
- ],
- [
- ['x' => '(0,∞]'],
- ['x' => 4934],
- ],
- [
- ['x' => '[-50,∞]'],
- ['x' => -50],
- ],
- [
- ['x' => '(-50,∞]'],
- ['x' => -49],
- ],
- [
- ['x' => '[-∞,10]'],
- ['x' => -89379837],
- ],
- [
- ['x' => '(-∞,10]'],
- ['x' => -95893223452],
- ],
- [
- ['x' => '[0,∞]', 'y' => '[0,∞]'],
- ['x' => 0, 'y' => 5],
- ],
- [
- ['x' => '[0,∞]', 'y' => '[0,∞]', 'z' => '[0,1]'],
- ['x' => 0, 'y' => 5],
- ],
- ];
- }
- /**
- * @test checkLimits out of bounds
- * @dataProvider dataProviderForCheckLimitsLowerLimitException
- * @param array $limits
- * @param array $params
- * @throws \Exception
- */
- public function testCheckLimitsLowerLimitException(array $limits, array $params)
- {
- // Then
- $this->expectException(Exception\OutOfBoundsException::class);
- // When
- Support::checkLimits($limits, $params);
- }
- /**
- * @return array
- */
- public function dataProviderForCheckLimitsLowerLimitException(): array
- {
- return [
- [
- ['x' => '[0,∞]'],
- ['x' => -1],
- ],
- [
- ['x' => '[0,∞]'],
- ['x' => -4],
- ],
- [
- ['x' => '[5,∞]'],
- ['x' => 4],
- ],
- [
- ['x' => '(0,∞]'],
- ['x' => -1],
- ],
- [
- ['x' => '(0,∞]'],
- ['x' => -4],
- ],
- [
- ['x' => '(5,∞]'],
- ['x' => 4],
- ],
- ];
- }
- /**
- * @test checkLimits on upper limit
- * @dataProvider dataProviderForCheckLimitsUpperLimit
- * @param array $limits
- * @param array $params
- * @throws \Exception
- */
- public function testCheckLimitsUpperLimit(array $limits, array $params)
- {
- // When
- $withinLimits = Support::checkLimits($limits, $params);
- // Then
- $this->assertTrue($withinLimits);
- }
- /**
- * @return array
- */
- public function dataProviderForCheckLimitsUpperLimit(): array
- {
- return [
- [
- ['x' => '[0,5]'],
- ['x' => 0],
- ],
- [
- ['x' => '[0,5]'],
- ['x' => 3],
- ],
- [
- ['x' => '[0,5]'],
- ['x' => 5],
- ],
- [
- ['x' => '[0,5)'],
- ['x' => 0],
- ],
- [
- ['x' => '[0,5)'],
- ['x' => 3],
- ],
- [
- ['x' => '[0,5)'],
- ['x' => 4.999],
- ],
- [
- ['x' => '[0,∞]'],
- ['x' => 9489859893],
- ],
- [
- ['x' => '[0,∞)'],
- ['x' => 9489859893],
- ],
- [
- ['x' => '[0,5]', 'y' => '[0,5]'],
- ['x' => 0],
- ],
- [
- ['x' => '[0,5]', 'y' => '[0,5]'],
- ['x' => 0, 'y' => 3],
- ],
- [
- ['x' => '[0,5]', 'y' => '[0,5]', 'z' => '[0,5]'],
- ['x' => 0, 'y' => 3],
- ],
- ];
- }
- /**
- * @test checkLimits out of bounds
- * @dataProvider dataProviderForCheckLimitsUpperLimitException
- * @param array $limits
- * @param array $params
- * @throws \Exception
- */
- public function testCheckLimitsUpperLimitException(array $limits, array $params)
- {
- // Then
- $this->expectException(Exception\OutOfBoundsException::class);
- // When
- Support::checkLimits($limits, $params);
- }
- /**
- * @return array
- */
- public function dataProviderForCheckLimitsUpperLimitException(): array
- {
- return [
- [
- ['x' => '[0,5]'],
- ['x' => 5.001],
- ],
- [
- ['x' => '[0,5]'],
- ['x' => 6],
- ],
- [
- ['x' => '[0,5]'],
- ['x' => 98349389],
- ],
- [
- ['x' => '[0,5)'],
- ['x' => 5],
- ],
- [
- ['x' => '[0,5)'],
- ['x' => 5.1],
- ],
- [
- ['x' => '[0,5)'],
- ['x' => 857385738],
- ],
- ];
- }
- /**
- * @test checkLimits bad data
- * @throws \Exception
- */
- public function testCheckLimitsLowerLimitEndpointException()
- {
- // Given
- $limits = ['x' => '{0,1)'];
- $params = ['x' => 0.5];
- // Then
- $this->expectException(Exception\BadDataException::class);
- // When
- Support::checkLimits($limits, $params);
- }
- /**
- * @test checkLimits bad data
- * @throws \Exception
- */
- public function testCheckLimitsUpperLimitEndpointException()
- {
- // Given
- $limits = ['x' => '(0,1}'];
- $params = ['x' => 0.5];
- // Then
- $this->expectException(Exception\BadDataException::class);
- // When
- Support::checkLimits($limits, $params);
- }
- /**
- * @test checkLimits bad parameter
- * @dataProvider dataProviderForCheckLimitsUndefinedParameterException
- * @param array $limits
- * @param array $params
- * @throws \Exception
- */
- public function testCheckLimitsUndefinedParameterException(array $limits, array $params)
- {
- // Then
- $this->expectException(Exception\BadParameterException::class);
- // When
- Support::checkLimits($limits, $params);
- }
- /**
- * @return array
- */
- public function dataProviderForCheckLimitsUndefinedParameterException(): array
- {
- return [
- [
- ['x' => '[0,1]'],
- ['y' => 0.5],
- ],
- [
- ['x' => '[0,1]', 'a' => '[0,10]'],
- ['y' => 0.5],
- ],
- [
- ['x' => '[0,1]', 'a' => '[0,10]'],
- ['x' => 0.5, 'b' => 4],
- ],
- [
- ['x' => '[0,1]', 'a' => '[0,10]'],
- ['x' => 0.5, 'a' => 4, 'z' => 9],
- ],
- ];
- }
- /**
- * @test isZero returns true for infinitesimal quantities less than the defined epsilon
- * @dataProvider dataProviderForZero
- * @param float $x
- */
- public function testIsZeroTrue(float $x)
- {
- // When
- $isZero = Support::isZero($x);
- // Then
- $this->assertTrue($isZero);
- }
- /**
- * @test isZero returns false for infinitesimal quantities greater than the defined epsilon
- * @dataProvider dataProviderForNotZero
- * @param float $x
- */
- public function testIsZeroFalse(float $x)
- {
- // When
- $isZero = Support::isZero($x);
- // Then
- $this->assertFalse($isZero);
- }
- /**
- * @test isNotZero returns true for infinitesimal quantities greater than the defined epsilon
- * @dataProvider dataProviderForNotZero
- * @param float $x
- */
- public function testIsNotZeroTrue(float $x)
- {
- // When
- $isNotZero = Support::isNotZero($x);
- // Then
- $this->assertTrue($isNotZero);
- }
- /**
- * @test isNotZero returns false for infinitesimal quantities less than the defined epsilon
- * @dataProvider dataProviderForZero
- * @param float $x
- */
- public function testIsNotZeroFalse(float $x)
- {
- // When
- $isNotZero = Support::isNotZero($x);
- // Then
- $this->assertFalse($isNotZero);
- }
- /**
- * @test isZero is true when setting a specific tolerance
- */
- public function testIsZeroWithinTolerance()
- {
- // Given
- $x = 0.00000001;
- $ε = 0.00000001;
- // When
- $isZero = Support::isZero($x, $ε);
- // Then
- $this->assertTrue($isZero);
- }
- /**
- * @test isZero is false when setting a specific tolerance
- */
- public function testIsZeroOutsideOfTolerance()
- {
- // Given
- $x = 0.00000002;
- $ε = 0.00000001;
- // When
- $isZero = Support::isZero($x, $ε);
- // Then
- $this->assertFalse($isZero);
- }
- /**
- * @test isNotZero is true when setting a specific tolerance
- */
- public function testIsNotZeroWithinTolerance()
- {
- // Given
- $x = 0.00000002;
- $ε = 0.00000001;
- // When
- $isZero = Support::isNotZero($x, $ε);
- // Then
- $this->assertTrue($isZero);
- }
- /**
- * @test isNotZero is false when setting a specific tolerance
- */
- public function testIsNotZeroOutsideOfTolerance()
- {
- // Given
- $x = 0.00000001;
- $ε = 0.00000001;
- // When
- $isZero = Support::isNotZero($x, $ε);
- // Then
- $this->assertFalse($isZero);
- }
- /**
- * @return array
- */
- public function dataProviderForZero(): array
- {
- return [
- [0],
- [0.0],
- [0.00],
- [0.000000000000000000000000000000],
- [0.000000000000001],
- [0.0000000000000001],
- [0.00000000000000001],
- [0.000000000000000001],
- [0.0000000000000000001],
- [0.00000000000000000001],
- [0.000000000000000000001],
- [0.0000000000000000000001],
- [0.00000000000000000000001],
- [0.000000000000000000000001],
- [-0],
- [-0.0],
- [-0.00],
- [-0.000000000000000000000000000000],
- [-0.000000000000001],
- [-0.0000000000000001],
- [-0.00000000000000001],
- [-0.000000000000000001],
- [-0.0000000000000000001],
- [-0.00000000000000000001],
- [-0.000000000000000000001],
- [-0.0000000000000000000001],
- [-0.00000000000000000000001],
- [-0.000000000000000000000001],
- ];
- }
- public function dataProviderForNotZero(): array
- {
- return [
- [1],
- [1.0],
- [1.00],
- [1.000000000000000000000000000000],
- [0.00000000002],
- [0.0000000001],
- [0.000000001],
- [0.00000001],
- [0.0000001],
- [0.000001],
- [-1],
- [-1.0],
- [-1.00],
- [-1.000000000000000000000000000000],
- [-0.00000000002],
- [-0.0000000001],
- [-0.000000001],
- [-0.00000001],
- [-0.0000001],
- [-0.000001],
- ];
- }
- /**
- * @test isEqual returns true for equal values
- * @dataProvider dataProviderForEqualValues
- * @param int|float $x
- * @param int|float $y
- */
- public function testIsEqual($x, $y)
- {
- // When
- $isEqual = Support::isEqual($x, $y);
- // Then
- $this->assertTrue($isEqual);
- }
- /**
- * @test isEqual returns false for unequal values
- * @dataProvider dataProviderForUnequalValues
- * @param int|float $x
- * @param int|float $y
- */
- public function testIsEqualWhenNotEqual($x, $y)
- {
- // When
- $isEqual = Support::isEqual($x, $y);
- // Then
- $this->assertFalse($isEqual);
- }
- /**
- * @test isNotEqual returns true for unequal values
- * @dataProvider dataProviderForUnequalValues
- * @param int|float $x
- * @param int|float $y
- */
- public function testIsNotEqual($x, $y)
- {
- // When
- $isNotEqual = Support::isNotEqual($x, $y);
- // Then
- $this->assertTrue($isNotEqual);
- }
- /**
- * @test isNotEqual returns false for equal values
- * @dataProvider dataProviderForEqualValues
- * @param int|float $x
- * @param int|float $y
- */
- public function testIsNotEqualWhenEqual($x, $y)
- {
- // When
- $isNotEqual = Support::isNotEqual($x, $y);
- // Then
- $this->assertFalse($isNotEqual);
- }
- /**
- * @test isEqual is true when setting a specific tolerance
- */
- public function testIsEqualWithinTolerance()
- {
- // Given
- $x = 1.000001;
- $y = 1.000002;
- $ε = 0.000002;
- // When
- $isEqual = Support::isEqual($x, $y, $ε);
- // Then
- $this->assertTrue($isEqual);
- }
- /**
- * @test isEqual is false when setting a specific tolerance
- */
- public function testIsEqualOutsideOfTolerance()
- {
- // Given
- $x = 1.000001;
- $y = 1.000002;
- $ε = 0.0000009;
- // When
- $isEqual = Support::isEqual($x, $y, $ε);
- // Then
- $this->assertFalse($isEqual);
- }
- /**
- * @test isNotEqual is true when setting a specific tolerance
- */
- public function testIsNotEqualWithinTolerance()
- {
- // Given
- $x = 1.000001;
- $y = 1.000002;
- $ε = 0.000001;
- // When
- $isEqual = Support::isNotEqual($x, $y, $ε);
- // Then
- $this->assertTrue($isEqual);
- }
- /**
- * @test isNotEqual is false when setting a specific tolerance
- */
- public function testIsNotEqualOutsideOfTolerance()
- {
- // Given
- $x = 1.000001;
- $y = 1.000002;
- $ε = 0.000002;
- // When
- $isEqual = Support::isNotEqual($x, $y, $ε);
- // Then
- $this->assertFalse($isEqual);
- }
- /**
- * @return array
- */
- public function dataProviderForEqualValues(): array
- {
- return [
- [0, 0],
- [1, 1],
- [2, 2],
- [489837, 489837],
- [-1, -1],
- [-2, -2],
- [-489837, -489837],
- [1.1, 1.1],
- [4.86, 4.86],
- [4.4948739874, 4.4948739874],
- [-1.1, -1.1],
- [-4.86, -4.86],
- [-4.4948739874, -4.4948739874],
- [0.01, 0.01],
- [0.001, 0.001],
- [0.0001, 0.0001],
- [0.00001, 0.00001],
- [0.000001, 0.000001],
- [0.0000001, 0.0000001],
- [0.00000001, 0.00000001],
- [0.000000001, 0.000000001],
- [0.0000000001, 0.0000000001],
- [0.00000000001, 0.00000000001],
- [0.000000000001, 0.000000000001],
- [-0.01, -0.01],
- [-0.001, -0.001],
- [-0.0001, -0.0001],
- [-0.00001, -0.00001],
- [-0.000001, -0.000001],
- [-0.0000001, -0.0000001],
- [-0.00000001, -0.00000001],
- [-0.000000001, -0.000000001],
- [-0.0000000001, -0.0000000001],
- [-0.00000000001, -0.00000000001],
- [-0.000000000001, -0.000000000001],
- ];
- }
- /**
- * @return array
- */
- public function dataProviderForUnequalValues(): array
- {
- return [
- [0, 1],
- [1, 2],
- [2, 3],
- [489838, 489837],
- [-1, -2],
- [-2, -3],
- [-489838, -489837],
- [1.1, 1.2],
- [4.86, 4.87],
- [4.4948739876, 4.4948739874],
- [-1.1, -1.2],
- [-4.86, -4.87],
- [-4.4948739873, -4.4948739874],
- [0.01, 0.02],
- [0.001, 0.002],
- [0.0001, 0.0002],
- [0.00001, 0.00002],
- [0.000001, 0.000002],
- [0.0000001, 0.0000002],
- [0.00000001, 0.00000002],
- [0.000000001, 0.000000002],
- [0.0000000001, 0.0000000002],
- [0.00000000001, 0.00000000002],
- [0.00000000002, 0.00000000003],
- [-0.01, -0.02],
- [-0.001, -0.002],
- [-0.0001, -0.0002],
- [-0.00001, -0.00002],
- [-0.000001, -0.000002],
- [-0.0000001, -0.0000002],
- [-0.00000001, -0.00000002],
- [-0.000000001, -0.000000002],
- [-0.0000000001, -0.0000000002],
- [-0.00000000001, -0.00000000002],
- [-0.00000000002, -0.00000000003],
- ];
- }
- }
|