42#ifndef TEUCHOS_TESTING_HELPERS_HPP
43#define TEUCHOS_TESTING_HELPERS_HPP
54#include "Teuchos_FancyOStream.hpp"
71inline const std::string
passfail(
const bool result);
78TEUCHOSCORE_LIB_DLL_EXPORT
const std::string
passfail_with_location(
const bool result,
const std::string &file,
const int lineNumber);
98template <
bool hasMachineParameters,
class Scalar>
101 static Scalar smallNumber()
112template <
class Scalar>
115 static Scalar smallNumber()
126template <
class Scalar>
129 static Scalar smallNumber()
140template <
class Scalar>
154template <
class Scalar1,
class Scalar2>
156relErr(
const Scalar1 &s1,
const Scalar2 &s2 );
165template <
typename T1,
typename T2,
typename Enabled =
void>
169 typedef typename std::common_type<magType1,magType2>::type magnitudeType;
191 <<
" = rel_err(" <<
v1 <<
", " <<
v2 <<
") "
198 <<
" = rel_err(" <<
v1 <<
", " <<
v2 <<
") "
208template <
typename T1,
typename T2>
233template<
class Array1,
class Array2>
235 const Array1 &a1,
const std::string &a1_name,
236 const Array2 &a2,
const std::string &a2_name,
253template<
class Array1,
class Array2,
class ScalarMag>
255 const Array1 &a1,
const std::string &a1_name,
256 const Array2 &a2,
const std::string &a2_name,
257 const ScalarMag &tol,
273#define TEUCHOS_PASS_FAIL(RESULT) \
274 Teuchos::passfail_with_location((RESULT), __FILE__, __LINE__)
283#define TEUCHOS_ECHO( statement, out ) \
284 (out) << #statement ";\n"; \
293#define TEUCHOS_TEST_EQUALITY_CONST( v1, v2, out, success ) \
295 (out) << #v1" = "<<Teuchos::toString(v1)<<" == "<<Teuchos::toString(v2)<<" : "; \
296 const bool l_result = (v1) == (v2); \
297 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
298 if (!l_result) (success) = false; \
307#define TEUCHOS_TEST_ASSERT( v1, out, success ) \
309 const bool l_result = v1; \
310 (out) << #v1" = "<<l_result<<" == true : "; \
311 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
312 if (!l_result) (success) = false; \
321#define TEUCHOS_TEST_EQUALITY( v1, v2, out, success ) \
323 (out) << #v1" = "<<Teuchos::toString(v1)<<" == "#v2" = "<<Teuchos::toString(v2)<<" : "; \
324 const bool l_result = (v1) == (v2); \
325 if (!l_result) (success) = false; \
326 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
336#define TEUCHOS_TEST_INEQUALITY_CONST( v1, v2, out, success ) \
338 (out) << #v1" = "<<Teuchos::toString(v1)<<" != "<<Teuchos::toString(v2)<<" : "; \
339 const bool l_result = (v1) != (v2); \
340 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
341 if (!l_result) (success) = false; \
351#define TEUCHOS_TEST_INEQUALITY( v1, v2, out, success ) \
353 (out) << #v1" = "<<Teuchos::toString(v1)<<" != "#v2" = "<<Teuchos::toString(v2)<<" : "; \
354 const bool l_result = (v1) != (v2); \
355 if (!l_result) (success) = false; \
356 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
366#define TEUCHOS_TEST_FLOATING_EQUALITY( v1, v2, tol, out, success ) \
368 const bool l_result = Teuchos::testRelErr( \
369 #v1, v1, #v2, v2, "tol", tol, "tol", tol, Teuchos::outArg(out) ); \
370 if (!l_result) (success) = false; \
383#define TEUCHOS_TEST_ITER_EQUALITY( iter1, iter2, out, success ) \
385 (out) << #iter1" == "#iter2" = : "; \
386 const bool l_result = (iter1) == (iter2); \
387 if (!l_result) (success) = false; \
388 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
401#define TEUCHOS_TEST_ITER_INEQUALITY( iter1, iter2, out, success ) \
403 (out) << #iter1" != "#iter2" = : "; \
404 const bool l_result = (iter1) != (iter2); \
405 if (!l_result) (success) = false; \
406 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
416#define TEUCHOS_TEST_ARRAY_ELE_EQUALITY( a, i, val, printPass, out, success ) \
418 const bool l_result = ( (a)[i] == (val) ); \
419 if (!l_result) (success) = false; \
420 if (printPass || !(l_result)) { \
421 out << #a"["<<i<<"] = " << Teuchos::toString((a)[i]) << " == "#val" = " << Teuchos::toString(val) \
422 << " : " << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
433#define TEUCHOS_TEST_ARRAY_ELE_INEQUALITY( a, i, val, printPass, out, success ) \
435 const bool l_result = ( (a)[i] != (val) ); \
436 if (!l_result) (success) = false; \
437 if (printPass || !(l_result)) { \
438 out << #a"["<<i<<"] = " << Teuchos::toString((a)[i]) << " != "#val" = " << Teuchos::toString(val) \
439 << " : " << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
451#define TEUCHOS_TEST_MATRIX_ELE_FLOATING_EQUALITY( a, i, j, val, tol, printPass, out, success ) \
453 std::ostringstream a_i_str; \
454 a_i_str <<#a<<"("<<i<<","<<j<<")"; \
455 const bool l_result = Teuchos::testRelErr( \
456 a_i_str.str(), (a)(i,j), #val, val, "tol", tol, "tol", tol, \
457 (printPass) ? Teuchos::outArg(out) : Teuchos::null ); \
458 if (!l_result) (success) = false; \
468#define TEUCHOS_TEST_MATRIX_ELE_EQUALITY( a, i, j, val, printPass, out, success ) \
470 const bool l_result = ( (a)(i,j) == (val) ); \
471 if (!l_result) (success) = false; \
472 if (printPass || !(l_result)) { \
473 out << #a"("<<i<<","<<j<<") = " << (a)(i,j) << " == "#val" = " << (val) \
474 << " : " << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
488#define TEUCHOS_TEST_COMPARE( v1, comp, v2, out, success ) \
490 out << #v1" = "<<(v1)<<" "#comp" "#v2" = "<<(v2)<<" : "; \
491 const bool l_result = (v1) comp (v2); \
492 if (!l_result) (success) = false; \
493 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
507#define TEUCHOS_TEST_COMPARE_CONST( v1, comp, v2, out, success ) \
509 out << #v1" = "<<(v1)<<" "#comp" "<<(v2)<<" : "; \
510 const bool l_result = (v1) comp (v2); \
511 if (!l_result) (success) = false; \
512 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
530#define TEUCHOS_TEST_THROW( code, ExceptType, out, success ) \
532 std::ostream& l_out = (out); \
534 l_out << "Test that code {"#code";} throws " \
535 << Teuchos::TypeNameTraits<ExceptType>::name () << ": "; \
538 l_out << "failed (code did not throw an exception at all)\n"; \
540 catch (const ExceptType& except) { \
541 l_out << "passed\n"; \
542 l_out << "\nException message for expected exception:\n\n"; \
544 Teuchos::OSTab l_tab (out); \
545 l_out << except.what () << "\n\n"; \
548 catch (std::exception& except) { \
549 l_out << "The code was supposed to throw an exception of type " \
550 << Teuchos::TypeNameTraits<ExceptType>::name () << ", but " \
551 << "instead threw an exception of type " \
552 << typeid (except).name () << ", which is a subclass of " \
553 << "std::exception. The exception's message is:\n\n"; \
555 Teuchos::OSTab l_tab (out); \
556 l_out << except.what () << "\n\n"; \
558 l_out << "failed\n"; \
561 l_out << "The code was supposed to throw an exception of type " \
562 << Teuchos::TypeNameTraits<ExceptType>::name () << ", but " \
563 << "instead threw an exception of some unknown type, which is " \
564 << "not a subclass of std::exception. This means we cannot " \
565 << "show you the exception's message, if it even has one.\n\n"; \
566 l_out << "failed\n"; \
577#define TEUCHOS_TEST_NOTHROW( code, out, success ) \
579 std::ostream& l_out = (out); \
581 l_out << "Test that code {"#code";} does not throw : "; \
583 l_out << "passed\n"; \
585 catch (std::exception& except) { \
587 l_out << "The code was not supposed to throw an exception, but " \
588 << "instead threw an exception of type " \
589 << typeid (except).name () << ", which is a subclass of " \
590 << "std::exception. The exception's message is:\n\n"; \
592 Teuchos::OSTab l_tab (out); \
593 l_out << except.what () << "\n\n"; \
595 l_out << "failed\n"; \
599 l_out << "The code was not supposed to throw an exception, but " \
600 << "instead threw an exception of some unknown type, which is " \
601 << "not a subclass of std::exception. This means we cannot " \
602 << "show you the exception's message, if it even has one.\n\n"; \
603 l_out << "failed\n"; \
629template <
class Scalar1,
class Scalar2>
633 typedef typename std::common_type<Scalar1,Scalar2>::type Scalar;
636 ST::magnitude(
s1 -
s2 )
641 + std::max( ST::magnitude(
s1), ST::magnitude(
s2) )
646template<
class Array1,
class Array2>
658 const int n = a1.size();
662 out <<
"\nError, "<<
a1_name<<
".size() = "<<a1.size()<<
" == "
663 <<
a2_name<<
".size() = "<<
a2.size()<<
" : failed!\n";
668 for(
int i = 0;
i < n; ++
i ) {
671 out <<
"\nError, "<<
a1_name<<
"["<<
i<<
"] = "<<a1[
i]<<
" == "
685template<
class Array1,
class Array2,
class ScalarMag>
698 const int n = a1.size();
702 out <<
"\nError, "<<
a1_name<<
".size() = "<<a1.size()<<
" == "
703 <<
a2_name<<
".size() = "<<
a2.size()<<
" : failed!\n";
708 for(
int i = 0;
i < n; ++
i ) {
712 <<
"\nError, relErr("<<
a1_name<<
"["<<
i<<
"],"
713 <<
a2_name<<
"["<<
i<<
"]) = relErr("<<a1[
i]<<
","<<
a2[
i]<<
") = "
714 <<
err<<
" <= tol = "<<
tol<<
": failed!\n";
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Defines basic traits for the scalar field type.
Defines basic traits returning the name of a type in a portable and readable way.
Smart reference counting pointer class for automatic garbage collection.
std::ostream subclass that performs the magic of indenting data sent to an std::ostream object among ...
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
Teuchos::ScalarTraits< typenamestd::common_type< Scalar1, Scalar2 >::type >::magnitudeType relErr(const Scalar1 &s1, const Scalar2 &s2)
Return relative error of two scalars.
TEUCHOSCORE_LIB_DLL_EXPORT const std::string passfail_with_location(const bool result, const std::string &file, const int lineNumber)
Helper function for TEUCHOS_PASS_FAIL(...).
bool 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.
bool 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.
bool showTestFailureLocation()
Return if TEUCHOS_PASS_FAIL(...) should print test failure location.
const std::string passfail(const bool result)
Return "passed" or "failed".
void updateSuccess(const bool result, bool &success)
Update the success bool flag.
Scalar defaultSmallNumber()
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
This structure defines some basic traits for a scalar field type.
static magnitudeType eps()
Returns relative machine precision.
Compute, check and optionally print the relative error in two scalars.