A Discrete-Event Network Simulator
API
waypoint-mobility-model-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 Phillip Sitbon
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Phillip Sitbon <phillip@sitbon.net>
18  */
19 
20 #include "ns3/boolean.h"
21 #include "ns3/config.h"
22 #include "ns3/simulator.h"
23 #include "ns3/test.h"
24 #include "ns3/waypoint-mobility-model.h"
25 
26 using namespace ns3;
27 
34 {
35  public:
42  : TestCase(lazy ? "Check Waypoint Mobility Model LAZY notification accuracy"
43  : "Check Waypoint Mobility Model NON-LAZY notification accuracy"),
44  lazyNotify(lazy)
45  {
46  }
47 
49  {
50  }
51 
52  private:
53  std::vector<Ptr<MobilityModel>> mobilityStack;
54  uint32_t mobilityCount;
55  uint32_t waypointCount;
56  std::deque<Waypoint> waypoints;
57  bool lazyNotify;
58  private:
59  void DoRun() override;
60  void DoTeardown() override;
62  void ForceUpdates();
68 };
69 
70 void
72 {
73  mobilityStack.clear();
74  waypoints.clear();
75 }
76 
77 void
79 {
80  mobilityCount = 1;
81  waypointCount = 100;
82 
83  ObjectFactory mobilityFactory;
84  mobilityFactory.SetTypeId("ns3::WaypointMobilityModel");
85  mobilityFactory.Set("LazyNotify", BooleanValue(lazyNotify));
86 
87  // Populate the vector of mobility models.
88  for (uint32_t i = 0; i < mobilityCount; i++)
89  {
90  // Create a new mobility model.
91  Ptr<MobilityModel> model = mobilityFactory.Create()->GetObject<MobilityModel>();
92 
93  // Add this mobility model to the stack.
94  mobilityStack.push_back(model);
96  }
97 
98  Waypoint wpt(Seconds(0.0), Vector(0.0, 0.0, 0.0));
99 
100  // Create waypoints
101  for (uint32_t iw = 0; iw < waypointCount; ++iw)
102  {
103  wpt.time += Seconds(1.0);
104  waypoints.push_back(wpt);
105  }
106 
107  // Add the same waypoints to each node
108  for (auto i = mobilityStack.begin(); i != mobilityStack.end(); ++i)
109  {
112  "CourseChange",
114 
115  for (auto 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  {
124  Simulator::Schedule(Seconds(updateTime),
126  this);
127  }
128 
129  Simulator::Stop(Seconds((double)waypointCount + 2.0));
130  Simulator::Run();
132 }
133 
134 void
136 {
137  for (auto i = mobilityStack.begin(); i != mobilityStack.end(); ++i)
138  {
140  mob->Update();
141  }
142 }
143 
144 void
146 {
147  const Time now = Simulator::Now();
148  const double sec = now.GetSeconds();
150 
151  NS_TEST_EXPECT_MSG_EQ(now, mob->m_current.time, "Waypoint time not properly updated");
152 
153  if (!lazyNotify)
154  {
155  // All waypoints are on second boundaries only
157  sec - ((double)((int)sec)) + sec,
158  sec,
159  "Course didn't change on one second time boundary with NON-LAZY notifications");
160  }
161  else
162  {
163  // Updates should happen at the times they are forced, in between waypoints.
164  NS_TEST_EXPECT_MSG_EQ(sec - ((double)((int)sec)),
165  0.5,
166  "Course didn't change between waypoints with LAZY notifications");
167  }
168 }
169 
176 {
177  public:
179  : TestCase("Check Waypoint Mobility Model waypoint add")
180  {
181  }
182 
184  {
185  }
186 
187  private:
189  uint32_t m_waypointCount;
190  uint32_t m_waypointCounter;
192  private:
193  void DoRun() override;
194  void DoTeardown() override;
200 };
201 
202 void
204 {
205  m_mobilityModel = nullptr;
206 }
207 
208 void
210 {
211  m_waypointCount = 10;
212  m_waypointCounter = 1;
213 
214  ObjectFactory mobilityFactory;
215  mobilityFactory.SetTypeId("ns3::WaypointMobilityModel");
216  mobilityFactory.Set("LazyNotify", BooleanValue(false));
217 
218  // Create a new mobility model.
219  m_mobilityModel = mobilityFactory.Create()->GetObject<MobilityModel>();
220  m_mobilityModel->TraceConnectWithoutContext(
221  "CourseChange",
223 
224  // Add this mobility model to the stack.
225  Simulator::Schedule(Seconds(0.0), &Object::Initialize, m_mobilityModel);
226 
227  Ptr<WaypointMobilityModel> mob = DynamicCast<WaypointMobilityModel>(m_mobilityModel);
228  Waypoint m_nextWaypoint(Seconds(m_waypointCounter), Vector(0.0, 0.0, 0.0));
229  mob->AddWaypoint(m_nextWaypoint);
230 
231  Simulator::Stop(Seconds((double)m_waypointCount + 2.0));
232  Simulator::Run();
234 }
235 
236 void
238 {
239  const Time now = Simulator::Now();
240  Ptr<WaypointMobilityModel> mob = DynamicCast<WaypointMobilityModel>(m_mobilityModel);
241 
242  std::cout << now << " CourseChangeCallback" << std::endl;
243 
244  NS_TEST_EXPECT_MSG_EQ(now, Seconds(m_waypointCounter), "Waypoint time not properly set");
245 
246  if (now < Seconds((double)m_waypointCount))
247  {
248  m_waypointCounter++;
249  m_nextWaypoint = Waypoint(Seconds(m_waypointCounter), Vector(0.0, 0.0, 0.0));
250  mob->AddWaypoint(m_nextWaypoint);
251  }
252 }
253 
260 {
262  : TestSuite("waypoint-mobility-model", UNIT)
263  {
264  AddTestCase(new WaypointMobilityModelNotifyTest(true), TestCase::QUICK);
265  AddTestCase(new WaypointMobilityModelNotifyTest(false), TestCase::QUICK);
267  }
Waypoint Mobility Model Add Waypoint Test.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
void CourseChangeCallback(Ptr< const MobilityModel > model)
Course change callback.
Ptr< MobilityModel > m_mobilityModel
mobility model
void DoRun() override
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
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
void DoRun() override
Implementation to actually run this TestCase.
std::vector< Ptr< MobilityModel > > mobilityStack
mobilty model
WaypointMobilityModelNotifyTest(bool lazy)
Constructor.
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:315
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
void Initialize()
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:186
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
encapsulates test code
Definition: test.h:1060
@ QUICK
Fast test.
Definition: test.h:1065
A suite of tests to run.
Definition: test.h:1256
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
a (time, location) pair.
Definition: waypoint.h:36
Time time
The waypoint time.
Definition: waypoint.h:53
Waypoint-based mobility model.
virtual void Update() const
Update the underlying state corresponding to the stored waypoints.
void AddWaypoint(const Waypoint &waypoint)
WaypointMobilityModelTestSuite g_waypointMobilityModelTestSuite
the test suite
#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:251
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
static void CourseChangeCallback(std::string path, Ptr< const MobilityModel > model)
This function will be used below as a trace sink, if the command-line argument or default value "useC...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
Waypoint Mobility Model Test Suite.