Coding standards.

This commit is contained in:
Jens True 2024-01-16 15:07:14 +00:00
parent 09ee4534e5
commit 46036cf82f
4 changed files with 131 additions and 0 deletions

@ -10,6 +10,18 @@ class GaussianDistributionTest extends TestCase
{ {
private const ERROR_TOLERANCE = 0.000001; 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 public function testCumulativeTo(): void
{ {
// Verified with WolframAlpha // Verified with WolframAlpha

@ -6,6 +6,7 @@ use DNW\Skills\Numerics\IdentityMatrix;
use DNW\Skills\Numerics\Matrix; use DNW\Skills\Numerics\Matrix;
use DNW\Skills\Numerics\SquareMatrix; use DNW\Skills\Numerics\SquareMatrix;
use DNW\Skills\Tests\TestCase; use DNW\Skills\Tests\TestCase;
use Exception;
// phpcs:disable PSR2.Methods.FunctionCallSignature,Generic.Functions.FunctionCallArgumentSpacing.TooMuchSpaceAfterComma // phpcs:disable PSR2.Methods.FunctionCallSignature,Generic.Functions.FunctionCallArgumentSpacing.TooMuchSpaceAfterComma
class MatrixTest extends TestCase class MatrixTest extends TestCase
@ -129,6 +130,16 @@ class MatrixTest extends TestCase
3, 4); 3, 4);
$this->assertTrue($g->equals($h)); $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 public function testAdjugate(): void
@ -185,5 +196,35 @@ class MatrixTest extends TestCase
$ccInverse = Matrix::multiply($c, $cInverse); $ccInverse = Matrix::multiply($c, $cInverse);
$this->assertTrue($identity3x3->equals($ccInverse)); $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 // phpcs:enable

@ -0,0 +1,29 @@
<?php
namespace DNW\Skills\Tests\Numerics;
use DNW\Skills\Numerics\Range;
use DNW\Skills\Tests\TestCase;
use Exception;
class RangeTest extends TestCase
{
public function testConstructInvalidParam(): void
{
$this->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());
}
}

@ -0,0 +1,49 @@
<?php
namespace DNW\Skills\Tests\TrueSkill;
use DNW\Skills\Tests\TestCase;
use DNW\Skills\GameInfo;
use DNW\Skills\Player;
use DNW\Skills\Team;
use DNW\Skills\TrueSkill\FactorGraphTrueSkillCalculator;
class FactorGraphTrueSkillCalculatorTest extends TestCase
{
public function testMicrosoftResearchExample(): void
{
$gameInfo = new GameInfo();
$players[] = new Player("alice");
$players[] = new Player("bob");
$players[] = new Player("chris");
$players[] = new Player("darren");
$players[] = new Player("eve");
$players[] = new Player("fabien");
$players[] = new Player("george");
$players[] = new Player("hillary");
foreach ($players as $player) {
$teams[] = new Team($player, $gameInfo->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);
}
}
}