ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
7 
12 abstract class ilMathBaseAdapterTest extends TestCase
13 {
14  const DEFAULT_SCALE = 50;
15 
19  protected $mathAdapter;
20 
24  protected $evalMath;
25 
29  protected function setUp() : void
30  {
31  require_once 'Services/Math/classes/class.ilMath.php';
32  require_once 'Services/Math/classes/class.EvalMath.php';
33 
34  ilMath::setDefaultAdapter($this->mathAdapter);
35  $this->evalMath = new EvalMath();
36  parent::setUp();
37  }
38 
47  private function assertEqualNumbers(string $actual, string $expected)
48  {
49  $differ = new Differ(new UnifiedDiffOutputBuilder("\n--- Expected\n+++ Actual\n"));
50 
51  $this->assertTrue($actual == $expected, $differ->diff($actual, $expected));
52  }
53 
57  public function testAdd($a, $b, $result, $scale)
58  {
59  $this->assertEqualNumbers($result, $this->mathAdapter->add($a, $b, $scale));
60  }
61 
65  public function testSub($a, $b, $result, $scale)
66  {
67  $this->assertEqualNumbers($result, $this->mathAdapter->sub($a, $b, $scale));
68  }
69 
73  public function testMul($a, $b, $result, $scale)
74  {
75  $this->assertEqualNumbers($result, $this->mathAdapter->mul($a, $b, $scale));
76  }
77 
81  public function testDiv($a, $b, $result, $scale)
82  {
83  $this->assertEqualNumbers($result, $this->mathAdapter->div($a, $b, $scale));
84  }
85 
89  public function testSqrt($a, $result, $scale)
90  {
91  $this->assertEqualNumbers($result, $this->mathAdapter->sqrt($a, $scale));
92  }
93 
97  public function testPow($a, $b, $result, $scale)
98  {
99  $this->assertEqualNumbers($result, $this->mathAdapter->pow($a, $b, $scale));
100  }
101 
105  public function testMod($a, $b, $result)
106  {
107  $this->assertEqualNumbers($result, $this->mathAdapter->mod($a, $b));
108  }
109 
113  public function testEquals($a, $b, $result, $scale)
114  {
115  $this->assertEqualNumbers($result, $this->mathAdapter->equals($a, $b, $scale));
116  }
117 
121  public function testCalculation($formula, $result, $scale)
122  {
123  $this->assertEqualNumbers($result, ilMath::_applyScale($this->evalMath->evaluate($formula), $scale));
124  }
125 
129  public function testDivisionsByZero()
130  {
131  $this->expectException(ilMathDivisionByZeroException::class);
132 
133  $this->mathAdapter->div(1, 0);
134  }
135 
139  public function testModuloByZero()
140  {
141  $this->expectException(ilMathDivisionByZeroException::class);
142 
143  $this->mathAdapter->mod(1, 0);
144  }
145 
149  public function addData()
150  {
151  return [
152  ['1', '2', '3', self::DEFAULT_SCALE]
153  ];
154  }
155 
159  public function subData()
160  {
161  return [
162  ['1', '2', '-1', self::DEFAULT_SCALE]
163  ];
164  }
165 
169  public function mulData()
170  {
171  return [
172  ['1', '2', '2', self::DEFAULT_SCALE],
173  ['1', '', '0', self::DEFAULT_SCALE]
174  ];
175  }
176 
180  public function divData()
181  {
182  return [
183  ['1', '2', '0.5', self::DEFAULT_SCALE],
184  ['', '2', '0', self::DEFAULT_SCALE],
185  ];
186  }
187 
191  public function modData()
192  {
193  return [
194  ['1', '2', '1']
195  ];
196  }
197 
201  public function sqrtData()
202  {
203  return [
204  ['9', '3', self::DEFAULT_SCALE],
205  ['4294967296', '65536', self::DEFAULT_SCALE],
206  ['12345678901234567890', '3513641828', null],
207  ['12345678901234567890', '3513641828.82', 2]
208  ];
209  }
210 
214  public function powData()
215  {
216  return [
217  ['3', '2', '9', self::DEFAULT_SCALE]
218  ];
219  }
220 
224  public function equalsData()
225  {
226  return [
227  ['3', '3', true, null],
228  ['27.424', '27.424', true, 5]
229  ];
230  }
231 
235  public function calcData()
236  {
237  return [
238  ['3+5', '8', self::DEFAULT_SCALE],
239  ['-3+5', '2', self::DEFAULT_SCALE],
240  ['3*6+5', '23', self::DEFAULT_SCALE],
241  ['10/2', '5', self::DEFAULT_SCALE],
242  ['13/60', '0.2166666666666', 13],
243  ['(-(-8)-sqrt((-8)^2-4*(7)))/(2)', '1', self::DEFAULT_SCALE],
244  ['(-(-8)+sqrt((-8)^2-4*(7)))/(2)', '7', self::DEFAULT_SCALE],
245  ['(-(-41)-sqrt((-41)^2-4*(1)*(5)))/(2*(1))', '0.122', 3],
246  ['(-(-41)+sqrt((-41)^2-4*(1)*(5)))/(2*(1))', '40.877', 3],
247  ['4^2-2*4+0.5*-16', '0', self::DEFAULT_SCALE],
248  ['-2^2-2*-2+0.5*-16', '-8', self::DEFAULT_SCALE]
249  ];
250  }
251 }
$result
testAdd($a, $b, $result, $scale)
addData
testMul($a, $b, $result, $scale)
mulData
assertEqualNumbers(string $actual, string $expected)
This method is used as a &#39;Comparator&#39; for two numeric strings and is equal to the ScalarComparator be...
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
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
testMod($a, $b, $result)
modData
testCalculation($formula, $result, $scale)
calcData
testPow($a, $b, $result, $scale)
powData