C++ newsgroupies:
I wrote a parser to solve math expressions like "3.0 ^(4 - 5)", or "3 / 8".
Below my sig is test.h, the file containing a tiny test framework. Using it
and Test-Driven Development, I produced the test suite and the parser,
appearing in parallel posts.
--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
// a NanoCppUnit in one header...
#ifndef TEST_
# include <list>
# include <iostream>
# include <sstream>
# define WIN32_LEAN_AND_ MEAN
# include <windows.h>
# include <math.h>
using std::cout;
using std::endl;
using std::stringstre am;
class
TestCase
{
public:
typedef std::list<TestC ase *> TestCases_t;
TestCases_t static cases; // TODO make me private
TestCase() { cases.push_back (this); }
virtual void setUp() {}
virtual void runCase() = 0;
virtual void tearDown() {}
static bool runTests();
protected:
static bool all_tests_passe d;
};
bool
TestCase::runTe sts()
{
TestCase::TestC ases_t::iterato r it(TestCase::ca ses.begin());
for ( ; it != TestCase::cases .end(); ++it )
{
TestCase & aCase = **it;
aCase.setUp();
aCase.runCase() ;
aCase.tearDown( );
}
return TestCase::all_t ests_passed;
}
#define TEST_(suite, target) \
struct suite##target: virtual suite \
{ void runCase(); } \
a##suite##targe t; \
void suite##target:: runCase()
#define CPPUNIT_ASSERT_ EQUAL(sample, result) \
if ((sample) != (result)) { stringstream out; \
out << __FILE__ << "(" << __LINE__ << ") : "; \
out << #sample << "(" << (sample) << ") != "; \
out << #result << "(" << (result) << ")"; \
cout << out.str() << endl; \
OutputDebugStri ngA(out.str().c _str()); \
OutputDebugStri ngA("\n"); \
all_tests_passe d = false; \
__asm { int 3 } }
bool tolerance(doubl e Subject, double Reference)
{
return fabs(Subject - Reference) < (0.001 + fabs(Subject)) / 100;
}
#define CPPUNIT_ASSERT_ CLOSE(sample, result) \
if (!tolerance(sam ple, result)) { stringstream out; \
out << __FILE__ << "(" << __LINE__ << ") : "; \
out << #sample << "(" << (sample) << ") != "; \
out << #result << "(" << (result) << ")"; \
cout << out.str() << endl; \
OutputDebugStri ngA(out.str().c _str()); \
OutputDebugStri ngA("\n"); \
all_tests_passe d = false; \
__asm { int 3 } }
#endif