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 
167  Ipv4InterfaceContainer interfaces = address.Assign (devices);
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 
231  m_energyModel->SetEnergySource (m_energySource);
232  m_energySource->AppendDeviceEnergyModel (m_energyModel);
233  m_energyModel->SetCurrentA (20);
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 (2));
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
tuple pointToPoint
Definition: first.py:28
virtual void DoRun(void)
Run unit tests for this class.
Definition: netanim-test.cc:97
virtual void CheckFileExistence()
Check file existence.
tuple devices
Definition: first.py:32
holds a vector of std::pair of Ptr 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
tuple echoServer
Definition: first.py:43
AbstractAnimationInterfaceTestCase(std::string name)
Constructor.
Definition: netanim-test.cc:86
virtual double GetRemainingEnergy(void)
Create an application which sends a UDP packet and waits for an echo of this packet.
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
This test suite implements a Unit Test.
Definition: test.h:1352
Animation Interface Test Case.
Ptr< SimpleDeviceEnergyModel > m_energyModel
energy model
tuple clientApps
Definition: first.py:54
ApplicationContainer Install(Ptr< Node > node) const
Create a UdpEchoServerApplication on the specified 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:1055
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.
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
double GetNodeEnergyFraction(Ptr< const Node > node) const
Get node's energy fraction (This used only for testing)
tuple interfaces
Definition: first.py:41
holds a vector of ns3::NetDevice pointers
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
#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.
tuple echoClient
Definition: first.py:49
tuple serverApps
Definition: first.py:45
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.
void AppendDeviceEnergyModel(Ptr< DeviceEnergyModel > deviceEnergyModelPtr)
NodeContainer m_nodes
the nodes
Definition: netanim-test.cc:69
AnimationInterfaceTestCase()
Constructor.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Animation Remaining Energy Test Case.
Abstract Animation Interface Test Case.
Definition: netanim-test.cc:47
tuple stack
Definition: first.py:34
virtual ~AbstractAnimationInterfaceTestCase()
Destructor.
Definition: netanim-test.cc:91
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
#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
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
Interface to network animator.
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.
tuple address
Definition: first.py:37
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const