Skip to content

Commit

Permalink
Merge pull request #29 from Ramy-Badr-Ahmed/tests/sort_modules
Browse files Browse the repository at this point in the history
Created tests for the existing `sort` modules
  • Loading branch information
SatinWukerORIG authored Oct 10, 2024
2 parents 056b2d3 + 5f45884 commit 1d80bb9
Show file tree
Hide file tree
Showing 13 changed files with 812 additions and 517 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/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: #29
!! https://github.com/TheAlgorithms/Fortran/pull/29
!!
!! 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

111 changes: 111 additions & 0 deletions tests/sorts/gnome_sort.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
!> Test program for the Gnome Sort algorithm
!!
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #9
!! https://github.com/TheAlgorithms/Fortran/pull/9
!!
!! 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 gnome_sort_module.

program tests_gnome_sort

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

! 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()

print *, "All tests completed."

contains

! 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 in module
call gnome_sort(array)

! 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

end subroutine run_test

end program tests_gnome_sort
114 changes: 114 additions & 0 deletions tests/sorts/heap_sort.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
!> Test program for the Heap Sort algorithm
!!
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #8
!! https://github.com/TheAlgorithms/Fortran/pull/8
!!
!! 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 heap_sort_module.

program tests_heap_sort

use heap_sort_module
implicit none
integer, dimension(:), allocatable :: array, expected

! 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()

print *, "All tests completed."

contains

! 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/)
expected = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10/)
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 = (/9, 8, 7, 6, 5, 4, 3, 2, 1/)
expected = (/1, 2, 3, 4, 5, 6, 7, 8, 9/)
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 = (/-110, -550, -430, -700, -20, -10, -150, -90, -250/)
expected = (/-700, -550, -430, -250, -150, -110, -90, -20, -10/)
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 = (/43/)
expected = (/43/)
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 = (/7, 7, 7, 7, 7/)
expected = (/7, 7, 7, 7, 7/)
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, 1000, 2, 999, 3, 998/)
expected = (/1, 2, 3, 998, 999, 1000/)
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
integer :: n

n = size(array)

! Call heap_sort in module
call heap_sort(array, n)

! 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

end subroutine run_test

end program tests_heap_sort
Loading

0 comments on commit 1d80bb9

Please sign in to comment.