A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
35using namespace ns3;
36using namespace ns3::lrwpan;
37
38NS_LOG_COMPONENT_DEFINE("lr-wpan-error-model-test");
39
40/**
41 * \ingroup lr-wpan-test
42 * \ingroup tests
43 *
44 * \brief LrWpan Error Vs Distance Test
45 */
47{
48 public:
51
52 /**
53 * \brief Get the number of received packets.
54 * \returns The number of received packets.
55 */
57 {
58 return m_received;
59 }
60
61 private:
62 void DoRun() override;
63
64 /**
65 * \brief Function to be called when a packet is received.
66 * \param params MCPS params.
67 * \param p The packet.
68 */
70 uint32_t m_received; //!< The number of received packets.
71};
72
73/**
74 * \ingroup lr-wpan-test
75 * \ingroup tests
76 *
77 * \brief LrWpan Error model Test
78 */
80{
81 public:
84
85 private:
86 void DoRun() override;
87};
88
90 : TestCase("Test the 802.15.4 error model vs distance"),
91 m_received(0)
92{
93}
94
96{
97}
98
99void
101{
102 m_received++;
103}
104
105void
107{
108 // Set the random seed and run number for this test
111
112 Ptr<Node> n0 = CreateObject<Node>();
113 Ptr<Node> n1 = CreateObject<Node>();
114 Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
115 Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
116
117 // Make random variable stream assignment deterministic
118 dev0->AssignStreams(0);
119 dev1->AssignStreams(10);
120
121 dev0->SetAddress(Mac16Address("00:01"));
122 dev1->SetAddress(Mac16Address("00:02"));
123 Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
124 Ptr<LogDistancePropagationLossModel> model = CreateObject<LogDistancePropagationLossModel>();
125 channel->AddPropagationLossModel(model);
126 dev0->SetChannel(channel);
127 dev1->SetChannel(channel);
128 n0->AddDevice(dev0);
129 n1->AddDevice(dev1);
130 Ptr<ConstantPositionMobilityModel> mob0 = CreateObject<ConstantPositionMobilityModel>();
131 dev0->GetPhy()->SetMobility(mob0);
132 Ptr<ConstantPositionMobilityModel> mob1 = CreateObject<ConstantPositionMobilityModel>();
133 dev1->GetPhy()->SetMobility(mob1);
134
137 dev1->GetMac()->SetMcpsDataIndicationCallback(cb0);
138
140 params.m_srcAddrMode = SHORT_ADDR;
141 params.m_dstAddrMode = SHORT_ADDR;
142 params.m_dstPanId = 0;
143 params.m_dstAddr = Mac16Address("00:02");
144 params.m_msduHandle = 0;
145 params.m_txOptions = 0;
146
147 Ptr<Packet> p;
148 mob0->SetPosition(Vector(0, 0, 0));
149 mob1->SetPosition(Vector(100, 0, 0));
150 for (int i = 0; i < 1000; i++)
151 {
152 p = Create<Packet>(20);
153 Simulator::Schedule(Seconds(i), &LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p);
154 }
155
157
158 // Test that we received 977 packets out of 1000, at distance of 100 m
159 // with default power of 0
160 NS_TEST_ASSERT_MSG_EQ(GetReceived(), 977, "Model fails");
161
163}
164
165// ==============================================================================
167 : TestCase("Test the 802.15.4 error model")
168{
169}
170
172{
173}
174
175void
177{
178 Ptr<LrWpanErrorModel> model = CreateObject<LrWpanErrorModel>();
179
180 // Test a few values
181 double snr = 5;
182 double ber = 1.0 - model->GetChunkSuccessRate(pow(10.0, snr / 10.0), 1);
183 NS_TEST_ASSERT_MSG_EQ_TOL(ber, 7.38e-14, 0.01e-14, "Model fails for SNR = " << snr);
184 snr = 2;
185 ber = 1.0 - model->GetChunkSuccessRate(pow(10.0, snr / 10.0), 1);
186 NS_TEST_ASSERT_MSG_EQ_TOL(ber, 5.13e-7, 0.01e-7, "Model fails for SNR = " << snr);
187 snr = -1;
188 ber = 1.0 - model->GetChunkSuccessRate(pow(10.0, snr / 10.0), 1);
189 NS_TEST_ASSERT_MSG_EQ_TOL(ber, 0.00114, 0.00001, "Model fails for SNR = " << snr);
190 snr = -4;
191 ber = 1.0 - model->GetChunkSuccessRate(pow(10.0, snr / 10.0), 1);
192 NS_TEST_ASSERT_MSG_EQ_TOL(ber, 0.0391, 0.0001, "Model fails for SNR = " << snr);
193 snr = -7;
194 ber = 1.0 - model->GetChunkSuccessRate(pow(10.0, snr / 10.0), 1);
195 NS_TEST_ASSERT_MSG_EQ_TOL(ber, 0.175, 0.001, "Model fails for SNR = " << snr);
196}
197
198/**
199 * \ingroup lr-wpan-test
200 * \ingroup tests
201 *
202 * \brief LrWpan Error model TestSuite
203 */
205{
206 public:
208};
209
211 : TestSuite("lr-wpan-error-model", Type::UNIT)
212{
213 AddTestCase(new LrWpanErrorModelTestCase, TestCase::Duration::QUICK);
214 AddTestCase(new LrWpanErrorDistanceTestCase, TestCase::Duration::QUICK);
215}
216
218 g_lrWpanErrorModelTestSuite; //!< Static variable for test initialization
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
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
void McpsDataRequest(McpsDataRequestParams params, Ptr< Packet > p) override
IEEE 802.15.4-2006, section 7.1.1.1 MCPS-DATA.request Request to transfer a MSDU.
Definition: lr-wpan-mac.cc:386
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:145
#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:338
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
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:706
MCPS-DATA.indication params.