##

P

Phlip

nik said:
plz tell me the use of ##
by giving example

It's the "token pasting" operator. Use it to combine token fragments into a
complete identifier.

Here's a complete, useful example. It tests a WTL dialog box. (Fear not,
topic police, we see little Windows Template Library code here, and the
actual TEST_() macro itself is quite portable.)

I like the TEST_() macro better than the usual Test Suite system (where you

must write extra code to push each Test Case into its Suite's list), so I
wrote a CppUnit clone that uses _only_ the TEST_() macro.

Firstly, all TestCases obey the Abstract Template pattern:

class
TestCase
{
public:
virtual void setUp() {}
virtual void runCase() = 0;
virtual void tearDown() {}
};

Their caller will iterate a polymorphic list of cases, and call setUp(),
runCase(), and tearDown() on each one.

To test a WTL dialog box called ProjectDlg, with a name and address on it,
we write a Suite called TestDialog:

class
TestDialog: public TestCase
{
public:
ProjectDlg m_aDlg;

virtual void setUp()
{ m_aDlg.Create(HWND_TOP); }

virtual void tearDown()
{ m_aDlg.DestroyWindow(); }

TestDialog(): m_aDlg(xml) {}
};

Now we naively derive one concrete class for each of two test cases:

class
TestDialog_first_name: public TestDialog
{
public:
void
runCase()
{
CPPUNIT_ASSERT_EQUAL( "Ignatz",
m_aDlg.getText(IDC_EDIT_FIRST_NAME) );
}
};

class
TestDialog_last_name: public TestDialog
{
public:
void
runCase()
{
CPPUNIT_ASSERT_EQUAL( "Mouse",
m_aDlg.getText(IDC_EDIT_LAST_NAME) );
}
};

Then we get this primordial test rig to pass, using an unrolled version of
the loop we will soon write:

int
main()
{
try {
TestDialog_first_name test1;
test1.setUp();
test1.runCase();
test1.tearDown();

TestDialog_last_name test2;
test2.setUp();
test2.runCase();
test2.tearDown();
}
catch(_com_error & e)
{
guard_(e);
ELIDE_(__asm { int 3 });
return 1;
}
return 0;
}

(Notice, despite I'm using that evil OLE COM ActiveX whatever library from
Microsoft, my code is not full of extra crud like CComBSTR or
CoCreateInstance(). Something chased it all away!)

From here, we need to upgrade TestCase et al to automatically collect the
Test Cases. They should not appear twice - once among the test suites and
again in main(). Removing behavioral duplication tends to improve code's
style.

Make each TestCase instance push itself into a static member list:

#include <list>

class
TestCase
{
public:
typedef std::list<TestCase *> TestCases_t;
TestCases_t static cases;

TestCase() { cases.push_back(this); }
virtual void setUp() {}
virtual void runCase() = 0;
virtual void tearDown() {}
};

TestCase::TestCases_t TestCase::cases;
....
class
TestDialog_last_name: public TestDialog
{
public:
void
runCase()
{
CPPUNIT_ASSERT_EQUAL( "Mouse",
m_aDlg.getText(IDC_EDIT_LAST_NAME) );
}
};
TestDialog_last_name test2;

(Remember that after each few edits all tests must still pass!) Now upgrade
main() to use the list:

int
main()
{
try {
TestCase::TestCases_t::iterator it (TestCase::cases.begin());

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

We continue to seek things that look similar, make them the same, and merge
their duplication.

The classes TestDialog_first_name (not shown) and TestDialog_last_name now
look very similar, but so do their object instances. To make them all the
same, we need to declare a class and instantiate an object in one command:

#define TEST_(suite, target) \
struct suite##target: public suite \
{ void runCase(); } \
a##suite##target; \
void suite##target::runCase()

TEST_(TestDialog, first_name)
{
CPPUNIT_ASSERT_EQUAL( "Ignatz",
m_aDlg.getText(IDC_EDIT_FIRST_NAME) );
}

TEST_(TestDialog, last_name)
{
CPPUNIT_ASSERT_EQUAL( "Mouse",
m_aDlg.getText(IDC_EDIT_LAST_NAME) );
}

The object instances went inside the TEST_() macro, which uses
token##pasting to give them unique names. But they all must construct,
globally, so their constructors can register them with the list of cases.

This pattern makes Test Fixtures easy. Classes like TestDialog take care of
setting up and tearing down tested objects, providing a framework to share
other common operations on the tested objects. Test cases are now easy. The
Test Collector pattern ensures when we write new cases, we can't forget to
add them to a remote list of cases.

But these Test Cases no longer look like normal C++ functions!
 
C

Christopher Benson-Manica

Phlip said:
What does the ISO C++ Standard call it?

<ot>Microsoft has said the following concerning C#:

"The spoken name of the language is "C sharp" in reference to the
musical "sharp" sign, which increases a tone denoted by a letter
(between A and G) by half a tone. However, for ease of typing it was
decided to represent the sharp sign by a pound symbol [3] (which is on
any keyboard) rather than the "musically correct" Unicode sharp
sign. The Microsoft and ECMA 334 representation symbols thus agree:
the # in C# is the pound sign, but it represents a sharp sign. Think
of it in the same way as the <= glyph in C languages which is a less
than sign and an equals sign, but represents a less-than-or-equals
sign."</ot>

On a somewhat more topical note, the n869 draft of the C standard only
names the '#' once and calls it a "sharp sign".

One could always call it an "octothorpe" :)
 
R

Rolf Magnus

Christopher said:
On a somewhat more topical note, the n869 draft of the C standard only
names the '#' once and calls it a "sharp sign".

The final version does that too. The C++ standard doesn't even contain the
word "sharp" at all. AFAICS, it doesn't give that symbol any name at all.
 
C

Christopher Benson-Manica

Rolf Magnus said:
The final version does that too. The C++ standard doesn't even contain the
word "sharp" at all. AFAICS, it doesn't give that symbol any name at all.

That seems to have been a wise decision on the Committees' parts.
 
M

Marcus Kwok

Christopher Benson-Manica said:
On a somewhat more topical note, the n869 draft of the C standard only
names the '#' once and calls it a "sharp sign".

One could always call it an "octothorpe" :)

Sometimes I feel the need to call it "pound". As in, I pronounce
#include as "pound include".
 
R

Rat Monkey Hybrid

Marcus said:
Sometimes I feel the need to call it "pound". As in, I pronounce
#include as "pound include".

As a Brit, I prefer to call it "hash" as in "hash include" or "hash
define". My American coworkers think I'm strange...
 
V

Victor Bazarov

Rat said:
As a Brit, I prefer to call it "hash" as in "hash include" or "hash
define". My American coworkers think I'm strange...

We are all strange for keeping coming back to "discuss" this nonsense.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top