ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilMathTest.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3
9{
10 const DEFAULT_SCALE = '50';
11
15 protected $eval_math;
16
20 protected function setUp()
21 {
22 require_once 'Services/Math/classes/class.ilMath.php';
23 require_once 'Services/Math/classes/class.EvalMath.php';
24
25 $this->eval_math = new EvalMath();
26 }
27
31 public function testAdd($a, $b, $result, $scale)
32 {
33 $this->assertEquals($result, ilMath::_add($a, $b, $scale));
34 }
35
39 public function testSub($a, $b, $result, $scale)
40 {
41 $this->assertEquals($result, ilMath::_sub($a, $b, $scale));
42 }
43
47 public function testMul($a, $b, $result, $scale)
48 {
49 $this->assertEquals($result, ilMath::_mul($a, $b, $scale));
50 }
51
55 public function testDiv($a, $b, $result, $scale)
56 {
57 $this->assertEquals($result, ilMath::_div($a, $b, $scale));
58 }
59
63 public function testSqrt($a, $result, $scale)
64 {
65 $this->assertEquals($result, ilMath::_sqrt($a, $scale));
66 }
67
71 public function testPow($a, $b, $result, $scale)
72 {
73 $this->assertEquals($result, ilMath::_pow($a, $b, $scale));
74 }
75
79 public function testMod($a, $b, $result)
80 {
81 $this->assertEquals($result, ilMath::_mod($a, $b));
82 }
83
87 public function testEquals($a, $b, $result, $scale)
88 {
89 $this->assertEquals($result, ilMath::_equals($a, $b, $scale));
90 }
91
95 public function testRound($a, $result, $scale)
96 {
97 $this->assertEquals($result, ilMath::_round($a, $scale));
98 }
99
103 public function testGcd($a, $b, $result)
104 {
105 $this->assertEquals($result, ilMath::getGreatestCommonDivisor($a, $b));
106 }
107
111 public function testCalculation($formula, $result, $scale)
112 {
113 $this->assertEquals($result, ilMath::_round($this->eval_math->evaluate($formula), $scale));
114 }
115
116 /******************************************************************************************************************/
117
121 public function addData()
122 {
123 return [
124 ['1', '2', '3', self::DEFAULT_SCALE]
125 ];
126 }
127
131 public function subData()
132 {
133 return [
134 ['1', '2', '-1', self::DEFAULT_SCALE]
135 ];
136 }
137
141 public function mulData()
142 {
143 return [
144 ['1', '2', '2', self::DEFAULT_SCALE]
145 ];
146 }
147
151 public function divData()
152 {
153 return [
154 ['1', '2', '0.5', self::DEFAULT_SCALE]
155 ];
156 }
157
161 public function modData()
162 {
163 return [
164 ['1', '2', '1']
165 ];
166 }
167
171 public function sqrtData()
172 {
173 $values = [
174 ['9', '3', self::DEFAULT_SCALE],
175 ['4294967296', '65536', self::DEFAULT_SCALE]
176 ];
177 if(extension_loaded('bcmath'))
178 {
179 array_push($values, ['4294967296', '65536', self::DEFAULT_SCALE]);
180 }
181 return $values;
182 }
183
187 public function powData()
188 {
189 return [
190 ['3', '2', '9', self::DEFAULT_SCALE],
191 ['2', '64', '18446744073709551616', self::DEFAULT_SCALE]
192 ];
193 }
194
198 public function equalsData()
199 {
200 return [
201 ['3', '3', true, self::DEFAULT_SCALE]
202 ];
203 }
204
208 public function roundData()
209 {
210 return [
211 ['2.4742', '2.47', '2'],
212 ['2.4762', '2.48', '2']
213 ];
214 }
215
219 public function gcdData()
220 {
221 return [
222 ['1254', '5298', '6'],
223 ['41414124', '41414124', '41414124']
224 ];
225 }
226
230 public function calcData()
231 {
232 return [
233 ['3+5', '8', self::DEFAULT_SCALE],
234 ['-3+5', '2', self::DEFAULT_SCALE],
235 ['3*6+5', '23', self::DEFAULT_SCALE],
236 ['10/2', '5', self::DEFAULT_SCALE],
237 ['13/60', '0.2166666666667', '13'],
238 ['(-(-8)-sqrt((-8)^2-4*(7)))/(2)', '1', self::DEFAULT_SCALE],
239 ['(-(-8)+sqrt((-8)^2-4*(7)))/(2)', '7', self::DEFAULT_SCALE],
240 ['(-(-41)-sqrt((-41)^2-4*(1)*(5)))/(2*(1))', '0.122', '3'],
241 ['(-(-41)+sqrt((-41)^2-4*(1)*(5)))/(2*(1))', '40.878', '3'],
242 ['4^2-2*4+0.5*-16', '0', self::DEFAULT_SCALE],
243 ['-2^2-2*-2+0.5*-16', '-8', self::DEFAULT_SCALE]
244 ];
245 }
246}
$result
An exception for terminatinating execution or to throw for unit testing.
testEquals($a, $b, $result, $scale)
@dataProvider equalsData
Definition: ilMathTest.php:87
testSqrt($a, $result, $scale)
@dataProvider sqrtData
Definition: ilMathTest.php:63
testRound($a, $result, $scale)
@dataProvider roundData
Definition: ilMathTest.php:95
testPow($a, $b, $result, $scale)
@dataProvider powData
Definition: ilMathTest.php:71
testSub($a, $b, $result, $scale)
@dataProvider subData
Definition: ilMathTest.php:39
testDiv($a, $b, $result, $scale)
@dataProvider divData
Definition: ilMathTest.php:55
testMod($a, $b, $result)
@dataProvider modData
Definition: ilMathTest.php:79
const DEFAULT_SCALE
Definition: ilMathTest.php:10
testMul($a, $b, $result, $scale)
@dataProvider mulData
Definition: ilMathTest.php:47
testCalculation($formula, $result, $scale)
@dataProvider calcData
Definition: ilMathTest.php:111
testGcd($a, $b, $result)
@dataProvider gcdData
Definition: ilMathTest.php:103
testAdd($a, $b, $result, $scale)
@dataProvider addData
Definition: ilMathTest.php:31
static _mod($left_operand, $modulus)
static _mul($left_operand, $right_operand, $scale=50)
static _round($value, $precision=0)
static _sqrt($operand, $scale=50)
static _div($left_operand, $right_operand, $scale=50)
static _pow($left_operand, $right_operand, $scale=50)
static _equals($value1, $value2, $scale)
static getGreatestCommonDivisor($a, $b)
static _sub($left_operand, $right_operand, $scale=50)
static _add($left_operand, $right_operand, $scale=50)