ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
ilMathBaseAdapterTestCase.php
Go to the documentation of this file.
1<?php
2
19use PHPUnit\Framework\TestCase;
20
24abstract class ilMathBaseAdapterTestCase extends TestCase
25{
26 protected const DEFAULT_SCALE = 50;
27
30
34 protected function setUp(): void
35 {
36 ilMath::setDefaultAdapter($this->mathAdapter);
37 $this->evalMath = new EvalMath();
39 }
40
49 private function assertEqualNumbers(string $expected, string $actual): void
50 {
52 $this->assertTrue(
53 $actual == $expected,
54 sprintf("Failed asserting that '%s' is numerically equal to '%s'.", $actual, $expected)
55 );
56 }
57
58 #[\PHPUnit\Framework\Attributes\DataProvider('addData')]
59 public function testAdd(string $a, string $b, string $result, int $scale): void
60 {
61 $this->assertEqualNumbers($result, $this->mathAdapter->add($a, $b, $scale));
62 }
63
64 #[\PHPUnit\Framework\Attributes\DataProvider('subData')]
65 public function testSub(string $a, string $b, string $result, int $scale): void
66 {
67 $this->assertEqualNumbers($result, $this->mathAdapter->sub($a, $b, $scale));
68 }
69
70 #[\PHPUnit\Framework\Attributes\DataProvider('mulData')]
71 public function testMul(string $a, string $b, string $result, int $scale): void
72 {
73 $this->assertEqualNumbers($result, $this->mathAdapter->mul($a, $b, $scale));
74 }
75
76 #[\PHPUnit\Framework\Attributes\DataProvider('divData')]
77 public function testDiv(string $a, string $b, string $result, int $scale): void
78 {
79 $this->assertEqualNumbers($result, $this->mathAdapter->div($a, $b, $scale));
80 }
81
82 #[\PHPUnit\Framework\Attributes\DataProvider('sqrtData')]
83 public function testSqrt(string $a, string $result, ?int $scale): void
84 {
85 $this->assertEqualNumbers($result, $this->mathAdapter->sqrt($a, $scale));
86 }
87
88 #[\PHPUnit\Framework\Attributes\DataProvider('powData')]
89 public function testPow(string $a, string $b, string $result, ?int $scale): void
90 {
91 $this->assertEqualNumbers($result, $this->mathAdapter->pow($a, $b, $scale));
92 }
93
97 #[\PHPUnit\Framework\Attributes\DataProvider('modData')]
98 public function testMod(string $a, string $b, string $result): void
99 {
100 $this->assertEqualNumbers($result, $this->mathAdapter->mod($a, $b));
101 }
102
103 #[\PHPUnit\Framework\Attributes\DataProvider('equalsData')]
104 public function testEquals(string $a, string $b, bool $result, ?int $scale): void
105 {
106 $this->assertEqualNumbers($result, $this->mathAdapter->equals($a, $b, $scale));
107 }
108
109 #[\PHPUnit\Framework\Attributes\DataProvider('calcData')]
110 public function testCalculation(string $formula, string $result, int $scale): void
111 {
112 $this->assertEqualNumbers($result, ilMath::_applyScale($this->evalMath->evaluate($formula), $scale));
113 }
114
118 public function testDivisionsByZero(): void
119 {
120 $this->expectException(ilMathDivisionByZeroException::class);
121
122 $this->mathAdapter->div(1, 0);
123 }
124
128 public function testModuloByZero(): void
129 {
130 $this->expectException(ilMathDivisionByZeroException::class);
131
132 $this->mathAdapter->mod(1, 0);
133 }
134
138 public static function addData(): array
139 {
140 return [
141 ['1', '2', '3', self::DEFAULT_SCALE]
142 ];
143 }
144
148 public static function subData(): array
149 {
150 return [
151 ['1', '2', '-1', self::DEFAULT_SCALE]
152 ];
153 }
154
158 public static function mulData(): array
159 {
160 return [
161 'Multiplication with integer operands' => ['1', '2', '2', self::DEFAULT_SCALE],
162 'Multiplication with empty string operand' => ['1', '', '0', self::DEFAULT_SCALE],
163 'Multiplication with decimal operands' => ['1.5', '2.5', '3.75', self::DEFAULT_SCALE]
164 ];
165 }
166
170 public static function divData(): array
171 {
172 return [
173 'Division with integer operands' => ['1', '2', '0.5', self::DEFAULT_SCALE],
174 'Division with empty string operand' => ['', '2', '0', self::DEFAULT_SCALE],
175 'Division with decimal operands' => ['3.75', '2.5', '1.5', self::DEFAULT_SCALE],
176 ];
177 }
178
182 public static function modData(): array
183 {
184 return [
185 ['1', '2', '1']
186 ];
187 }
188
192 public static function sqrtData(): array
193 {
194 return [
195 ['9', '3', self::DEFAULT_SCALE],
196 ['4294967296', '65536', self::DEFAULT_SCALE],
197 ['12345678901234567890', '3513641828', null],
198 ['12345678901234567890', '3513641828.82', 2]
199 ];
200 }
201
205 public static function powData(): array
206 {
207 return [
208 ['3', '2', '9', self::DEFAULT_SCALE]
209 ];
210 }
211
215 public static function equalsData(): array
216 {
217 return [
218 ['3', '3', true, null],
219 ['27.424', '27.424', true, 5]
220 ];
221 }
222
226 public static function calcData(): array
227 {
228 return [
229 ['3+5', '8', self::DEFAULT_SCALE],
230 ['-3+5', '2', self::DEFAULT_SCALE],
231 ['3*6+5', '23', self::DEFAULT_SCALE],
232 ['10/2', '5', self::DEFAULT_SCALE],
233 ['13/60', '0.2166666666666', 13],
234 ['(-(-8)-sqrt((-8)^2-4*(7)))/(2)', '1', self::DEFAULT_SCALE],
235 ['(-(-8)+sqrt((-8)^2-4*(7)))/(2)', '7', self::DEFAULT_SCALE],
236 ['(-(-41)-sqrt((-41)^2-4*(1)*(5)))/(2*(1))', '0.122', 3],
237 ['(-(-41)+sqrt((-41)^2-4*(1)*(5)))/(2*(1))', '40.877', 3],
238 ['4^2-2*4+0.5*-16', '0', self::DEFAULT_SCALE],
239 ['-2^2-2*-2+0.5*-16', '-8', self::DEFAULT_SCALE]
240 ];
241 }
242}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testSub(string $a, string $b, string $result, int $scale)
testDiv(string $a, string $b, string $result, int $scale)
testSqrt(string $a, string $result, ?int $scale)
testAdd(string $a, string $b, string $result, int $scale)
testEquals(string $a, string $b, bool $result, ?int $scale)
testMod(string $a, string $b, string $result)
assertEqualNumbers(string $expected, string $actual)
This method is used as a 'Comparator' for two numeric strings and is equal to the ScalarComparator be...
testMul(string $a, string $b, string $result, int $scale)
testPow(string $a, string $b, string $result, ?int $scale)
testCalculation(string $formula, string $result, int $scale)
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