Skip to content

Commit 331e09b

Browse files
committed
Restructure ep 6 and add pfunit task and solution
1 parent ddeff76 commit 331e09b

File tree

14 files changed

+299
-43
lines changed

14 files changed

+299
-43
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
**.mod
44
**build
55
**build-cmake
6-
pfunit
6+
./pfunit
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
2+
3+
# Set project name
4+
project(
5+
"temp_conversions"
6+
LANGUAGES "Fortran"
7+
VERSION "0.0.1"
8+
DESCRIPTION "Library for converting between various temperatures"
9+
)
10+
11+
# Define src file(s)
12+
set(PROJ_SRC_FILES "${PROJECT_SOURCE_DIR}/src/temp_conversions.f90")
13+
14+
enable_testing()
15+
add_subdirectory("test")

episodes/6-writing-your-first-unit-test/standard-fortran/challenge/README.md renamed to episodes/6-writing-your-first-unit-test/challenge/README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ provided are...
1313
- **fahrenheit_to_celsius**: Which takes in a temperature in Fahrenheit and returns a temperature in Celsius.
1414
- **celsius_to_kelvin**: Which takes in a temperature in Celsius and returns a temperature in Kelvin.
1515

16-
## The task
16+
## The tasks
17+
18+
### Part 1 - Test with Standard Fortran
1719

1820
Imagine you wish to use the temp_conversions library to convert Fahrenheit to Kelvin. We
1921
know that there is no function which does this direct conversion. With this is mind, write
@@ -28,3 +30,28 @@ the test was successful. The character array `failure_message`, should be popula
2830
message that will be printed to the terminal in the event that `passed` is `.false.`. Once
2931
the test subroutine is written it should be called within the main body of the test program
3032
as indicated in `test_temp_conversions.f90`.
33+
34+
### Part 2 - Convert tests to use pFUnit
35+
36+
Convert your tests from [Part 1](#part-1---test-with-standard-fortran), to use
37+
[pFUnit](https://github.com/Goddard-Fortran-Ecosystem/pFUnit).
38+
39+
A file [test_temp_conversions.pf](./test/pfunit/test_temp_conversions.pf) containing a template
40+
for your pFUnit test(s) has been provided. Comments within this file indicate the aspects of
41+
the pFUnit test you must write.
42+
43+
> Note: This template has been written to facilitate conversion of
44+
> [test_temp_conversions.f90](./test/test_temp_conversions.f90) as provided with this repo
45+
> to pFUnit. If your version of test_temp_conversions.f90, produced in Part 1, is significantly
46+
> different, You may prefer to use a different structure to the one provided in the template.
47+
48+
To build and run your pFUnit test(s) you must add the pFUnit lib to the `CMAKE_PREFIX_PATH`
49+
when building via the following command.
50+
51+
```bash
52+
cmake -B build -DCMAKE_PREFIX_PATH=/path/to/pfunit/install
53+
cmake --build build
54+
ctest --test-dir build --output-on-failure
55+
```
56+
57+
> If you are using the devcontainer, there is an installation of pFUnit at /home/vscode/pfunit/build/installed

episodes/6-writing-your-first-unit-test/standard-fortran/challenge/src/temp_conversions.f90 renamed to episodes/6-writing-your-first-unit-test/challenge/src/temp_conversions.f90

File renamed without changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
find_package(PFUNIT QUIET)
2+
3+
if (PFUNIT_FOUND)
4+
add_subdirectory("pfunit")
5+
else()
6+
add_subdirectory("standard_fortran")
7+
endif()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.f90
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
message(STATUS "Using pFUnit")
2+
3+
set(TEST_DIR "${PROJECT_SOURCE_DIR}/test/pfunit")
4+
5+
# Create library for src code
6+
add_library (sut STATIC ${PROJ_SRC_FILES})
7+
8+
# List all test files
9+
file(GLOB
10+
PROJ_TEST_FILES
11+
"${TEST_DIR}/*.pf"
12+
)
13+
14+
list(LENGTH test_srcs num_test_srcs)
15+
if (num_test_srcs EQUAL 0)
16+
message(FATAL_ERROR "No .pf files found in: ${TEST_DIR}")
17+
endif()
18+
19+
add_pfunit_ctest (pfunit_test_temp_conversions_exec
20+
TEST_SOURCES ${test_srcs}
21+
LINK_LIBRARIES sut # your application library
22+
)
23+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module test_temp_conversions
2+
use temp_conversions, only : fahrenheit_to_celsius, celsius_to_kelvin
3+
! allow(C121)
4+
use funit
5+
implicit none
6+
7+
public
8+
9+
!> Test parameter type to package the test parameters
10+
! Your changes here...
11+
12+
!> Test case type to specify the style of test (paramaterized)
13+
! Your changes here...
14+
15+
contains
16+
17+
!*************************************************************************!
18+
! Test fahrenheit_to_celsius !
19+
!*************************************************************************!
20+
21+
!> Test suite for tests of fahrenheit_to_celsius
22+
! Your changes here...
23+
24+
!> Unit test subroutine for fahrenheit_to_celsius
25+
! Your changes here...
26+
27+
!*************************************************************************!
28+
! Test celsius_to_kelvin !
29+
!*************************************************************************!
30+
31+
!> Test suite for tests of celsius_to_kelvin
32+
! Your changes here...
33+
34+
!> Unit test subroutine for celsius_to_kelvin
35+
! Your changes here...
36+
37+
!*************************************************************************!
38+
! Constructors !
39+
!*************************************************************************!
40+
41+
!> Constructor for converting test parameters into a test case
42+
! Your changes here...
43+
44+
!> Constructor for converting test parameters into a string
45+
! Your changes here...
46+
47+
end module test_temp_conversions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
message(STATUS "Using standard Fortran")
2+
3+
set(TEST_DIR "${PROJECT_SOURCE_DIR}/test/standard_fortran")
4+
5+
# Define test file(s)
6+
file(GLOB test_srcs "${TEST_DIR}/*.f90")
7+
8+
list(LENGTH test_srcs num_test_srcs)
9+
if (num_test_srcs EQUAL 0)
10+
message(FATAL_ERROR "No .f90 files found in: ${TEST_DIR}")
11+
endif()
12+
13+
# Build test executable
14+
add_executable(test_temp_conversions_exec
15+
"${PROJ_SRC_FILES}"
16+
"${test_srcs}"
17+
)
18+
19+
# Add test as ctest
20+
add_test(NAME test_temp_conversions COMMAND test_temp_conversions_exec)
21+

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

File renamed without changes.

0 commit comments

Comments
 (0)