A Discrete-Event Network Simulator
API
mobility-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright 2010 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  */
19 
20 /*
21  * This test suite is intended to test mobility use cases in general,
22  * as typically used by user programs (i.e. with the helper layer
23  * involved).
24  */
25 
26 #include "ns3/test.h"
27 #include "ns3/boolean.h"
28 #include "ns3/simulator.h"
29 #include "ns3/scheduler.h"
30 #include "ns3/vector.h"
31 #include "ns3/mobility-model.h"
32 #include "ns3/waypoint-mobility-model.h"
33 #include "ns3/mobility-helper.h"
34 
35 using namespace ns3;
36 
45 {
46 public:
48  virtual ~WaypointLazyNotifyFalse ();
49 
50 private:
55  void TestXPosition (double expectedXPos);
61  void CourseChangeCallback (std::string path, Ptr<const MobilityModel> model);
62  virtual void DoRun (void);
66 };
67 
69  : TestCase ("Test behavior when LazyNotify is false"),
70  m_courseChanges (0)
71 {
72 }
73 
75 {
76 }
77 
78 void
80 {
81  Vector pos = m_mob->GetPosition ();
82  NS_TEST_EXPECT_MSG_EQ_TOL_INTERNAL (pos.x, expectedXPos, 0.001, "Position not equal", __FILE__, __LINE__);
83 }
84 
85 void
87 {
88  // All waypoints (at 10 second intervals) should trigger a course change
89  NS_TEST_EXPECT_MSG_EQ_TOL_INTERNAL (m_courseChanges * 10.0, Simulator::Now ().GetSeconds (), 0.001, "Course change not notified correctly", __FILE__, __LINE__);
91 }
92 
93 void
95 {
96  m_node = CreateObject<Node> ();
97  m_mob = CreateObject<WaypointMobilityModel> ();
98  // LazyNotify should by default be false
100  Waypoint wpt (Seconds (0.0), Vector (0.0, 0.0, 0.0));
101  m_mob->AddWaypoint (wpt);
102  Waypoint wpt2 (Seconds (10.0), Vector (10.0, 10.0, 10.0));
103  m_mob->AddWaypoint (wpt2);
104  Waypoint wpt3 (Seconds (20.0), Vector (20.0, 20.0, 20.0));
105  m_mob->AddWaypoint (wpt3);
106 
107  Simulator::Schedule (Seconds (5.0), &WaypointLazyNotifyFalse::TestXPosition, this, 5);
108  Simulator::Run ();
109  Simulator::Destroy ();
110 }
111 
119 {
120 public:
122  virtual ~WaypointLazyNotifyTrue ();
123 
124 private:
129  void TestXPosition (double expectedXPos);
135  void CourseChangeCallback (std::string path, Ptr<const MobilityModel> model);
136  virtual void DoRun (void);
139 };
140 
142  : TestCase ("Test behavior when LazyNotify is true")
143 {
144 }
145 
147 {
148 }
149 
150 void
152 {
153  Vector pos = m_mob->GetPosition ();
154  NS_TEST_EXPECT_MSG_EQ_TOL_INTERNAL (pos.x, expectedXPos, 0.001, "Position not equal", __FILE__, __LINE__);
155 }
156 
157 void
159 {
160  // This should trigger at time 15 only, since that is the first time that
161  // position is updated due to LazyNotify
162  NS_TEST_EXPECT_MSG_EQ_TOL_INTERNAL (15, Simulator::Now ().GetSeconds (), 0.001, "Course change not notified correctly", __FILE__, __LINE__);
163 }
164 
165 void
167 {
168  m_node = CreateObject<Node> ();
169  m_mob = CreateObject<WaypointMobilityModel> ();
170  m_mob->SetAttributeFailSafe ("LazyNotify", BooleanValue (true));
172  Waypoint wpt (Seconds (0.0), Vector (0.0, 0.0, 0.0));
173  m_mob->AddWaypoint (wpt);
174  Waypoint wpt2 (Seconds (10.0), Vector (10.0, 10.0, 10.0));
175  m_mob->AddWaypoint (wpt2);
176  Waypoint wpt3 (Seconds (20.0), Vector (20.0, 20.0, 20.0));
177  m_mob->AddWaypoint (wpt3);
178 
179  Simulator::Schedule (Seconds (15.0), &WaypointLazyNotifyTrue::TestXPosition, this, 15);
180  Simulator::Run ();
181  Simulator::Destroy ();
182 }
183 
191 {
192 public:
195 
196 private:
202  void TestXPosition (Ptr<const WaypointMobilityModel> model, double expectedXPos);
208  void TestNumWaypoints (Ptr<const WaypointMobilityModel> model, uint32_t num);
209  virtual void DoRun (void);
215 };
216 
218  : TestCase ("Test behavior of Waypoint InitialPositionIsWaypoint")
219 {
220 }
221 
223 {
224 }
225 
226 void
228 {
229  Vector pos = model->GetPosition ();
230  NS_TEST_EXPECT_MSG_EQ_TOL_INTERNAL (pos.x, expectedXPos, 0.001, "Position not equal", __FILE__, __LINE__);
231 }
232 
233 void
235 {
236  NS_TEST_EXPECT_MSG_EQ (model->WaypointsLeft (), num, "Unexpected number of waypoints left");
237 }
238 
239 void
241 {
242  // Case 1: InitialPositionIsWaypoint == false, and we call SetPosition
243  // without any waypoints added. There should be no waypoints after
244  // time 0
245  m_mob1 = CreateObject<WaypointMobilityModel> ();
246  m_mob1->SetAttributeFailSafe ("InitialPositionIsWaypoint", BooleanValue (false));
247  m_mob1->SetPosition (Vector (10.0, 10.0, 10.0));
248  // At time 1s, there should be no waypoints
249  Simulator::Schedule (Seconds (1.0), &WaypointInitialPositionIsWaypoint::TestNumWaypoints, this, m_mob1, 0);
250  // At time 15s, the model should still be at x position 10.0
251  Simulator::Schedule (Seconds (15.0), &WaypointInitialPositionIsWaypoint::TestXPosition, this, m_mob1, 10.0);
252 
253  // Case 2: InitialPositionIsWaypoint == false, and we call SetPosition
254  // after adding a waypoint.
255  m_mob2 = CreateObject<WaypointMobilityModel> ();
256  m_mob2->SetAttributeFailSafe ("InitialPositionIsWaypoint", BooleanValue (false));
257  Waypoint wpt21 (Seconds (5.0), Vector (15.0, 15.0, 15.0));
258  m_mob2->AddWaypoint (wpt21);
259  Waypoint wpt22 (Seconds (10.0), Vector (20.0, 20.0, 20.0));
260  m_mob2->AddWaypoint (wpt22);
261  m_mob2->SetPosition (Vector (10.0, 10.0, 10.0));
262  // At time 3, no waypoints have been hit, so position should be 10 and
263  // numWaypoints should be 2, or 1 excluding the next one
264  Simulator::Schedule (Seconds (3.0), &WaypointInitialPositionIsWaypoint::TestXPosition, this, m_mob2, 10.0);
265  Simulator::Schedule (Seconds (3.0), &WaypointInitialPositionIsWaypoint::TestNumWaypoints, this, m_mob2, 1);
266  // At time 8, check that X position is 18 (i.e. position is interpolating
267  // between 15 and 20) and there is one waypoint left, but we exclude
268  // the next one so we test for zero waypoints
269  Simulator::Schedule (Seconds (8.0), &WaypointInitialPositionIsWaypoint::TestXPosition, this, m_mob2, 18.0);
270  Simulator::Schedule (Seconds (8.0), &WaypointInitialPositionIsWaypoint::TestNumWaypoints, this, m_mob2, 0);
271 
272  // Case 3: InitialPositionIsWaypoint == true, and we call SetPosition
273  // without any waypoints added.
274  m_mob3 = CreateObject<WaypointMobilityModel> ();
275  m_mob3->SetAttributeFailSafe ("InitialPositionIsWaypoint", BooleanValue (true));
276  m_mob3->SetPosition (Vector (10.0, 10.0, 10.0));
277  // At time 1s, there should be zero waypoints not counting the next one
278  Simulator::Schedule (Seconds (1.0), &WaypointInitialPositionIsWaypoint::TestNumWaypoints, this, m_mob3, 0);
279  // At time 15s, the model should still be at x position 10.0
280  Simulator::Schedule (Seconds (15.0), &WaypointInitialPositionIsWaypoint::TestXPosition, this, m_mob3, 10.0);
281 
282  // Case 4: InitialPositionIsWaypoint == true, and we call SetPosition
283  // after adding a waypoint.
284  m_mob4 = CreateObject<WaypointMobilityModel> ();
285  m_mob4->SetAttributeFailSafe ("InitialPositionIsWaypoint", BooleanValue (true));
286  Waypoint wpt41 (Seconds (5.0), Vector (15.0, 15.0, 15.0));
287  m_mob4->AddWaypoint (wpt41);
288  Waypoint wpt42 (Seconds (10.0), Vector (20.0, 20.0, 20.0));
289  m_mob4->AddWaypoint (wpt42);
290  // Here, SetPosition() is called after waypoints have been added. In
291  // this case, the initial position is set until the time of the first
292  // waypoint, at which time it jumps to the waypoint and begins moving
293  m_mob4->SetPosition (Vector (10.0, 10.0, 10.0));
294  // At time 3, position should be fixed still at 10
295  Simulator::Schedule (Seconds (3.0), &WaypointInitialPositionIsWaypoint::TestXPosition, this, m_mob4, 10.0);
296  Simulator::Schedule (Seconds (3.0), &WaypointInitialPositionIsWaypoint::TestNumWaypoints, this, m_mob4, 1);
297  // At time 6, we should be moving between 15 and 20
298  Simulator::Schedule (Seconds (6.0), &WaypointInitialPositionIsWaypoint::TestXPosition, this, m_mob4, 16.0);
299  // At time 15, we should be fixed at 20
300  Simulator::Schedule (Seconds (15.0), &WaypointInitialPositionIsWaypoint::TestXPosition, this, m_mob4, 20.0);
301 
302  // case 5: If waypoint and SetPosition both called at time 0,
303  // SetPosition takes precedence
304  m_mob5 = CreateObject<WaypointMobilityModel> ();
305  m_mob5->SetAttributeFailSafe ("InitialPositionIsWaypoint", BooleanValue (true));
306  // Note: The below statement would result in a crash, because it would
307  // violate the rule that waypoints must increase in start time
308  // m_mob5->SetPosition (Vector (10.0, 10.0, 10.0));
309  Waypoint wpt51 (Seconds (0.0), Vector (200.0, 200.0, 200.0));
310  m_mob5->AddWaypoint (wpt51);
311  Waypoint wpt52 (Seconds (5.0), Vector (15.0, 15.0, 15.0));
312  m_mob5->AddWaypoint (wpt52);
313  Waypoint wpt53 (Seconds (10.0), Vector (20.0, 20.0, 20.0));
314  m_mob5->AddWaypoint (wpt53);
315  // Here, since waypoints already exist, the below SetPosition will cancel
316  // out wpt51 above, and model will stay at initial position until time 5
317  m_mob5->SetPosition (Vector (10.0, 10.0, 10.0));
318  Simulator::Schedule (Seconds (3.0), &WaypointInitialPositionIsWaypoint::TestXPosition, this, m_mob5, 10.0);
319 
320  Simulator::Run ();
321  Simulator::Destroy ();
322 }
323 
331 {
332 public:
335 
336 private:
342  void TestXPosition (Ptr<const WaypointMobilityModel> mob, double expectedXPos);
343  virtual void DoRun (void);
344 };
345 
347  : TestCase ("Test behavior using MobilityHelper and PositionAllocator")
348 {
349 }
350 
352 {
353 }
354 
355 void
357 {
358  Vector pos = mob->GetPosition ();
359  NS_TEST_EXPECT_MSG_EQ_TOL_INTERNAL (pos.x, expectedXPos, 0.001, "Position not equal", __FILE__, __LINE__);
360 }
361 
362 // WaypointMobilityModel tests using the helper
363 void
365 {
366  NodeContainer c;
367  c.Create (1);
369  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
370  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
371  mobility.SetPositionAllocator (positionAlloc);
372  // When InitialPositionIsWaypoint is false (default), the position
373  // set by the position allocator is ignored. The first waypoint set will
374  // set the initial position (with velocity 0 until first waypoint time)
375  mobility.SetMobilityModel ("ns3::WaypointMobilityModel",
376  "InitialPositionIsWaypoint", BooleanValue (false));
377  mobility.Install (c);
378 
379  // Get back a pointer to this
381  // Waypoint added at time 0 will override initial position
382  Waypoint wpt (Seconds (5.0), Vector (20.0, 20.0, 20.0));
383  Waypoint wpt2 (Seconds (10.0), Vector (10.0, 10.0, 10.0));
384  mob->AddWaypoint (wpt);
385  mob->AddWaypoint (wpt2);
386  // At time 3 (before first waypoint, position is 20
387  Simulator::Schedule (Seconds (3), &WaypointMobilityModelViaHelper::TestXPosition, this, mob, 20);
388  // At time 7.5 (midway between points 1 and 2, position is 15
389  Simulator::Schedule (Seconds (7.5), &WaypointMobilityModelViaHelper::TestXPosition, this, mob, 15);
390 
391  // When InitialPositionIsWaypoint is true, the position allocator creates
392  // the first waypoint, and movement occurs between this origin and the
393  // initial waypoint below at 5 seconds
394  NodeContainer c2;
395  c2.Create (1);
396  MobilityHelper mobility2;
397  Ptr<ListPositionAllocator> positionAlloc2 = CreateObject<ListPositionAllocator> ();
398  positionAlloc2->Add (Vector (0.0, 0.0, 0.0));
399  mobility2.SetPositionAllocator (positionAlloc2);
400  mobility2.SetMobilityModel ("ns3::WaypointMobilityModel",
401  "InitialPositionIsWaypoint", BooleanValue (true));
402  mobility2.Install (c2);
404  Waypoint wpt3 (Seconds (5.0), Vector (20.0, 20.0, 20.0));
405  mob2->AddWaypoint (wpt3);
406  // Move to position 12 at 3 seconds
407  Simulator::Schedule (Seconds (3), &WaypointMobilityModelViaHelper::TestXPosition, this, mob2, 12);
408 
409  Simulator::Run ();
410  Simulator::Destroy ();
411 }
412 
420 {
421 public:
423 };
424 
426  : TestSuite ("mobility", UNIT)
427 {
428  AddTestCase (new WaypointLazyNotifyFalse, TestCase::QUICK);
429  AddTestCase (new WaypointLazyNotifyTrue, TestCase::QUICK);
430  AddTestCase (new WaypointInitialPositionIsWaypoint, TestCase::QUICK);
431  AddTestCase (new WaypointMobilityModelViaHelper, TestCase::QUICK);
432 }
433 
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
Test whether course change notifications occur regardless of calls to Update() position (which are tr...
AttributeValue implementation for Boolean.
Definition: boolean.h:36
void CourseChangeCallback(std::string path, Ptr< const MobilityModel > model)
Course change callback.
void TestXPosition(Ptr< const WaypointMobilityModel > model, double expectedXPos)
Text X position function.
A suite of tests to run.
Definition: test.h:1343
void AddWaypoint(const Waypoint &waypoint)
Waypoint Initial Position Is Waypoint Test.
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
Waypoint Mobility Model Via Helper Test.
#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:283
Ptr< WaypointMobilityModel > m_mob5
mobility model 5
Ptr< WaypointMobilityModel > m_mob3
mobility model 3
bool SetAttributeFailSafe(std::string name, const AttributeValue &value)
Set a single attribute without raising errors.
Definition: object-base.cc:207
Ptr< WaypointMobilityModel > m_mob4
mobility model 4
encapsulates test code
Definition: test.h:1153
virtual void DoRun(void)
Implementation to actually run this TestCase.
uint32_t WaypointsLeft(void) const
Get the number of waypoints left for this object, excluding the next one.
Ptr< WaypointMobilityModel > m_mob
modility model
mobility
Definition: third.py:108
virtual void DoRun(void)
Implementation to actually run this TestCase.
void TestNumWaypoints(Ptr< const WaypointMobilityModel > model, uint32_t num)
Test number of way points.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
void TestXPosition(Ptr< const WaypointMobilityModel > mob, double expectedXPos)
Text X position function.
void TestXPosition(double expectedXPos)
Text X position function.
static MobilityTestSuite mobilityTestSuite
the test suite
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:464
virtual void DoRun(void)
Implementation to actually run this TestCase.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Mobility Test Suite.
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
Waypoint Lazy Notify True.
void SetPosition(const Vector &position)
Ptr< WaypointMobilityModel > m_mob1
mobility model 1
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
Vector GetPosition(void) const
Helper class used to assign positions and mobility models to nodes.
#define NS_TEST_EXPECT_MSG_EQ_TOL_INTERNAL(actual, limit, tol, msg, file, line)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report if ...
Definition: test.h:482
int m_courseChanges
course changes
void TestXPosition(double expectedXPos)
Test X position function.
Ptr< WaypointMobilityModel > m_mob2
mobility model 2
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1070
void Add(Vector v)
Add a position to the list of positions.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Waypoint-based mobility model.
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:309
Ptr< WaypointMobilityModel > m_mob
modility model
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
a (time, location) pair.
Definition: waypoint.h:35
void CourseChangeCallback(std::string path, Ptr< const MobilityModel > model)
Course change callback.
static void CourseChangeCallback(std::string path, Ptr< const MobilityModel > model)