Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)

basics/testcase.hh
Go to the documentation of this file.
00001 // Test from Chuck Allison in C/C++ Users Journal, September 2000:
00002 // "The Simplest Automated Unit Test Framework That Could Possibly Work"
00003 
00004 #ifndef TEST_H
00005 #define TEST_H
00006 
00007 #include <iostream>
00008 #include <string>
00009 #include <iosfwd>
00010 #include <complex>
00011 
00063 namespace test {
00064 
00065   using std::string;
00066   using std::ostream;
00067 
00068   // The following have underscores because they are macros
00069   // (and it's impolite to usurp other users' functions!).
00070   // For consistency, _succeed() also has an underscore.
00071 
00073 #define _test(cond) do_test(cond, #cond, __FILE__, __LINE__)
00074 
00075 #define _numtest(num, orig) do_numtest(num, orig, #num, #orig, __FILE__, __LINE__)
00076 
00077 #define _numtesttol(num, orig, tol) do_numtest(num, orig, #num, #orig, __FILE__, __LINE__, tol)
00078 
00079 #define _fail(str) do_fail(str, __FILE__, __LINE__)
00080 
00089   class TestCase {
00090   public:
00094     TestCase(ostream* osptr = 0);
00095     virtual ~TestCase() {}
00097     virtual void run() = 0;
00098 
00100     long getNumPassed() const { return m_nPass; }
00102     long getNumFailed() const { return m_nFail; }
00104     const ostream* getStream() const { return m_osptr; }
00106     void setStream(ostream* osptr) { m_osptr = osptr; }
00107 
00109     void _succeed() { ++m_nPass; }
00114     long report() const;
00116     virtual void reset() { m_nPass = m_nFail = 0; }
00117   protected:
00119     bool do_test(bool cond, const string& lbl,
00120                  const char* fname, long lineno);
00122     bool do_numtest(double num, double orig, const string& lbl,
00123         const string& lbl2, const char* fname, long lineno,
00124         const double tol = 1e-10);
00125     bool do_numtest(std::complex<double> num, std::complex<double> orig,
00126         const string& lbl, const string& lbl2, const char* fname,
00127         long lineno, const double tol = 1e-10);
00131     void do_fail(const string& lbl,
00132                  const char* fname, long lineno);
00133   private:
00134     ostream* m_osptr;
00135     long m_nPass;
00136     long m_nFail;
00137 
00139     TestCase(const TestCase&);
00141     TestCase& operator=(const TestCase&);
00142   };
00143 
00144   inline TestCase::TestCase(ostream* osptr) :
00145     m_osptr(osptr), m_nPass(0), m_nFail(0) {
00146     if (m_osptr == 0)
00147       m_osptr = &std::cout;
00148   }
00149 
00150 } // namespace test
00151 
00152 #endif // TEST_H

Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)