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 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
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 ();
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);
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 
205  m_energySource->SetInitialEnergy (m_initialEnergy);
206  m_energyModel->SetEnergySource (m_energySource);
207  m_energySource->AppendDeviceEnergyModel (m_energyModel);
208  m_energyModel->SetCurrentA (20);
209 
210  m_nodes.Create (1);
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  {
239  }
241 
242 } // namespace ns3
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
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
tuple devices
Definition: first.py:32
ns3::AnimationInterfaceTestSuite g_animationInterfaceTestSuite
holds a vector of std::pair of Ptr and interface index.
hold variables of type string
Definition: string.h:19
#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:326
A suite of tests to run.
Definition: test.h:1025
tuple echoServer
Definition: first.py:42
Create an application which sends a UDP packet and waits for an echo of this packet.
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
aggregate IP/TCP/UDP functionality to existing Nodes.
#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:539
Build a set of PointToPointNetDevice objects.
encapsulates test code
Definition: test.h:849
virtual ~AbstractAnimationInterfaceTestCase()
Destructor.
Definition: netanim-test.cc:78
tuple clientApps
Definition: first.py:53
ApplicationContainer Install(Ptr< Node > node) const
Create a UdpEchoServerApplication on the specified Node.
static void SetConstantPosition(Ptr< Node > n, double x, double y, double z=0)
Helper function to set Constant Position for a given node.
Create a server application which waits for input UDP packets and sends them back to the original sen...
hold objects of type ns3::Time
Definition: nstime.h:961
AnimationInterfaceTestCase()
Constructor.
Hold an unsigned integer type.
Definition: uinteger.h:46
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
Ptr< SimpleDeviceEnergyModel > m_energyModel
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:243
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
tuple echoClient
Definition: first.py:48
tuple serverApps
Definition: first.py:44
keep track of a set of node pointers.
virtual void DoRun(void)
Run unit tests for this class.
Definition: netanim-test.cc:84
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:173
tuple stack
Definition: first.py:34
Fast test.
Definition: test.h:857
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...
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
AbstractAnimationInterfaceTestCase(std::string name)
Constructor.
Definition: netanim-test.cc:73
Interface to network animator.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
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:1035
Ptr< BasicEnergySource > m_energySource
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
#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:137
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const