Week 23 contribution of PDK documentation content. See release notes for details. Fixes bugs Bug 2714, Bug 462.
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License
"Eclipse Public License v1.0" which accompanies this distribution,
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
Nokia Corporation - initial contribution.
Contributors:
-->
<!DOCTYPE concept
PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-5AA3E9E4-9727-5B54-81CB-DADA73DEC51E" xml:lang="en"><title>Using
__DECLARE_TEST</title><shortdesc>This topic explains how to use declare test macro.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
<p>To illustrate the use of the <codeph>__DECLARE_TEST</codeph> macro, we
can define the class <codeph>TEgInvariant</codeph>. It has a very simple state,
a single data member <codeph>iData</codeph>. An object is in an invalid state
when <codeph>iData</codeph> is greater than 100.</p>
<codeblock id="GUID-27C84A0C-E3CE-545D-BEA0-BB211C7CA673" xml:space="preserve">class TEgInvariant
{
public:
void SetData(TUint a);
TUint Data();
void DoSomeThing();
private:
TUint iData;
__DECLARE_TEST;
};</codeblock>
<p>Then we define the getter/setter functions for <codeph>iData</codeph>:</p>
<codeblock id="GUID-6D5BEA03-27AC-59D7-A921-236290D2379B" xml:space="preserve">void TEgInvariant::SetData(TUint a)
{
iData=a;
}
TUint TEgInvariant::Data()
{
return iData;
}</codeblock>
<p><codeph>TEgInvariant::DoSomeThing()</codeph> is a function that would perform
some useful work. We verify the object’s state at its beginning and end through <codeph>__TEST_INVARIANT</codeph>. </p>
<codeblock id="GUID-E7164503-1FFF-5522-8DAE-20C8604708D1" xml:space="preserve">void TEgInvariant::DoSomeThing()
{
__TEST_INVARIANT;
//...do something with iData
__TEST_INVARIANT;
}</codeblock>
<p><codeph>TEgInvariant::__DbgTestInvariant()</codeph> performs the invariance
test:</p>
<codeblock id="GUID-37842C4B-7A40-59E5-B5DF-5800980CBFF0" xml:space="preserve">void TEgInvariant::__DbgTestInvariant() const
{
#if defined(_DEBUG)
if(iData > 100)
User::Invariant();
#endif
}</codeblock>
<p>We could test the class with the following code:</p>
<codeblock id="GUID-23C56C04-7218-53ED-834E-965BF2CA3A58" xml:space="preserve"> TEgInvariant Eg;
Eg.SetData(10);
Eg.DoSomeThing();
Eg.SetData(1000);
Eg.DoSomeThing();</codeblock>
<p>In debug builds, the second call to <codeph>DoSomeThing()</codeph> causes
a panic, alerting us to a problem in the code that needs fixing. </p>
</conbody></concept>