A Discrete-Event Network Simulator
API
netanim-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Author: John Abraham <john.abraham@gatech.edu>
17  * Contributions: Eugene Kalishenko <ydginster@gmail.com> (Open Source and Linux Laboratory http://dev.osll.ru/)
18  */
19 
20 #include <iostream>
21 #include "unistd.h"
22 
23 #include "ns3/core-module.h"
24 #include "ns3/network-module.h"
25 #include "ns3/internet-module.h"
26 #include "ns3/point-to-point-module.h"
27 #include "ns3/netanim-module.h"
28 #include "ns3/applications-module.h"
29 #include "ns3/point-to-point-layout-module.h"
30 #include "ns3/basic-energy-source.h"
31 #include "ns3/simple-device-energy-model.h"
32 
33 using namespace ns3;
34 
48 {
49 public:
54  AbstractAnimationInterfaceTestCase (std::string name);
58  virtual
64  virtual void
65  DoRun (void);
66 
67 protected:
68 
71 
72 private:
73 
75  virtual void PrepareNetwork () = 0;
76 
78  virtual void CheckLogic () = 0;
79 
81  virtual void CheckFileExistence ();
82 
83  const char* m_traceFileName;
84 };
85 
87  TestCase (name), m_anim (NULL), m_traceFileName ("netanim-test.xml")
88 {
89 }
90 
92 {
93  delete m_anim;
94 }
95 
96 void
98 {
99  PrepareNetwork ();
100 
102 
103  Simulator::Run ();
104  CheckLogic ();
106  Simulator::Destroy ();
107 }
108 
109 void
111 {
112  FILE * fp = fopen (m_traceFileName, "r");
113  NS_TEST_ASSERT_MSG_NE (fp, 0, "Trace file was not created");
114  fclose (fp);
115  unlink (m_traceFileName);
116 }
117 
125 {
126 public:
131 
132 private:
133 
134  virtual void
135  PrepareNetwork ();
136 
137  virtual void
138  CheckLogic ();
139 
140 };
141 
143  AbstractAnimationInterfaceTestCase ("Verify AnimationInterface")
144 {
145 }
146 
147 void
149 {
150  m_nodes.Create (2);
151  AnimationInterface::SetConstantPosition (m_nodes.Get (0), 0 , 10);
152  AnimationInterface::SetConstantPosition (m_nodes.Get (1), 1 , 10);
153 
155  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
156  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
157 
159  devices = pointToPoint.Install (m_nodes);
160 
162  stack.Install (m_nodes);
163 
165  address.SetBase ("10.1.1.0", "255.255.255.0");
166 
168 
170 
172  serverApps.Start (Seconds (1.0));
173  serverApps.Stop (Seconds (10.0));
174 
175  UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
176  echoClient.SetAttribute ("MaxPackets", UintegerValue (100));
177  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
178  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
179 
181  clientApps.Start (Seconds (2.0));
182  clientApps.Stop (Seconds (10.0));
183 }
184 
185 void
187 {
188  NS_TEST_ASSERT_MSG_EQ (m_anim->GetTracePktCount (), 16, "Expected 16 packets traced");
189 }
190 
198 {
199 public:
204 
205 private:
206 
207  virtual void
208  PrepareNetwork ();
209 
210  virtual void
211  CheckLogic ();
212 
215  const double m_initialEnergy;
216 };
217 
219  AbstractAnimationInterfaceTestCase ("Verify Remaining energy tracing"),
220  m_initialEnergy (100)
221 {
222 }
223 
224 void
226 {
227  m_energySource = CreateObject<BasicEnergySource>();
228  m_energyModel = CreateObject<SimpleDeviceEnergyModel>();
229 
234 
235  m_nodes.Create (1);
236  AnimationInterface::SetConstantPosition (m_nodes.Get (0), 0 , 10);
237 
238  // aggregate energy source to node
240  // once node's energy will be depleted according to the model
241  Simulator::Stop (Seconds (1));
242 }
243 
244 void
246 {
247  const double remainingEnergy = m_energySource->GetRemainingEnergy ();
248 
249  NS_TEST_ASSERT_MSG_EQ ((remainingEnergy < m_initialEnergy), true, "Energy hasn't depleted!");
251  remainingEnergy / m_initialEnergy,
252  1.0e-13,
253  "Wrong remaining energy value was traced");
254 }
255 
263 {
264 public:
266  TestSuite ("animation-interface", UNIT)
267  {
268  AddTestCase (new AnimationInterfaceTestCase (), TestCase::QUICK);
269  AddTestCase (new AnimationRemainingEnergyTestCase (), TestCase::QUICK);
270  }
holds a vector of ns3::Application pointers.
uint64_t GetTracePktCount()
Get trace file packet count (This used only for testing)
AnimationInterface * m_anim
animation
Definition: netanim-test.cc:70
virtual void DoRun(void)
Run unit tests for this class.
Definition: netanim-test.cc:97
virtual void CheckFileExistence()
Check file existence.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Hold variables of type string.
Definition: string.h:41
virtual void PrepareNetwork()=0
Prepare network function.
A suite of tests to run.
Definition: test.h:1342
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
AbstractAnimationInterfaceTestCase(std::string name)
Constructor.
Definition: netanim-test.cc:86
virtual double GetRemainingEnergy(void)
serverApps
Definition: first.py:45
Create an application which sends a UDP packet and waits for an echo of this packet.
echoServer
Definition: first.py:43
aggregate IP/TCP/UDP functionality to existing Nodes.
AnimationInterfaceTestSuite g_animationInterfaceTestSuite
the test suite
Build a set of PointToPointNetDevice objects.
encapsulates test code
Definition: test.h:1155
Animation Interface Test Case.
Ptr< SimpleDeviceEnergyModel > m_energyModel
energy model
stack
Definition: first.py:34
virtual void SetEnergySource(Ptr< EnergySource > source)
Sets pointer to EnergySouce installed on node.
const double m_initialEnergy
initial energy
Create a server application which waits for input UDP packets and sends them back to the original sen...
virtual void CheckLogic()
Check logic function.
Ptr< BasicEnergySource > m_energySource
energy source
AnimationRemainingEnergyTestCase()
Constructor.
virtual void CheckLogic()
Check logic function.
AttributeValue implementation for Time.
Definition: nstime.h:1124
virtual void PrepareNetwork()
Prepare network function.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
virtual void PrepareNetwork()
Prepare network function.
echoClient
Definition: first.py:49
Hold an unsigned integer type.
Definition: uinteger.h:44
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:168
pointToPoint
Definition: first.py:28
holds a vector of ns3::NetDevice pointers
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition: test.h:380
Animation Interface Test Suite.
const char * m_traceFileName
trace file name
Definition: netanim-test.cc:83
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
address
Definition: first.py:37
void AppendDeviceEnergyModel(Ptr< DeviceEnergyModel > deviceEnergyModelPtr)
NodeContainer m_nodes
the nodes
Definition: netanim-test.cc:69
AnimationInterfaceTestCase()
Constructor.
Animation Remaining Energy Test Case.
Abstract Animation Interface Test Case.
Definition: netanim-test.cc:47
virtual ~AbstractAnimationInterfaceTestCase()
Destructor.
Definition: netanim-test.cc:91
double GetNodeEnergyFraction(Ptr< const Node > node) const
Get node&#39;s energy fraction (This used only for testing)
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not...
Definition: test.h:624
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
interfaces
Definition: first.py:41
Interface to network animator.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
clientApps
Definition: first.py:54
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetInitialEnergy(double initialEnergyJ)
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
virtual void CheckLogic()=0
Check logic function.
devices
Definition: first.py:32
This test suite implements a Unit Test.
Definition: test.h:1352