[Snippet] a Recursive Descent Parser via TDD - test.h

Discussion in 'C++' started by Phlip, Aug 2, 2004.

  1. Phlip

    Phlip Guest

    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/bin/view/Main/TestFirstUserInterfaces


    // 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::stringstream;

    class
    TestCase
    {
    public:
    typedef std::list<TestCase *> 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_passed;
    };

    bool
    TestCase::runTests()
    {
    TestCase::TestCases_t::iterator it(TestCase::cases.begin());

    for ( ; it != TestCase::cases.end(); ++it )
    {
    TestCase & aCase = **it;
    aCase.setUp();
    aCase.runCase();
    aCase.tearDown();
    }
    return TestCase::all_tests_passed;
    }

    #define TEST_(suite, target) \
    struct suite##target: virtual suite \
    { void runCase(); } \
    a##suite##target; \
    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; \
    OutputDebugStringA(out.str().c_str()); \
    OutputDebugStringA("\n"); \
    all_tests_passed = false; \
    __asm { int 3 } }

    bool tolerance(double Subject, double Reference)
    {
    return fabs(Subject - Reference) < (0.001 + fabs(Subject)) / 100;
    }

    #define CPPUNIT_ASSERT_CLOSE(sample, result) \
    if (!tolerance(sample, result)) { stringstream out; \
    out << __FILE__ << "(" << __LINE__ << ") : "; \
    out << #sample << "(" << (sample) << ") != "; \
    out << #result << "(" << (result) << ")"; \
    cout << out.str() << endl; \
    OutputDebugStringA(out.str().c_str()); \
    OutputDebugStringA("\n"); \
    all_tests_passed = false; \
    __asm { int 3 } }

    #endif
    Phlip, Aug 2, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phlip
    Replies:
    0
    Views:
    456
    Phlip
    Aug 2, 2004
  2. Phlip
    Replies:
    6
    Views:
    425
    Phlip
    Aug 5, 2004
  3. Replies:
    4
    Views:
    447
    Ben Sizer
    Oct 2, 2006
  4. Just Another Victim of the Ambient Morality

    Is pyparsing really a recursive descent parser?

    Just Another Victim of the Ambient Morality, Nov 2, 2007, in forum: Python
    Replies:
    39
    Views:
    1,373
    Kay Schluehr
    Nov 9, 2007
  5. Robert
    Replies:
    1
    Views:
    574
    Puppet_Sock
    Apr 14, 2008
Loading...

Share This Page