From a944fe4e5631a6b3085ee2962e3159261f90bfcd Mon Sep 17 00:00:00 2001 From: Bruce Wells Date: Wed, 1 Jun 2022 18:11:51 -0400 Subject: Bcmath (#115) * Add useBCMath * Support for % operator (mod) --- tests/MathTest.php | 242 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 241 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/MathTest.php b/tests/MathTest.php index 7af7c59..94a14d4 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -113,6 +113,7 @@ class MathTest extends TestCase ['tanh(1.5)'], ['0.1 + 0.2'], + ['0.1 + 0.2 - 0.3'], ['1 + 2'], ['0.1 - 0.2'], @@ -246,7 +247,246 @@ class MathTest extends TestCase ['max(1,2,4.9,3)'], ['min(1,2,4.9,3)'], ['max([1,2,4.9,3])'], - ['min([1,2,4.9,3])'] + ['min([1,2,4.9,3])'], + + ['4 % 4'], + ['7 % 4'], + ['99 % 4'], + ['123 % 7'], + ]; + } + + /** + * @dataProvider bcMathExpressions + */ + public function testBCMathCalculating(string $expression, string $expected = '') : void + { + $calculator = new MathExecutor(); + $calculator->useBCMath(); + + if ('' === $expected) + { + $expected = $expression; + } + + /** @var float $phpResult */ + eval('$phpResult = ' . $expected . ';'); + + try { + $result = $calculator->execute($expression); + } catch (Exception $e) { + $this->fail(\sprintf('Exception: %s (%s:%d), expression was: %s', \get_class($e), $e->getFile(), $e->getLine(), $expression)); + } + $this->assertEquals($phpResult, $result, "Expression was: {$expression}"); + } + + /** + * Expressions data provider + * + * Most tests can go in here. The idea is that each expression will be evaluated by MathExecutor and by PHP with eval. + * The results should be the same. If they are not, then the test fails. No need to add extra test unless you are doing + * something more complex and not a simple mathmatical expression. + */ + public function bcMathExpressions() + { + return [ + ['-5'], + ['-5+10'], + ['4-5'], + ['4 -5'], + ['(4*2)-5'], + ['(4*2) - 5'], + ['4*-5'], + ['4 * -5'], + ['+5'], + ['+(3+2)'], + ['+(+3+2)'], + ['+(-3+2)'], + ['-5'], + ['-(-5)'], + ['-(+5)'], + ['+(-5)'], + ['+(+5)'], + ['-(3+2)'], + ['-(-3+-2)'], + + ['abs(1.5)'], + ['acos(0.15)'], + ['acosh(1.5)'], + ['asin(0.15)'], + ['atan(0.15)'], + ['atan2(1.5, 3.5)'], + ['atanh(0.15)'], + ['bindec("10101")'], + ['ceil(1.5)'], + ['cos(1.5)'], + ['cosh(1.5)'], + ['decbin("15")'], + ['dechex("15")'], + ['decoct("15")'], + ['deg2rad(1.5)'], + ['exp(1.5)'], + ['expm1(1.5)'], + ['floor(1.5)'], + ['fmod(1.5, 3.5)'], + ['hexdec("abcdef")'], + ['hypot(1.5, 3.5)'], + ['intdiv(10, 2)'], + ['log(1.5)'], + ['log10(1.5)'], + ['log1p(1.5)'], + ['max(1.5, 3.5)'], + ['min(1.5, 3.5)'], + ['octdec("15")'], + ['pi()'], + ['pow(1.5, 3.5)'], + ['rad2deg(1.5)'], + ['round(1.5)'], + ['sin(1.5)'], + ['sin(12)'], + ['+sin(12)'], + ['-sin(12)', '0.53'], + ['sinh(1.5)'], + ['sqrt(1.5)'], + ['tan(1.5)'], + ['tanh(1.5)'], + + ['0.1 + 0.2'], + ['0.1 + 0.2 - 0.3'], + ['1 + 2'], + + ['0.1 - 0.2'], + ['1 - 2'], + + ['0.1 * 2'], + ['1 * 2'], + + ['0.1 / 0.2'], + ['1 / 2'], + + ['2 * 2 + 3 * 3'], + ['2 * 2 / 3 * 3', '3.99'], + ['2 / 2 / 3 / 3', '0.11'], + ['2 / 2 * 3 / 3'], + ['2 / 2 * 3 * 3'], + + ['1 + 0.6 - 3 * 2 / 50'], + + ['(5 + 3) * -1'], + + ['-2- 2*2'], + ['2- 2*2'], + ['2-(2*2)'], + ['(2- 2)*2'], + ['2 + 2*2'], + ['2+ 2*2'], + ['2+2*2'], + ['(2+2)*2'], + ['(2 + 2)*-2'], + ['(2+-2)*2'], + + ['1 + 2 * 3 / (min(1, 5) + 2 + 1)'], + ['1 + 2 * 3 / (min(1, 5) - 2 + 5)'], + ['1 + 2 * 3 / (min(1, 5) * 2 + 1)'], + ['1 + 2 * 3 / (min(1, 5) / 2 + 1)'], + ['1 + 2 * 3 / (min(1, 5) / 2 * 1)'], + ['1 + 2 * 3 / (min(1, 5) / 2 / 1)'], + ['1 + 2 * 3 / (3 + min(1, 5) + 2 + 1)', '1.85'], + ['1 + 2 * 3 / (3 - min(1, 5) - 2 + 1)'], + ['1 + 2 * 3 / (3 * min(1, 5) * 2 + 1)', '1.85'], + ['1 + 2 * 3 / (3 / min(1, 5) / 2 + 1)'], + + ['(1 + 2) * 3 / (3 / min(1, 5) / 2 + 1)'], + + ['sin(10) * cos(50) / min(10, 20/2)', '-0.05'], + ['sin(10) * cos(50) / min(10, (20/2))', '-0.05'], + ['sin(10) * cos(50) / min(10, (max(10,20)/2))', '-0.05'], + + ['1 + "2" / 3', '1.66'], + ["1.5 + '2.5' / 4", '2.12'], + ['1.5 + "2.5" * ".5"'], + + ['-1 + -2'], + ['-1+-2'], + ['-1- -2'], + ['-1/-2'], + ['-1*-2'], + + ['(1+2+3+4-5)*7/100'], + ['(-1+2+3+4- 5)*7/100'], + ['(1+2+3+4- 5)*7/100'], + ['( 1 + 2 + 3 + 4 - 5 ) * 7 / 100'], + + ['1 && 0'], + ['1 && 0 && 1'], + ['1 || 0'], + ['1 && 0 || 1'], + + ['5 == 3'], + ['5 == 5'], + ['5 != 3'], + ['5 != 5'], + ['5 > 3'], + ['3 > 5'], + ['3 >= 5'], + ['3 >= 3'], + ['3 < 5'], + ['5 < 3'], + ['3 <= 5'], + ['5 <= 5'], + ['10 < 9 || 4 > (2+1)'], + ['10 < 9 || 4 > (-2+1)'], + ['10 < 9 || 4 > (2+1) && 5 == 5 || 4 != 6 || 3 >= 4 || 3 <= 7'], + + ['1 + 5 == 3 + 1'], + ['1 + 5 == 5 + 1'], + ['1 + 5 != 3 + 1'], + ['1 + 5 != 5 + 1'], + ['1 + 5 > 3 + 1'], + ['1 + 3 > 5 + 1'], + ['1 + 3 >= 5 + 1'], + ['1 + 3 >= 3 + 1'], + ['1 + 3 < 5 + 1'], + ['1 + 5 < 3 + 1'], + ['1 + 3 <= 5 + 1'], + ['1 + 5 <= 5 + 1'], + + ['(-4)'], + ['(-4 + 5)'], + ['(3 * 1)'], + ['(-3 * -1)'], + ['1 + (-3 * -1)'], + ['1 + ( -3 * 1)'], + ['1 + (3 *-1)'], + ['1 - 0'], + ['1-0'], + + ['-(1.5)'], + ['-log(4)', '-1.38'], + ['0-acosh(1.5)', '-0.96'], + ['-acosh(1.5)', '-0.96'], + ['-(-4)'], + ['-(-4 + 5)'], + ['-(3 * 1)'], + ['-(-3 * -1)'], + ['-1 + (-3 * -1)'], + ['-1 + ( -3 * 1)'], + ['-1 + (3 *-1)'], + ['-1 - 0'], + ['-1-0'], + ['-(4*2)-5'], + ['-(4*-2)-5'], + ['-(-4*2) - 5'], + ['-4*-5'], + ['max(1,2,4.9,3)'], + ['min(1,2,4.9,3)'], + ['max([1,2,4.9,3])'], + ['min([1,2,4.9,3])'], + + ['4 % 4'], + ['7 % 4'], + ['99 % 4'], + ['123 % 7'], ]; } -- cgit v1.2.3