A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
waypoint-mobility-model-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 Phillip Sitbon
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Phillip Sitbon <phillip@sitbon.net>
7 */
8
9#include "ns3/boolean.h"
10#include "ns3/config.h"
11#include "ns3/simulator.h"
12#include "ns3/test.h"
13#include "ns3/waypoint-mobility-model.h"
14
15using namespace ns3;
16
17/**
18 * \ingroup mobility-test
19 *
20 * \brief Waypoint Mobility Model Notify Test
21 */
23{
24 public:
25 /**
26 * Constructor
27 *
28 * \param lazy lazy?
29 */
31 : TestCase(lazy ? "Check Waypoint Mobility Model LAZY notification accuracy"
32 : "Check Waypoint Mobility Model NON-LAZY notification accuracy"),
33 lazyNotify(lazy)
34 {
35 }
36
40
41 private:
42 std::vector<Ptr<MobilityModel>> mobilityStack; ///< mobilty model
43 uint32_t mobilityCount; ///< mobility count
44 uint32_t waypointCount; ///< waypoint count
45 std::deque<Waypoint> waypoints; ///< waypoints
46 bool lazyNotify; ///< lazy notify?
47 private:
48 void DoRun() override;
49 void DoTeardown() override;
50 /// Force updates
51 void ForceUpdates();
52 /**
53 * Course change callback
54 * \param model the mobility model
55 */
57};
58
59void
65
66void
68{
69 mobilityCount = 1;
70 waypointCount = 100;
71
72 ObjectFactory mobilityFactory;
73 mobilityFactory.SetTypeId("ns3::WaypointMobilityModel");
74 mobilityFactory.Set("LazyNotify", BooleanValue(lazyNotify));
75
76 // Populate the vector of mobility models.
77 for (uint32_t i = 0; i < mobilityCount; i++)
78 {
79 // Create a new mobility model.
80 Ptr<MobilityModel> model = mobilityFactory.Create()->GetObject<MobilityModel>();
81
82 // Add this mobility model to the stack.
83 mobilityStack.push_back(model);
85 }
86
87 Waypoint wpt(Seconds(0.0), Vector(0.0, 0.0, 0.0));
88
89 // Create waypoints
90 for (uint32_t iw = 0; iw < waypointCount; ++iw)
91 {
92 wpt.time += Seconds(1.0);
93 waypoints.push_back(wpt);
94 }
95
96 // Add the same waypoints to each node
97 for (auto i = mobilityStack.begin(); i != mobilityStack.end(); ++i)
98 {
100 mob->TraceConnectWithoutContext(
101 "CourseChange",
103
104 for (auto w = waypoints.begin(); w != waypoints.end(); ++w)
105 {
106 mob->AddWaypoint(*w);
107 }
108 }
109
110 // Schedule updates at non-waypoint times to make sure lazy notifications don't happen
111 for (double updateTime = 0.5; updateTime <= ((double)waypointCount + 1.5); updateTime += 1.0)
112 {
113 Simulator::Schedule(Seconds(updateTime),
115 this);
116 }
117
118 Simulator::Stop(Seconds((double)waypointCount + 2.0));
121}
122
123void
125{
126 for (auto i = mobilityStack.begin(); i != mobilityStack.end(); ++i)
127 {
128 Ptr<WaypointMobilityModel> mob = (*i)->GetObject<WaypointMobilityModel>();
129 mob->Update();
130 }
131}
132
133void
135{
136 const Time now = Simulator::Now();
137 const double sec = now.GetSeconds();
139
140 NS_TEST_EXPECT_MSG_EQ(now, mob->m_current.time, "Waypoint time not properly updated");
141
142 if (!lazyNotify)
143 {
144 // All waypoints are on second boundaries only
146 sec - ((double)((int)sec)) + sec,
147 sec,
148 "Course didn't change on one second time boundary with NON-LAZY notifications");
149 }
150 else
151 {
152 // Updates should happen at the times they are forced, in between waypoints.
153 NS_TEST_EXPECT_MSG_EQ(sec - ((double)((int)sec)),
154 0.5,
155 "Course didn't change between waypoints with LAZY notifications");
156 }
157}
158
159/**
160 * \ingroup mobility-test
161 *
162 * \brief Waypoint Mobility Model Add Waypoint Test
163 */
165{
166 public:
168 : TestCase("Check Waypoint Mobility Model waypoint add")
169 {
170 }
171
175
176 private:
178 uint32_t m_waypointCount; ///< waypoint count
179 uint32_t m_waypointCounter; ///< waypoint counter
180 Waypoint m_nextWaypoint; ///< next waypoint
181 private:
182 void DoRun() override;
183 void DoTeardown() override;
184 /**
185 * Course change callback
186 * \param model the mobility model
187 */
189};
190
191void
196
197void
199{
200 m_waypointCount = 10;
202
203 ObjectFactory mobilityFactory;
204 mobilityFactory.SetTypeId("ns3::WaypointMobilityModel");
205 mobilityFactory.Set("LazyNotify", BooleanValue(false));
206
207 // Create a new mobility model.
208 m_mobilityModel = mobilityFactory.Create()->GetObject<MobilityModel>();
210 "CourseChange",
212
213 // Add this mobility model to the stack.
215
217 Waypoint m_nextWaypoint(Seconds(m_waypointCounter), Vector(0.0, 0.0, 0.0));
218 mob->AddWaypoint(m_nextWaypoint);
219
223}
224
225void
227{
228 const Time now = Simulator::Now();
230
231 std::cout << now << " CourseChangeCallback" << std::endl;
232
233 NS_TEST_EXPECT_MSG_EQ(now, Seconds(m_waypointCounter), "Waypoint time not properly set");
234
235 if (now < Seconds((double)m_waypointCount))
236 {
238 m_nextWaypoint = Waypoint(Seconds(m_waypointCounter), Vector(0.0, 0.0, 0.0));
239 mob->AddWaypoint(m_nextWaypoint);
240 }
241}
242
243/**
244 * \ingroup mobility-test
245 *
246 * \brief Waypoint Mobility Model Test Suite
247 */
249{
251 : TestSuite("waypoint-mobility-model", Type::UNIT)
252 {
253 AddTestCase(new WaypointMobilityModelNotifyTest(true), TestCase::Duration::QUICK);
254 AddTestCase(new WaypointMobilityModelNotifyTest(false), TestCase::Duration::QUICK);
255 AddTestCase(new WaypointMobilityModelAddWaypointTest(), TestCase::Duration::QUICK);
256 }
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
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.
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:203
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
static constexpr auto UNIT
Definition test.h:1291
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
a (time, location) pair.
Definition waypoint.h:25
Time time
The waypoint time.
Definition waypoint.h:42
Waypoint-based mobility model.
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:241
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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:684
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
Waypoint Mobility Model Test Suite.