2007-05-25 06:50:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if (!defined('MOODLE_INTERNAL')) {
|
|
|
|
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once($CFG->libdir . '/mathslib.php');
|
|
|
|
|
2007-05-26 12:59:13 +00:00
|
|
|
/**
|
|
|
|
* Unit tests of mathslib wrapper and underlying EvalMath library.
|
|
|
|
*
|
|
|
|
* @author Petr Skoda (skodak)
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
2009-01-10 13:23:37 +00:00
|
|
|
class mathsslib_test extends UnitTestCase {
|
2007-05-25 06:50:09 +00:00
|
|
|
|
2009-06-26 17:49:15 +00:00
|
|
|
public static $includecoverage = array('lib/mathslib.php');
|
|
|
|
|
2007-05-25 06:50:09 +00:00
|
|
|
/**
|
2007-05-26 12:59:13 +00:00
|
|
|
* Tests the basic formula evaluation
|
2007-05-25 06:50:09 +00:00
|
|
|
*/
|
|
|
|
function test__basic() {
|
2007-05-25 07:07:05 +00:00
|
|
|
$formula = new calc_formula('=1+2');
|
|
|
|
$res = $formula->evaluate();
|
2007-05-25 06:50:09 +00:00
|
|
|
$this->assertEqual($res, 3, '3+1 is: %s');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the formula params
|
|
|
|
*/
|
|
|
|
function test__params() {
|
2007-05-25 07:07:05 +00:00
|
|
|
$formula = new calc_formula('=a+b+c', array('a'=>10,'b'=>20,'c'=>30));
|
|
|
|
$res = $formula->evaluate();
|
2007-05-25 06:50:09 +00:00
|
|
|
$this->assertEqual($res, 60, '10+20+30 is: %s');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-05-26 12:59:13 +00:00
|
|
|
* Tests the changed params
|
|
|
|
*/
|
|
|
|
function test__changing_params() {
|
|
|
|
$formula = new calc_formula('=a+b+c', array('a'=>10,'b'=>20,'c'=>30));
|
|
|
|
$res = $formula->evaluate();
|
|
|
|
$this->assertEqual($res, 60, '10+20+30 is: %s');
|
|
|
|
$formula->set_params(array('a'=>1,'b'=>2,'c'=>3));
|
|
|
|
$res = $formula->evaluate();
|
|
|
|
$this->assertEqual($res, 6, 'changed params 1+2+3 is: %s');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the spreadsheet emulation function in formula
|
2007-05-25 06:50:09 +00:00
|
|
|
*/
|
|
|
|
function test__calc_function() {
|
2007-05-25 07:07:05 +00:00
|
|
|
$formula = new calc_formula('=sum(a,b,c)', array('a'=>10,'b'=>20,'c'=>30));
|
|
|
|
$res = $formula->evaluate();
|
2007-05-25 06:50:09 +00:00
|
|
|
$this->assertEqual($res, 60, 'sum(a,b,c) is: %s');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-05-26 12:59:13 +00:00
|
|
|
* Tests the min and max functions
|
2007-05-25 06:50:09 +00:00
|
|
|
*/
|
2007-05-26 12:59:13 +00:00
|
|
|
function test__minmax_function() {
|
|
|
|
$formula = new calc_formula('=min(a,b,c)', array('a'=>10,'b'=>20,'c'=>30));
|
2007-05-25 07:07:05 +00:00
|
|
|
$res = $formula->evaluate();
|
2007-05-26 12:59:13 +00:00
|
|
|
$this->assertEqual($res, 10, 'minimum is: %s');
|
|
|
|
$formula = new calc_formula('=max(a,b,c)', array('a'=>10,'b'=>20,'c'=>30));
|
2007-05-25 07:07:05 +00:00
|
|
|
$res = $formula->evaluate();
|
2007-05-26 12:59:13 +00:00
|
|
|
$this->assertEqual($res, 30, 'maximum is: %s');
|
2007-05-25 06:50:09 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 13:43:40 +00:00
|
|
|
/**
|
|
|
|
* Tests special chars
|
|
|
|
*/
|
|
|
|
function test__specialchars() {
|
|
|
|
$formula = new calc_formula('=gi1 + gi2 + gi11', array('gi1'=>10,'gi2'=>20,'gi11'=>30));
|
|
|
|
$res = $formula->evaluate();
|
|
|
|
$this->assertEqual($res, 60, 'sum is: %s');
|
|
|
|
}
|
|
|
|
|
2007-05-25 06:50:09 +00:00
|
|
|
}
|
|
|
|
|
2009-11-02 00:32:03 +00:00
|
|
|
|