42 #ifndef TEUCHOS_TESTING_HELPERS_HPP
43 #define TEUCHOS_TESTING_HELPERS_HPP
71 inline const std::string
passfail(
const bool result);
98 template <
bool hasMachineParameters,
class Scalar>
112 template <
class Scalar>
126 template <
class Scalar>
140 template <
class Scalar>
154 template <
class Scalar1,
class Scalar2>
156 relErr(
const Scalar1 &s1,
const Scalar2 &s2 );
165 template <
typename T1,
typename T2,
typename Enabled =
void>
171 const std::string &v1_name,
173 const std::string &v2_name,
175 const std::string &maxRelErr_error_name,
177 const std::string &maxRelErr_warning_name,
185 const bool success = ( !SMT::isnaninf(rel_err) && !SMT::isnaninf(maxRelErr_error)
186 && rel_err <= maxRelErr_error );
190 <<
"Check: rel_err(" << v1_name <<
", " << v2_name <<
")\n"
191 <<
" = rel_err(" << v1 <<
", " << v2 <<
") "
192 <<
"= " << rel_err << endl
193 <<
" <= " << maxRelErr_error_name
194 <<
" = " << maxRelErr_error <<
" : " <<
passfail(success) << endl;
195 if( success && rel_err >= maxRelErr_warning ) {
197 <<
"Warning! rel_err(" << v1_name <<
", " << v2_name <<
")\n"
198 <<
" = rel_err(" << v1 <<
", " << v2 <<
") "
199 <<
"= " << rel_err << endl
200 <<
" >= " << maxRelErr_warning_name
201 <<
" = " << maxRelErr_warning <<
"!\n";
208 template <
typename T1,
typename T2>
210 const std::string &v1_name,
212 const std::string &v2_name,
214 const std::string &maxRelErr_error_name,
216 const std::string &maxRelErr_warning_name,
219 return TestRelErr<T1,T2>::eval(v1_name, v1, v2_name,v2, maxRelErr_error_name, maxRelErr_error, maxRelErr_warning_name, maxRelErr_warning, out);
233 template<
class Array1,
class Array2>
235 const Array1 &a1,
const std::string &a1_name,
236 const Array2 &a2,
const std::string &a2_name,
253 template<
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"; \
615 if (!result) success =
false;
629 template <
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) )
646 template<
class Array1,
class Array2>
648 const Array1 &a1,
const std::string &a1_name,
649 const Array2 &a2,
const std::string &a2_name,
656 out <<
"Comparing " << a1_name <<
" == " << a2_name <<
" ... ";
658 const int n = a1.size();
661 if (as<int>(a2.size()) != n) {
662 out <<
"\nError, "<<a1_name<<
".size() = "<<a1.size()<<
" == "
663 << a2_name<<
".size() = "<<a2.size()<<
" : failed!\n";
668 for(
int i = 0; i <
n; ++i ) {
669 const bool result = ( a1[i] == a2[i] );
671 out <<
"\nError, "<<a1_name<<
"["<<i<<
"] = "<<a1[i]<<
" == "
672 << a2_name<<
"["<<i<<
"] = "<<a2[i]<<
": failed!\n";
685 template<
class Array1,
class Array2,
class ScalarMag>
687 const Array1 &a1,
const std::string &a1_name,
688 const Array2 &a2,
const std::string &a2_name,
689 const ScalarMag &tol,
696 out <<
"Comparing " << a1_name <<
" == " << a2_name <<
" ... ";
698 const int n = a1.size();
701 if (as<int>(a2.size()) != n) {
702 out <<
"\nError, "<<a1_name<<
".size() = "<<a1.size()<<
" == "
703 << a2_name<<
".size() = "<<a2.size()<<
" : failed!\n";
708 for(
int i = 0; i <
n; ++i ) {
709 const ScalarMag err =
relErr( a1[i], a2[i] );
710 if ( !(err <= tol) ) {
712 <<
"\nError, relErr("<<a1_name<<
"["<<i<<
"],"
713 <<a2_name<<
"["<<i<<
"]) = relErr("<<a1[i]<<
","<<a2[i]<<
") = "
714 <<err<<
" <= tol = "<<tol<<
": failed!\n";
727 #endif // TEUCHOS_TESTING_HELPERS_HPP
static bool eval(const std::string &v1_name, const T1 &v1, const std::string &v2_name, const T2 &v2, const std::string &maxRelErr_error_name, const magnitudeType &maxRelErr_error, const std::string &maxRelErr_warning_name, const magnitudeType &maxRelErr_warning, const Ptr< std::ostream > &out)
void showTestFailureLocation(bool)
Set if TEUCHOS_PASS_FAIL(...) should print test failure location.
Scalar defaultSmallNumber()
static magnitudeType eps()
Returns relative machine precision.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
Teuchos::ScalarTraits< T2 >::magnitudeType magType2
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
const std::string passfail(const bool result)
Return "passed" or "failed".
std::common_type< magType1, magType2 >::type magnitudeType
This structure defines some basic traits for a scalar field type.
Teuchos::ScalarTraits< T1 >::magnitudeType magType1
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.
void updateSuccess(const bool result, bool &success)
Update the success bool flag.
#define TEUCHOSCORE_LIB_DLL_EXPORT
std::ostream subclass that performs the magic of indenting data sent to an std::ostream object among ...
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.
Compute, check and optionally print the relative error in 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 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)
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
Teuchos::ScalarTraits< typename std::common_type< Scalar1, Scalar2 >::type >::magnitudeType relErr(const Scalar1 &s1, const Scalar2 &s2)
Return relative error of two scalars.
static Scalar smallNumber()
static Scalar smallNumber()
Defines basic traits for the scalar field type.
static Scalar smallNumber()
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...