Skip to content

Commit 1066da8

Browse files
committed
Align exercise 3 solution with new challenge structure
1 parent b12f775 commit 1066da8

2 files changed

Lines changed: 53 additions & 99 deletions

File tree

.github/workflows/test-exercises.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
- name: Test episode 2
5252
run: podman build . -f docker-tests/Dockerfile.02
5353

54-
- name: Test episode 2
54+
- name: Test episode 3
5555
run: podman build . -f docker-tests/Dockerfile.03
5656

5757
- name: Test episode 4

exercises/3-writing-your-first-unit-test/solution/pfunit/test_temp_conversions.pf

Lines changed: 52 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,43 @@ module test_temp_conversions
66

77
public
88

9-
!*************************************************************************!
10-
! Derived Types !
11-
!-------------------------------------------------------------------------!
12-
13-
!> Test parameter type to package the test parameters
14-
@TestParameter
15-
type, extends(AbstractTestParameter) :: temp_conversions_test_params_t
16-
!> The temperature to input into the function being tested
17-
real :: input
18-
!> Theb temperature expected to be returned from the function being tested
19-
real :: expected_output
20-
!> A description of the test to be outputted for logging
21-
character(len=100) :: description
22-
contains
23-
procedure :: toString => temp_conversions_test_params_t_toString
24-
end type temp_conversions_test_params_t
25-
26-
!> Test case type to specify the style of test (paramaterized)
27-
@TestCase(constructor=new_test_case)
28-
type, extends(ParameterizedTestCase) :: temp_conversions_test_case_t
29-
type(temp_conversions_test_params_t) :: params
30-
end type temp_conversions_test_case_t
31-
32-
!*************************************************************************!
33-
349
contains
3510

3611
!*************************************************************************!
3712
! Test fahrenheit_to_celsius !
3813
!-------------------------------------------------------------------------!
14+
!> Test Logic, unit test subroutine for fahrenheit_to_celsius with 0.0°F
15+
@Test
16+
subroutine test_fahrenheit_to_celsius()
17+
integer, parameter :: num_tests = 4
18+
real :: actual_output, inputs(num_tests), expected_outputs(num_tests)
19+
character(len=200) :: failure_message
20+
integer :: i
3921

40-
!> Test Suite for tests of fahrenheit_to_celsius
41-
function fahrenheit_to_celsius_testsuite() result(params)
42-
!> An array of test parameters, each specifying an individual test
43-
class(temp_conversions_test_params_t), allocatable :: params(:)
44-
45-
params = [ &
46-
temp_conversions_test_params_t(0.0, -17.777779, "0.0 °F"), &
47-
temp_conversions_test_params_t(32.0, 0.0, "0.0 °C"), &
48-
temp_conversions_test_params_t(-100.0, -73.333336, "100 °F"), &
49-
temp_conversions_test_params_t(1.23,-17.094444, "Decimal °F") &
22+
inputs = [ &
23+
0.0, &
24+
32.0, &
25+
-100.0, &
26+
1.23 &
27+
]
28+
expected_outputs = [ &
29+
-17.777779, &
30+
0.0, &
31+
-73.333336, &
32+
-17.094444 &
5033
]
51-
end function fahrenheit_to_celsius_testsuite
52-
53-
!> Test Logic, unit test subroutine for fahrenheit_to_celsius
54-
@Test(testParameters={fahrenheit_to_celsius_testsuite()})
55-
subroutine test_fahrenheit_to_celsius(this)
56-
!> The test case which indicates the type of test we are running
57-
class(temp_conversions_test_case_t), intent(inout) :: this
5834

59-
character(len=200) :: failure_message
60-
real :: actual_output
35+
do i = 1, num_tests
36+
! Get the actual celsius value returned from fahrenheit_to_celsius
37+
actual_output = fahrenheit_to_celsius(inputs(i))
6138

62-
! Get the actual celsius value returned from fahrenheit_to_celsius
63-
actual_output = fahrenheit_to_celsius(this%params%input)
39+
! Populate the failure message
40+
write(failure_message, '(A,F7.2,A,F7.2,A,F7.2,A)') "Failed With ", inputs(i), " °F: Expected ", &
41+
expected_outputs(i), "°C but got ", actual_output, "°C"
42+
@assertEqual(expected_outputs(i), actual_output, tolerance=1e-6, message=trim(failure_message))
6443

65-
! Populate the failure message
66-
write(failure_message, '(A,F7.2,A,F7.2,A,F7.2,A)') "Failed With ", this%params%input, " °F: Expected ", &
67-
this%params%expected_output, "°C but got ", actual_output, "°C"
68-
@assertEqual(this%params%expected_output, actual_output, tolerance=1e-6, message=trim(failure_message))
44+
failure_message = ""
45+
end do
6946

7047
end subroutine test_fahrenheit_to_celsius
7148

@@ -75,63 +52,40 @@ contains
7552
!*************************************************************************!
7653
! Test celsius_to_kelvin !
7754
!-------------------------------------------------------------------------!
78-
79-
!> Test Suite for tests of celsius_to_kelvin
80-
function celsius_to_kelvin_testsuite() result(params)
81-
!> An array of test parameters, each specifying an individual test
82-
class(temp_conversions_test_params_t), allocatable :: params(:)
83-
84-
params = [ &
85-
temp_conversions_test_params_t(0.0, 273.15, "0.0 °C"), &
86-
temp_conversions_test_params_t(-273.15, 0.0, "0.0 °K"), &
87-
temp_conversions_test_params_t(-173.15, 100.0, "100.0 °K") &
88-
]
89-
end function celsius_to_kelvin_testsuite
90-
9155
!> Test Logic, unit test subroutine for celsius_to_kelvin
92-
@Test(testParameters={celsius_to_kelvin_testsuite()})
93-
subroutine test_celsius_to_kelvin(this)
94-
!> The test case which indicates the type of test we are running
95-
class(temp_conversions_test_case_t), intent(inout) :: this
96-
56+
@Test
57+
subroutine test_celsius_to_kelvin()
58+
integer, parameter :: num_tests = 3
59+
real :: actual_output, inputs(num_tests), expected_outputs(num_tests)
9760
character(len=200) :: failure_message
98-
real :: actual_output
99-
100-
! Get the actual kelvin value returned from celsius_to_kelvin
101-
actual_output = celsius_to_kelvin(this%params%input)
102-
103-
! Populate the failure message
104-
write(failure_message, '(A,F7.2,A,F7.2,A,F7.2,A)') "Failed With ", this%params%input, "°C: Expected ", &
105-
this%params%expected_output, "°K but got ", actual_output, "°K"
106-
@assertEqual(this%params%expected_output, actual_output, tolerance=1e-6, message=trim(failure_message))
107-
108-
end subroutine test_celsius_to_kelvin
61+
integer :: i
10962

110-
!*************************************************************************!
63+
inputs = [ &
64+
0.0, &
65+
-273.15, &
66+
-173.15 &
67+
]
11168

69+
expected_outputs = [ &
70+
273.15, &
71+
0.0, &
72+
100.0 &
73+
]
11274

113-
!*************************************************************************!
114-
! Type Constructors !
115-
!-------------------------------------------------------------------------!
75+
do i = 1, num_tests
11676

117-
!> Constructor for converting test parameters into a test case
118-
function new_test_case(testParameter) result(tst)
119-
!> The parameters to be converted to a test case
120-
type(temp_conversions_test_params_t), intent(in) :: testParameter
121-
!> The test case to return after conversion from parameters
122-
type(temp_conversions_test_case_t) :: tst
77+
! Get the actual kelvin value returned from celsius_to_kelvin
78+
actual_output = celsius_to_kelvin(inputs(i))
12379

124-
tst%params = testParameter
125-
end function new_test_case
80+
! Populate the failure message
81+
write(failure_message, '(A,F7.2,A,F7.2,A,F7.2,A)') "Failed With ", inputs(i), "°C: Expected ", &
82+
expected_outputs(i), "°K but got ", actual_output, "°K"
83+
@assertEqual(expected_outputs(i), actual_output, tolerance=1e-6, message=trim(failure_message))
12684

127-
!> Constructor for converting test parameters into a string
128-
function temp_conversions_test_params_t_toString(this) result(string)
129-
!> The parameters to be converted to a string
130-
class(temp_conversions_test_params_t), intent(in) :: this
131-
character(:), allocatable :: string
85+
failure_message = ""
86+
end do
13287

133-
string = trim(this%description)
134-
end function temp_conversions_test_params_t_toString
88+
end subroutine test_celsius_to_kelvin
13589

13690
!*************************************************************************!
13791

0 commit comments

Comments
 (0)