2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Unit tests of mathslib wrapper and underlying EvalMath library.
22 * @copyright 2007 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->libdir
. '/mathslib.php');
32 class core_mathslib_testcase
extends basic_testcase
{
35 * Tests the basic formula evaluation.
37 public function test__basic() {
38 $formula = new calc_formula('=1+2');
39 $res = $formula->evaluate();
40 $this->assertSame($res, 3, '3+1 is: %s');
44 * Tests the formula params.
46 public function test__params() {
47 $formula = new calc_formula('=a+b+c', array('a'=>10, 'b'=>20, 'c'=>30));
48 $res = $formula->evaluate();
49 $this->assertSame(60, $res, '10+20+30 is: %s');
53 * Tests the changed params.
55 public function test__changing_params() {
56 $formula = new calc_formula('=a+b+c', array('a'=>10, 'b'=>20, 'c'=>30));
57 $res = $formula->evaluate();
58 $this->assertSame(60, $res, '10+20+30 is: %s');
59 $formula->set_params(array('a'=>1, 'b'=>2, 'c'=>3));
60 $res = $formula->evaluate();
61 $this->assertSame(6, $res, 'changed params 1+2+3 is: %s');
65 * Tests the spreadsheet emulation function in formula.
67 public function test__calc_function() {
68 $formula = new calc_formula('=sum(a, b, c)', array('a'=>10, 'b'=>20, 'c'=>30));
69 $res = $formula->evaluate();
70 $this->assertSame(60, $res, 'sum(a, b, c) is: %s');
73 public function test_other_functions() {
74 $formula = new calc_formula('=average(1,2,3)');
75 $this->assertSame(2, $formula->evaluate());
77 $formula = new calc_formula('=mod(10,3)');
78 $this->assertSame(1, $formula->evaluate());
80 $formula = new calc_formula('=power(2,3)');
81 $this->assertSame(8, $formula->evaluate());
84 public function test_conditional_functions() {
85 $formula = new calc_formula('=ifthenelse(1,2,3)');
86 $this->assertSame(2, (int)$formula->evaluate());
88 $formula = new calc_formula('=ifthenelse(0,2,3)');
89 $this->assertSame(3, (int) $formula->evaluate());
91 $formula = new calc_formula('=ifthenelse(2<3,2,3)');
92 $this->assertSame(2, (int) $formula->evaluate());
95 $formula = new calc_formula('=if(1,2,3)');
96 $this->assertSame(2, (int)$formula->evaluate());
98 $formula = new calc_formula('=if(0,2,3)');
99 $this->assertSame(3, (int) $formula->evaluate());
101 $formula = new calc_formula('=if(2<3,2,3)');
102 $this->assertSame(2, (int) $formula->evaluate());
105 public function test_conditional_operators() {
106 $formula = new calc_formula('=2==2');
107 $this->assertSame(1, $formula->evaluate());
109 $formula = new calc_formula('=2>3');
110 $this->assertSame(0, $formula->evaluate());
111 $formula = new calc_formula('=2<3');
112 $this->assertSame(1, $formula->evaluate());
114 $formula = new calc_formula('=(2<=3)');
115 $this->assertSame(1, $formula->evaluate());
117 $formula = new calc_formula('=(2<=3)*10');
118 $this->assertSame(10, $formula->evaluate());
120 $formula = new calc_formula('=(2>=3)*10');
121 $this->assertSame(0, $formula->evaluate());
122 $formula = new calc_formula('=2<3*10');
123 $this->assertSame(10, $formula->evaluate());
126 * Tests the min and max functions.
128 public function test__minmax_function() {
129 $formula = new calc_formula('=min(a, b, c)', array('a'=>10, 'b'=>20, 'c'=>30));
130 $res = $formula->evaluate();
131 $this->assertSame(10, $res, 'minimum is: %s');
132 $formula = new calc_formula('=max(a, b, c)', array('a'=>10, 'b'=>20, 'c'=>30));
133 $res = $formula->evaluate();
134 $this->assertSame(30, $res, 'maximum is: %s');
138 * Tests special chars.
140 public function test__specialchars() {
141 $formula = new calc_formula('=gi1 + gi2 + gi11', array('gi1'=>10, 'gi2'=>20, 'gi11'=>30));
142 $res = $formula->evaluate();
143 $this->assertSame(60, $res, 'sum is: %s');
147 * Tests some slightly more complex expressions.
149 public function test__more_complex_expressions() {
150 $formula = new calc_formula('=pi() + a', array('a'=>10));
151 $res = $formula->evaluate();
152 $this->assertSame(pi()+
10, $res);
153 $formula = new calc_formula('=pi()^a', array('a'=>10));
154 $res = $formula->evaluate();
155 $this->assertSame(pow(pi(), 10), $res);
156 $formula = new calc_formula('=-8*(5/2)^2*(1-sqrt(4))-8');
157 $res = $formula->evaluate();
158 $this->assertSame(-8*pow((5/2), 2)*(1-sqrt(4))-8, $res);
162 * Tests some slightly more complex expressions.
164 public function test__error_handling() {
165 $formula = new calc_formula('=pi( + a', array('a'=>10));
166 $res = $formula->evaluate();
167 $this->assertFalse($res);
168 $this->assertSame(get_string('unexpectedoperator', 'mathslib', '+'), $formula->get_error());
170 $formula = new calc_formula('=pi(');
171 $res = $formula->evaluate();
172 $this->assertSame($res, false);
173 $this->assertSame(get_string('expectingaclosingbracket', 'mathslib'), $formula->get_error());
175 $formula = new calc_formula('=pi()^');
176 $res = $formula->evaluate();
177 $this->assertSame($res, false);
178 $this->assertSame(get_string('operatorlacksoperand', 'mathslib', '^'), $formula->get_error());
182 public function test_rounding_function() {
183 // Rounding to the default number of decimal places.
186 $formula = new calc_formula('=round(2.5)');
187 $this->assertSame(3.0, $formula->evaluate());
189 $formula = new calc_formula('=round(1.5)');
190 $this->assertSame(2.0, $formula->evaluate());
192 $formula = new calc_formula('=round(-1.49)');
193 $this->assertSame(-1.0, $formula->evaluate());
195 $formula = new calc_formula('=round(-2.49)');
196 $this->assertSame(-2.0, $formula->evaluate());
198 $formula = new calc_formula('=round(-1.5)');
199 $this->assertSame(-2.0, $formula->evaluate());
201 $formula = new calc_formula('=round(-2.5)');
202 $this->assertSame(-3.0, $formula->evaluate());
204 $formula = new calc_formula('=ceil(2.5)');
205 $this->assertSame(3.0, $formula->evaluate());
207 $formula = new calc_formula('=ceil(1.5)');
208 $this->assertSame(2.0, $formula->evaluate());
210 $formula = new calc_formula('=ceil(-1.49)');
211 $this->assertSame(-1.0, $formula->evaluate());
213 $formula = new calc_formula('=ceil(-2.49)');
214 $this->assertSame(-2.0, $formula->evaluate());
216 $formula = new calc_formula('=ceil(-1.5)');
217 $this->assertSame(-1.0, $formula->evaluate());
219 $formula = new calc_formula('=ceil(-2.5)');
220 $this->assertSame(-2.0, $formula->evaluate());
222 $formula = new calc_formula('=floor(2.5)');
223 $this->assertSame(2.0, $formula->evaluate());
225 $formula = new calc_formula('=floor(1.5)');
226 $this->assertSame(1.0, $formula->evaluate());
228 $formula = new calc_formula('=floor(-1.49)');
229 $this->assertSame(-2.0, $formula->evaluate());
231 $formula = new calc_formula('=floor(-2.49)');
232 $this->assertSame(-3.0, $formula->evaluate());
234 $formula = new calc_formula('=floor(-1.5)');
235 $this->assertSame(-2.0, $formula->evaluate());
237 $formula = new calc_formula('=floor(-2.5)');
238 $this->assertSame(-3.0, $formula->evaluate());
240 // Rounding to an explicit number of decimal places.
242 $formula = new calc_formula('=round(2.5, 1)');
243 $this->assertSame(2.5, $formula->evaluate());
245 $formula = new calc_formula('=round(2.5, 0)');
246 $this->assertSame(3.0, $formula->evaluate());
248 $formula = new calc_formula('=round(1.2345, 2)');
249 $this->assertSame(1.23, $formula->evaluate());
251 $formula = new calc_formula('=round(123.456, -1)');
252 $this->assertSame(120.0, $formula->evaluate());
255 public function test_scientific_notation() {
256 $formula = new calc_formula('=10e10');
257 $this->assertEquals(1e11
, $formula->evaluate(), '', 1e11
*1e-15);
259 $formula = new calc_formula('=10e-10');
260 $this->assertEquals(1e-9, $formula->evaluate(), '', 1e11
*1e-15);
262 $formula = new calc_formula('=10e+10');
263 $this->assertEquals(1e11
, $formula->evaluate(), '', 1e11
*1e-15);
265 $formula = new calc_formula('=10e10*5');
266 $this->assertEquals(5e11
, $formula->evaluate(), '', 1e11
*1e-15);
268 $formula = new calc_formula('=10e10^2');
269 $this->assertEquals(1e22
, $formula->evaluate(), '', 1e22
*1e-15);
272 public function test_rand_float() {
273 $formula = new calc_formula('=rand_float()');
274 $result = $formula->evaluate();
275 $this->assertTrue(is_float($result));