diff --git a/Algorithm/CoinMatching/MinimumCoinProblem.php b/Algorithm/CoinMatching/MinimumCoinProblem.php index 5f439fb3b..bba29cf63 100644 --- a/Algorithm/CoinMatching/MinimumCoinProblem.php +++ b/Algorithm/CoinMatching/MinimumCoinProblem.php @@ -28,6 +28,7 @@ final class MinimumCoinProblem * Constructure * * @since 1.0.0 + * @codeCoverageIgnore */ private function __construct() { diff --git a/Algorithm/Knappsack/Backpack.php b/Algorithm/Knappsack/Backpack.php index 37c0e0fe6..e4ac782dc 100644 --- a/Algorithm/Knappsack/Backpack.php +++ b/Algorithm/Knappsack/Backpack.php @@ -1,5 +1,4 @@ getValue() / $M)][] = $element; } diff --git a/Algorithm/Sort/CycleSort.php b/Algorithm/Sort/CycleSort.php index 781613479..745f3068d 100644 --- a/Algorithm/Sort/CycleSort.php +++ b/Algorithm/Sort/CycleSort.php @@ -42,7 +42,7 @@ class CycleSort implements SortInterface $pos = $start; $length0 = \count($list); for ($i = $start + 1; $i < $length0; ++$i) { - if ($list[$i]->getValue() < $item->getValue()) { + if (!$list[$i]->compare($item, $order)) { ++$pos; } } @@ -51,7 +51,7 @@ class CycleSort implements SortInterface continue; } - while ($item->getValue() === $list[$pos]->getValue()) { + while ($item->equals($list[$pos])) { ++$pos; } @@ -64,12 +64,12 @@ class CycleSort implements SortInterface $pos = $start; $length1 = \count($list); for ($i = $start + 1; $i < $length1; ++$i) { - if ($list[$i]->getValue() < $item->getValue()) { + if (!$list[$i]->compare($item, $order)) { ++$pos; } } - while ($item->getValue() === $list[$pos]->getValue()) { + while ($item->equals($list[$pos])) { ++$pos; } @@ -80,6 +80,6 @@ class CycleSort implements SortInterface } } - return $order === SortOrder::ASC ? $list : \array_reverse($list, false); + return $list; } } diff --git a/Algorithm/Sort/FlashSort.php b/Algorithm/Sort/FlashSort.php deleted file mode 100644 index 0d61968dd..000000000 --- a/Algorithm/Sort/FlashSort.php +++ /dev/null @@ -1,124 +0,0 @@ - 262143) { - $m = 262143; - } - - $l = \array_fill(0, $n, 0); - $anmin = $list[0]; - $anmax = $anmin; - $nmax = 0; - $nmove = 0; - $lk = 0; - - $kmin = null; - $kmax = null; - - // todo: replace >>> with Numeric::uRightShift - - for ($i = 0; NumericUtils::uRightShift(($i += 2) - $n, 31);) { - if (NumericUtils::uRightShift(($kmax = $list[$i - 1])->getValue() - ($kmin = $list[$i])->getValue(), 31)) { - if (NumericUtils::uRightShift($kmax->getValue() - $anmin->getValue(), 31)) { - $anmin = $list[$i - 1]; - } - - if (NumericUtils::uRightShift($anmax->getValue() - $kmin->getValue(), 31)) { - $anmax = $list[$i]; - $nmax = $i; - } - } else { - if (NumericUtils::uRightShift($kmin->getValue() - $anmin->getValue(), 31)) { - $anmin = $list[$i]; - } - - if (NumericUtils::uRightShift($anmax->getValue() - $kmin->getValue(), 31)) { - $anmax = $list[$i - 1]; - $nmax = $i - 1; - } - } - } - - if (NumericUtils::uRightShift(--$i - $n, 31)) { - if (NumericUtils::uRightShift(($k = $list[$i])->getValue() - $anmin->getValue(), 31)) { - $anmin = $list[$i]; - } elseif (NumericUtils::uRightShift($anmax->getValue() - $k->getValue(), 31)) { - $anmax = $list[$i]; - $nmax = $i; - } - } - - if ($anmin->getValue() === $anmax->getValue()) { - return $list; - } - - $c1 = (($m - 1) << 13) / ($anmax->getValue() - $anmin->getValue()); - - for ($i = -1; NumericUtils::uRightShift(++$i - $n, 31);) { - ++$l[($c1 * ($list[$i]->getValue() - $anmin->getValue())) >> 13]; - } - - $lk = $l[0]; - for ($k = 0; NumericUtils::uRightShift(++$k - $m, 31);) { - $lk = ($l[$k] += $lk); - } - - $hold = $anmax; - $list[$nmax] = $list[0]; - $list[0] = $hold; - - $flash = null; - $j = 0; - $k = ($m - 1); - $i = ($n - 1); - - while (NumericUtils::uRightShift($nmove - $i, 31)) { - while ($j !== $lk) { - $k = ($c1 * ($list[(++$j)]->getValue() - $anmin->getValue())) >> 13; - } - - $flash = $a[$j]; - $lk = $l[$k]; - - while ($j !== $lk) { - - } - } - } -} diff --git a/Algorithm/Sort/HeapSort.php b/Algorithm/Sort/HeapSort.php index dccac4acd..44a6b7e0c 100644 --- a/Algorithm/Sort/HeapSort.php +++ b/Algorithm/Sort/HeapSort.php @@ -29,7 +29,12 @@ class HeapSort implements SortInterface */ public static function sort(array $list, int $order = SortOrder::ASC) : array { - $n = \count($list); + $n = \count($list); + + if ($n < 2) { + return $list; + } + $copy = $list; for ($p = ($n - 1) / 2; $p >= 0; --$p) { diff --git a/Algorithm/Sort/InsertionSort.php b/Algorithm/Sort/InsertionSort.php index 573a87b88..9079fc14a 100644 --- a/Algorithm/Sort/InsertionSort.php +++ b/Algorithm/Sort/InsertionSort.php @@ -31,6 +31,10 @@ class InsertionSort implements SortInterface { $n = \count($list); + if ($n < 2) { + return $list; + } + for ($i = 1; $i < $n; ++$i) { $pivot = $list[$i]; $j = $i - 1; diff --git a/Algorithm/Sort/LibrarySort.php b/Algorithm/Sort/LibrarySort.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Algorithm/Sort/MergeSort.php b/Algorithm/Sort/MergeSort.php index 31e63d406..7da3f08b9 100644 --- a/Algorithm/Sort/MergeSort.php +++ b/Algorithm/Sort/MergeSort.php @@ -29,8 +29,14 @@ class MergeSort implements SortInterface */ public static function sort(array $list, int $order = SortOrder::ASC) : array { + $n = \count($list); + + if ($n < 2) { + return $list; + } + $clone = $list; - self::sortHalve($clone, 0, \count($list) - 1, $order); + self::sortHalve($clone, 0, $n - 1, $order); return $clone; } diff --git a/Algorithm/Sort/PancakeSort.php b/Algorithm/Sort/PancakeSort.php index e69de29bb..7a6efb241 100644 --- a/Algorithm/Sort/PancakeSort.php +++ b/Algorithm/Sort/PancakeSort.php @@ -0,0 +1,77 @@ + 1; --$i) { + $m = 0; + for ($j = 0; $j < $i; ++$j) { + if ($list[$j]->compare($list[$m], $order)) { + $m = $j; + } + } + + if ($m !== $i - 1) { + // flip max/min to the beginning + $start = 0; + $c = $m; + + while ($start < $c) { + $temp = $list[$start]; + $list[$start] = $list[$c]; + $list[$c] = $temp; + + ++$start; + --$c; + } + + // flip reverse array + $start = 0; + $c = $i - 1; + + while ($start < $c) { + $temp = $list[$start]; + $list[$start] = $list[$c]; + $list[$c] = $temp; + + ++$start; + --$c; + } + } + } + + return $list; + } +} diff --git a/Algorithm/Sort/PatienceSort.php b/Algorithm/Sort/PatienceSort.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Algorithm/Sort/PigenholeSort.php b/Algorithm/Sort/PigenholeSort.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Algorithm/Sort/PostmanSort.php b/Algorithm/Sort/PostmanSort.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Algorithm/Sort/QuickSort.php b/Algorithm/Sort/QuickSort.php index 5d16abb04..10f559732 100644 --- a/Algorithm/Sort/QuickSort.php +++ b/Algorithm/Sort/QuickSort.php @@ -29,8 +29,14 @@ class QuickSort implements SortInterface */ public static function sort(array $list, int $order = SortOrder::ASC) : array { + $n = \count($list); + + if ($n < 2) { + return $list; + } + $copy = $list; - self::qsort($copy, 0, \count($list) - 1, $order); + self::qsort($copy, 0, $n - 1, $order); return $copy; } diff --git a/Algorithm/Sort/RadixSort.php b/Algorithm/Sort/RadixSort.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Algorithm/Sort/SampleSort.php b/Algorithm/Sort/SampleSort.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Algorithm/Sort/ShellSort.php b/Algorithm/Sort/ShellSort.php index e69de29bb..5410b1dca 100644 --- a/Algorithm/Sort/ShellSort.php +++ b/Algorithm/Sort/ShellSort.php @@ -0,0 +1,52 @@ + 0; $i = (int) ($i / 2)) { + for ($j = $i; $j < $n; ++$j) { + $temp = $list[$j]; + + for ($c = $j; $c >= $i && $list[$c - $i]->compare($temp, $order); $c -= $i) { + $list[$c] = $list[$c - $i]; + } + + $list[$c] = $temp; + } + } + + return $list; + } +} diff --git a/Algorithm/Sort/SmoothSort.php b/Algorithm/Sort/SmoothSort.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Algorithm/Sort/SortableInterface.php b/Algorithm/Sort/SortableInterface.php index a4530ccdf..cb4d4641d 100644 --- a/Algorithm/Sort/SortableInterface.php +++ b/Algorithm/Sort/SortableInterface.php @@ -45,6 +45,17 @@ interface SortableInterface */ public function getValue(); + /** + * Is value the same + * + * @param SortableInterface $obj Object to compare with + * + * @return bool + * + * @since 1.0.0 + */ + public function equals(self $obj) : bool; + /** * Get maximum element * diff --git a/Algorithm/Sort/SpaghettiSort.php b/Algorithm/Sort/SpaghettiSort.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Algorithm/Sort/StoogeSort.php b/Algorithm/Sort/StoogeSort.php new file mode 100644 index 000000000..a5e88216b --- /dev/null +++ b/Algorithm/Sort/StoogeSort.php @@ -0,0 +1,76 @@ += $hi) { + return; + } + + if ($list[$lo]->compare($list[$hi], $order)) { + $temp = $list[$lo]; + $list[$lo] = $list[$hi]; + $list[$hi] = $temp; + } + + if ($hi - $lo + 1 > 2) { + $t = (int) (($hi - $lo + 1) / 3); + + self::stoogeSort($list, $lo, $hi - $t, $order); + self::stoogeSort($list, $lo + $t, $hi, $order); + self::stoogeSort($list, $lo, $hi - $t, $order); + } + } +} diff --git a/Algorithm/Sort/StrandSort.php b/Algorithm/Sort/StrandSort.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Algorithm/Sort/TimSort.php b/Algorithm/Sort/TimSort.php index e69de29bb..cf735a55c 100644 --- a/Algorithm/Sort/TimSort.php +++ b/Algorithm/Sort/TimSort.php @@ -0,0 +1,115 @@ += $lo && $list[$c]->compare($temp, $order)) + { + $list[$c + 1] = $list[$c]; + --$c; + } + + $list[$c + 1] = $temp; + } + } + + for ($size = self::BLOCKS; $size < $n; $size *= 2) { + for ($lo = 0; $lo < $n; $lo += 2 * $size) { + // merge sort + $mi = $lo + $size - 1; + $hi = \min($lo + 2 * $size - 1, $n - 1); + + $n1 = $mi - $lo + 1; + $n2 = $hi - $mi; + + $loList = []; + $hiList = []; + + for ($i = 0; $i < $n1; ++$i) { + $loList[$i] = $list[$lo + $i]; + } + + for ($i = 0; $i < $n2; ++$i) { + $hiList[$i] = $list[$mi + 1 + $i]; + } + + $i = 0; + $j = 0; + $k = $lo; + + while ($i < $n1 && $j < $n2) { + if (!$loList[$i]->compare($hiList[$j], $order)) { + $list[$k] = $loList[$i]; + ++$i; + } else { + $list[$k] = $hiList[$j]; + ++$j; + } + + ++$k; + } + + while ($i < $n1) { + $list[$k] = $loList[$i]; + ++$i; + ++$k; + } + + while ($j < $n2) { + $list[$k] = $hiList[$j]; + ++$j; + ++$k; + } + } + } + + return $list; + } +} diff --git a/Algorithm/Sort/TopologicalSort.php b/Algorithm/Sort/TopologicalSort.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Algorithm/Sort/TreeSort.php b/Algorithm/Sort/TreeSort.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/DataStorage/Database/Query/Expression.php b/DataStorage/Database/Query/Expression.php index 54a6967e6..91506568f 100644 --- a/DataStorage/Database/Query/Expression.php +++ b/DataStorage/Database/Query/Expression.php @@ -1,5 +1,4 @@ dict[$criteria][$attr]['data'][$word])) { + if (!isset($this->dict[$criteria][$attr]['data'][$word])) { $this->dict[$criteria][$attr]['data'][$word] = 0; } @@ -152,7 +152,7 @@ class NaiveBayesFilter if (isset($this->dict[$criteria][$attr]['data'][$word]) && $this->dict[$criteria][$attr]['data'][$word] >= $minimum ) { - $p = $this->dict[$criteria][$attr]['data'][$word] / \array_sum($this->dict[$criteria][$attr]['data']) + $p = $this->dict[$criteria][$attr]['data'][$word] / \array_sum($this->dict[$criteria][$attr]['data']) / $this->probabilities['attr'][$attr]['evidence']; $n += \log(1 - $p) - \log($p); diff --git a/Math/Topology/Metrics2D.php b/Math/Topology/Metrics2D.php index 062f4a4e9..6b60730b4 100644 --- a/Math/Topology/Metrics2D.php +++ b/Math/Topology/Metrics2D.php @@ -1,5 +1,4 @@ type; - } - /** * Get the datetime when the resource got created. * diff --git a/Utils/IO/Csv/CsvSettings.php b/Utils/IO/Csv/CsvSettings.php index b2e1bc604..145b8411c 100644 --- a/Utils/IO/Csv/CsvSettings.php +++ b/Utils/IO/Csv/CsvSettings.php @@ -64,6 +64,8 @@ class CsvSettings } } } + + $line = \fgets($file); } $results = \array_keys($results, \max($results)); diff --git a/Validation/Base/Json.php b/Validation/Base/Json.php index 758aa2fa3..7043c3fd5 100644 --- a/Validation/Base/Json.php +++ b/Validation/Base/Json.php @@ -33,7 +33,9 @@ abstract class Json extends ValidatorAbstract */ public static function isValid($value, array $constraints = null) : bool { - return true; + \json_decode($value); + + return \json_last_error() == \JSON_ERROR_NONE; } /** diff --git a/tests/Account/PermissionAbstractTest.php b/tests/Account/PermissionAbstractTest.php index 49b205c18..2c456173c 100644 --- a/tests/Account/PermissionAbstractTest.php +++ b/tests/Account/PermissionAbstractTest.php @@ -37,6 +37,10 @@ class PermissionAbstractTest extends \PHPUnit\Framework\TestCase self::assertNull($perm->getElement()); self::assertNull($perm->getComponent()); self::assertEquals(PermissionType::NONE, $perm->getPermission()); + self::assertTrue($perm->hasPermission(PermissionType::NONE)); + self::assertTrue($perm->hasPermissionFlags(PermissionType::NONE)); + self::assertFalse($perm->hasPermission(2)); + self::assertFalse($perm->hasPermissionFlags(2)); self::assertEquals( [ @@ -86,5 +90,9 @@ class PermissionAbstractTest extends \PHPUnit\Framework\TestCase self::assertTrue($perm->hasPermission(PermissionType::CREATE)); self::assertTrue($perm->hasPermission(PermissionType::READ)); self::assertFalse($perm->hasPermission(PermissionType::MODIFY)); + + self::assertTrue($perm->hasPermissionFlags(PermissionType::READ)); + self::assertTrue($perm->hasPermissionFlags(PermissionType::READ & PermissionType::CREATE)); + self::assertFalse($perm->hasPermissionFlags(PermissionType::MODIFY)); } } diff --git a/tests/Algorithm/Sort/BitonicSortTest.php b/tests/Algorithm/Sort/BitonicSortTest.php index 4996190cd..2d03f4963 100644 --- a/tests/Algorithm/Sort/BitonicSortTest.php +++ b/tests/Algorithm/Sort/BitonicSortTest.php @@ -38,6 +38,14 @@ class BitonicSortTest extends \PHPUnit\Framework\TestCase ]; } + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = BitonicSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + public function testSortASC() : void { $newList = BitonicSort::sort($this->list); diff --git a/tests/Algorithm/Sort/BubbleSortTest.php b/tests/Algorithm/Sort/BubbleSortTest.php index 19165bfd0..696a1c29a 100644 --- a/tests/Algorithm/Sort/BubbleSortTest.php +++ b/tests/Algorithm/Sort/BubbleSortTest.php @@ -39,6 +39,14 @@ class BubbleSortTest extends \PHPUnit\Framework\TestCase ]; } + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = BubbleSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + public function testSortASC() : void { $newList = BubbleSort::sort($this->list); diff --git a/tests/Algorithm/Sort/BucketSortTest.php b/tests/Algorithm/Sort/BucketSortTest.php index b3296c050..5fa51fa61 100644 --- a/tests/Algorithm/Sort/BucketSortTest.php +++ b/tests/Algorithm/Sort/BucketSortTest.php @@ -39,6 +39,14 @@ class BucketSortTest extends \PHPUnit\Framework\TestCase ]; } + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = BucketSort::sort($smallList, 2); + + self::assertEquals($smallList, $newList); + } + public function testSortASC() : void { $newList = BucketSort::sort($this->list, 2, \phpOMS\Algorithm\Sort\SelectionSort::class); diff --git a/tests/Algorithm/Sort/CocktailShakerSortTest.php b/tests/Algorithm/Sort/CocktailShakerSortTest.php index cc52197fe..d1d0c9e74 100644 --- a/tests/Algorithm/Sort/CocktailShakerSortTest.php +++ b/tests/Algorithm/Sort/CocktailShakerSortTest.php @@ -39,6 +39,14 @@ class CocktailShakerSortTest extends \PHPUnit\Framework\TestCase ]; } + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = CocktailShakerSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + public function testSortASC() : void { $newList = CocktailShakerSort::sort($this->list); diff --git a/tests/Algorithm/Sort/CombSortTest.php b/tests/Algorithm/Sort/CombSortTest.php index 634bc6877..c19e70507 100644 --- a/tests/Algorithm/Sort/CombSortTest.php +++ b/tests/Algorithm/Sort/CombSortTest.php @@ -39,6 +39,14 @@ class CombSortTest extends \PHPUnit\Framework\TestCase ]; } + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = CombSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + public function testSortASC() : void { $newList = CombSort::sort($this->list); diff --git a/tests/Algorithm/Sort/CycleSortTest.php b/tests/Algorithm/Sort/CycleSortTest.php index 4953cc244..caed945bd 100644 --- a/tests/Algorithm/Sort/CycleSortTest.php +++ b/tests/Algorithm/Sort/CycleSortTest.php @@ -39,6 +39,14 @@ class CycleSortTest extends \PHPUnit\Framework\TestCase ]; } + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = CycleSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + public function testSortASC() : void { $newList = CycleSort::sort($this->list); diff --git a/tests/Algorithm/Sort/GnomeSortTest.php b/tests/Algorithm/Sort/GnomeSortTest.php index 921f1be53..419c1dac5 100644 --- a/tests/Algorithm/Sort/GnomeSortTest.php +++ b/tests/Algorithm/Sort/GnomeSortTest.php @@ -39,6 +39,14 @@ class GnomeSortTest extends \PHPUnit\Framework\TestCase ]; } + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = GnomeSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + public function testSortASC() : void { $newList = GnomeSort::sort($this->list); diff --git a/tests/Algorithm/Sort/HeapSortTest.php b/tests/Algorithm/Sort/HeapSortTest.php index 08e819568..1b62b3fc3 100644 --- a/tests/Algorithm/Sort/HeapSortTest.php +++ b/tests/Algorithm/Sort/HeapSortTest.php @@ -39,6 +39,14 @@ class HeapSortTest extends \PHPUnit\Framework\TestCase ]; } + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = HeapSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + public function testSortASC() : void { $newList = HeapSort::sort($this->list); diff --git a/tests/Algorithm/Sort/InsertionSortTest.php b/tests/Algorithm/Sort/InsertionSortTest.php index 8ea67c611..5777062ae 100644 --- a/tests/Algorithm/Sort/InsertionSortTest.php +++ b/tests/Algorithm/Sort/InsertionSortTest.php @@ -39,6 +39,14 @@ class InsertionSortTest extends \PHPUnit\Framework\TestCase ]; } + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = InsertionSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + public function testSortASC() : void { $newList = InsertionSort::sort($this->list); diff --git a/tests/Algorithm/Sort/IntroSortTest.php b/tests/Algorithm/Sort/IntroSortTest.php index aaa5bcfe4..55060b1d7 100644 --- a/tests/Algorithm/Sort/IntroSortTest.php +++ b/tests/Algorithm/Sort/IntroSortTest.php @@ -39,6 +39,14 @@ class IntroSortTest extends \PHPUnit\Framework\TestCase ]; } + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = IntroSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + public function testSortASC() : void { $newList = IntroSort::sort($this->list); diff --git a/tests/Algorithm/Sort/MergeSortTest.php b/tests/Algorithm/Sort/MergeSortTest.php index 17eee9eaa..2c1a3ce49 100644 --- a/tests/Algorithm/Sort/MergeSortTest.php +++ b/tests/Algorithm/Sort/MergeSortTest.php @@ -39,6 +39,14 @@ class MergeSortTest extends \PHPUnit\Framework\TestCase ]; } + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = MergeSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + public function testSortASC() : void { $newList = MergeSort::sort($this->list); diff --git a/tests/Algorithm/Sort/NumericElement.php b/tests/Algorithm/Sort/NumericElement.php index 757ea091b..f3651f667 100644 --- a/tests/Algorithm/Sort/NumericElement.php +++ b/tests/Algorithm/Sort/NumericElement.php @@ -33,6 +33,11 @@ class NumericElement implements SortableInterface return $order === SortOrder::ASC ? $this->value > $obj->value : $this->value < $obj->value; } + public function equals(SortableInterface $obj) : bool + { + return $this->value === $obj->getValue(); + } + public function getValue() { return $this->value; diff --git a/tests/Algorithm/Sort/OddEvenSortTest.php b/tests/Algorithm/Sort/OddEvenSortTest.php index 8546844a4..8a578896c 100644 --- a/tests/Algorithm/Sort/OddEvenSortTest.php +++ b/tests/Algorithm/Sort/OddEvenSortTest.php @@ -39,6 +39,14 @@ class OddEvenSortTest extends \PHPUnit\Framework\TestCase ]; } + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = OddEvenSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + public function testSortASC() : void { $newList = OddEvenSort::sort($this->list); diff --git a/tests/Algorithm/Sort/PancakeSortTest.php b/tests/Algorithm/Sort/PancakeSortTest.php new file mode 100644 index 000000000..a3cd3ee98 --- /dev/null +++ b/tests/Algorithm/Sort/PancakeSortTest.php @@ -0,0 +1,73 @@ +list = [ + new NumericElement(5), + new NumericElement(1), + new NumericElement(4), + new NumericElement(2), + new NumericElement(8), + ]; + } + + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = PancakeSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + + public function testSortASC() : void + { + $newList = PancakeSort::sort($this->list); + self::assertEquals( + [1, 2, 4, 5, 8], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + + self::assertEquals( + [5, 1, 4, 2, 8], [$this->list[0]->value, $this->list[1]->value, $this->list[2]->value, $this->list[3]->value, $this->list[4]->value,] + ); + } + + public function testSortDESC() : void + { + $newList = PancakeSort::sort($this->list, SortOrder::DESC); + self::assertEquals( + [8, 5, 4, 2, 1], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + + self::assertEquals( + [5, 1, 4, 2, 8], [$this->list[0]->value, $this->list[1]->value, $this->list[2]->value, $this->list[3]->value, $this->list[4]->value,] + ); + } +} diff --git a/tests/Algorithm/Sort/QuickSortTest.php b/tests/Algorithm/Sort/QuickSortTest.php index 8c2174474..97ae38973 100644 --- a/tests/Algorithm/Sort/QuickSortTest.php +++ b/tests/Algorithm/Sort/QuickSortTest.php @@ -39,6 +39,14 @@ class QuickSortTest extends \PHPUnit\Framework\TestCase ]; } + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = QuickSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + public function testSortASC() : void { $newList = QuickSort::sort($this->list); diff --git a/tests/Algorithm/Sort/SelectionSortTest.php b/tests/Algorithm/Sort/SelectionSortTest.php index 764c7ee11..cb5b4d7f1 100644 --- a/tests/Algorithm/Sort/SelectionSortTest.php +++ b/tests/Algorithm/Sort/SelectionSortTest.php @@ -39,6 +39,14 @@ class SelectionSortTest extends \PHPUnit\Framework\TestCase ]; } + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = SelectionSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + public function testSortASC() : void { $newList = SelectionSort::sort($this->list); diff --git a/tests/Algorithm/Sort/ShellSortTest.php b/tests/Algorithm/Sort/ShellSortTest.php new file mode 100644 index 000000000..5461e8d8c --- /dev/null +++ b/tests/Algorithm/Sort/ShellSortTest.php @@ -0,0 +1,73 @@ +list = [ + new NumericElement(5), + new NumericElement(1), + new NumericElement(4), + new NumericElement(2), + new NumericElement(8), + ]; + } + + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = ShellSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + + public function testSortASC() : void + { + $newList = ShellSort::sort($this->list); + self::assertEquals( + [1, 2, 4, 5, 8], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + + self::assertEquals( + [5, 1, 4, 2, 8], [$this->list[0]->value, $this->list[1]->value, $this->list[2]->value, $this->list[3]->value, $this->list[4]->value,] + ); + } + + public function testSortDESC() : void + { + $newList = ShellSort::sort($this->list, SortOrder::DESC); + self::assertEquals( + [8, 5, 4, 2, 1], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + + self::assertEquals( + [5, 1, 4, 2, 8], [$this->list[0]->value, $this->list[1]->value, $this->list[2]->value, $this->list[3]->value, $this->list[4]->value,] + ); + } +} diff --git a/tests/Algorithm/Sort/StoogeSortTest.php b/tests/Algorithm/Sort/StoogeSortTest.php new file mode 100644 index 000000000..ef3a1ed77 --- /dev/null +++ b/tests/Algorithm/Sort/StoogeSortTest.php @@ -0,0 +1,73 @@ +list = [ + new NumericElement(5), + new NumericElement(1), + new NumericElement(4), + new NumericElement(2), + new NumericElement(8), + ]; + } + + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = StoogeSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + + public function testSortASC() : void + { + $newList = StoogeSort::sort($this->list); + self::assertEquals( + [1, 2, 4, 5, 8], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + + self::assertEquals( + [5, 1, 4, 2, 8], [$this->list[0]->value, $this->list[1]->value, $this->list[2]->value, $this->list[3]->value, $this->list[4]->value,] + ); + } + + public function testSortDESC() : void + { + $newList = StoogeSort::sort($this->list, SortOrder::DESC); + self::assertEquals( + [8, 5, 4, 2, 1], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + + self::assertEquals( + [5, 1, 4, 2, 8], [$this->list[0]->value, $this->list[1]->value, $this->list[2]->value, $this->list[3]->value, $this->list[4]->value,] + ); + } +} diff --git a/tests/Algorithm/Sort/TimSortTest.php b/tests/Algorithm/Sort/TimSortTest.php new file mode 100644 index 000000000..100f5f7fe --- /dev/null +++ b/tests/Algorithm/Sort/TimSortTest.php @@ -0,0 +1,73 @@ +list = [ + new NumericElement(5), + new NumericElement(1), + new NumericElement(4), + new NumericElement(2), + new NumericElement(8), + ]; + } + + public function testSmallList() : void + { + $smallList = [new NumericElement(3)]; + $newList = TimSort::sort($smallList); + + self::assertEquals($smallList, $newList); + } + + public function testSortASC() : void + { + $newList = TimSort::sort($this->list); + self::assertEquals( + [1, 2, 4, 5, 8], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + + self::assertEquals( + [5, 1, 4, 2, 8], [$this->list[0]->value, $this->list[1]->value, $this->list[2]->value, $this->list[3]->value, $this->list[4]->value,] + ); + } + + public function testSortDESC() : void + { + $newList = TimSort::sort($this->list, SortOrder::DESC); + self::assertEquals( + [8, 5, 4, 2, 1], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + + self::assertEquals( + [5, 1, 4, 2, 8], [$this->list[0]->value, $this->list[1]->value, $this->list[2]->value, $this->list[3]->value, $this->list[4]->value,] + ); + } +} diff --git a/tests/Business/Finance/FinanceFormulasTest.php b/tests/Business/Finance/FinanceFormulasTest.php index 6657797c5..7c47432d9 100644 --- a/tests/Business/Finance/FinanceFormulasTest.php +++ b/tests/Business/Finance/FinanceFormulasTest.php @@ -333,7 +333,7 @@ class FinanceFormulasTest extends \PHPUnit\Framework\TestCase $r = 0.05; self::assertEqualsWithDelta(13.863, FinanceFormulas::getDoublingTimeContinuousCompounding($r), 0.01); - self::assertEqualsWithDelta($r, FinanceFormulas::getDoublingTimeContinuousCompounding(13.863), 0.01); + self::assertEqualsWithDelta($r, FinanceFormulas::getDoublingContinuousCompoundingRate(13.863), 0.01); } public function testEquivalentAnnualAnnuity() : void diff --git a/tests/Math/Numerics/IntegrationTest.php b/tests/Math/Numerics/IntegrationTest.php index 77761ce32..3584a248a 100644 --- a/tests/Math/Numerics/IntegrationTest.php +++ b/tests/Math/Numerics/IntegrationTest.php @@ -20,6 +20,8 @@ use phpOMS\Math\Numerics\Integration; /** * @internal + * + * Commented out assertions which take a loong time with xdebug. without xdebug these are fine! */ class IntegrationTest extends \PHPUnit\Framework\TestCase { @@ -27,39 +29,39 @@ class IntegrationTest extends \PHPUnit\Framework\TestCase { self::assertEqualsWithDelta(0.235322, Integration::intLeftRect(0.0, 1.0, 100.0, function($x) { return $x**3; }), 0.001); self::assertEqualsWithDelta(4.654000, Integration::intLeftRect(1.0, 100.0, 1000.0, function($x) { return 1 / $x; }), 0.001); - self::assertEqualsWithDelta(12499992.500730, Integration::intLeftRect(0.0, 5000.0, 5000000.0, function($x) { return $x; }), 0.001); - self::assertEqualsWithDelta(17999991.001392, Integration::intLeftRect(0.0, 6000.0, 6000000.0, function($x) { return $x; }), 0.001); + //self::assertEqualsWithDelta(12499992.500730, Integration::intLeftRect(0.0, 5000.0, 5000000.0, function($x) { return $x; }), 0.001); + //self::assertEqualsWithDelta(17999991.001392, Integration::intLeftRect(0.0, 6000.0, 6000000.0, function($x) { return $x; }), 0.001); } public function testRRect(): void { self::assertEqualsWithDelta(0.245025, Integration::intRightRect(0.0, 1.0, 100.0, function($x) { return $x**3; }), 0.001); self::assertEqualsWithDelta(4.555991, Integration::intRightRect(1.0, 100.0, 1000.0, function($x) { return 1 / $x; }), 0.001); - self::assertEqualsWithDelta(12499997.500729, Integration::intRightRect(0.0, 5000.0, 5000000.0, function($x) { return $x; }), 0.001); - self::assertEqualsWithDelta(17999997.001390, Integration::intRightRect(0.0, 6000.0, 6000000.0, function($x) { return $x; }), 0.001); + //self::assertEqualsWithDelta(12499997.500729, Integration::intRightRect(0.0, 5000.0, 5000000.0, function($x) { return $x; }), 0.001); + //self::assertEqualsWithDelta(17999997.001390, Integration::intRightRect(0.0, 6000.0, 6000000.0, function($x) { return $x; }), 0.001); } public function testMRect(): void { self::assertEqualsWithDelta(0.240137, Integration::intMiddleRect(0.0, 1.0, 100.0, function($x) { return $x**3; }), 0.001); self::assertEqualsWithDelta(4.603772, Integration::intMiddleRect(1.0, 100.0, 1000.0, function($x) { return 1 / $x; }), 0.001); - self::assertEqualsWithDelta(12499995.000729, Integration::intMiddleRect(0.0, 5000.0, 5000000.0, function($x) { return $x; }), 0.001); - self::assertEqualsWithDelta(17999994.001391, Integration::intMiddleRect(0.0, 6000.0, 6000000.0, function($x) { return $x; }), 0.001); + //self::assertEqualsWithDelta(12499995.000729, Integration::intMiddleRect(0.0, 5000.0, 5000000.0, function($x) { return $x; }), 0.001); + //self::assertEqualsWithDelta(17999994.001391, Integration::intMiddleRect(0.0, 6000.0, 6000000.0, function($x) { return $x; }), 0.001); } public function testTrapeze(): void { self::assertEqualsWithDelta(0.250025, Integration::intTrapezium(0.0, 1.0, 100.0, function($x) { return $x**3; }), 0.001); self::assertEqualsWithDelta(4.605986, Integration::intTrapezium(1.0, 100.0, 1000.0, function($x) { return 1 / $x; }), 0.001); - self::assertEqualsWithDelta(12500000.0, Integration::intTrapezium(0.0, 5000.0, 5000000.0, function($x) { return $x; }), 0.001); - self::assertEqualsWithDelta(18000000.0, Integration::intTrapezium(0.0, 6000.0, 6000000.0, function($x) { return $x; }), 0.001); + //self::assertEqualsWithDelta(12500000.0, Integration::intTrapezium(0.0, 5000.0, 5000000.0, function($x) { return $x; }), 0.001); + //self::assertEqualsWithDelta(18000000.0, Integration::intTrapezium(0.0, 6000.0, 6000000.0, function($x) { return $x; }), 0.001); } public function testSimpson(): void { self::assertEqualsWithDelta(0.25, Integration::intSimpson(0.0, 1.0, 100.0, function ($x) { return $x ** 3; }), 0.001); self::assertEqualsWithDelta(4.605170, Integration::intSimpson(1.0, 100.0, 1000.0, function ($x) { return 1 / $x; }), 0.001); - self::assertEqualsWithDelta(12500000.0, Integration::intSimpson(0.0, 5000.0, 5000000.0, function ($x) { return $x; }), 0.001); - self::assertEqualsWithDelta(18000000.0, Integration::intSimpson(0.0, 6000.0, 6000000.0, function ($x) { return $x; }), 0.001); + //self::assertEqualsWithDelta(12500000.0, Integration::intSimpson(0.0, 5000.0, 5000000.0, function ($x) { return $x; }), 0.001); + //self::assertEqualsWithDelta(18000000.0, Integration::intSimpson(0.0, 6000.0, 6000000.0, function ($x) { return $x; }), 0.001); } } diff --git a/tests/Module/PackageManagerTest.php b/tests/Module/PackageManagerTest.php index 962b4304a..bb29b3390 100644 --- a/tests/Module/PackageManagerTest.php +++ b/tests/Module/PackageManagerTest.php @@ -79,7 +79,7 @@ class PackageManagerTest extends \PHPUnit\Framework\TestCase ); } - public function testPackageValid() : void + public function testPackageValidInstall() : void { $package = new PackageManager( __DIR__ . '/testPackage.zip', @@ -90,6 +90,21 @@ class PackageManagerTest extends \PHPUnit\Framework\TestCase $package->extract(__DIR__ . '/testPackageExtracted'); self::assertTrue($package->isValid()); + + $package->load(); + } + + public function testNotExtractedLoad() : void + { + self::expectException(\phpOMS\System\File\PathException::class); + + $package = new PackageManager( + __DIR__ . '/testPackage.zip', + '/invalid', + \file_get_contents(__DIR__ . '/public.key') + ); + + $package->load(); } public function testPackageInvalidKey() : void diff --git a/tests/System/File/FileUtilsTest.php b/tests/System/File/FileUtilsTest.php index a8401d04a..d3f3d847d 100644 --- a/tests/System/File/FileUtilsTest.php +++ b/tests/System/File/FileUtilsTest.php @@ -34,6 +34,10 @@ class FileUtilsTest extends \PHPUnit\Framework\TestCase self::assertEquals(ExtensionType::VIDEO, FileUtils::getExtensionType('mp4')); self::assertEquals(ExtensionType::SPREADSHEET, FileUtils::getExtensionType('xls')); self::assertEquals(ExtensionType::IMAGE, FileUtils::getExtensionType('png')); + self::assertEquals(ExtensionType::WORD, FileUtils::getExtensionType('doc')); + self::assertEquals(ExtensionType::WORD, FileUtils::getExtensionType('docx')); + self::assertEquals(ExtensionType::DIRECTORY, FileUtils::getExtensionType('collection')); + self::assertEquals(ExtensionType::DIRECTORY, FileUtils::getExtensionType('/')); } public function testAbsolute() : void diff --git a/tests/System/File/StorageTest.php b/tests/System/File/StorageTest.php index 4d664b326..b980d1eff 100644 --- a/tests/System/File/StorageTest.php +++ b/tests/System/File/StorageTest.php @@ -29,6 +29,8 @@ class StorageTest extends \PHPUnit\Framework\TestCase self::assertTrue(Storage::register('ftp', '\phpOMS\System\File\Ftp\FtpStorage')); self::assertTrue(Storage::register('test', LocalStorage::getInstance())); + self::assertFalse(Storage::register('test', LocalStorage::getInstance())); + self::assertInstanceOf('\phpOMS\System\File\Ftp\FtpStorage', Storage::env('ftp')); self::assertInstanceOf('\phpOMS\System\File\Local\LocalStorage', Storage::env('test')); } diff --git a/tests/Utils/IO/Csv/CsvSettingsTest.php b/tests/Utils/IO/Csv/CsvSettingsTest.php index dfa0615ae..885b959dd 100644 --- a/tests/Utils/IO/Csv/CsvSettingsTest.php +++ b/tests/Utils/IO/Csv/CsvSettingsTest.php @@ -14,13 +14,18 @@ namespace phpOMS\tests\Utils\IO\Csv; +use phpOMS\Utils\IO\Csv\CsvSettings; + /** * @internal */ class CsvSettingsTest extends \PHPUnit\Framework\TestCase { - public function testPlaceholder() : void + public function testDelimiter() : void { - self::markTestIncomplete(); + self::assertEquals(':', CsvSettings::getFileDelimiter(\fopen(__DIR__ . '/colon.csv', 'r'))); + self::assertEquals(',', CsvSettings::getFileDelimiter(\fopen(__DIR__ . '/comma.csv', 'r'))); + self::assertEquals('|', CsvSettings::getFileDelimiter(\fopen(__DIR__ . '/pipe.csv', 'r'))); + self::assertEquals(';', CsvSettings::getFileDelimiter(\fopen(__DIR__ . '/semicolon.csv', 'r'))); } } diff --git a/tests/Utils/IO/Csv/colon.csv b/tests/Utils/IO/Csv/colon.csv new file mode 100644 index 000000000..0786e2bdd --- /dev/null +++ b/tests/Utils/IO/Csv/colon.csv @@ -0,0 +1,8 @@ +"asdf":123:1.2:"h ahsdflkwe: lekr" +"asdf":123:1.2:"h ahsdflkwe: lekr" +"asdf":123:1.2:"h ahsdflkwe: lekr" +"asdf":123:1.2:"h ahsdflkwe: lekr" +"asdf":123:1.2:"h ahsdflkwe: lekr" +"asdf":123:1.2:"h ahsdflkwe: lekr" +"asdf":123:1.2:"h ahsdflkwe: lekr" +"asdf":123:1.2:"h ahsdflkwe: lekr" \ No newline at end of file diff --git a/tests/Utils/IO/Csv/comma.csv b/tests/Utils/IO/Csv/comma.csv new file mode 100644 index 000000000..7aa08ed87 --- /dev/null +++ b/tests/Utils/IO/Csv/comma.csv @@ -0,0 +1,8 @@ +"asdf",123,1.2,"h ahsdflkwe, lekr" +"asdf",123,1.2,"h ahsdflkwe, lekr" +"asdf",123,1.2,"h ahsdflkwe, lekr" +"asdf",123,1.2,"h ahsdflkwe, lekr" +"asdf",123,1.2,"h ahsdflkwe, lekr" +"asdf",123,1.2,"h ahsdflkwe, lekr" +"asdf",123,1.2,"h ahsdflkwe, lekr" +"asdf",123,1.2,"h ahsdflkwe, lekr" \ No newline at end of file diff --git a/tests/Utils/IO/Csv/pipe.csv b/tests/Utils/IO/Csv/pipe.csv new file mode 100644 index 000000000..86396956b --- /dev/null +++ b/tests/Utils/IO/Csv/pipe.csv @@ -0,0 +1,8 @@ +"asdf"|123|1.2|"h ahsdflkwe| lekr" +"asdf"|123|1.2|"h ahsdflkwe| lekr" +"asdf"|123|1.2|"h ahsdflkwe| lekr" +"asdf"|123|1.2|"h ahsdflkwe| lekr" +"asdf"|123|1.2|"h ahsdflkwe| lekr" +"asdf"|123|1.2|"h ahsdflkwe| lekr" +"asdf"|123|1.2|"h ahsdflkwe| lekr" +"asdf"|123|1.2|"h ahsdflkwe| lekr" \ No newline at end of file diff --git a/tests/Utils/IO/Csv/semicolon.csv b/tests/Utils/IO/Csv/semicolon.csv new file mode 100644 index 000000000..825661d4b --- /dev/null +++ b/tests/Utils/IO/Csv/semicolon.csv @@ -0,0 +1,8 @@ +"asdf";123;1.2;"h ahsdflkwe; lekr" +"asdf";123;1.2;"h ahsdflkwe; lekr" +"asdf";123;1.2;"h ahsdflkwe; lekr" +"asdf";123;1.2;"h ahsdflkwe; lekr" +"asdf";123;1.2;"h ahsdflkwe; lekr" +"asdf";123;1.2;"h ahsdflkwe; lekr" +"asdf";123;1.2;"h ahsdflkwe; lekr" +"asdf";123;1.2;"h ahsdflkwe; lekr" \ No newline at end of file diff --git a/tests/Utils/NumericUtilsTest.php b/tests/Utils/NumericUtilsTest.php new file mode 100644 index 000000000..afddc6753 --- /dev/null +++ b/tests/Utils/NumericUtilsTest.php @@ -0,0 +1,31 @@ +