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;
35using namespace ns3::energy;
36
37/**
38 * \ingroup netanim
39 * \ingroup tests
40 * \defgroup netanim-test animation module tests
41 */
42
43/**
44 * \ingroup netanim-test
45 *
46 * \brief Abstract Animation Interface Test Case
47 */
49{
50 public:
51 /**
52 * \brief Constructor.
53 * \param name testcase name
54 */
55 AbstractAnimationInterfaceTestCase(std::string name);
56 /**
57 * \brief Destructor.
58 */
59
61 void DoRun() override;
62
63 protected:
64 NodeContainer m_nodes; ///< the nodes
65 AnimationInterface* m_anim; ///< animation
66
67 private:
68 /// Prepare network function
69 virtual void PrepareNetwork() = 0;
70
71 /// Check logic function
72 virtual void CheckLogic() = 0;
73
74 /// Check file existence
75 virtual void CheckFileExistence();
76
77 const char* m_traceFileName; ///< trace file name
78};
79
81 : TestCase(name),
82 m_anim(nullptr),
83 m_traceFileName("netanim-test.xml")
84{
85}
86
88{
89 delete m_anim;
90}
91
92void
94{
96
98
100 CheckLogic();
103}
104
105void
107{
108 FILE* fp = fopen(m_traceFileName, "r");
109 NS_TEST_ASSERT_MSG_NE(fp, nullptr, "Trace file was not created");
110 fclose(fp);
111 unlink(m_traceFileName);
112}
113
114/**
115 * \ingroup netanim-test
116 *
117 * \brief Animation Interface Test Case
118 */
120{
121 public:
122 /**
123 * \brief Constructor.
124 */
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);
144
145 PointToPointHelper pointToPoint;
146 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
147 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
148
149 NetDeviceContainer devices;
150 devices = pointToPoint.Install(m_nodes);
151
153 stack.Install(m_nodes);
154
155 Ipv4AddressHelper address;
156 address.SetBase("10.1.1.0", "255.255.255.0");
157
158 Ipv4InterfaceContainer interfaces = address.Assign(devices);
159
160 UdpEchoServerHelper echoServer(9);
161
162 ApplicationContainer serverApps = echoServer.Install(m_nodes.Get(1));
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
171 ApplicationContainer clientApps = echoClient.Install(m_nodes.Get(0));
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
182/**
183 * \ingroup netanim-test
184 *
185 * \brief Animation Remaining Energy Test Case
186 */
188{
189 public:
190 /**
191 * \brief Constructor.
192 */
194
195 private:
196 void PrepareNetwork() override;
197
198 void CheckLogic() override;
199
202 const double m_initialEnergy; ///< initial energy
203};
204
206 : AbstractAnimationInterfaceTestCase("Verify Remaining energy tracing"),
207 m_initialEnergy(100)
208{
209}
210
211void
213{
214 m_energySource = CreateObject<BasicEnergySource>();
215 m_energyModel = CreateObject<SimpleDeviceEnergyModel>();
216
221
222 m_nodes.Create(1);
224
225 // aggregate energy source to node
227 // once node's energy will be depleted according to the model
229}
230
231void
233{
234 const double remainingEnergy = m_energySource->GetRemainingEnergy();
235
236 NS_TEST_ASSERT_MSG_EQ((remainingEnergy < m_initialEnergy), true, "Energy hasn't depleted!");
238 remainingEnergy / m_initialEnergy,
239 1.0e-13,
240 "Wrong remaining energy value was traced");
241}
242
243/**
244 * \ingroup netanim-test
245 *
246 * \brief Animation Interface Test Suite
247 */
249{
250 public:
252 : TestSuite("animation-interface", Type::UNIT)
253 {
254 AddTestCase(new AnimationInterfaceTestCase(), TestCase::Duration::QUICK);
255 AddTestCase(new AnimationRemainingEnergyTestCase(), TestCase::Duration::QUICK);
256 }
257} g_animationInterfaceTestSuite; ///< the test suite
Abstract Animation Interface Test Case.
Definition: netanim-test.cc:49
virtual void CheckFileExistence()
Check file existence.
AbstractAnimationInterfaceTestCase(std::string name)
Constructor.
Definition: netanim-test.cc:80
AnimationInterface * m_anim
animation
Definition: netanim-test.cc:65
const char * m_traceFileName
trace file name
Definition: netanim-test.cc:77
NodeContainer m_nodes
the nodes
Definition: netanim-test.cc:64
virtual void CheckLogic()=0
Check logic function.
~AbstractAnimationInterfaceTestCase() override
Destructor.
Definition: netanim-test.cc:87
virtual void PrepareNetwork()=0
Prepare network function.
void DoRun() override
Implementation to actually run this TestCase.
Definition: netanim-test.cc:93
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:309
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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:302
A suite of tests to run.
Definition: test.h:1273
Type
Type of test.
Definition: test.h:1280
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:45
void SetInitialEnergy(double initialEnergyJ)
void AppendDeviceEnergyModel(Ptr< DeviceEnergyModel > deviceEnergyModelPtr)
void SetEnergySource(Ptr< EnergySource > source) override
Sets pointer to EnergySource installed on node.
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:1319
Every class exported by the ns3 library is enclosed in the ns3 namespace.