A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
waypoint-mobility-model-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 Phillip Sitbon
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  * Author: Phillip Sitbon <phillip@sitbon.net>
19  */
20 
21 #include "ns3/simulator.h"
22 #include "ns3/boolean.h"
23 #include "ns3/config.h"
24 #include "ns3/waypoint-mobility-model.h"
25 #include "ns3/test.h"
26 
27 using namespace ns3;
28 
30 {
31 public:
33  : TestCase (lazy ? "Check Waypoint Mobility Model LAZY notification accuracy"
34  : "Check Waypoint Mobility Model NON-LAZY notification accuracy"),
35  lazyNotify (lazy)
36  {
37  }
39  {
40  }
41 
42 private:
43  std::vector<Ptr<MobilityModel> > mobilityStack;
44  uint32_t mobilityCount;
45  uint32_t waypointCount;
46  std::deque<Waypoint> waypoints;
47  bool lazyNotify;
48 private:
49  virtual void DoRun (void);
50  virtual void DoTeardown (void);
51  void ForceUpdates (void);
52  void CourseChangeCallback (std::string path, Ptr<const MobilityModel> model);
53 };
54 
55 void
57 {
58  mobilityStack.clear();
59  waypoints.clear();
60 }
61 
62 void
64 {
65  mobilityCount = 1;
66  waypointCount = 100;
67 
68  ObjectFactory mobilityFactory;
69  mobilityFactory.SetTypeId ("ns3::WaypointMobilityModel");
70  mobilityFactory.Set ("LazyNotify", BooleanValue (lazyNotify));
71 
72  // Populate the vector of mobility models.
73  for (uint32_t i = 0; i < mobilityCount; i++)
74  {
75  // Create a new mobility model.
76  Ptr<MobilityModel> model = mobilityFactory.Create ()->GetObject<MobilityModel> ();
77 
78  // Add this mobility model to the stack.
79  mobilityStack.push_back (model);
80  Simulator::Schedule (Seconds (0.0), &Object::Initialize, model);
81  }
82 
83  Waypoint wpt (Seconds (0.0), Vector (0.0, 0.0, 0.0));
84 
85  // Create waypoints
86  for ( uint32_t iw = 0; iw < waypointCount; ++iw )
87  {
88  wpt.time += Seconds (1.0);
89  waypoints.push_back (wpt);
90  }
91 
92  // Add the same waypoints to each node
93  std::vector<Ptr<MobilityModel> >::iterator i;
94  for (i = mobilityStack.begin (); i != mobilityStack.end (); ++i)
95  {
97 
98  for ( std::deque<Waypoint>::iterator w = waypoints.begin (); w != waypoints.end (); ++w )
99  {
100  mob->AddWaypoint (*w);
101  }
102  }
103 
104  // Schedule updates at non-waypoint times to make sure lazy notifications don't happen
105  for ( double updateTime = 0.5; updateTime <= ((double)waypointCount + 1.5); updateTime += 1.0 )
106  {
108  }
109 
110  Config::Connect ("/NodeList/*/$ns3::WaypointMobilityModel/CourseChange",
112 
113  Simulator::Stop (Seconds ((double)waypointCount + 2.0));
114  Simulator::Run ();
116 }
117 void
119 {
120  std::vector<Ptr<MobilityModel> >::iterator i;
121  for (i = mobilityStack.begin (); i != mobilityStack.end (); ++i)
122  {
124  mob->Update ();
125  }
126 }
127 void
129 {
130  const Time now = Simulator::Now ();
131  const double sec = now.GetSeconds ();
133 
134  NS_TEST_EXPECT_MSG_EQ (now, mob->m_current.time, "Waypoint time not properly updated");
135 
136  if ( !lazyNotify )
137  {
138  // All waypoints are on second boundaries only
139  NS_TEST_EXPECT_MSG_EQ (sec - ((double)((int)sec)) + sec, sec,
140  "Course didn't change on one second time boundary with NON-LAZY notifcations");
141  }
142  else
143  {
144  // Updates should happen at the times they are forced, in between waypoints.
145  NS_TEST_EXPECT_MSG_EQ (sec - ((double)((int)sec)), 0.5,
146  "Course didn't change between waypoints with LAZY notifications");
147  }
148 }
149 
151 {
152  WaypointMobilityModelTestSuite () : TestSuite ("waypoint-mobility-model", UNIT)
153  {
154  AddTestCase (new WaypointMobilityModelNotifyTest (true), TestCase::QUICK);
155  AddTestCase (new WaypointMobilityModelNotifyTest (false), TestCase::QUICK);
156  }
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
Hold a bool native type.
Definition: boolean.h:38
WaypointMobilityModelTestSuite g_waypointMobilityModelTestSuite
A suite of tests to run.
Definition: test.h:1105
void AddWaypoint(const Waypoint &waypoint)
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
void SetTypeId(TypeId tid)
#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:265
encapsulates test code
Definition: test.h:929
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
static void CourseChangeCallback(std::string path, Ptr< const MobilityModel > model)
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
a 3d vector
Definition: vector.h:31
Keep track of the current position and velocity of an object.
double GetSeconds(void) const
Definition: nstime.h:272
Ptr< Object > Create(void) const
virtual void DoRun(void)
Implementation to actually run this TestCase.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void Set(std::string name, const AttributeValue &value)
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
Fast test.
Definition: test.h:937
instantiate subclasses of ns3::Object.
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
void Initialize(void)
This method calls the virtual DoInitialize method on all the objects aggregated to this object...
Definition: object.cc:179
void CourseChangeCallback(std::string path, Ptr< const MobilityModel > model)
Waypoint-based mobility model.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
std::vector< Ptr< MobilityModel > > mobilityStack
Ptr< T > GetObject(void) const
Definition: object.h:362
a (time, location) pair.
Definition: waypoint.h:34