10 #ifndef TEUCHOS_TESTING_HELPERS_HPP
11 #define TEUCHOS_TESTING_HELPERS_HPP
22 #include "Teuchos_FancyOStream.hpp"
39 inline const std::string
passfail(
const bool result);
46 TEUCHOSCORE_LIB_DLL_EXPORT
const std::string
passfail_with_location(
const bool result,
const std::string &file,
const int lineNumber);
66 template <
bool hasMachineParameters,
class Scalar>
69 static Scalar smallNumber()
80 template <
class Scalar>
83 static Scalar smallNumber()
94 template <
class Scalar>
97 static Scalar smallNumber()
108 template <
class Scalar>
122 template <
class Scalar1,
class Scalar2>
124 relErr(
const Scalar1 &s1,
const Scalar2 &s2 );
133 template <
typename T1,
typename T2,
typename Enabled =
void>
137 typedef typename std::common_type<magType1,magType2>::type magnitudeType;
139 const std::string &v1_name,
141 const std::string &v2_name,
143 const std::string &maxRelErr_error_name,
144 const magnitudeType &maxRelErr_error,
145 const std::string &maxRelErr_warning_name,
146 const magnitudeType &maxRelErr_warning,
152 const magnitudeType rel_err =
relErr( v1, v2 );
153 const bool success = ( !SMT::isnaninf(rel_err) && !SMT::isnaninf(maxRelErr_error)
154 && rel_err <= maxRelErr_error );
158 <<
"Check: rel_err(" << v1_name <<
", " << v2_name <<
")\n"
159 <<
" = rel_err(" << v1 <<
", " << v2 <<
") "
160 <<
"= " << rel_err << endl
161 <<
" <= " << maxRelErr_error_name
162 <<
" = " << maxRelErr_error <<
" : " <<
passfail(success) << endl;
163 if( success && rel_err >= maxRelErr_warning ) {
165 <<
"Warning! rel_err(" << v1_name <<
", " << v2_name <<
")\n"
166 <<
" = rel_err(" << v1 <<
", " << v2 <<
") "
167 <<
"= " << rel_err << endl
168 <<
" >= " << maxRelErr_warning_name
169 <<
" = " << maxRelErr_warning <<
"!\n";
176 template <
typename T1,
typename T2>
178 const std::string &v1_name,
180 const std::string &v2_name,
182 const std::string &maxRelErr_error_name,
183 const typename TestRelErr<T1,T2>::magnitudeType &maxRelErr_error,
184 const std::string &maxRelErr_warning_name,
185 const typename TestRelErr<T1,T2>::magnitudeType &maxRelErr_warning,
187 return TestRelErr<T1,T2>::eval(v1_name, v1, v2_name,v2, maxRelErr_error_name, maxRelErr_error, maxRelErr_warning_name, maxRelErr_warning, out);
203 template<
class Array1,
class Array2>
205 const Array1 &a1,
const std::string &a1_name,
206 const Array2 &a2,
const std::string &a2_name,
226 template<
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,
250 template<
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"; \
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,
657 out <<
"Comparing " << a1_name <<
" == " << a2_name <<
" ... ";
659 const int n = a1.size();
662 if (as<int>(a2.size()) != n) {
663 out <<
"\nError, "<<a1_name<<
".size() = "<<a1.size()<<
" == "
664 << a2_name<<
".size() = "<<a2.size()<<
" : failed!\n";
669 for(
int i = 0; i < n; ++i ) {
670 const bool result = ( a1[i] == a2[i] );
672 out <<
"\nError, "<<a1_name<<
"["<<i<<
"] = "<<a1[i]<<
" == "
673 << a2_name<<
"["<<i<<
"] = "<<a2[i]<<
": failed!\n";
686 template<
class Array1,
class Array2,
class ScalarMag>
688 const Array1 &a1,
const std::string &a1_name,
689 const Array2 &a2,
const std::string &a2_name,
690 const ScalarMag &tol,
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.");
705 out <<
"Comparing " << a1_name <<
" == " << a2_name <<
" ... ";
707 const int n = a1.size();
710 if (as<int>(a2.size()) != n) {
711 out <<
"\nError, "<<a1_name<<
".size() = "<<a1.size()<<
" == "
712 << a2_name<<
".size() = "<<a2.size()<<
" : failed!\n";
717 for(
int i = 0; i < n; ++i ) {
718 const ScalarMag err =
relErr( a1[i], a2[i] );
719 if ( !(err <= tol) ) {
721 <<
"\nError, relErr("<<a1_name<<
"["<<i<<
"],"
722 <<a2_name<<
"["<<i<<
"]) = relErr("<<a1[i]<<
","<<a2[i]<<
") = "
723 <<err<<
" <= tol = "<<tol<<
": failed!\n";
735 template<
class Array1,
class Array2,
class ScalarMag>
737 const Array1 &a1,
const std::string &a1_name,
738 const Array2 &a2,
const std::string &a2_name,
739 const ScalarMag &tol,
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.");
755 out <<
"Comparing " << a1_name <<
" == " << a2_name <<
" ... ";
757 const int n = a1.size();
760 if (as<int>(a2.size()) != n) {
761 out <<
"\nError, "<<a1_name<<
".size() = "<<a1.size()<<
" == "
762 << a2_name<<
".size() = "<<a2.size()<<
" : failed!\n";
767 for(
int i = 0; i < n; ++i ) {
769 if ( !(err <= tol) ) {
771 <<
"\nError, ||"<<a1_name<<
"["<<i<<
"] - " << a2_name<<
"["<<i<<
"]|| = "
772 <<err<<
" <= tol = "<<tol<<
": failed!\n";
783 #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 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...
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 ...
Compute, check and optionally print the relative error in two scalars.
void updateSuccess(const bool result, bool &success)
Update the success bool flag.
Teuchos::ScalarTraits< typename std::common_type< Scalar1, Scalar2 >::type >::magnitudeType relErr(const Scalar1 &s1, const Scalar2 &s2)
Return relative error of two scalars.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
Defines basic traits for the scalar field type.
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...