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
26using namespace ns3;
27
35{
36 public:
43 : TestCase(lazy ? "Check Waypoint Mobility Model LAZY notification accuracy"
44 : "Check Waypoint Mobility Model NON-LAZY notification accuracy"),
45 lazyNotify(lazy)
46 {
47 }
48
50 {
51 }
52
53 private:
54 std::vector<Ptr<MobilityModel>> mobilityStack;
57 std::deque<Waypoint> waypoints;
59 private:
60 void DoRun() override;
61 void DoTeardown() override;
63 void ForceUpdates();
69};
70
71void
73{
74 mobilityStack.clear();
75 waypoints.clear();
76}
77
78void
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);
96 Simulator::Schedule(Seconds(0.0), &Object::Initialize, 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 "CourseChange",
116
117 for (std::deque<Waypoint>::iterator w = waypoints.begin(); w != waypoints.end(); ++w)
118 {
119 mob->AddWaypoint(*w);
120 }
121 }
122
123 // Schedule updates at non-waypoint times to make sure lazy notifications don't happen
124 for (double updateTime = 0.5; updateTime <= ((double)waypointCount + 1.5); updateTime += 1.0)
125 {
126 Simulator::Schedule(Seconds(updateTime),
128 this);
129 }
130
131 Simulator::Stop(Seconds((double)waypointCount + 2.0));
132 Simulator::Run();
133 Simulator::Destroy();
134}
135
136void
138{
139 std::vector<Ptr<MobilityModel>>::iterator i;
140 for (i = mobilityStack.begin(); i != mobilityStack.end(); ++i)
141 {
143 mob->Update();
144 }
145}
146
147void
149{
150 const Time now = Simulator::Now();
151 const double sec = now.GetSeconds();
153
154 NS_TEST_EXPECT_MSG_EQ(now, mob->m_current.time, "Waypoint time not properly updated");
155
156 if (!lazyNotify)
157 {
158 // All waypoints are on second boundaries only
160 sec - ((double)((int)sec)) + sec,
161 sec,
162 "Course didn't change on one second time boundary with NON-LAZY notifications");
163 }
164 else
165 {
166 // Updates should happen at the times they are forced, in between waypoints.
167 NS_TEST_EXPECT_MSG_EQ(sec - ((double)((int)sec)),
168 0.5,
169 "Course didn't change between waypoints with LAZY notifications");
170 }
171}
172
180{
181 public:
183 : TestCase("Check Waypoint Mobility Model waypoint add")
184 {
185 }
186
188 {
189 }
190
191 private:
196 private:
197 void DoRun() override;
198 void DoTeardown() override;
204};
205
206void
208{
209 m_mobilityModel = nullptr;
210}
211
212void
214{
215 m_waypointCount = 10;
217
218 ObjectFactory mobilityFactory;
219 mobilityFactory.SetTypeId("ns3::WaypointMobilityModel");
220 mobilityFactory.Set("LazyNotify", BooleanValue(false));
221
222 // Create a new mobility model.
223 m_mobilityModel = mobilityFactory.Create()->GetObject<MobilityModel>();
225 "CourseChange",
227
228 // Add this mobility model to the stack.
229 Simulator::Schedule(Seconds(0.0), &Object::Initialize, m_mobilityModel);
230
231 Ptr<WaypointMobilityModel> mob = DynamicCast<WaypointMobilityModel>(m_mobilityModel);
232 Waypoint m_nextWaypoint(Seconds(m_waypointCounter), Vector(0.0, 0.0, 0.0));
234
235 Simulator::Stop(Seconds((double)m_waypointCount + 2.0));
236 Simulator::Run();
237 Simulator::Destroy();
238}
239
240void
242{
243 const Time now = Simulator::Now();
244 Ptr<WaypointMobilityModel> mob = DynamicCast<WaypointMobilityModel>(m_mobilityModel);
245
246 std::cout << now << " CourseChangeCallback" << std::endl;
247
248 NS_TEST_EXPECT_MSG_EQ(now, Seconds(m_waypointCounter), "Waypoint time not properly set");
249
250 if (now < Seconds((double)m_waypointCount))
251 {
253 m_nextWaypoint = Waypoint(Seconds(m_waypointCounter), Vector(0.0, 0.0, 0.0));
255 }
256}
257
265{
267 : TestSuite("waypoint-mobility-model", UNIT)
268 {
269 AddTestCase(new WaypointMobilityModelNotifyTest(true), TestCase::QUICK);
270 AddTestCase(new WaypointMobilityModelNotifyTest(false), TestCase::QUICK);
272 }
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 calback.
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.
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:369
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.
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:78
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:305
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
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:402
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)
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
#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
WaypointMobilityModelTestSuite g_waypointMobilityModelTestSuite
the test suite
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
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:691
Waypoint Mobility Model Test Suite.