ILIAS  release_8 Revision v8.25-1-g13de6a5eca6
ilMathBaseAdapterTest.php
Go to the documentation of this file.
1<?php
2
19use PHPUnit\Framework\TestCase;
20use SebastianBergmann\Diff\Differ;
21use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
22
26abstract class ilMathBaseAdapterTest extends TestCase
27{
28 protected const DEFAULT_SCALE = 50;
29
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
62 public function testAdd(string $a, string $b, string $result, int $scale): void
63 {
64 $this->assertEqualNumbers($result, $this->mathAdapter->add($a, $b, $scale));
65 }
66
70 public function testSub(string $a, string $b, string $result, int $scale): void
71 {
72 $this->assertEqualNumbers($result, $this->mathAdapter->sub($a, $b, $scale));
73 }
74
78 public function testMul(string $a, string $b, string $result, int $scale): void
79 {
80 $this->assertEqualNumbers($result, $this->mathAdapter->mul($a, $b, $scale));
81 }
82
86 public function testDiv(string $a, string $b, string $result, int $scale): void
87 {
88 $this->assertEqualNumbers($result, $this->mathAdapter->div($a, $b, $scale));
89 }
90
94 public function testSqrt(string $a, string $result, ?int $scale): void
95 {
96 $this->assertEqualNumbers($result, $this->mathAdapter->sqrt($a, $scale));
97 }
98
102 public function testPow(string $a, string $b, string $result, ?int $scale): void
103 {
104 $this->assertEqualNumbers($result, $this->mathAdapter->pow($a, $b, $scale));
105 }
106
111 public function testMod(string $a, string $b, string $result): void
112 {
113 $this->assertEqualNumbers($result, $this->mathAdapter->mod($a, $b));
114 }
115
119 public function testEquals(string $a, string $b, bool $result, ?int $scale): void
120 {
121 $this->assertEqualNumbers($result, $this->mathAdapter->equals($a, $b, $scale));
122 }
123
127 public function testCalculation(string $formula, string $result, int $scale): void
128 {
129 $this->assertEqualNumbers($result, ilMath::_applyScale($this->evalMath->evaluate($formula), $scale));
130 }
131
135 public function testDivisionsByZero(): void
136 {
137 $this->expectException(ilMathDivisionByZeroException::class);
138
139 $this->mathAdapter->div(1, 0);
140 }
141
145 public function testModuloByZero(): void
146 {
147 $this->expectException(ilMathDivisionByZeroException::class);
148
149 $this->mathAdapter->mod(1, 0);
150 }
151
155 public function addData(): array
156 {
157 return [
158 ['1', '2', '3', self::DEFAULT_SCALE]
159 ];
160 }
161
165 public function subData(): array
166 {
167 return [
168 ['1', '2', '-1', self::DEFAULT_SCALE]
169 ];
170 }
171
175 public function mulData(): array
176 {
177 return [
178 'Multiplication with integer operands' => ['1', '2', '2', self::DEFAULT_SCALE],
179 'Multiplication with empty string operand' => ['1', '', '0', self::DEFAULT_SCALE],
180 'Multiplication with decimal operands' => ['1.5', '2.5', '3.75', self::DEFAULT_SCALE]
181 ];
182 }
183
187 public function divData(): array
188 {
189 return [
190 'Division with integer operands' => ['1', '2', '0.5', self::DEFAULT_SCALE],
191 'Division with empty string operand' => ['', '2', '0', self::DEFAULT_SCALE],
192 'Division with decimal operands' => ['3.75', '2.5', '1.5', self::DEFAULT_SCALE],
193 ];
194 }
195
199 public function modData(): array
200 {
201 return [
202 ['1', '2', '1']
203 ];
204 }
205
209 public function sqrtData(): array
210 {
211 return [
212 ['9', '3', self::DEFAULT_SCALE],
213 ['4294967296', '65536', self::DEFAULT_SCALE],
214 ['12345678901234567890', '3513641828', null],
215 ['12345678901234567890', '3513641828.82', 2]
216 ];
217 }
218
222 public function powData(): array
223 {
224 return [
225 ['3', '2', '9', self::DEFAULT_SCALE]
226 ];
227 }
228
232 public function equalsData(): array
233 {
234 return [
235 ['3', '3', true, null],
236 ['27.424', '27.424', true, 5]
237 ];
238 }
239
243 public function calcData(): array
244 {
245 return [
246 ['3+5', '8', self::DEFAULT_SCALE],
247 ['-3+5', '2', self::DEFAULT_SCALE],
248 ['3*6+5', '23', self::DEFAULT_SCALE],
249 ['10/2', '5', self::DEFAULT_SCALE],
250 ['13/60', '0.2166666666666', 13],
251 ['(-(-8)-sqrt((-8)^2-4*(7)))/(2)', '1', self::DEFAULT_SCALE],
252 ['(-(-8)+sqrt((-8)^2-4*(7)))/(2)', '7', self::DEFAULT_SCALE],
253 ['(-(-41)-sqrt((-41)^2-4*(1)*(5)))/(2*(1))', '0.122', 3],
254 ['(-(-41)+sqrt((-41)^2-4*(1)*(5)))/(2*(1))', '40.877', 3],
255 ['4^2-2*4+0.5*-16', '0', self::DEFAULT_SCALE],
256 ['-2^2-2*-2+0.5*-16', '-8', self::DEFAULT_SCALE]
257 ];
258 }
259}
assertEqualNumbers(string $actual, string $expected)
This method is used as a 'Comparator' for two numeric strings and is equal to the ScalarComparator be...
testCalculation(string $formula, string $result, int $scale)
@dataProvider calcData
testMul(string $a, string $b, string $result, int $scale)
@dataProvider mulData
testAdd(string $a, string $b, string $result, int $scale)
@dataProvider addData
testPow(string $a, string $b, string $result, ?int $scale)
@dataProvider powData
testDiv(string $a, string $b, string $result, int $scale)
@dataProvider divData
testMod(string $a, string $b, string $result)
@dataProvider modData
testSub(string $a, string $b, string $result, int $scale)
@dataProvider subData
testEquals(string $a, string $b, bool $result, ?int $scale)
@dataProvider equalsData
testSqrt(string $a, string $result, ?int $scale)
@dataProvider sqrtData
static setDefaultAdapter(ilMathAdapter $adapter)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples