Skip to content

Commit

Permalink
test: add minimal unit tests for some utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed Nov 15, 2023
1 parent e125e2f commit 48b46b9
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 2 deletions.
28 changes: 28 additions & 0 deletions autotest/TestDevFeature.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module TestDevFeature
use testdrive, only: error_type, unittest_type, new_unittest, check
use DevFeatureModule, only: dev_feature
use ConstantsModule, only: LINELENGTH
use VersionModule, only: IDEVELOPMODE

implicit none
private
public :: collect_dev_feature

contains

subroutine collect_dev_feature(testsuite)
type(unittest_type), allocatable, intent(out) :: testsuite(:)
testsuite = [ &
! expect failure if in release mode, otherwise pass
new_unittest("dev_feature", test_dev_feature, &
should_fail=(IDEVELOPMODE == 0)) &
]
end subroutine collect_dev_feature

subroutine test_dev_feature(error)
type(error_type), allocatable, intent(out) :: error
character(len=LINELENGTH) :: errmsg
call dev_feature(errmsg)
end subroutine test_dev_feature

end module TestDevFeature
63 changes: 63 additions & 0 deletions autotest/TestInputOutput.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module TestInputOutput
use testdrive, only: error_type, unittest_type, new_unittest, check
use ConstantsModule, only: LINELENGTH
use InputOutputModule, only: get_node, get_ijk
implicit none
private
public :: collect_inputoutput

contains

subroutine collect_inputoutput(testsuite)
type(unittest_type), allocatable, intent(out) :: testsuite(:)
testsuite = [ &
new_unittest("get_node_get_ijk", test_get_node_get_ijk) &
]
end subroutine collect_inputoutput

subroutine test_get_node_get_ijk(error)
type(error_type), allocatable, intent(out) :: error
integer :: ilay
integer :: irow
integer :: icol
integer :: nlay
integer :: nrow
integer :: ncol
integer :: nnum
integer :: ncls
integer :: k, i, j

! trivial grid with 1 cell
nnum = get_node(1, 1, 1, 1, 1, 1)
call get_ijk(nnum, 1, 1, 1, ilay, irow, icol)
call check(error, nnum == 1)
call check(error, ilay == 1)
call check(error, irow == 1)
call check(error, icol == 1)
if (allocated(error)) return

! small grid, 3x4x5
nlay = 3
nrow = 4
ncol = 5
ncls = nlay * nrow * ncol
do k = 1, nlay
do i = 1, nrow
do j = 1, ncol
! node number from ijk
nnum = get_node(k, i, j, nlay, nrow, ncol)
call check(error, nnum == (k - 1) * nrow * ncol + (i - 1) * ncol + j)
if (allocated(error)) return

! ijk from node number
call get_ijk(nnum, nrow, ncol, nlay, irow, icol, ilay)
call check(error, ilay == k)
call check(error, irow == i)
call check(error, icol == j)
if (allocated(error)) return
end do
end do
end do
end subroutine test_get_node_get_ijk

end module TestInputOutput
58 changes: 58 additions & 0 deletions autotest/TestSim.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module TestSim
use testdrive, only: error_type, unittest_type, new_unittest, check
use SimModule, only: store_error, store_warning, store_note, &
initial_message, count_errors, count_notes, &
count_warnings
use ConstantsModule, only: LINELENGTH

implicit none
private
public :: collect_sim

contains

subroutine collect_sim(testsuite)
type(unittest_type), allocatable, intent(out) :: testsuite(:)
testsuite = [ &
new_unittest("store_and_count", test_store_and_count) &
]
end subroutine collect_sim

subroutine test_store_and_count(error)
type(error_type), allocatable, intent(out) :: error
character(len=LINELENGTH) :: ntemsg
character(len=LINELENGTH) :: wrnmsg
character(len=LINELENGTH) :: errmsg

! define messages
ntemsg = "NOTE"
wrnmsg = "WARNING"
errmsg = "ERROR"

! initialize message arrays
call initial_message()

! check no messages stored
call check(error, count_errors() == 0)
call check(error, count_warnings() == 0)
call check(error, count_notes() == 0)
if (allocated(error)) return

! todo store a note and check that it's stored
call store_note(ntemsg)
call check(error, count_notes() == 1)
if (allocated(error)) return

! todo store a warning and check that it's stored
call store_warning(wrnmsg)
call check(error, count_warnings() == 1)
if (allocated(error)) return

! store an error and check that it's stored
call store_error(errmsg, terminate=.false.)
call check(error, count_errors() == 1)
if (allocated(error)) return

end subroutine test_store_and_count

end module TestSim
5 changes: 4 additions & 1 deletion autotest/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ test_drive = dependency('test-drive', required : false)
if test_drive.found()
tests = [
'ArrayHandlers',
'GeomUtil'
'DevFeature',
'GeomUtil',
'InputOutput',
'Sim'
]

test_srcs = files(
Expand Down
8 changes: 7 additions & 1 deletion autotest/tester.f90
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ program tester
use testdrive, only: run_testsuite, new_testsuite, testsuite_type, &
& select_suite, run_selected, get_argument
use TestArrayHandlers, only: collect_arrayhandlers
use TestDevFeature, only: collect_dev_feature
use TestGeomUtil, only: collect_geomutil
use TestInputOutput, only: collect_inputoutput
use TestSim, only: collect_sim
implicit none
integer :: stat, is
character(len=:), allocatable :: suite_name, test_name
Expand All @@ -13,7 +16,10 @@ program tester
stat = 0
testsuites = [ &
new_testsuite("ArrayHandlers", collect_arrayhandlers), &
new_testsuite("GeomUtil", collect_geomutil) &
new_testsuite("DevFeature", collect_dev_feature), &
new_testsuite("GeomUtil", collect_geomutil), &
new_testsuite("InputOutput", collect_inputoutput), &
new_testsuite("Sim", collect_sim) &
]

call get_argument(1, suite_name)
Expand Down

0 comments on commit 48b46b9

Please sign in to comment.