Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
ff1618e
Merge branch 'feature/numerical_integration'
Ramy-Badr-Ahmed Sep 24, 2024
2525374
Merge branch 'TheAlgorithms:main' into main
Ramy-Badr-Ahmed Sep 24, 2024
581e311
Merge branch 'feature/ternary_search_implementation'
Ramy-Badr-Ahmed Sep 24, 2024
a609b42
Merge branch 'feature/numerical_integration'
Ramy-Badr-Ahmed Sep 24, 2024
6cb9b33
Create blank.yml
Ramy-Badr-Ahmed Sep 24, 2024
cbb37ec
Update blank.yml
Ramy-Badr-Ahmed Sep 25, 2024
4c2571c
Update blank.yml
Ramy-Badr-Ahmed Sep 25, 2024
bddd048
Update blank.yml
Ramy-Badr-Ahmed Sep 25, 2024
324e2e6
Update blank.yml
Ramy-Badr-Ahmed Sep 25, 2024
c4eb404
Update blank.yml
Ramy-Badr-Ahmed Sep 25, 2024
31b4b5e
Update blank.yml
Ramy-Badr-Ahmed Sep 25, 2024
aa89542
Update blank.yml
Ramy-Badr-Ahmed Sep 25, 2024
53fb8ef
Update blank.yml
Ramy-Badr-Ahmed Sep 25, 2024
61cb114
Update blank.yml
Ramy-Badr-Ahmed Sep 25, 2024
7272baa
Update blank.yml
Ramy-Badr-Ahmed Sep 25, 2024
cdcbe96
Update blank.yml
Ramy-Badr-Ahmed Sep 25, 2024
ffb7b28
Update blank.yml
Ramy-Badr-Ahmed Sep 26, 2024
d2d8561
Update blank.yml
Ramy-Badr-Ahmed Sep 26, 2024
f9e59b0
Update blank.yml
Ramy-Badr-Ahmed Sep 26, 2024
503722c
Update blank.yml
Ramy-Badr-Ahmed Sep 26, 2024
01f8028
Update blank.yml
Ramy-Badr-Ahmed Sep 26, 2024
5c00133
Update blank.yml
Ramy-Badr-Ahmed Sep 26, 2024
18895de
Update blank.yml
Ramy-Badr-Ahmed Sep 26, 2024
ff3d52e
Update blank.yml
Ramy-Badr-Ahmed Sep 26, 2024
2e74cff
Merge branch 'TheAlgorithms:main' into main
Ramy-Badr-Ahmed Sep 27, 2024
d8c0e43
Merge branch 'feature/numerical_integration'
Ramy-Badr-Ahmed Sep 27, 2024
a2e4db3
Merge branch 'feature/ternary_search_implementation'
Ramy-Badr-Ahmed Sep 27, 2024
9158523
Merge branch 'feature/ternary_search_implementation'
Ramy-Badr-Ahmed Sep 27, 2024
fee3677
Merge branch 'feature/numerical_integration'
Ramy-Badr-Ahmed Sep 27, 2024
9b14480
Merge branch 'feature/ternary_search_implementation'
Ramy-Badr-Ahmed Sep 27, 2024
711ff42
Merge branch 'feature/numerical_integration'
Ramy-Badr-Ahmed Sep 28, 2024
a607053
Merge branch 'feature/ternary_search_implementation'
Ramy-Badr-Ahmed Sep 28, 2024
e343f24
Merge branch 'TheAlgorithms:main' into main
Ramy-Badr-Ahmed Sep 30, 2024
7d6bbe7
Merge branch 'feature/numerical_integration'
Ramy-Badr-Ahmed Sep 30, 2024
34b544b
Delete .github/workflows/blank.yml
Ramy-Badr-Ahmed Sep 30, 2024
96909dc
Merge branch 'TheAlgorithms:main' into main
Ramy-Badr-Ahmed Oct 9, 2024
cf268b2
Implementing ctest-friendly tests for /searches modules
Ramy-Badr-Ahmed Oct 9, 2024
76e7c83
code_style fix
Ramy-Badr-Ahmed Oct 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:

- name: Test
working-directory: ${{env.build_path}}
run: ctest
run: ctest --output-on-failure

- name: Run examples
working-directory: ${{env.build_path}}
Expand Down
121 changes: 121 additions & 0 deletions tests/searches/linear_search.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
!> 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
121 changes: 121 additions & 0 deletions tests/searches/recursive_linear_search.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
!> 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
121 changes: 121 additions & 0 deletions tests/searches/ternary_search_array.f90
Original file line number Diff line number Diff line change
@@ -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
Loading