A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
netanim-test.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Author: John Abraham <john.abraham@gatech.edu>
5 * Contributions: Eugene Kalishenko <ydginster@gmail.com> (Open Source and Linux Laboratory
6 * http://wiki.osll.ru/doku.php/start)
7 */
8
9#include "unistd.h"
10
11#include "ns3/basic-energy-source.h"
12#include "ns3/core-module.h"
13#include "ns3/internet-module.h"
14#include "ns3/netanim-module.h"
15#include "ns3/network-module.h"
16#include "ns3/point-to-point-layout-module.h"
17#include "ns3/point-to-point-module.h"
18#include "ns3/simple-device-energy-model.h"
19#include "ns3/udp-echo-helper.h"
20
21#include <iostream>
22
23using namespace ns3;
24using namespace ns3::energy;
25
26/**
27 * \ingroup netanim
28 * \ingroup tests
29 * \defgroup netanim-test animation module tests
30 */
31
32/**
33 * \ingroup netanim-test
34 *
35 * \brief Abstract Animation Interface Test Case
36 */
38{
39 public:
40 /**
41 * \brief Constructor.
42 * \param name testcase name
43 */
44 AbstractAnimationInterfaceTestCase(std::string name);
45 /**
46 * \brief Destructor.
47 */
48
50 void DoRun() override;
51
52 protected:
53 NodeContainer m_nodes; ///< the nodes
54 AnimationInterface* m_anim; ///< animation
55
56 private:
57 /// Prepare network function
58 virtual void PrepareNetwork() = 0;
59
60 /// Check logic function
61 virtual void CheckLogic() = 0;
62
63 /// Check file existence
64 virtual void CheckFileExistence();
65
66 const char* m_traceFileName; ///< trace file name
67};
68
70 : TestCase(name),
71 m_anim(nullptr),
72 m_traceFileName("netanim-test.xml")
73{
74}
75
80
81void
93
94void
96{
97 FILE* fp = fopen(m_traceFileName, "r");
98 NS_TEST_ASSERT_MSG_NE(fp, nullptr, "Trace file was not created");
99 fclose(fp);
100 unlink(m_traceFileName);
101}
102
103/**
104 * \ingroup netanim-test
105 *
106 * \brief Animation Interface Test Case
107 */
109{
110 public:
111 /**
112 * \brief Constructor.
113 */
115
116 private:
117 void PrepareNetwork() override;
118
119 void CheckLogic() override;
120};
121
126
127void
129{
130 m_nodes.Create(2);
133
134 PointToPointHelper pointToPoint;
135 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
136 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
137
138 NetDeviceContainer devices;
139 devices = pointToPoint.Install(m_nodes);
140
142 stack.Install(m_nodes);
143
144 Ipv4AddressHelper address;
145 address.SetBase("10.1.1.0", "255.255.255.0");
146
147 Ipv4InterfaceContainer interfaces = address.Assign(devices);
148
149 UdpEchoServerHelper echoServer(9);
150
151 ApplicationContainer serverApps = echoServer.Install(m_nodes.Get(1));
152 serverApps.Start(Seconds(1.0));
153 serverApps.Stop(Seconds(10.0));
154
155 UdpEchoClientHelper echoClient(interfaces.GetAddress(1), 9);
156 echoClient.SetAttribute("MaxPackets", UintegerValue(100));
157 echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
158 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
159
160 ApplicationContainer clientApps = echoClient.Install(m_nodes.Get(0));
161 clientApps.Start(Seconds(2.0));
162 clientApps.Stop(Seconds(10.0));
163}
164
165void
167{
168 NS_TEST_ASSERT_MSG_EQ(m_anim->GetTracePktCount(), 16, "Expected 16 packets traced");
169}
170
171/**
172 * \ingroup netanim-test
173 *
174 * \brief Animation Remaining Energy Test Case
175 */
177{
178 public:
179 /**
180 * \brief Constructor.
181 */
183
184 private:
185 void PrepareNetwork() override;
186
187 void CheckLogic() override;
188
191 const double m_initialEnergy; ///< initial energy
192};
193
195 : AbstractAnimationInterfaceTestCase("Verify Remaining energy tracing"),
196 m_initialEnergy(100)
197{
198}
199
200void
202{
205
206 m_energySource->SetInitialEnergy(m_initialEnergy);
207 m_energyModel->SetEnergySource(m_energySource);
208 m_energySource->AppendDeviceEnergyModel(m_energyModel);
209 m_energyModel->SetCurrentA(20);
210
211 m_nodes.Create(1);
213
214 // aggregate energy source to node
216 // once node's energy will be depleted according to the model
218}
219
220void
222{
223 const double remainingEnergy = m_energySource->GetRemainingEnergy();
224
225 NS_TEST_ASSERT_MSG_EQ((remainingEnergy < m_initialEnergy), true, "Energy hasn't depleted!");
227 remainingEnergy / m_initialEnergy,
228 1.0e-13,
229 "Wrong remaining energy value was traced");
230}
231
232/**
233 * \ingroup netanim-test
234 *
235 * \brief Animation Interface Test Suite
236 */
238{
239 public:
241 : TestSuite("animation-interface", Type::UNIT)
242 {
243 AddTestCase(new AnimationInterfaceTestCase(), TestCase::Duration::QUICK);
244 AddTestCase(new AnimationRemainingEnergyTestCase(), TestCase::Duration::QUICK);
245 }
246} g_animationInterfaceTestSuite; ///< the test suite
Abstract Animation Interface Test Case.
virtual void CheckFileExistence()
Check file existence.
AbstractAnimationInterfaceTestCase(std::string name)
Constructor.
AnimationInterface * m_anim
animation
const char * m_traceFileName
trace file name
NodeContainer m_nodes
the nodes
virtual void CheckLogic()=0
Check logic function.
~AbstractAnimationInterfaceTestCase() override
Destructor.
virtual void PrepareNetwork()=0
Prepare network function.
void DoRun() override
Implementation to actually run this TestCase.
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.
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:298
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
static constexpr auto UNIT
Definition test.h:1291
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:34
AnimationInterfaceTestSuite g_animationInterfaceTestSuite
the test suite
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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:134
#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:554
#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:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.