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://dev.osll.ru/)
18 */
19
20#include "unistd.h"
21
22#include "ns3/applications-module.h"
23#include "ns3/basic-energy-source.h"
24#include "ns3/core-module.h"
25#include "ns3/internet-module.h"
26#include "ns3/netanim-module.h"
27#include "ns3/network-module.h"
28#include "ns3/point-to-point-layout-module.h"
29#include "ns3/point-to-point-module.h"
30#include "ns3/simple-device-energy-model.h"
31
32#include <iostream>
33
34using 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
91void
93{
95
97
98 Simulator::Run();
99 CheckLogic();
101 Simulator::Destroy();
102}
103
104void
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
120{
121 public:
126
127 private:
128 void PrepareNetwork() override;
129
130 void CheckLogic() override;
131};
132
134 : AbstractAnimationInterfaceTestCase("Verify AnimationInterface")
135{
136}
137
138void
140{
141 m_nodes.Create(2);
142 AnimationInterface::SetConstantPosition(m_nodes.Get(0), 0, 10);
143 AnimationInterface::SetConstantPosition(m_nodes.Get(1), 1, 10);
144
146 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
147 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
148
150 devices = pointToPoint.Install(m_nodes);
151
153 stack.Install(m_nodes);
154
156 address.SetBase("10.1.1.0", "255.255.255.0");
157
159
161
163 serverApps.Start(Seconds(1.0));
164 serverApps.Stop(Seconds(10.0));
165
166 UdpEchoClientHelper echoClient(interfaces.GetAddress(1), 9);
167 echoClient.SetAttribute("MaxPackets", UintegerValue(100));
168 echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
169 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
170
172 clientApps.Start(Seconds(2.0));
173 clientApps.Stop(Seconds(10.0));
174}
175
176void
178{
179 NS_TEST_ASSERT_MSG_EQ(m_anim->GetTracePktCount(), 16, "Expected 16 packets traced");
180}
181
189{
190 public:
195
196 private:
197 void PrepareNetwork() override;
198
199 void CheckLogic() override;
200
203 const double m_initialEnergy;
204};
205
207 : AbstractAnimationInterfaceTestCase("Verify Remaining energy tracing"),
208 m_initialEnergy(100)
209{
210}
211
212void
214{
215 m_energySource = CreateObject<BasicEnergySource>();
216 m_energyModel = CreateObject<SimpleDeviceEnergyModel>();
217
222
223 m_nodes.Create(1);
224 AnimationInterface::SetConstantPosition(m_nodes.Get(0), 0, 10);
225
226 // aggregate energy source to node
228 // once node's energy will be depleted according to the model
229 Simulator::Stop(Seconds(1));
230}
231
232void
234{
235 const double remainingEnergy = m_energySource->GetRemainingEnergy();
236
237 NS_TEST_ASSERT_MSG_EQ((remainingEnergy < m_initialEnergy), true, "Energy hasn't depleted!");
239 remainingEnergy / m_initialEnergy,
240 1.0e-13,
241 "Wrong remaining energy value was traced");
242}
243
251{
252 public:
254 : TestSuite("animation-interface", UNIT)
255 {
256 AddTestCase(new AnimationInterfaceTestCase(), TestCase::QUICK);
257 AddTestCase(new AnimationRemainingEnergyTestCase(), TestCase::QUICK);
258 }
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()
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 EnergySouce installed on node.
Hold variables of type string.
Definition: string.h:42
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:305
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
AttributeValue implementation for Time.
Definition: nstime.h:1425
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
#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
AnimationInterfaceTestSuite g_animationInterfaceTestSuite
the test suite
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
echoClient
Definition: first.py:53
address
Definition: first.py:40
serverApps
Definition: first.py:48
pointToPoint
Definition: first.py:31
echoServer
Definition: first.py:46
clientApps
Definition: first.py:58
devices
Definition: first.py:35
stack
Definition: first.py:37
interfaces
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.