Skip to content

Commit

Permalink
Implementing ctest-friendly tests for /sorts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramy-Badr-Ahmed committed Oct 9, 2024
1 parent 056b2d3 commit d1c3119
Show file tree
Hide file tree
Showing 8 changed files with 662 additions and 367 deletions.
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

0 comments on commit d1c3119

Please sign in to comment.