Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 11
Línea 34... Línea 34...
34
class mathslib_test extends \basic_testcase {
34
class mathslib_test extends \basic_testcase {
Línea 35... Línea 35...
35
 
35
 
36
    /**
36
    /**
37
     * Tests the basic formula evaluation.
37
     * Tests the basic formula evaluation.
38
     */
38
     */
39
    public function test__basic() {
39
    public function test__basic(): void {
40
        $formula = new calc_formula('=1+2');
40
        $formula = new calc_formula('=1+2');
41
        $res = $formula->evaluate();
41
        $res = $formula->evaluate();
42
        $this->assertSame($res, 3, '3+1 is: %s');
42
        $this->assertSame($res, 3, '3+1 is: %s');
Línea 43... Línea 43...
43
    }
43
    }
44
 
44
 
45
    /**
45
    /**
46
     * Tests the formula params.
46
     * Tests the formula params.
47
     */
47
     */
48
    public function test__params() {
48
    public function test__params(): void {
49
        $formula = new calc_formula('=a+b+c', array('a'=>10, 'b'=>20, 'c'=>30));
49
        $formula = new calc_formula('=a+b+c', array('a'=>10, 'b'=>20, 'c'=>30));
50
        $res = $formula->evaluate();
50
        $res = $formula->evaluate();
Línea 51... Línea 51...
51
        $this->assertSame(60, $res, '10+20+30 is: %s');
51
        $this->assertSame(60, $res, '10+20+30 is: %s');
52
    }
52
    }
53
 
53
 
54
    /**
54
    /**
55
     * Tests the changed params.
55
     * Tests the changed params.
56
     */
56
     */
57
    public function test__changing_params() {
57
    public function test__changing_params(): void {
58
        $formula = new calc_formula('=a+b+c', array('a'=>10, 'b'=>20, 'c'=>30));
58
        $formula = new calc_formula('=a+b+c', array('a'=>10, 'b'=>20, 'c'=>30));
59
        $res = $formula->evaluate();
59
        $res = $formula->evaluate();
Línea 64... Línea 64...
64
    }
64
    }
Línea 65... Línea 65...
65
 
65
 
66
    /**
66
    /**
67
     * Tests the spreadsheet emulation function in formula.
67
     * Tests the spreadsheet emulation function in formula.
68
     */
68
     */
69
    public function test__calc_function() {
69
    public function test__calc_function(): void {
70
        $formula = new calc_formula('=sum(a, b, c)', array('a'=>10, 'b'=>20, 'c'=>30));
70
        $formula = new calc_formula('=sum(a, b, c)', array('a'=>10, 'b'=>20, 'c'=>30));
71
        $res = $formula->evaluate();
71
        $res = $formula->evaluate();
72
        $this->assertSame(60, $res, 'sum(a, b, c) is: %s');
72
        $this->assertSame(60, $res, 'sum(a, b, c) is: %s');
Línea 73... Línea 73...
73
    }
73
    }
74
 
74
 
75
    public function test_other_functions() {
75
    public function test_other_functions(): void {
Línea 76... Línea 76...
76
        $formula = new calc_formula('=average(1,2,3)');
76
        $formula = new calc_formula('=average(1,2,3)');
77
        $this->assertSame(2, $formula->evaluate());
77
        $this->assertSame(2, $formula->evaluate());
Línea 78... Línea 78...
78
 
78
 
79
        $formula = new calc_formula('=mod(10,3)');
79
        $formula = new calc_formula('=mod(10,3)');
80
        $this->assertSame(1, $formula->evaluate());
80
        $this->assertSame(1, $formula->evaluate());
Línea 81... Línea 81...
81
 
81
 
82
        $formula = new calc_formula('=power(2,3)');
82
        $formula = new calc_formula('=power(2,3)');
83
        $this->assertSame(8, $formula->evaluate());
83
        $this->assertSame(8, $formula->evaluate());
84
    }
84
    }
Línea 85... Línea 85...
85
 
85
 
Línea 143... Línea 143...
143
 
143
 
144
        $formula = new calc_formula('=or(0,0,0)');
144
        $formula = new calc_formula('=or(0,0,0)');
145
        $this->assertSame(0, (int) $formula->evaluate());
145
        $this->assertSame(0, (int) $formula->evaluate());
Línea 146... Línea 146...
146
    }
146
    }
147
 
147
 
148
    public function test_conditional_operators() {
148
    public function test_conditional_operators(): void {
Línea 149... Línea 149...
149
        $formula = new calc_formula('=2==2');
149
        $formula = new calc_formula('=2==2');
150
        $this->assertSame(1, $formula->evaluate());
150
        $this->assertSame(1, $formula->evaluate());
Línea 166... Línea 166...
166
        $this->assertSame(10, $formula->evaluate());
166
        $this->assertSame(10, $formula->evaluate());
167
    }
167
    }
168
    /**
168
    /**
169
     * Tests the min and max functions.
169
     * Tests the min and max functions.
170
     */
170
     */
171
    public function test__minmax_function() {
171
    public function test__minmax_function(): void {
172
        $formula = new calc_formula('=min(a, b, c)', array('a'=>10, 'b'=>20, 'c'=>30));
172
        $formula = new calc_formula('=min(a, b, c)', array('a'=>10, 'b'=>20, 'c'=>30));
173
        $res = $formula->evaluate();
173
        $res = $formula->evaluate();
174
        $this->assertSame(10, $res, 'minimum is: %s');
174
        $this->assertSame(10, $res, 'minimum is: %s');
175
        $formula = new calc_formula('=max(a, b, c)', array('a'=>10, 'b'=>20, 'c'=>30));
175
        $formula = new calc_formula('=max(a, b, c)', array('a'=>10, 'b'=>20, 'c'=>30));
176
        $res = $formula->evaluate();
176
        $res = $formula->evaluate();
Línea 178... Línea 178...
178
    }
178
    }
Línea 179... Línea 179...
179
 
179
 
180
    /**
180
    /**
181
     * Tests special chars.
181
     * Tests special chars.
182
     */
182
     */
183
    public function test__specialchars() {
183
    public function test__specialchars(): void {
184
        $formula = new calc_formula('=gi1 + gi2 + gi11', array('gi1'=>10, 'gi2'=>20, 'gi11'=>30));
184
        $formula = new calc_formula('=gi1 + gi2 + gi11', array('gi1'=>10, 'gi2'=>20, 'gi11'=>30));
185
        $res = $formula->evaluate();
185
        $res = $formula->evaluate();
186
        $this->assertSame(60, $res, 'sum is: %s');
186
        $this->assertSame(60, $res, 'sum is: %s');
Línea 187... Línea 187...
187
    }
187
    }
188
 
188
 
189
    /**
189
    /**
190
     * Tests some slightly more complex expressions.
190
     * Tests some slightly more complex expressions.
191
     */
191
     */
192
    public function test__more_complex_expressions() {
192
    public function test__more_complex_expressions(): void {
193
        $formula = new calc_formula('=pi() + a', array('a'=>10));
193
        $formula = new calc_formula('=pi() + a', array('a'=>10));
194
        $res = $formula->evaluate();
194
        $res = $formula->evaluate();
195
        $this->assertSame(pi()+10, $res);
195
        $this->assertSame(pi()+10, $res);
Línea 202... Línea 202...
202
    }
202
    }
Línea 203... Línea 203...
203
 
203
 
204
    /**
204
    /**
205
     * Tests some slightly more complex expressions.
205
     * Tests some slightly more complex expressions.
206
     */
206
     */
207
    public function test__error_handling() {
207
    public function test__error_handling(): void {
208
        $formula = new calc_formula('=pi( + a', array('a'=>10));
208
        $formula = new calc_formula('=pi( + a', array('a'=>10));
209
        $res = $formula->evaluate();
209
        $res = $formula->evaluate();
210
        $this->assertFalse($res);
210
        $this->assertFalse($res);
Línea 220... Línea 220...
220
        $this->assertSame($res, false);
220
        $this->assertSame($res, false);
221
        $this->assertSame(get_string('operatorlacksoperand', 'mathslib', '^'), $formula->get_error());
221
        $this->assertSame(get_string('operatorlacksoperand', 'mathslib', '^'), $formula->get_error());
Línea 222... Línea 222...
222
 
222
 
Línea 223... Línea 223...
223
    }
223
    }
224
 
224
 
225
    public function test_rounding_function() {
225
    public function test_rounding_function(): void {
Línea 226... Línea 226...
226
        // Rounding to the default number of decimal places.
226
        // Rounding to the default number of decimal places.
227
        // The default == 0.
227
        // The default == 0.
Línea 293... Línea 293...
293
 
293
 
294
        $formula = new calc_formula('=round(123.456, -1)');
294
        $formula = new calc_formula('=round(123.456, -1)');
295
        $this->assertSame(120.0, $formula->evaluate());
295
        $this->assertSame(120.0, $formula->evaluate());
Línea 296... Línea 296...
296
    }
296
    }
297
 
297
 
298
    public function test_scientific_notation() {
298
    public function test_scientific_notation(): void {
Línea 299... Línea 299...
299
        $formula = new calc_formula('=10e10');
299
        $formula = new calc_formula('=10e10');
300
        $this->assertEqualsWithDelta(1e11, $formula->evaluate(), 1e11 * 1e-15);
300
        $this->assertEqualsWithDelta(1e11, $formula->evaluate(), 1e11 * 1e-15);
Línea 310... Línea 310...
310
 
310
 
311
        $formula = new calc_formula('=10e10^2');
311
        $formula = new calc_formula('=10e10^2');
312
        $this->assertEqualsWithDelta(1e22, $formula->evaluate(), 1e22 * 1e-15);
312
        $this->assertEqualsWithDelta(1e22, $formula->evaluate(), 1e22 * 1e-15);
Línea 313... Línea 313...
313
    }
313
    }
314
 
314
 
315
    public function test_rand_float() {
315
    public function test_rand_float(): void {
316
        $formula = new calc_formula('=rand_float()');
316
        $formula = new calc_formula('=rand_float()');
317
        $result = $formula->evaluate();
317
        $result = $formula->evaluate();