A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 #include "ns3/test.h"
4 #include "ns3/simple-net-device.h"
5 #include "ns3/simple-channel.h"
6 #include "ns3/address.h"
7 #include "ns3/mac48-address.h"
8 #include "ns3/packet.h"
9 #include "ns3/callback.h"
10 #include "ns3/node.h"
11 #include "ns3/simulator.h"
12 #include "ns3/error-model.h"
13 #include "ns3/pointer.h"
14 #include "ns3/double.h"
15 #include "ns3/string.h"
16 #include "ns3/rng-seed-manager.h"
17 
18 using namespace ns3;
19 
20 static void SendPacket (int num, Ptr<NetDevice> device, Address& addr)
21 {
22  for (int i = 0; i < num; i++)
23  {
24  Ptr<Packet> pkt = Create<Packet> (1000); // 1000 dummy bytes of data
25  device->Send (pkt, addr, 0);
26  }
27 }
28 
29 // Two nodes, two devices, one channel
31 {
32  a->AddDevice (input);
33  b->AddDevice (output);
35  input->SetChannel (channel);
36  input->SetNode (a);
37  output->SetChannel (channel);
38  output->SetNode (b);
40 }
41 
42 class ErrorModelSimple : public TestCase
43 {
44 public:
46  virtual ~ErrorModelSimple ();
47 
48 private:
49  virtual void DoRun (void);
50  bool Receive (Ptr<NetDevice> nd, Ptr<const Packet> p, uint16_t protocol, const Address& addr);
51  void DropEvent (Ptr<const Packet> p);
52  uint32_t m_count;
53  uint32_t m_drops;
54 };
55 
56 // Add some help text to this case to describe what it is intended to test
58  : TestCase ("ErrorModel and PhyRxDrop trace for SimpleNetDevice"), m_count (0), m_drops (0)
59 {
60 }
61 
63 {
64 }
65 
66 bool
67 ErrorModelSimple::Receive (Ptr<NetDevice> nd, Ptr<const Packet> p, uint16_t protocol, const Address& addr)
68 {
69  m_count++;
70  return true;
71 }
72 
73 void
75 {
76  m_drops++;
77 }
78 
79 void
81 {
82  // Set some arbitrary deterministic values
83  RngSeedManager::SetSeed (7);
84  RngSeedManager::SetRun (2);
85 
86  Ptr<Node> a = CreateObject<Node> ();
87  Ptr<Node> b = CreateObject<Node> ();
88 
89  Ptr<SimpleNetDevice> input = CreateObject<SimpleNetDevice> ();
90  Ptr<SimpleNetDevice> output = CreateObject<SimpleNetDevice> ();
91  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
92  BuildSimpleTopology (a, b, input, output, channel);
93 
94  output->SetReceiveCallback (MakeCallback (&ErrorModelSimple::Receive, this));
95  Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable> ();
96  // Set this variable to a specific stream
97  uv->SetStream (50);
98 
99  Ptr<RateErrorModel> em = CreateObject<RateErrorModel> ();
100  em->SetRandomVariable (uv);
101  em->SetAttribute ("ErrorRate", DoubleValue (0.001));
102  em->SetAttribute ("ErrorUnit", StringValue ("ERROR_UNIT_PACKET"));
103 
104  // The below hooks will cause drops and receptions to be counted
105  output->SetAttribute ("ReceiveErrorModel", PointerValue (em));
106  output->TraceConnectWithoutContext ("PhyRxDrop", MakeCallback (&ErrorModelSimple::DropEvent, this));
107 
108  // Send 10000 packets
109  Simulator::Schedule (Seconds (0), &SendPacket, 10000, input, output->GetAddress ());
110 
111  Simulator::Run ();
112  Simulator::Destroy ();
113 
114  // For this combination of values, we expect about 1 packet in 1000 to be
115  // dropped. For this specific RNG stream, we see 9991 receptions and 9 drops
116  NS_TEST_ASSERT_MSG_EQ (m_count, 9991, "Wrong number of receptions.");
117  NS_TEST_ASSERT_MSG_EQ (m_drops, 9, "Wrong number of drops.");
118 }
119 
120 // This is the start of an error model test suite. For starters, this is
121 // just testing that the SimpleNetDevice is working but this can be
122 // extended to many more test cases in the future
124 {
125 public:
127 };
128 
130  : TestSuite ("error-model", UNIT)
131 {
133 }
134 
135 // Do not forget to allocate an instance of this TestSuite