|
| 1 | +!> Test program for the square root function. |
| 2 | +!! |
| 3 | +!! Created by: ITZ-NIHALPATEL |
| 4 | +!! |
| 5 | +!! This program provides test cases to validate the square root function |
| 6 | +!! in the square_root_module. |
| 7 | + |
| 8 | +program tests_square_root |
| 9 | + use square_root_module |
| 10 | + implicit none |
| 11 | + |
| 12 | + real :: result, expected |
| 13 | + real, parameter :: tolerance = 1e-6 ! Tolerance for floating-point comparison |
| 14 | + |
| 15 | + ! Run test cases |
| 16 | + call test_sqrt_zero() |
| 17 | + call test_sqrt_one() |
| 18 | + call test_sqrt_perfect_square() |
| 19 | + call test_sqrt_non_perfect_square() |
| 20 | + call test_sqrt_large_number() |
| 21 | + call test_sqrt_negative_input() |
| 22 | + |
| 23 | + print *, "All square root tests completed." |
| 24 | + |
| 25 | +contains |
| 26 | + |
| 27 | + ! Test case 1: Square root of 0 |
| 28 | + subroutine test_sqrt_zero() |
| 29 | + expected = 0.0 |
| 30 | + result = calculate_sqrt(0.0) |
| 31 | + call assert_test(result, expected, "Test 1: Square root of 0", tolerance) |
| 32 | + end subroutine test_sqrt_zero |
| 33 | + |
| 34 | + ! Test case 2: Square root of 1 |
| 35 | + subroutine test_sqrt_one() |
| 36 | + expected = 1.0 |
| 37 | + result = calculate_sqrt(1.0) |
| 38 | + call assert_test(result, expected, "Test 2: Square root of 1", tolerance) |
| 39 | + end subroutine test_sqrt_one |
| 40 | + |
| 41 | + ! Test case 3: Square root of a perfect square (e.g., 16) |
| 42 | + subroutine test_sqrt_perfect_square() |
| 43 | + expected = 4.0 |
| 44 | + result = calculate_sqrt(16.0) |
| 45 | + call assert_test(result, expected, "Test 3: Square root of 16", tolerance) |
| 46 | + end subroutine test_sqrt_perfect_square |
| 47 | + |
| 48 | + ! Test case 4: Square root of a non-perfect square (e.g., 2) |
| 49 | + subroutine test_sqrt_non_perfect_square() |
| 50 | + expected = 1.41421356 |
| 51 | + result = calculate_sqrt(2.0) |
| 52 | + call assert_test(result, expected, "Test 4: Square root of 2", tolerance) |
| 53 | + end subroutine test_sqrt_non_perfect_square |
| 54 | + |
| 55 | + ! Test case 5: Square root of a larger number (e.g., 12345.0) |
| 56 | + subroutine test_sqrt_large_number() |
| 57 | + expected = 111.108055 |
| 58 | + result = calculate_sqrt(12345.0) |
| 59 | + call assert_test(result, expected, "Test 5: Square root of 12345.0", tolerance) |
| 60 | + end subroutine test_sqrt_large_number |
| 61 | + |
| 62 | + ! Test case 6: Square root of a negative number |
| 63 | + subroutine test_sqrt_negative_input() |
| 64 | + expected = -1.0 ! Expected error indicator |
| 65 | + result = calculate_sqrt(-9.0) |
| 66 | + call assert_test(result, expected, "Test 6: Square root of -9 (Negative Input)", tolerance) |
| 67 | + end subroutine test_sqrt_negative_input |
| 68 | + |
| 69 | + !> Subroutine to assert the test results for real numbers |
| 70 | + !! Includes a tolerance for floating-point comparisons. |
| 71 | + subroutine assert_test(actual, expected, test_name, tol) |
| 72 | + real, intent(in) :: actual, expected, tol |
| 73 | + character(len=*), intent(in) :: test_name |
| 74 | + |
| 75 | + if (abs(actual - expected) <= tol) then |
| 76 | + print *, test_name, " PASSED" |
| 77 | + else |
| 78 | + print *, test_name, " FAILED" |
| 79 | + print *, "Expected: ", expected |
| 80 | + print *, "Got: ", actual |
| 81 | + stop 1 ! Stop execution on failure |
| 82 | + end if |
| 83 | + |
| 84 | + end subroutine assert_test |
| 85 | + |
| 86 | +end program tests_square_root |
0 commit comments