473,657 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Jul 22 '05 #1
0 1765

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1913
by: Magnus Lie Hetland | last post by:
Hi! I've been looking at ways of dealing with nested structures in regexps (becuase I figured that would be faster than the Python parsing code I've currently got) and came across a few interesting things... First there is this, mentioning the (?<DEPTH>) and (?<-DEPTH>) constructs of the .NET regexp matcher: http://www.rassoc.com/gregr/weblog/archive.aspx?post=590
6
1662
by: Phil | last post by:
Hello I'm trying to display all DIV tags of a document : Example : + <DIV id="1"> - <DIV id="1-1"></DIV> </DIV> + <DIV id="2"> - <DIV id="2-1"></DIV>
0
1492
by: Phlip | last post by:
C++ newsgroupies: I wrote a parser to solve math expressions like "3.0 ^(4 - 5)", or "3 / 8". Below my sig is recursiveDescentParser.cpp, the test suite that drove the implementation of the expression solver found in recursiveDescentParser.h, in a parallel post. Test-Driven Development works by accumulating assertions. Before the very first assertion existed...
6
1894
by: Phlip | last post by:
C++ newsgroupies: I wrote a parser to solve math expressions like "3.0 ^(4 - 5)", or "3 / 8". It's bad luck to name an interface after its implementation. This file could have been "expressionSolver.h". But naturally, if I set out to implement recursive descent parsing, that tainted my filename selection. Below my sig is recursiveDescentParser.h, a math expression solver. Using the test framework, and test suite appearing in parallel...
6
2425
by: Jon Shemitz | last post by:
I just wrote my second method that reads an XML stream with XmlReader and spits out a instance of an object that represents the XML stream. In effect, this was a simple recursive descent parser that uses the XmlReader as a tokenizer. Realizing this naturally led to the question: Are there any tools which would let me supply a description of the domain semantics (perhaps in XSD) and which would construct a parser shell that can turn a...
9
3317
by: seberino | last post by:
I'm a compiler newbie and curious if Python grammar is able to be parsed by a recursive descent parser or if it requires a more powerful algorithm. Chris
4
1947
by: seberino | last post by:
I'm a compiler newbie and was curious if Python's language/grammar can be handled by a recursive descent parser. Well? Chris
18
4711
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: from pyparsing import * grammar = OneOrMore(Word(alphas)) + Literal('end') grammar.parseString('First Second Third end')
0
8312
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.