10#ifndef TEUCHOS_TESTING_HELPERS_HPP
11#define TEUCHOS_TESTING_HELPERS_HPP
22#include "Teuchos_FancyOStream.hpp"
39inline const std::string
passfail(
const bool result);
46TEUCHOSCORE_LIB_DLL_EXPORT
const std::string
passfail_with_location(
const bool result,
const std::string &file,
const int lineNumber);
66template <
bool hasMachineParameters,
class Scalar>
69 static Scalar smallNumber()
80template <
class Scalar>
83 static Scalar smallNumber()
94template <
class Scalar>
97 static Scalar smallNumber()
108template <
class Scalar>
122template <
class Scalar1,
class Scalar2>
124relErr(
const Scalar1 &s1,
const Scalar2 &s2 );
133template <
typename T1,
typename T2,
typename Enabled =
void>
137 typedef typename std::common_type<magType1,magType2>::type magnitudeType;
159 <<
" = rel_err(" <<
v1 <<
", " <<
v2 <<
") "
166 <<
" = rel_err(" <<
v1 <<
", " <<
v2 <<
") "
176template <
typename T1,
typename T2>
203template<
class Array1,
class Array2>
205 const Array1 &a1,
const std::string &a1_name,
206 const Array2 &a2,
const std::string &a2_name,
226template<
class Array1,
class Array2,
class ScalarMag>
228 const Array1 &a1,
const std::string &a1_name,
229 const Array2 &a2,
const std::string &a2_name,
230 const ScalarMag &tol,
250template<
class Array1,
class Array2,
class ScalarMag>
252 const Array1 &a1,
const std::string &a1_name,
253 const Array2 &a2,
const std::string &a2_name,
254 const ScalarMag &tol,
270#define TEUCHOS_PASS_FAIL(RESULT) \
271 Teuchos::passfail_with_location((RESULT), __FILE__, __LINE__)
280#define TEUCHOS_ECHO( statement, out ) \
281 (out) << #statement ";\n"; \
290#define TEUCHOS_TEST_EQUALITY_CONST( v1, v2, out, success ) \
292 (out) << #v1" = "<<Teuchos::toString(v1)<<" == "<<Teuchos::toString(v2)<<" : "; \
293 const bool l_result = (v1) == (v2); \
294 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
295 if (!l_result) (success) = false; \
304#define TEUCHOS_TEST_ASSERT( v1, out, success ) \
306 const bool l_result = v1; \
307 (out) << #v1" = "<<l_result<<" == true : "; \
308 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
309 if (!l_result) (success) = false; \
318#define TEUCHOS_TEST_EQUALITY( v1, v2, out, success ) \
320 (out) << #v1" = "<<Teuchos::toString(v1)<<" == "#v2" = "<<Teuchos::toString(v2)<<" : "; \
321 const bool l_result = (v1) == (v2); \
322 if (!l_result) (success) = false; \
323 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
333#define TEUCHOS_TEST_INEQUALITY_CONST( v1, v2, out, success ) \
335 (out) << #v1" = "<<Teuchos::toString(v1)<<" != "<<Teuchos::toString(v2)<<" : "; \
336 const bool l_result = (v1) != (v2); \
337 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
338 if (!l_result) (success) = false; \
348#define TEUCHOS_TEST_INEQUALITY( v1, v2, out, success ) \
350 (out) << #v1" = "<<Teuchos::toString(v1)<<" != "#v2" = "<<Teuchos::toString(v2)<<" : "; \
351 const bool l_result = (v1) != (v2); \
352 if (!l_result) (success) = false; \
353 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
363#define TEUCHOS_TEST_FLOATING_EQUALITY( v1, v2, tol, out, success ) \
365 const bool l_result = Teuchos::testRelErr( \
366 #v1, v1, #v2, v2, "tol", tol, "tol", tol, Teuchos::outArg(out) ); \
367 if (!l_result) (success) = false; \
380#define TEUCHOS_TEST_ITER_EQUALITY( iter1, iter2, out, success ) \
382 (out) << #iter1" == "#iter2" = : "; \
383 const bool l_result = (iter1) == (iter2); \
384 if (!l_result) (success) = false; \
385 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
398#define TEUCHOS_TEST_ITER_INEQUALITY( iter1, iter2, out, success ) \
400 (out) << #iter1" != "#iter2" = : "; \
401 const bool l_result = (iter1) != (iter2); \
402 if (!l_result) (success) = false; \
403 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
413#define TEUCHOS_TEST_ARRAY_ELE_EQUALITY( a, i, val, printPass, out, success ) \
415 const bool l_result = ( (a)[i] == (val) ); \
416 if (!l_result) (success) = false; \
417 if (printPass || !(l_result)) { \
418 out << #a"["<<i<<"] = " << Teuchos::toString((a)[i]) << " == "#val" = " << Teuchos::toString(val) \
419 << " : " << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
430#define TEUCHOS_TEST_ARRAY_ELE_INEQUALITY( a, i, val, printPass, out, success ) \
432 const bool l_result = ( (a)[i] != (val) ); \
433 if (!l_result) (success) = false; \
434 if (printPass || !(l_result)) { \
435 out << #a"["<<i<<"] = " << Teuchos::toString((a)[i]) << " != "#val" = " << Teuchos::toString(val) \
436 << " : " << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
448#define TEUCHOS_TEST_MATRIX_ELE_FLOATING_EQUALITY( a, i, j, val, tol, printPass, out, success ) \
450 std::ostringstream a_i_str; \
451 a_i_str <<#a<<"("<<i<<","<<j<<")"; \
452 const bool l_result = Teuchos::testRelErr( \
453 a_i_str.str(), (a)(i,j), #val, val, "tol", tol, "tol", tol, \
454 (printPass) ? Teuchos::outArg(out) : Teuchos::null ); \
455 if (!l_result) (success) = false; \
465#define TEUCHOS_TEST_MATRIX_ELE_EQUALITY( a, i, j, val, printPass, out, success ) \
467 const bool l_result = ( (a)(i,j) == (val) ); \
468 if (!l_result) (success) = false; \
469 if (printPass || !(l_result)) { \
470 out << #a"("<<i<<","<<j<<") = " << (a)(i,j) << " == "#val" = " << (val) \
471 << " : " << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
485#define TEUCHOS_TEST_COMPARE( v1, comp, v2, out, success ) \
487 out << #v1" = "<<(v1)<<" "#comp" "#v2" = "<<(v2)<<" : "; \
488 const bool l_result = (v1) comp (v2); \
489 if (!l_result) (success) = false; \
490 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
504#define TEUCHOS_TEST_COMPARE_CONST( v1, comp, v2, out, success ) \
506 out << #v1" = "<<(v1)<<" "#comp" "<<(v2)<<" : "; \
507 const bool l_result = (v1) comp (v2); \
508 if (!l_result) (success) = false; \
509 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \
527#define TEUCHOS_TEST_THROW( code, ExceptType, out, success ) \
529 std::ostream& l_out = (out); \
531 l_out << "Test that code {"#code";} throws " \
532 << Teuchos::TypeNameTraits<ExceptType>::name () << ": "; \
535 l_out << "failed (code did not throw an exception at all)\n"; \
537 catch (const ExceptType& except) { \
538 l_out << "passed\n"; \
539 l_out << "\nException message for expected exception:\n\n"; \
541 Teuchos::OSTab l_tab(out); \
542 l_out << except.what () << "\n\n"; \
546 catch (std::exception& except) { \
547 l_out << "The code was supposed to throw an exception of type " \
548 << Teuchos::TypeNameTraits<ExceptType>::name () << ", but " \
549 << "instead threw an exception of type " \
550 << typeid (except).name () << ", which is a subclass of " \
551 << "std::exception. The exception's message is:\n\n"; \
553 Teuchos::OSTab l_tab(out); \
554 l_out << except.what () << "\n\n"; \
557 l_out << "failed\n"; \
560 l_out << "The code was supposed to throw an exception of type " \
561 << Teuchos::TypeNameTraits<ExceptType>::name () << ", but " \
562 << "instead threw an exception of some unknown type, which is " \
563 << "not a subclass of std::exception. This means we cannot " \
564 << "show you the exception's message, if it even has one.\n\n"; \
565 l_out << "failed\n"; \
576#define TEUCHOS_TEST_NOTHROW( code, out, success ) \
578 std::ostream& l_out = (out); \
580 l_out << "Test that code {"#code";} does not throw : "; \
582 l_out << "passed\n"; \
584 catch (std::exception& except) { \
586 l_out << "The code was not supposed to throw an exception, but " \
587 << "instead threw an exception of type " \
588 << typeid (except).name () << ", which is a subclass of " \
589 << "std::exception. The exception's message is:\n\n"; \
591 Teuchos::OSTab l_tab(out); \
592 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
Scalar3;
636 ST::magnitude(
s1 -
s2 )
641 + std::max( ST::magnitude(
s1), ST::magnitude(
s2) )
646template<
class Array1,
class Array2>
659 const int n = a1.size();
663 out <<
"\nError, "<<
a1_name<<
".size() = "<<a1.size()<<
" == "
664 <<
a2_name<<
".size() = "<<
a2.size()<<
" : failed!\n";
669 for(
int i = 0;
i < n; ++
i ) {
672 out <<
"\nError, "<<
a1_name<<
"["<<
i<<
"] = "<<a1[
i]<<
" == "
686template<
class Array1,
class Array2,
class ScalarMag>
697 using Elem1 = std::decay_t<decltype(std::declval<Array1>()[0])>;
698 using Elem2 = std::decay_t<decltype(std::declval<Array2>()[0])>;
700 static_assert(std::is_same_v<Elem1, Elem2>,
701 "Teuchos::compareFloatingArrays: element types of Array1 and Array2 must be the same.");
707 const int n = a1.size();
711 out <<
"\nError, "<<
a1_name<<
".size() = "<<a1.size()<<
" == "
712 <<
a2_name<<
".size() = "<<
a2.size()<<
" : failed!\n";
717 for(
int i = 0;
i < n; ++
i ) {
721 <<
"\nError, relErr("<<
a1_name<<
"["<<
i<<
"],"
722 <<
a2_name<<
"["<<
i<<
"]) = relErr("<<a1[
i]<<
","<<
a2[
i]<<
") = "
723 <<
err<<
" <= tol = "<<
tol<<
": failed!\n";
735template<
class Array1,
class Array2,
class ScalarMag>
747 using Elem1 = std::decay_t<decltype(std::declval<Array1>()[0])>;
748 using Elem2 = std::decay_t<decltype(std::declval<Array2>()[0])>;
750 static_assert(std::is_same_v<Elem1, Elem2>,
751 "Teuchos::compareFloatingArraysAbsolute: element types of Array1 and Array2 must be the same.");
757 const int n = a1.size();
761 out <<
"\nError, "<<
a1_name<<
".size() = "<<a1.size()<<
" == "
762 <<
a2_name<<
".size() = "<<
a2.size()<<
" : failed!\n";
767 for(
int i = 0;
i < n; ++
i ) {
772 <<
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.
bool 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 c...
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.