ILIAS  trunk Revision v11.0_alpha-2662-g519ff7d528f
ilMathBaseAdapterTestCase.php
Go to the documentation of this file.
1 <?php
2 
22 
26 abstract class ilMathBaseAdapterTestCase extends TestCase
27 {
28  protected const DEFAULT_SCALE = 50;
29 
31  protected EvalMath $evalMath;
32 
36  protected function setUp(): void
37  {
38  ilMath::setDefaultAdapter($this->mathAdapter);
39  $this->evalMath = new EvalMath();
40  parent::setUp();
41  }
42 
51  private function assertEqualNumbers(string $actual, string $expected): void
52  {
53  $differ = new Differ(new UnifiedDiffOutputBuilder("\n--- Expected\n+++ Actual\n"));
54 
56  $this->assertTrue($actual == $expected, $differ->diff($actual, $expected));
57  }
58 
59  #[\PHPUnit\Framework\Attributes\DataProvider('addData')]
60  public function testAdd(string $a, string $b, string $result, int $scale): void
61  {
62  $this->assertEqualNumbers($result, $this->mathAdapter->add($a, $b, $scale));
63  }
64 
65  #[\PHPUnit\Framework\Attributes\DataProvider('subData')]
66  public function testSub(string $a, string $b, string $result, int $scale): void
67  {
68  $this->assertEqualNumbers($result, $this->mathAdapter->sub($a, $b, $scale));
69  }
70 
71  #[\PHPUnit\Framework\Attributes\DataProvider('mulData')]
72  public function testMul(string $a, string $b, string $result, int $scale): void
73  {
74  $this->assertEqualNumbers($result, $this->mathAdapter->mul($a, $b, $scale));
75  }
76 
77  #[\PHPUnit\Framework\Attributes\DataProvider('divData')]
78  public function testDiv(string $a, string $b, string $result, int $scale): void
79  {
80  $this->assertEqualNumbers($result, $this->mathAdapter->div($a, $b, $scale));
81  }
82 
83  #[\PHPUnit\Framework\Attributes\DataProvider('sqrtData')]
84  public function testSqrt(string $a, string $result, ?int $scale): void
85  {
86  $this->assertEqualNumbers($result, $this->mathAdapter->sqrt($a, $scale));
87  }
88 
89  #[\PHPUnit\Framework\Attributes\DataProvider('powData')]
90  public function testPow(string $a, string $b, string $result, ?int $scale): void
91  {
92  $this->assertEqualNumbers($result, $this->mathAdapter->pow($a, $b, $scale));
93  }
94 
98  #[\PHPUnit\Framework\Attributes\DataProvider('modData')]
99  public function testMod(string $a, string $b, string $result): void
100  {
101  $this->assertEqualNumbers($result, $this->mathAdapter->mod($a, $b));
102  }
103 
104  #[\PHPUnit\Framework\Attributes\DataProvider('equalsData')]
105  public function testEquals(string $a, string $b, bool $result, ?int $scale): void
106  {
107  $this->assertEqualNumbers($result, $this->mathAdapter->equals($a, $b, $scale));
108  }
109 
110  #[\PHPUnit\Framework\Attributes\DataProvider('calcData')]
111  public function testCalculation(string $formula, string $result, int $scale): void
112  {
113  $this->assertEqualNumbers($result, ilMath::_applyScale($this->evalMath->evaluate($formula), $scale));
114  }
115 
119  public function testDivisionsByZero(): void
120  {
121  $this->expectException(ilMathDivisionByZeroException::class);
122 
123  $this->mathAdapter->div(1, 0);
124  }
125 
129  public function testModuloByZero(): void
130  {
131  $this->expectException(ilMathDivisionByZeroException::class);
132 
133  $this->mathAdapter->mod(1, 0);
134  }
135 
139  public static function addData(): array
140  {
141  return [
142  ['1', '2', '3', self::DEFAULT_SCALE]
143  ];
144  }
145 
149  public static function subData(): array
150  {
151  return [
152  ['1', '2', '-1', self::DEFAULT_SCALE]
153  ];
154  }
155 
159  public static function mulData(): array
160  {
161  return [
162  'Multiplication with integer operands' => ['1', '2', '2', self::DEFAULT_SCALE],
163  'Multiplication with empty string operand' => ['1', '', '0', self::DEFAULT_SCALE],
164  'Multiplication with decimal operands' => ['1.5', '2.5', '3.75', self::DEFAULT_SCALE]
165  ];
166  }
167 
171  public static function divData(): array
172  {
173  return [
174  'Division with integer operands' => ['1', '2', '0.5', self::DEFAULT_SCALE],
175  'Division with empty string operand' => ['', '2', '0', self::DEFAULT_SCALE],
176  'Division with decimal operands' => ['3.75', '2.5', '1.5', self::DEFAULT_SCALE],
177  ];
178  }
179 
183  public static function modData(): array
184  {
185  return [
186  ['1', '2', '1']
187  ];
188  }
189 
193  public static function sqrtData(): array
194  {
195  return [
196  ['9', '3', self::DEFAULT_SCALE],
197  ['4294967296', '65536', self::DEFAULT_SCALE],
198  ['12345678901234567890', '3513641828', null],
199  ['12345678901234567890', '3513641828.82', 2]
200  ];
201  }
202 
206  public static function powData(): array
207  {
208  return [
209  ['3', '2', '9', self::DEFAULT_SCALE]
210  ];
211  }
212 
216  public static function equalsData(): array
217  {
218  return [
219  ['3', '3', true, null],
220  ['27.424', '27.424', true, 5]
221  ];
222  }
223 
227  public static function calcData(): array
228  {
229  return [
230  ['3+5', '8', self::DEFAULT_SCALE],
231  ['-3+5', '2', self::DEFAULT_SCALE],
232  ['3*6+5', '23', self::DEFAULT_SCALE],
233  ['10/2', '5', self::DEFAULT_SCALE],
234  ['13/60', '0.2166666666666', 13],
235  ['(-(-8)-sqrt((-8)^2-4*(7)))/(2)', '1', self::DEFAULT_SCALE],
236  ['(-(-8)+sqrt((-8)^2-4*(7)))/(2)', '7', self::DEFAULT_SCALE],
237  ['(-(-41)-sqrt((-41)^2-4*(1)*(5)))/(2*(1))', '0.122', 3],
238  ['(-(-41)+sqrt((-41)^2-4*(1)*(5)))/(2*(1))', '40.877', 3],
239  ['4^2-2*4+0.5*-16', '0', self::DEFAULT_SCALE],
240  ['-2^2-2*-2+0.5*-16', '-8', self::DEFAULT_SCALE]
241  ];
242  }
243 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testPow(string $a, string $b, string $result, ?int $scale)
testMod(string $a, string $b, string $result)
testSub(string $a, string $b, string $result, int $scale)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
testCalculation(string $formula, string $result, int $scale)
testDiv(string $a, string $b, string $result, int $scale)
testSqrt(string $a, string $result, ?int $scale)
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...
testEquals(string $a, string $b, bool $result, ?int $scale)
testAdd(string $a, string $b, string $result, int $scale)
static setDefaultAdapter(ilMathAdapter $adapter)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
testMul(string $a, string $b, string $result, int $scale)