|
1 <?xml version="1.0" encoding="utf-8"?> |
|
2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. --> |
|
3 <!-- This component and the accompanying materials are made available under the terms of the License |
|
4 "Eclipse Public License v1.0" which accompanies this distribution, |
|
5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". --> |
|
6 <!-- Initial Contributors: |
|
7 Nokia Corporation - initial contribution. |
|
8 Contributors: |
|
9 --> |
|
10 <!DOCTYPE concept |
|
11 PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd"> |
|
12 <concept id="GUID-5AA3E9E4-9727-5B54-81CB-DADA73DEC51E" xml:lang="en"><title>Using |
|
13 __DECLARE_TEST</title><shortdesc>This topic explains how to use declare test macro.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody> |
|
14 <p>To illustrate the use of the <codeph>__DECLARE_TEST</codeph> macro, we |
|
15 can define the class <codeph>TEgInvariant</codeph>. It has a very simple state, |
|
16 a single data member <codeph>iData</codeph>. An object is in an invalid state |
|
17 when <codeph>iData</codeph> is greater than 100.</p> |
|
18 <codeblock id="GUID-27C84A0C-E3CE-545D-BEA0-BB211C7CA673" xml:space="preserve">class TEgInvariant |
|
19 { |
|
20 public: |
|
21 void SetData(TUint a); |
|
22 TUint Data(); |
|
23 void DoSomeThing(); |
|
24 private: |
|
25 TUint iData; |
|
26 __DECLARE_TEST; |
|
27 };</codeblock> |
|
28 <p>Then we define the getter/setter functions for <codeph>iData</codeph>:</p> |
|
29 <codeblock id="GUID-6D5BEA03-27AC-59D7-A921-236290D2379B" xml:space="preserve">void TEgInvariant::SetData(TUint a) |
|
30 { |
|
31 iData=a; |
|
32 } |
|
33 |
|
34 TUint TEgInvariant::Data() |
|
35 { |
|
36 return iData; |
|
37 }</codeblock> |
|
38 <p><codeph>TEgInvariant::DoSomeThing()</codeph> is a function that would perform |
|
39 some useful work. We verify the object’s state at its beginning and end through <codeph>__TEST_INVARIANT</codeph>. </p> |
|
40 <codeblock id="GUID-E7164503-1FFF-5522-8DAE-20C8604708D1" xml:space="preserve">void TEgInvariant::DoSomeThing() |
|
41 { |
|
42 __TEST_INVARIANT; |
|
43 |
|
44 //...do something with iData |
|
45 |
|
46 __TEST_INVARIANT; |
|
47 }</codeblock> |
|
48 <p><codeph>TEgInvariant::__DbgTestInvariant()</codeph> performs the invariance |
|
49 test:</p> |
|
50 <codeblock id="GUID-37842C4B-7A40-59E5-B5DF-5800980CBFF0" xml:space="preserve">void TEgInvariant::__DbgTestInvariant() const |
|
51 { |
|
52 #if defined(_DEBUG) |
|
53 if(iData > 100) |
|
54 User::Invariant(); |
|
55 #endif |
|
56 }</codeblock> |
|
57 <p>We could test the class with the following code:</p> |
|
58 <codeblock id="GUID-23C56C04-7218-53ED-834E-965BF2CA3A58" xml:space="preserve"> TEgInvariant Eg; |
|
59 |
|
60 Eg.SetData(10); |
|
61 Eg.DoSomeThing(); |
|
62 |
|
63 Eg.SetData(1000); |
|
64 Eg.DoSomeThing();</codeblock> |
|
65 <p>In debug builds, the second call to <codeph>DoSomeThing()</codeph> causes |
|
66 a panic, alerting us to a problem in the code that needs fixing. </p> |
|
67 </conbody></concept> |