Permalink
Browse files

NOBUG if we are going to reuse the functions used within the evalmath…

… code then

the calling api should not be weird with reversed parameters etc.
  • Loading branch information...
jamiepratt authored and timhunt committed May 31, 2011
1 parent 9085134 commit 1e257a3a54d68133bdc02f693a248203a2d2888b
Showing with 39 additions and 45 deletions.
  1. +26 −45 lib/evalmath/evalmath.class.php
  2. +13 −0 lib/simpletest/testmathslib.php
@@ -383,8 +383,7 @@ function pfx($tokens, $vars = array()) {
for ($i = $count-1; $i >= 0; $i--) {
if (is_null($args[] = $stack->pop())) return $this->trigger(get_string('internalerror', 'mathslib'));
}
$classname = 'EvalMathCalcEmul_'.$fnn;
$res = call_user_func(array($classname, 'calculate'), $args);
$res = call_user_func_array(array('EvalMathFuncs', $fnn), array_reverse($args));
if ($res === FALSE) {
return $this->trigger(get_string('internalerror', 'mathslib'));
}
@@ -473,16 +472,15 @@ function last($n=1) {
// spreadsheet functions emulation
// watch out for reversed args!!
class EvalMathCalcEmul_average {
class EvalMathFuncs {
static function calculate($args) {
return (EvalMathCalcEmul_sum::calculate($args)/count($args));
static function average() {
$args = func_get_args();
return (call_user_func_array(array('self', 'sum'), $args) / count($args));
}
}
class EvalMathCalcEmul_max {
static function calculate($args) {
static function max() {
$args = func_get_args();
$res = array_pop($args);
foreach($args as $a) {
if ($res < $a) {
@@ -491,10 +489,9 @@ static function calculate($args) {
}
return $res;
}
}
class EvalMathCalcEmul_min {
static function calculate($args) {
static function min() {
$args = func_get_args();
$res = array_pop($args);
foreach($args as $a) {
if ($res > $a) {
@@ -503,42 +500,32 @@ static function calculate($args) {
}
return $res;
}
}
class EvalMathCalcEmul_mod {
static function calculate($args) {
return $args[1] % $args[0];
static function mod($op1, $op2) {
return $op1 % $op2;
}
}
class EvalMathCalcEmul_pi {
static function calculate($args) {
static function pi() {
return pi();
}
}
class EvalMathCalcEmul_power {
static function calculate($args) {
return $args[1]^$args[0];
static function power($op1, $op2) {
return pow($op1, $op2);
}
}
class EvalMathCalcEmul_round {
static function calculate($args) {
if (count($args)==1) {
return round($args[0]);
} else {
return round($args[1], $args[0]);
}
static function round($val, $precision = 0) {
return round($val, $precision);
}
}
class EvalMathCalcEmul_sum {
static function calculate($args) {
static function sum() {
$args = func_get_args();
$res = 0;
foreach($args as $a) {
$res += $a;
}
return $res;
}
}
class EvalMathCalcEmul_randomised {
protected static $randomseed = null;
static function set_random_seed($randomseed) {
@@ -553,12 +540,7 @@ static function get_random_seed() {
}
}
}
class EvalMathCalcEmul_rand_int extends EvalMathCalcEmul_randomised {
static function calculate($args){
$min = $args[1];
$max = $args[0];
static function rand_int($min, $max){
if ($min >= $max) {
return false; //error
}
@@ -574,9 +556,8 @@ static function calculate($args){
} while (($min + $randomno) > $max);
return $min + $randomno;
}
}
class EvalMathCalcEmul_rand_float extends EvalMathCalcEmul_randomised {
static function calculate(){
static function rand_float(){
$randomvalue = array_shift(unpack('v', md5(self::get_random_seed(), true)));
return $randomvalue / 65536;
}
@@ -68,6 +68,17 @@ public function test__calc_function() {
$this->assertEqual($res, 60, 'sum(a, b, c) is: %s');
}
public function test_other_functions() {
$formula = new calc_formula('=average(1,2,3)');
$this->assertEqual($formula->evaluate(), 2);
$formula = new calc_formula('=mod(10,3)');
$this->assertEqual($formula->evaluate(), 1);
$formula = new calc_formula('=power(2,3)');
$this->assertEqual($formula->evaluate(), 8);
}
/**
* Tests the min and max functions
*/
@@ -212,6 +223,8 @@ public function test_scientific_notation() {
}
}

0 comments on commit 1e257a3

Please sign in to comment.