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 
33 using namespace ns3;
34 
48 {
49 public:
54  AbstractAnimationInterfaceTestCase (std::string name);
58  virtual
64  virtual void
65  DoRun (void);
66 
67 protected:
68 
71 
72 private:
73 
75  virtual void PrepareNetwork () = 0;
76 
78  virtual void CheckLogic () = 0;
79 
81  virtual void CheckFileExistence ();
82 
83  const char* m_traceFileName;
84 };
85 
87  TestCase (name), m_anim (NULL), m_traceFileName ("netanim-test.xml")
88 {
89 }
90 
92 {
93  delete m_anim;
94 }
95 
96 void
98 {
99  PrepareNetwork ();
100 
102 
103  Simulator::Run ();
104  CheckLogic ();
106  Simulator::Destroy ();
107 }
108 
109 void
111 {
112  FILE * fp = fopen (m_traceFileName, "r");
113  NS_TEST_ASSERT_MSG_NE (fp, 0, "Trace file was not created");
114  fclose (fp);
115  unlink (m_traceFileName);
116 }
117 
125 {
126 public:
131 
132 private:
133 
134  virtual void
135  PrepareNetwork ();
136 
137  virtual void
138  CheckLogic ();
139 
140 };
141 
143  AbstractAnimationInterfaceTestCase ("Verify AnimationInterface")
144 {
145 }
146 
147 void
149 {
150  m_nodes.Create (2);
151  AnimationInterface::SetConstantPosition (m_nodes.Get (0), 0 , 10);
152  AnimationInterface::SetConstantPosition (m_nodes.Get (1), 1 , 10);
153 
155  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
156  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
157 
159  devices = pointToPoint.Install (m_nodes);
160 
162  stack.Install (m_nodes);
163 
165  address.SetBase ("10.1.1.0", "255.255.255.0");
166 
168 
170 
172  serverApps.Start (Seconds (1.0));
173  serverApps.Stop (Seconds (10.0));
174 
175  UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
176  echoClient.SetAttribute ("MaxPackets", UintegerValue (100));
177  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
178  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
179 
181  clientApps.Start (Seconds (2.0));
182  clientApps.Stop (Seconds (10.0));
183 }
184 
185 void
187 {
188  NS_TEST_ASSERT_MSG_EQ (m_anim->GetTracePktCount (), 16, "Expected 16 packets traced");
189 }
190 
198 {
199 public:
204 
205 private:
206 
207  virtual void
208  PrepareNetwork ();
209 
210  virtual void
211  CheckLogic ();
212 
215  const double m_initialEnergy;
216 };
217 
219  AbstractAnimationInterfaceTestCase ("Verify Remaining energy tracing"),
220  m_initialEnergy (100)
221 {
222 }
223 
224 void
226 {
227  m_energySource = CreateObject<BasicEnergySource>();
228  m_energyModel = CreateObject<SimpleDeviceEnergyModel>();
229 
234 
235  m_nodes.Create (1);
236  AnimationInterface::SetConstantPosition (m_nodes.Get (0), 0 , 10);
237 
238  // aggregate energy source to node
240  // once node's energy will be depleted according to the model
241  Simulator::Stop (Seconds (1));
242 }
243 
244 void
246 {
247  const double remainingEnergy = m_energySource->GetRemainingEnergy ();
248 
249  NS_TEST_ASSERT_MSG_EQ ((remainingEnergy < m_initialEnergy), true, "Energy hasn't depleted!");
251  remainingEnergy / m_initialEnergy,
252  1.0e-13,
253  "Wrong remaining energy value was traced");
254 }
255 
263 {
264 public:
266  TestSuite ("animation-interface", UNIT)
267  {
268  AddTestCase (new AnimationInterfaceTestCase (), TestCase::QUICK);
269  AddTestCase (new AnimationRemainingEnergyTestCase (), TestCase::QUICK);
270  }
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
AbstractAnimationInterfaceTestCase::m_nodes
NodeContainer m_nodes
the nodes
Definition: netanim-test.cc:69
AnimationRemainingEnergyTestCase::CheckLogic
virtual void CheckLogic()
Check logic function.
Definition: netanim-test.cc:245
AbstractAnimationInterfaceTestCase::DoRun
virtual void DoRun(void)
Run unit tests for this class.
Definition: netanim-test.cc:97
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
ns3::UdpEchoClientHelper
Create an application which sends a UDP packet and waits for an echo of this packet.
Definition: udp-echo-helper.h:107
AbstractAnimationInterfaceTestCase::CheckLogic
virtual void CheckLogic()=0
Check logic function.
AnimationRemainingEnergyTestCase::m_initialEnergy
const double m_initialEnergy
initial energy
Definition: netanim-test.cc:215
ns3::AnimationInterface
Interface to network animator.
Definition: animation-interface.h:76
AnimationRemainingEnergyTestCase::AnimationRemainingEnergyTestCase
AnimationRemainingEnergyTestCase()
Constructor.
Definition: netanim-test.cc:218
AnimationInterfaceTestSuite::AnimationInterfaceTestSuite
AnimationInterfaceTestSuite()
Definition: netanim-test.cc:265
ns3::BasicEnergySource::SetInitialEnergy
void SetInitialEnergy(double initialEnergyJ)
Definition: basic-energy-source.cc:90
NS_TEST_ASSERT_MSG_NE
#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:622
first.devices
devices
Definition: first.py:39
ns3::NodeContainer::Create
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Definition: node-container.cc:98
AnimationRemainingEnergyTestCase::m_energyModel
Ptr< SimpleDeviceEnergyModel > m_energyModel
energy model
Definition: netanim-test.cc:214
AbstractAnimationInterfaceTestCase::AbstractAnimationInterfaceTestCase
AbstractAnimationInterfaceTestCase(std::string name)
Constructor.
Definition: netanim-test.cc:86
ns3::SimpleDeviceEnergyModel::SetCurrentA
void SetCurrentA(double current)
Definition: simple-device-energy-model.cc:100
g_animationInterfaceTestSuite
AnimationInterfaceTestSuite g_animationInterfaceTestSuite
the test suite
ns3::TestCase
encapsulates test code
Definition: test.h:1154
ns3::Ptr< BasicEnergySource >
AnimationRemainingEnergyTestCase::m_energySource
Ptr< BasicEnergySource > m_energySource
energy source
Definition: netanim-test.cc:213
first.stack
stack
Definition: first.py:41
AbstractAnimationInterfaceTestCase::CheckFileExistence
virtual void CheckFileExistence()
Check file existence.
Definition: netanim-test.cc:110
AnimationInterfaceTestCase::AnimationInterfaceTestCase
AnimationInterfaceTestCase()
Constructor.
Definition: netanim-test.cc:142
AnimationRemainingEnergyTestCase::PrepareNetwork
virtual void PrepareNetwork()
Prepare network function.
Definition: netanim-test.cc:225
AnimationInterfaceTestSuite
Animation Interface Test Suite.
Definition: netanim-test.cc:263
NS_TEST_ASSERT_MSG_EQ_TOL
#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:378
AnimationInterfaceTestCase::CheckLogic
virtual void CheckLogic()
Check logic function.
Definition: netanim-test.cc:186
AbstractAnimationInterfaceTestCase::m_traceFileName
const char * m_traceFileName
trace file name
Definition: netanim-test.cc:83
ns3::Object::AggregateObject
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
AbstractAnimationInterfaceTestCase::PrepareNetwork
virtual void PrepareNetwork()=0
Prepare network function.
ns3::Ipv4InterfaceContainer
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Definition: ipv4-interface-container.h:55
ns3::NodeContainer::Get
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Definition: node-container.cc:93
first.address
address
Definition: first.py:44
AbstractAnimationInterfaceTestCase::~AbstractAnimationInterfaceTestCase
virtual ~AbstractAnimationInterfaceTestCase()
Destructor.
Definition: netanim-test.cc:91
AnimationInterfaceTestCase
Animation Interface Test Case.
Definition: netanim-test.cc:125
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
ns3::AnimationInterface::GetNodeEnergyFraction
double GetNodeEnergyFraction(Ptr< const Node > node) const
Get node's energy fraction (This used only for testing)
Definition: animation-interface.cc:401
first.echoClient
echoClient
Definition: first.py:56
first.interfaces
interfaces
Definition: first.py:48
ns3::TestSuite
A suite of tests to run.
Definition: test.h:1344
first.clientApps
clientApps
Definition: first.py:61
NS_TEST_ASSERT_MSG_EQ
#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:166
AbstractAnimationInterfaceTestCase
Abstract Animation Interface Test Case.
Definition: netanim-test.cc:48
ns3::TestSuite::UNIT
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1353
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
AbstractAnimationInterfaceTestCase::m_anim
AnimationInterface * m_anim
animation
Definition: netanim-test.cc:70
first.serverApps
serverApps
Definition: first.py:52
first.pointToPoint
pointToPoint
Definition: first.py:35
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::PointToPointHelper
Build a set of PointToPointNetDevice objects.
Definition: point-to-point-helper.h:45
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::AnimationInterface::GetTracePktCount
uint64_t GetTracePktCount()
Get trace file packet count (This used only for testing)
Definition: animation-interface.cc:1462
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::BasicEnergySource::GetRemainingEnergy
virtual double GetRemainingEnergy(void)
Definition: basic-energy-source.cc:134
AnimationRemainingEnergyTestCase
Animation Remaining Energy Test Case.
Definition: netanim-test.cc:198
ns3::UdpEchoServerHelper
Create a server application which waits for input UDP packets and sends them back to the original sen...
Definition: udp-echo-helper.h:38
AnimationInterfaceTestCase::PrepareNetwork
virtual void PrepareNetwork()
Prepare network function.
Definition: netanim-test.cc:148
ns3::SimpleDeviceEnergyModel::SetEnergySource
virtual void SetEnergySource(Ptr< EnergySource > source)
Sets pointer to EnergySouce installed on node.
Definition: simple-device-energy-model.cc:62
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::EnergySource::AppendDeviceEnergyModel
void AppendDeviceEnergyModel(Ptr< DeviceEnergyModel > deviceEnergyModelPtr)
Definition: energy-source.cc:70
first.echoServer
echoServer
Definition: first.py:50