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 
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);
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);
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  {
98 
99  for ( std::deque<Waypoint>::iterator w = waypoints.begin (); w != waypoints.end (); ++w )
100  {
101  mob->AddWaypoint (*w);
102  }
103  }
104 
105  // Schedule updates at non-waypoint times to make sure lazy notifications don't happen
106  for ( double updateTime = 0.5; updateTime <= ((double)waypointCount + 1.5); updateTime += 1.0 )
107  {
109  }
110 
111  Simulator::Stop (Seconds ((double)waypointCount + 2.0));
112  Simulator::Run ();
114 }
115 void
117 {
118  std::vector<Ptr<MobilityModel> >::iterator i;
119  for (i = mobilityStack.begin (); i != mobilityStack.end (); ++i)
120  {
122  mob->Update ();
123  }
124 }
125 void
127 {
128  const Time now = Simulator::Now ();
129  const double sec = now.GetSeconds ();
131 
132  NS_TEST_EXPECT_MSG_EQ (now, mob->m_current.time, "Waypoint time not properly updated");
133 
134  if ( !lazyNotify )
135  {
136  // All waypoints are on second boundaries only
137  NS_TEST_EXPECT_MSG_EQ (sec - ((double)((int)sec)) + sec, sec,
138  "Course didn't change on one second time boundary with NON-LAZY notifications");
139  }
140  else
141  {
142  // Updates should happen at the times they are forced, in between waypoints.
143  NS_TEST_EXPECT_MSG_EQ (sec - ((double)((int)sec)), 0.5,
144  "Course didn't change between waypoints with LAZY notifications");
145  }
146 }
147 
149 {
150 public:
152  : TestCase ("Check Waypoint Mobility Model waypoint add")
153  {
154  }
156  {
157  }
158 
159 private:
161  uint32_t m_waypointCount;
164 private:
165  virtual void DoRun (void);
166  virtual void DoTeardown (void);
168 };
169 
170 
171 void
173 {
174  m_mobilityModel = 0;
175 }
176 
177 void
179 {
180  m_waypointCount = 10;
181  m_waypointCounter = 1;
182 
183  ObjectFactory mobilityFactory;
184  mobilityFactory.SetTypeId ("ns3::WaypointMobilityModel");
185  mobilityFactory.Set ("LazyNotify", BooleanValue (false));
186 
187  // Create a new mobility model.
188  m_mobilityModel = mobilityFactory.Create ()->GetObject<MobilityModel> ();
190 
191  // Add this mobility model to the stack.
192  Simulator::Schedule (Seconds (0.0), &Object::Initialize, m_mobilityModel);
193 
194  Ptr<WaypointMobilityModel> mob = DynamicCast<WaypointMobilityModel> (m_mobilityModel);
195  Waypoint m_nextWaypoint (Seconds (m_waypointCounter), Vector (0.0, 0.0, 0.0));
196  mob->AddWaypoint (m_nextWaypoint);
197 
198  Simulator::Stop (Seconds ((double)m_waypointCount + 2.0));
199  Simulator::Run ();
201 }
202 
203 void
205 {
206  const Time now = Simulator::Now ();
207  Ptr<WaypointMobilityModel> mob = DynamicCast<WaypointMobilityModel> (m_mobilityModel);
208 
209  std::cout << now << " CourseChangeCallback" << std::endl;
210 
211  NS_TEST_EXPECT_MSG_EQ (now, Seconds (m_waypointCounter), "Waypoint time not properly set");
212 
213  if (now < Seconds ((double)m_waypointCount) )
214  {
215  m_waypointCounter ++;
216  m_nextWaypoint = Waypoint (Seconds (m_waypointCounter), Vector (0.0, 0.0, 0.0));
217  mob->AddWaypoint (m_nextWaypoint);
218 
219  }
220 }
221 
223 {
224  WaypointMobilityModelTestSuite () : TestSuite ("waypoint-mobility-model", UNIT)
225  {
226  AddTestCase (new WaypointMobilityModelNotifyTest (true), TestCase::QUICK);
227  AddTestCase (new WaypointMobilityModelNotifyTest (false), TestCase::QUICK);
229  }
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Time time
The waypoint time.
Definition: waypoint.h:53
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
AttributeValue implementation for Boolean.
Definition: boolean.h:34
WaypointMobilityModelTestSuite g_waypointMobilityModelTestSuite
void CourseChangeCallback(Ptr< const MobilityModel > model)
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:462
Fast test.
Definition: test.h:1152
A suite of tests to run.
Definition: test.h:1333
void AddWaypoint(const Waypoint &waypoint)
virtual void DoRun(void)
Implementation to actually run this TestCase.
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
#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:278
encapsulates test code
Definition: test.h:1147
static void CourseChangeCallback(std::string path, Ptr< const MobilityModel > model)
Keep track of the current position and velocity of an object.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:341
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1238
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void Update(void) const
Update the underlying state corresponding to the stored waypoints.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:299
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void CourseChangeCallback(Ptr< const MobilityModel > model)
void Set(std::string name, const AttributeValue &value)
Set an attribute to be set during construction.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:224
Instantiate subclasses of ns3::Object.
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:209
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
Waypoint m_current
The ns3::Waypoint currently being used.
Waypoint-based mobility model.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
std::vector< Ptr< MobilityModel > > mobilityStack
a (time, location) pair.
Definition: waypoint.h:35
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183