|
9 | 9 | !!
|
10 | 10 | !! This program provides test cases to validate the trapezoidal_rule module against known integral values.
|
11 | 11 |
|
| 12 | +!> Test program for the Trapezoidal Rule module |
| 13 | +!! |
| 14 | +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) |
| 15 | +!! in Pull Request: #32 |
| 16 | +!! https://github.com/TheAlgorithms/Fortran/pull/32 |
| 17 | +!! |
| 18 | +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request |
| 19 | +!! addressing bugs/corrections to this file. Thank you! |
| 20 | +!! |
| 21 | +!! This program provides test cases to validate the trapezoidal_rule module against known integral values. |
12 | 22 |
|
13 | 23 | program test_trapezoidal_rule
|
14 | 24 | use trapezoidal_rule
|
15 | 25 | implicit none
|
16 | 26 |
|
17 |
| - real(dp) :: lower_bound, upper_bound, integral_result, expected |
18 |
| - real(dp), parameter :: pi = 4.d0*DATAN(1.d0) ! Define Pi. Ensures maximum precision available on any architecture |
19 |
| - |
20 |
| - integer :: panels_number |
21 |
| - |
22 |
| - ! Test 1: ∫ x^2 dx from 0 to 1 (Exact result = 1/3 ≈ 0.3333) |
23 |
| - lower_bound = 0.0_dp |
24 |
| - upper_bound = 1.0_dp |
25 |
| - panels_number = 1000000 |
26 |
| - expected = 1.0_dp / 3.0_dp |
27 |
| - call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) |
28 |
| - call assert_test(integral_result, expected, "Test 1: ∫ x^2 dx from 0 to 1") |
29 |
| - |
30 |
| - ! Test 2: ∫ x^2 dx from 0 to 2 (Exact result = 8/3 ≈ 2.6667) |
31 |
| - lower_bound = 0.0_dp |
32 |
| - upper_bound = 2.0_dp |
33 |
| - panels_number = 1000000 |
34 |
| - expected = 8.0_dp / 3.0_dp |
35 |
| - call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) |
36 |
| - call assert_test(integral_result, expected, "Test 2: ∫ x^2 dx from 0 to 2") |
37 |
| - |
38 |
| - ! Test 3: ∫ sin(x) dx from 0 to π (Exact result = 2) |
39 |
| - lower_bound = 0.0_dp |
40 |
| - upper_bound = pi |
41 |
| - panels_number = 1000000 |
42 |
| - expected = 2.0_dp |
43 |
| - call trapezoid(integral_result, lower_bound, upper_bound, panels_number, sin_function) |
44 |
| - call assert_test(integral_result, expected, "Test 3: ∫ sin(x) dx from 0 to π") |
| 27 | + ! Run test cases |
| 28 | + call test_integral_x_squared_0_to_1() |
| 29 | + call test_integral_x_squared_0_to_2() |
| 30 | + call test_integral_sin_0_to_pi() |
| 31 | + call test_integral_e_x_0_to_1() |
| 32 | + call test_integral_1_over_x_1_to_e() |
| 33 | + call test_integral_cos_0_to_pi_over_2() |
| 34 | + call test_integral_x_cubed_0_to_1() |
| 35 | + call test_integral_sin_x_squared_0_to_1() |
| 36 | + |
| 37 | + print *, "All tests completed." |
45 | 38 |
|
46 | 39 | contains
|
47 | 40 |
|
| 41 | + ! Test case 1: ∫ x^2 dx from 0 to 1 (Exact result = 1/3 ≈ 0.3333) |
| 42 | + subroutine test_integral_x_squared_0_to_1() |
| 43 | + real(dp) :: lower_bound, upper_bound, integral_result, expected |
| 44 | + integer :: panels_number |
| 45 | + lower_bound = 0.0_dp |
| 46 | + upper_bound = 1.0_dp |
| 47 | + panels_number = 1000000 |
| 48 | + expected = 1.0_dp/3.0_dp |
| 49 | + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) |
| 50 | + call assert_test(integral_result, expected, "Test 1: ∫ x^2 dx from 0 to 1") |
| 51 | + end subroutine test_integral_x_squared_0_to_1 |
| 52 | + |
| 53 | + ! Test case 2: ∫ x^2 dx from 0 to 2 (Exact result = 8/3 ≈ 2.6667) |
| 54 | + subroutine test_integral_x_squared_0_to_2() |
| 55 | + real(dp) :: lower_bound, upper_bound, integral_result, expected |
| 56 | + integer :: panels_number |
| 57 | + lower_bound = 0.0_dp |
| 58 | + upper_bound = 2.0_dp |
| 59 | + panels_number = 1000000 |
| 60 | + expected = 8.0_dp/3.0_dp |
| 61 | + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) |
| 62 | + call assert_test(integral_result, expected, "Test 2: ∫ x^2 dx from 0 to 2") |
| 63 | + end subroutine test_integral_x_squared_0_to_2 |
| 64 | + |
| 65 | + ! Test case 3: ∫ sin(x) dx from 0 to π (Exact result = 2) |
| 66 | + subroutine test_integral_sin_0_to_pi() |
| 67 | + real(dp) :: lower_bound, upper_bound, integral_result, expected |
| 68 | + integer :: panels_number |
| 69 | + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. |
| 70 | + lower_bound = 0.0_dp |
| 71 | + upper_bound = pi |
| 72 | + panels_number = 1000000 |
| 73 | + expected = 2.0_dp |
| 74 | + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, sin_function) |
| 75 | + call assert_test(integral_result, expected, "Test 3: ∫ sin(x) dx from 0 to π") |
| 76 | + end subroutine test_integral_sin_0_to_pi |
| 77 | + |
| 78 | + ! Test case 4: ∫ e^x dx from 0 to 1 (Exact result = e - 1 ≈ 1.7183) |
| 79 | + subroutine test_integral_e_x_0_to_1() |
| 80 | + real(dp) :: lower_bound, upper_bound, integral_result, expected |
| 81 | + integer :: panels_number |
| 82 | + lower_bound = 0.0_dp |
| 83 | + upper_bound = 1.0_dp |
| 84 | + panels_number = 1000000 |
| 85 | + expected = exp(1.0_dp) - 1.0_dp |
| 86 | + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, exp_function) |
| 87 | + call assert_test(integral_result, expected, "Test 4: ∫ e^x dx from 0 to 1") |
| 88 | + end subroutine test_integral_e_x_0_to_1 |
| 89 | + |
| 90 | + ! Test case 5: ∫ (1/x) dx from 1 to e (Exact result = 1) |
| 91 | + subroutine test_integral_1_over_x_1_to_e() |
| 92 | + real(dp) :: lower_bound, upper_bound, integral_result, expected |
| 93 | + integer :: panels_number |
| 94 | + lower_bound = 1.0_dp |
| 95 | + upper_bound = exp(1.0_dp) |
| 96 | + panels_number = 1000000 |
| 97 | + expected = 1.0_dp |
| 98 | + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, log_function) |
| 99 | + call assert_test(integral_result, expected, "Test 5: ∫ (1/x) dx from 1 to e") |
| 100 | + end subroutine test_integral_1_over_x_1_to_e |
| 101 | + |
| 102 | + ! Test case 6: ∫ cos(x) dx from 0 to π/2 (Exact result = 1) |
| 103 | + subroutine test_integral_cos_0_to_pi_over_2() |
| 104 | + real(dp) :: lower_bound, upper_bound, integral_result, expected |
| 105 | + integer :: panels_number |
| 106 | + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. |
| 107 | + lower_bound = 0.0_dp |
| 108 | + upper_bound = pi/2.0_dp |
| 109 | + panels_number = 1000000 |
| 110 | + expected = 1.0_dp |
| 111 | + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, cos_function) |
| 112 | + call assert_test(integral_result, expected, "Test 6: ∫ cos(x) dx from 0 to π/2") |
| 113 | + end subroutine test_integral_cos_0_to_pi_over_2 |
| 114 | + |
| 115 | + ! Test case 7: ∫ x^3 dx from 0 to 1 (Exact result = 1/4 = 0.25) |
| 116 | + subroutine test_integral_x_cubed_0_to_1() |
| 117 | + real(dp) :: lower_bound, upper_bound, integral_result, expected |
| 118 | + integer :: panels_number |
| 119 | + lower_bound = 0.0_dp |
| 120 | + upper_bound = 1.0_dp |
| 121 | + panels_number = 1000000 |
| 122 | + expected = 0.25_dp |
| 123 | + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_cubed) |
| 124 | + call assert_test(integral_result, expected, "Test 7: ∫ x^3 dx from 0 to 1") |
| 125 | + end subroutine test_integral_x_cubed_0_to_1 |
| 126 | + |
| 127 | + ! Test case 8: ∫ sin(x^2) dx from 0 to 1 (Approximate value) |
| 128 | + subroutine test_integral_sin_x_squared_0_to_1() |
| 129 | + real(dp) :: lower_bound, upper_bound, integral_result, expected |
| 130 | + integer :: panels_number |
| 131 | + lower_bound = 0.0_dp |
| 132 | + upper_bound = 1.0_dp |
| 133 | + panels_number = 1000000 |
| 134 | + expected = 0.31026_dp ! Approximate value, you can adjust tolerance as needed |
| 135 | + call trapezoid(integral_result, lower_bound, upper_bound, panels_number, sin_squared_function) |
| 136 | + call assert_test(integral_result, expected, "Test 8: ∫ sin(x^2) dx from 0 to 1") |
| 137 | + end subroutine test_integral_sin_x_squared_0_to_1 |
| 138 | + |
48 | 139 | ! Function for x^2
|
49 | 140 | real(dp) function f_x_squared(x)
|
50 | 141 | real(dp), intent(in) :: x
|
51 | 142 | f_x_squared = x**2
|
52 | 143 | end function f_x_squared
|
53 | 144 |
|
| 145 | + ! Function for e^x |
| 146 | + real(dp) function exp_function(x) |
| 147 | + real(dp), intent(in) :: x |
| 148 | + exp_function = exp(x) |
| 149 | + end function exp_function |
| 150 | + |
| 151 | + ! Function for 1/x |
| 152 | + real(dp) function log_function(x) |
| 153 | + real(dp), intent(in) :: x |
| 154 | + log_function = 1.0_dp/x |
| 155 | + end function log_function |
| 156 | + |
| 157 | + ! Function for cos(x) |
| 158 | + real(dp) function cos_function(x) |
| 159 | + real(dp), intent(in) :: x |
| 160 | + cos_function = cos(x) |
| 161 | + end function cos_function |
| 162 | + |
| 163 | + ! Function for x^3 |
| 164 | + real(dp) function f_x_cubed(x) |
| 165 | + real(dp), intent(in) :: x |
| 166 | + f_x_cubed = x**3 |
| 167 | + end function f_x_cubed |
| 168 | + |
| 169 | + ! Function for sin(x^2) |
| 170 | + real(dp) function sin_squared_function(x) |
| 171 | + real(dp), intent(in) :: x |
| 172 | + sin_squared_function = sin(x**2) |
| 173 | + end function sin_squared_function |
| 174 | + |
54 | 175 | ! Function for sin(x)
|
55 | 176 | real(dp) function sin_function(x)
|
56 | 177 | real(dp), intent(in) :: x
|
57 | 178 | sin_function = sin(x)
|
58 | 179 | end function sin_function
|
59 | 180 |
|
60 |
| - ! Assertion subroutine |
61 |
| - subroutine assert_test(result, expected, test_name) |
62 |
| - real(dp), intent(in) :: result, expected |
| 181 | + !> Subroutine to assert the test results |
| 182 | + subroutine assert_test(actual, expected, test_name) |
| 183 | + real(dp), intent(in) :: actual, expected |
63 | 184 | character(len=*), intent(in) :: test_name
|
64 | 185 | real(dp), parameter :: tol = 1.0e-5_dp
|
65 | 186 |
|
66 |
| - if (abs(result - expected) < tol) then |
| 187 | + if (abs(actual - expected) < tol) then |
67 | 188 | print *, test_name, " PASSED"
|
68 | 189 | else
|
69 | 190 | print *, test_name, " FAILED"
|
70 | 191 | print *, " Expected: ", expected
|
71 |
| - print *, " Got: ", result |
| 192 | + print *, " Got: ", actual |
72 | 193 | stop 1
|
73 | 194 | end if
|
74 | 195 | end subroutine assert_test
|
75 | 196 |
|
76 | 197 | end program test_trapezoidal_rule
|
| 198 | + |
0 commit comments