Skip to content

Commit 5fb71ff

Browse files
Merge branch 'TheAlgorithms:main' into tests/numerical_integration_modules
2 parents 3782898 + 35b2be4 commit 5fb71ff

17 files changed

+1260
-517
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141

4242
- name: Test
4343
working-directory: ${{env.build_path}}
44-
run: ctest
44+
run: ctest --output-on-failure
4545

4646
- name: Run examples
4747
working-directory: ${{env.build_path}}

tests/searches/linear_search.f90

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
!> Test program for the Linear Search algorithm
2+
!!
3+
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
4+
!! in Pull Request: #30
5+
!! https://github.com/TheAlgorithms/Fortran/pull/30
6+
!!
7+
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
8+
!! addressing bugs/corrections to this file. Thank you!
9+
!!
10+
!! This program provides additional test cases to validate the linear_search_module.
11+
12+
program tests_linear_search
13+
use linear_search_module
14+
implicit none
15+
integer, dimension(:), allocatable :: array
16+
integer :: target, index, expected
17+
18+
! Run test cases
19+
call test_found()
20+
call test_not_found()
21+
call test_first_element()
22+
call test_last_element()
23+
call test_multiple_occurrences()
24+
call test_single_element_found()
25+
call test_single_element_not_found()
26+
call test_empty_array()
27+
28+
print *, "All tests completed."
29+
30+
contains
31+
32+
! Test case 1: Target is found in the array
33+
subroutine test_found()
34+
array = (/30, 10, 20, 40, 55, 61, 72, 86, 97, 101/)
35+
target = 97
36+
expected = 9
37+
index = linear_search(array, target)
38+
call assert_test(index, expected, "Test 1: Target found in the array")
39+
end subroutine test_found
40+
41+
! Test case 2: Target is not found in the array
42+
subroutine test_not_found()
43+
array = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11/)
44+
target = 66
45+
expected = -1
46+
index = linear_search(array, target)
47+
call assert_test(index, expected, "Test 2: Target not found in the array")
48+
end subroutine test_not_found
49+
50+
! Test case 3: Target is the first element
51+
subroutine test_first_element()
52+
array = (/10, 20, 30, 40, 50/)
53+
target = array(1)
54+
expected = 1
55+
index = linear_search(array, target)
56+
call assert_test(index, expected, "Test 3: Target is the first element")
57+
end subroutine test_first_element
58+
59+
! Test case 4: Target is the last element
60+
subroutine test_last_element()
61+
array = (/10, 20, 30, 40, 50, 60, 70, 80/)
62+
target = array(size(array))
63+
expected = size(array)
64+
index = linear_search(array, target)
65+
call assert_test(index, expected, "Test 4: Target is the last element")
66+
end subroutine test_last_element
67+
68+
! Test case 5: Multiple occurrences of the target
69+
subroutine test_multiple_occurrences()
70+
array = (/1, 2, 3, 2, 4, 2, 5, 2, 4/)
71+
target = 4
72+
expected = 5
73+
index = linear_search(array, target)
74+
call assert_test(index, expected, "Test 5: Target has multiple occurrences (first found)")
75+
end subroutine test_multiple_occurrences
76+
77+
! Test case 6: Single element found
78+
subroutine test_single_element_found()
79+
array = (/42/)
80+
target = 42
81+
expected = 1
82+
index = linear_search(array, target)
83+
call assert_test(index, expected, "Test 6: Single element found")
84+
end subroutine test_single_element_found
85+
86+
! Test case 7: Single element not found
87+
subroutine test_single_element_not_found()
88+
array = (/42/)
89+
target = 99
90+
expected = -1
91+
index = linear_search(array, target)
92+
call assert_test(index, expected, "Test 7: Single element not found")
93+
end subroutine test_single_element_not_found
94+
95+
! Test case 8: Empty array
96+
subroutine test_empty_array()
97+
if (allocated(array)) deallocate (array)
98+
allocate (array(0)) ! Empty array
99+
target = 1
100+
expected = -1
101+
index = linear_search(array, target)
102+
call assert_test(index, expected, "Test 8: Search in an empty array")
103+
end subroutine test_empty_array
104+
105+
!> Subroutine to assert the test results
106+
subroutine assert_test(actual, expected, test_name)
107+
integer, intent(in) :: actual, expected
108+
character(len=*), intent(in) :: test_name
109+
110+
if (actual == expected) then
111+
print *, test_name, " PASSED"
112+
else
113+
print *, test_name, " FAILED"
114+
print *, "Expected: ", expected
115+
print *, "Got: ", actual
116+
stop 1
117+
end if
118+
119+
end subroutine assert_test
120+
121+
end program tests_linear_search
+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
!> Test program for the Recursive Linear Search algorithm
2+
!!
3+
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
4+
!! in Pull Request: #30
5+
!! https://github.com/TheAlgorithms/Fortran/pull/30
6+
!!
7+
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
8+
!! addressing bugs/corrections to this file. Thank you!
9+
!!
10+
!! This program provides additional test cases to validate the recursive_linear_search_module.
11+
12+
program tests_recursive_linear_search
13+
use linear_search_module
14+
implicit none
15+
integer, dimension(:), allocatable :: array
16+
integer :: target, index, expected
17+
18+
! Run test cases
19+
call test_found()
20+
call test_not_found()
21+
call test_first_element()
22+
call test_last_element()
23+
call test_multiple_occurrences()
24+
call test_single_element_found()
25+
call test_single_element_not_found()
26+
call test_empty_array()
27+
28+
print *, "All tests completed."
29+
30+
contains
31+
32+
! Test case 1: Target is found in the array
33+
subroutine test_found()
34+
array = (/30, 10, 20, 40, 55, 61, 72, 86, 97, 101/)
35+
target = 97
36+
expected = 9
37+
index = linear_search(array, target)
38+
call assert_test(index, expected, "Test 1: Target found in the array")
39+
end subroutine test_found
40+
41+
! Test case 2: Target is not found in the array
42+
subroutine test_not_found()
43+
array = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11/)
44+
target = 66
45+
expected = -1
46+
index = linear_search(array, target)
47+
call assert_test(index, expected, "Test 2: Target not found in the array")
48+
end subroutine test_not_found
49+
50+
! Test case 3: Target is the first element
51+
subroutine test_first_element()
52+
array = (/10, 20, 30, 40, 50/)
53+
target = array(1)
54+
expected = 1
55+
index = linear_search(array, target)
56+
call assert_test(index, expected, "Test 3: Target is the first element")
57+
end subroutine test_first_element
58+
59+
! Test case 4: Target is the last element
60+
subroutine test_last_element()
61+
array = (/10, 20, 30, 40, 50, 60, 70, 80/)
62+
target = array(size(array))
63+
expected = size(array)
64+
index = linear_search(array, target)
65+
call assert_test(index, expected, "Test 4: Target is the last element")
66+
end subroutine test_last_element
67+
68+
! Test case 5: Multiple occurrences of the target
69+
subroutine test_multiple_occurrences()
70+
array = (/1, 2, 3, 2, 4, 2, 5, 2, 4/)
71+
target = 4
72+
expected = 5
73+
index = linear_search(array, target)
74+
call assert_test(index, expected, "Test 5: Target has multiple occurrences (first found)")
75+
end subroutine test_multiple_occurrences
76+
77+
! Test case 6: Single element found
78+
subroutine test_single_element_found()
79+
array = (/42/)
80+
target = 42
81+
expected = 1
82+
index = linear_search(array, target)
83+
call assert_test(index, expected, "Test 6: Single element found")
84+
end subroutine test_single_element_found
85+
86+
! Test case 7: Single element not found
87+
subroutine test_single_element_not_found()
88+
array = (/42/)
89+
target = 99
90+
expected = -1
91+
index = linear_search(array, target)
92+
call assert_test(index, expected, "Test 7: Single element not found")
93+
end subroutine test_single_element_not_found
94+
95+
! Test case 8: Empty array
96+
subroutine test_empty_array()
97+
if (allocated(array)) deallocate (array)
98+
allocate (array(0)) ! Empty array
99+
target = 1
100+
expected = -1
101+
index = linear_search(array, target)
102+
call assert_test(index, expected, "Test 8: Search in an empty array")
103+
end subroutine test_empty_array
104+
105+
!> Subroutine to assert the test results
106+
subroutine assert_test(actual, expected, test_name)
107+
integer, intent(in) :: actual, expected
108+
character(len=*), intent(in) :: test_name
109+
110+
if (actual == expected) then
111+
print *, test_name, " PASSED"
112+
else
113+
print *, test_name, " FAILED"
114+
print *, "Expected: ", expected
115+
print *, "Got: ", actual
116+
stop 1
117+
end if
118+
119+
end subroutine assert_test
120+
121+
end program tests_recursive_linear_search
+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
!> Test program for the Array-Based Ternary Search algorithm
2+
!!
3+
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
4+
!! in Pull Request: #30
5+
!! https://github.com/TheAlgorithms/Fortran/pull/30
6+
!!
7+
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
8+
!! addressing bugs/corrections to this file. Thank you!
9+
!!
10+
!! This program provides additional test cases to validate the array-based ternary_search module.
11+
12+
program tests_ternary_search_array
13+
use ternary_search
14+
implicit none
15+
integer, dimension(:), allocatable :: sorted_array
16+
integer :: target, index, expected
17+
18+
! Run test cases
19+
call test_found()
20+
call test_not_found()
21+
call test_first_element()
22+
call test_last_element()
23+
call test_multiple_occurrences()
24+
call test_single_element_found()
25+
call test_single_element_not_found()
26+
call test_empty_array()
27+
28+
print *, "All tests completed."
29+
30+
contains
31+
32+
! Test case 1: Target found
33+
subroutine test_found()
34+
sorted_array = (/1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25/)
35+
target = 21
36+
expected = 11
37+
index = ternary_search_array(sorted_array, target, 1, size(sorted_array))
38+
call assert_test(index, expected, "Test 1: Target found in the array")
39+
end subroutine test_found
40+
41+
! Test case 2: Target not found
42+
subroutine test_not_found()
43+
sorted_array = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12/)
44+
target = 110
45+
expected = -1
46+
index = ternary_search_array(sorted_array, target, 1, size(sorted_array))
47+
call assert_test(index, -1, "Test 2: Target not found in the array")
48+
end subroutine test_not_found
49+
50+
! Test case 3: Target is the first element
51+
subroutine test_first_element()
52+
sorted_array = (/10, 20, 30, 40, 50, 60, 70, 80/)
53+
target = sorted_array(1)
54+
expected = 1
55+
index = ternary_search_array(sorted_array, target, 1, size(sorted_array))
56+
call assert_test(index, expected, "Test 3: Target is the first element")
57+
end subroutine test_first_element
58+
59+
! Test case 4: Target is the last element
60+
subroutine test_last_element()
61+
sorted_array = (/100, 200, 300, 400, 500, 600, 700, 800, 900/)
62+
target = sorted_array(size(sorted_array))
63+
expected = size(sorted_array)
64+
index = ternary_search_array(sorted_array, target, 1, size(sorted_array))
65+
call assert_test(index, expected, "Test 4: Target is the last element")
66+
end subroutine test_last_element
67+
68+
! Test case 5: Multiple occurrences of the target
69+
subroutine test_multiple_occurrences()
70+
sorted_array = (/1, 1, 2, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11, 12, 12/)
71+
target = 12
72+
expected = 16
73+
index = ternary_search_array(sorted_array, target, 1, size(sorted_array))
74+
call assert_test(index, expected, "Test 5: Target has multiple occurrences (first found)")
75+
end subroutine test_multiple_occurrences
76+
77+
! Test case 6: Single element found
78+
subroutine test_single_element_found()
79+
sorted_array = (/59/)
80+
target = 59
81+
expected = 1
82+
index = ternary_search_array(sorted_array, target, 1, size(sorted_array))
83+
call assert_test(index, expected, "Test 6: Single element found")
84+
end subroutine test_single_element_found
85+
86+
! Test case 7: Single element not found
87+
subroutine test_single_element_not_found()
88+
sorted_array = (/42/)
89+
target = 99
90+
expected = -1
91+
index = ternary_search_array(sorted_array, target, 1, size(sorted_array))
92+
call assert_test(index, expected, "Test 7: Single element not found")
93+
end subroutine test_single_element_not_found
94+
95+
! Test case 8: Empty array
96+
subroutine test_empty_array()
97+
if (allocated(sorted_array)) deallocate (sorted_array)
98+
allocate (sorted_array(0)) ! Empty array
99+
target = 1
100+
expected = -1
101+
index = ternary_search_array(sorted_array, target, 1, size(sorted_array))
102+
call assert_test(index, expected, "Test 8: Search in an empty array")
103+
end subroutine test_empty_array
104+
105+
!> Subroutine to assert the test results
106+
subroutine assert_test(actual, expected, test_name)
107+
integer, intent(in) :: actual, expected
108+
character(len=*), intent(in) :: test_name
109+
110+
if (actual == expected) then
111+
print *, test_name, " PASSED"
112+
else
113+
print *, test_name, " FAILED"
114+
print *, "Expected: ", expected
115+
print *, "Got: ", actual
116+
stop 1
117+
end if
118+
119+
end subroutine assert_test
120+
121+
end program tests_ternary_search_array

0 commit comments

Comments
 (0)