A Discrete-Event Network Simulator
API
netanim-test.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation;
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 * Author: John Abraham <john.abraham@gatech.edu>
17 * Contributions: Eugene Kalishenko <ydginster@gmail.com> (Open Source and Linux Laboratory http://dev.osll.ru/)
18 */
19
20#include <iostream>
21#include "unistd.h"
22
23#include "ns3/core-module.h"
24#include "ns3/network-module.h"
25#include "ns3/internet-module.h"
26#include "ns3/point-to-point-module.h"
27#include "ns3/netanim-module.h"
28#include "ns3/applications-module.h"
29#include "ns3/point-to-point-layout-module.h"
30#include "ns3/basic-energy-source.h"
31#include "ns3/simple-device-energy-model.h"
32
33using namespace ns3;
34
48{
49public:
54 AbstractAnimationInterfaceTestCase (std::string name);
58 virtual
60 virtual void
61 DoRun (void);
62
63protected:
64
67
68private:
69
71 virtual void PrepareNetwork () = 0;
72
74 virtual void CheckLogic () = 0;
75
77 virtual void CheckFileExistence ();
78
79 const char* m_traceFileName;
80};
81
83 TestCase (name), m_anim (NULL), m_traceFileName ("netanim-test.xml")
84{
85}
86
88{
89 delete m_anim;
90}
91
92void
94{
96
98
99 Simulator::Run ();
100 CheckLogic ();
102 Simulator::Destroy ();
103}
104
105void
107{
108 FILE * fp = fopen (m_traceFileName, "r");
109 NS_TEST_ASSERT_MSG_NE (fp, 0, "Trace file was not created");
110 fclose (fp);
111 unlink (m_traceFileName);
112}
113
121{
122public:
127
128private:
129
130 virtual void
132
133 virtual void
134 CheckLogic ();
135
136};
137
139 AbstractAnimationInterfaceTestCase ("Verify AnimationInterface")
140{
141}
142
143void
145{
146 m_nodes.Create (2);
147 AnimationInterface::SetConstantPosition (m_nodes.Get (0), 0 , 10);
148 AnimationInterface::SetConstantPosition (m_nodes.Get (1), 1 , 10);
149
151 pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
152 pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
153
155 devices = pointToPoint.Install (m_nodes);
156
158 stack.Install (m_nodes);
159
161 address.SetBase ("10.1.1.0", "255.255.255.0");
162
164
166
168 serverApps.Start (Seconds (1.0));
169 serverApps.Stop (Seconds (10.0));
170
171 UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
172 echoClient.SetAttribute ("MaxPackets", UintegerValue (100));
173 echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
174 echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
175
177 clientApps.Start (Seconds (2.0));
178 clientApps.Stop (Seconds (10.0));
179}
180
181void
183{
184 NS_TEST_ASSERT_MSG_EQ (m_anim->GetTracePktCount (), 16, "Expected 16 packets traced");
185}
186
194{
195public:
200
201private:
202
203 virtual void
205
206 virtual void
207 CheckLogic ();
208
211 const double m_initialEnergy;
212};
213
215 AbstractAnimationInterfaceTestCase ("Verify Remaining energy tracing"),
216 m_initialEnergy (100)
217{
218}
219
220void
222{
223 m_energySource = CreateObject<BasicEnergySource>();
224 m_energyModel = CreateObject<SimpleDeviceEnergyModel>();
225
230
231 m_nodes.Create (1);
232 AnimationInterface::SetConstantPosition (m_nodes.Get (0), 0 , 10);
233
234 // aggregate energy source to node
236 // once node's energy will be depleted according to the model
237 Simulator::Stop (Seconds (1));
238}
239
240void
242{
243 const double remainingEnergy = m_energySource->GetRemainingEnergy ();
244
245 NS_TEST_ASSERT_MSG_EQ ((remainingEnergy < m_initialEnergy), true, "Energy hasn't depleted!");
247 remainingEnergy / m_initialEnergy,
248 1.0e-13,
249 "Wrong remaining energy value was traced");
250}
251
259{
260public:
262 TestSuite ("animation-interface", UNIT)
263 {
264 AddTestCase (new AnimationInterfaceTestCase (), TestCase::QUICK);
265 AddTestCase (new AnimationRemainingEnergyTestCase (), TestCase::QUICK);
266 }
Abstract Animation Interface Test Case.
Definition: netanim-test.cc:48
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: netanim-test.cc:93
virtual void CheckFileExistence()
Check file existence.
AbstractAnimationInterfaceTestCase(std::string name)
Constructor.
Definition: netanim-test.cc:82
AnimationInterface * m_anim
animation
Definition: netanim-test.cc:66
const char * m_traceFileName
trace file name
Definition: netanim-test.cc:79
NodeContainer m_nodes
the nodes
Definition: netanim-test.cc:65
virtual ~AbstractAnimationInterfaceTestCase()
Destructor.
Definition: netanim-test.cc:87
virtual void CheckLogic()=0
Check logic function.
virtual void PrepareNetwork()=0
Prepare network function.
Animation Interface Test Case.
AnimationInterfaceTestCase()
Constructor.
virtual void CheckLogic()
Check logic function.
virtual void PrepareNetwork()
Prepare network function.
Animation Interface Test Suite.
Animation Remaining Energy Test Case.
virtual void PrepareNetwork()
Prepare network function.
const double m_initialEnergy
initial energy
virtual void CheckLogic()
Check logic function.
AnimationRemainingEnergyTestCase()
Constructor.
Ptr< SimpleDeviceEnergyModel > m_energyModel
energy model
Ptr< BasicEnergySource > m_energySource
energy source
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.
virtual double GetRemainingEnergy(void)
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:252
Build a set of PointToPointNetDevice objects.
virtual void SetEnergySource(Ptr< EnergySource > source)
Sets pointer to EnergySouce installed on node.
Hold variables of type string.
Definition: string.h:41
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1197
AttributeValue implementation for Time.
Definition: nstime.h:1308
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:44
#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:141
#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:542
#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:323
AnimationInterfaceTestSuite g_animationInterfaceTestSuite
the test suite
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
echoClient
Definition: first.py:56
address
Definition: first.py:44
serverApps
Definition: first.py:52
pointToPoint
Definition: first.py:35
echoServer
Definition: first.py:50
clientApps
Definition: first.py:61
devices
Definition: first.py:39
stack
Definition: first.py:41
interfaces
Definition: first.py:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.