From ffaa6c4abd8f2b89b060cf80e067879d7c145513 Mon Sep 17 00:00:00 2001 From: skodak Date: Fri, 25 May 2007 06:50:09 +0000 Subject: [PATCH] MDL-9643 mathslib.php library, unit tests, removed e and pi constants --- lib/evalmath/evalmath.class.php | 5 +-- lib/evalmath/readme_moodle.txt | 1 + lib/mathslib.php | 57 +++++++++++++++++++++++++++++++++ lib/simpletest/testmathslib.php | 56 ++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 lib/mathslib.php create mode 100755 lib/simpletest/testmathslib.php diff --git a/lib/evalmath/evalmath.class.php b/lib/evalmath/evalmath.class.php index f689e2840d..2270d626e0 100644 --- a/lib/evalmath/evalmath.class.php +++ b/lib/evalmath/evalmath.class.php @@ -91,7 +91,7 @@ class EvalMath { var $suppress_errors = false; var $last_error = null; - var $v = array('e'=>2.71,'pi'=>3.14); // variables (and constants) + var $v = array(); // variables (and constants) var $f = array(); // user-defined functions var $vb = array('e', 'pi'); // constants var $fb = array( // built-in functions @@ -104,9 +104,6 @@ class EvalMath { 'sum'=>array(-1), 'pi'=>array(0), 'power'=>array(2), 'round'=>array(2,1), 'average'=>array(-1)); function EvalMath() { - // make the variables a little more accurate - $this->v['pi'] = pi(); - $this->v['e'] = exp(1); } function e($expr) { diff --git a/lib/evalmath/readme_moodle.txt b/lib/evalmath/readme_moodle.txt index 20301333d0..00bc223824 100644 --- a/lib/evalmath/readme_moodle.txt +++ b/lib/evalmath/readme_moodle.txt @@ -3,6 +3,7 @@ Description of MathEval library import into Moodle Our changes: * implicit multiplication not allowed * new custom calc emulation functions +* removed e and pi constants - not use din calc To see all changes diff against version 1.1 diff --git a/lib/mathslib.php b/lib/mathslib.php new file mode 100644 index 0000000000..fa4f7abfd0 --- /dev/null +++ b/lib/mathslib.php @@ -0,0 +1,57 @@ +dirroot.'/lib/evalmath/evalmath.class.php'; + +class calc_formula { + + var $em; + var $nfx = false; + var $error = false; + + function calc_formula($formula, $params=false) { + $this->em = new EvalMath(); + $this->em->suppress_errors = true; + if (strpos($formula, '=') !== 0) { + $this->error = "missing '='"; + return; + } + $formula = substr($formula, 1); + if (strpos($formula, '=') !== false) { + $this->error = "too many '='"; + return; + } + $this->nfx = $this->em->nfx($formula); + if ($this->nfx == false) { + $this->error = $this->em->last_error; + return; + } + if ($params != false) { + $this->em->v = $params; + } + } + + function set_params($params) { + $this->em->v = $params; + } + + function evaluate() { + if ($this->nfx == false) { + return false; + } + $res = $this->em->pfx($this->nfx); + if ($res === false) { + $this->error = $this->em->last_error; + return false; + } else { + $this->error = false; + return $res; + } + + } + + function get_error() { + return $this->error; + } +} + +?> \ No newline at end of file diff --git a/lib/simpletest/testmathslib.php b/lib/simpletest/testmathslib.php new file mode 100755 index 0000000000..541a3f9678 --- /dev/null +++ b/lib/simpletest/testmathslib.php @@ -0,0 +1,56 @@ +libdir . '/simpletestlib.php'); +require_once($CFG->libdir . '/mathslib.php'); + +class mathsslib_test extends UnitTestCase { + + /** + * Tests the basic formula execition + */ + function test__basic() { + $calc = new calc_formula('=1+2'); + $res = $calc->evaluate(); + $this->assertEqual($res, 3, '3+1 is: %s'); + } + + /** + * Tests the formula params + */ + function test__params() { + $calc = new calc_formula('=a+b+c', array('a'=>10,'b'=>20,'c'=>30)); + $res = $calc->evaluate(); + $this->assertEqual($res, 60, '10+20+30 is: %s'); + } + + /** + * Tests the formula params + */ + function test__calc_function() { + $calc = new calc_formula('=sum(a,b,c)', array('a'=>10,'b'=>20,'c'=>30)); + $res = $calc->evaluate(); + $this->assertEqual($res, 60, 'sum(a,b,c) is: %s'); + } + + /** + * Tests the formula changed params + */ + function test__changing_params() { + $calc = new calc_formula('=a+b+c', array('a'=>10,'b'=>20,'c'=>30)); + $res = $calc->evaluate(); + $this->assertEqual($res, 60, '10+20+30 is: %s'); + $calc->set_params(array('a'=>1,'b'=>2,'c'=>3)); + $res = $calc->evaluate(); + $this->assertEqual($res, 6, '1+2+3 is: %s'); + } + +} + +?> \ No newline at end of file -- 2.39.5