Using __DECLARE_TEST

This topic explains how to use declare test macro.

To illustrate the use of the __DECLARE_TEST macro, we can define the class TEgInvariant. It has a very simple state, a single data member iData. An object is in an invalid state when iData is greater than 100.

class TEgInvariant 
 {
public:
 void SetData(TUint a);
 TUint Data();
 void DoSomeThing();
private:
 TUint iData;
__DECLARE_TEST;
 };

Then we define the getter/setter functions for iData:

void TEgInvariant::SetData(TUint a)
 {
 iData=a;
 }

TUint TEgInvariant::Data()
 {
 return iData;
 }

TEgInvariant::DoSomeThing() is a function that would perform some useful work. We verify the object’s state at its beginning and end through __TEST_INVARIANT.

void TEgInvariant::DoSomeThing()
 {
 __TEST_INVARIANT;

 //...do something with iData

 __TEST_INVARIANT;
 }

TEgInvariant::__DbgTestInvariant() performs the invariance test:

void TEgInvariant::__DbgTestInvariant() const
 {
#if defined(_DEBUG)
 if(iData > 100)
  User::Invariant();
#endif
 }

We could test the class with the following code:

 TEgInvariant Eg; 

 Eg.SetData(10);
 Eg.DoSomeThing();

 Eg.SetData(1000);
 Eg.DoSomeThing();

In debug builds, the second call to DoSomeThing() causes a panic, alerting us to a problem in the code that needs fixing.