A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
outdoor-random-walk-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 SIGNET Lab, Department of Information Engineering,
3 * University of Padova
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
19#include "ns3/abort.h"
20#include "ns3/building-position-allocator.h"
21#include "ns3/building.h"
22#include "ns3/config.h"
23#include "ns3/double.h"
24#include "ns3/enum.h"
25#include "ns3/log.h"
26#include "ns3/mobility-helper.h"
27#include "ns3/pointer.h"
28#include "ns3/random-walk-2d-outdoor-mobility-model.h"
29#include "ns3/simulator.h"
30#include "ns3/test.h"
31
32using namespace ns3;
33
34NS_LOG_COMPONENT_DEFINE("OutdoorRandomWalkTest");
35
36/**
37 * \ingroup building-test
38 *
39 * Test case for the class OutdoorRandomWalkTestCase. It checks if the
40 * positions visited by the user are outside buildings
41 */
43{
44 public:
47
48 private:
49 void DoRun() override;
50
51 /**
52 * Check that the position is the expected one
53 * \param model Mobility model
54 */
56
57 std::vector<Ptr<Building>> m_buildings; //!< Buildings
58};
59
61 : TestCase("Test case for the BuildingsChannelConditionModel"),
62 m_buildings()
63{
64}
65
67{
68}
69
70void
72{
73 auto position = model->GetPosition();
74 for (auto building : m_buildings)
75 {
76 NS_TEST_ASSERT_MSG_EQ(building->IsInside(position),
77 false,
78 "Position " << position << " is inside");
79 }
80}
81
82void
84{
85 // Samples to test and time steps
86 constexpr double TEST_STEP_S = 10; // s
87 constexpr int MAX_CHECKS = 1000;
88
89 // create a grid of buildings
90 double buildingSizeX = 100; // m
91 double buildingSizeY = 50; // m
92 double streetWidth = 25; // m
93 double buildingHeight = 10; // m
94 uint32_t numBuildingsX = 20;
95 uint32_t numBuildingsY = 20;
96 double maxAxisX = (buildingSizeX + streetWidth) * numBuildingsX;
97 double maxAxisY = (buildingSizeY + streetWidth) * numBuildingsY;
98
99 for (uint32_t buildingIdX = 0; buildingIdX < numBuildingsX; ++buildingIdX)
100 {
101 for (uint32_t buildingIdY = 0; buildingIdY < numBuildingsY; ++buildingIdY)
102 {
103 Ptr<Building> building;
104 building = CreateObject<Building>();
105
106 building->SetBoundaries(Box(buildingIdX * (buildingSizeX + streetWidth),
107 buildingIdX * (buildingSizeX + streetWidth) + buildingSizeX,
108 buildingIdY * (buildingSizeY + streetWidth),
109 buildingIdY * (buildingSizeY + streetWidth) + buildingSizeY,
110 0.0,
111 buildingHeight));
112 building->SetNRoomsX(1);
113 building->SetNRoomsY(1);
114 building->SetNFloors(1);
115 m_buildings.push_back(building);
116 }
117 }
118
119 // create one node
121 nodes.Create(1);
122
123 // set the RandomWalk2dOutdoorMobilityModel mobility model
124 MobilityHelper mobility;
125 mobility.SetMobilityModel(
126 "ns3::RandomWalk2dOutdoorMobilityModel",
127 "Bounds",
128 RectangleValue(Rectangle(-streetWidth, maxAxisX, -streetWidth, maxAxisY)),
129 "Mode",
131 "Time",
132 TimeValue(Seconds(TEST_STEP_S * MAX_CHECKS)));
133 // create an OutdoorPositionAllocator and set its boundaries to match those of the mobility
134 // model
135 Ptr<OutdoorPositionAllocator> position = CreateObject<OutdoorPositionAllocator>();
136 Ptr<UniformRandomVariable> xPos = CreateObject<UniformRandomVariable>();
137 xPos->SetAttribute("Min", DoubleValue(-streetWidth));
138 xPos->SetAttribute("Max", DoubleValue(maxAxisX));
139 Ptr<UniformRandomVariable> yPos = CreateObject<UniformRandomVariable>();
140 yPos->SetAttribute("Min", DoubleValue(-streetWidth));
141 yPos->SetAttribute("Max", DoubleValue(maxAxisY));
142 position->SetAttribute("X", PointerValue(xPos));
143 position->SetAttribute("Y", PointerValue(yPos));
144 mobility.SetPositionAllocator(position);
145 // install the mobility model
146 mobility.Install(nodes.Get(0));
147
148 auto mobilityModel = nodes.Get(0)->GetObject<RandomWalk2dOutdoorMobilityModel>();
149
150 // get MAX_CHECKS positions, check if they are outdoors
151 for (int i = 0; i < MAX_CHECKS; i++)
152 {
153 Simulator::Schedule(Seconds(i * TEST_STEP_S),
155 this,
156 mobilityModel);
157 }
158
159 Simulator::Stop(Seconds(MAX_CHECKS * TEST_STEP_S + 1));
162}
163
164/**
165 * \ingroup building-test
166 *
167 * Test suite for the buildings channel condition model
168 */
170{
171 public:
173};
174
176 : TestSuite("outdoor-random-walk-model", Type::UNIT)
177{
178 AddTestCase(new OutdoorRandomWalkTestCase, TestCase::Duration::QUICK);
179}
180
181/// Static variable for test initialization
Test case for the class OutdoorRandomWalkTestCase.
void DoRun() override
Implementation to actually run this TestCase.
std::vector< Ptr< Building > > m_buildings
Buildings.
void CheckPositionOutdoor(Ptr< RandomWalk2dOutdoorMobilityModel > model)
Check that the position is the expected one.
Test suite for the buildings channel condition model.
a 3d box
Definition: box.h:35
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Hold variables of type enum.
Definition: enum.h:62
Helper class used to assign positions and mobility models to nodes.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:522
AttributeValue implementation for Pointer.
Definition: pointer.h:48
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
2D random walk mobility model which avoids buildings.
a 2d rectangle
Definition: rectangle.h:35
AttributeValue implementation for Rectangle.
Definition: rectangle.h:125
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 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:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
AttributeValue implementation for Time.
Definition: nstime.h:1406
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:145
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.