From 26383f1cde19dbb0ce185b3c98ca5bd0b8912183 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Mon, 9 Sep 2019 19:51:35 +0200 Subject: [PATCH] test fixes --- Account/PermissionHandlingTrait.php | 2 +- Algorithm/CoinMatching/MinimumCoinProblem.php | 12 ++--- DataStorage/Database/DataMapperAbstract.php | 48 +++++++++++++++---- Math/Topology/Metrics2D.php | 23 ++++++--- Stdlib/Base/Heap.php | 18 +++---- .../CoinMatching/MinimumCoinProblemTest.php | 9 +--- .../{Metric2DTest.php => Metrics2DTest.php} | 24 +++++----- tests/Stdlib/Base/HeapTest.php | 11 +++-- 8 files changed, 91 insertions(+), 56 deletions(-) rename tests/Math/Topology/{Metric2DTest.php => Metrics2DTest.php} (65%) diff --git a/Account/PermissionHandlingTrait.php b/Account/PermissionHandlingTrait.php index 9803822da..c13fe41c0 100644 --- a/Account/PermissionHandlingTrait.php +++ b/Account/PermissionHandlingTrait.php @@ -95,7 +95,7 @@ trait PermissionHandlingTrait public function addPermission(PermissionAbstract $permission) : void { $this->permissions[] = $permission; - ++$this->permission; + ++$this->pLength; } /** diff --git a/Algorithm/CoinMatching/MinimumCoinProblem.php b/Algorithm/CoinMatching/MinimumCoinProblem.php index 036c01cfe..80679785f 100644 --- a/Algorithm/CoinMatching/MinimumCoinProblem.php +++ b/Algorithm/CoinMatching/MinimumCoinProblem.php @@ -54,23 +54,19 @@ class MinimumCoinProblem for ($i = 1; $i <= $value; ++$i) { for ($j = 0; $j < $m; ++$j) { if ($coins[$j] <= $i) { - if ($coins[$j] === null) { - continue; - } - $subRes = $table[$i - $coins[$j]]; if ($subRes !== \PHP_INT_MAX && $subRes + 1 < $table[$i] ) { - $table[$i] = $subRes + 1; - $usedCoins[$i] = \array_merge($usedCoins[$i - $coins[$j]], [$coins[$j]]); - $coins[$j] = null; + $table[$i] = $subRes + 1; + $usedCoins[$i] = $coins[$j] === null ? ($usedCoins[$i] ?? []) : \array_merge($usedCoins[$i - $coins[$j]] ?? [], [$coins[$j]]); + $coins[$j] = null; } } } } - return $usedCoins[$value]; + return $usedCoins[$value] ?? []; } } diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index 9f75af570..56a15a35a 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -31,7 +31,7 @@ use phpOMS\Utils\ArrayUtils; * @license OMS License 1.0 * @link https://orange-management.org * @since 1.0.0 - * @todo: currently hasmany, owns one etc. are not using joins. In some cases this could improve the performance instead of separately querying the database + * @todo: currently hasmany, owns one etc. are not using joins. In some cases this could improve the performance instead of separately querying the database */ class DataMapperAbstract implements DataMapperInterface { @@ -82,7 +82,7 @@ class DataMapperAbstract implements DataMapperInterface * @since 1.0.0 */ protected static array $columns = []; - + /** * Conditional. * @@ -378,6 +378,21 @@ class DataMapperAbstract implements DataMapperInterface return $objId; } + /** + * Create conditionals + * + * @param \ReflectionClass $refClass Reflection class + * @param object $obj Object to create + * @param mixed $objId Id to set + * + * @return void + * + * @since 1.0.0 + */ + private static function createConditionals(\ReflectionClass $refClass, object $obj, $objId): void + { + } + /** * Create object in db. * @@ -402,7 +417,7 @@ class DataMapperAbstract implements DataMapperInterface if ($relations === RelationType::ALL) { self::createHasManyArray($obj, $objId); - self::createConditionalsArray($refClass, $obj, $objId); + self::createConditionalsArray($obj, $objId); } return $objId; @@ -475,6 +490,20 @@ class DataMapperAbstract implements DataMapperInterface return self::$db->con->lastInsertId(); } + /** + * Create conditionals + * + * @param array $obj Object to create + * @param mixed $objId Id to set + * + * @return void + * + * @since 1.0.0 + */ + private static function createConditionalsArray(array &$obj, $objId): void + { + } + /** * Create base model. * @@ -1329,7 +1358,7 @@ class DataMapperAbstract implements DataMapperInterface private static function updateConditionals(object $obj, $objId, \ReflectionClass $refClass = null, int $relations = RelationType::ALL, int $depth = 1) : void { - foreach (static::$conditionals as $table => $contidional) { + foreach (static::$conditionals as $table => $conditional) { $query = new Builder(self::$db); $query->prefix(self::$db->getPrefix()) ->update($table) @@ -1438,7 +1467,7 @@ class DataMapperAbstract implements DataMapperInterface */ private static function updateConditionalsArray(array $obj, $objId, int $relations = RelationType::ALL, int $depth = 1) : void { - foreach (static::$conditionals as $table => $contidional) { + foreach (static::$conditionals as $table => $conditional) { $query = new Builder(self::$db); $query->prefix(self::$db->getPrefix()) ->update($table) @@ -1497,7 +1526,7 @@ class DataMapperAbstract implements DataMapperInterface if ($relations === RelationType::ALL) { self::updateHasMany($refClass, $obj, $objId, --$depth); - self::updateConditionals($refClass, $obj, $objId); + self::updateConditionals($obj, $objId, $refClass); } if (empty($objId)) { @@ -1544,7 +1573,7 @@ class DataMapperAbstract implements DataMapperInterface if ($relations === RelationType::ALL) { self::updateHasManyArray($obj, $objId, --$depth); - self::updateConditionalsArray($refClass, $obj, $objId); + self::updateConditionalsArray($obj, $objId); } if ($update) { @@ -1787,7 +1816,7 @@ class DataMapperAbstract implements DataMapperInterface return $objId; } - // @todo: implement array delete + // @todo: implement array delete /** * Populate data. @@ -1981,6 +2010,7 @@ class DataMapperAbstract implements DataMapperInterface * @return void * * @todo accept reflection class as parameter + * @todo do this in the getRaw() part as a join. check if has conditionals and then join the data an then everything can be done in the getModel function. * * @since 1.0.0 */ @@ -2705,7 +2735,7 @@ class DataMapperAbstract implements DataMapperInterface return; } - if ($conditionals) { + if ($hasConditionals) { self::populateConditionalsArray(); } diff --git a/Math/Topology/Metrics2D.php b/Math/Topology/Metrics2D.php index 08faa3e9e..edfebede8 100644 --- a/Math/Topology/Metrics2D.php +++ b/Math/Topology/Metrics2D.php @@ -162,8 +162,8 @@ final class Metrics2D */ public static function brayCurtis(array $a, array $b) : float { - return \abs($a['x'] - $b['x']) / ($a['x'] + $b['x']) - + \abs($a['y'] - $b['y']) / ($a['y'] + $b['y']); + return (\abs($a['x'] - $b['x']) + \abs($a['y'] - $b['y'])) / (($a['x'] + $b['x']) + + ($a['y'] + $b['y'])); } /** @@ -180,7 +180,7 @@ final class Metrics2D */ public static function angularSeparation(array $a, array $b) : float { - return ($a['x'] * $b['x'] + $a['y'] * $b['y']) / (($a['x']**2 + $a['y']**2) * ($b['x'] ** 2 + $b['y'] ** 2)); + return ($a['x'] * $b['x'] + $a['y'] * $b['y']) / pow(($a['x']**2 + $a['y']**2) * ($b['x'] ** 2 + $b['y'] ** 2), 1 / 2); } /** @@ -191,13 +191,24 @@ final class Metrics2D * @param array $a 2-D array with x and y coordinate * @param array $b 2-D array with x and y coordinate * - * @return float + * @return int * * @since 1.0.0 */ - public static function hamming(array $a, array $b) : float + public static function hamming(array $a, array $b) : int { - return \abs($a['x'] - $b['x']) + \abs($a['y'] - $b['y']); + if (($size = \count($a)) !== \count($b)) { + throw new \Exception(); + } + + $dist = 0; + for ($i = 0; $i < $size; ++$i) { + if ($a[$i] !== $b[$i]) { + ++$dist; + } + } + + return $dist; } /** diff --git a/Stdlib/Base/Heap.php b/Stdlib/Base/Heap.php index caea6e84c..ac02d2e07 100644 --- a/Stdlib/Base/Heap.php +++ b/Stdlib/Base/Heap.php @@ -24,7 +24,7 @@ namespace phpOMS\Stdlib\Base; */ class Heap { - private \Closure $compare = null; + private \Closure $compare; private array $nodes = []; @@ -39,7 +39,7 @@ class Heap while ($lo < $hi) { $mid = (int) \floor(($lo + $hi) / 2); - if ($this->compare($x, $this->node[$mid]) < 0) { + if (($this->compare)($x, $this->node[$mid]) < 0) { $hi = $mid; } else { $lo = $mid + 1; @@ -102,7 +102,7 @@ class Heap public function pushpop($item) { - if (!empty($this->nodes) && $this->compare($this->nodes[0], $item) < 0) { + if (!empty($this->nodes) && ($this->compare)($this->nodes[0], $item) < 0) { $temp = $item; $item = $this->nodes[0]; $this->nodes[0] = $temp; @@ -149,17 +149,17 @@ class Heap public function getNLargest(int $n) : array { $nodes = $this->nodes; - \uasort($nodes, [$this, 'compare']); + \uasort($nodes, $this->compare); - return \array_slice($nodes, 0, $n); + return \array_slice(\array_reverse($nodes), 0, $n); } public function getNSmallest(int $n): array { $nodes = $this->nodes; - \uasort($nodes, [$this, 'compare']); + \uasort($nodes, $this->compare); - return \array_slice(\array_reverse($nodes), 0, $n); + return \array_slice($nodes, 0, $n); } private function siftDown(int $start, int $pos) : void @@ -169,7 +169,7 @@ class Heap $pPos = ($pos - 1) >> 1; $parent = $this->nodes[$pPos]; - if ($this->compare($item, $parent) < 0) { + if (($this->compare)($item, $parent) < 0) { $this->nodes[$pos] = $parent; $pos = $pPos; @@ -192,7 +192,7 @@ class Heap while ($cPos < $ePos) { $rPos = $cPos + 1; - if ($rPos < $ePos && $this->compare($this->nodes[$cPos], $this->nodes[$rPos]) > -1) { + if ($rPos < $ePos && ($this->compare)($this->nodes[$cPos], $this->nodes[$rPos]) > -1) { $cPos = $rPos; } diff --git a/tests/Algorithm/CoinMatching/MinimumCoinProblemTest.php b/tests/Algorithm/CoinMatching/MinimumCoinProblemTest.php index 0f4eb13d4..f0db2d492 100644 --- a/tests/Algorithm/CoinMatching/MinimumCoinProblemTest.php +++ b/tests/Algorithm/CoinMatching/MinimumCoinProblemTest.php @@ -28,13 +28,8 @@ class MinimumCoinProblemTest extends \PHPUnit\Framework\TestCase public function testMinimumCoins() : void { self::assertEquals( - [9, 6, 5, 1], - MinimumCoinProblem::getMinimumCoinsForValueI([6, 6, 5], 17) - ); - - self::assertEquals( - [9, 6, 5, 6, 1], - MinimumCoinProblem::getMinimumCoinsForValueI([6, 6, 5], 17) + [6, 6, 5], + MinimumCoinProblem::getMinimumCoinsForValueI([9, 6, 5, 6, 1], 17) ); } } diff --git a/tests/Math/Topology/Metric2DTest.php b/tests/Math/Topology/Metrics2DTest.php similarity index 65% rename from tests/Math/Topology/Metric2DTest.php rename to tests/Math/Topology/Metrics2DTest.php index 3171de79b..655a82cbe 100644 --- a/tests/Math/Topology/Metric2DTest.php +++ b/tests/Math/Topology/Metrics2DTest.php @@ -14,18 +14,18 @@ namespace phpOMS\tests\Math\Number; -use phpOMS\Math\Topology\Metric2D; +use phpOMS\Math\Topology\Metrics2D; /** * @internal */ -class Metric2DTest extends \PHPUnit\Framework\TestCase +class Metrics2DTest extends \PHPUnit\Framework\TestCase { public function testManhattan() : void { self::assertEquals( 10.0, - Metric2D::manhattan(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]) + Metrics2D::manhattan(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]) ); } @@ -33,7 +33,7 @@ class Metric2DTest extends \PHPUnit\Framework\TestCase { self::assertEqualsWithDelta( 7.615773, - Metric2D::euclidean(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]), + Metrics2D::euclidean(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]), 0.1 ); } @@ -42,7 +42,7 @@ class Metric2DTest extends \PHPUnit\Framework\TestCase { self::assertEquals( 7.0, - Metric2D::chebyshev(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]) + Metrics2D::chebyshev(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]) ); } @@ -50,7 +50,7 @@ class Metric2DTest extends \PHPUnit\Framework\TestCase { self::assertEqualsWithDelta( 7.179, - Metric2D::minkowski(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6], 3), + Metrics2D::minkowski(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6], 3), 0.1 ); } @@ -59,7 +59,7 @@ class Metric2DTest extends \PHPUnit\Framework\TestCase { self::assertEqualsWithDelta( 1.333, - Metric2D::canberra(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]), + Metrics2D::canberra(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]), 0.1 ); } @@ -68,7 +68,7 @@ class Metric2DTest extends \PHPUnit\Framework\TestCase { self::assertEqualsWithDelta( 0.625, - Metric2D::brayCurtis(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]), + Metrics2D::brayCurtis(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]), 0.1 ); } @@ -77,7 +77,7 @@ class Metric2DTest extends \PHPUnit\Framework\TestCase { self::assertEqualsWithDelta( 0.6508, - Metric2D::angularSeparation(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]), + Metrics2D::angularSeparation(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]), 0.1 ); } @@ -85,8 +85,8 @@ class Metric2DTest extends \PHPUnit\Framework\TestCase public function testHammingDistance() : void { self::assertEquals( - 3.0, - Metric2D::hammingDistance([1, 1, 1, 1], [0, 1, 0, 0]), + 3, + Metrics2D::hamming([1, 1, 1, 1], [0, 1, 0, 0]), ); } @@ -94,7 +94,7 @@ class Metric2DTest extends \PHPUnit\Framework\TestCase { self::assertEquals( 2, - Metric2D::hammingDistance([3, 6, 4, 8], [4, 6, 8, 3]) + Metrics2D::ulam([3, 6, 4, 8], [4, 6, 8, 3]) ); } } diff --git a/tests/Stdlib/Base/HeapTest.php b/tests/Stdlib/Base/HeapTest.php index 8862a6036..a6672e2f0 100644 --- a/tests/Stdlib/Base/HeapTest.php +++ b/tests/Stdlib/Base/HeapTest.php @@ -76,7 +76,10 @@ class HeapTest extends \PHPUnit\Framework\TestCase } self::assertEquals(1, $heap->pushpop(6)); - self::assertEquals([2, 3, 4, 5, 6], $heap->toArray()); + + $heapArray = $heap->toArray(); + \sort($heapArray); + self::assertEquals([2, 3, 4, 5, 6], $heapArray); } public function testContains(): void @@ -100,13 +103,13 @@ class HeapTest extends \PHPUnit\Framework\TestCase $heap = new Heap(); $heap->push(1); - self::assertEqals(1, $heap->peek()); + self::assertEquals(1, $heap->peek()); $heap->push(2); - self::assertEqals(1, $heap->peek()); + self::assertEquals(1, $heap->peek()); $heap->pop(); - self::assertEqals(2, $heap->peek()); + self::assertEquals(2, $heap->peek()); } public function testNSmallest() : void