A Discrete-Event Network Simulator
API
error-model-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 University of Washington
4  * Copyright (c) 2013 ResiliNets, ITTC, University of Kansas
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 
21 /* BurstErrorModel additions
22  *
23  * Author: Truc Anh N. Nguyen <annguyen@ittc.ku.edu>
24  * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
25  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
26  */
27 
28 #include "ns3/test.h"
29 #include "ns3/simple-net-device.h"
30 #include "ns3/simple-channel.h"
31 #include "ns3/address.h"
32 #include "ns3/mac48-address.h"
33 #include "ns3/packet.h"
34 #include "ns3/callback.h"
35 #include "ns3/node.h"
36 #include "ns3/simulator.h"
37 #include "ns3/error-model.h"
38 #include "ns3/pointer.h"
39 #include "ns3/double.h"
40 #include "ns3/string.h"
41 #include "ns3/rng-seed-manager.h"
42 
43 using namespace ns3;
44 
45 static void SendPacket (int num, Ptr<NetDevice> device, Address& addr)
46 {
47  for (int i = 0; i < num; i++)
48  {
49  Ptr<Packet> pkt = Create<Packet> (1000); // 1000 dummy bytes of data
50  device->Send (pkt, addr, 0);
51  }
52 }
53 
54 // Two nodes, two devices, one channel
56 {
57  a->AddDevice (input);
58  b->AddDevice (output);
60  input->SetChannel (channel);
61  input->SetNode (a);
62  output->SetChannel (channel);
63  output->SetNode (b);
65 }
66 
73 class ErrorModelSimple : public TestCase
74 {
75 public:
77  virtual ~ErrorModelSimple ();
78 
79 private:
80  virtual void DoRun (void);
89  bool Receive (Ptr<NetDevice> nd, Ptr<const Packet> p, uint16_t protocol, const Address& addr);
94  void DropEvent (Ptr<const Packet> p);
95 
96  uint32_t m_count;
97  uint32_t m_drops;
98 };
99 
100 // Add some help text to this case to describe what it is intended to test
102  : TestCase ("ErrorModel and PhyRxDrop trace for SimpleNetDevice"), m_count (0), m_drops (0)
103 {
104 }
105 
107 {
108 }
109 
110 bool
111 ErrorModelSimple::Receive (Ptr<NetDevice> nd, Ptr<const Packet> p, uint16_t protocol, const Address& addr)
112 {
113  m_count++;
114  return true;
115 }
116 
117 void
119 {
120  m_drops++;
121 }
122 
123 void
125 {
126  // Set some arbitrary deterministic values
127  RngSeedManager::SetSeed (7);
128  RngSeedManager::SetRun (2);
129 
130  Ptr<Node> a = CreateObject<Node> ();
131  Ptr<Node> b = CreateObject<Node> ();
132 
133  Ptr<SimpleNetDevice> input = CreateObject<SimpleNetDevice> ();
134  Ptr<SimpleNetDevice> output = CreateObject<SimpleNetDevice> ();
135  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
136  BuildSimpleTopology (a, b, input, output, channel);
137 
138  output->SetReceiveCallback (MakeCallback (&ErrorModelSimple::Receive, this));
139  Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable> ();
140  // Set this variable to a specific stream
141  uv->SetStream (50);
142 
143  Ptr<RateErrorModel> em = CreateObject<RateErrorModel> ();
144  em->SetRandomVariable (uv);
145  em->SetAttribute ("ErrorRate", DoubleValue (0.001));
146  em->SetAttribute ("ErrorUnit", StringValue ("ERROR_UNIT_PACKET"));
147 
148  // The below hooks will cause drops and receptions to be counted
149  output->SetAttribute ("ReceiveErrorModel", PointerValue (em));
150  output->TraceConnectWithoutContext ("PhyRxDrop", MakeCallback (&ErrorModelSimple::DropEvent, this));
151 
152  // Send 10000 packets
153  Simulator::Schedule (Seconds (0), &SendPacket, 10000, input, output->GetAddress ());
154 
155  Simulator::Run ();
156  Simulator::Destroy ();
157 
158  // For this combination of values, we expect about 1 packet in 1000 to be
159  // dropped. For this specific RNG stream, we see 9991 receptions and 9 drops
160  NS_TEST_ASSERT_MSG_EQ (m_count, 9991, "Wrong number of receptions.");
161  NS_TEST_ASSERT_MSG_EQ (m_drops, 9, "Wrong number of drops.");
162 }
163 
171 {
172 public:
174  virtual ~BurstErrorModelSimple ();
175 
176 private:
177  virtual void DoRun (void);
186  bool Receive (Ptr<NetDevice> nd, Ptr<const Packet> p, uint16_t protocol, const Address& addr);
191  void DropEvent (Ptr<const Packet> p);
192 
193  uint32_t m_count;
194  uint32_t m_drops;
195 };
196 
197 // Add some help text to this case to describe what it is intended to test
199  : TestCase ("ErrorModel and PhyRxDrop trace for SimpleNetDevice"), m_count (0), m_drops (0)
200 {
201 }
202 
204 {
205 }
206 
207 bool
209 {
210  m_count++;
211  return true;
212 }
213 
214 void
216 {
217  m_drops++;
218 }
219 
220 void
222 {
223  // Set some arbitrary deterministic values
224  RngSeedManager::SetSeed (5);
225  RngSeedManager::SetRun (8);
226 
227  Ptr<Node> a = CreateObject<Node> ();
228  Ptr<Node> b = CreateObject<Node> ();
229 
230  Ptr<SimpleNetDevice> input = CreateObject<SimpleNetDevice> ();
231  Ptr<SimpleNetDevice> output = CreateObject<SimpleNetDevice> ();
232  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
233  BuildSimpleTopology (a, b, input, output, channel);
234 
235  output->SetReceiveCallback (MakeCallback (&BurstErrorModelSimple::Receive, this));
236  Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable> ();
237  // Set this variable to a specific stream
238  uv->SetStream (50);
239 
240  Ptr<BurstErrorModel> em = CreateObject<BurstErrorModel> ();
241  em->SetRandomVariable (uv);
242  em->SetAttribute ("ErrorRate", DoubleValue (0.01));
243 
244  // Assign the underlying error model random variables to specific streams
245  em->AssignStreams (51);
246 
247  // The below hooks will cause drops and receptions to be counted
248  output->SetAttribute ("ReceiveErrorModel", PointerValue (em));
249  output->TraceConnectWithoutContext ("PhyRxDrop", MakeCallback (&BurstErrorModelSimple::DropEvent, this));
250 
251  // Send 10000 packets
252  Simulator::Schedule (Seconds (0), &SendPacket, 10000, input, output->GetAddress ());
253 
254  Simulator::Run ();
255  Simulator::Destroy ();
256 
257  // With the burst error rate to be 0.01 and the burst size to be from 1 to 4,
258  // we expect about 2.5 packets being dropped every 1000 packets.
259  // That means for 10000 packets, we expect a total of about 250 packet drops.
260  // For this specific RNG seed, we see 9740 receptions and 260 drops.
261  NS_TEST_ASSERT_MSG_EQ (m_count, 9740, "Wrong number of receptions.");
262  NS_TEST_ASSERT_MSG_EQ (m_drops, 260 , "Wrong number of drops.");
263 }
264 
276 {
277 public:
279 };
280 
282  : TestSuite ("error-model", UNIT)
283 {
284  AddTestCase (new ErrorModelSimple, TestCase::QUICK);
285  AddTestCase (new BurstErrorModelSimple, TestCase::QUICK);
286 }
287 
288 // Do not forget to allocate an instance of this TestSuite
uint32_t m_count
The received packets counter.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
static void BuildSimpleTopology(Ptr< Node > a, Ptr< Node > b, Ptr< SimpleNetDevice > input, Ptr< SimpleNetDevice > output, Ptr< SimpleChannel > channel)
Hold variables of type string.
Definition: string.h:41
void DropEvent(Ptr< const Packet > p)
Register a Drop.
A suite of tests to run.
Definition: test.h:1341
BurstErrorModel unit tests.
virtual void DoRun(void)
Implementation to actually run this TestCase.
encapsulates test code
Definition: test.h:1153
a polymophic address class
Definition: address.h:90
channel
Definition: third.py:92
virtual void SetNode(Ptr< Node > node)
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
ErrorModel TestSuite.
#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
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Definition: error-model.cc:353
uint32_t m_drops
The dropped packets counter.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1610
bool Receive(Ptr< NetDevice > nd, Ptr< const Packet > p, uint16_t protocol, const Address &addr)
Receive form a NetDevice.
ErrorModel unit tests.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
void SetRandomVariable(Ptr< RandomVariableStream > ranVar)
Definition: error-model.cc:339
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)=0
uint32_t m_count
The received packets counter.
void DropEvent(Ptr< const Packet > p)
Register a Drop.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
virtual void SetAddress(Address address)
Set the address of this interface.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1064
uint32_t m_drops
The dropped packets counter.
void SetRandomVariable(Ptr< RandomVariableStream >)
Definition: error-model.cc:215
bool Receive(Ptr< NetDevice > nd, Ptr< const Packet > p, uint16_t protocol, const Address &addr)
Receive form a NetDevice.
static ErrorModelTestSuite errorModelTestSuite
Static variable for test initialization.
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:187
static void SendPacket(int num, Ptr< NetDevice > device, Address &addr)
virtual void DoRun(void)
Implementation to actually run this TestCase.