Skip to content

Commit 6f65c17

Browse files
committed
Use arrays for passed and failure_message to make it easier for learners to write tests
1 parent 1391a4f commit 6f65c17

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

episodes/6-writing-your-first-unit-test/standard-fortran/challenge/test/test_temp_conversions.f90

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,39 @@ program test_temp_conversions
22
use temp_conversions, only : fahrenheit_to_celsius, celsius_to_kelvin
33
implicit none
44

5-
! Declare passed and failure message to be set by a test subroutine
6-
logical :: passed
7-
character(len=200) :: failure_message
5+
integer :: i
86

9-
! Call your test subroutine here
10-
call test(passed, failure_message)
7+
! Declare passed and failure message arrays to be set by a test subroutine(s)
8+
logical :: passed(1)
9+
character(len=200) :: failure_message(1)
1110

12-
if (.not. passed) then
13-
write(*,*) "FAIL: ", trim(failure_message)
14-
stop 1
15-
else
11+
! Call your test subroutine(s) here
12+
call test(passed(1), failure_message(1))
13+
14+
if (all(passed)) then
1615
write(*,*) "All tests passed!"
16+
else
17+
do i = 1, size(passed)
18+
if (.not. passed(i)) then
19+
write(*,*) "FAIL: ", trim(failure_message(i))
20+
end if
21+
end do
22+
stop 1
1723
end if
1824

1925
contains
2026
!> The test subroutine
2127
subroutine test(passed, failure_message)
22-
!> A logical to track whether the test passed or not
23-
logical, intent(out) :: passed
24-
!> A failure message to be displayed if passed is false
25-
character(len=200), intent(out) :: failure_message
28+
!> A logical to track whether the test passed or not
29+
logical, intent(out) :: passed
30+
!> A failure message to be displayed if passed is false
31+
character(len=200), intent(out) :: failure_message
2632

27-
! No test has been written yet so just default passed to .true.
28-
passed = .true.
33+
! No test has been written yet so just default passed to .true.
34+
passed = .true.
2935

30-
! Populate the failure message
31-
write(failure_message, '(A,A)') "It is useful to include input, expected output and actual output values here. To do ", &
32-
"that, replace (A,A) with the correct format for your values, for example (A,I3,A,I3,A,I3)."
36+
! Populate the failure message
37+
write(failure_message, '(A,A)') "It is useful to include input, expected output and actual output values here. To do ", &
38+
"that, replace (A,A) with the correct format for your values, for example (A,I3,A,I3,A,I3)."
3339
end subroutine test
3440
end program test_temp_conversions

0 commit comments

Comments
 (0)