diff options
author | franksl <info@streamlake.com> | 2019-11-26 17:00:24 +0300 |
---|---|---|
committer | Bruce Wells <brucekwells@gmail.com> | 2019-11-26 17:00:24 +0300 |
commit | 29c5b5006ccb5cb308b9467ca25813b29be8cfc6 (patch) | |
tree | 88cafa1f3fe84e1db662470851a68947973cadda /tests/MathTest.php | |
parent | b8456057af1bcaad362ccb5ef55e8c696dd63e11 (diff) |
Logicandcompare (#50)
* TokenFactory: allowing multicharacter tokens
* Added logical and compare operators: <, <=, >, >=, ==, !=, ||, &&
* Fixed operator priorities
* Error messages fixes
* Fixed operators priority
The priorities are assigned by following the php language standard
(https://www.php.net/manual/en/language.operators.precedence.php)
I've assigned precedence in steps of 10 units by following the linked page:
230 clone new
220 **
210 ++ -- ~ (int) (float) (string) (array) (object) (bool) @
200 instanceof
190 !
180 * / %
170 + - .
160 << >>
150 < <= > >=
140 == != === !== <> <=>
130 &
120 ^
110 |
100 &&
90 ||
80 ??
70 ? :
60 = += -= *= **= /= .= %= &= |= ^= <<= >>=
50 yield from
40 yield
30 and
20 xor
10 or
* Added if() function
* Cache key fix
There are cases where the cache key creation raised an error, for example
while evaluating the expression "if(cos(2), cos(2), 0)", because the
if() function was passing a float to the MathExecutor:execute() method.
Diffstat (limited to 'tests/MathTest.php')
-rw-r--r-- | tests/MathTest.php | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/MathTest.php b/tests/MathTest.php index e289016..90cdccf 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -50,6 +50,8 @@ class MathTest extends \PHPUnit\Framework\TestCase ['4*-5'], ['4 * -5'], + [cos(2)], + ['0.1 + 0.2'], ['1 + 2'], @@ -63,6 +65,10 @@ class MathTest extends \PHPUnit\Framework\TestCase ['1 / 2'], ['2 * 2 + 3 * 3'], + ['2 * 2 / 3 * 3'], + ['2 / 2 / 3 / 3'], + ['2 / 2 * 3 / 3'], + ['2 / 2 * 3 * 3'], ['1 + 0.6 - 3 * 2 / 50'], @@ -89,6 +95,8 @@ class MathTest extends \PHPUnit\Framework\TestCase ['1 + 2 * 3 / (3 * min(1, 5) * 2 + 1)'], ['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)'], ['sin(10) * cos(50) / min(10, (20/2))'], ['sin(10) * cos(50) / min(10, (max(10,20)/2))'], @@ -110,6 +118,38 @@ class MathTest extends \PHPUnit\Framework\TestCase ['(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) && 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'], ]; } @@ -163,6 +203,27 @@ class MathTest extends \PHPUnit\Framework\TestCase $this->assertEquals(round(100/30), $calculator->execute('round(100/30)')); } + public function testFunctionIf() + { + $calculator = new MathExecutor(); + $this->assertEquals(30, $calculator->execute( + 'if(100 > 99, 30, 0)')); + $this->assertEquals(0, $calculator->execute( + 'if(100 < 99, 30, 0)')); + $this->assertEquals(30, $calculator->execute( + 'if(98 < 99 && sin(1) < 1, 30, 0)')); + $this->assertEquals(40, $calculator->execute( + 'if(98 < 99 && sin(1) < 1, max(30, 40), 0)')); + $this->assertEquals(40, $calculator->execute( + 'if(98 < 99 && sin(1) < 1, if(10 > 5, max(30, 40), 1), 0)')); + $this->assertEquals(20, $calculator->execute( + 'if(98 < 99 && sin(1) > 1, if(10 > 5, max(30, 40), 1), if(4 <= 4, 20, 21))')); + $this->assertEquals(cos(2), $calculator->execute( + 'if(98 < 99 && sin(1) >= 1, max(30, 40), cos(2))')); + $this->assertEquals(cos(2), $calculator->execute( + 'if(cos(2), cos(2), 0)')); + } + public function testEvaluateFunctionParameters() { $calculator = new MathExecutor(); |