forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-9643 mathslib.php library, unit tests, removed e and pi constants
- Loading branch information
skodak
committed
May 25, 2007
1 parent
2d53fa5
commit ffaa6c4
Showing
4 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
require_once $CFG->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; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* $Id$ */ | ||
|
||
if (!defined('MOODLE_INTERNAL')) { | ||
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page | ||
} | ||
|
||
global $CFG; | ||
require_once($CFG->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'); | ||
} | ||
|
||
} | ||
|
||
?> |