|
| 1 | +program test_temp_conversions |
| 2 | + use temp_conversions, only : fahrenheit_to_celsius, celsius_to_kelvin |
| 3 | + implicit none |
| 4 | + |
| 5 | + ! The tolerance to use when comparing floats |
| 6 | + real, parameter :: tolerance = 1e-6 |
| 7 | + |
| 8 | + integer :: i |
| 9 | + |
| 10 | + ! Declare passed and failure message arrays to be set by a test subroutine(s) |
| 11 | + logical :: passed(7) |
| 12 | + character(len=200) :: failure_message(7) |
| 13 | + |
| 14 | + call test_fahrenheit_to_celsius(0.0, -17.77778, passed(1), failure_message(1)) |
| 15 | + call test_fahrenheit_to_celsius(32.0, 0.0, passed(2), failure_message(2)) |
| 16 | + call test_fahrenheit_to_celsius(-100.0, -73.33, passed(3), failure_message(3)) |
| 17 | + call test_fahrenheit_to_celsius(1.23,-17.09, passed(4), failure_message(4)) |
| 18 | + |
| 19 | + call test_celsius_to_kelvin(0.0, 273.15, passed(5), failure_message(5)) |
| 20 | + call test_celsius_to_kelvin(-273.15, 0.0, passed(6), failure_message(6)) |
| 21 | + call test_celsius_to_kelvin(-173.15, 100.0, passed(7), failure_message(7)) |
| 22 | + |
| 23 | + if (all(passed)) then |
| 24 | + write(*,*) "All tests passed!" |
| 25 | + else |
| 26 | + do i = 1, size(passed) |
| 27 | + if (.not. passed(i)) then |
| 28 | + write(*,*) "FAIL: ", trim(failure_message(i)) |
| 29 | + end if |
| 30 | + end do |
| 31 | + stop 1 |
| 32 | + end if |
| 33 | + |
| 34 | +contains |
| 35 | + !> Unit test subroutine for celsius_to_kelvin |
| 36 | + subroutine test_fahrenheit_to_celsius(input, expected_output, passed, failure_message) |
| 37 | + !> The input fahrenheit value to pass to fahrenheit_to_celsius |
| 38 | + real, intent(in) :: input |
| 39 | + !> The celsius value we expect to be returner from fahrenheit_to_celsius |
| 40 | + real, intent(in) :: expected_output |
| 41 | + !> A logical to track whether the test passed or not |
| 42 | + logical, intent(out) :: passed |
| 43 | + !> A failure message to be displayed if passed is false |
| 44 | + character(len=200), intent(out) :: failure_message |
| 45 | + |
| 46 | + real :: actual_output |
| 47 | + |
| 48 | + ! Get the actual celsius value returned from fahrenheit_to_celsius |
| 49 | + actual_output = fahrenheit_to_celsius(input) |
| 50 | + |
| 51 | + ! Check that the actual value is within some tolerance of the expected value |
| 52 | + passed = abs(actual_output - expected_output) < tolerance |
| 53 | + |
| 54 | + ! Populate the failure message |
| 55 | + write(failure_message, '(A,F7.2,A,F7.2,A,F7.2)') "Failed With ", input, ": Expected ", expected_output, " but got ", & |
| 56 | + actual_output |
| 57 | + end subroutine test_fahrenheit_to_celsius |
| 58 | + |
| 59 | + !> Unit test subroutine for celsius_to_kelvin |
| 60 | + subroutine test_celsius_to_kelvin(input, expected_output, passed, failure_message) |
| 61 | + !> The input celsius value to pass to celsius_to_kelvin |
| 62 | + real, intent(in) :: input |
| 63 | + !> The kelvin value we expect to be returner from celsius_to_kelvin |
| 64 | + real, intent(in) :: expected_output |
| 65 | + !> A logical to track whether the test passed or not |
| 66 | + logical, intent(out) :: passed |
| 67 | + !> A failure message to be displayed if passed is false |
| 68 | + character(len=200), intent(out) :: failure_message |
| 69 | + |
| 70 | + real :: actual_output |
| 71 | + |
| 72 | + ! Get the actual celsius value returned from celsius_to_kelvin |
| 73 | + actual_output = celsius_to_kelvin(input) |
| 74 | + |
| 75 | + ! Check that the actual value is within some tolerance of the expected value |
| 76 | + passed = abs(actual_output - expected_output) < tolerance |
| 77 | + |
| 78 | + ! Populate the failure message |
| 79 | + write(failure_message, '(A,F7.2,A,F7.2,A,F7.2)') "Failed With ", input, ": Expected ", expected_output, " but got ", & |
| 80 | + actual_output |
| 81 | + end subroutine test_celsius_to_kelvin |
| 82 | +end program test_temp_conversions |
0 commit comments