-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This directory is to demonstrate how to construct parameterized tests.
- Loading branch information
Showing
13 changed files
with
222 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
# Change Log | ||
|
||
## [1.0.0] - December 19, 2019 | ||
|
||
## [1.1.0] - 2022-06-20 | ||
|
||
### Added | ||
|
||
- Introduced directory to show how to construct parameterized tests with pFunit. | ||
|
||
## [1.0.0] - 2019-12-19 | ||
- Improved Make example to find the lastest installed version of pFUnit. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
add_pfunit_ctest (mpi_tests | ||
TEST_SOURCES test_halo.pf | ||
LINK_LIBRARIES sut | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
cmake_minimum_required(VERSION 3.12) | ||
|
||
project (PFUNIT_DEMO_PARAMETERIZED | ||
VERSION 1.0.0 | ||
LANGUAGES Fortran) | ||
|
||
find_package(PFUNIT REQUIRED) | ||
enable_testing() | ||
|
||
add_subdirectory(src) | ||
add_subdirectory(tests) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
ifeq (nagfor,$(findstring nagfor,$(FC))) | ||
FFLAGS += -fpp | ||
endif | ||
export FFLAGS | ||
|
||
LATEST_PFUNIT_DIR := $(lastword $(shell echo $(wildcard $(PFUNIT_DIR)/PFUNIT-4.*) | xargs -n1 | sort -V)) | ||
include $(LATEST_PFUNIT_DIR)/include/PFUNIT.mk | ||
FFLAGS += $(PFUNIT_EXTRA_FFLAGS) | ||
|
||
all: | ||
$(MAKE) -C src all | ||
$(MAKE) -C tests all | ||
|
||
|
||
%.o : %.F90 | ||
$(FC) -c $(FFLAGS) $< | ||
|
||
|
||
|
||
clean: | ||
$(MAKE) -C src clean | ||
$(MAKE) -C tests clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
One of the hardest aspects of putting parameterized tests in this set | ||
of demos is that it is hard to come up with a motivating example that | ||
is neither trivial nor too complex. I am quite open to suggestions as | ||
having a number of examples would be useful to potential users. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/bash -f | ||
|
||
if [[ -d build ]] | ||
then | ||
rm -rf build | ||
fi | ||
|
||
mkdir -p build | ||
cd build | ||
cmake .. -DCMAKE_PREFIX_PATH=$PFUNIT_DIR | ||
make | ||
|
||
ctest --verbose | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/bash -f | ||
|
||
make clean | ||
|
||
make all | ||
|
||
./tests/trig_tests | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
add_library(sut | ||
Trig.F90 | ||
) | ||
|
||
set_target_properties (sut PROPERTIES | ||
Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) | ||
|
||
target_include_directories(sut PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) | ||
target_include_directories(sut PUBLIC ${MPI_Fortran_INCLUDE_PATH}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
SRCS := Trig.F90 | ||
OBJS := $(SRCS:%.F90=%.o) | ||
|
||
all: libsut.a | ||
|
||
libsut.a: $(OBJS) | ||
$(AR) -r $@ $? | ||
|
||
%.o : %.F90 | ||
$(FC) -c $(FFLAGS) $< | ||
|
||
clean: | ||
$(RM) *.o *.mod *.a | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module Trig_mod | ||
implicit none | ||
private | ||
|
||
public :: compute_hypotenuse | ||
|
||
contains | ||
|
||
! Simple implementation of function that computes the hypotenuse of | ||
! a right triangle given legs a and b. | ||
pure function compute_hypotenuse(a, b) result(c) | ||
real :: c | ||
real, intent(in) :: a, b | ||
|
||
c = sqrt(a**2 + b**2) | ||
end function compute_hypotenuse | ||
|
||
end module Trig_mod |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
add_pfunit_ctest (trig_tests | ||
TEST_SOURCES test_trig.pf | ||
LINK_LIBRARIES sut | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
LATEST_PFUNIT_DIR := $(lastword $(shell echo $(wildcard $(PFUNIT_DIR)/PFUNIT-4.*) | xargs -n1 | sort -V)) | ||
include $(LATEST_PFUNIT_DIR)/include/PFUNIT.mk | ||
|
||
all: trig_tests | ||
|
||
%.o : %.F90 | ||
$(FC) -c $(FFLAGS) $< | ||
|
||
FFLAGS += $(PFUNIT_EXTRA_FFLAGS) | ||
FFLAGS += -I../src | ||
|
||
trig_tests := test_trig.pf | ||
test_simple_OTHER_LIBRARIES := -L../src -lsut | ||
$(eval $(call make_pfunit_test,trig_tests)) | ||
|
||
clean: | ||
$(RM) *.o *.mod *.a *.inc | ||
$(RM) trig_tests test_trig.F90 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
module test_trig | ||
use FUnit ! not FUnit | ||
use Trig_mod | ||
implicit none | ||
|
||
@testParameter | ||
type, extends(AbstractTestParameter) :: PythagoreanTriple | ||
real :: a | ||
real :: b | ||
real :: c | ||
contains | ||
procedure :: toString | ||
end type PythagoreanTriple | ||
|
||
@testCase(constructor=Test_Hypotenuse, testParameters={getParameters()}) | ||
type, extends(ParameterizedTestCase) :: Test_Hypotenuse | ||
type(PythagoreanTriple) :: sides | ||
contains | ||
procedure :: setUp | ||
end type Test_Hypotenuse | ||
|
||
interface Test_Hypotenuse | ||
module procedure newTest_Hypotenuse | ||
end interface Test_Hypotenuse | ||
|
||
contains | ||
|
||
! This empty constructor is only necessary due to a bug in the | ||
! pfunit preprocessor script. | ||
function newTest_Hypotenuse(testParameter) result(aTest) | ||
type (Test_Hypotenuse) :: aTest | ||
class (PythagoreanTriple), intent(in) :: testParameter | ||
end function newTest_Hypotenuse | ||
|
||
|
||
function getParameters() result(params) | ||
type (PythagoreanTriple), allocatable :: params(:) | ||
|
||
params = [ & | ||
PythagoreanTriple(3., 4., 5.), & | ||
PythagoreanTriple(5.,12.,13.), & | ||
PythagoreanTriple(8.,15.,17.) ] | ||
|
||
end function getParameters | ||
|
||
subroutine setUp(this) | ||
class (Test_Hypotenuse), intent(inout) :: this | ||
|
||
! Have to cast the frameworks' testParamater into the subclass | ||
! used by the test. This could be done inside each test, but it | ||
! is usually simpler to do it once in the setup. | ||
select type (p => this%testParameter) | ||
type is (PythagoreanTriple) | ||
this%sides = p | ||
end select | ||
end subroutine setUp | ||
@test | ||
subroutine test_pythagorean_theorem(this) | ||
class(Test_Hypotenuse), intent(inout) :: this | ||
associate (a => this%sides%a, b => this%sides%b, c => this%sides%c ) | ||
@assert_that(compute_hypotenuse(a,b), is(Near(c,tolerance=0.0001))) | ||
end associate | ||
end subroutine test_pythagorean_theorem | ||
! This function is used by pFUnit to label failing cases. Otherwise one cannot | ||
! easily ascertain which specific cases failed. | ||
function tostring(this) result(s) | ||
character(:), allocatable :: s | ||
class(PythagoreanTriple), intent(in) :: this | ||
character(20) :: buffer | ||
write(buffer,'(f4.0)') this%a | ||
s = '[ '// trim(buffer) // ', ' | ||
write(buffer,'(f4.0)') this%b | ||
s = s // trim(buffer) // ', ' | ||
write(buffer,'(f4.0)') this%c | ||
s = s // trim(buffer) // ' ]' | ||
end function tostring | ||
end module test_trig |