A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
uan-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Leonard Tracy <lentracy@gmail.com>
7 */
8
9#include "ns3/callback.h"
10#include "ns3/constant-position-mobility-model.h"
11#include "ns3/node.h"
12#include "ns3/object-factory.h"
13#include "ns3/pointer.h"
14#include "ns3/simulator.h"
15#include "ns3/test.h"
16#include "ns3/uan-channel.h"
17#include "ns3/uan-mac-aloha.h"
18#include "ns3/uan-net-device.h"
19#include "ns3/uan-phy-gen.h"
20#include "ns3/uan-prop-model-ideal.h"
21#include "ns3/uan-transducer-hd.h"
22
23using namespace ns3;
24
25/**
26 * \ingroup uan-test
27 * \ingroup tests
28 *
29 * \brief Uan Test
30 */
31class UanTest : public TestCase
32{
33 public:
34 UanTest();
35
36 void DoRun() override;
37
38 private:
39 /**
40 * Create node function
41 * \param pos the position of the device
42 * \param chan the communication channel
43 * \returns the UAN device
44 */
46 /**
47 * Phy test function
48 * \returns true if successful
49 */
50 bool DoPhyTests();
51 /**
52 * Do one Phy test function
53 * \param t1 the time to send first packet
54 * \param t2 the time to send the second packet
55 * \param r1 first distance constant
56 * \param r2 second distance constant
57 * \param prop the propagation model
58 * \param mode1 the send mode for device 1
59 * \param mode2 the send mode for device 2
60 * \returns number of bytes received
61 */
63 Time t2,
64 uint32_t r1,
65 uint32_t r2,
67 uint16_t mode1 = 0,
68 uint16_t mode2 = 0);
69 /**
70 * Receive packet function
71 * \param dev the device
72 * \param pkt the packet
73 * \param mode the receive mode
74 * \param sender the address of the sender
75 * \returns true if successful
76 */
77 bool RxPacket(Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address& sender);
78 /**
79 * Send one packet function
80 * \param dev the device
81 * \param mode the transmit mode
82 */
83 void SendOnePacket(Ptr<UanNetDevice> dev, uint16_t mode);
85 uint32_t m_bytesRx; ///< byes received
86};
87
89 : TestCase("UAN")
90{
91}
92
93bool
96 uint16_t /* mode */,
97 const Address& /* sender */)
98{
99 m_bytesRx += pkt->GetSize();
100 return true;
101}
102
103void
105{
106 Ptr<Packet> pkt = Create<Packet>(17);
107 dev->SetTxModeIndex(mode);
108 dev->Send(pkt, dev->GetBroadcast(), 0);
109}
110
113{
119
121
122 mobility->SetPosition(pos);
123 node->AggregateObject(mobility);
124 mac->SetAddress(Mac8Address::Allocate());
125
126 dev->SetPhy(phy);
127 dev->SetMac(mac);
128 dev->SetChannel(chan);
129 dev->SetTransducer(trans);
130 node->AddDevice(dev);
131
132 return dev;
133}
134
137 Time txTime2,
138 uint32_t r1,
139 uint32_t r2,
141 uint16_t mode1,
142 uint16_t mode2)
143{
145 channel->SetAttribute("PropagationModel", PointerValue(prop));
146
147 Ptr<UanNetDevice> dev0 = CreateNode(Vector(r1, 50, 50), channel);
148 Ptr<UanNetDevice> dev1 = CreateNode(Vector(0, 50, 50), channel);
149 Ptr<UanNetDevice> dev2 = CreateNode(Vector(r1 + r2, 50, 50), channel);
150
151 dev0->SetReceiveCallback(MakeCallback(&UanTest::RxPacket, this));
152
153 Simulator::Schedule(txTime1, &UanTest::SendOnePacket, this, dev1, mode1);
154 Simulator::Schedule(txTime2, &UanTest::SendOnePacket, this, dev2, mode2);
155
156 m_bytesRx = 0;
160
161 return m_bytesRx;
162}
163
164bool
166{
167 // Phy Gen / Default PER / Default SINR
168 UanModesList mList;
169 UanTxMode mode =
170 UanTxModeFactory::CreateMode(UanTxMode::FSK, 80, 80, 10000, 4000, 2, "TestMode");
171 mList.AppendMode(UanTxMode(mode));
174 m_phyFac.SetTypeId("ns3::UanPhyGen");
175 m_phyFac.Set("PerModel", PointerValue(perDef));
176 m_phyFac.Set("SinrModel", PointerValue(sinrDef));
177 m_phyFac.Set("SupportedModes", UanModesListValue(mList));
178
180
181 // No collision (Get 2 packets)
183 34,
184 "Should have received 34 bytes from 2 disjoint packets");
185
186 // Collision (Lose both packets)
188 0,
189 "Expected collision resulting in loss of both packets");
190
191 // Phy Gen / FH-FSK SINR check
192
194 m_phyFac.Set("PerModel", PointerValue(perDef));
195 m_phyFac.Set("SinrModel", PointerValue(sinrFhfsk));
196 m_phyFac.Set("SupportedModes", UanModesListValue(mList));
197
198#ifdef UAN_PROP_BH_INSTALLED
200#endif // UAN_PROP_BH_INSTALLED
201
202 // No collision (Get 2 packets)
204 34,
205 "Should have received 34 bytes from 2 disjoint packets");
206
207 // Should correctly receive first arriving packet
209 17,
210 "Should have received 17 bytes from first arriving packet");
211
212 // Packets should collide and both be lost
214 DoOnePhyTest(Seconds(1.0), Seconds(1.0 + 7.01 * (13.0 / 80.0)), 50, 50, prop),
215 0,
216 "Packets should collide, but received data");
217
218 // Phy Dual
219 UanTxMode mode00 =
220 UanTxModeFactory::CreateMode(UanTxMode::FSK, 80, 80, 10000, 4000, 2, "TestMode00");
221 UanTxMode mode10 =
222 UanTxModeFactory::CreateMode(UanTxMode::FSK, 80, 80, 11000, 4000, 2, "TestMode10");
223 UanTxMode mode20 =
224 UanTxModeFactory::CreateMode(UanTxMode::FSK, 80, 80, 15000, 4000, 2, "TestMode20");
225 UanTxMode mode01 =
226 UanTxModeFactory::CreateMode(UanTxMode::FSK, 80, 80, 10000, 4000, 2, "TestMode01");
227 UanTxMode mode11 =
228 UanTxModeFactory::CreateMode(UanTxMode::FSK, 80, 80, 11000, 4000, 2, "TestMode11");
229 UanTxMode mode21 =
230 UanTxModeFactory::CreateMode(UanTxMode::FSK, 80, 80, 15000, 4000, 2, "TestMode21");
231
232 UanModesList m0;
233 m0.AppendMode(mode00);
234 m0.AppendMode(mode10);
235 m0.AppendMode(mode20);
237 m1.AppendMode(mode01);
238 m1.AppendMode(mode11);
239 m1.AppendMode(mode21);
240
242 m_phyFac.SetTypeId("ns3::UanPhyDual");
243 m_phyFac.Set("SupportedModesPhy1", UanModesListValue(m0));
244 m_phyFac.Set("SupportedModesPhy2", UanModesListValue(m1));
245
246 // No collision (Get 2 packets)
248 34,
249 "Expected no collision");
250
252 DoOnePhyTest(Seconds(1.0), Seconds(2.99), 50, 50, prop, 0, 0),
253 0,
254 "Expected collision with both packets lost");
255
257 DoOnePhyTest(Seconds(1.0), Seconds(2.99), 50, 50, prop, 0, 2),
258 17,
259 "Expected collision with only one packets lost");
260
262 DoOnePhyTest(Seconds(1.0), Seconds(2.99), 50, 50, prop, 0, 5),
263 34,
264 "Expected no collision");
265
267 DoOnePhyTest(Seconds(1.0), Seconds(2.99), 50, 50, prop, 2, 3),
268 34,
269 "Expected no collision");
270
271 return false;
272}
273
274void
276{
278 Ptr<Packet> pkt = Create<Packet>(1000);
279 double error = per->CalcPer(pkt, 9, UanPhyGen::GetDefaultModes()[0]);
280 NS_TEST_ASSERT_MSG_EQ_TOL(error, 0.539, 0.001, "Got PER outside of tolerance");
281
282#ifdef UAN_PROP_BH_INSTALLED
283 // Correct DB lookup for BH prop.
285 BellhopResp resp = propBh->GetResp(10000, 50, 50, 1000);
286
287 NS_TEST_ASSERT_MSG_EQ_TOL(resp.GetPathLossDb(),
288 -44.1753,
289 0.001,
290 "Got BH Pathloss outside of tolerance");
291
292 NS_TEST_ASSERT_MSG_EQ_TOL(resp.GetPdp().GetTap(4).GetAmp(),
293 0.14159,
294 0.001,
295 "Got BH arrival outside of tolerance");
296
297#endif // UAN_PROP_BH_INSTALLED
298
299 DoPhyTests();
300}
301
302/**
303 * \ingroup uan-test
304 * \ingroup tests
305 *
306 * \brief UanModesList Test
307 */
309{
310 public:
312
313 void DoRun() override;
314};
315
317 : TestCase("UanModesListTest")
318{
319}
320
321void
323{
324 // operator >> (std::istream &is, UanModesList &ml)
325 std::string inputStr;
326 std::istringstream iss;
327 UanModesList ml;
328
329 inputStr = "3|0|1|1|";
330 iss.str(inputStr);
331 iss >> ml;
332 NS_TEST_ASSERT_MSG_EQ(ml.GetNModes(), 3, "Expected 3 modes in the list");
333 NS_TEST_ASSERT_MSG_EQ(iss.eof(), true, "Expected end of file state");
334 NS_TEST_ASSERT_MSG_EQ(iss.fail(), false, "Expected no fail state");
335
336 inputStr = "3|0|1|";
337 iss.str(inputStr);
338 iss.clear();
339 iss >> ml;
340 NS_TEST_ASSERT_MSG_EQ(iss.fail(), true, "Expected fail state due to incomplete input");
341
342 inputStr = "|3|0|1|1|";
343 iss.str(inputStr);
344 iss.clear();
345 iss >> ml;
346 NS_TEST_ASSERT_MSG_EQ(iss.fail(), true, "Expected fail state due to leading delimiter");
347
348 inputStr = "3|0|1|1|0|";
349 iss.str(inputStr);
350 iss.clear();
351 iss >> ml;
352 NS_TEST_ASSERT_MSG_EQ(iss.eof(), false, "Expected no end of file state");
353
354 inputStr = "0|";
355 iss.str(inputStr);
356 iss.clear();
357 iss >> ml;
358 NS_TEST_ASSERT_MSG_EQ(ml.GetNModes(), 0, "Expected 0 modes in the list");
359 NS_TEST_ASSERT_MSG_EQ(iss.eof(), true, "Expected end of file state");
360 NS_TEST_ASSERT_MSG_EQ(iss.fail(), false, "Expected no fail state");
361
362 inputStr = "0|1|0|";
363 iss.str(inputStr);
364 iss.clear();
365 iss >> ml;
366 NS_TEST_ASSERT_MSG_EQ(iss.eof(), false, "Expected end of file state");
367
368 inputStr = "a|1|2|";
369 iss.str(inputStr);
370 iss.clear();
371 iss >> ml;
372 NS_TEST_ASSERT_MSG_EQ(iss.fail(), true, "Expected fail state due to non-numeric input");
373
374 inputStr = "a|b|c|";
375 iss.str(inputStr);
376 iss.clear();
377 iss >> ml;
378 NS_TEST_ASSERT_MSG_EQ(iss.fail(), true, "Expected fail state due to non-numeric input");
379
380 inputStr = "3|a|b|c|";
381 iss.str(inputStr);
382 iss.clear();
383 iss >> ml;
384 NS_TEST_ASSERT_MSG_EQ(iss.fail(), true, "Expected fail state due to non-numeric input");
385}
386
387/**
388 * \ingroup uan-test
389 * \ingroup tests
390 *
391 * \brief Uan Test Suite
392 */
394{
395 public:
396 UanTestSuite();
397};
398
400 : TestSuite("devices-uan", Type::UNIT)
401{
402 AddTestCase(new UanTest, TestCase::Duration::QUICK);
403 AddTestCase(new UanModesListTest, TestCase::Duration::QUICK);
404}
405
406static UanTestSuite g_uanTestSuite; ///< the test suite
UanModesList Test.
Definition uan-test.cc:309
void DoRun() override
Implementation to actually run this TestCase.
Definition uan-test.cc:322
Uan Test.
Definition uan-test.cc:32
bool RxPacket(Ptr< NetDevice > dev, Ptr< const Packet > pkt, uint16_t mode, const Address &sender)
Receive packet function.
Definition uan-test.cc:94
Ptr< UanNetDevice > CreateNode(Vector pos, Ptr< UanChannel > chan)
Create node function.
Definition uan-test.cc:112
void SendOnePacket(Ptr< UanNetDevice > dev, uint16_t mode)
Send one packet function.
Definition uan-test.cc:104
uint32_t DoOnePhyTest(Time t1, Time t2, uint32_t r1, uint32_t r2, Ptr< UanPropModel > prop, uint16_t mode1=0, uint16_t mode2=0)
Do one Phy test function.
Definition uan-test.cc:136
uint32_t m_bytesRx
byes received
Definition uan-test.cc:85
ObjectFactory m_phyFac
Phy.
Definition uan-test.cc:84
UanTest()
Definition uan-test.cc:88
void DoRun() override
Implementation to actually run this TestCase.
Definition uan-test.cc:275
bool DoPhyTests()
Phy test function.
Definition uan-test.cc:165
Uan Test Suite.
Definition uan-test.cc:394
a polymophic address class
Definition address.h:90
static Mac8Address Allocate()
Allocates Mac8Address from 0-254.
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
AttributeValue implementation for Pointer.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
Container for UanTxModes.
uint32_t GetNModes() const
Get the number of modes in this list.
void AppendMode(UanTxMode mode)
Add mode to this list.
static UanModesList GetDefaultModes()
Get the default transmission modes.
Base class for UAN Phy models.
Definition uan-phy.h:167
static UanTxMode CreateMode(UanTxMode::ModulationType type, uint32_t dataRateBps, uint32_t phyRateSps, uint32_t cfHz, uint32_t bwHz, uint32_t constSize, std::string name)
Abstraction of packet modulation information.
Definition uan-tx-mode.h:32
@ FSK
Frequency shift keying.
Definition uan-tx-mode.h:44
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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:134
#define NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition test.h:189
#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:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
const double m1
First component modulus, 232 - 209.
Definition rng-stream.cc:49
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:684
static UanTestSuite g_uanTestSuite
the test suite
Definition uan-test.cc:406