Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_TestingTools.cpp
1 // @HEADER
2 // *****************************************************************************
3 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
4 //
5 // Copyright 2004 NTESS and the Thyra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Thyra_TestingTools.hpp"
11 
12 
14  const std::string &boolExprName,
15  const bool &boolExpr,
16  const bool &boolExpected,
17  const Ptr<std::ostream> &out,
18  const std::string &li
19  )
20 {
21  const bool success = ( boolExpr == boolExpected );
22  if (nonnull(out)) {
23  *out
24  << std::endl
25  << li << "Check: " << boolExprName << " = " << boolExpr << " == " << boolExpected
26  << " : " << passfail(success) << std::endl;
27  }
28  return success;
29 }
30 
31 
32 void Thyra::printTestResults(
33  const bool result,
34  const std::string &test_summary,
35  const bool show_all_tests,
36  const Ptr<bool> &success,
37  const Ptr<std::ostream> &out
38  )
39 {
40  if (!result) *success = false;
41  if (nonnull(out)) {
42  if (!result || show_all_tests) {
43  *out << std::endl << test_summary;
44  }
45  else {
46  *out << "passed!\n";
47  }
48  }
49 }
50 
51 
52 // TestResultsPrinter
53 
54 
55 namespace Thyra {
56 
57 
58 TestResultsPrinter::TestResultsPrinter(
59  const RCP<FancyOStream> &out, const bool show_all_tests)
60  : out_(out.assert_not_null()), show_all_tests_(show_all_tests),
61  printedTestResults_(false)
62 {
63  if (show_all_tests_) {
64  oss_ = out_;
65  }
66  else {
67  oss_ = Teuchos::fancyOStream(Teuchos::rcpFromRef(ossStore_));
68  ossStore_.copyfmt(*out_);
69  }
70 }
71 
72 
74 {
75  using Teuchos::inoutArg;
76  if (!printedTestResults_) {
77  // If we get here, either someone made a mistake in not calling
78  // printTestResults() or an exception was thrown. Either way, we are
79  // going to assume failure and dump everything.
80  try {
81  bool dummy_success = true;
82  this->printTestResults(false, inoutArg(dummy_success));
83  }
84  catch(...) {
85  // Need to eat any exceptions in case an exception is already active
86  // which is calling this destructor.
87  }
88  }
89  printedTestResults_ = true;
90 }
91 
92 
95 {
96  const RCP<FancyOStream> oldOut = out_;
97  out_ = out;
98  return oldOut;
99 }
100 
101 
103 {
104  return oss_;
105 }
106 
107 
108 void TestResultsPrinter::printTestResults(const bool this_result,
109  const Ptr<bool> &success)
110 {
111  if (!show_all_tests_) {
112  Thyra::printTestResults(this_result, ossStore_.str(), false,
113  success, out_.ptr());
114  }
115  else {
116  if (!this_result) {
117  *success = false;
118  }
119  }
120  printedTestResults_ = true;
121 }
122 
123 
124 } // namespace Thyra
const std::string passfail(const bool result)
RCP< FancyOStream > replaceOStream(const RCP< FancyOStream > &out)
Replace the underlying output stream (used for unit testing this class).
Ptr< T > ptr() const
void printTestResults(const bool this_result, const Ptr< bool > &success)
Print the test result.
~TestResultsPrinter()
Print the test results on destruction if not already printed.
bool nonnull(const boost::shared_ptr< T > &p)
RCP< FancyOStream > getTestOStream()
Return the stream used for testing.
bool testBoolExpr(const std::string &boolExprName, const bool &boolExpr, const bool &boolExpected, const Ptr< std::ostream > &out, const std::string &leadingIndent=std::string(""))
Check a boolean result against expected result.