From 46036cf82f0965bba339571fe7d9e17dc2a87f23 Mon Sep 17 00:00:00 2001 From: Jens True Date: Tue, 16 Jan 2024 15:07:14 +0000 Subject: [PATCH] Coding standards. --- tests/Numerics/GaussianDistributionTest.php | 12 +++++ tests/Numerics/MatrixTest.php | 41 ++++++++++++++++ tests/Numerics/RangeTest.php | 29 +++++++++++ .../FactorGraphTrueSkillCalculatorTest.php | 49 +++++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 tests/Numerics/RangeTest.php create mode 100644 tests/TrueSkill/FactorGraphTrueSkillCalculatorTest.php diff --git a/tests/Numerics/GaussianDistributionTest.php b/tests/Numerics/GaussianDistributionTest.php index cdc24a8..65ec0c1 100644 --- a/tests/Numerics/GaussianDistributionTest.php +++ b/tests/Numerics/GaussianDistributionTest.php @@ -10,6 +10,18 @@ class GaussianDistributionTest extends TestCase { private const ERROR_TOLERANCE = 0.000001; + public function testGetters(): void + { + $gd = new GaussianDistribution(10, 3); + + $this->assertEquals(10, $gd->getMean()); + $this->assertEquals(9, $gd->getVariance()); + $this->assertEquals(3, $gd->getStandardDeviation()); + $this->assertEquals(1 / 9, $gd->getPrecision()); + $this->assertEquals(1 / 9 * 10, $gd->getPrecisionMean()); + $this->assertEqualsWithDelta(0.13298076013, $gd->getNormalizationConstant(), GaussianDistributionTest::ERROR_TOLERANCE); + } + public function testCumulativeTo(): void { // Verified with WolframAlpha diff --git a/tests/Numerics/MatrixTest.php b/tests/Numerics/MatrixTest.php index 74d8054..030715a 100644 --- a/tests/Numerics/MatrixTest.php +++ b/tests/Numerics/MatrixTest.php @@ -6,6 +6,7 @@ use DNW\Skills\Numerics\IdentityMatrix; use DNW\Skills\Numerics\Matrix; use DNW\Skills\Numerics\SquareMatrix; use DNW\Skills\Tests\TestCase; +use Exception; // phpcs:disable PSR2.Methods.FunctionCallSignature,Generic.Functions.FunctionCallArgumentSpacing.TooMuchSpaceAfterComma class MatrixTest extends TestCase @@ -129,6 +130,16 @@ class MatrixTest extends TestCase 3, 4); $this->assertTrue($g->equals($h)); + + $i = new Matrix(1, 2, [[1,2]]); + $j = new Matrix(2, 1, [[1],[2]]); + + $this->assertFalse($i->equals($j)); + + $k = new Matrix(2, 2, [[1,2],[3,4]]); + $l = new Matrix(2, 2, [[4,3],[2,1]]); + + $this->assertFalse($k->equals($l)); } public function testAdjugate(): void @@ -185,5 +196,35 @@ class MatrixTest extends TestCase $ccInverse = Matrix::multiply($c, $cInverse); $this->assertTrue($identity3x3->equals($ccInverse)); } + + public function testErrorDeterminant(): void + { + $this->expectException(Exception::class); + $matrix = new Matrix(2, 3, [[1,2,3],[1,2,3]]); + $matrix->getDeterminant(); + } + + public function testErrorAdjugate(): void + { + $this->expectException(Exception::class); + $matrix = new Matrix(2, 3, [[1,2,3],[1,2,3]]); + $matrix->getAdjugate(); + } + + public function testErrorAdd(): void + { + $this->expectException(Exception::class); + $m1 = new Matrix(2, 3, [[1,2,3],[1,2,3]]); + $m2 = new Matrix(1, 1, [[1,1]]); + Matrix::add($m1, $m2); + } + + public function testErrorMultiply(): void + { + $this->expectException(Exception::class); + $m1 = new Matrix(2, 3, [[1,2,3],[1,2,3]]); + $m2 = new Matrix(1, 1, [[1,1]]); + Matrix::multiply($m1, $m2); + } } // phpcs:enable diff --git a/tests/Numerics/RangeTest.php b/tests/Numerics/RangeTest.php new file mode 100644 index 0000000..ef40f66 --- /dev/null +++ b/tests/Numerics/RangeTest.php @@ -0,0 +1,29 @@ +expectException(Exception::class); + $range = new Range(10, 5); + } + + public function testFactoryInclusiveInvalidParam(): void + { + $this->expectException(Exception::class); + $range = Range::inclusive(10, 5); + } + + public function testNormalUse(): void + { + $range = Range::inclusive(1, 10); + $this->assertEquals(1, $range->getMin()); + $this->assertEquals(10, $range->getMax()); + } +} diff --git a/tests/TrueSkill/FactorGraphTrueSkillCalculatorTest.php b/tests/TrueSkill/FactorGraphTrueSkillCalculatorTest.php new file mode 100644 index 0000000..606d6cb --- /dev/null +++ b/tests/TrueSkill/FactorGraphTrueSkillCalculatorTest.php @@ -0,0 +1,49 @@ +getDefaultRating()); + } + + $calculator = new FactorGraphTrueSkillCalculator(); + + $newRatings = $calculator->calculateNewRatings($gameInfo, $teams, [1,2,3,4,5,6,7,8]); + + $expected['alice'] = [36.771, 5.749]; + $expected['bob'] = [32.242, 5.133]; + $expected['chris'] = [29.074, 4.943]; + $expected['darren'] = [26.322, 4.874]; + $expected['eve'] = [23.678, 4.874]; + $expected['fabien'] = [20.926, 4.943]; + $expected['george'] = [17.758, 5.133]; + $expected['hillary'] = [13.229, 5.749]; + + foreach ($players as $player) { + $rating = $newRatings->getRating($player); + $this->assertEqualsWithDelta($expected[$player->getId()][0], $rating->getMean(), 0.001); + $this->assertEqualsWithDelta($expected[$player->getId()][1], $rating->getStandardDeviation(), 0.001); + } + } +}