Skip to content

Commit 4e8de95

Browse files
Merge pull request #31 from Ramy-Badr-Ahmed/tests/math_modules
2 parents 35b2be4 + 5759c95 commit 4e8de95

File tree

5 files changed

+328
-2
lines changed

5 files changed

+328
-2
lines changed

modules/maths/euclid_gcd.f90

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
!> Module implementing the Euclidean Algorithm for GCD
2+
!!
3+
!! Modified by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
4+
!! in Pull Request: #31
5+
!! https://github.com/TheAlgorithms/Fortran/pull/31
6+
!!
7+
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
8+
!! addressing bugs/corrections to this file. Thank you!
9+
!!
210
!! Reference: https://en.wikipedia.org/wiki/Euclidean_algorithm
311

412
module gcd_module
@@ -10,6 +18,10 @@ function gcd(a, b) result(val)
1018
integer, value :: a, b
1119
integer :: t, val
1220

21+
! Ensure the GCD is non-negative
22+
a = abs(a)
23+
b = abs(b)
24+
1325
! Euclidean algorithm for GCD
1426
do while (b /= 0)
1527
t = b

modules/maths/fibonacci.f90

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
!> Module for Fibonacci series calculations
2+
!!
3+
!! Modified by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
4+
!! in Pull Request: #31
5+
!! https://github.com/TheAlgorithms/Fortran/pull/31
6+
!!
7+
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
8+
!! addressing bugs/corrections to this file. Thank you!
9+
!!
210
!! Provides both recursive and iterative implementations.
311
!! Reference: https://en.wikipedia.org/wiki/Fibonacci_number
412

@@ -10,7 +18,9 @@ module fibonacci_module
1018
recursive function fib_rec(n) result(f)
1119
integer, intent(in), value :: n
1220
integer :: f
13-
if (n <= 1) then
21+
if (n < 0) then
22+
f = -1 ! signal error condition
23+
else if (n <= 1) then
1424
f = n
1525
else
1626
f = fib_rec(n - 1) + fib_rec(n - 2)
@@ -22,7 +32,9 @@ function fib_itr(n) result(f)
2232
integer, intent(in) :: n
2333
integer :: f, tmp, f_1
2434
integer :: i
25-
if (n <= 1) then
35+
if (n < 0) then
36+
f = -1 ! signal error condition
37+
else if (n <= 1) then
2638
f = n
2739
else
2840
f_1 = 0

tests/maths/eculid_gcd.f90

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
!> Test program for the GCD module
2+
!!
3+
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
4+
!! in Pull Request: #31
5+
!! https://github.com/TheAlgorithms/Fortran/pull/31
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 test cases to validate the GCD function in the gcd_module.
11+
12+
program tests_gcd_module
13+
use gcd_module
14+
implicit none
15+
integer :: result, expected
16+
17+
! Run test cases
18+
call test_gcd_positive_numbers()
19+
call test_gcd_with_zero()
20+
call test_gcd_negative_numbers()
21+
call test_gcd_same_numbers()
22+
call test_gcd_large_numbers()
23+
24+
print *, "All tests completed."
25+
26+
contains
27+
28+
! Test case 1: GCD of two positive numbers
29+
subroutine test_gcd_positive_numbers()
30+
integer :: a, b
31+
a = 48
32+
b = 18
33+
expected = 6
34+
result = gcd(a, b)
35+
call assert_test(result, expected, "Test 1: GCD of two positive numbers")
36+
end subroutine test_gcd_positive_numbers
37+
38+
! Test case 2: GCD with one number as zero
39+
subroutine test_gcd_with_zero()
40+
integer :: a, b
41+
a = 0
42+
b = 5
43+
expected = 5
44+
result = gcd(a, b)
45+
call assert_test(result, expected, "Test 2: GCD with one number as zero")
46+
end subroutine test_gcd_with_zero
47+
48+
! Test case 3: GCD of two negative numbers
49+
subroutine test_gcd_negative_numbers()
50+
integer :: a, b
51+
a = -48
52+
b = -18
53+
expected = 6
54+
result = gcd(a, b)
55+
call assert_test(result, expected, "Test 3: GCD of two negative numbers")
56+
end subroutine test_gcd_negative_numbers
57+
58+
! Test case 4: GCD of the same number
59+
subroutine test_gcd_same_numbers()
60+
integer :: a, b
61+
a = 42
62+
b = 42
63+
expected = 42
64+
result = gcd(a, b)
65+
call assert_test(result, expected, "Test 4: GCD of the same number")
66+
end subroutine test_gcd_same_numbers
67+
68+
! Test case 5: GCD of large numbers
69+
subroutine test_gcd_large_numbers()
70+
integer :: a, b
71+
a = 123456
72+
b = 789012
73+
expected = 12
74+
result = gcd(a, b)
75+
call assert_test(result, expected, "Test 5: GCD of large numbers")
76+
end subroutine test_gcd_large_numbers
77+
78+
!> Subroutine to assert the test results
79+
subroutine assert_test(actual, expected, test_name)
80+
integer, intent(in) :: actual, expected
81+
character(len=*), intent(in) :: test_name
82+
83+
if (actual == expected) then
84+
print *, test_name, " PASSED"
85+
else
86+
print *, test_name, " FAILED"
87+
print *, "Expected: ", expected
88+
print *, "Got: ", actual
89+
stop 1
90+
end if
91+
92+
end subroutine assert_test
93+
94+
end program tests_gcd_module

tests/maths/factorial.f90

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
!> Test program for factorial functions
2+
!!
3+
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
4+
!! in Pull Request: #31
5+
!! https://github.com/TheAlgorithms/Fortran/pull/31
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 test cases to validate the two facotrial functions in the factorial_module.
11+
12+
program tests_factorial
13+
use factorial_module
14+
implicit none
15+
16+
integer :: result, expected
17+
18+
! Run test cases
19+
call test_factorial()
20+
call test_recursive_factorial()
21+
22+
print *, "All tests completed."
23+
24+
contains
25+
26+
! Test case for iterative factorial for known values
27+
subroutine test_factorial()
28+
expected = 120
29+
result = factorial(5)
30+
call assert_test(result, expected, "Test 1: Iterative Factorial of 5")
31+
32+
expected = 1
33+
result = factorial(0)
34+
call assert_test(result, expected, "Test 2: Iterative Factorial of edge case: 0")
35+
36+
expected = 1
37+
result = factorial(1)
38+
call assert_test(result, expected, "Test 3: Iterative Factorial of edge case: 1")
39+
40+
expected = 40320
41+
result = factorial(8)
42+
call assert_test(result, expected, "Test 4: Iterative Factorial of 8")
43+
44+
expected = 720
45+
result = factorial(6)
46+
call assert_test(result, expected, "Test 5: Iterative Factorial of 6")
47+
end subroutine test_factorial
48+
49+
! Test case for recursive factorial for known values
50+
subroutine test_recursive_factorial()
51+
expected = 120
52+
result = recursive_factorial(5)
53+
call assert_test(result, expected, "Test 1: Recursive Factorial of 5")
54+
55+
expected = 1
56+
result = recursive_factorial(0)
57+
call assert_test(result, expected, "Test 2: Recursive Factorial of edge case: 0")
58+
59+
expected = 1
60+
result = recursive_factorial(1)
61+
call assert_test(result, expected, "Test 3: Recursive Factorial of edge case: 1")
62+
63+
expected = 40320
64+
result = recursive_factorial(8)
65+
call assert_test(result, expected, "Test 4: Recursive Factorial of 8")
66+
67+
expected = 720
68+
result = recursive_factorial(6)
69+
call assert_test(result, expected, "Test 5: Recursive Factorial of 6")
70+
end subroutine test_recursive_factorial
71+
72+
!> Subroutine to assert the test results
73+
subroutine assert_test(actual, expected, test_name)
74+
integer, intent(in) :: actual, expected
75+
character(len=*), intent(in) :: test_name
76+
77+
if (actual == expected) then
78+
print *, test_name, " PASSED"
79+
else
80+
print *, test_name, " FAILED"
81+
print *, "Expected: ", expected
82+
print *, "Got: ", actual
83+
stop 1
84+
end if
85+
end subroutine assert_test
86+
87+
end program tests_factorial

tests/maths/fibonacci.f90

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
!> Test program for Fibonacci functions
2+
!!
3+
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
4+
!! in Pull Request: #31
5+
!! https://github.com/TheAlgorithms/Fortran/pull/31
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 test cases to validate the two fibonacci functions in the fibonacci_module.
11+
12+
program tests_fibonacci
13+
use fibonacci_module
14+
implicit none
15+
16+
integer :: result, expected
17+
18+
! Run test cases
19+
call test_fib_zero()
20+
call test_fib_one()
21+
call test_fib_two()
22+
call test_fib_three()
23+
call test_fib_five()
24+
call test_fib_thirty()
25+
call test_fib_negative_one()
26+
call test_fib_negative_five()
27+
28+
print *, "All tests completed."
29+
30+
contains
31+
32+
! Test case 1: Fibonacci of 0
33+
subroutine test_fib_zero()
34+
expected = 0
35+
result = fib_rec(0)
36+
call assert_test(result, expected, "Test 1: Fibonacci of 0 (Recursive)")
37+
result = fib_itr(0)
38+
call assert_test(result, expected, "Test 1: Fibonacci of 0 (Iterative)")
39+
end subroutine test_fib_zero
40+
41+
! Test case 2: Fibonacci of 1
42+
subroutine test_fib_one()
43+
expected = 1
44+
result = fib_rec(1)
45+
call assert_test(result, expected, "Test 2: Fibonacci of 1 (Recursive)")
46+
result = fib_itr(1)
47+
call assert_test(result, expected, "Test 2: Fibonacci of 1 (Iterative)")
48+
end subroutine test_fib_one
49+
50+
! Test case 3: Fibonacci of 2
51+
subroutine test_fib_two()
52+
expected = 1
53+
result = fib_rec(2)
54+
call assert_test(result, expected, "Test 3: Fibonacci of 2 (Recursive)")
55+
result = fib_itr(2)
56+
call assert_test(result, expected, "Test 3: Fibonacci of 2 (Iterative)")
57+
end subroutine test_fib_two
58+
59+
! Test case 4: Fibonacci of 3
60+
subroutine test_fib_three()
61+
expected = 2
62+
result = fib_rec(3)
63+
call assert_test(result, expected, "Test 4: Fibonacci of 3 (Recursive)")
64+
result = fib_itr(3)
65+
call assert_test(result, expected, "Test 4: Fibonacci of 3 (Iterative)")
66+
end subroutine test_fib_three
67+
68+
! Test case 5: Fibonacci of 5
69+
subroutine test_fib_five()
70+
expected = 5
71+
result = fib_rec(5)
72+
call assert_test(result, expected, "Test 5: Fibonacci of 5 (Recursive)")
73+
result = fib_itr(5)
74+
call assert_test(result, expected, "Test 5: Fibonacci of 5 (Iterative)")
75+
end subroutine test_fib_five
76+
77+
! Test case 6: Fibonacci of 30
78+
subroutine test_fib_thirty()
79+
expected = 832040
80+
result = fib_rec(30)
81+
call assert_test(result, expected, "Test 5: Fibonacci of 30 (Recursive)")
82+
result = fib_itr(30)
83+
call assert_test(result, expected, "Test 5: Fibonacci of 30 (Iterative)")
84+
end subroutine test_fib_thirty
85+
86+
! Test case 7: Fibonacci of negative input
87+
subroutine test_fib_negative_one()
88+
expected = -1
89+
result = fib_rec(-9)
90+
call assert_test(result, expected, "Test 6: Fibonacci of -1 (Recursive)")
91+
result = fib_itr(-9)
92+
call assert_test(result, expected, "Test 6: Fibonacci of -1 (Iterative)")
93+
end subroutine test_fib_negative_one
94+
95+
! Test case 8: Fibonacci of negative input
96+
subroutine test_fib_negative_five()
97+
expected = -1
98+
result = fib_rec(-9)
99+
call assert_test(result, expected, "Test 7: Fibonacci of -5 (Recursive)")
100+
result = fib_itr(-9)
101+
call assert_test(result, expected, "Test 7: Fibonacci of -5 (Iterative)")
102+
end subroutine test_fib_negative_five
103+
104+
!> Subroutine to assert the test results
105+
subroutine assert_test(actual, expected, test_name)
106+
integer, intent(in) :: actual, expected
107+
character(len=*), intent(in) :: test_name
108+
109+
if (actual == expected) then
110+
print *, test_name, " PASSED"
111+
else
112+
print *, test_name, " FAILED"
113+
print *, "Expected: ", expected
114+
print *, "Got: ", actual
115+
stop 1
116+
end if
117+
118+
end subroutine assert_test
119+
120+
end program tests_fibonacci
121+

0 commit comments

Comments
 (0)