testcase.hh

Go to the documentation of this file.
1 
7 #ifndef TEST_H
8 #define TEST_H
9 
10 #include <iostream>
11 #include <string>
12 #include <iosfwd>
13 #include <complex>
14 
66 namespace test {
67 
68  using std::string;
69  using std::ostream;
70 
71  // The following have underscores because they are macros
72  // (and it's impolite to usurp other users' functions!).
73  // For consistency, _succeed() also has an underscore.
74 
76 #define _test(cond) do_test(cond, #cond, __FILE__, __LINE__)
77 #define _numtest(num, orig) do_numtest(num, orig, #num, #orig, __FILE__, __LINE__)
79 #define _numtesttol(num, orig, tol) do_numtest(num, orig, #num, #orig, __FILE__, __LINE__, tol)
81 #define _fail(str) do_fail(str, __FILE__, __LINE__)
83 
92  class TestCase {
93  public:
97  TestCase(ostream* osptr = 0);
98  virtual ~TestCase() {}
100  virtual void run() = 0;
101 
103  long getNumPassed() const { return m_nPass; }
105  long getNumFailed() const { return m_nFail; }
107  const ostream* getStream() const { return m_osptr; }
109  void setStream(ostream* osptr) { m_osptr = osptr; }
110 
112  void _succeed() { ++m_nPass; }
117  long report() const;
119  virtual void reset() { m_nPass = m_nFail = 0; }
120  protected:
122  bool do_test(bool cond, const string& lbl,
123  const char* fname, long lineno);
125  bool do_numtest(double num, double orig, const string& lbl,
126  const string& lbl2, const char* fname, long lineno,
127  const double tol = 1e-10);
128  bool do_numtest(std::complex<double> num, std::complex<double> orig,
129  const string& lbl, const string& lbl2, const char* fname,
130  long lineno, const double tol = 1e-10);
134  void do_fail(const string& lbl,
135  const char* fname, long lineno);
136  private:
137  ostream* m_osptr;
138  long m_nPass;
139  long m_nFail;
140 
145  };
146 
147  inline TestCase::TestCase(ostream* osptr) :
148  m_osptr(osptr), m_nPass(0), m_nFail(0) {
149  if (m_osptr == 0)
150  m_osptr = &std::cout;
151  }
152 
153 } // namespace test
154 
155 #endif // TEST_H
const ostream * getStream() const
Returns output stream.
Definition: testcase.hh:107
virtual ~TestCase()
Definition: testcase.hh:98
bool do_numtest(std::complex< double > num, std::complex< double > orig, const string &lbl, const string &lbl2, const char *fname, long lineno, const double tol=1e-10)
Base class for tests.
Definition: testcase.hh:92
TestCase & operator=(const TestCase &)
Disallowed.
long report() const
Prints a report on the number of passed and failed tests to the output stream.
void do_fail(const string &lbl, const char *fname, long lineno)
Internal function to report a failed test (besides increasing the failed counter)
Unit tests.
Definition: testcase.hh:66
TestCase(const TestCase &)
Disallowed.
ostream * m_osptr
Definition: testcase.hh:137
virtual void reset()
Resets the counters for the failed and passed tests.
Definition: testcase.hh:119
void setStream(ostream *osptr)
Sets the output stream.
Definition: testcase.hh:109
bool do_test(bool cond, const string &lbl, const char *fname, long lineno)
Internal function to do a test.
bool do_numtest(double num, double orig, const string &lbl, const string &lbl2, const char *fname, long lineno, const double tol=1e-10)
Internal function to do a numerical test.
void _succeed()
Explicitly succeds a test.
Definition: testcase.hh:112
long getNumFailed() const
Returns number of failed tests.
Definition: testcase.hh:105
TestCase(ostream *osptr=0)
Constructor.
Definition: testcase.hh:147
virtual void run()=0
Runs the tests. Must be overwritten by the specialization.
long getNumPassed() const
Returns number of passed tests.
Definition: testcase.hh:103
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich