A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
mobility-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright 2010 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8/*
9 * This test suite is intended to test mobility use cases in general,
10 * as typically used by user programs (i.e. with the helper layer
11 * involved).
12 */
13
14#include "ns3/boolean.h"
15#include "ns3/mobility-helper.h"
16#include "ns3/mobility-model.h"
17#include "ns3/scheduler.h"
18#include "ns3/simulator.h"
19#include "ns3/test.h"
20#include "ns3/vector.h"
21#include "ns3/waypoint-mobility-model.h"
22
23using namespace ns3;
24
25/**
26 * \ingroup mobility-test
27 *
28 * \brief Test whether course change notifications occur regardless of calls
29 * to Update() position (which are triggered by calls to GetPosition())
30 */
32{
33 public:
35 ~WaypointLazyNotifyFalse() override;
36
37 private:
38 /**
39 * Test X position function
40 * \param expectedXPos the expected X position
41 */
42 void TestXPosition(double expectedXPos);
43 /**
44 * Course change callback
45 * \param path the path
46 * \param model the mobility model
47 */
48 void CourseChangeCallback(std::string path, Ptr<const MobilityModel> model);
49 void DoRun() override;
50 Ptr<Node> m_node; ///< mode
51 Ptr<WaypointMobilityModel> m_mob; ///< modility model
52 int m_courseChanges; ///< course changes
53};
54
56 : TestCase("Test behavior when LazyNotify is false"),
57 m_courseChanges(0)
58{
59}
60
64
65void
67{
68 Vector pos = m_mob->GetPosition();
69 NS_TEST_EXPECT_MSG_EQ_TOL(pos.x, expectedXPos, 0.001, "Position not equal");
70}
71
72void
74{
75 // All waypoints (at 10 second intervals) should trigger a course change
77 Simulator::Now().GetSeconds(),
78 0.001,
79 "Course change not notified correctly");
81}
82
83void
85{
88 // LazyNotify should by default be false
90 Waypoint wpt(Seconds(0.0), Vector(0.0, 0.0, 0.0));
91 m_mob->AddWaypoint(wpt);
92 Waypoint wpt2(Seconds(10.0), Vector(10.0, 10.0, 10.0));
93 m_mob->AddWaypoint(wpt2);
94 Waypoint wpt3(Seconds(20.0), Vector(20.0, 20.0, 20.0));
95 m_mob->AddWaypoint(wpt3);
96
100}
101
102/**
103 * \ingroup mobility-test
104 *
105 * \brief Waypoint Lazy Notify True
106 */
108{
109 public:
111 ~WaypointLazyNotifyTrue() override;
112
113 private:
114 /**
115 * Text X position function
116 * \param expectedXPos the expected X position
117 */
118 void TestXPosition(double expectedXPos);
119 /**
120 * Course change callback
121 * \param path the path
122 * \param model the mobility model
123 */
124 void CourseChangeCallback(std::string path, Ptr<const MobilityModel> model);
125 void DoRun() override;
126 Ptr<Node> m_node; ///< node
128};
129
131 : TestCase("Test behavior when LazyNotify is true")
132{
133}
134
138
139void
141{
142 Vector pos = m_mob->GetPosition();
143 NS_TEST_EXPECT_MSG_EQ_TOL(pos.x, expectedXPos, 0.001, "Position not equal");
144}
145
146void
148{
149 // This should trigger at time 15 only, since that is the first time that
150 // position is updated due to LazyNotify
152 Simulator::Now().GetSeconds(),
153 0.001,
154 "Course change not notified correctly");
155}
156
157void
159{
162 m_mob->SetAttributeFailSafe("LazyNotify", BooleanValue(true));
164 Waypoint wpt(Seconds(0.0), Vector(0.0, 0.0, 0.0));
165 m_mob->AddWaypoint(wpt);
166 Waypoint wpt2(Seconds(10.0), Vector(10.0, 10.0, 10.0));
167 m_mob->AddWaypoint(wpt2);
168 Waypoint wpt3(Seconds(20.0), Vector(20.0, 20.0, 20.0));
169 m_mob->AddWaypoint(wpt3);
170
174}
175
176/**
177 * \ingroup mobility-test
178 *
179 * \brief Waypoint Initial Position Is Waypoint Test
180 */
182{
183 public:
186
187 private:
188 /**
189 * Text X position function
190 * \param model the mobility model
191 * \param expectedXPos the expected X position
192 */
193 void TestXPosition(Ptr<const WaypointMobilityModel> model, double expectedXPos);
194 /**
195 * Test number of way points
196 * \param model the mobility model
197 * \param num the number of way points
198 */
200 void DoRun() override;
201 Ptr<WaypointMobilityModel> m_mob1; ///< mobility model 1
202 Ptr<WaypointMobilityModel> m_mob2; ///< mobility model 2
203 Ptr<WaypointMobilityModel> m_mob3; ///< mobility model 3
204 Ptr<WaypointMobilityModel> m_mob4; ///< mobility model 4
205 Ptr<WaypointMobilityModel> m_mob5; ///< mobility model 5
206};
207
209 : TestCase("Test behavior of Waypoint InitialPositionIsWaypoint")
210{
211}
212
216
217void
219 double expectedXPos)
220{
221 Vector pos = model->GetPosition();
222 NS_TEST_EXPECT_MSG_EQ_TOL(pos.x, expectedXPos, 0.001, "Position not equal");
223}
224
225void
227 uint32_t num)
228{
229 NS_TEST_EXPECT_MSG_EQ(model->WaypointsLeft(), num, "Unexpected number of waypoints left");
230}
231
232void
234{
235 // Case 1: InitialPositionIsWaypoint == false, and we call SetPosition
236 // without any waypoints added. There should be no waypoints after
237 // time 0
239 m_mob1->SetAttributeFailSafe("InitialPositionIsWaypoint", BooleanValue(false));
240 m_mob1->SetPosition(Vector(10.0, 10.0, 10.0));
241 // At time 1s, there should be no waypoints
244 this,
245 m_mob1,
246 0);
247 // At time 15s, the model should still be at x position 10.0
250 this,
251 m_mob1,
252 10.0);
253
254 // Case 2: InitialPositionIsWaypoint == false, and we call SetPosition
255 // after adding a waypoint.
257 m_mob2->SetAttributeFailSafe("InitialPositionIsWaypoint", BooleanValue(false));
258 Waypoint wpt21(Seconds(5.0), Vector(15.0, 15.0, 15.0));
259 m_mob2->AddWaypoint(wpt21);
260 Waypoint wpt22(Seconds(10.0), Vector(20.0, 20.0, 20.0));
261 m_mob2->AddWaypoint(wpt22);
262 m_mob2->SetPosition(Vector(10.0, 10.0, 10.0));
263 // At time 3, no waypoints have been hit, so position should be 10 and
264 // numWaypoints should be 2, or 1 excluding the next one
267 this,
268 m_mob2,
269 10.0);
272 this,
273 m_mob2,
274 1);
275 // At time 8, check that X position is 18 (i.e. position is interpolating
276 // between 15 and 20) and there is one waypoint left, but we exclude
277 // the next one so we test for zero waypoints
280 this,
281 m_mob2,
282 18.0);
285 this,
286 m_mob2,
287 0);
288
289 // Case 3: InitialPositionIsWaypoint == true, and we call SetPosition
290 // without any waypoints added.
292 m_mob3->SetAttributeFailSafe("InitialPositionIsWaypoint", BooleanValue(true));
293 m_mob3->SetPosition(Vector(10.0, 10.0, 10.0));
294 // At time 1s, there should be zero waypoints not counting the next one
297 this,
298 m_mob3,
299 0);
300 // At time 15s, the model should still be at x position 10.0
303 this,
304 m_mob3,
305 10.0);
306
307 // Case 4: InitialPositionIsWaypoint == true, and we call SetPosition
308 // after adding a waypoint.
310 m_mob4->SetAttributeFailSafe("InitialPositionIsWaypoint", BooleanValue(true));
311 Waypoint wpt41(Seconds(5.0), Vector(15.0, 15.0, 15.0));
312 m_mob4->AddWaypoint(wpt41);
313 Waypoint wpt42(Seconds(10.0), Vector(20.0, 20.0, 20.0));
314 m_mob4->AddWaypoint(wpt42);
315 // Here, SetPosition() is called after waypoints have been added. In
316 // this case, the initial position is set until the time of the first
317 // waypoint, at which time it jumps to the waypoint and begins moving
318 m_mob4->SetPosition(Vector(10.0, 10.0, 10.0));
319 // At time 3, position should be fixed still at 10
322 this,
323 m_mob4,
324 10.0);
327 this,
328 m_mob4,
329 1);
330 // At time 6, we should be moving between 15 and 20
333 this,
334 m_mob4,
335 16.0);
336 // At time 15, we should be fixed at 20
339 this,
340 m_mob4,
341 20.0);
342
343 // case 5: If waypoint and SetPosition both called at time 0,
344 // SetPosition takes precedence
346 m_mob5->SetAttributeFailSafe("InitialPositionIsWaypoint", BooleanValue(true));
347 // Note: The below statement would result in a crash, because it would
348 // violate the rule that waypoints must increase in start time
349 // m_mob5->SetPosition (Vector (10.0, 10.0, 10.0));
350 Waypoint wpt51(Seconds(0.0), Vector(200.0, 200.0, 200.0));
351 m_mob5->AddWaypoint(wpt51);
352 Waypoint wpt52(Seconds(5.0), Vector(15.0, 15.0, 15.0));
353 m_mob5->AddWaypoint(wpt52);
354 Waypoint wpt53(Seconds(10.0), Vector(20.0, 20.0, 20.0));
355 m_mob5->AddWaypoint(wpt53);
356 // Here, since waypoints already exist, the below SetPosition will cancel
357 // out wpt51 above, and model will stay at initial position until time 5
358 m_mob5->SetPosition(Vector(10.0, 10.0, 10.0));
361 this,
362 m_mob5,
363 10.0);
364
367}
368
369/**
370 * \ingroup mobility-test
371 *
372 * \brief Waypoint Mobility Model Via Helper Test
373 */
375{
376 public:
379
380 private:
381 /**
382 * Text X position function
383 * \param mob the mobility model
384 * \param expectedXPos the expected X position
385 */
386 void TestXPosition(Ptr<const WaypointMobilityModel> mob, double expectedXPos);
387 void DoRun() override;
388};
389
391 : TestCase("Test behavior using MobilityHelper and PositionAllocator")
392{
393}
394
398
399void
401 double expectedXPos)
402{
403 Vector pos = mob->GetPosition();
404 NS_TEST_EXPECT_MSG_EQ_TOL(pos.x, expectedXPos, 0.001, "Position not equal");
405}
406
407// WaypointMobilityModel tests using the helper
408void
410{
412 c.Create(1);
413 MobilityHelper mobility;
415 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
416 mobility.SetPositionAllocator(positionAlloc);
417 // When InitialPositionIsWaypoint is false (default), the position
418 // set by the position allocator is ignored. The first waypoint set will
419 // set the initial position (with velocity 0 until first waypoint time)
420 mobility.SetMobilityModel("ns3::WaypointMobilityModel",
421 "InitialPositionIsWaypoint",
422 BooleanValue(false));
423 mobility.Install(c);
424
425 // Get back a pointer to this
427 // Waypoint added at time 0 will override initial position
428 Waypoint wpt(Seconds(5.0), Vector(20.0, 20.0, 20.0));
429 Waypoint wpt2(Seconds(10.0), Vector(10.0, 10.0, 10.0));
430 mob->AddWaypoint(wpt);
431 mob->AddWaypoint(wpt2);
432 // At time 3 (before first waypoint, position is 20
434 // At time 7.5 (midway between points 1 and 2, position is 15
437 this,
438 mob,
439 15);
440
441 // When InitialPositionIsWaypoint is true, the position allocator creates
442 // the first waypoint, and movement occurs between this origin and the
443 // initial waypoint below at 5 seconds
444 NodeContainer c2;
445 c2.Create(1);
446 MobilityHelper mobility2;
448 positionAlloc2->Add(Vector(0.0, 0.0, 0.0));
449 mobility2.SetPositionAllocator(positionAlloc2);
450 mobility2.SetMobilityModel("ns3::WaypointMobilityModel",
451 "InitialPositionIsWaypoint",
452 BooleanValue(true));
453 mobility2.Install(c2);
455 Waypoint wpt3(Seconds(5.0), Vector(20.0, 20.0, 20.0));
456 mob2->AddWaypoint(wpt3);
457 // Move to position 12 at 3 seconds
459
462}
463
464/**
465 * \ingroup mobility-test
466 *
467 * \brief Mobility Test Suite
468 */
470{
471 public:
473};
474
476 : TestSuite("mobility", Type::UNIT)
477{
478 AddTestCase(new WaypointLazyNotifyFalse, TestCase::Duration::QUICK);
479 AddTestCase(new WaypointLazyNotifyTrue, TestCase::Duration::QUICK);
480 AddTestCase(new WaypointInitialPositionIsWaypoint, TestCase::Duration::QUICK);
481 AddTestCase(new WaypointMobilityModelViaHelper, TestCase::Duration::QUICK);
482}
483
484/**
485 * \ingroup mobility-test
486 * Static variable for test initialization
487 */
Mobility Test Suite.
Waypoint Initial Position Is Waypoint Test.
Ptr< WaypointMobilityModel > m_mob4
mobility model 4
Ptr< WaypointMobilityModel > m_mob5
mobility model 5
void DoRun() override
Implementation to actually run this TestCase.
Ptr< WaypointMobilityModel > m_mob2
mobility model 2
Ptr< WaypointMobilityModel > m_mob1
mobility model 1
void TestNumWaypoints(Ptr< const WaypointMobilityModel > model, uint32_t num)
Test number of way points.
void TestXPosition(Ptr< const WaypointMobilityModel > model, double expectedXPos)
Text X position function.
Ptr< WaypointMobilityModel > m_mob3
mobility model 3
Test whether course change notifications occur regardless of calls to Update() position (which are tr...
void CourseChangeCallback(std::string path, Ptr< const MobilityModel > model)
Course change callback.
void TestXPosition(double expectedXPos)
Test X position function.
int m_courseChanges
course changes
void DoRun() override
Implementation to actually run this TestCase.
Ptr< WaypointMobilityModel > m_mob
modility model
Waypoint Lazy Notify True.
void TestXPosition(double expectedXPos)
Text X position function.
Ptr< WaypointMobilityModel > m_mob
modility model
void DoRun() override
Implementation to actually run this TestCase.
void CourseChangeCallback(std::string path, Ptr< const MobilityModel > model)
Course change callback.
Waypoint Mobility Model Via Helper Test.
void DoRun() override
Implementation to actually run this TestCase.
void TestXPosition(Ptr< const WaypointMobilityModel > mob, double expectedXPos)
Text X position function.
Helper class used to assign positions and mobility models to nodes.
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
void SetMobilityModel(std::string type, Ts &&... args)
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition object.cc:298
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 Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
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
a (time, location) pair.
Definition waypoint.h:25
Waypoint-based mobility model.
static MobilityTestSuite mobilityTestSuite
Static variable for test initialization.
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition test.h:241
#define NS_TEST_EXPECT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report if ...
Definition test.h:500
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.