Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Namespaces | Macros | Functions
Teuchos_TestingHelpers.hpp File Reference

Utilities to make writing tests easier. More...

#include "Teuchos_ConfigDefs.hpp"
#include "Teuchos_ScalarTraits.hpp"
#include "Teuchos_TypeNameTraits.hpp"
#include "Teuchos_FancyOStream.hpp"
Include dependency graph for Teuchos_TestingHelpers.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Teuchos::RelErrSmallNumber< hasMachineParameters, Scalar >
 
class  Teuchos::RelErrSmallNumber< false, Scalar >
 
class  Teuchos::RelErrSmallNumber< true, Scalar >
 
struct  Teuchos::TestRelErr< T1, T2, Enabled >
 Compute, check and optionally print the relative error in two scalars. More...
 

Namespaces

 Teuchos
 

Macros

#define TEUCHOS_PASS_FAIL(RESULT)   Teuchos::passfail_with_location((RESULT), __FILE__, __LINE__)
 Macro that prints "passed" or "failed" and optionally prints the file name and line number as well. More...
 
#define TEUCHOS_ECHO(statement, out)
 Echo a statement and then invoke it. More...
 
#define TEUCHOS_TEST_EQUALITY_CONST(v1, v2, out, success)
 Test that an object is equal to a given constant. More...
 
#define TEUCHOS_TEST_ASSERT(v1, out, success)
 Assert that a give object is true. More...
 
#define TEUCHOS_TEST_EQUALITY(v1, v2, out, success)
 Test that two values are equal. More...
 
#define TEUCHOS_TEST_INEQUALITY_CONST(v1, v2, out, success)
 Test that an object is not equal to a given constant. More...
 
#define TEUCHOS_TEST_INEQUALITY(v1, v2, out, success)
 Test that two values are not equal. More...
 
#define TEUCHOS_TEST_FLOATING_EQUALITY(v1, v2, tol, out, success)
 Test if two floating point values are equal to a given tolerance. More...
 
#define TEUCHOS_TEST_ITER_EQUALITY(iter1, iter2, out, success)
 Test if two iterators are equal. More...
 
#define TEUCHOS_TEST_ITER_INEQUALITY(iter1, iter2, out, success)
 Test if two iterators are NOT equal. More...
 
#define TEUCHOS_TEST_ARRAY_ELE_EQUALITY(a, i, val, printPass, out, success)
 Test that an array element value is equal to a given constant. More...
 
#define TEUCHOS_TEST_ARRAY_ELE_INEQUALITY(a, i, val, printPass, out, success)
 Test that an array element value is not equal to a given constant. More...
 
#define TEUCHOS_TEST_MATRIX_ELE_FLOATING_EQUALITY(a, i, j, val, tol, printPass, out, success)
 Test if a floating-point array element value is equal to a given constant for a given tolerance. More...
 
#define TEUCHOS_TEST_MATRIX_ELE_EQUALITY(a, i, j, val, printPass, out, success)
 Test if a matrix element value is equal to a given constant. More...
 
#define TEUCHOS_TEST_COMPARE(v1, comp, v2, out, success)
 Compare two objects using an input comparsion operator. More...
 
#define TEUCHOS_TEST_COMPARE_CONST(v1, comp, v2, out, success)
 Compare an object and a constant using an input comparsion operator. More...
 
#define TEUCHOS_TEST_THROW(code, ExceptType, out, success)
 Test that the chunk of code 'code' throws an expected exception. More...
 
#define TEUCHOS_TEST_NOTHROW(code, out, success)
 Test that a chunk of code does not throw any exceptions. More...
 

Functions

void Teuchos::updateSuccess (const bool result, bool &success)
 Update the success bool flag. More...
 
const std::string Teuchos::passfail (const bool result)
 Return "passed" or "failed". More...
 
TEUCHOSCORE_LIB_DLL_EXPORT
const std::string 
Teuchos::passfail_with_location (const bool result, const std::string &file, const int lineNumber)
 Helper function for TEUCHOS_PASS_FAIL(...). More...
 
void Teuchos::showTestFailureLocation (bool)
 Set if TEUCHOS_PASS_FAIL(...) should print test failure location. More...
 
bool Teuchos::showTestFailureLocation ()
 Return if TEUCHOS_PASS_FAIL(...) should print test failure location. More...
 
template<class Scalar >
Scalar Teuchos::defaultSmallNumber ()
 
template<class Scalar1 , class Scalar2 >
Teuchos::ScalarTraits
< typename std::common_type
< Scalar1, Scalar2 >::type >
::magnitudeType 
Teuchos::relErr (const Scalar1 &s1, const Scalar2 &s2)
 Return relative error of two scalars. More...
 
template<typename T1 , typename T2 >
bool Teuchos::testRelErr (const std::string &v1_name, const T1 &v1, const std::string &v2_name, const T2 &v2, const std::string &maxRelErr_error_name, const typename TestRelErr< T1, T2 >::magnitudeType &maxRelErr_error, const std::string &maxRelErr_warning_name, const typename TestRelErr< T1, T2 >::magnitudeType &maxRelErr_warning, const Ptr< std::ostream > &out)
 
template<class Array1 , class Array2 >
bool Teuchos::compareArrays (const Array1 &a1, const std::string &a1_name, const Array2 &a2, const std::string &a2_name, Teuchos::FancyOStream &out)
 Compare if two array objects are the same or not. More...
 
template<class Array1 , class Array2 , class ScalarMag >
bool Teuchos::compareFloatingArrays (const Array1 &a1, const std::string &a1_name, const Array2 &a2, const std::string &a2_name, const ScalarMag &tol, Teuchos::FancyOStream &out)
 Compare if two array objects are the same or not up to a relative floating point precision. More...
 
template<class Array1 , class Array2 , class ScalarMag >
bool Teuchos::compareFloatingArraysAbsolute (const Array1 &a1, const std::string &a1_name, const Array2 &a2, const std::string &a2_name, const ScalarMag &tol, Teuchos::FancyOStream &out)
 Compare if two array objects are the same up to an absolute tolerance: elements a1[i] and a2[i] are considered the same if |a1[i]-a2[i]| <= tol. More...
 

Detailed Description

Utilities to make writing tests easier.

Definition in file Teuchos_TestingHelpers.hpp.

Macro Definition Documentation

#define TEUCHOS_PASS_FAIL (   RESULT)    Teuchos::passfail_with_location((RESULT), __FILE__, __LINE__)

Macro that prints "passed" or "failed" and optionally prints the file name and line number as well.

Only prints the file name and line number if Teuchos::showTestFailureLocation() == true.

Definition at line 270 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_ECHO (   statement,
  out 
)
Value:
(out) << #statement ";\n"; \
statement;

Echo a statement and then invoke it.

This macro is not complicated so take a look for yourself!

Definition at line 280 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_EQUALITY_CONST (   v1,
  v2,
  out,
  success 
)
Value:
{ \
(out) << #v1" = "<<Teuchos::toString(v1)<<" == "<<Teuchos::toString(v2)<<" : "; \
const bool l_result = (v1) == (v2); \
(out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
if (!l_result) (success) = false; \
}
std::string toString(const HashSet< Key > &h)
#define TEUCHOS_PASS_FAIL(RESULT)
Macro that prints &quot;passed&quot; or &quot;failed&quot; and optionally prints the file name and line number as well...

Test that an object is equal to a given constant.

This macro is not complicated so take a look for yourself!

Definition at line 290 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_ASSERT (   v1,
  out,
  success 
)
Value:
{ \
const bool l_result = v1; \
(out) << #v1" = "<<l_result<<" == true : "; \
(out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
if (!l_result) (success) = false; \
}
#define TEUCHOS_PASS_FAIL(RESULT)
Macro that prints &quot;passed&quot; or &quot;failed&quot; and optionally prints the file name and line number as well...

Assert that a give object is true.

This macro is not complicated so take a look for yourself!

Definition at line 304 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_EQUALITY (   v1,
  v2,
  out,
  success 
)
Value:
{ \
(out) << #v1" = "<<Teuchos::toString(v1)<<" == "#v2" = "<<Teuchos::toString(v2)<<" : "; \
const bool l_result = (v1) == (v2); \
if (!l_result) (success) = false; \
(out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
}
std::string toString(const HashSet< Key > &h)
#define TEUCHOS_PASS_FAIL(RESULT)
Macro that prints &quot;passed&quot; or &quot;failed&quot; and optionally prints the file name and line number as well...

Test that two values are equal.

This macro is not complicated so take a look for yourself!

Definition at line 318 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_INEQUALITY_CONST (   v1,
  v2,
  out,
  success 
)
Value:
{ \
(out) << #v1" = "<<Teuchos::toString(v1)<<" != "<<Teuchos::toString(v2)<<" : "; \
const bool l_result = (v1) != (v2); \
(out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
if (!l_result) (success) = false; \
}
std::string toString(const HashSet< Key > &h)
#define TEUCHOS_PASS_FAIL(RESULT)
Macro that prints &quot;passed&quot; or &quot;failed&quot; and optionally prints the file name and line number as well...

Test that an object is not equal to a given constant.

This macro is not complicated so take a look for yourself!

Definition at line 333 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_INEQUALITY (   v1,
  v2,
  out,
  success 
)
Value:
{ \
(out) << #v1" = "<<Teuchos::toString(v1)<<" != "#v2" = "<<Teuchos::toString(v2)<<" : "; \
const bool l_result = (v1) != (v2); \
if (!l_result) (success) = false; \
(out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
}
std::string toString(const HashSet< Key > &h)
#define TEUCHOS_PASS_FAIL(RESULT)
Macro that prints &quot;passed&quot; or &quot;failed&quot; and optionally prints the file name and line number as well...

Test that two values are not equal.

This macro is not complicated so take a look for yourself!

Definition at line 348 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_FLOATING_EQUALITY (   v1,
  v2,
  tol,
  out,
  success 
)
Value:
{ \
const bool l_result = Teuchos::testRelErr( \
#v1, v1, #v2, v2, "tol", tol, "tol", tol, Teuchos::outArg(out) ); \
if (!l_result) (success) = false; \
}
bool testRelErr(const std::string &v1_name, const T1 &v1, const std::string &v2_name, const T2 &v2, const std::string &maxRelErr_error_name, const typename TestRelErr< T1, T2 >::magnitudeType &maxRelErr_error, const std::string &maxRelErr_warning_name, const typename TestRelErr< T1, T2 >::magnitudeType &maxRelErr_warning, const Ptr< std::ostream > &out)

Test if two floating point values are equal to a given tolerance.

This macro is not complicated so take a look for yourself!

Definition at line 363 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_ITER_EQUALITY (   iter1,
  iter2,
  out,
  success 
)
Value:
{ \
(out) << #iter1" == "#iter2" = : "; \
const bool l_result = (iter1) == (iter2); \
if (!l_result) (success) = false; \
(out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
}
#define TEUCHOS_PASS_FAIL(RESULT)
Macro that prints &quot;passed&quot; or &quot;failed&quot; and optionally prints the file name and line number as well...

Test if two iterators are equal.

This macro does not try to print the iterators so it is more portable (in terms of types).

This macro is not complicated so take a look for yourself!

Definition at line 380 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_ITER_INEQUALITY (   iter1,
  iter2,
  out,
  success 
)
Value:
{ \
(out) << #iter1" != "#iter2" = : "; \
const bool l_result = (iter1) != (iter2); \
if (!l_result) (success) = false; \
(out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
}
#define TEUCHOS_PASS_FAIL(RESULT)
Macro that prints &quot;passed&quot; or &quot;failed&quot; and optionally prints the file name and line number as well...

Test if two iterators are NOT equal.

This macro does not try to print the iterators so it is more portable (in terms of types).

This macro is not complicated so take a look for yourself!

Definition at line 398 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_ARRAY_ELE_EQUALITY (   a,
  i,
  val,
  printPass,
  out,
  success 
)
Value:
{ \
const bool l_result = ( (a)[i] == (val) ); \
if (!l_result) (success) = false; \
if (printPass || !(l_result)) { \
out << #a"["<<i<<"] = " << Teuchos::toString((a)[i]) << " == "#val" = " << Teuchos::toString(val) \
<< " : " << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
} \
}
std::string toString(const HashSet< Key > &h)
#define TEUCHOS_PASS_FAIL(RESULT)
Macro that prints &quot;passed&quot; or &quot;failed&quot; and optionally prints the file name and line number as well...

Test that an array element value is equal to a given constant.

This macro is not complicated so take a look for yourself!

Definition at line 413 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_ARRAY_ELE_INEQUALITY (   a,
  i,
  val,
  printPass,
  out,
  success 
)
Value:
{ \
const bool l_result = ( (a)[i] != (val) ); \
if (!l_result) (success) = false; \
if (printPass || !(l_result)) { \
out << #a"["<<i<<"] = " << Teuchos::toString((a)[i]) << " != "#val" = " << Teuchos::toString(val) \
<< " : " << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
} \
}
std::string toString(const HashSet< Key > &h)
#define TEUCHOS_PASS_FAIL(RESULT)
Macro that prints &quot;passed&quot; or &quot;failed&quot; and optionally prints the file name and line number as well...

Test that an array element value is not equal to a given constant.

This macro is not complicated so take a look for yourself!

Definition at line 430 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_MATRIX_ELE_FLOATING_EQUALITY (   a,
  i,
  j,
  val,
  tol,
  printPass,
  out,
  success 
)
Value:
{ \
std::ostringstream a_i_str; \
a_i_str <<#a<<"("<<i<<","<<j<<")"; \
const bool l_result = Teuchos::testRelErr( \
a_i_str.str(), (a)(i,j), #val, val, "tol", tol, "tol", tol, \
(printPass) ? Teuchos::outArg(out) : Teuchos::null ); \
if (!l_result) (success) = false; \
}
bool testRelErr(const std::string &v1_name, const T1 &v1, const std::string &v2_name, const T2 &v2, const std::string &maxRelErr_error_name, const typename TestRelErr< T1, T2 >::magnitudeType &maxRelErr_error, const std::string &maxRelErr_warning_name, const typename TestRelErr< T1, T2 >::magnitudeType &maxRelErr_warning, const Ptr< std::ostream > &out)

Test if a floating-point array element value is equal to a given constant for a given tolerance.

This macro is not complicated so take a look for yourself!

Definition at line 448 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_MATRIX_ELE_EQUALITY (   a,
  i,
  j,
  val,
  printPass,
  out,
  success 
)
Value:
{ \
const bool l_result = ( (a)(i,j) == (val) ); \
if (!l_result) (success) = false; \
if (printPass || !(l_result)) { \
out << #a"("<<i<<","<<j<<") = " << (a)(i,j) << " == "#val" = " << (val) \
<< " : " << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
} \
}
#define TEUCHOS_PASS_FAIL(RESULT)
Macro that prints &quot;passed&quot; or &quot;failed&quot; and optionally prints the file name and line number as well...

Test if a matrix element value is equal to a given constant.

This macro is not complicated so take a look for yourself!

Definition at line 465 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_COMPARE (   v1,
  comp,
  v2,
  out,
  success 
)
Value:
{ \
out << #v1" = "<<(v1)<<" "#comp" "#v2" = "<<(v2)<<" : "; \
const bool l_result = (v1) comp (v2); \
if (!l_result) (success) = false; \
(out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
}
#define TEUCHOS_PASS_FAIL(RESULT)
Macro that prints &quot;passed&quot; or &quot;failed&quot; and optionally prints the file name and line number as well...
int comp(BigUInt< n > const &a, BigUInt< n > const &b)

Compare two objects using an input comparsion operator.

The test succeeds (passes) if and only if "(v1) comp (v2)". For example, TEUCHOS_TEST_COMPARE( 2, <, 3, out, success ) succeeds, but TEUCHOS_TEST_COMPARE( 2, >, 3, out, success ) and TEUCHOS_TEST_COMPARE( 3, <, 2, out, success ) both fail.

Definition at line 485 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_COMPARE_CONST (   v1,
  comp,
  v2,
  out,
  success 
)
Value:
{ \
out << #v1" = "<<(v1)<<" "#comp" "<<(v2)<<" : "; \
const bool l_result = (v1) comp (v2); \
if (!l_result) (success) = false; \
(out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
}
#define TEUCHOS_PASS_FAIL(RESULT)
Macro that prints &quot;passed&quot; or &quot;failed&quot; and optionally prints the file name and line number as well...
int comp(BigUInt< n > const &a, BigUInt< n > const &b)

Compare an object and a constant using an input comparsion operator.

The test succeeds (passes) if and only if "(v1) comp (v2)". For example, TEUCHOS_TEST_COMPARE( 2, <, 3, out, success ) succeeds, but TEUCHOS_TEST_COMPARE( 2, >, 3, out, success ) and TEUCHOS_TEST_COMPARE( 3, <, 2, out, success ) both fail.

Definition at line 504 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_THROW (   code,
  ExceptType,
  out,
  success 
)

Test that the chunk of code 'code' throws an expected exception.

'code' is a chunk of code to execute. It will be executed exactly once. If it throws an exception of type ExceptType, this test passes (and prints "passed"). Otherwise, it prints "failed" with an informative message. The macro prints all messages to the given output stream (std::ostream&) out. Furthermore, if the test passes, it assigns true to success; if the test fails, it assigns false to success.

The macro's implementation does not evaluate 'out' more than once.

Definition at line 527 of file Teuchos_TestingHelpers.hpp.

#define TEUCHOS_TEST_NOTHROW (   code,
  out,
  success 
)
Value:
{ \
std::ostream& l_out = (out); \
try { \
l_out << "Test that code {"#code";} does not throw : "; \
code; \
l_out << "passed\n"; \
} \
catch (std::exception& except) { \
(success) = false; \
l_out << "The code was not supposed to throw an exception, but " \
<< "instead threw an exception of type " \
<< typeid (except).name () << ", which is a subclass of " \
<< "std::exception. The exception's message is:\n\n"; \
{ \
Teuchos::OSTab l_tab(out); \
l_out << except.what () << "\n\n"; \
(void)l_tab; \
} \
l_out << "failed\n"; \
} \
catch (...) { \
(success) = false; \
l_out << "The code was not supposed to throw an exception, but " \
<< "instead threw an exception of some unknown type, which is " \
<< "not a subclass of std::exception. This means we cannot " \
<< "show you the exception's message, if it even has one.\n\n"; \
l_out << "failed\n"; \
} \
}
basic_OSTab< char > OSTab

Test that a chunk of code does not throw any exceptions.

This macro is not complicated so take a look for yourself!

Definition at line 576 of file Teuchos_TestingHelpers.hpp.