diff --git a/DIRECTORY.md b/DIRECTORY.md index 8355e85..3e794c5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -32,17 +32,12 @@ ## Searches * [Binarysearch](./Searches/BinarySearch.php) - -## Searches - * [Fibonaccisearch](./searches/fibonacciSearch.php) - -## Searches + * [Fibonaccisearch](./Searches/FibonacciSearch.php) * [Linearsearch](./Searches/LinearSearch.php) * [Lowerbound](./Searches/LowerBound.php) * [Upperbound](./Searches/UpperBound.php) ## Sorting - * [Arraykeyssort](./sorting/arrayKeysSort.php) * [Bubblesort](./Sorting/BubbleSort.php) * [Bubblesort2](./Sorting/BubbleSort2.php) * [Countsort](./Sorting/CountSort.php) @@ -75,10 +70,10 @@ * [Mathstest](./tests/Maths/MathsTest.php) * [Projecteulertest](./tests/Maths/ProjectEulerTest.php) * Searches - * [Binarysearchtest](./tests/Searches/BinarySearchTest.php) * [Searchestest](./tests/Searches/SearchesTest.php) * Sorting * [Arraykeyssorttest](./tests/sorting/arrayKeysSortTest.php) + * Sorting * [Sortingtests](./tests/Sorting/SortingTests.php) * Strings * [Stringstest](./tests/Strings/StringsTest.php) diff --git a/Searches/BinarySearch.php b/Searches/BinarySearch.php index 337d33c..7b9b1f1 100644 --- a/Searches/BinarySearch.php +++ b/Searches/BinarySearch.php @@ -61,6 +61,14 @@ function binarySearchIterative($list, $target) */ function binarySearchByRecursion($list, $target, $start, $end) { + if (count($list) == 0) { + return null; + } + + if (count($list) < 2) { + return $list[0] == $target ? 0 : null; + } + if ($start > $end) return null; diff --git a/searches/fibonacciSearch.php b/Searches/FibonacciSearch.php similarity index 100% rename from searches/fibonacciSearch.php rename to Searches/FibonacciSearch.php diff --git a/Searches/LowerBound.php b/Searches/LowerBound.php index b3dfd5c..cf46555 100644 --- a/Searches/LowerBound.php +++ b/Searches/LowerBound.php @@ -1,6 +1,7 @@ assertEquals('Aopz pz h alza.', encrypt('This is a test.', 7)); + $this->assertEquals('Aopz pz h alza.', encrypt('This is a test.', 7 + 26)); + $this->assertEquals('This is a test.', decrypt('Aopz pz h alza.', 7)); + $this->assertEquals('This is a test.', decrypt('Aopz pz h alza.', 7 + 26)); } public function testXorCipher() @@ -22,7 +22,7 @@ class CiphersTest extends TestCase $input_str = "test@string"; $key = "test-key"; $invalid_key = "wrong-key"; - assertEquals( $input_str, xorCipher( xorCipher( $input_str , $key) , $key)); - assertNotEquals( $input_str, xorCipher( xorCipher( $input_str , $key) , $invalid_key)); + $this->assertEquals( $input_str, xorCipher( xorCipher( $input_str , $key) , $key)); + $this->assertNotEquals( $input_str, xorCipher( xorCipher( $input_str , $key) , $invalid_key)); } } diff --git a/tests/Conversions/ConversionsTest.php b/tests/Conversions/ConversionsTest.php index fa80c07..d888f5c 100644 --- a/tests/Conversions/ConversionsTest.php +++ b/tests/Conversions/ConversionsTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; require_once __DIR__ . '/../../vendor/autoload.php'; require_once __DIR__ . '/../../Conversions/BinaryToDecimal.php'; +require_once __DIR__ . '/../../Conversions/DecimalToBinary.php'; require_once __DIR__ . '/../../Conversions/OctalToDecimal.php'; require_once __DIR__ . '/../../Conversions/HexadecimalToDecimal.php'; diff --git a/tests/Maths/MathsTest.php b/tests/Maths/MathsTest.php index 3dbe3d7..87f7572 100644 --- a/tests/Maths/MathsTest.php +++ b/tests/Maths/MathsTest.php @@ -54,10 +54,10 @@ class MathTest extends TestCase public function testPerfectSquare() { - assertTrue(is_perfect_square(25)); - assertFalse(is_perfect_square(43)); - assertFalse(is_perfect_square(2)); - assertTrue(is_perfect_square(225)); + assertTrue(isPerfectSquare(25)); + assertFalse(isPerfectSquare(43)); + assertFalse(isPerfectSquare(2)); + assertTrue(isPerfectSquare(225)); } public function testFastExponentiation() @@ -80,10 +80,10 @@ class MathTest extends TestCase public function testFibonacciGenerator() { - assertEquals([0, 1, 1, 2, 3], loop(5, fib())); - assertEquals([0, 1, 1, 2, 3, 5, 8, 13, 21, 34], loop(10, fib())); + assertEquals([0, 1, 1, 2, 3], iterator_to_array(loop(5, fib()))); + assertEquals([0, 1, 1, 2, 3, 5, 8, 13, 21, 34], iterator_to_array(loop(10, fib()))); - assertEquals([0, 1, 1, 2, 3], loop(5, loop())); - assertEquals([0, 1, 1, 2, 3, 5, 8, 13, 21, 34], loop(10, fib())); + assertEquals([0, 1, 1, 2, 3], iterator_to_array(loop(5, fib()))); + assertEquals([0, 1, 1, 2, 3, 5, 8, 13, 21, 34], iterator_to_array(loop(10, fib()))); } } diff --git a/tests/Searches/BinarySearchTest.php b/tests/Searches/BinarySearchTest.php deleted file mode 100644 index e69de29..0000000 diff --git a/tests/Searches/SearchesTest.php b/tests/Searches/SearchesTest.php index 339f096..7eed4bf 100644 --- a/tests/Searches/SearchesTest.php +++ b/tests/Searches/SearchesTest.php @@ -9,6 +9,8 @@ require_once __DIR__ . '/../../vendor/autoload.php'; require_once __DIR__ . '/../../Searches/BinarySearch.php'; require_once __DIR__ . '/../../Searches/FibonacciSearch.php'; require_once __DIR__ . '/../../Searches/LinearSearch.php'; +require_once __DIR__ . '/../../Searches/LowerBound.php'; +require_once __DIR__ . '/../../Searches/UpperBound.php'; class SearchesTest extends TestCase { @@ -58,10 +60,10 @@ class SearchesTest extends TestCase { $list = [0]; $target = 0; - $result = binarySearchByRecursion($list, $target, 0, 1); + $result = binarySearchByRecursion($list, $target, 0, 0); assertEquals(0, $result); $target = 1; - $result = binarySearchByRecursion($list, $target, 0, 1); + $result = binarySearchByRecursion($list, $target, 0, 0); assertEquals(null, $result); } @@ -69,13 +71,13 @@ class SearchesTest extends TestCase { $list = [0, 1]; $target = 0; - $result = binarySearchByRecursion($list, $target, 0, 2); + $result = binarySearchByRecursion($list, $target, 0, 1); assertEquals(0, $result); $target = 1; - $result = binarySearchByRecursion($list, $target, 0, 2); + $result = binarySearchByRecursion($list, $target, 0, 1); assertEquals(1, $result); $target = 2; - $result = binarySearchByRecursion($list, $target, 0, 2); + $result = binarySearchByRecursion($list, $target, 0, 1); assertEquals(null, $result); } @@ -83,16 +85,16 @@ class SearchesTest extends TestCase { $list = [0, 1, 2]; $target = 0; - $result = binarySearchByRecursion($list, $target, 0, 3); + $result = binarySearchByRecursion($list, $target, 0, 2); assertEquals(0, $result); $target = 1; - $result = binarySearchByRecursion($list, $target, 0, 3); + $result = binarySearchByRecursion($list, $target, 0, 2); assertEquals(1, $result); $target = 2; - $result = binarySearchByRecursion($list, $target, 0, 3); + $result = binarySearchByRecursion($list, $target, 0, 2); assertEquals(2, $result); $target = 3; - $result = binarySearchByRecursion($list, $target, 0, 3); + $result = binarySearchByRecursion($list, $target, 0, 2); assertEquals(null, $result); } diff --git a/tests/Sorting/SortingTests.php b/tests/Sorting/SortingTests.php index 4e360cb..16d0e09 100644 --- a/tests/Sorting/SortingTests.php +++ b/tests/Sorting/SortingTests.php @@ -143,4 +143,33 @@ class SortingTests extends TestCase $end = microtime(true); $this->assertLessThan(1, $end - $start); } + + public function testCountSortCipher() + { + $firstArray = array(20, 16, -5, -8, 6, 12, 2, 4, -3, 9); + $expectedResultOne = array(-8, -5, -3, 2, 4, 6, 9, 12, 16, 20); + $secondArray = array(-6, 12, 14, 17, 5, 4, -9, 15, 0, -8); + $expectedResultTwo = array(-9, -8, -6, 0, 4, 5, 12, 14, 15, 17); + + $resultOne = countSort($firstArray, $minRange = -10, $maxRange = 20); + $resultTwo = countSort($secondArray, $minRange = -10, $maxRange = 20); + + $this->assertEquals($expectedResultOne, $resultOne); + $this->assertEquals($expectedResultTwo, $resultTwo); + } + + public function testQuickSortCipher() + { + $array1 = [20, 16, -5, -8, 6, 12, 2, 4, -3, 9]; + $array2 = [-6, 12, 14, 17, 5, 4, -9, 15, 0, -8]; + + $result1 = [-8, -5, -3, 2, 4, 6, 9, 12, 16, 20]; + $result2 = [-9, -8, -6, 0, 4, 5, 12, 14, 15, 17]; + + $test1 = quickSort($array1); + $test2 = quickSort($array2); + + $this->assertEquals($result1, $test1); + $this->assertEquals($result2, $test2); + } } diff --git a/tests/Strings/StringsTest.php b/tests/Strings/StringsTest.php index 9f5618f..f401aa0 100644 --- a/tests/Strings/StringsTest.php +++ b/tests/Strings/StringsTest.php @@ -6,16 +6,18 @@ use function PHPUnit\Framework\assertNotEquals; use function PHPUnit\Framework\assertTrue; use PHPUnit\Framework\TestCase; -require_once __DIR__ . '/../vendor/autoload.php'; +require_once __DIR__ . '/../../vendor/autoload.php'; -require_once __DIR__ . '/../String/CheckPalindrome.php'; -require_once __DIR__ . '/../String/ReverseString.php'; -require_once __DIR__ . '/../String/ReverseWords.php'; -require_once __DIR__ . '/../String/CheckAnagram.php'; -require_once __DIR__ . '/../String/MaxCharacter.php'; -require_once __DIR__ . '/../String/CountVowels.php'; -require_once __DIR__ . '/../String/CountConsonants.php'; -require_once __DIR__ . '/../String/Distance.php'; +require_once __DIR__ . '/../../Strings/CheckAnagram.php'; +require_once __DIR__ . '/../../Strings/CheckPalindrome.php'; +require_once __DIR__ . '/../../Strings/CheckPalindrome2.php'; +require_once __DIR__ . '/../../Strings/CountConsonants.php'; +require_once __DIR__ . '/../../Strings/CountSentences.php'; +require_once __DIR__ . '/../../Strings/CountVowels.php'; +require_once __DIR__ . '/../../Strings/Distance.php'; +require_once __DIR__ . '/../../Strings/MaxCharacter.php'; +require_once __DIR__ . '/../../Strings/ReverseString.php'; +require_once __DIR__ . '/../../Strings/ReverseWords.php'; class StringTest extends TestCase { @@ -28,15 +30,6 @@ class StringTest extends TestCase assertFalse(isPalindrome('Sator Arepo Tenet Opera Rotas', false)); // false since we are doing case-sensitive check } - public function testIsPalindrome2() - { - assertTrue(checkPalindromeString('131')); // true - assertFalse(checkPalindromeString('roman')); // false - assertTrue(checkPalindromeString('Level', true)); // true - assertFalse(checkPalindromeString('Level', false)); // false - assertFalse(checkPalindromeString('palindrome')); // false - } - public function testCountSentences() { assertEquals(countSentences('Hi! Hello world.'), 2);