numerical_testing_type module reference

A simple type for keeping track of numerical tests.

More…

Data Types

testing

Class to assist in unit tests, not to be used outside of Recon1d types.

Functions/Subroutines

test()

Update the state with “test”.

set()

Set attributes.

summarize()

Summarize results.

real_scalar()

Compare u_test to u_true, report, and return true if a difference larger than tol is measured.

real_arr()

Compare u_test to u_true, report, and return true if a difference larger than tol is measured.

int_arr()

Compare i_test to i_true and report and return true if a difference is found.

numerical_testing_type_unit_tests()

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 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 , 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.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.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 numerical_testing_type/testing

Class to assist in unit tests, not to be used outside of Recon1d types.

Type fields:
  • % state [logical,private] :: True if any fail has been encountered since this instance of “testing” was created.

  • % num_tests_checked [integer,private] :: Count of tests checked.

  • % num_tests_failed [integer,private] :: Count of tests failed.

  • % verbose [logical,private] :: If true, be verbose and write results to stdout. Default True.

  • % stop_instantly [logical,private] :: If true, stop instantly.

  • % ignore_fail [logical,private] :: If true, ignore fails until ignore_fail=.false.

  • % ifailed [integer(100),private] :: Record instances that fail.

  • % label_first_fail [character (len=:),allocatable, private] :: Record label of first instance that failed.

  • % stderr [integer,public] :: Error channel.

  • % stdout [integer,public] :: Standard output channel.

  • % test [procedure,private] :: Update the testing state.

  • % set [procedure,private] :: Set attributes.

  • % summarize [procedure,private] :: Summarize testing state.

  • % real_scalar [procedure,private] :: Compare two reals.

  • % real_arr [procedure,private] :: Compare array of reals.

  • % int_arr [procedure,private] :: Compare array of integers.

Function/Subroutine Documentation

subroutine numerical_testing_type/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

Called from:

numerical_testing_type_unit_tests

subroutine numerical_testing_type/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

function numerical_testing_type/summarize(this, label) [logical]

Summarize results.

Parameters:
  • this :: [inout] This testing class

  • label :: [in] Message

subroutine numerical_testing_type/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 numerical_testing_type/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 numerical_testing_type/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

function numerical_testing_type/numerical_testing_type_unit_tests(verbose) [logical]

Tests the testing type itself.

Parameters:

verbose :: [in] If true, write results to stdout

Call to:

test