A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
lr-wpan-error-model-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 The Boeing Company
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Tom Henderson <thomas.r.henderson@boeing.com>
18  */
19 #include "ns3/rng-seed-manager.h"
20 #include <ns3/callback.h>
21 #include <ns3/constant-position-mobility-model.h>
22 #include <ns3/log.h>
23 #include <ns3/lr-wpan-error-model.h>
24 #include <ns3/lr-wpan-mac.h>
25 #include <ns3/lr-wpan-net-device.h>
26 #include <ns3/mac16-address.h>
27 #include <ns3/net-device.h>
28 #include <ns3/node.h>
29 #include <ns3/packet.h>
30 #include <ns3/propagation-loss-model.h>
31 #include <ns3/simulator.h>
32 #include <ns3/single-model-spectrum-channel.h>
33 #include <ns3/test.h>
34 
35 using namespace ns3;
36 
37 NS_LOG_COMPONENT_DEFINE("lr-wpan-error-model-test");
38 
46 {
47  public:
49  ~LrWpanErrorDistanceTestCase() override;
50 
55  uint32_t GetReceived() const
56  {
57  return m_received;
58  }
59 
60  private:
61  void DoRun() override;
62 
69  uint32_t m_received;
70 };
71 
79 {
80  public:
82  ~LrWpanErrorModelTestCase() override;
83 
84  private:
85  void DoRun() override;
86 };
87 
89  : TestCase("Test the 802.15.4 error model vs distance"),
90  m_received(0)
91 {
92 }
93 
95 {
96 }
97 
98 void
100 {
101  m_received++;
102 }
103 
104 void
106 {
107  // Set the random seed and run number for this test
108  RngSeedManager::SetSeed(1);
109  RngSeedManager::SetRun(6);
110 
111  Ptr<Node> n0 = CreateObject<Node>();
112  Ptr<Node> n1 = CreateObject<Node>();
113  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
114  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
115 
116  // Make random variable stream assignment deterministic
117  dev0->AssignStreams(0);
118  dev1->AssignStreams(10);
119 
120  dev0->SetAddress(Mac16Address("00:01"));
121  dev1->SetAddress(Mac16Address("00:02"));
122  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
123  Ptr<LogDistancePropagationLossModel> model = CreateObject<LogDistancePropagationLossModel>();
124  channel->AddPropagationLossModel(model);
125  dev0->SetChannel(channel);
126  dev1->SetChannel(channel);
127  n0->AddDevice(dev0);
128  n1->AddDevice(dev1);
129  Ptr<ConstantPositionMobilityModel> mob0 = CreateObject<ConstantPositionMobilityModel>();
130  dev0->GetPhy()->SetMobility(mob0);
131  Ptr<ConstantPositionMobilityModel> mob1 = CreateObject<ConstantPositionMobilityModel>();
132  dev1->GetPhy()->SetMobility(mob1);
133 
136  dev1->GetMac()->SetMcpsDataIndicationCallback(cb0);
137 
139  params.m_srcAddrMode = SHORT_ADDR;
140  params.m_dstAddrMode = SHORT_ADDR;
141  params.m_dstPanId = 0;
142  params.m_dstAddr = Mac16Address("00:02");
143  params.m_msduHandle = 0;
144  params.m_txOptions = 0;
145 
146  Ptr<Packet> p;
147  mob0->SetPosition(Vector(0, 0, 0));
148  mob1->SetPosition(Vector(100, 0, 0));
149  for (int i = 0; i < 1000; i++)
150  {
151  p = Create<Packet>(20);
152  Simulator::Schedule(Seconds(i), &LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p);
153  }
154 
155  Simulator::Run();
156 
157  // Test that we received 977 packets out of 1000, at distance of 100 m
158  // with default power of 0
159  NS_TEST_ASSERT_MSG_EQ(GetReceived(), 977, "Model fails");
160 
161  Simulator::Destroy();
162 }
163 
164 // ==============================================================================
166  : TestCase("Test the 802.15.4 error model")
167 {
168 }
169 
171 {
172 }
173 
174 void
176 {
177  Ptr<LrWpanErrorModel> model = CreateObject<LrWpanErrorModel>();
178 
179  // Test a few values
180  double snr = 5;
181  double ber = 1.0 - model->GetChunkSuccessRate(pow(10.0, snr / 10.0), 1);
182  NS_TEST_ASSERT_MSG_EQ_TOL(ber, 7.38e-14, 0.01e-14, "Model fails for SNR = " << snr);
183  snr = 2;
184  ber = 1.0 - model->GetChunkSuccessRate(pow(10.0, snr / 10.0), 1);
185  NS_TEST_ASSERT_MSG_EQ_TOL(ber, 5.13e-7, 0.01e-7, "Model fails for SNR = " << snr);
186  snr = -1;
187  ber = 1.0 - model->GetChunkSuccessRate(pow(10.0, snr / 10.0), 1);
188  NS_TEST_ASSERT_MSG_EQ_TOL(ber, 0.00114, 0.00001, "Model fails for SNR = " << snr);
189  snr = -4;
190  ber = 1.0 - model->GetChunkSuccessRate(pow(10.0, snr / 10.0), 1);
191  NS_TEST_ASSERT_MSG_EQ_TOL(ber, 0.0391, 0.0001, "Model fails for SNR = " << snr);
192  snr = -7;
193  ber = 1.0 - model->GetChunkSuccessRate(pow(10.0, snr / 10.0), 1);
194  NS_TEST_ASSERT_MSG_EQ_TOL(ber, 0.175, 0.001, "Model fails for SNR = " << snr);
195 }
196 
204 {
205  public:
207 };
208 
210  : TestSuite("lr-wpan-error-model", UNIT)
211 {
212  AddTestCase(new LrWpanErrorModelTestCase, TestCase::QUICK);
213  AddTestCase(new LrWpanErrorDistanceTestCase, TestCase::QUICK);
214 }
215 
LrWpan Error Vs Distance Test.
void Callback(McpsDataIndicationParams params, Ptr< Packet > p)
Function to be called when a packet is received.
uint32_t GetReceived() const
Get the number of received packets.
uint32_t m_received
The number of received packets.
void DoRun() override
Implementation to actually run this TestCase.
LrWpan Error model Test.
void DoRun() override
Implementation to actually run this TestCase.
LrWpan Error model TestSuite.
Callback template class.
Definition: callback.h:438
void SetChannel(Ptr< SpectrumChannel > channel)
Set the channel to which the NetDevice, and therefore the PHY, should be attached to.
Ptr< LrWpanMac > GetMac() const
Get the MAC used by this NetDevice.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Ptr< LrWpanPhy > GetPhy() const
Get the PHY used by this NetDevice.
void SetAddress(Address address) override
This method indirects to LrWpanMac::SetShortAddress ()
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
@ SHORT_ADDR
#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:144
#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:337
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
static LrWpanErrorModelTestSuite g_lrWpanErrorModelTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
channel
Definition: third.py:88
params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.
MCPS-DATA.indication params.
MCPS-DATA.request params.