~ubuntu-branches/ubuntu/quantal/ns3/quantal

« back to all changes in this revision

Viewing changes to ns-3.13/src/test/error-model-test-suite.cc

  • Committer: Package Import Robot
  • Author(s): YunQiang Su, Aron Xu, YunQiang Su, Upstream
  • Date: 2012-01-06 00:35:42 UTC
  • mfrom: (10.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20120106003542-vcn5g03mhapm991h
Tags: 3.13+dfsg-1
[ Aron Xu ]:
        add tag binary and binary-indep, 
  for not build doc when --binary-arch (Closes: #654493).
[ YunQiang Su ]
        add waf 1.5/1.6 source to debian directory, 
  and build waf from there (Closes: #642217).
[ Upstream ]
  Successfully link with --as-needed option (Closes: #642225).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
17
using namespace ns3;
 
18
 
 
19
static void SendPacket (int num, Ptr<NetDevice> device, Address& addr)
 
20
{
 
21
  for (int i = 0; i < num; i++)
 
22
    {
 
23
      Ptr<Packet> pkt = Create<Packet> (1000);  // 1000 dummy bytes of data
 
24
      device->Send (pkt, addr, 0);
 
25
    }
 
26
}
 
27
 
 
28
// Two nodes, two devices, one channel
 
29
static void BuildSimpleTopology (Ptr<Node> a, Ptr<Node> b, Ptr<SimpleNetDevice> input, Ptr<SimpleNetDevice> output, Ptr<SimpleChannel> channel)
 
30
{
 
31
  a->AddDevice (input);
 
32
  b->AddDevice (output);
 
33
  input->SetAddress (Mac48Address::Allocate ());
 
34
  input->SetChannel (channel);
 
35
  input->SetNode (a);
 
36
  output->SetChannel (channel);
 
37
  output->SetNode (b);
 
38
  output->SetAddress (Mac48Address::Allocate ());
 
39
}
 
40
 
 
41
class ErrorModelSimple : public TestCase
 
42
{
 
43
public:
 
44
  ErrorModelSimple ();
 
45
  virtual ~ErrorModelSimple ();
 
46
 
 
47
private:
 
48
  virtual void DoRun (void);
 
49
  bool Receive (Ptr<NetDevice> nd, Ptr<const Packet> p, uint16_t protocol, const Address& addr);
 
50
  void DropEvent (Ptr<const Packet> p);
 
51
  uint32_t m_count;
 
52
  uint32_t m_drops;
 
53
};
 
54
 
 
55
// Add some help text to this case to describe what it is intended to test
 
56
ErrorModelSimple::ErrorModelSimple ()
 
57
  : TestCase ("ErrorModel and PhyRxDrop trace for SimpleNetDevice"), m_count (0), m_drops (0)
 
58
{
 
59
}
 
60
 
 
61
ErrorModelSimple::~ErrorModelSimple ()
 
62
{
 
63
}
 
64
 
 
65
bool 
 
66
ErrorModelSimple::Receive (Ptr<NetDevice> nd, Ptr<const Packet> p, uint16_t protocol, const Address& addr)
 
67
{
 
68
  m_count++;
 
69
  return true;
 
70
}
 
71
 
 
72
void 
 
73
ErrorModelSimple::DropEvent (Ptr<const Packet> p)
 
74
{
 
75
  m_drops++;
 
76
}
 
77
 
 
78
void
 
79
ErrorModelSimple::DoRun (void)
 
80
{
 
81
  // Set some arbitrary deterministic values
 
82
  SeedManager::SetSeed (7);
 
83
  SeedManager::SetRun (5);
 
84
 
 
85
  Ptr<Node> a = CreateObject<Node> ();
 
86
  Ptr<Node> b = CreateObject<Node> ();
 
87
 
 
88
  Ptr<SimpleNetDevice> input = CreateObject<SimpleNetDevice> ();
 
89
  Ptr<SimpleNetDevice> output = CreateObject<SimpleNetDevice> ();
 
90
  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
 
91
  BuildSimpleTopology (a, b, input, output, channel);
 
92
 
 
93
  output->SetReceiveCallback (MakeCallback (&ErrorModelSimple::Receive, this));
 
94
 
 
95
  Ptr<RateErrorModel> em = CreateObjectWithAttributes<RateErrorModel> ("RanVar", RandomVariableValue (UniformVariable (0.0, 1.0)));
 
96
  em->SetAttribute ("ErrorRate", DoubleValue (0.001));
 
97
  em->SetAttribute ("ErrorUnit", StringValue ("EU_PKT"));
 
98
 
 
99
  // The below hooks will cause drops and receptions to be counted
 
100
  output->SetAttribute ("ReceiveErrorModel", PointerValue (em));
 
101
  output->TraceConnectWithoutContext ("PhyRxDrop", MakeCallback (&ErrorModelSimple::DropEvent, this));
 
102
 
 
103
  // Send 10000 packets
 
104
  Simulator::Schedule (Seconds (0), &SendPacket, 10000, input, output->GetAddress ());
 
105
 
 
106
  Simulator::Run ();
 
107
  Simulator::Destroy ();
 
108
 
 
109
  // For this combination of values, we expect about 1 packet in 1000 to be
 
110
  // dropped.  For this specific RNG stream, we see 9992 receptions and 8 drops
 
111
  NS_TEST_ASSERT_MSG_EQ (m_count, 9992, "Wrong number of receptions.");
 
112
  NS_TEST_ASSERT_MSG_EQ (m_drops, 8, "Wrong number of drops.");
 
113
}
 
114
 
 
115
// This is the start of an error model test suite.  For starters, this is
 
116
// just testing that the SimpleNetDevice is working but this can be
 
117
// extended to many more test cases in the future
 
118
class ErrorModelTestSuite : public TestSuite
 
119
{
 
120
public:
 
121
  ErrorModelTestSuite ();
 
122
};
 
123
 
 
124
ErrorModelTestSuite::ErrorModelTestSuite ()
 
125
  : TestSuite ("error-model", UNIT)
 
126
{
 
127
  AddTestCase (new ErrorModelSimple);
 
128
}
 
129
 
 
130
// Do not forget to allocate an instance of this TestSuite
 
131
static ErrorModelTestSuite errorModelTestSuite;