A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
36 {
37 public:
41  AbstractAnimationInterfaceTestCase (std::string name);
45  virtual
51  virtual void
52  DoRun (void);
53 
54 protected:
55 
58 
59 private:
60 
61  virtual void
62  PrepareNetwork () = 0;
63 
64  virtual void
65  CheckLogic () = 0;
66 
67  virtual void
68  CheckFileExistence ();
69 
70  const char* m_traceFileName;
71 };
72 
74  TestCase (name), m_anim (NULL), m_traceFileName ("netanim-test.xml")
75 {
76 }
77 
79 {
80  delete m_anim;
81 }
82 
83 void
85 {
86  PrepareNetwork ();
87 
89 
90  Simulator::Run ();
91  CheckLogic ();
93  Simulator::Destroy ();
94 }
95 
96 void
98 {
99  FILE * fp = fopen (m_traceFileName, "r");
100  NS_TEST_ASSERT_MSG_NE (fp, 0, "Trace file was not created");
101  fclose (fp);
102  unlink (m_traceFileName);
103 }
104 
106 {
107 public:
112 
113 private:
114 
115  virtual void
116  PrepareNetwork ();
117 
118  virtual void
119  CheckLogic ();
120 
121 };
122 
124  AbstractAnimationInterfaceTestCase ("Verify AnimationInterface")
125 {
126 }
127 
128 void
130 {
131  m_nodes.Create (2);
132  AnimationInterface::SetConstantPosition (m_nodes.Get (0), 0 , 10);
133  AnimationInterface::SetConstantPosition (m_nodes.Get (1), 1 , 10);
134 
136  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
137  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
138 
140  devices = pointToPoint.Install (m_nodes);
141 
143  stack.Install (m_nodes);
144 
146  address.SetBase ("10.1.1.0", "255.255.255.0");
147 
148  Ipv4InterfaceContainer interfaces = address.Assign (devices);
149 
151 
153  serverApps.Start (Seconds (1.0));
154  serverApps.Stop (Seconds (10.0));
155 
156  UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
157  echoClient.SetAttribute ("MaxPackets", UintegerValue (100));
158  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
159  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
160 
162  clientApps.Start (Seconds (2.0));
163  clientApps.Stop (Seconds (10.0));
164 }
165 
166 void
168 {
169  NS_TEST_ASSERT_MSG_EQ (m_anim->GetTracePktCount (), 32, "Expected 32 packets traced");
170 }
171 
173 {
174 public:
179 
180 private:
181 
182  virtual void
183  PrepareNetwork ();
184 
185  virtual void
186  CheckLogic ();
187 
190  const double m_initialEnergy;
191 };
192 
194  AbstractAnimationInterfaceTestCase ("Verify Remaining energy tracing"),
195  m_initialEnergy (100)
196 {
197 }
198 
199 void
201 {
202  m_energySource = CreateObject<BasicEnergySource>();
203  m_energyModel = CreateObject<SimpleDeviceEnergyModel>();
204 
206  m_energyModel->SetEnergySource (m_energySource);
207  m_energySource->AppendDeviceEnergyModel (m_energyModel);
208  m_energyModel->SetCurrentA (20);
209 
210  m_nodes.Create (1);
211  AnimationInterface::SetConstantPosition (m_nodes.Get (0), 0 , 10);
212 
213  // aggregate energy source to node
215  // once node's energy will be depleted according to the model
216  Simulator::Stop (Seconds (2));
217 }
218 
219 void
221 {
222  const double remainingEnergy = m_energySource->GetRemainingEnergy ();
223 
224  NS_TEST_ASSERT_MSG_EQ ((remainingEnergy < m_initialEnergy), true, "Energy hasn't depleted!");
226  remainingEnergy / m_initialEnergy,
227  1.0e-13,
228  "Wrong remaining energy value was traced");
229 }
230 
232 {
233 public:
235  TestSuite ("animation-interface", UNIT)
236  {
237  AddTestCase (new AnimationInterfaceTestCase (), TestCase::QUICK);
238  AddTestCase (new AnimationRemainingEnergyTestCase (), TestCase::QUICK);
239  }
holds a vector of ns3::Application pointers.
uint64_t GetTracePktCount()
Get trace file packet count (This used only for testing)
tuple pointToPoint
Definition: first.py:28
virtual void DoRun(void)
Run unit tests for this class.
Definition: netanim-test.cc:84
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:18
A suite of tests to run.
Definition: test.h:1105
tuple echoServer
Definition: first.py:42
AbstractAnimationInterfaceTestCase(std::string name)
Constructor.
Definition: netanim-test.cc:73
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.
Build a set of PointToPointNetDevice objects.
encapsulates test code
Definition: test.h:929
Ptr< SimpleDeviceEnergyModel > m_energyModel
tuple clientApps
Definition: first.py:53
ApplicationContainer Install(Ptr< Node > node) const
Create a UdpEchoServerApplication on the specified Node.
Create a server application which waits for input UDP packets and sends them back to the original sen...
Ptr< BasicEnergySource > m_energySource
AnimationRemainingEnergyTestCase()
Constructor.
hold objects of type ns3::Time
Definition: nstime.h:1008
virtual void PrepareNetwork()
Hold an unsigned integer type.
Definition: uinteger.h:46
#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:148
AnimationInterfaceTestSuite g_animationInterfaceTestSuite
double GetNodeEnergyFraction(Ptr< const Node > node) const
Get node's energy fraction (This used only for testing)
tuple interfaces
Definition: first.py:40
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...
void AggregateObject(Ptr< Object > other)
Definition: object.cc:242
#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:355
tuple echoClient
Definition: first.py:48
tuple serverApps
Definition: first.py:44
keep track of a set of node pointers.
void AppendDeviceEnergyModel(Ptr< DeviceEnergyModel > deviceEnergyModelPtr)
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...
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
tuple stack
Definition: first.py:34
virtual ~AbstractAnimationInterfaceTestCase()
Destructor.
Definition: netanim-test.cc:78
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:589
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
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.
tuple address
Definition: first.py:37
This test suite implements a Unit Test.
Definition: test.h:1115
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