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#ifndef __WIN32__
10#include "unistd.h"
11#endif
12
13#include "ns3/basic-energy-source.h"
14#include "ns3/core-module.h"
15#include "ns3/internet-module.h"
16#include "ns3/netanim-module.h"
17#include "ns3/network-module.h"
18#include "ns3/point-to-point-layout-module.h"
19#include "ns3/point-to-point-module.h"
20#include "ns3/simple-device-energy-model.h"
21#include "ns3/udp-echo-helper.h"
22
23#include <iostream>
24
25using namespace ns3;
26using namespace ns3::energy;
27
28/**
29 * @ingroup netanim
30 * @ingroup tests
31 * @defgroup netanim-test animation module tests
32 */
33
34/**
35 * @ingroup netanim-test
36 *
37 * @brief Abstract Animation Interface Test Case
38 */
40{
41 public:
42 /**
43 * @brief Constructor.
44 * @param name testcase name
45 */
46 AbstractAnimationInterfaceTestCase(std::string name);
47 /**
48 * @brief Destructor.
49 */
50
52 void DoRun() override;
53
54 protected:
55 NodeContainer m_nodes; ///< the nodes
56 AnimationInterface* m_anim; ///< animation
57
58 private:
59 /// Prepare network function
60 virtual void PrepareNetwork() = 0;
61
62 /// Check logic function
63 virtual void CheckLogic() = 0;
64
65 /// Check file existence
66 virtual void CheckFileExistence();
67
68 const char* m_traceFileName; ///< trace file name
69};
70
72 : TestCase(name),
73 m_anim(nullptr),
74 m_traceFileName("netanim-test.xml")
75{
76}
77
82
83void
95
96void
98{
99 FILE* fp = fopen(m_traceFileName, "r");
100 NS_TEST_ASSERT_MSG_NE(fp, nullptr, "Trace file was not created");
101 fclose(fp);
102 remove(m_traceFileName);
103}
104
105/**
106 * @ingroup netanim-test
107 *
108 * @brief Animation Interface Test Case
109 */
111{
112 public:
113 /**
114 * @brief Constructor.
115 */
117
118 private:
119 void PrepareNetwork() override;
120
121 void CheckLogic() override;
122};
123
128
129void
131{
132 m_nodes.Create(2);
135
136 PointToPointHelper pointToPoint;
137 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
138 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
139
140 NetDeviceContainer devices;
141 devices = pointToPoint.Install(m_nodes);
142
144 stack.Install(m_nodes);
145
146 Ipv4AddressHelper address;
147 address.SetBase("10.1.1.0", "255.255.255.0");
148
149 Ipv4InterfaceContainer interfaces = address.Assign(devices);
150
151 UdpEchoServerHelper echoServer(9);
152
153 ApplicationContainer serverApps = echoServer.Install(m_nodes.Get(1));
154 serverApps.Start(Seconds(1));
155 serverApps.Stop(Seconds(10));
156
157 UdpEchoClientHelper echoClient(interfaces.GetAddress(1), 9);
158 echoClient.SetAttribute("MaxPackets", UintegerValue(100));
159 echoClient.SetAttribute("Interval", TimeValue(Seconds(1)));
160 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
161
162 ApplicationContainer clientApps = echoClient.Install(m_nodes.Get(0));
163 clientApps.Start(Seconds(2));
164 clientApps.Stop(Seconds(10));
165}
166
167void
169{
170 NS_TEST_ASSERT_MSG_EQ(m_anim->GetTracePktCount(), 16, "Expected 16 packets traced");
171}
172
173/**
174 * @ingroup netanim-test
175 *
176 * @brief Animation Remaining Energy Test Case
177 */
179{
180 public:
181 /**
182 * @brief Constructor.
183 */
185
186 private:
187 void PrepareNetwork() override;
188
189 void CheckLogic() override;
190
193 const double m_initialEnergy; ///< initial energy
194};
195
197 : AbstractAnimationInterfaceTestCase("Verify Remaining energy tracing"),
198 m_initialEnergy(100)
199{
200}
201
202void
204{
207
208 m_energySource->SetInitialEnergy(m_initialEnergy);
209 m_energyModel->SetEnergySource(m_energySource);
210 m_energySource->AppendDeviceEnergyModel(m_energyModel);
211 m_energyModel->SetCurrentA(20);
212
213 m_nodes.Create(1);
215
216 // aggregate energy source to node
218 // once node's energy will be depleted according to the model
220}
221
222void
224{
225 const double remainingEnergy = m_energySource->GetRemainingEnergy();
226
227 NS_TEST_ASSERT_MSG_EQ((remainingEnergy < m_initialEnergy), true, "Energy hasn't depleted!");
229 remainingEnergy / m_initialEnergy,
230 1.0e-13,
231 "Wrong remaining energy value was traced");
232}
233
234/**
235 * @ingroup netanim-test
236 *
237 * @brief Animation Interface Test Suite
238 */
240{
241 public:
243 : TestSuite("animation-interface", Type::UNIT)
244 {
245 AddTestCase(new AnimationInterfaceTestCase(), TestCase::Duration::QUICK);
246 AddTestCase(new AnimationRemainingEnergyTestCase(), TestCase::Duration::QUICK);
247 }
248} 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
AttributeValue implementation for Time.
Definition nstime.h:1432
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:1345
Every class exported by the ns3 library is enclosed in the ns3 namespace.