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), 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
251 NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL(DoOnePhyTest(Seconds(1), Seconds(2.99), 50, 50, prop, 0, 0),
252 0,
253 "Expected collision with both packets lost");
254
255 NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL(DoOnePhyTest(Seconds(1), Seconds(2.99), 50, 50, prop, 0, 2),
256 17,
257 "Expected collision with only one packets lost");
258
259 NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL(DoOnePhyTest(Seconds(1), Seconds(2.99), 50, 50, prop, 0, 5),
260 34,
261 "Expected no collision");
262
263 NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL(DoOnePhyTest(Seconds(1), Seconds(2.99), 50, 50, prop, 2, 3),
264 34,
265 "Expected no collision");
266
267 return false;
268}
269
270void
272{
274 Ptr<Packet> pkt = Create<Packet>(1000);
275 double error = per->CalcPer(pkt, 9, UanPhyGen::GetDefaultModes()[0]);
276 NS_TEST_ASSERT_MSG_EQ_TOL(error, 0.539, 0.001, "Got PER outside of tolerance");
277
278#ifdef UAN_PROP_BH_INSTALLED
279 // Correct DB lookup for BH prop.
281 BellhopResp resp = propBh->GetResp(10000, 50, 50, 1000);
282
283 NS_TEST_ASSERT_MSG_EQ_TOL(resp.GetPathLossDb(),
284 -44.1753,
285 0.001,
286 "Got BH Pathloss outside of tolerance");
287
288 NS_TEST_ASSERT_MSG_EQ_TOL(resp.GetPdp().GetTap(4).GetAmp(),
289 0.14159,
290 0.001,
291 "Got BH arrival outside of tolerance");
292
293#endif // UAN_PROP_BH_INSTALLED
294
295 DoPhyTests();
296}
297
298/**
299 * @ingroup uan-test
300 * @ingroup tests
301 *
302 * @brief UanModesList Test
303 */
305{
306 public:
308
309 void DoRun() override;
310};
311
313 : TestCase("UanModesListTest")
314{
315}
316
317void
319{
320 // operator >> (std::istream &is, UanModesList &ml)
321 std::string inputStr;
322 std::istringstream iss;
323 UanModesList ml;
324
325 inputStr = "3|0|1|1|";
326 iss.str(inputStr);
327 iss >> ml;
328 NS_TEST_ASSERT_MSG_EQ(ml.GetNModes(), 3, "Expected 3 modes in the list");
329 NS_TEST_ASSERT_MSG_EQ(iss.eof(), true, "Expected end of file state");
330 NS_TEST_ASSERT_MSG_EQ(iss.fail(), false, "Expected no fail state");
331
332 inputStr = "3|0|1|";
333 iss.str(inputStr);
334 iss.clear();
335 iss >> ml;
336 NS_TEST_ASSERT_MSG_EQ(iss.fail(), true, "Expected fail state due to incomplete input");
337
338 inputStr = "|3|0|1|1|";
339 iss.str(inputStr);
340 iss.clear();
341 iss >> ml;
342 NS_TEST_ASSERT_MSG_EQ(iss.fail(), true, "Expected fail state due to leading delimiter");
343
344 inputStr = "3|0|1|1|0|";
345 iss.str(inputStr);
346 iss.clear();
347 iss >> ml;
348 NS_TEST_ASSERT_MSG_EQ(iss.eof(), false, "Expected no end of file state");
349
350 inputStr = "0|";
351 iss.str(inputStr);
352 iss.clear();
353 iss >> ml;
354 NS_TEST_ASSERT_MSG_EQ(ml.GetNModes(), 0, "Expected 0 modes in the list");
355 NS_TEST_ASSERT_MSG_EQ(iss.eof(), true, "Expected end of file state");
356 NS_TEST_ASSERT_MSG_EQ(iss.fail(), false, "Expected no fail state");
357
358 inputStr = "0|1|0|";
359 iss.str(inputStr);
360 iss.clear();
361 iss >> ml;
362 NS_TEST_ASSERT_MSG_EQ(iss.eof(), false, "Expected end of file state");
363
364 inputStr = "a|1|2|";
365 iss.str(inputStr);
366 iss.clear();
367 iss >> ml;
368 NS_TEST_ASSERT_MSG_EQ(iss.fail(), true, "Expected fail state due to non-numeric input");
369
370 inputStr = "a|b|c|";
371 iss.str(inputStr);
372 iss.clear();
373 iss >> ml;
374 NS_TEST_ASSERT_MSG_EQ(iss.fail(), true, "Expected fail state due to non-numeric input");
375
376 inputStr = "3|a|b|c|";
377 iss.str(inputStr);
378 iss.clear();
379 iss >> ml;
380 NS_TEST_ASSERT_MSG_EQ(iss.fail(), true, "Expected fail state due to non-numeric input");
381}
382
383/**
384 * @ingroup uan-test
385 * @ingroup tests
386 *
387 * @brief Uan Test Suite
388 */
390{
391 public:
392 UanTestSuite();
393};
394
396 : TestSuite("devices-uan", Type::UNIT)
397{
398 AddTestCase(new UanTest, TestCase::Duration::QUICK);
399 AddTestCase(new UanModesListTest, TestCase::Duration::QUICK);
400}
401
402static UanTestSuite g_uanTestSuite; ///< the test suite
UanModesList Test.
Definition uan-test.cc:305
void DoRun() override
Implementation to actually run this TestCase.
Definition uan-test.cc:318
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:271
bool DoPhyTests()
Phy test function.
Definition uan-test.cc:165
Uan Test Suite.
Definition uan-test.cc:390
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.
AttributeValue implementation for UanModesList.
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:1344
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:402