test fixes

This commit is contained in:
Dennis Eichhorn 2019-09-09 19:51:35 +02:00
parent c41a59ad1d
commit 26383f1cde
8 changed files with 91 additions and 56 deletions

View File

@ -95,7 +95,7 @@ trait PermissionHandlingTrait
public function addPermission(PermissionAbstract $permission) : void public function addPermission(PermissionAbstract $permission) : void
{ {
$this->permissions[] = $permission; $this->permissions[] = $permission;
++$this->permission; ++$this->pLength;
} }
/** /**

View File

@ -54,23 +54,19 @@ class MinimumCoinProblem
for ($i = 1; $i <= $value; ++$i) { for ($i = 1; $i <= $value; ++$i) {
for ($j = 0; $j < $m; ++$j) { for ($j = 0; $j < $m; ++$j) {
if ($coins[$j] <= $i) { if ($coins[$j] <= $i) {
if ($coins[$j] === null) {
continue;
}
$subRes = $table[$i - $coins[$j]]; $subRes = $table[$i - $coins[$j]];
if ($subRes !== \PHP_INT_MAX if ($subRes !== \PHP_INT_MAX
&& $subRes + 1 < $table[$i] && $subRes + 1 < $table[$i]
) { ) {
$table[$i] = $subRes + 1; $table[$i] = $subRes + 1;
$usedCoins[$i] = \array_merge($usedCoins[$i - $coins[$j]], [$coins[$j]]); $usedCoins[$i] = $coins[$j] === null ? ($usedCoins[$i] ?? []) : \array_merge($usedCoins[$i - $coins[$j]] ?? [], [$coins[$j]]);
$coins[$j] = null; $coins[$j] = null;
} }
} }
} }
} }
return $usedCoins[$value]; return $usedCoins[$value] ?? [];
} }
} }

View File

@ -378,6 +378,21 @@ class DataMapperAbstract implements DataMapperInterface
return $objId; 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. * Create object in db.
* *
@ -402,7 +417,7 @@ class DataMapperAbstract implements DataMapperInterface
if ($relations === RelationType::ALL) { if ($relations === RelationType::ALL) {
self::createHasManyArray($obj, $objId); self::createHasManyArray($obj, $objId);
self::createConditionalsArray($refClass, $obj, $objId); self::createConditionalsArray($obj, $objId);
} }
return $objId; return $objId;
@ -475,6 +490,20 @@ class DataMapperAbstract implements DataMapperInterface
return self::$db->con->lastInsertId(); 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. * 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 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 = new Builder(self::$db);
$query->prefix(self::$db->getPrefix()) $query->prefix(self::$db->getPrefix())
->update($table) ->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 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 = new Builder(self::$db);
$query->prefix(self::$db->getPrefix()) $query->prefix(self::$db->getPrefix())
->update($table) ->update($table)
@ -1497,7 +1526,7 @@ class DataMapperAbstract implements DataMapperInterface
if ($relations === RelationType::ALL) { if ($relations === RelationType::ALL) {
self::updateHasMany($refClass, $obj, $objId, --$depth); self::updateHasMany($refClass, $obj, $objId, --$depth);
self::updateConditionals($refClass, $obj, $objId); self::updateConditionals($obj, $objId, $refClass);
} }
if (empty($objId)) { if (empty($objId)) {
@ -1544,7 +1573,7 @@ class DataMapperAbstract implements DataMapperInterface
if ($relations === RelationType::ALL) { if ($relations === RelationType::ALL) {
self::updateHasManyArray($obj, $objId, --$depth); self::updateHasManyArray($obj, $objId, --$depth);
self::updateConditionalsArray($refClass, $obj, $objId); self::updateConditionalsArray($obj, $objId);
} }
if ($update) { if ($update) {
@ -1981,6 +2010,7 @@ class DataMapperAbstract implements DataMapperInterface
* @return void * @return void
* *
* @todo accept reflection class as parameter * @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 * @since 1.0.0
*/ */
@ -2705,7 +2735,7 @@ class DataMapperAbstract implements DataMapperInterface
return; return;
} }
if ($conditionals) { if ($hasConditionals) {
self::populateConditionalsArray(); self::populateConditionalsArray();
} }

View File

@ -162,8 +162,8 @@ final class Metrics2D
*/ */
public static function brayCurtis(array $a, array $b) : float public static function brayCurtis(array $a, array $b) : float
{ {
return \abs($a['x'] - $b['x']) / ($a['x'] + $b['x']) return (\abs($a['x'] - $b['x']) + \abs($a['y'] - $b['y'])) / (($a['x'] + $b['x'])
+ \abs($a['y'] - $b['y']) / ($a['y'] + $b['y']); + ($a['y'] + $b['y']));
} }
/** /**
@ -180,7 +180,7 @@ final class Metrics2D
*/ */
public static function angularSeparation(array $a, array $b) : float 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 $a 2-D array with x and y coordinate
* @param array $b 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 * @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;
} }
/** /**

View File

@ -24,7 +24,7 @@ namespace phpOMS\Stdlib\Base;
*/ */
class Heap class Heap
{ {
private \Closure $compare = null; private \Closure $compare;
private array $nodes = []; private array $nodes = [];
@ -39,7 +39,7 @@ class Heap
while ($lo < $hi) { while ($lo < $hi) {
$mid = (int) \floor(($lo + $hi) / 2); $mid = (int) \floor(($lo + $hi) / 2);
if ($this->compare($x, $this->node[$mid]) < 0) { if (($this->compare)($x, $this->node[$mid]) < 0) {
$hi = $mid; $hi = $mid;
} else { } else {
$lo = $mid + 1; $lo = $mid + 1;
@ -102,7 +102,7 @@ class Heap
public function pushpop($item) 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; $temp = $item;
$item = $this->nodes[0]; $item = $this->nodes[0];
$this->nodes[0] = $temp; $this->nodes[0] = $temp;
@ -149,17 +149,17 @@ class Heap
public function getNLargest(int $n) : array public function getNLargest(int $n) : array
{ {
$nodes = $this->nodes; $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 public function getNSmallest(int $n): array
{ {
$nodes = $this->nodes; $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 private function siftDown(int $start, int $pos) : void
@ -169,7 +169,7 @@ class Heap
$pPos = ($pos - 1) >> 1; $pPos = ($pos - 1) >> 1;
$parent = $this->nodes[$pPos]; $parent = $this->nodes[$pPos];
if ($this->compare($item, $parent) < 0) { if (($this->compare)($item, $parent) < 0) {
$this->nodes[$pos] = $parent; $this->nodes[$pos] = $parent;
$pos = $pPos; $pos = $pPos;
@ -192,7 +192,7 @@ class Heap
while ($cPos < $ePos) { while ($cPos < $ePos) {
$rPos = $cPos + 1; $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; $cPos = $rPos;
} }

View File

@ -28,13 +28,8 @@ class MinimumCoinProblemTest extends \PHPUnit\Framework\TestCase
public function testMinimumCoins() : void public function testMinimumCoins() : void
{ {
self::assertEquals( self::assertEquals(
[9, 6, 5, 1], [6, 6, 5],
MinimumCoinProblem::getMinimumCoinsForValueI([6, 6, 5], 17) MinimumCoinProblem::getMinimumCoinsForValueI([9, 6, 5, 6, 1], 17)
);
self::assertEquals(
[9, 6, 5, 6, 1],
MinimumCoinProblem::getMinimumCoinsForValueI([6, 6, 5], 17)
); );
} }
} }

View File

@ -14,18 +14,18 @@
namespace phpOMS\tests\Math\Number; namespace phpOMS\tests\Math\Number;
use phpOMS\Math\Topology\Metric2D; use phpOMS\Math\Topology\Metrics2D;
/** /**
* @internal * @internal
*/ */
class Metric2DTest extends \PHPUnit\Framework\TestCase class Metrics2DTest extends \PHPUnit\Framework\TestCase
{ {
public function testManhattan() : void public function testManhattan() : void
{ {
self::assertEquals( self::assertEquals(
10.0, 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( self::assertEqualsWithDelta(
7.615773, 7.615773,
Metric2D::euclidean(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]), Metrics2D::euclidean(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]),
0.1 0.1
); );
} }
@ -42,7 +42,7 @@ class Metric2DTest extends \PHPUnit\Framework\TestCase
{ {
self::assertEquals( self::assertEquals(
7.0, 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( self::assertEqualsWithDelta(
7.179, 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 0.1
); );
} }
@ -59,7 +59,7 @@ class Metric2DTest extends \PHPUnit\Framework\TestCase
{ {
self::assertEqualsWithDelta( self::assertEqualsWithDelta(
1.333, 1.333,
Metric2D::canberra(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]), Metrics2D::canberra(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]),
0.1 0.1
); );
} }
@ -68,7 +68,7 @@ class Metric2DTest extends \PHPUnit\Framework\TestCase
{ {
self::assertEqualsWithDelta( self::assertEqualsWithDelta(
0.625, 0.625,
Metric2D::brayCurtis(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]), Metrics2D::brayCurtis(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]),
0.1 0.1
); );
} }
@ -77,7 +77,7 @@ class Metric2DTest extends \PHPUnit\Framework\TestCase
{ {
self::assertEqualsWithDelta( self::assertEqualsWithDelta(
0.6508, 0.6508,
Metric2D::angularSeparation(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]), Metrics2D::angularSeparation(['x' => 0, 'y' => 3], ['x' => 7, 'y' => 6]),
0.1 0.1
); );
} }
@ -85,8 +85,8 @@ class Metric2DTest extends \PHPUnit\Framework\TestCase
public function testHammingDistance() : void public function testHammingDistance() : void
{ {
self::assertEquals( self::assertEquals(
3.0, 3,
Metric2D::hammingDistance([1, 1, 1, 1], [0, 1, 0, 0]), Metrics2D::hamming([1, 1, 1, 1], [0, 1, 0, 0]),
); );
} }
@ -94,7 +94,7 @@ class Metric2DTest extends \PHPUnit\Framework\TestCase
{ {
self::assertEquals( self::assertEquals(
2, 2,
Metric2D::hammingDistance([3, 6, 4, 8], [4, 6, 8, 3]) Metrics2D::ulam([3, 6, 4, 8], [4, 6, 8, 3])
); );
} }
} }

View File

@ -76,7 +76,10 @@ class HeapTest extends \PHPUnit\Framework\TestCase
} }
self::assertEquals(1, $heap->pushpop(6)); 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 public function testContains(): void
@ -100,13 +103,13 @@ class HeapTest extends \PHPUnit\Framework\TestCase
$heap = new Heap(); $heap = new Heap();
$heap->push(1); $heap->push(1);
self::assertEqals(1, $heap->peek()); self::assertEquals(1, $heap->peek());
$heap->push(2); $heap->push(2);
self::assertEqals(1, $heap->peek()); self::assertEquals(1, $heap->peek());
$heap->pop(); $heap->pop();
self::assertEqals(2, $heap->peek()); self::assertEquals(2, $heap->peek());
} }
public function testNSmallest() : void public function testNSmallest() : void