1 // test.cpp
2 
3 /*
4  * C/C++ Users Journal Sept 2000 <br>
5  * The Simplest Automated Unit Test Framework That Could Possibly Work <br>
6  * Chuck Allison <br>
7  */
8 
9 #include "test.h"
10 #include <iostream>
11 #include <string>
12 #include <typeinfo>     // Visual Studio requires /GR""
13 
14 #ifdef _MSC_VER
15 //Allow return-less mains:
16 #pragma warning(disable: 4541)
17 #endif
18 
19 using namespace std;
20 
do_test(bool cond,const std::string & lbl,const char * fname,long lineno)21 bool Test::do_test(bool cond, const std::string& lbl,
22                    const char* fname, long lineno)
23 {
24     if (!cond)
25     {
26         do_fail(lbl, fname, lineno);
27         return false;
28     }
29     else
30         _succeed();
31 
32     return true;
33 }
34 
do_fail(const std::string & lbl,const char * fname,long lineno)35 void Test::do_fail(const std::string& lbl,
36                    const char* fname, long lineno)
37 {
38     ++m_nFail;
39     if (m_osptr)
40     {
41         *m_osptr << typeid(*this).name()
42                              << "failure: (" << lbl << ") , "
43                                  << fname
44                  << " (line " << lineno << ")\n";
45     }
46 }
47 
report() const48 long Test::report() const
49 {
50     // Use RTTI to get class name then
51     // parse out the junk at the start
52     // of the string.
53     // Requires that all class names
54     // begin with "Test".
55     string name( typeid(*this).name() );
56     name.erase( 0, name.find( "Test" ) );
57 
58     if (m_osptr)
59         {
60             *m_osptr << "Test \""  << name << "\":" << endl
61                      //<< this->getClassName().c_str() << "\":" << endl
62                      << "\tPassed: " << m_nPass
63                      << "\tFailed: " << m_nFail
64                      << endl;
65         }
66     return m_nFail;
67 }
68 
69 
70