|
| 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