GRASP rci-qed
Data Types | Functions/Subroutines
grasptest_testing Module Reference

Various handy routines useful for writing unit tests. More...

Data Types

interface  test_isequal
 

Functions/Subroutines

real(real64) function reldiff (a, b)
 Calculate the relative difference of a and b. More...
 
logical function within_tolerance (a, b, relative_tolerance)
 Checks if the difference of a and b are within the tolerance relative to \(\max(|a|,|b|)\). More...
 
subroutine test_isequal_real64 (test_passed, which, a, b, relative_tolerance)
 Tests if two floating point values are the same withing the specified relative tolerance. More...
 
subroutine test_isequal_logical (test_passed, which, a, b)
 Tests if two logical values are equal. More...
 
subroutine test_isequal_integer (test_passed, which, a, b)
 Tests if two integer values are equal. More...
 
subroutine test_isequal_atol (test_passed, which, a, b, absolute_tolerance)
 Tests if two floating point values are the same withing the specified absolute tolerance. More...
 

Detailed Description

Various handy routines useful for writing unit tests.

Function/Subroutine Documentation

◆ reldiff()

real(real64) function grasptest_testing::reldiff ( real(real64), intent(in)  a,
real(real64), intent(in)  b 
)

Calculate the relative difference of a and b.

The relative difference is defined as:

\[ \frac{|a-b|}{\max(|a|, |b|)} \]

Parameters
a,bInput values.
Returns
The relative difference of a and b.

◆ test_isequal_atol()

subroutine grasptest_testing::test_isequal_atol ( logical, intent(inout)  test_passed,
character(*), intent(in)  which,
real(real64), intent(in)  a,
real(real64), intent(in)  b,
real(real64), intent(in)  absolute_tolerance 
)

Tests if two floating point values are the same withing the specified absolute tolerance.

Specifically, it tests that

\[ |a-b| < \sigma \]

where \(\sigma\) is the specified absolute tolerance.

If the test fails, test_passed gets set to .false., but the value is unchanged if the tests pass. This allows for the pattern where the test function is called multiple times before checking if any of the tests failed:

call test_isequal_atol(success, "T1", a1, b1, atol)
call test_isequal_atol(success, "T2", a2, b2, atol)
if(.not.success) then
print *, "Test failures occurred."
endif

On failure, a message gets printed into the standard output that a test failed, which also includes the which string, allowing for the failed test to be identified easily.

Parameters
test_passedGets set to .false. if the test fails, and is left unchanged otherwise.
whichA string that identifies the test.
a,bValues to be compared.
absolute_toleranceThe absolute tolerance \(\sigma\).
Here is the caller graph for this function:

◆ test_isequal_integer()

subroutine grasptest_testing::test_isequal_integer ( logical, intent(inout)  test_passed,
character(*), intent(in)  which,
integer, intent(in)  a,
integer, intent(in)  b 
)

Tests if two integer values are equal.

◆ test_isequal_logical()

subroutine grasptest_testing::test_isequal_logical ( logical, intent(inout)  test_passed,
character(*), intent(in)  which,
logical, intent(in)  a,
logical, intent(in)  b 
)

Tests if two logical values are equal.

◆ test_isequal_real64()

subroutine grasptest_testing::test_isequal_real64 ( logical, intent(inout)  test_passed,
character(*), intent(in)  which,
real(real64), intent(in)  a,
real(real64), intent(in)  b,
real(real64), intent(in)  relative_tolerance 
)

Tests if two floating point values are the same withing the specified relative tolerance.

Specifically, it tests that

\[ \frac{|a-b|}{\max(|a|, |b|)} < \sigma \]

where \(\sigma\) is the specified relative tolerance.

If the test fails, test_passed gets set to .false., but the value is unchanged if the tests pass. This allows for the pattern where the test function is called multiple times before checking if any of the tests failed:

call test_isequal(success, "T1", a1, b1, rtol)
call test_isequal(success, "T2", a2, b2, rtol)
if(.not.success) then
print *, "Test failures occurred."
endif

On failure, a message gets printed into the standard output that a test failed, which also includes the which string, allowing for the failed test to be identified easily.

Parameters
test_passedGets set to .false. if the test fails, and is left unchanged otherwise.
whichA string that identifies the test.
a,bValues to be compared.
relative_toleranceThe relative tolerance \(\sigma\).
Here is the call graph for this function:

◆ within_tolerance()

logical function grasptest_testing::within_tolerance ( real(real64), intent(in)  a,
real(real64), intent(in)  b,
real(real64), intent(in)  relative_tolerance 
)

Checks if the difference of a and b are within the tolerance relative to \(\max(|a|,|b|)\).

Parameters
a,bValues to be checked.
relative_toleranceRelative tolerance \(\sigma\).
Returns
Whether \(|a-b| / \max(|a|,|b|) < \sigma\).