diff --git a/tests/searches/linear_search.f90 b/tests/searches/linear_search.f90 new file mode 100644 index 0000000..8d23203 --- /dev/null +++ b/tests/searches/linear_search.f90 @@ -0,0 +1,122 @@ +!> Test program for the Linear Search algorithm +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #30 +!! https://github.com/TheAlgorithms/Fortran/pull/30 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! +!! This program provides additional test cases to validate the linear_search_module. + + +program tests_linear_search + use linear_search_module + implicit none + integer, dimension(:), allocatable :: array + integer :: target, index, expected + + ! Run test cases + call test_found() + call test_not_found() + call test_first_element() + call test_last_element() + call test_multiple_occurrences() + call test_single_element_found() + call test_single_element_not_found() + call test_empty_array() + + print *, "All tests completed." + +contains + + ! Test case 1: Target is found in the array + subroutine test_found() + array = (/30, 10, 20, 40, 55, 61, 72, 86, 97, 101/) + target = 97 + expected = 9 + index = linear_search(array, target) + call assert_test(index, expected, "Test 1: Target found in the array") + end subroutine test_found + + ! Test case 2: Target is not found in the array + subroutine test_not_found() + array = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11/) + target = 66 + expected = -1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 2: Target not found in the array") + end subroutine test_not_found + + ! Test case 3: Target is the first element + subroutine test_first_element() + array = (/10, 20, 30, 40, 50/) + target = array(1) + expected = 1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 3: Target is the first element") + end subroutine test_first_element + + ! Test case 4: Target is the last element + subroutine test_last_element() + array = (/10, 20, 30, 40, 50, 60, 70, 80/) + target = array(size(array)) + expected = size(array) + index = linear_search(array, target) + call assert_test(index, expected, "Test 4: Target is the last element") + end subroutine test_last_element + + ! Test case 5: Multiple occurrences of the target + subroutine test_multiple_occurrences() + array = (/1, 2, 3, 2, 4, 2, 5, 2, 4/) + target = 4 + expected = 5 + index = linear_search(array, target) + call assert_test(index, expected, "Test 5: Target has multiple occurrences (first found)") + end subroutine test_multiple_occurrences + + ! Test case 6: Single element found + subroutine test_single_element_found() + array = (/42/) + target = 42 + expected = 1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 6: Single element found") + end subroutine test_single_element_found + + ! Test case 7: Single element not found + subroutine test_single_element_not_found() + array = (/42/) + target = 99 + expected = -1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 7: Single element not found") + end subroutine test_single_element_not_found + + ! Test case 8: Empty array + subroutine test_empty_array() + if (allocated(array)) deallocate (array) + allocate (array(0)) ! Empty array + target = 1 + expected = -1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 8: Search in an empty array") + end subroutine test_empty_array + + !> Subroutine to assert the test results + subroutine assert_test(actual, expected, test_name) + integer, intent(in) :: actual, expected + character(len=*), intent(in) :: test_name + + if (actual == expected) then + print *, test_name, " PASSED" + else + print *, test_name, " FAILED" + print *, "Expected: ", expected + print *, "Got: ", actual + stop 1 + end if + + end subroutine assert_test + +end program tests_linear_search diff --git a/tests/searches/recursive_linear_search.f90 b/tests/searches/recursive_linear_search.f90 new file mode 100644 index 0000000..d264e03 --- /dev/null +++ b/tests/searches/recursive_linear_search.f90 @@ -0,0 +1,122 @@ +!> Test program for the Recursive Linear Search algorithm +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #30 +!! https://github.com/TheAlgorithms/Fortran/pull/30 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! +!! This program provides additional test cases to validate the recursive_linear_search_module. + + +program tests_recursive_linear_search + use linear_search_module + implicit none + integer, dimension(:), allocatable :: array + integer :: target, index, expected + + ! Run test cases + call test_found() + call test_not_found() + call test_first_element() + call test_last_element() + call test_multiple_occurrences() + call test_single_element_found() + call test_single_element_not_found() + call test_empty_array() + + print *, "All tests completed." + +contains + + ! Test case 1: Target is found in the array + subroutine test_found() + array = (/30, 10, 20, 40, 55, 61, 72, 86, 97, 101/) + target = 97 + expected = 9 + index = linear_search(array, target) + call assert_test(index, expected, "Test 1: Target found in the array") + end subroutine test_found + + ! Test case 2: Target is not found in the array + subroutine test_not_found() + array = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11/) + target = 66 + expected = -1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 2: Target not found in the array") + end subroutine test_not_found + + ! Test case 3: Target is the first element + subroutine test_first_element() + array = (/10, 20, 30, 40, 50/) + target = array(1) + expected = 1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 3: Target is the first element") + end subroutine test_first_element + + ! Test case 4: Target is the last element + subroutine test_last_element() + array = (/10, 20, 30, 40, 50, 60, 70, 80/) + target = array(size(array)) + expected = size(array) + index = linear_search(array, target) + call assert_test(index, expected, "Test 4: Target is the last element") + end subroutine test_last_element + + ! Test case 5: Multiple occurrences of the target + subroutine test_multiple_occurrences() + array = (/1, 2, 3, 2, 4, 2, 5, 2, 4/) + target = 4 + expected = 5 + index = linear_search(array, target) + call assert_test(index, expected, "Test 5: Target has multiple occurrences (first found)") + end subroutine test_multiple_occurrences + + ! Test case 6: Single element found + subroutine test_single_element_found() + array = (/42/) + target = 42 + expected = 1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 6: Single element found") + end subroutine test_single_element_found + + ! Test case 7: Single element not found + subroutine test_single_element_not_found() + array = (/42/) + target = 99 + expected = -1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 7: Single element not found") + end subroutine test_single_element_not_found + + ! Test case 8: Empty array + subroutine test_empty_array() + if (allocated(array)) deallocate (array) + allocate (array(0)) ! Empty array + target = 1 + expected = -1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 8: Search in an empty array") + end subroutine test_empty_array + + !> Subroutine to assert the test results + subroutine assert_test(actual, expected, test_name) + integer, intent(in) :: actual, expected + character(len=*), intent(in) :: test_name + + if (actual == expected) then + print *, test_name, " PASSED" + else + print *, test_name, " FAILED" + print *, "Expected: ", expected + print *, "Got: ", actual + stop 1 + end if + + end subroutine assert_test + +end program tests_recursive_linear_search diff --git a/tests/searches/ternary_search_array.f90 b/tests/searches/ternary_search_array.f90 new file mode 100644 index 0000000..b66ac1e --- /dev/null +++ b/tests/searches/ternary_search_array.f90 @@ -0,0 +1,121 @@ +!> Test program for the Array-Based Ternary Search algorithm +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #30 +!! https://github.com/TheAlgorithms/Fortran/pull/30 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! +!! This program provides additional test cases to validate the array-based ternary_search module. + +program tests_ternary_search_array + use ternary_search + implicit none + integer, dimension(:), allocatable :: sorted_array + integer :: target, index, expected + + ! Run test cases + call test_found() + call test_not_found() + call test_first_element() + call test_last_element() + call test_multiple_occurrences() + call test_single_element_found() + call test_single_element_not_found() + call test_empty_array() + + print *, "All tests completed." + +contains + + ! Test case 1: Target found + subroutine test_found() + sorted_array = (/1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25/) + target = 21 + expected = 11 + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, expected, "Test 1: Target found in the array") + end subroutine test_found + + ! Test case 2: Target not found + subroutine test_not_found() + sorted_array = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12/) + target = 110 + expected = -1 + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, -1, "Test 2: Target not found in the array") + end subroutine test_not_found + + ! Test case 3: Target is the first element + subroutine test_first_element() + sorted_array = (/10, 20, 30, 40, 50, 60, 70, 80/) + target = sorted_array(1) + expected = 1 + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, expected, "Test 3: Target is the first element") + end subroutine test_first_element + + ! Test case 4: Target is the last element + subroutine test_last_element() + sorted_array = (/100, 200, 300, 400, 500, 600, 700, 800, 900/) + target = sorted_array(size(sorted_array)) + expected = size(sorted_array) + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, expected, "Test 4: Target is the last element") + end subroutine test_last_element + + ! Test case 5: Multiple occurrences of the target + subroutine test_multiple_occurrences() + sorted_array = (/1, 1, 2, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11, 12, 12/) + target = 12 + expected = 16 + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, expected, "Test 5: Target has multiple occurrences (first found)") + end subroutine test_multiple_occurrences + + ! Test case 6: Single element found + subroutine test_single_element_found() + sorted_array = (/59/) + target = 59 + expected = 1 + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, expected, "Test 6: Single element found") + end subroutine test_single_element_found + + ! Test case 7: Single element not found + subroutine test_single_element_not_found() + sorted_array = (/42/) + target = 99 + expected = -1 + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, expected, "Test 7: Single element not found") + end subroutine test_single_element_not_found + + ! Test case 8: Empty array + subroutine test_empty_array() + if (allocated(sorted_array)) deallocate (sorted_array) + allocate (sorted_array(0)) ! Empty array + target = 1 + expected = -1 + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, expected, "Test 8: Search in an empty array") + end subroutine test_empty_array + + !> Subroutine to assert the test results + subroutine assert_test(actual, expected, test_name) + integer, intent(in) :: actual, expected + character(len=*), intent(in) :: test_name + + if (actual == expected) then + print *, test_name, " PASSED" + else + print *, test_name, " FAILED" + print *, "Expected: ", expected + print *, "Got: ", actual + stop 1 + end if + + end subroutine assert_test + +end program tests_ternary_search_array diff --git a/tests/searches/ternary_search_function.f90 b/tests/searches/ternary_search_function.f90 new file mode 100644 index 0000000..e137b46 --- /dev/null +++ b/tests/searches/ternary_search_function.f90 @@ -0,0 +1,85 @@ +!> Test program for the Function-Based Ternary Search algorithm +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #30 +!! https://github.com/TheAlgorithms/Fortran/pull/30 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! +!! This program provides test cases to validate the function-based ternary search algorithms for known functions. + +program tests_ternary_search_function + use ternary_search + implicit none + real(8) :: tol, left, right, result, expected + + tol = 1.0d-6 + left = -100.0d0 + right = 100.0d0 + + ! Run test cases + call test_find_min_parabola() + call test_find_max_negative_parabola() + call test_find_min_custom_function() + + print *, "All tests completed." + +contains + + ! Test case 1: Find minimum of a parabola (f(x) = x^2) + subroutine test_find_min_parabola() + result = ternary_search_minimum(parabola, left, right, tol) + expected = 0.0d0 + call assert_test(abs(result), expected, "Test 1: Find minimum of f(x) = x^2") + end subroutine test_find_min_parabola + + ! Test case 2: Find maximum of a negative parabola (f(x) = -x^2) + subroutine test_find_max_negative_parabola() + result = ternary_search_maximum(negative_parabola, left, right, tol) + expected = 0.0d0 + call assert_test(abs(result), expected, "Test 2: Find maximum of f(x) = -x^2") + end subroutine test_find_max_negative_parabola + + ! Test case 3: Find minimum of a custom unimodal function + subroutine test_find_min_custom_function() + result = ternary_search_minimum(custom_unimodal_function, left, right, tol) + expected = 50.0d0 + call assert_test(result, 50.0d0, "Test 3: Find minimum of custom unimodal function") + end subroutine test_find_min_custom_function + + !> Subroutine to assert the test results + subroutine assert_test(actual, expected, test_name) + real(8), intent(in) :: actual, expected + character(len=*), intent(in) :: test_name + + if (abs(actual - expected) < tol) then + print *, test_name, " PASSED" + else + print *, test_name, " FAILED" + print *, "Expected: ", expected + print *, "Got: ", actual + stop 1 + end if + + end subroutine assert_test + + ! Parabola function: f(x) = x^2 + real(8) function parabola(x) + real(8), intent(in) :: x + parabola = x**2 + end function parabola + + ! Negative parabola function: f(x) = -x^2 + real(8) function negative_parabola(x) + real(8), intent(in) :: x + negative_parabola = -x**2 + end function negative_parabola + + ! Custom unimodal function: A function with a known minimum at x = 50 + real(8) function custom_unimodal_function(x) + real(8), intent(in) :: x + custom_unimodal_function = (x - 50.0d0)**2 + 100.0d0 + end function custom_unimodal_function + +end program tests_ternary_search_function