Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created tests for the existing sort modules #29

Merged
merged 40 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
40 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
86220c9
Implementing ctest friendly tests
Ramy-Badr-Ahmed Oct 9, 2024
3ee89ba
Implementing ctest-friendly tests for /sorts
Ramy-Badr-Ahmed Oct 9, 2024
0bcd2a4
minor comment edit
Ramy-Badr-Ahmed Oct 9, 2024
5f45884
File renaming
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
107 changes: 107 additions & 0 deletions tests/sorts/tests_bubble_sort.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
!> Test program for the Bubble Sort algorithm
!!
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #27
!! https://github.com/TheAlgorithms/Fortran/pull/27
!!
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
!! addressing bugs/corrections to this file. Thank you!
!!
!! This program tests the bubble_sort_module for correct sorting behavior.

program tests_bubble_sort
use bubble_sort_module
implicit none
real, dimension(:), allocatable :: array, expected

! Run test cases
call test_sorted_array()
call test_reverse_sorted_array()
call test_unsorted_array()
call test_array_with_repeated_elements()
call test_array_with_identical_elements()
call test_single_element_array()
call test_empty_array()

print *, "All tests completed."

contains

! Test case 1: Already sorted array
subroutine test_sorted_array()
array = (/1.0, 2.0, 3.0, 4.0, 5.0/)
expected = array
call run_test(array, expected, "Test 1: Already sorted array")
end subroutine test_sorted_array

! Test case 2: Reverse sorted array
subroutine test_reverse_sorted_array()
array = (/5.0, 4.0, 3.0, 2.0, 1.0/)
expected = (/1.0, 2.0, 3.0, 4.0, 5.0/)
call run_test(array, expected, "Test 2: Reverse sorted array")
end subroutine test_reverse_sorted_array

! Test case 3: Unsorted array
subroutine test_unsorted_array()
array = (/3.5, 1.2, 4.8, 2.7, 5.0/)
expected = (/1.2, 2.7, 3.5, 4.8, 5.0/)
call run_test(array, expected, "Test 3: Unsorted array")
end subroutine test_unsorted_array

! Test case 4: Array with repeated elements
subroutine test_array_with_repeated_elements()
array = (/3.0, 1.0, 2.0, 3.0, 4.0, 3.0/)
expected = (/1.0, 2.0, 3.0, 3.0, 3.0, 4.0/)
call run_test(array, expected, "Test 4: Array with repeated elements")
end subroutine test_array_with_repeated_elements

! Test case 5: Array with identical elements
subroutine test_array_with_identical_elements()
array = (/7.0, 7.0, 7.0, 7.0, 7.0/)
expected = array
call run_test(array, expected, "Test 5: Array with identical elements")
end subroutine test_array_with_identical_elements

! Test case 6: Single element array
subroutine test_single_element_array()
array = (/42.0/)
expected = array
call run_test(array, expected, "Test 6: Single element array")
end subroutine test_single_element_array

! Test case 7: Empty array
subroutine test_empty_array()
if (allocated(array)) deallocate (array)
if (allocated(expected)) deallocate (expected)
allocate (array(0))
allocate (expected(0))
call run_test(array, expected, "Test 7: Empty array")
end subroutine test_empty_array

!> Subroutine to run the bubble sort test
subroutine run_test(array, expected, test_name)
real, dimension(:), intent(inout) :: array
real, dimension(:), intent(in) :: expected
character(len=*), intent(in) :: test_name
real :: tolerance

! Call bubble_sort in module
call bubble_sort(array)

! Set an appropriate tolerance value
tolerance = 1.0e-6

! Assert if the sorted values are sufficiently close to the expected array otherwise report failure
if (all(abs(array - expected) < tolerance)) then
print *, test_name, " PASSED"
else
print *, test_name, " FAILED"
print *, "Expected: ", expected
print *, "Got: ", array
stop 1
end if

end subroutine run_test

end program tests_bubble_sort

159 changes: 86 additions & 73 deletions tests/sorts/tests_gnome_sort.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
!> Test program for the Gnome Sort algorithm
!!
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #09
!! in Pull Request: #9
!! https://github.com/TheAlgorithms/Fortran/pull/9
!!
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
Expand All @@ -13,86 +13,99 @@ program tests_gnome_sort

use gnome_sort_module
implicit none
integer, dimension(:), allocatable :: array

! Test 1: Repeated elements
print *, "Test 1: Array with repeated elements"
array = (/5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1/)
call run_test(array)

! Test 2: Already sorted array
print *, "Test 2: Already sorted array"
if (allocated(array)) deallocate (array)
allocate (array(8))
array = (/1, 2, 3, 4, 5, 6, 7, 8/)
call run_test(array)

! Test 3: Reverse sorted array
print *, "Test 3: Reverse sorted array"
if (allocated(array)) deallocate (array)
allocate (array(8))
array = (/8, 7, 6, 5, 4, 3, 2, 1/)
call run_test(array)

! Test 4: Array with all negative numbers
print *, "Test 4: Array with all negative numbers"
if (allocated(array)) deallocate (array)
allocate (array(8))
array = (/-1, -5, -3, -7, -2, -12, -15, -4/)
call run_test(array)

! Test 5: Single element array
print *, "Test 5: Single element array"
if (allocated(array)) deallocate (array)
allocate (array(1))
array = (/42/)
call run_test(array)

! Test 6: Array with identical elements
print *, "Test 6: Array with identical elements"
if (allocated(array)) deallocate (array)
allocate (array(5))
array = (/7, 7, 7, 7, 7/)
call run_test(array)

! Test 7: Array with alternating high and low values
print *, "Test 7: Array with alternating high and low values"
if (allocated(array)) deallocate (array)
allocate (array(6))
array = (/1, 1000, 2, 999, 3, 998/)
call run_test(array)

! Test 8: Empty array
print *, "Test 8: Empty array"
if (allocated(array)) deallocate (array)
allocate (array(0))
call run_test(array)
integer, dimension(:), allocatable :: array, expected

contains
! Run test cases
call test_repeated_elements()
call test_already_sorted()
call test_reverse_sorted()
call test_negative_numbers()
call test_single_element()
call test_identical_elements()
call test_alternating_values()
call test_empty_array()

!> Subroutine to run and print the gnome sort test
subroutine run_test(array)
integer, dimension(:), intent(inout) :: array
integer :: n, i
print *, "All tests completed."

n = size(array)
contains

! Print original array
print *, "Original array:"
do i = 1, n
print *, array(i)
end do
! Test case 1: Array with repeated elements
subroutine test_repeated_elements()
array = (/5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1/)
expected = (/1, 1, 3, 3, 3, 5, 5, 5, 7, 7, 8, 10/)
call run_test(array, expected, "Test 1: Array with repeated elements")
end subroutine test_repeated_elements

! Test case 2: Already sorted array
subroutine test_already_sorted()
array = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13/)
expected = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13/)
call run_test(array, expected, "Test 2: Already sorted array")
end subroutine test_already_sorted

! Test case 3: Reverse sorted array
subroutine test_reverse_sorted()
array = (/11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1/)
expected = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11/)
call run_test(array, expected, "Test 3: Reverse sorted array")
end subroutine test_reverse_sorted

! Test case 4: Array with all negative numbers
subroutine test_negative_numbers()
array = (/-1, -5, -4, -7, -2, -1, -1, -9, -2/)
expected = (/-9, -7, -5, -4, -2, -2, -1, -1, -1/)
call run_test(array, expected, "Test 4: Array with all negative numbers")
end subroutine test_negative_numbers

! Test case 5: Single element array
subroutine test_single_element()
array = (/73/)
expected = (/73/)
call run_test(array, expected, "Test 5: Single element array")
end subroutine test_single_element

! Test case 6: Array with identical elements
subroutine test_identical_elements()
array = (/8, 8, 8, 8, 8/)
expected = (/8, 8, 8, 8, 8/)
call run_test(array, expected, "Test 6: Array with identical elements")
end subroutine test_identical_elements

! Test case 7: Array with alternating high and low values
subroutine test_alternating_values()
array = (/1, 999, 2, 600, 3, 950/)
expected = (/1, 2, 3, 600, 950, 999/)
call run_test(array, expected, "Test 7: Array with alternating high and low values")
end subroutine test_alternating_values

! Test case 8: Empty array
subroutine test_empty_array()
if (allocated(array)) deallocate (array)
if (allocated(expected)) deallocate (expected)
allocate (array(0))
allocate (expected(0))
call run_test(array, expected, "Test 8: Empty array")
end subroutine test_empty_array

!> Subroutine to run the heap sort test
subroutine run_test(array, expected, test_name)
integer, dimension(:), intent(inout) :: array
integer, dimension(:), intent(in) :: expected
character(len=*), intent(in) :: test_name

! Call gnome_sort
! Call gnome_sort in module
call gnome_sort(array)

! Print sorted array
print *, "Sorted array:"
do i = 1, n
print *, array(i)
end do
! Assert that the sorted array matches the expected array otherwise report failure for ctest
if (all(array == expected)) then
print *, test_name, " PASSED"
else
print *, test_name, " FAILED"
print *, "Expected: ", expected
print *, "Got: ", array
stop 1
end if

print *, ""
end subroutine run_test

end program tests_gnome_sort
Loading