diff -r 43e37759235e -r 51a74ef9ed63 Symbian3/SDK/Source/GUID-5AA3E9E4-9727-5B54-81CB-DADA73DEC51E.dita --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Symbian3/SDK/Source/GUID-5AA3E9E4-9727-5B54-81CB-DADA73DEC51E.dita Wed Mar 31 11:11:55 2010 +0100 @@ -0,0 +1,67 @@ + + + + + +Using +__DECLARE_TESTThis 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.

+
\ No newline at end of file