Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pFUnit 4 for testing #70

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
compiler: [gfortran8, gfortran9, gfortran10]
runs-on: ubuntu-20.04
container:
image: mikolajkowalski/scone-test:${{matrix.compiler}}
image: mikolajkowalski/scone-test:${{matrix.compiler}}_pfu4
steps:
- uses: actions/checkout@v3
- name: CompileAndTest
Expand All @@ -27,7 +27,7 @@ jobs:
build-and-test-debug:
runs-on: ubuntu-20.04
container:
image: mikolajkowalski/scone-test:gfortran10
image: mikolajkowalski/scone-test:gfortran10_pfu4
steps:
- uses: actions/checkout@v3
- name: CompileAndTest
Expand All @@ -41,7 +41,7 @@ jobs:
build-and-test-no-openmp:
runs-on: ubuntu-20.04
container:
image: mikolajkowalski/scone-test:gfortran10
image: mikolajkowalski/scone-test:gfortran10_pfu4
steps:
- uses: actions/checkout@v3
- name: CompileAndTest
Expand Down
121 changes: 32 additions & 89 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
cmake_minimum_required(VERSION 3.10)
project(SCONE)

# Set CMAKE Policies
# CMP0074: Starting with CMake 3.12, all `find_package` commands will use `<package>_ROOT`
# variables as hints for where to search. This policy provides compatibility
# with older projects.
# https://cmake.org/cmake/help/latest/policy/CMP0074.html
cmake_policy(SET CMP0074 NEW)

####################################################################################################
# DEFINE COMPILATION OPTIONS

Expand Down Expand Up @@ -86,7 +93,7 @@ if (BUILD_TESTS)
# Sets PYTHONINTERP_FOUND & PYTHON_EXECUTABLE
find_package(PythonInterp REQUIRED)

find_package(PFUNIT REQUIRED)
find_package(PFUNIT 4.7 REQUIRED)
add_library(pFUnit STATIC IMPORTED)
set_property(TARGET pFUnit PROPERTY IMPORTED_LOCATION ${PFUNIT_LIBRARIES})

Expand Down Expand Up @@ -144,112 +151,48 @@ target_link_libraries(scone.out scone )
####################################################################################################
# COMPILE UNIT TESTS
if(BUILD_TESTS)
enable_testing()

# Create directory in binary_dir for temporary preprocessed test files
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/generated)

# Create required test suites listing file
file(WRITE ${PROJECT_BINARY_DIR}/generated/testSuites.inc "")

include_directories(
${PFUNIT_MOD}
)

# Preprocess collected test files from global property into variable
# Copy files to the build folder

set(PREP_UNIT_TESTS)
get_property(UNIT_TESTS GLOBAL PROPERTY UNIT_TESTS_LIST)

# Make absolute paths relative to source directory
foreach(_testPath IN LISTS UNIT_TESTS)
# OBTAIN EXTENSION
string(REGEX MATCH "\\.[a-zA-Z0-9]+$" extension ${_testPath})

# REMOVE EXTENSION
string(REGEX REPLACE "${extension}$" "" _testPath_temp ${_testPath})

# OBTAIN FILE NAME WITHOUT EXTENSION
string(REGEX MATCH "[a-zA-Z0-9_]+$" testName ${_testPath_temp})

# ADD RULE PREPROCESS ALL TEST FILES TO A SINGLE FOLDER IN BINARY_DIR
add_custom_command(
OUTPUT ${PROJECT_BINARY_DIR}/generated/${testName}${extension}
COMMAND ${PYTHON_EXECUTABLE} ${PFUNIT_PREPROC}/pFUnitParser.py ${_testPath}
${PROJECT_BINARY_DIR}/generated/${testName}${extension}
DEPENDS pFUnit ${_testPath}
COMMENT "Preprocessing test ${testName}"
VERBATIM
)
# APPEND LIST OF ALL PREPROCESSED UNIT TEST FILES & ADD TEST SUITE TO testSuites.inc
set(PREP_UNIT_TESTS ${PREP_UNIT_TESTS} ${PROJECT_BINARY_DIR}/generated/${testName}${extension})
file(APPEND ${PROJECT_BINARY_DIR}/generated/testSuites.inc "ADD_TEST_SUITE(${testName}_suite)\n")
file(RELATIVE_PATH _testPath ${CMAKE_SOURCE_DIR} ${_testPath})
list(APPEND UNIT_TESTS_RELATIVE ${_testPath})
endforeach()

add_executable(unitTests ${PFUNIT_INCLUDE_DIRS}/driver.F90 ${PREP_UNIT_TESTS})
target_link_libraries(unitTests pFUnit scone)
target_include_directories(unitTests PUBLIC ${PROJECT_BINARY_DIR}/generated )
add_pfunit_ctest(unitTests
TEST_SOURCES ${UNIT_TESTS_RELATIVE}
LINK_LIBRARIES scone ${LAPACK_LIBRARIES}
)

# Switch off warnings when compiling tests. Has some unused variables and invalid preprocessor
# directives
# Select GNU compiler and Build Robust Test Suite
set_target_properties(unitTests PROPERTIES COMPILE_FLAGS "-w -DGNU -DBUILD_ROBUST")
# pFUnit may have a bug which causes a unused variable `class(Test), allocatable :: t` be
# present if the suite contains only a TestCase and its methods
# We need to suppress this warning for clarity
target_compile_options(unitTests PRIVATE "-Wno-unused-variable" )

####################################################################################################
# COMPILE INTEGRATION TESTS

# Create directory in binary_dir for temporary preprocessed test files
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/generatedInt)

# Create required test suites listing file
file(WRITE ${PROJECT_BINARY_DIR}/generatedInt/testSuites.inc "")

# Preprocess collected test files from global property into variable
# Copy files to the build folder

set(PREP_INTEGRATION_TESTS)
get_property(INTEGRATION_TESTS GLOBAL PROPERTY INTEGRATION_TESTS_LIST)

# Make absolute paths relative to source directory
foreach(_testPath IN LISTS INTEGRATION_TESTS)
# OBTAIN EXTENSION
string(REGEX MATCH "\\.[a-zA-Z0-9]+$" extension ${_testPath})

# REMOVE EXTENSION
string(REGEX REPLACE "${extension}$" "" _testPath_temp ${_testPath})

# OBTAIN FILE NAME WITHOUT EXTENSION
string(REGEX MATCH "[a-zA-Z0-9_]+$" testName ${_testPath_temp})

# ADD RULE PREPROCESS ALL TEST FILES TO A SINGLE FOLDER IN BINARY_DIR
add_custom_command(
OUTPUT ${PROJECT_BINARY_DIR}/generatedInt/${testName}${extension}
COMMAND ${PYTHON_EXECUTABLE} ${PFUNIT_PREPROC}/pFUnitParser.py ${_testPath}
${PROJECT_BINARY_DIR}/generatedInt/${testName}${extension}
DEPENDS pFUnit ${_testPath}
COMMENT "Preprocessing test ${testName}"
VERBATIM
)
# APPEND LIST OF ALL PREPROCESSED UNIT TEST FILES & ADD TEST SUITE TO testSuites.inc
set(PREP_INTEGRATION_TESTS ${PREP_INTEGRATION_TESTS} ${PROJECT_BINARY_DIR}/generatedInt/${testName}${extension})
file(APPEND ${PROJECT_BINARY_DIR}/generatedInt/testSuites.inc "ADD_TEST_SUITE(${testName}_suite)\n")
file(RELATIVE_PATH _testPath ${CMAKE_SOURCE_DIR} ${_testPath})
list(APPEND INTEGRATION_TESTS_RELATIVE ${_testPath})
endforeach()

add_pfunit_ctest(integrationTests
TEST_SOURCES ${INTEGRATION_TESTS_RELATIVE}
LINK_LIBRARIES scone ${LAPACK_LIBRARIES}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)

add_executable(integrationTests ${PFUNIT_INCLUDE_DIRS}/driver.F90 ${PREP_INTEGRATION_TESTS})
target_link_libraries(integrationTests pFUnit scone)
target_include_directories(integrationTests PUBLIC ${PROJECT_BINARY_DIR}/generatedInt )
# pFUnit may have a bug which causes a unused variable `class(Test), allocatable :: t` be
# present if the suite contains only a TestCase and its methods
# We need to suppress this warning for clarity
target_compile_options(integrationTests PRIVATE "-Wno-unused-variable" )


# Switch off warnings when compiling tests. Has some unused variables and invalid preprocessor
# directives
# Select GNU compiler and Build Robust Test Suite
set_target_properties(integrationTests PROPERTIES COMPILE_FLAGS "-w -DGNU -DBUILD_ROBUST")

##################################################################################################
# Register the test suites with CMake
# NOTE: Integration tests must be run from source directory
enable_testing()
add_test(NAME Unit_Tests COMMAND unitTests)
add_test(NAME Integration_Tests COMMAND integrationTests WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
endif()

####################################################################################################
Expand Down
6 changes: 2 additions & 4 deletions DataStructures/Tests/charMap_test.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module charMap_test
use numPrecision
use charMap_class, only : charMap
use pFUnit_mod
use funit

implicit none

Expand Down Expand Up @@ -39,8 +39,6 @@ module charMap_test
!!
subroutine setUp(this)
class(test_charMap), intent(inout) :: this
integer(shortInt) :: temp
integer(shortInt) :: N, i

! Load entries
call this % map % add(KEY1, VAL1)
Expand Down Expand Up @@ -198,7 +196,7 @@ subroutine testLooping(this)
character(nameLen) :: tKey

! Initialise parameters
KEYS_PAST = "This is not a Key. It's picture of a key!"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?!

KEYS_PAST = "This is not a Key."
counter = 0

! Delete Entry 6 from map
Expand Down
4 changes: 1 addition & 3 deletions DataStructures/Tests/dictParser_iTest.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module dictParser_iTest
use numPrecision
use dictionary_class, only : dictionary
use dictParser_func, only : fileToDict
use pFUnit_mod
use funit
implicit none

contains
Expand All @@ -15,11 +15,9 @@ subroutine testFromFile()
type(dictionary) :: dict
integer(shortInt) :: tempInt
real(defReal) :: tempReal
character(nameLen) :: tempChar
class(dictionary), pointer :: dictPtr
integer(shortInt), dimension(:), allocatable :: tempIntArray
real(defReal), dimension(:), allocatable :: tempRealArray
character(nameLen), dimension(:), allocatable :: tempCharArray

call fileToDict(dict,'./IntegrationTestFiles/testDictionary')

Expand Down
18 changes: 8 additions & 10 deletions DataStructures/Tests/dictParser_test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module dictParser_test
use numPrecision
use dictionary_class, only : dictionary
use dictParser_func, only : charToDict
use pFUnit_mod
use funit
implicit none

contains
Expand All @@ -15,19 +15,17 @@ subroutine testFromChar()
type(dictionary) :: dict
integer(shortInt) :: tempInt
real(defReal) :: tempReal
character(nameLen) :: tempChar
class(dictionary), pointer :: dictPtr
integer(shortInt), dimension(:), allocatable :: tempIntArray
real(defReal), dimension(:), allocatable :: tempRealArray
character(nameLen), dimension(:), allocatable :: tempCharArray
character(*),parameter :: tape = " myInt 7; &
myChar my; &
myReal 1.3; &
weirdFloat 1E-11; &
intArray (1 2 4 5); &
realArray (1.1 2.2 3.4 1E-11); &
charArray (One element ); &
subDict { myInt 3; myReal 3.2; }"
& myChar my; &
& myReal 1.3; &
& weirdFloat 1E-11; &
& intArray (1 2 4 5); &
& realArray (1.1 2.2 3.4 1E-11); &
& charArray (One element ); &
& subDict { myInt 3; myReal 3.2; }"


! Create dictionary
Expand Down
2 changes: 1 addition & 1 deletion DataStructures/Tests/dictionary_test.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module dictionary_test
use numPrecision
use dictionary_class, only : dictionary
use pFUnit_mod
use funit
implicit none

@TestCase
Expand Down
8 changes: 4 additions & 4 deletions DataStructures/Tests/dynArray_test.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module dynArray_test
use numPrecision
use dynArray_class, only : dynIntArray
use pFUnit_mod
use funit

implicit none

Expand Down Expand Up @@ -54,13 +54,13 @@ subroutine testResizeInt()
type(dynIntArray) :: array

call array % resize(2)
@assertLessThanOrEqual(2, array % capacity(),'Resize to 2 from 0')
@assertLessThanOrEqual(2, array % capacity(), message='Resize to 2 from 0')

call array % resize(5)
@assertLessThanOrEqual(5, array % capacity(),'Resize to 5 from 2')
@assertLessThanOrEqual(5, array % capacity(), message='Resize to 5 from 2')

call array % resize(20)
@assertLessThanOrEqual(20, array % capacity(),'Resize to 20 from 5 ')
@assertLessThanOrEqual(20, array % capacity(), message='Resize to 20 from 5 ')

call array % kill()
@assertEqual(0, array % capacity(),'Capacity of a killed array')
Expand Down
4 changes: 1 addition & 3 deletions DataStructures/Tests/intMap_test.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module intMap_test
use numPrecision
use intMap_class, only : intMap
use pFUnit_mod
use funit

implicit none

Expand Down Expand Up @@ -42,8 +42,6 @@ module intMap_test
!!
subroutine setUp(this)
class(test_intMap), intent(inout) :: this
integer(shortInt) :: temp
integer(shortInt) :: N, i

! Load entries
call this % map % add(KEY1, VAL1)
Expand Down
2 changes: 1 addition & 1 deletion Geometry/Cells/Tests/cellShelf_test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module cellShelf_test
use surfaceShelf_class, only : surfaceShelf
use cellShelf_class, only : cellShelf
use cell_inter, only : cell
use pFUnit_mod
use funit

implicit none

Expand Down
2 changes: 1 addition & 1 deletion Geometry/Cells/Tests/simpleCell_test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module simpleCell_test
use dictParser_func, only : charToDict
use surfaceShelf_class, only : surfaceShelf
use simpleCell_class, only : simpleCell
use pFUnit_mod
use funit

implicit none

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module uniformScalarField_test
use field_inter, only : field
use scalarField_inter, only : scalarField, scalarField_CptrCast
use uniformScalarField_class, only : uniformScalarField, uniformScalarField_TptrCast
use pFUnit_mod
use funit

implicit none

Expand Down
10 changes: 5 additions & 5 deletions Geometry/Fields/VectorFields/Tests/uniFissSitesField_test.f90
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module uniFissSitesField_test
use numPrecision
use pFUnit_mod
use funit
use particle_class, only : particle, particleState
use dictionary_class, only : dictionary
use dictParser_func, only : charToDict
Expand All @@ -25,7 +25,7 @@ module uniFissSitesField_test
!!
character(*), parameter :: DICT_DEF = &
" type spaceMap; axis z; grid unstruct; &
bins (0.0 20.0 40.0 60.0 80.0); "
&bins (0.0 20.0 40.0 60.0 80.0); "

contains

Expand Down Expand Up @@ -76,14 +76,14 @@ subroutine testGetValue(this)
class(test_uniFissSitesField), intent(inout) :: this
type(particle) :: p
type(particleState) :: state
integer(shortInt), dimension(3) :: bins, EXPECTED_BINS
real(defReal), dimension(3) :: bins, EXPECTED_BINS

! Test case in the map
p % coords % lvl(1) % r = [0.5, 7.0, 50.0]

bins = this % ufsField % at(p)
EXPECTED_BINS = [0.25, 0.25, 0.0]
@assertEqual(EXPECTED_BINS,bins)
@assertEqual(EXPECTED_BINS, bins, tolerance=1.0e-6)

! Test case outside the map
p % coords % lvl(1) % r = [0.5, 7.0, 100.0]
Expand All @@ -109,7 +109,7 @@ subroutine testGetValue(this)

bins = this % ufsField % at(p)
EXPECTED_BINS = [0.25, 0.06666666667, 0.0]
@assertEqual(EXPECTED_BINS,bins)
@assertEqual(EXPECTED_BINS, bins, tolerance=1.0e-6)

end subroutine testGetValue

Expand Down
Loading