A Discrete-Event Network Simulator
API
lr-wpan-error-model-test.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 The Boeing Company
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Tom Henderson <thomas.r.henderson@boeing.com>
19  */
20 #include <ns3/test.h>
21 #include <ns3/log.h>
22 #include <ns3/callback.h>
23 #include <ns3/packet.h>
24 #include <ns3/simulator.h>
25 #include <ns3/lr-wpan-error-model.h>
26 #include <ns3/propagation-loss-model.h>
27 #include <ns3/lr-wpan-net-device.h>
28 #include <ns3/lr-wpan-mac.h>
29 #include <ns3/node.h>
30 #include <ns3/net-device.h>
31 #include <ns3/single-model-spectrum-channel.h>
32 #include <ns3/mac16-address.h>
33 #include <ns3/constant-position-mobility-model.h>
34 #include "ns3/rng-seed-manager.h"
35 
36 using namespace ns3;
37 
38 NS_LOG_COMPONENT_DEFINE ("lr-wpan-error-model-test");
39 
41 {
42 public:
44  virtual ~LrWpanErrorDistanceTestCase ();
45  uint32_t GetReceived (void) const
46  {
47  return m_received;
48  }
49 
50 private:
51  virtual void DoRun (void);
53  uint32_t m_received;
54 };
55 
57 {
58 public:
60  virtual ~LrWpanErrorModelTestCase ();
61 
62 private:
63  virtual void DoRun (void);
64 };
65 
67  : TestCase ("Test the 802.15.4 error model vs distance"),
68  m_received (0)
69 {
70 }
71 
73 {
74 }
75 
76 void
78 {
79  m_received++;
80 }
81 
82 void
84 {
85  // Set the random seed and run number for this test
86  RngSeedManager::SetSeed (1);
87  RngSeedManager::SetRun (6);
88 
89  Ptr<Node> n0 = CreateObject <Node> ();
90  Ptr<Node> n1 = CreateObject <Node> ();
91  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
92  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
93 
94  // Make random variable stream assignment deterministic
95  dev0->AssignStreams (0);
96  dev1->AssignStreams (10);
97 
98  dev0->SetAddress (Mac16Address ("00:01"));
99  dev1->SetAddress (Mac16Address ("00:02"));
100  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
101  Ptr<LogDistancePropagationLossModel> model = CreateObject<LogDistancePropagationLossModel> ();
102  channel->AddPropagationLossModel (model);
103  dev0->SetChannel (channel);
104  dev1->SetChannel (channel);
105  n0->AddDevice (dev0);
106  n1->AddDevice (dev1);
107  Ptr<ConstantPositionMobilityModel> mob0 = CreateObject<ConstantPositionMobilityModel> ();
108  dev0->GetPhy ()->SetMobility (mob0);
109  Ptr<ConstantPositionMobilityModel> mob1 = CreateObject<ConstantPositionMobilityModel> ();
110  dev1->GetPhy ()->SetMobility (mob1);
111 
114  dev1->GetMac ()->SetMcpsDataIndicationCallback (cb0);
115 
116  McpsDataRequestParams params;
117  params.m_srcAddrMode = SHORT_ADDR;
118  params.m_dstAddrMode = SHORT_ADDR;
119  params.m_dstPanId = 0;
120  params.m_dstAddr = Mac16Address ("00:02");
121  params.m_msduHandle = 0;
122  params.m_txOptions = 0;
123 
124  Ptr<Packet> p;
125  mob0->SetPosition (Vector (0,0,0));
126  mob1->SetPosition (Vector (100,0,0));
127  for (int i = 0; i < 1000; i++)
128  {
129  p = Create<Packet> (20);
130  Simulator::Schedule (Seconds (i),
131  &LrWpanMac::McpsDataRequest,
132  dev0->GetMac (), params, p);
133  }
134 
135  Simulator::Run ();
136 
137  // Test that we received 977 packets out of 1000, at distance of 100 m
138  // with default power of 0
139  NS_TEST_ASSERT_MSG_EQ (GetReceived (), 977, "Model fails");
140 
141  Simulator::Destroy ();
142 }
143 
144 // ==============================================================================
146  : TestCase ("Test the 802.15.4 error model")
147 {
148 }
149 
151 {
152 }
153 
154 void
156 {
157 
158  Ptr<LrWpanErrorModel> model = CreateObject<LrWpanErrorModel> ();
159 
160  // Test a few values
161  double snr = 5;
162  double ber = 1.0 - model->GetChunkSuccessRate (pow (10.0, snr / 10.0), 1);
163  NS_TEST_ASSERT_MSG_EQ_TOL (ber, 7.38e-14, 0.01e-14, "Model fails for SNR = " << snr);
164  snr = 2;
165  ber = 1.0 - model->GetChunkSuccessRate (pow (10.0, snr / 10.0), 1);
166  NS_TEST_ASSERT_MSG_EQ_TOL (ber, 5.13e-7, 0.01e-7, "Model fails for SNR = " << snr);
167  snr = -1;
168  ber = 1.0 - model->GetChunkSuccessRate (pow (10.0, snr / 10.0), 1);
169  NS_TEST_ASSERT_MSG_EQ_TOL (ber, 0.00114, 0.00001, "Model fails for SNR = " << snr);
170  snr = -4;
171  ber = 1.0 - model->GetChunkSuccessRate (pow (10.0, snr / 10.0), 1);
172  NS_TEST_ASSERT_MSG_EQ_TOL (ber, 0.0391, 0.0001, "Model fails for SNR = " << snr);
173  snr = -7;
174  ber = 1.0 - model->GetChunkSuccessRate (pow (10.0, snr / 10.0), 1);
175  NS_TEST_ASSERT_MSG_EQ_TOL (ber, 0.175, 0.001, "Model fails for SNR = " << snr);
176 }
177 
178 // ==============================================================================
180 {
181 public:
183 };
184 
186  : TestSuite ("lr-wpan-error-model", UNIT)
187 {
188  AddTestCase (new LrWpanErrorModelTestCase, TestCase::QUICK);
189  AddTestCase (new LrWpanErrorDistanceTestCase, TestCase::QUICK);
190 }
191 
tuple channel
Definition: third.py:85
double GetChunkSuccessRate(double snr, uint32_t nbits) const
Return chunk success rate for given SNR.
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:159
Callback template class.
Definition: callback.h:1176
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:160
A suite of tests to run.
Definition: test.h:1333
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
virtual void DoRun(void)
Implementation to actually run this TestCase.
encapsulates test code
Definition: test.h:1147
virtual void AddPropagationLossModel(Ptr< PropagationLossModel > loss)
Set the single-frequency propagation loss model to be used.
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:161
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
#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:161
virtual void DoRun(void)
Implementation to actually run this TestCase.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
uint8_t m_txOptions
Tx Options (bitfield)
Definition: lr-wpan-mac.h:162
static LrWpanErrorModelTestSuite lrWpanErrorModelTestSuite
#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:373
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:157
Every class exported by the ns3 library is enclosed in the ns3 namespace.
LrWpanAddressMode m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:158
This class can contain 16 bit addresses.
Definition: mac16-address.h:41
void SetPosition(const Vector &position)
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:146
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:181
void Callback(McpsDataIndicationParams params, Ptr< Packet > p)