A Discrete-Event Network Simulator
API
netanim-test.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * Author: John Abraham <john.abraham@gatech.edu>
16  * Contributions: Eugene Kalishenko <ydginster@gmail.com> (Open Source and Linux Laboratory
17  * http://wiki.osll.ru/doku.php/start)
18  */
19 
20 #include "unistd.h"
21 
22 #include "ns3/basic-energy-source.h"
23 #include "ns3/core-module.h"
24 #include "ns3/internet-module.h"
25 #include "ns3/netanim-module.h"
26 #include "ns3/network-module.h"
27 #include "ns3/point-to-point-layout-module.h"
28 #include "ns3/point-to-point-module.h"
29 #include "ns3/simple-device-energy-model.h"
30 #include "ns3/udp-echo-helper.h"
31 
32 #include <iostream>
33 
34 using namespace ns3;
35 
48 {
49  public:
54  AbstractAnimationInterfaceTestCase(std::string name);
60  void DoRun() override;
61 
62  protected:
65 
66  private:
68  virtual void PrepareNetwork() = 0;
69 
71  virtual void CheckLogic() = 0;
72 
74  virtual void CheckFileExistence();
75 
76  const char* m_traceFileName;
77 };
78 
80  : TestCase(name),
81  m_anim(nullptr),
82  m_traceFileName("netanim-test.xml")
83 {
84 }
85 
87 {
88  delete m_anim;
89 }
90 
91 void
93 {
95 
97 
98  Simulator::Run();
99  CheckLogic();
101  Simulator::Destroy();
102 }
103 
104 void
106 {
107  FILE* fp = fopen(m_traceFileName, "r");
108  NS_TEST_ASSERT_MSG_NE(fp, nullptr, "Trace file was not created");
109  fclose(fp);
110  unlink(m_traceFileName);
111 }
112 
119 {
120  public:
125 
126  private:
127  void PrepareNetwork() override;
128 
129  void CheckLogic() override;
130 };
131 
133  : AbstractAnimationInterfaceTestCase("Verify AnimationInterface")
134 {
135 }
136 
137 void
139 {
140  m_nodes.Create(2);
141  AnimationInterface::SetConstantPosition(m_nodes.Get(0), 0, 10);
142  AnimationInterface::SetConstantPosition(m_nodes.Get(1), 1, 10);
143 
145  pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
146  pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
147 
149  devices = pointToPoint.Install(m_nodes);
150 
152  stack.Install(m_nodes);
153 
155  address.SetBase("10.1.1.0", "255.255.255.0");
156 
158 
160 
162  serverApps.Start(Seconds(1.0));
163  serverApps.Stop(Seconds(10.0));
164 
165  UdpEchoClientHelper echoClient(interfaces.GetAddress(1), 9);
166  echoClient.SetAttribute("MaxPackets", UintegerValue(100));
167  echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
168  echoClient.SetAttribute("PacketSize", UintegerValue(1024));
169 
171  clientApps.Start(Seconds(2.0));
172  clientApps.Stop(Seconds(10.0));
173 }
174 
175 void
177 {
178  NS_TEST_ASSERT_MSG_EQ(m_anim->GetTracePktCount(), 16, "Expected 16 packets traced");
179 }
180 
187 {
188  public:
193 
194  private:
195  void PrepareNetwork() override;
196 
197  void CheckLogic() override;
198 
201  const double m_initialEnergy;
202 };
203 
205  : AbstractAnimationInterfaceTestCase("Verify Remaining energy tracing"),
206  m_initialEnergy(100)
207 {
208 }
209 
210 void
212 {
213  m_energySource = CreateObject<BasicEnergySource>();
214  m_energyModel = CreateObject<SimpleDeviceEnergyModel>();
215 
220 
221  m_nodes.Create(1);
222  AnimationInterface::SetConstantPosition(m_nodes.Get(0), 0, 10);
223 
224  // aggregate energy source to node
226  // once node's energy will be depleted according to the model
227  Simulator::Stop(Seconds(1));
228 }
229 
230 void
232 {
233  const double remainingEnergy = m_energySource->GetRemainingEnergy();
234 
235  NS_TEST_ASSERT_MSG_EQ((remainingEnergy < m_initialEnergy), true, "Energy hasn't depleted!");
237  remainingEnergy / m_initialEnergy,
238  1.0e-13,
239  "Wrong remaining energy value was traced");
240 }
241 
248 {
249  public:
251  : TestSuite("animation-interface", UNIT)
252  {
253  AddTestCase(new AnimationInterfaceTestCase(), TestCase::QUICK);
254  AddTestCase(new AnimationRemainingEnergyTestCase(), TestCase::QUICK);
255  }
Abstract Animation Interface Test Case.
Definition: netanim-test.cc:48
virtual void CheckFileExistence()
Check file existence.
AbstractAnimationInterfaceTestCase(std::string name)
Constructor.
Definition: netanim-test.cc:79
AnimationInterface * m_anim
animation
Definition: netanim-test.cc:64
const char * m_traceFileName
trace file name
Definition: netanim-test.cc:76
NodeContainer m_nodes
the nodes
Definition: netanim-test.cc:63
virtual void CheckLogic()=0
Check logic function.
~AbstractAnimationInterfaceTestCase() override
Destructor.
Definition: netanim-test.cc:86
virtual void PrepareNetwork()=0
Prepare network function.
void DoRun() override
Implementation to actually run this TestCase.
Definition: netanim-test.cc:92
Animation Interface Test Case.
AnimationInterfaceTestCase()
Constructor.
void CheckLogic() override
Check logic function.
void PrepareNetwork() override
Prepare network function.
Animation Interface Test Suite.
Animation Remaining Energy Test Case.
const double m_initialEnergy
initial energy
AnimationRemainingEnergyTestCase()
Constructor.
Ptr< SimpleDeviceEnergyModel > m_energyModel
energy model
Ptr< BasicEnergySource > m_energySource
energy source
void CheckLogic() override
Check logic function.
void PrepareNetwork() override
Prepare network function.
Interface to network animator.
double GetNodeEnergyFraction(Ptr< const Node > node) const
Get node's energy fraction (This used only for testing)
uint64_t GetTracePktCount() const
Get trace file packet count (This used only for testing)
holds a vector of ns3::Application pointers.
double GetRemainingEnergy() override
void SetInitialEnergy(double initialEnergyJ)
void AppendDeviceEnergyModel(Ptr< DeviceEnergyModel > deviceEnergyModelPtr)
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:259
Build a set of PointToPointNetDevice objects.
void SetEnergySource(Ptr< EnergySource > source) override
Sets pointer to EnergySource installed on node.
Hold variables of type string.
Definition: string.h:56
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
Create an application which sends a UDP packet and waits for an echo of this packet.
Create a server application which waits for input UDP packets and sends them back to the original sen...
Hold an unsigned integer type.
Definition: uinteger.h:45
AnimationInterfaceTestSuite g_animationInterfaceTestSuite
the test suite
#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:144
#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:564
#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:337
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
echoClient
Definition: first.py:59
address
Definition: first.py:47
serverApps
Definition: first.py:54
pointToPoint
Definition: first.py:38
echoServer
Definition: first.py:52
clientApps
Definition: first.py:64
devices
Definition: first.py:42
stack
Definition: first.py:44
interfaces
Definition: first.py:50
Every class exported by the ns3 library is enclosed in the ns3 namespace.