A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
34using namespace ns3;
35
36/**
37 * \ingroup netanim
38 * \ingroup tests
39 * \defgroup netanim-test animation module tests
40 */
41
42/**
43 * \ingroup netanim-test
44 *
45 * \brief Abstract Animation Interface Test Case
46 */
48{
49 public:
50 /**
51 * \brief Constructor.
52 * \param name testcase name
53 */
54 AbstractAnimationInterfaceTestCase(std::string name);
55 /**
56 * \brief Destructor.
57 */
58
60 void DoRun() override;
61
62 protected:
63 NodeContainer m_nodes; ///< the nodes
64 AnimationInterface* m_anim; ///< animation
65
66 private:
67 /// Prepare network function
68 virtual void PrepareNetwork() = 0;
69
70 /// Check logic function
71 virtual void CheckLogic() = 0;
72
73 /// Check file existence
74 virtual void CheckFileExistence();
75
76 const char* m_traceFileName; ///< trace file name
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
99 CheckLogic();
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
113/**
114 * \ingroup netanim-test
115 *
116 * \brief Animation Interface Test Case
117 */
119{
120 public:
121 /**
122 * \brief Constructor.
123 */
125
126 private:
127 void PrepareNetwork() override;
128
129 void CheckLogic() override;
130};
131
133 : AbstractAnimationInterfaceTestCase("Verify AnimationInterface")
134{
135}
136
137void
139{
140 m_nodes.Create(2);
143
144 PointToPointHelper pointToPoint;
145 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
146 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
147
148 NetDeviceContainer devices;
149 devices = pointToPoint.Install(m_nodes);
150
152 stack.Install(m_nodes);
153
154 Ipv4AddressHelper address;
155 address.SetBase("10.1.1.0", "255.255.255.0");
156
157 Ipv4InterfaceContainer interfaces = address.Assign(devices);
158
159 UdpEchoServerHelper echoServer(9);
160
161 ApplicationContainer serverApps = echoServer.Install(m_nodes.Get(1));
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
170 ApplicationContainer clientApps = echoClient.Install(m_nodes.Get(0));
171 clientApps.Start(Seconds(2.0));
172 clientApps.Stop(Seconds(10.0));
173}
174
175void
177{
178 NS_TEST_ASSERT_MSG_EQ(m_anim->GetTracePktCount(), 16, "Expected 16 packets traced");
179}
180
181/**
182 * \ingroup netanim-test
183 *
184 * \brief Animation Remaining Energy Test Case
185 */
187{
188 public:
189 /**
190 * \brief Constructor.
191 */
193
194 private:
195 void PrepareNetwork() override;
196
197 void CheckLogic() override;
198
201 const double m_initialEnergy; ///< initial energy
202};
203
205 : AbstractAnimationInterfaceTestCase("Verify Remaining energy tracing"),
206 m_initialEnergy(100)
207{
208}
209
210void
212{
213 m_energySource = CreateObject<BasicEnergySource>();
214 m_energyModel = CreateObject<SimpleDeviceEnergyModel>();
215
220
221 m_nodes.Create(1);
223
224 // aggregate energy source to node
226 // once node's energy will be depleted according to the model
228}
229
230void
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
242/**
243 * \ingroup netanim-test
244 *
245 * \brief Animation Interface Test Suite
246 */
248{
249 public:
251 : TestSuite("animation-interface", Type::UNIT)
252 {
253 AddTestCase(new AnimationInterfaceTestCase(), TestCase::Duration::QUICK);
254 AddTestCase(new AnimationRemainingEnergyTestCase(), TestCase::Duration::QUICK);
255 }
256} g_animationInterfaceTestSuite; ///< the test suite
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)
static void SetConstantPosition(Ptr< Node > n, double x, double y, double z=0)
Helper function to set Constant Position for a given node.
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:309
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
void SetEnergySource(Ptr< EnergySource > source) override
Sets pointer to EnergySource installed on node.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
static constexpr auto UNIT
Definition: test.h:1286
AttributeValue implementation for Time.
Definition: nstime.h:1413
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:145
#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:565
#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:338
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.