B
Belebele
Suppose I want to test several implementations of the same protocol
(interface with clearly defined semantics) using cppUnit. How to reuse
the test that checks the semantics?
Take, for example, the simple SetterGetter interface and the even
simpler implementations Impl1 and Impl2.
class SetterGetter {
SetterGetter& operator=(SetterGetter const& ); // Not Implemented
public:
virtual ~SetterGetter() {}
virtual void set(int ) = 0;
virtual int get() const = 0;
};
class Impl1: public SetterGetter {
/* This class clearly fails to comply with
the intended semantics, thus the protocol
test should fail when passed an object of
this class */
public:
virtual void set(int ) {}
virtual int get() const {return 0;}
};
class Impl2: public SetterGetter {
int i_;
public:
virtual void set(int i) {i_ = i;}
virtual int get() const {return i_;}
};
I would like to write a single class that tests the semantics and that
can be reused for any implementation of the protocol. Any ideas?
I made an attempt, but it did not work. The output said that there was
a Segmentation fault. See below:
class TestSetterGetter: public CppUnit::TestCase {
CPPUNIT_TEST_SUITE( TestSetterGetter );
CPPUNIT_TEST( testSetGet );
CPPUNIT_TEST_SUITE_END();
SetterGetter *psetterGetter_;
public:
TestSetterGetter(): psetterGetter_(0) {}
void set(SetterGetter& sg) {psetterGetter_ = &sg;}
void testSetGet() {
psetterGetter_->set(0);
CPPUNIT_ASSERT_EQUAL(0, psetterGetter_->get());
}
};
class TestImpl1: public TestSetterGetter {
Impl1 impl1_;
public:
TestImpl1() {set(impl1_);}
};
CPPUNIT_TEST_SUITE_REGISTRATION( TestImpl1 );
class TestImpl2: public TestSetterGetter {
Impl2 impl2_;
public:
TestImpl2() {set(impl2_);}
};
CPPUNIT_TEST_SUITE_REGISTRATION( TestImpl2 );
(interface with clearly defined semantics) using cppUnit. How to reuse
the test that checks the semantics?
Take, for example, the simple SetterGetter interface and the even
simpler implementations Impl1 and Impl2.
class SetterGetter {
SetterGetter& operator=(SetterGetter const& ); // Not Implemented
public:
virtual ~SetterGetter() {}
virtual void set(int ) = 0;
virtual int get() const = 0;
};
class Impl1: public SetterGetter {
/* This class clearly fails to comply with
the intended semantics, thus the protocol
test should fail when passed an object of
this class */
public:
virtual void set(int ) {}
virtual int get() const {return 0;}
};
class Impl2: public SetterGetter {
int i_;
public:
virtual void set(int i) {i_ = i;}
virtual int get() const {return i_;}
};
I would like to write a single class that tests the semantics and that
can be reused for any implementation of the protocol. Any ideas?
I made an attempt, but it did not work. The output said that there was
a Segmentation fault. See below:
class TestSetterGetter: public CppUnit::TestCase {
CPPUNIT_TEST_SUITE( TestSetterGetter );
CPPUNIT_TEST( testSetGet );
CPPUNIT_TEST_SUITE_END();
SetterGetter *psetterGetter_;
public:
TestSetterGetter(): psetterGetter_(0) {}
void set(SetterGetter& sg) {psetterGetter_ = &sg;}
void testSetGet() {
psetterGetter_->set(0);
CPPUNIT_ASSERT_EQUAL(0, psetterGetter_->get());
}
};
class TestImpl1: public TestSetterGetter {
Impl1 impl1_;
public:
TestImpl1() {set(impl1_);}
};
CPPUNIT_TEST_SUITE_REGISTRATION( TestImpl1 );
class TestImpl2: public TestSetterGetter {
Impl2 impl2_;
public:
TestImpl2() {set(impl2_);}
};
CPPUNIT_TEST_SUITE_REGISTRATION( TestImpl2 );