Skip to content

Commit b05c51d

Browse files
authored
gh-45: Update exercises to match new lesson structure (#65)
1 parent 1bda46f commit b05c51d

12 files changed

Lines changed: 255 additions & 381 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

episodes/6-parameterising-pfunit-tests.md

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,16 @@ testParameters = dot_product_test_parameters(a, b, expected_dot_product, "10x10
345345

346346
::: challenge
347347

348-
### Challenge: Parameterising temperature conversion tests with pFUnit, part 1
348+
### Challenge: Parameterising tests with pFUnit, part 1
349349

350-
Begin parameterising your pFUnit tests of temperature conversions by creating a custom derived type for your test parameters.
351-
352-
See part 2 of [3-writing-your-first-unit-test/challenge](https://github.com/carpentries-incubator/fortran-unit-testing/tree/main/exercises/3-writing-your-first-unit-test/challenge).
350+
Take a look at
351+
[4-fortran-unit-test-syntax/challenge](https://github.com/carpentries-incubator/fortran-unit-testing/tree/main/exercises/4-fortran-unit-test-syntax/challenge).
352+
Complete tasks 1 and 2.1.
353353

354354
::: solution
355355

356-
A solution is provided in [3-writing-your-first-unit-test/solution](https://github.com/carpentries-incubator/fortran-unit-testing/tree/main/exercises/3-writing-your-first-unit-test/solution).
356+
A solution is provided in
357+
[4-fortran-unit-test-syntax/solution](https://github.com/carpentries-incubator/fortran-unit-testing/tree/main/exercises/4-fortran-unit-test-syntax/solution).
357358

358359
:::
359360

@@ -413,23 +414,6 @@ our test case type called **setUp**.
413414

414415
::::::::::::::::::::
415416

416-
::: challenge
417-
418-
### Challenge: Parameterising temperature conversion tests with pFUnit, part 2
419-
420-
Continue parameterising your pFUnit tests of temperature conversions by Updating your custom test case type to utilise your new
421-
parameter type.
422-
423-
See part 2 of [3-writing-your-first-unit-test/challenge](https://github.com/carpentries-incubator/fortran-unit-testing/tree/main/exercises/3-writing-your-first-unit-test/challenge).
424-
425-
::: solution
426-
427-
A solution is provided in [3-writing-your-first-unit-test/solution](https://github.com/carpentries-incubator/fortran-unit-testing/tree/main/exercises/3-writing-your-first-unit-test/solution).
428-
429-
:::
430-
431-
:::
432-
433417
::::::::::::
434418

435419
:::::::::::: spoiler
@@ -478,16 +462,16 @@ Let's look at the key aspects of this function:
478462

479463
::: challenge
480464

481-
### Challenge: Parameterising temperature conversion tests with pFUnit, part 3
482-
483-
Continue parameterising your pFUnit tests of temperature conversions by defining your parameters sets and returning them from your
484-
test suite function.
465+
### Challenge: Parameterising tests with pFUnit, part 2
485466

486-
See part 2 of [3-writing-your-first-unit-test/challenge](https://github.com/carpentries-incubator/fortran-unit-testing/tree/main/exercises/3-writing-your-first-unit-test/challenge).
467+
Continuing with
468+
[4-fortran-unit-test-syntax/challenge](https://github.com/carpentries-incubator/fortran-unit-testing/tree/main/exercises/4-fortran-unit-test-syntax/challenge),
469+
complete task 2.2.
487470

488471
::: solution
489472

490-
A solution is provided in [3-writing-your-first-unit-test/solution](https://github.com/carpentries-incubator/fortran-unit-testing/tree/main/exercises/3-writing-your-first-unit-test/solution).
473+
A solution is provided in
474+
[4-fortran-unit-test-syntax/solution](https://github.com/carpentries-incubator/fortran-unit-testing/tree/main/exercises/4-fortran-unit-test-syntax/solution).
491475

492476
:::
493477

@@ -520,16 +504,16 @@ The key aspects are:
520504

521505
::: challenge
522506

523-
### Challenge: Parameterising temperature conversion tests with pFUnit, part 4
524-
525-
Finish parameterising your pFUnit tests of temperature conversions by Updating your test subroutine to make use of your new custom
526-
parameter type and test suite.
507+
### Challenge: Parameterising tests with pFUnit, part 3
527508

528-
See part 2 of [3-writing-your-first-unit-test/challenge](https://github.com/carpentries-incubator/fortran-unit-testing/tree/main/exercises/3-writing-your-first-unit-test/challenge).
509+
Continuing with
510+
[4-fortran-unit-test-syntax/challenge](https://github.com/carpentries-incubator/fortran-unit-testing/tree/main/exercises/4-fortran-unit-test-syntax/challenge),
511+
complete task 3.
529512

530513
::: solution
531514

532-
A solution is provided in [3-writing-your-first-unit-test/solution](https://github.com/carpentries-incubator/fortran-unit-testing/tree/main/exercises/3-writing-your-first-unit-test/solution).
515+
A solution is provided in
516+
[4-fortran-unit-test-syntax/solution](https://github.com/carpentries-incubator/fortran-unit-testing/tree/main/exercises/4-fortran-unit-test-syntax/solution).
533517

534518
:::
535519

exercises/3-writing-your-first-unit-test/challenge/README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,6 @@ A file [test_temp_conversions.pf](./test/pfunit/test_temp_conversions.pf) contai
7575
for your pFUnit test(s) has been provided. Comments within this file indicate the aspects of
7676
the pFUnit test you must write.
7777

78-
> Note: This template has been written to facilitate conversion of
79-
> [test_temp_conversions.f90](./test/standard_fortran/test_temp_conversions.f90) as provided with this repo
80-
> to pFUnit. If your version of test_temp_conversions.f90, produced in Part 1, is significantly
81-
> different, You may prefer to use a different structure to the one provided in the template.
82-
8378
#### Building the test
8479

8580
- **i. Build your new test(s) with Make** - A top level [Makefile](./Makefile) has already been provided to build the

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

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,11 @@ module test_temp_conversions
66

77
public
88

9-
!*************************************************************************!
10-
! Derived Types !
11-
!-------------------------------------------------------------------------!
12-
13-
!> Test parameter type to package the test parameters
14-
! Your changes here...
15-
16-
!> Test case type to specify the style of test (paramaterized)
17-
! Your changes here...
18-
19-
!*************************************************************************!
20-
219
contains
2210

2311
!*************************************************************************!
2412
! Test fahrenheit_to_celsius !
2513
!-------------------------------------------------------------------------!
26-
27-
!> Test Suite for tests of fahrenheit_to_celsius
28-
! Your changes here...
29-
3014
!> Test Logic, unit test subroutine for fahrenheit_to_celsius
3115
! Your changes here...
3216

@@ -36,26 +20,9 @@ contains
3620
!*************************************************************************!
3721
! Test celsius_to_kelvin !
3822
!-------------------------------------------------------------------------!
39-
40-
!> Test Suite for tests of celsius_to_kelvin
41-
! Your changes here...
42-
4323
!> Test Logic, unit test subroutine for celsius_to_kelvin
4424
! Your changes here...
4525

4626
!*************************************************************************!
4727

48-
49-
!*************************************************************************!
50-
! Type Constructors !
51-
!-------------------------------------------------------------------------!
52-
53-
!> Constructor for converting test parameters into a test case
54-
! Your changes here...
55-
56-
!> Constructor for converting test parameters into a string
57-
! Your changes here...
58-
59-
!*************************************************************************!
60-
6128
end module test_temp_conversions

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

exercises/4-fortran-unit-test-syntax/challenge/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ project(
88
DESCRIPTION "Conway's game of life"
99
)
1010

11+
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -ffree-line-length-none")
12+
1113
# Define src files
1214
set(SRC_DIR "${PROJECT_SOURCE_DIR}/src")
1315
set(PROJ_SRC_FILES

exercises/4-fortran-unit-test-syntax/challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ cmake --build build
2121

2222
1. Update the [test/CMakeLists.txt](./test/CMakeLists.txt) to build the fully implemented test, **test_evolve_board.pf**.
2323
2. Finish the partially implemented tests in [test](./test/) and ensure they are built via CMake.
24-
- [test_check_for_steady_state.pf](./test/test_check_for_steady_state.pf)
25-
- [test_read_model_from_file.pf](./test/test_read_model_from_file.pf)
24+
1. [test_check_for_steady_state.pf](./test/test_check_for_steady_state.pf)
25+
2. [test_read_model_from_file.pf](./test/test_read_model_from_file.pf)
2626
3. Write a completely new pFUnit test for the subroutine `find_steady_state` in [game_of_life_mod.f90](./src/game_of_life_mod.f90)
2727
and ensure it is built via CMake.

0 commit comments

Comments
 (0)