A Discrete-Event Network Simulator
API
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 
36 {
37 public:
44  : TestCase (lazy ? "Check Waypoint Mobility Model LAZY notification accuracy"
45  : "Check Waypoint Mobility Model NON-LAZY notification accuracy"),
46  lazyNotify (lazy)
47  {
48  }
50  {
51  }
52 
53 private:
54  std::vector<Ptr<MobilityModel> > mobilityStack;
55  uint32_t mobilityCount;
56  uint32_t waypointCount;
57  std::deque<Waypoint> waypoints;
58  bool lazyNotify;
59 private:
60  virtual void DoRun (void);
61  virtual void DoTeardown (void);
63  void ForceUpdates (void);
69 };
70 
71 void
73 {
74  mobilityStack.clear();
75  waypoints.clear();
76 }
77 
78 void
80 {
81  mobilityCount = 1;
82  waypointCount = 100;
83 
84  ObjectFactory mobilityFactory;
85  mobilityFactory.SetTypeId ("ns3::WaypointMobilityModel");
86  mobilityFactory.Set ("LazyNotify", BooleanValue (lazyNotify));
87 
88  // Populate the vector of mobility models.
89  for (uint32_t i = 0; i < mobilityCount; i++)
90  {
91  // Create a new mobility model.
92  Ptr<MobilityModel> model = mobilityFactory.Create ()->GetObject<MobilityModel> ();
93 
94  // Add this mobility model to the stack.
95  mobilityStack.push_back (model);
97  }
98 
99  Waypoint wpt (Seconds (0.0), Vector (0.0, 0.0, 0.0));
100 
101  // Create waypoints
102  for ( uint32_t iw = 0; iw < waypointCount; ++iw )
103  {
104  wpt.time += Seconds (1.0);
105  waypoints.push_back (wpt);
106  }
107 
108  // Add the same waypoints to each node
109  std::vector<Ptr<MobilityModel> >::iterator i;
110  for (i = mobilityStack.begin (); i != mobilityStack.end (); ++i)
111  {
114 
115  for ( std::deque<Waypoint>::iterator w = waypoints.begin (); w != waypoints.end (); ++w )
116  {
117  mob->AddWaypoint (*w);
118  }
119  }
120 
121  // Schedule updates at non-waypoint times to make sure lazy notifications don't happen
122  for ( double updateTime = 0.5; updateTime <= ((double)waypointCount + 1.5); updateTime += 1.0 )
123  {
125  }
126 
127  Simulator::Stop (Seconds ((double)waypointCount + 2.0));
128  Simulator::Run ();
130 }
131 void
133 {
134  std::vector<Ptr<MobilityModel> >::iterator i;
135  for (i = mobilityStack.begin (); i != mobilityStack.end (); ++i)
136  {
138  mob->Update ();
139  }
140 }
141 void
143 {
144  const Time now = Simulator::Now ();
145  const double sec = now.GetSeconds ();
146  Ptr<const WaypointMobilityModel> mob = model->GetObject<WaypointMobilityModel> ();
147 
148  NS_TEST_EXPECT_MSG_EQ (now, mob->m_current.time, "Waypoint time not properly updated");
149 
150  if ( !lazyNotify )
151  {
152  // All waypoints are on second boundaries only
153  NS_TEST_EXPECT_MSG_EQ (sec - ((double)((int)sec)) + sec, sec,
154  "Course didn't change on one second time boundary with NON-LAZY notifications");
155  }
156  else
157  {
158  // Updates should happen at the times they are forced, in between waypoints.
159  NS_TEST_EXPECT_MSG_EQ (sec - ((double)((int)sec)), 0.5,
160  "Course didn't change between waypoints with LAZY notifications");
161  }
162 }
163 
171 {
172 public:
174  : TestCase ("Check Waypoint Mobility Model waypoint add")
175  {
176  }
178  {
179  }
180 
181 private:
183  uint32_t m_waypointCount;
184  uint32_t m_waypointCounter;
186 private:
187  virtual void DoRun (void);
188  virtual void DoTeardown (void);
194 };
195 
196 
197 void
199 {
200  m_mobilityModel = 0;
201 }
202 
203 void
205 {
206  m_waypointCount = 10;
207  m_waypointCounter = 1;
208 
209  ObjectFactory mobilityFactory;
210  mobilityFactory.SetTypeId ("ns3::WaypointMobilityModel");
211  mobilityFactory.Set ("LazyNotify", BooleanValue (false));
212 
213  // Create a new mobility model.
214  m_mobilityModel = mobilityFactory.Create ()->GetObject<MobilityModel> ();
216 
217  // Add this mobility model to the stack.
218  Simulator::Schedule (Seconds (0.0), &Object::Initialize, m_mobilityModel);
219 
220  Ptr<WaypointMobilityModel> mob = DynamicCast<WaypointMobilityModel> (m_mobilityModel);
221  Waypoint m_nextWaypoint (Seconds (m_waypointCounter), Vector (0.0, 0.0, 0.0));
222  mob->AddWaypoint (m_nextWaypoint);
223 
224  Simulator::Stop (Seconds ((double)m_waypointCount + 2.0));
225  Simulator::Run ();
227 }
228 
229 void
231 {
232  const Time now = Simulator::Now ();
233  Ptr<WaypointMobilityModel> mob = DynamicCast<WaypointMobilityModel> (m_mobilityModel);
234 
235  std::cout << now << " CourseChangeCallback" << std::endl;
236 
237  NS_TEST_EXPECT_MSG_EQ (now, Seconds (m_waypointCounter), "Waypoint time not properly set");
238 
239  if (now < Seconds ((double)m_waypointCount) )
240  {
241  m_waypointCounter ++;
242  m_nextWaypoint = Waypoint (Seconds (m_waypointCounter), Vector (0.0, 0.0, 0.0));
243  mob->AddWaypoint (m_nextWaypoint);
244 
245  }
246 }
247 
255 {
256  WaypointMobilityModelTestSuite () : TestSuite ("waypoint-mobility-model", UNIT)
257  {
258  AddTestCase (new WaypointMobilityModelNotifyTest (true), TestCase::QUICK);
259  AddTestCase (new WaypointMobilityModelNotifyTest (false), TestCase::QUICK);
261  }
Waypoint Mobility Model Add Waypoint Test.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
void CourseChangeCallback(Ptr< const MobilityModel > model)
Course change calback.
Ptr< MobilityModel > m_mobilityModel
mobility model
virtual void DoRun(void)
Implementation to actually run this TestCase.
Waypoint Mobility Model Notify Test.
void CourseChangeCallback(Ptr< const MobilityModel > model)
Course change callback.
std::deque< Waypoint > waypoints
waypoints
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
virtual void DoRun(void)
Implementation to actually run this TestCase.
std::vector< Ptr< MobilityModel > > mobilityStack
mobilty model
WaypointMobilityModelNotifyTest(bool lazy)
Constructor.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Keep track of the current position and velocity of an object.
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:364
Instantiate subclasses of ns3::Object.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
encapsulates test code
Definition: test.h:994
@ QUICK
Fast test.
Definition: test.h:999
A suite of tests to run.
Definition: test.h:1188
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:379
a (time, location) pair.
Definition: waypoint.h:36
Time time
The waypoint time.
Definition: waypoint.h:53
Waypoint-based mobility model.
virtual void Update(void) const
Update the underlying state corresponding to the stored waypoints.
void AddWaypoint(const Waypoint &waypoint)
#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:240
WaypointMobilityModelTestSuite g_waypointMobilityModelTestSuite
the test suite
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
static void CourseChangeCallback(std::string path, Ptr< const MobilityModel > model)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
Waypoint Mobility Model Test Suite.