ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
ilMathBaseAdapterTest.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2017 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
9 {
10  const DEFAULT_SCALE = 50;
11 
15  protected $mathAdapter;
16 
20  protected $evalMath;
21 
25  protected function setUp()
26  {
27  require_once 'Services/Math/classes/class.ilMath.php';
28  require_once 'Services/Math/classes/class.EvalMath.php';
29 
30  ilMath::setDefaultAdapter($this->mathAdapter);
31  $this->evalMath = new EvalMath();
32  parent::setUp();
33  }
34 
38  public function testAdd($a, $b, $result, $scale)
39  {
40  $this->assertEquals($result, $this->mathAdapter->add($a, $b, $scale));
41  }
42 
46  public function testSub($a, $b, $result, $scale)
47  {
48  $this->assertEquals($result, $this->mathAdapter->sub($a, $b, $scale));
49  }
50 
54  public function testMul($a, $b, $result, $scale)
55  {
56  $this->assertEquals($result, $this->mathAdapter->mul($a, $b, $scale));
57  }
58 
62  public function testDiv($a, $b, $result, $scale)
63  {
64  $this->assertEquals($result, $this->mathAdapter->div($a, $b, $scale));
65  }
66 
70  public function testSqrt($a, $result, $scale)
71  {
72  $this->assertEquals($result, $this->mathAdapter->sqrt($a, $scale));
73  }
74 
78  public function testPow($a, $b, $result, $scale)
79  {
80  $this->assertEquals($result, $this->mathAdapter->pow($a, $b, $scale));
81  }
82 
86  public function testMod($a, $b, $result)
87  {
88  $this->assertEquals($result, $this->mathAdapter->mod($a, $b));
89  }
90 
94  public function testEquals($a, $b, $result, $scale)
95  {
96  $this->assertEquals($result, $this->mathAdapter->equals($a, $b, $scale));
97  }
98 
102  public function testCalculation($formula, $result, $scale)
103  {
104  $this->assertEquals($result, ilMath::_applyScale($this->evalMath->evaluate($formula), $scale));
105  }
106 
110  public function testDivisionsByZero()
111  {
112  $this->setExpectedException(ilMathDivisionByZeroException::class);
113 
114  $this->mathAdapter->div(1, 0);
115  }
116 
120  public function testModuloByZero()
121  {
122  $this->setExpectedException(ilMathDivisionByZeroException::class);
123 
124  $this->mathAdapter->mod(1, 0);
125  }
126 
130  public function addData()
131  {
132  return [
133  ['1', '2', '3', self::DEFAULT_SCALE]
134  ];
135  }
136 
140  public function subData()
141  {
142  return [
143  ['1', '2', '-1', self::DEFAULT_SCALE]
144  ];
145  }
146 
150  public function mulData()
151  {
152  return [
153  ['1', '2', '2', self::DEFAULT_SCALE],
154  ['1', '', '0', self::DEFAULT_SCALE]
155  ];
156  }
157 
161  public function divData()
162  {
163  return [
164  ['1', '2', '0.5', self::DEFAULT_SCALE],
165  ['', '2', '0', self::DEFAULT_SCALE],
166  ];
167  }
168 
172  public function modData()
173  {
174  return [
175  ['1', '2', '1']
176  ];
177  }
178 
182  public function sqrtData()
183  {
184  return [
185  ['9', '3', self::DEFAULT_SCALE],
186  ['4294967296', '65536', self::DEFAULT_SCALE],
187  ['12345678901234567890', '3513641828', null],
188  ['12345678901234567890', '3513641828.82', 2]
189  ];
190  }
191 
195  public function powData()
196  {
197  return [
198  ['3', '2', '9', self::DEFAULT_SCALE]
199  ];
200  }
201 
205  public function equalsData()
206  {
207  return [
208  ['3', '3', true, null],
209  ['27.424', '27.424', true, 5]
210  ];
211  }
212 
216  public function calcData()
217  {
218  return [
219  ['3+5', '8', self::DEFAULT_SCALE],
220  ['-3+5', '2', self::DEFAULT_SCALE],
221  ['3*6+5', '23', self::DEFAULT_SCALE],
222  ['10/2', '5', self::DEFAULT_SCALE],
223  ['13/60', '0.2166666666666', 13],
224  ['(-(-8)-sqrt((-8)^2-4*(7)))/(2)', '1', self::DEFAULT_SCALE],
225  ['(-(-8)+sqrt((-8)^2-4*(7)))/(2)', '7', self::DEFAULT_SCALE],
226  ['(-(-41)-sqrt((-41)^2-4*(1)*(5)))/(2*(1))', '0.122', 3],
227  ['(-(-41)+sqrt((-41)^2-4*(1)*(5)))/(2*(1))', '40.877', 3],
228  ['4^2-2*4+0.5*-16', '0', self::DEFAULT_SCALE],
229  ['-2^2-2*-2+0.5*-16', '-8', self::DEFAULT_SCALE]
230  ];
231  }
232 }
$result
testAdd($a, $b, $result, $scale)
addData
testMul($a, $b, $result, $scale)
mulData
testSub($a, $b, $result, $scale)
subData
testDiv($a, $b, $result, $scale)
divData
testSqrt($a, $result, $scale)
sqrtData
static setDefaultAdapter(ilMathAdapter $adapter)
testEquals($a, $b, $result, $scale)
equalsData
testMod($a, $b, $result)
modData
testCalculation($formula, $result, $scale)
calcData
testPow($a, $b, $result, $scale)
powData