added simple math and charts modules

This commit is contained in:
2024-12-10 23:53:23 +01:00
parent 62a5f3c97c
commit 688ff42752
196 changed files with 20285 additions and 8 deletions

View File

@@ -0,0 +1,216 @@
<?php
/**
* @author Serge Rodovnichenko <serge@syrnik.com>
* @copyright Serge Rodovnichenko, 2019
* @license BSD 2.0
*/
namespace Andileco\Util\EvalMath\Tests;
use PHPUnit\Framework\TestCase;
use Andileco\Util\EvalMath\EvalMath;
use Andileco\Util\EvalMath\Exception\AbstractEvalMathException;
use Andileco\Util\EvalMath\Exception\BuiltInFunctionRedefinitionException;
use Andileco\Util\EvalMath\Exception\ConstantAssignmentException;
use Andileco\Util\EvalMath\Exception\DivisionByZeroException;
use Andileco\Util\EvalMath\Exception\ExpectingTokenException;
use Andileco\Util\EvalMath\Exception\IllegalCharacterException;
use Andileco\Util\EvalMath\Exception\InternalErrorException;
use Andileco\Util\EvalMath\Exception\InvalidArgumentCountException;
use Andileco\Util\EvalMath\Exception\OperatorLacksOperandException;
use Andileco\Util\EvalMath\Exception\OperatorRequiredException;
use Andileco\Util\EvalMath\Exception\UndefinedVariableException;
use Andileco\Util\EvalMath\Exception\UndefinedVariableInFunctionDefinitionException;
use Andileco\Util\EvalMath\Exception\UnexpectedOperatorException;
use Andileco\Util\EvalMath\Exception\UnexpectedTokenException;
class EvalMathExceptionsTest extends TestCase
{
private $EvalMath;
protected function setUp()
{
$this->EvalMath = new EvalMath();
}
/**
* @throws AbstractEvalMathException
*/
public function testBuiltInFunctionRedefinition()
{
$this->expectException(BuiltInFunctionRedefinitionException::class);
$this->EvalMath->evaluate('cos(x) = x*2');
}
/**
* @throws AbstractEvalMathException
*/
public function testConstantAssignmentException()
{
$this->expectException(ConstantAssignmentException::class);
$this->EvalMath->evaluate('pi = 123');
}
/**
* @throws AbstractEvalMathException
*/
public function testDivisionByZero()
{
$this->expectException(DivisionByZeroException::class);
$this->EvalMath->evaluate('5/0');
}
/**
* @throws AbstractEvalMathException
*/
public function testExpectingTokenException()
{
$this->expectException(ExpectingTokenException::class);
$this->EvalMath->evaluate('p = 6*(3+1');
}
/**
* @throws AbstractEvalMathException
*/
public function testIllegalCharacterException()
{
$this->expectException(IllegalCharacterException::class);
$this->EvalMath->evaluate('$v = 5');
}
/**
* @throws AbstractEvalMathException
*/
public function testInternalErrorException()
{
$this->expectException(InternalErrorException::class);
$this->EvalMath->e('k=');
}
/**
* @throws AbstractEvalMathException
*/
public function testInvalidArgumentCountException()
{
$this->expectException(InvalidArgumentCountException::class);
$this->EvalMath->e('a=sin(5,7,6)');
}
/**
* @throws AbstractEvalMathException
*/
public function testInvalidArgumentFuncCountException()
{
$this->expectException(InvalidArgumentCountException::class);
$this->EvalMath->e('iif(1)');
}
/**
* @throws AbstractEvalMathException
*/
public function testInvalidArgumentFuncCountException2()
{
$this->expectException(InvalidArgumentCountException::class);
$this->EvalMath->e('iif()');
}
/**
* @throws AbstractEvalMathException
*/
public function testInvalidArgumentUserFuncCountException()
{
$this->expectException(InvalidArgumentCountException::class);
$this->EvalMath->e('f(a,b) = a+b');
$this->EvalMath->e('f(1)');
}
/**
* @throws AbstractEvalMathException
*/
public function testOperatorLacksOperandException()
{
$this->expectException(OperatorLacksOperandException::class);
$this->EvalMath->e('k=5+');
}
/**
* @throws AbstractEvalMathException
*/
public function testOperatorRequiredException()
{
$this->expectException(OperatorRequiredException::class);
$this->EvalMath->e('a=5(7+1)');
}
/**
* @throws AbstractEvalMathException
*/
public function testUndefinedVariableException()
{
$this->expectException(UndefinedVariableException::class);
$this->EvalMath->e('b=a+1');
}
/**
* @throws AbstractEvalMathException
*/
public function testUndefinedVariableInFunctionDefinitionException()
{
$this->expectException(UndefinedVariableInFunctionDefinitionException::class);
$this->EvalMath->e('f(a) = b*3');
}
/**
* @throws AbstractEvalMathException
*/
public function testUnexpectedOperatorException()
{
$this->expectException(UnexpectedOperatorException::class);
$this->EvalMath->e('1 + * 2');
}
/**
* @throws AbstractEvalMathException
*/
public function testUnexpectedTokenException()
{
$this->expectException(UnexpectedTokenException::class);
$this->EvalMath->e('a=(3+)');
}
/**
* @throws AbstractEvalMathException
*/
public function testUnexpectedClosingPrenticeException()
{
$this->expectException(UnexpectedTokenException::class);
$this->EvalMath->e('a=5)');
}
/**
* @throws AbstractEvalMathException
*/
public function testUnexpectedComma()
{
$this->expectException(UnexpectedTokenException::class);
$this->EvalMath->e('a=5,3)');
}
/**
* @throws AbstractEvalMathException
*/
public function testUnexpectedComma2()
{
$this->expectException(UnexpectedTokenException::class);
$this->EvalMath->e('a=sin(0.3),3)');
}
/**
* @throws AbstractEvalMathException
*/
public function testUnexpectedComma3()
{
$this->expectException(UnexpectedTokenException::class);
$this->EvalMath->e('a=sin(0.1,)');
}
}

View File

@@ -0,0 +1,140 @@
<?php
/**
* EvalMathTest.php
*
* @author dbojdo - Daniel Bojdo <daniel.bojdo@8x8.com>
* Created on 02 12, 2016, 17:17
* Copyright (C) 8x8
*/
namespace Andileco\Util\EvalMath\Tests;
use PHPUnit\Framework\TestCase;
use Andileco\Util\EvalMath\EvalMath;
class EvalMathTest extends TestCase
{
/**
* @var EvalMath
*/
private $evalMath;
protected function setUp()
{
$this->evalMath = new EvalMath();
}
/**
* @test
* @dataProvider moduloOperatorData
*/
public function shouldSupportModuloOperator($formula, $values, $expectedResult)
{
foreach ($values as $k => $v) {
$this->evalMath->v[$k] = $v;
}
$this->assertEquals($expectedResult, $this->evalMath->evaluate($formula));
}
public function moduloOperatorData()
{
return array(
array(
'a%b', // 9%3 => 0
array('a' => 9, 'b' => 3),
0
),
array(
'a%b', // 10%3 => 1
array('a' => 10, 'b' => 3),
1
),
array(
'10-a%(b+c*d)', // 10-10%(7-2*2) => 9
array('a' => '10', 'b' => 7, 'c'=> -2, 'd' => 2),
9
)
);
}
/**
* @test
* @dataProvider doubleMinusData
*/
public function shouldConsiderDoubleMinusAsPlus($formula, $values, $expectedResult)
{
foreach ($values as $k => $v) {
$this->evalMath->v[$k] = $v;
}
$this->assertEquals(
$expectedResult,
$this->evalMath->evaluate($formula)
);
}
public function doubleMinusData()
{
return array(
array(
'a+b*c--d', // 1+2*3--4 => 1+6+4 => 11
array(
'a' => 1,
'b' => 2,
'c' => 3,
'd' => 4
),
11
),
array(
'a+b*c--d', // 1+2*3---4 => 1+6-4 => 3
array(
'a' => 1,
'b' => 2,
'c' => 3,
'd' => -4
),
3
)
);
}
/**
* @dataProvider maximumsData
*/
public function testMaxCalcFunction($formula, $expected)
{
$this->assertEquals($expected, $this->evalMath->e($formula));
}
public function maximumsData()
{
return [
['max(1,2,3,4,5)', 5],
['max(5,2,3,1,4)', 5],
['max(5)', 5],
['max(5,5)', 5]
];
}
/**
* @dataProvider comparisonsData
*/
public function testComparisonOperators($formula, $expected)
{
$this->assertEquals($expected, $this->evalMath->e($formula));
}
public function comparisonsData()
{
return [
['1==1', 1],
['1==2', 0],
['1>2', 0],
['1<2', 1],
['1>=1', 1],
['1<=2', 1]
];
}
}

View File

@@ -0,0 +1,63 @@
<?php
/**
* @author Serge Rodovnichenko <serge@syrnik.com>
* @copyright Serge Rodovnichenko, 2019
* @license BSD 2.0
*/
namespace Andileco\Util\EvalMath\Tests;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Andileco\Util\EvalMath\Methods\Maximum;
use Andileco\Util\EvalMath\MethodsRegistry;
class MethodRegistryTest extends TestCase
{
private $registry;
protected function setUp()
{
$this->registry = new MethodsRegistry();
}
public function testSetFindUnset()
{
$r = $this->registry->set(new Maximum);
$this->assertInstanceOf(MethodsRegistry::class, $r);
$c = $this->registry->findByName('max');
$this->assertInstanceOf(Maximum::class, $c);
$this->assertTrue(isset($this->registry['max']));
$c = $this->registry['max'];
$this->assertTrue(is_array($c));
// $this->assertInstanceOf(Maximum::class, $c);
$this->assertEquals(1, $this->registry->count());
unset($this->registry['max']);
$this->assertEquals(0, $this->registry->count());
$r = $this->registry->unsetByName('max');
$this->assertInstanceOf(MethodsRegistry::class, $r);
}
/**
* @expectedException RuntimeException
*/
public function testOffsetSetException()
{
$this->registry['max'] = new Maximum();
}
public function testAliases()
{
$this->registry = new MethodsRegistry();
$this->registry->set(new Maximum())->set((new Maximum)->setName('mmaaxx'));
$this->assertEquals(2, $this->registry->count());
$this->assertTrue(isset($this->registry['max']));
$this->assertTrue(isset($this->registry['mmaaxx']));
}
}

View File

@@ -0,0 +1,69 @@
<?php
/**
* @author Serge Rodovnichenko <serge@syrnik.com>
* @copyright Serge Rodovnichenko, 2019
* @license BSD 2.0
*/
namespace Andileco\Util\EvalMath\Tests;
use PHPUnit\Framework\TestCase;
use Andileco\Util\EvalMath\Methods\Conditional;
use Andileco\Util\EvalMath\Methods\Maximum;
use Andileco\Util\EvalMath\Methods\Minimum;
use Andileco\Util\EvalMath\Methods\Round;
class MethodsTest extends TestCase
{
public function testConditional()
{
$m = new Conditional();
$this->assertEquals('if', $m->getName());
$this->assertEquals([3], $m->getArgumentCount());
$this->assertEquals(1, $m->evaluate(true, 1, 0));
$this->assertEquals(0, $m->evaluate(false, 1, 0));
}
public function testMaximum()
{
$m = new Maximum();
$this->assertEquals('max', $m->getName());
$this->assertEquals([-1], $m->getArgumentCount());
$this->assertEquals(1, $m->evaluate(1, 0));
$this->assertEquals(10, $m->evaluate(false, 1, 0, 10));
$this->assertEquals(1, $m->evaluate(1));
}
public function testMinimum()
{
$m = new Minimum();
$this->assertEquals('min', $m->getName());
$this->assertEquals([-1], $m->getArgumentCount());
$this->assertEquals(0, $m->evaluate(1, 0));
$this->assertEquals(0, $m->evaluate(false, 1, 0, 10));
$this->assertEquals(1, $m->evaluate(1));
}
public function testRound()
{
$m = new Round();
$this->assertEquals('round', $m->getName());
$this->assertEquals([-1], $m->getArgumentCount());
$this->assertEquals(2, $m->evaluate(1.5321));
$this->assertEquals(1, $m->evaluate(1.4321));
$this->assertEquals(1, $m->evaluate(1.4921));
$this->assertEquals(git add .1.57, $m->evaluate(1.5678, 2));
$this->assertEquals(1.568, $m->evaluate(1.5678, 3));
$this->assertEquals(1.5678, $m->evaluate(1.5678, 4));
$this->assertEquals(1.54, $m->evaluate(1.5378, 2));
$this->assertEquals(1.538, $m->evaluate(1.5378, 3));
$this->assertEquals(1.5378, $m->evaluate(1.5378, 4));
$this->assertEquals(1.53, $m->evaluate(1.5321, 2));
$this->assertEquals(1.532, $m->evaluate(1.5321, 3));
$this->assertEquals(1.5321, $m->evaluate(1.5321, 4));
}
}

View File

@@ -0,0 +1,24 @@
<?php
/**
* @author Serge Rodovnichenko <serge@syrnik.com>
* @copyright Serge Rodovnichenko, 2019
* @license BSD 2.0
*/
namespace Andileco\Util\EvalMath\Tests;
use PHPUnit\Framework\TestCase;
use Andileco\Util\EvalMath\EvalMath;
class UserFunctionsTest extends TestCase
{
public function testFunctionsDump()
{
$e = new EvalMath();
$this->assertEmpty($e->funcs());
$e->e('f(x)=2*x');
$this->assertEquals(1, count($e->funcs()));
}
}

View File

@@ -0,0 +1,25 @@
<?php
/**
* @author Serge Rodovnichenko <serge@syrnik.com>
* @copyright Serge Rodovnichenko, 2019
* @license BSD 2.0
*/
namespace Andileco\Util\EvalMath\Tests;
use PHPUnit\Framework\TestCase;
use Andileco\Util\EvalMath\EvalMath;
class UserVariablesTest extends TestCase
{
public function testVariablesDump()
{
$e = new EvalMath();
$this->assertEmpty($e->vars());
$e->evaluate('a=1');
$this->assertArrayHasKey('a',$e->vars());
$this->assertEquals('1', $e->vars()['a']);
}
}