Skip to content

Commit

Permalink
Introduce Parameterized directory.
Browse files Browse the repository at this point in the history
This directory is to demonstrate how to construct parameterized tests.
  • Loading branch information
tclune committed Jun 20, 2022
1 parent df02c37 commit 9bd45ff
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 3 deletions.
10 changes: 8 additions & 2 deletions ChangeLog.md
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.
1 change: 0 additions & 1 deletion MPI/tests/CMakeLists.txt
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
Expand Down
11 changes: 11 additions & 0 deletions Parameterized/CMakeLists.txt
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)
23 changes: 23 additions & 0 deletions Parameterized/Makefile
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
4 changes: 4 additions & 0 deletions Parameterized/README.md
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.
14 changes: 14 additions & 0 deletions Parameterized/build_with_cmake_and_run.x
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

9 changes: 9 additions & 0 deletions Parameterized/build_with_make_and_run.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash -f

make clean

make all

./tests/trig_tests


10 changes: 10 additions & 0 deletions Parameterized/src/CMakeLists.txt
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})

14 changes: 14 additions & 0 deletions Parameterized/src/Makefile
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

18 changes: 18 additions & 0 deletions Parameterized/src/Trig.F90
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
5 changes: 5 additions & 0 deletions Parameterized/tests/CMakeLists.txt
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
)

19 changes: 19 additions & 0 deletions Parameterized/tests/Makefile
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

87 changes: 87 additions & 0 deletions Parameterized/tests/test_trig.pf
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

0 comments on commit 9bd45ff

Please sign in to comment.