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);
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);
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: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
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
Fast test.
Definition: test.h:1152
A suite of tests to run.
Definition: test.h:1333
void AddWaypoint(const Waypoint &waypoint)
static void Run(void)
Run the simulation.
Definition: simulator.cc:200
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:1216
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:1480
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:835
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:164
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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:223
Instantiate subclasses of ns3::Object.
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:208
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void CourseChangeCallback(std::string path, Ptr< const MobilityModel > model)
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