diff --git a/Simplex/Math/math.php b/Simplex/Math/math.php new file mode 100644 index 0000000..9a3cbfa --- /dev/null +++ b/Simplex/Math/math.php @@ -0,0 +1,125 @@ + */ + public function provideAddData(): array + { + return [ + ['0', '0', '0'], + ['1', '0', '1'], + ['-1', '0', '-1'], + ['1', '2', '3'], + ['-1', '-2', '-3'], + ['1', '-2', '-1'], + ['-1', '2', '1'], + ['9223372036854775807', '9223372036854775807', '18446744073709551614'], + ]; + } + + + /** @dataProvider provideSubData */ + public function testSub(string $a, string $b, string $expected): void + { + Assert::same($expected, sub($a, $b)); + } + + + /** @return array */ + public function provideSubData(): array + { + return [ + ['0', '0', '0'], + ['1', '2', '-1'], + ['-1', '-2', '1'], + ['1', '-2', '3'], + ['-1', '2', '-3'], + ['9223372036854775807', '9223372036854775807', '0'], + ['9223372036854775807', '9223372036854775808', '-1'], + ['-9223372036854775807', '9223372036854775807', '-18446744073709551614'], + ]; + } + + + /** @dataProvider provideMulData */ + public function testMul(string $a, string $b, string $expected): void + { + Assert::same($expected, mul($a, $b)); + } + + + /** @return array */ + public function provideMulData(): array + { + return [ + ['0', '0', '0'], + ['3', '0', '0'], + ['0', '3', '0'], + ['1', '3', '3'], + ['3', '1', '3'], + ['-1', '-3', '3'], + ['1', '-2', '-2'], + ['-1', '2', '-2'], + ['9223372036854775807', '9223372036854775807', '85070591730234615847396907784232501249'], + ]; + } + + + /** @dataProvider provideDivData */ + public function testDiv(string $a, string $b, int $precision, string $expected): void + { + Assert::same($expected, div($a, $b, $precision)); + } + + + /** @return array */ + public function provideDivData(): array + { + return [ + ['0', '1', 0, '0'], + ['0', '1', 1, '0.0'], + ['0', '1', 2, '0.00'], + + ['0', '-1', 0, '0'], + ['0', '-1', 1, '0.0'], + ['0', '-1', 2, '0.00'], + + ['0', '2', 0, '0'], + ['0', '2', 1, '0.0'], + ['0', '2', 2, '0.00'], + + ['0', '-2', 0, '0'], + ['0', '-2', 1, '0.0'], + ['0', '-2', 2, '0.00'], + + ['1', '2', 0, '0'], + ['1', '2', 1, '0.5'], + ['1', '2', 2, '0.50'], + + ['-1', '2', 0, '0'], + ['-1', '2', 1, '-0.5'], + ['-1', '2', 2, '-0.50'], + + ['1', '-2', 0, '0'], + ['1', '-2', 1, '-0.5'], + ['1', '-2', 2, '-0.50'], + + ['-1', '-2', 0, '0'], + ['-1', '-2', 1, '0.5'], + ['-1', '-2', 2, '0.50'], + + ['2', '3', 0, '0'], + ['2', '3', 1, '0.6'], + ['2', '3', 2, '0.66'], + ['2', '3', 3, '0.666'], + + ['1', '6', 0, '0'], + ['1', '6', 1, '0.1'], + ['1', '6', 2, '0.16'], + ['1', '6', 3, '0.166'], + + ['6', '3', 0, '2'], + ['6', '3', 1, '2.0'], + ['6', '3', 2, '2.00'], + + ['6', '4', 0, '1'], + ['6', '4', 1, '1.5'], + ['6', '4', 2, '1.50'], + + ['18446744073709551614', '2', 0, '9223372036854775807'], + ['18446744073709551614', '2', 1, '9223372036854775807.0'], + ['18446744073709551614', '2', 2, '9223372036854775807.00'], + ]; + } + + + /** @dataProvider provideDivisionByZeroData */ + public function testDivisionByZero(string $a, string $b, int $precision): void + { + Assert::error(static function () use ($a, $b, $precision): void { + div($a, $b, $precision); + + }, \DivisionByZeroError::class); + } + + + /** @return array */ + public function provideDivisionByZeroData(): array + { + return [ + ['', '', 0], + ['', '', 1], + ['', '', 2], + + ['1', '', 0], + ['1', '', 1], + ['1', '', 2], + + ['-1', '', 0], + ['-1', '', 1], + ['-1', '', 2], + + ['0', '0', 0], + ['0', '0', 1], + ['0', '0', 2], + + ['1', '0', 0], + ['1', '0', 1], + ['1', '0', 2], + + ['-1', '0', 0], + ['-1', '0', 1], + ['-1', '0', 2], + + ['9223372036854775807', '0', 0], + ['9223372036854775807', '0', 1], + ['9223372036854775807', '0', 2], + + ['9223372036854775807', '0.0', 0], + ['9223372036854775807', '0.0', 1], + ['9223372036854775807', '0.0', 2], + + ['9223372036854775807', '0.00', 0], + ['9223372036854775807', '0.00', 1], + ['9223372036854775807', '0.00', 2], + + ['-9223372036854775807', '0', 0], + ['-9223372036854775807', '0', 1], + ['-9223372036854775807', '0', 2], + ]; + } + + + /** @dataProvider providePowData */ + public function testPow(string $a, string $b, string $expected): void + { + Assert::same($expected, pow($a, $b)); + } + + + /** @return array */ + public function providePowData(): array + { + return [ + ['0', '0', '1'], + ['1', '0', '1'], + ['-1', '0', '1'], + ['0', '1', '0'], + ['0', '-1', '0'], + ['2', '3', '8'], + ['-2', '3', '-8'], + ['8', '2', '64'], + ['-8', '2', '64'], + ['9223372036854775807', '2', '85070591730234615847396907784232501249'], + ['-9223372036854775807', '2', '85070591730234615847396907784232501249'], + ['0', '-1', '0'], + ['1', '-1', '1'], + ['-1', '-1', '-1'], + ['2', '-1', '0'], + ['-2', '-1', '0'], + ['3', '-1', '0'], + ['-3', '-1', '0'], + ]; + } + + + /** @dataProvider provideModData */ + public function testMod(string $a, string $b, string $expected): void + { + Assert::same($expected, mod($a, $b)); + } + + + /** @return array */ + public function provideModData(): array + { + return [ + ['0', '1', '0'], + ['0', '2', '0'], + ['0', '3', '0'], + ['0', '4', '0'], + ['0', '5', '0'], + + ['1', '1', '0'], + ['1', '2', '1'], + ['1', '3', '1'], + ['1', '4', '1'], + ['1', '5', '1'], + + ['4', '3', '1'], + ['4', '2', '0'], + ['4', '1', '0'], + + ['-4', '3', '-1'], + ['-4', '2', '0'], + ['-4', '1', '0'], + + ['4', '-3', '1'], + ['4', '-2', '0'], + ['4', '-1', '0'], + + ['5', '4', '1'], + ['5', '3', '2'], + ['5', '2', '1'], + ['5', '1', '0'], + ]; + } + + + /** @dataProvider provideAbsData */ + public function testAbs(string $n, string $expected): void + { + Assert::same($expected, abs($n)); + } + + + /** @return array */ + public function provideAbsData(): array + { + return [ + ['', ''], + ['0', '0'], + ['1', '1'], + ['2', '2'], + ['3', '3'], + ['4', '4'], + ['5', '5'], + ['-1', '1'], + ['-2', '2'], + ['-3', '3'], + ['-4', '4'], + ['-5', '5'], + ]; + } + + + /** @dataProvider provideIsNegativeData */ + public function testIsNegative(string $n, bool $expected): void + { + Assert::same($expected, isNegative($n)); + } + + + /** @return array */ + public function provideIsNegativeData(): array + { + return [ + ['', false], + ['0', false], + ['1', false], + ['2', false], + ['3', false], + ['-1', true], + ['-2', true], + ['-3', true], + ]; + } + + + /** @dataProvider provideCompData */ + public function testComp(string $a, string $b, int $expected): void + { + Assert::same($expected, comp($a, $b)); + } + + + /** @return array */ + public function provideCompData(): array + { + return [ + ['0', '0', 0], + ['1', '1', 0], + ['-1', '-1', 0], + ['1', '-1', 1], + ['-1', '1', -1], + ['-2', '-1', -1], + ['-1', '-2', 1], + ['1', '2', -1], + ['2', '1', 1], + ]; + } + + + /** @dataProvider provideRoundData */ + public function testRound(string $a, int $precision, string $expected): void + { + Assert::same($expected, round($a, $precision)); + } + + + /** @return array */ + public function provideRoundData(): array + { + return [ + ['0', 0, '0'], + ['0', 1, '0'], + ['0', 2, '0'], + ['0', 3, '0'], + ['0', 4, '0'], + ['0', 5, '0'], + + ['1.1', 0, '1'], + ['1.1', 1, '1.1'], + ['1.1', 2, '1.1'], + + ['-1.1', 0, '-1'], + ['-1.1', 1, '-1.1'], + ['-1.1', 2, '-1.1'], + + ['1.3', 0, '1'], + ['1.3', 1, '1.3'], + ['1.3', 2, '1.3'], + + ['-1.3', 0, '-1'], + ['-1.3', 1, '-1.3'], + ['-1.3', 2, '-1.3'], + + ['1.5', 0, '2'], + ['1.5', 1, '1.5'], + ['1.5', 2, '1.5'], + + ['-1.5', 0, '-2'], + ['-1.5', 1, '-1.5'], + ['-1.5', 2, '-1.5'], + + ['2.33', 0, '2'], + ['2.33', 1, '2.3'], + ['2.33', 2, '2.33'], + ['2.33', 3, '2.33'], + + ['-2.33', 0, '-2'], + ['-2.33', 1, '-2.3'], + ['-2.33', 2, '-2.33'], + ['-2.33', 3, '-2.33'], + + ['3.45', 0, '3'], + ['3.45', 1, '3.5'], + ['3.45', 2, '3.45'], + ['3.45', 3, '3.45'], + + ['-3.45', 0, '-3'], + ['-3.45', 1, '-3.5'], + ['-3.45', 2, '-3.45'], + ['-3.45', 3, '-3.45'], + ]; + } + + + /** @dataProvider provideGcdData */ + public function testGcd(string $a, string $b, string $expected): void + { + Assert::same($expected, gcd($a, $b)); + } + + + /** @return array */ + public function provideGcdData(): array + { + return [ + ['0', '21', '21'], + ['21', '0', '21'], + + ['1', '24', '1'], + ['24', '1', '1'], + + ['6', '27', '3'], + ['-6', '27', '3'], + ['6', '-27', '3'], + ['-6', '-27', '3'], + + ['1400000000000000256', '100000000000000000', '256'], + ]; + } + + + public function testGcdZeroArguments(): void + { + Assert::exception(static function () { + gcd('0', '0'); + + }, ZeroGcdArgumentsException::class, 'At least one number must not be a zero.'); + } +} + + +(new MathTest)->run();