numerical_testing_type module reference
A simple type for keeping track of numerical tests
Data Types
Class to assist in unit tests, not to be used outside of Recon1d types |
Functions/Subroutines
Update the state with "test" |
|
Set attributes |
|
Summarize results |
|
Compare u_test to u_true, report, and return true if a difference larger than tol is measured |
|
Compare u_test to u_true, report, and return true if a difference larger than tol is measured |
|
Compare i_test to i_true and report and return true if a difference is found |
|
Tests the testing type itself |
Detailed Description
numerical_testing_type is a helper class to facilitate implementing tests of a numerical nature. The class helps hide the logic and code associated with handling the results of a test, essentially reducing the multiple lines of if ... then ... print ... else ... error_mesg ... into one line.
The class is light weight, meaning is does not depend on anything else, allowing to be particularly useful in unit tests and small drivers. However, this means it is up to the user to do something with the results, e.g. call MOM_error() appropriately.
Each test, e.g. real_scalar(), is expected to pass. If a fail is encountered, it is immediately reported to stderr and stdour, recorded internally, but does not terminate execuation unless set(stop_instantly=.true.) was called previously. Most tests take the form of f(a,b) where a should equal b. Only test() takes a single input (boolean) which is expected to be false for the test to pass.
summarize() is used to "finalize" the tests. It prints a summary of how many and which tests faield, and returns a logical that is set to .true. if any test failed.
Usage by example:
use numerical_testing_type, only : testing
...
!> Runs my unit_tests. Returns .true. if a test fails, .false. otherwise
logical function my_unit_tests(verbose)
logical, intent(in) :: verbose !< If true, write results to stdout
...
type(testing) :: test ! An instance of the numerical_testing_type
...
call test%set( verbose=.true. ) ! Show intermediate results rather than just the fails
...
call test%test(flag, 'Flag is not set') ! Check flag=.false.
call test%real_scalar(a, 1., 'u = 1') ! Check a=1
call test%real_arr(3, u, (/1.,2.,3./), 'u = [1,2,3]') ! Check u(:)=[1,2,3]
call test%int_arr(2, iv, (/1,2/), 'iv = [1,2]') ! Check that iv(:)=[1,2]
my_unit_tests = test%summarize('my_unit_tests') ! Return true if a fail occurs
end function my_unit_tests(verbose)
Type Documentation
- type :: testing
Class to assist in unit tests, not to be used outside of Recon1d types
- Parameters
state
Count of tests checked
num_tests_checked
Count of tests failed
num_tests_failed
If true, be verbose and write results to stdout. Default True.
verbose
Error channel
stderr
Standard output channel
stdout
If true, stop instantly
stop_instantly
If true, ignore fails until ignore_fail=.false.
ignore_fail
Record instances that fail
ifailed
Record label of first instance that failed
Functions/Subroutine Documentation
- subroutine test(this, state, label, ignore)
Update the state with "test"
- Parameters
this [inout]
This testing class
state [in]
True to indicate a fail, false otherwise
label [in]
Message
ignore [in]
If present and true, ignore a fail
- subroutine set(this, verbose, stdout, stderr, stop_instantly, ignore_fail)
Set attributes
- Parameters
this [inout]
This testing class
verbose [in]
True or false setting to assign to verbosity
stdout [in]
The stdout channel to use
stderr [in]
The stderr channel to use
stop_instantly [in]
If true, stop immediately on error detection
ignore_fail [in]
If true, ignore fails until this option is set false
- logical function summarize(this, label)
Summarize results
- Parameters
this [inout]
This testing class
label [in]
Message
- subroutine real_scalar(this, u_test, u_true, label, tol, robits, ignore)
Compare u_test to u_true, report, and return true if a difference larger than tol is measured
If in verbose mode, display results to stdout If a difference is measured, display results to stdout and stderr
- Parameters
this [inout]
This testing class
u_test [in]
Value to test [A]
u_true [in]
Value to test against (correct answer) [A]
label [in]
Message
tol [in]
The tolerance for differences between u and u_true [A]
robits [in]
Number of bits of round-off to allow
ignore [in]
If present and true, ignore a fail
- subroutine real_arr(this, n, u_test, u_true, label, tol, robits, ignore)
Compare u_test to u_true, report, and return true if a difference larger than tol is measured
If in verbose mode, display results to stdout If a difference is measured, display results to stdout and stderr
- Parameters
this [inout]
This testing class
n [in]
Number of cells in u
u_test [in]
Values to test [A]
u_true [in]
Values to test against (correct answer) [A]
label [in]
Message
tol [in]
The tolerance for differences between u and u_true [A]
robits [in]
Number of bits of round-off to allow
ignore [in]
If present and true, ignore a fail
- subroutine int_arr(this, n, i_test, i_true, label, ignore)
Compare i_test to i_true and report and return true if a difference is found
If in verbose mode, display results to stdout If a difference is measured, display results to stdout and stderr
- Parameters
this [inout]
This testing class
n [in]
Number of cells in u
i_test [in]
Values to test [A]
i_true [in]
Values to test against (correct answer) [A]
label [in]
Message
ignore [in]
If present and true, ignore a fail
- logical function numerical_testing_type_unit_tests(verbose)
Tests the testing type itself
- Parameters
verbose [in]
If true, write results to stdout