MDL-29173 - fix whitespace error
[moodle.git] / lib / tests / mathslib_test.php
CommitLineData
a3d5830a
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * Unit tests of mathslib wrapper and underlying EvalMath library.
19 *
20 * @package core
21 * @category phpunit
22 * @copyright 2007 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->libdir . '/mathslib.php');
30
31
32class mathsslib_testcase extends basic_testcase {
33
34 /**
35 * Tests the basic formula evaluation
36 */
37 public function test__basic() {
38 $formula = new calc_formula('=1+2');
39 $res = $formula->evaluate();
40 $this->assertEquals($res, 3, '3+1 is: %s');
41 }
42
43 /**
44 * Tests the formula params
45 */
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->assertEquals($res, 60, '10+20+30 is: %s');
50 }
51
52 /**
53 * Tests the changed params
54 */
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->assertEquals($res, 60, '10+20+30 is: %s');
59 $formula->set_params(array('a'=>1, 'b'=>2, 'c'=>3));
60 $res = $formula->evaluate();
61 $this->assertEquals($res, 6, 'changed params 1+2+3 is: %s');
62 }
63
64 /**
65 * Tests the spreadsheet emulation function in formula
66 */
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->assertEquals($res, 60, 'sum(a, b, c) is: %s');
71 }
72
73 public function test_other_functions() {
74 $formula = new calc_formula('=average(1,2,3)');
75 $this->assertEquals($formula->evaluate(), 2);
76
77 $formula = new calc_formula('=mod(10,3)');
78 $this->assertEquals($formula->evaluate(), 1);
79
80 $formula = new calc_formula('=power(2,3)');
81 $this->assertEquals($formula->evaluate(), 8);
82 }
83
84 /**
85 * Tests the min and max functions
86 */
87 public function test__minmax_function() {
88 $formula = new calc_formula('=min(a, b, c)', array('a'=>10, 'b'=>20, 'c'=>30));
89 $res = $formula->evaluate();
90 $this->assertEquals($res, 10, 'minimum is: %s');
91 $formula = new calc_formula('=max(a, b, c)', array('a'=>10, 'b'=>20, 'c'=>30));
92 $res = $formula->evaluate();
93 $this->assertEquals($res, 30, 'maximum is: %s');
94 }
95
96 /**
97 * Tests special chars
98 */
99 public function test__specialchars() {
100 $formula = new calc_formula('=gi1 + gi2 + gi11', array('gi1'=>10, 'gi2'=>20, 'gi11'=>30));
101 $res = $formula->evaluate();
102 $this->assertEquals($res, 60, 'sum is: %s');
103 }
104
105 /**
106 * Tests some slightly more complex expressions
107 */
108 public function test__more_complex_expressions() {
109 $formula = new calc_formula('=pi() + a', array('a'=>10));
110 $res = $formula->evaluate();
111 $this->assertEquals($res, pi()+10);
112 $formula = new calc_formula('=pi()^a', array('a'=>10));
113 $res = $formula->evaluate();
114 $this->assertEquals($res, pow(pi(), 10));
115 $formula = new calc_formula('=-8*(5/2)^2*(1-sqrt(4))-8');
116 $res = $formula->evaluate();
117 $this->assertEquals($res, -8*pow((5/2), 2)*(1-sqrt(4))-8);
118 }
119
120 /**
121 * Tests some slightly more complex expressions
122 */
123 public function test__error_handling() {
124 $formula = new calc_formula('=pi( + a', array('a'=>10));
125 $res = $formula->evaluate();
126 $this->assertEquals($res, false);
127 $this->assertEquals($formula->get_error(),
128 get_string('unexpectedoperator', 'mathslib', '+'));
129
130 $formula = new calc_formula('=pi(');
131 $res = $formula->evaluate();
132 $this->assertEquals($res, false);
133 $this->assertEquals($formula->get_error(),
134 get_string('expectingaclosingbracket', 'mathslib'));
135
136 $formula = new calc_formula('=pi()^');
137 $res = $formula->evaluate();
138 $this->assertEquals($res, false);
139 $this->assertEquals($formula->get_error(),
140 get_string('operatorlacksoperand', 'mathslib', '^'));
141
142 }
143
144 public function test_rounding_function() {
eb9f06f9
AD
145 // Rounding to the default number of decimal places
146 // The default == 0
147
a3d5830a
PS
148 $formula = new calc_formula('=round(2.5)');
149 $this->assertEquals($formula->evaluate(), 3);
150
151 $formula = new calc_formula('=round(1.5)');
152 $this->assertEquals($formula->evaluate(), 2);
153
154 $formula = new calc_formula('=round(-1.49)');
155 $this->assertEquals($formula->evaluate(), -1);
156
157 $formula = new calc_formula('=round(-2.49)');
158 $this->assertEquals($formula->evaluate(), -2);
159
160 $formula = new calc_formula('=round(-1.5)');
161 $this->assertEquals($formula->evaluate(), -2);
162
163 $formula = new calc_formula('=round(-2.5)');
164 $this->assertEquals($formula->evaluate(), -3);
165
166 $formula = new calc_formula('=ceil(2.5)');
167 $this->assertEquals($formula->evaluate(), 3);
168
169 $formula = new calc_formula('=ceil(1.5)');
170 $this->assertEquals($formula->evaluate(), 2);
171
172 $formula = new calc_formula('=ceil(-1.49)');
173 $this->assertEquals($formula->evaluate(), -1);
174
175 $formula = new calc_formula('=ceil(-2.49)');
176 $this->assertEquals($formula->evaluate(), -2);
177
178 $formula = new calc_formula('=ceil(-1.5)');
179 $this->assertEquals($formula->evaluate(), -1);
180
181 $formula = new calc_formula('=ceil(-2.5)');
182 $this->assertEquals($formula->evaluate(), -2);
183
184 $formula = new calc_formula('=floor(2.5)');
185 $this->assertEquals($formula->evaluate(), 2);
186
187 $formula = new calc_formula('=floor(1.5)');
188 $this->assertEquals($formula->evaluate(), 1);
189
190 $formula = new calc_formula('=floor(-1.49)');
191 $this->assertEquals($formula->evaluate(), -2);
192
193 $formula = new calc_formula('=floor(-2.49)');
194 $this->assertEquals($formula->evaluate(), -3);
195
196 $formula = new calc_formula('=floor(-1.5)');
197 $this->assertEquals($formula->evaluate(), -2);
198
199 $formula = new calc_formula('=floor(-2.5)');
200 $this->assertEquals($formula->evaluate(), -3);
201
eb9f06f9
AD
202 // Rounding to an explicit number of decimal places
203
204 $formula = new calc_formula('=round(2.5, 1)');
205 $this->assertEquals($formula->evaluate(), 2.5);
206
207 $formula = new calc_formula('=round(2.5, 0)');
208 $this->assertEquals($formula->evaluate(), 3);
209
210 $formula = new calc_formula('=round(1.2345, 2)');
211 $this->assertEquals($formula->evaluate(), 1.23);
212
213 $formula = new calc_formula('=round(123.456, -1)');
214 $this->assertEquals($formula->evaluate(), 120);
a3d5830a
PS
215 }
216
217 public function test_scientific_notation() {
218 $formula = new calc_formula('=10e10');
219 $this->assertEquals($formula->evaluate(), 1e11, '', 1e11*1e-15);
220
221 $formula = new calc_formula('=10e-10');
222 $this->assertEquals($formula->evaluate(), 1e-9, '', 1e11*1e-15);
223
224 $formula = new calc_formula('=10e+10');
225 $this->assertEquals($formula->evaluate(), 1e11, '', 1e11*1e-15);
226
227 $formula = new calc_formula('=10e10*5');
228 $this->assertEquals($formula->evaluate(), 5e11, '', 1e11*1e-15);
229
230 $formula = new calc_formula('=10e10^2');
231 $this->assertEquals($formula->evaluate(), 1e22, '', 1e22*1e-15);
232
233 }
234}