42 #ifndef TEUCHOS_TESTING_HELPERS_HPP 43 #define TEUCHOS_TESTING_HELPERS_HPP 54 #include "Teuchos_FancyOStream.hpp" 71 inline const std::string
passfail(
const bool result);
78 TEUCHOSCORE_LIB_DLL_EXPORT
const std::string
passfail_with_location(
const bool result,
const std::string &file,
const int lineNumber);
98 template <
bool hasMachineParameters,
class Scalar>
101 static Scalar smallNumber()
112 template <
class Scalar>
115 static Scalar smallNumber()
126 template <
class Scalar>
129 static Scalar smallNumber()
140 template <
class Scalar>
154 template <
class Scalar>
156 relErr(
const Scalar &s1,
const Scalar &s2 );
165 template<
class Scalar>
167 const std::string &v1_name,
169 const std::string &v2_name,
171 const std::string &maxRelErr_error_name,
173 const std::string &maxRelErr_warning_name,
190 template<
class Array1,
class Array2>
192 const Array1 &a1,
const std::string &a1_name,
193 const Array2 &a2,
const std::string &a2_name,
210 template<
class Array1,
class Array2,
class ScalarMag>
212 const Array1 &a1,
const std::string &a1_name,
213 const Array2 &a2,
const std::string &a2_name,
214 const ScalarMag &tol,
230 #define TEUCHOS_PASS_FAIL(RESULT) \ 231 Teuchos::passfail_with_location((RESULT), __FILE__, __LINE__) 240 #define TEUCHOS_ECHO( statement, out ) \ 241 (out) << #statement ";\n"; \ 250 #define TEUCHOS_TEST_EQUALITY_CONST( v1, v2, out, success ) \ 252 (out) << #v1" = "<<Teuchos::toString(v1)<<" == "<<Teuchos::toString(v2)<<" : "; \ 253 const bool l_result = (v1) == (v2); \ 254 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \ 255 if (!l_result) (success) = false; \ 264 #define TEUCHOS_TEST_ASSERT( v1, out, success ) \ 266 const bool l_result = v1; \ 267 (out) << #v1" = "<<l_result<<" == true : "; \ 268 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \ 269 if (!l_result) (success) = false; \ 278 #define TEUCHOS_TEST_EQUALITY( v1, v2, out, success ) \ 280 (out) << #v1" = "<<Teuchos::toString(v1)<<" == "#v2" = "<<Teuchos::toString(v2)<<" : "; \ 281 const bool l_result = (v1) == (v2); \ 282 if (!l_result) (success) = false; \ 283 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \ 293 #define TEUCHOS_TEST_INEQUALITY_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; \ 308 #define TEUCHOS_TEST_INEQUALITY( v1, v2, out, success ) \ 310 (out) << #v1" = "<<Teuchos::toString(v1)<<" != "#v2" = "<<Teuchos::toString(v2)<<" : "; \ 311 const bool l_result = (v1) != (v2); \ 312 if (!l_result) (success) = false; \ 313 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \ 323 #define TEUCHOS_TEST_FLOATING_EQUALITY( v1, v2, tol, out, success ) \ 325 const bool l_result = Teuchos::testRelErr( \ 326 #v1, v1, #v2, v2, "tol", tol, "tol", tol, Teuchos::outArg(out) ); \ 327 if (!l_result) (success) = false; \ 340 #define TEUCHOS_TEST_ITER_EQUALITY( iter1, iter2, out, success ) \ 342 (out) << #iter1" == "#iter2" = : "; \ 343 const bool l_result = (iter1) == (iter2); \ 344 if (!l_result) (success) = false; \ 345 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \ 358 #define TEUCHOS_TEST_ITER_INEQUALITY( iter1, iter2, out, success ) \ 360 (out) << #iter1" != "#iter2" = : "; \ 361 const bool l_result = (iter1) != (iter2); \ 362 if (!l_result) (success) = false; \ 363 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \ 373 #define TEUCHOS_TEST_ARRAY_ELE_EQUALITY( a, i, val, printPass, out, success ) \ 375 const bool l_result = ( (a)[i] == (val) ); \ 376 if (!l_result) (success) = false; \ 377 if (printPass || !(l_result)) { \ 378 out << #a"["<<i<<"] = " << Teuchos::toString((a)[i]) << " == "#val" = " << Teuchos::toString(val) \ 379 << " : " << TEUCHOS_PASS_FAIL(l_result) << "\n"; \ 390 #define TEUCHOS_TEST_ARRAY_ELE_INEQUALITY( a, i, val, printPass, out, success ) \ 392 const bool l_result = ( (a)[i] != (val) ); \ 393 if (!l_result) (success) = false; \ 394 if (printPass || !(l_result)) { \ 395 out << #a"["<<i<<"] = " << Teuchos::toString((a)[i]) << " != "#val" = " << Teuchos::toString(val) \ 396 << " : " << TEUCHOS_PASS_FAIL(l_result) << "\n"; \ 408 #define TEUCHOS_TEST_MATRIX_ELE_FLOATING_EQUALITY( a, i, j, val, tol, printPass, out, success ) \ 410 std::ostringstream a_i_str; \ 411 a_i_str <<#a<<"("<<i<<","<<j<<")"; \ 412 const bool l_result = Teuchos::testRelErr( \ 413 a_i_str.str(), (a)(i,j), #val, val, "tol", tol, "tol", tol, \ 414 (printPass) ? Teuchos::outArg(out) : Teuchos::null ); \ 415 if (!l_result) (success) = false; \ 425 #define TEUCHOS_TEST_MATRIX_ELE_EQUALITY( a, i, j, val, printPass, out, success ) \ 427 const bool l_result = ( (a)(i,j) == (val) ); \ 428 if (!l_result) (success) = false; \ 429 if (printPass || !(l_result)) { \ 430 out << #a"("<<i<<","<<j<<") = " << (a)(i,j) << " == "#val" = " << (val) \ 431 << " : " << TEUCHOS_PASS_FAIL(l_result) << "\n"; \ 445 #define TEUCHOS_TEST_COMPARE( v1, comp, v2, out, success ) \ 447 out << #v1" = "<<(v1)<<" "#comp" "#v2" = "<<(v2)<<" : "; \ 448 const bool l_result = (v1) comp (v2); \ 449 if (!l_result) (success) = false; \ 450 (out) << TEUCHOS_PASS_FAIL(l_result) << "\n"; \ 468 #define TEUCHOS_TEST_THROW( code, ExceptType, out, success ) \ 470 std::ostream& l_out = (out); \ 472 l_out << "Test that code {"#code";} throws " \ 473 << Teuchos::TypeNameTraits<ExceptType>::name () << ": "; \ 476 l_out << "failed (code did not throw an exception at all)\n"; \ 478 catch (const ExceptType& except) { \ 479 l_out << "passed\n"; \ 480 l_out << "\nException message for expected exception:\n\n"; \ 482 Teuchos::OSTab l_tab (out); \ 483 l_out << except.what () << "\n\n"; \ 486 catch (std::exception& except) { \ 487 l_out << "The code was supposed to throw an exception of type " \ 488 << Teuchos::TypeNameTraits<ExceptType>::name () << ", but " \ 489 << "instead threw an exception of type " \ 490 << typeid (except).name () << ", which is a subclass of " \ 491 << "std::exception. The exception's message is:\n\n"; \ 493 Teuchos::OSTab l_tab (out); \ 494 l_out << except.what () << "\n\n"; \ 496 l_out << "failed\n"; \ 499 l_out << "The code was supposed to throw an exception of type " \ 500 << Teuchos::TypeNameTraits<ExceptType>::name () << ", but " \ 501 << "instead threw an exception of some unknown type, which is " \ 502 << "not a subclass of std::exception. This means we cannot " \ 503 << "show you the exception's message, if it even has one.\n\n"; \ 504 l_out << "failed\n"; \ 515 #define TEUCHOS_TEST_NOTHROW( code, out, success ) \ 517 std::ostream& l_out = (out); \ 519 l_out << "Test that code {"#code";} does not throw : "; \ 521 l_out << "passed\n"; \ 523 catch (std::exception& except) { \ 525 l_out << "The code was not supposed to throw an exception, but " \ 526 << "instead threw an exception of type " \ 527 << typeid (except).name () << ", which is a subclass of " \ 528 << "std::exception. The exception's message is:\n\n"; \ 530 Teuchos::OSTab l_tab (out); \ 531 l_out << except.what () << "\n\n"; \ 533 l_out << "failed\n"; \ 537 l_out << "The code was not supposed to throw an exception, but " \ 538 << "instead threw an exception of some unknown type, which is " \ 539 << "not a subclass of std::exception. This means we cannot " \ 540 << "show you the exception's message, if it even has one.\n\n"; \ 541 l_out << "failed\n"; \ 553 if (!result) success =
false;
567 template <
class Scalar>
573 ST::magnitude( s1 - s2 )
578 + std::max( ST::magnitude(s1), ST::magnitude(s2) )
583 template<
class Scalar>
585 const std::string &v1_name,
587 const std::string &v2_name,
589 const std::string &maxRelErr_error_name,
591 const std::string &maxRelErr_warning_name,
598 typedef typename ST::magnitudeType ScalarMag;
600 const ScalarMag rel_err =
relErr( v1, v2 );
601 const bool success = ( !SMT::isnaninf(rel_err) && !SMT::isnaninf(maxRelErr_error)
602 && rel_err <= maxRelErr_error );
606 <<
"Check: rel_err(" << v1_name <<
", " << v2_name <<
")\n" 607 <<
" = rel_err(" << v1 <<
", " << v2 <<
") " 608 <<
"= " << rel_err << endl
609 <<
" <= " << maxRelErr_error_name
610 <<
" = " << maxRelErr_error <<
" : " <<
passfail(success) << endl;
611 if( success && rel_err >= maxRelErr_warning ) {
613 <<
"Warning! rel_err(" << v1_name <<
", " << v2_name <<
")\n" 614 <<
" = rel_err(" << v1 <<
", " << v2 <<
") " 615 <<
"= " << rel_err << endl
616 <<
" >= " << maxRelErr_warning_name
617 <<
" = " << maxRelErr_warning <<
"!\n";
624 template<
class Array1,
class Array2>
626 const Array1 &a1,
const std::string &a1_name,
627 const Array2 &a2,
const std::string &a2_name,
634 out <<
"Comparing " << a1_name <<
" == " << a2_name <<
" ... ";
636 const int n = a1.size();
639 if (as<int>(a2.size()) != n) {
640 out <<
"\nError, "<<a1_name<<
".size() = "<<a1.size()<<
" == " 641 << a2_name<<
".size() = "<<a2.size()<<
" : failed!\n";
646 for(
int i = 0; i < n; ++i ) {
647 const bool result = ( a1[i] == a2[i] );
649 out <<
"\nError, "<<a1_name<<
"["<<i<<
"] = "<<a1[i]<<
" == " 650 << a2_name<<
"["<<i<<
"] = "<<a2[i]<<
": failed!\n";
663 template<
class Array1,
class Array2,
class ScalarMag>
665 const Array1 &a1,
const std::string &a1_name,
666 const Array2 &a2,
const std::string &a2_name,
667 const ScalarMag &tol,
674 out <<
"Comparing " << a1_name <<
" == " << a2_name <<
" ... ";
676 const int n = a1.size();
679 if (as<int>(a2.size()) != n) {
680 out <<
"\nError, "<<a1_name<<
".size() = "<<a1.size()<<
" == " 681 << a2_name<<
".size() = "<<a2.size()<<
" : failed!\n";
686 for(
int i = 0; i < n; ++i ) {
687 const ScalarMag err =
relErr( a1[i], a2[i] );
688 if ( !(err <= tol) ) {
690 <<
"\nError, relErr("<<a1_name<<
"["<<i<<
"]," 691 <<a2_name<<
"["<<i<<
"]) = relErr("<<a1[i]<<
","<<a2[i]<<
") = " 692 <<err<<
" <= tol = "<<tol<<
": failed!\n";
705 #endif // TEUCHOS_TESTING_HELPERS_HPP 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.
T magnitudeType
Mandatory typedef for result of magnitude.
static magnitudeType eps()
Returns relative machine precision.
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 is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
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(...).
const std::string passfail(const bool result)
Return "passed" or "failed".
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
This structure defines some basic traits for a scalar field type.
Scalar defaultSmallNumber()
void showTestFailureLocation(bool)
Set if TEUCHOS_PASS_FAIL(...) should print test failure location.
std::ostream subclass that performs the magic of indenting data sent to an std::ostream object among ...
ScalarTraits< Scalar >::magnitudeType relErr(const Scalar &s1, const Scalar &s2)
Return relative error of two scalars.
void updateSuccess(const bool result, bool &success)
Update the success bool flag.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
Defines basic traits for the scalar field type.
bool testRelErr(const std::string &v1_name, const Scalar &v1, const std::string &v2_name, const Scalar &v2, const std::string &maxRelErr_error_name, const typename Teuchos::ScalarTraits< Scalar >::magnitudeType &maxRelErr_error, const std::string &maxRelErr_warning_name, const typename Teuchos::ScalarTraits< Scalar >::magnitudeType &maxRelErr_warning, const Ptr< std::ostream > &out)
Compute, check and optionally print the relative error in two scalars.
Defines basic traits returning the name of a type in a portable and readable way. ...
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...