A Discrete-Event Network Simulator
API
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/log.h"
25 #include "ns3/mobility-helper.h"
26 #include "ns3/pointer.h"
27 #include "ns3/random-walk-2d-outdoor-mobility-model.h"
28 #include "ns3/simulator.h"
29 #include "ns3/test.h"
30 
31 using namespace ns3;
32 
33 NS_LOG_COMPONENT_DEFINE("OutdoorRandomWalkTest");
34 
43 {
44  public:
46  ~OutdoorRandomWalkTestCase() override;
47 
48  private:
49  void DoRun() override;
50 
55  void CheckPositionOutdoor(Ptr<RandomWalk2dOutdoorMobilityModel> model);
56 
57  std::vector<Ptr<Building>> m_buildings;
58 };
59 
61  : TestCase("Test case for the BuildingsChannelConditionModel"),
62  m_buildings()
63 {
64 }
65 
67 {
68 }
69 
70 void
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 
82 void
84 {
85  // create a grid of buildings
86  double buildingSizeX = 100; // m
87  double buildingSizeY = 50; // m
88  double streetWidth = 25; // m
89  double buildingHeight = 10; // m
90  uint32_t numBuildingsX = 20;
91  uint32_t numBuildingsY = 20;
92  double maxAxisX = (buildingSizeX + streetWidth) * numBuildingsX;
93  double maxAxisY = (buildingSizeY + streetWidth) * numBuildingsY;
94 
95  for (uint32_t buildingIdX = 0; buildingIdX < numBuildingsX; ++buildingIdX)
96  {
97  for (uint32_t buildingIdY = 0; buildingIdY < numBuildingsY; ++buildingIdY)
98  {
99  Ptr<Building> building;
100  building = CreateObject<Building>();
101 
102  building->SetBoundaries(Box(buildingIdX * (buildingSizeX + streetWidth),
103  buildingIdX * (buildingSizeX + streetWidth) + buildingSizeX,
104  buildingIdY * (buildingSizeY + streetWidth),
105  buildingIdY * (buildingSizeY + streetWidth) + buildingSizeY,
106  0.0,
107  buildingHeight));
108  building->SetNRoomsX(1);
109  building->SetNRoomsY(1);
110  building->SetNFloors(1);
111  m_buildings.push_back(building);
112  }
113  }
114 
115  // create one node
117  nodes.Create(1);
118 
119  // set the RandomWalk2dOutdoorMobilityModel mobility model
121  mobility.SetMobilityModel(
122  "ns3::RandomWalk2dOutdoorMobilityModel",
123  "Bounds",
124  RectangleValue(Rectangle(-streetWidth, maxAxisX, -streetWidth, maxAxisY)));
125  // create an OutdoorPositionAllocator and set its boundaries to match those of the mobility
126  // model
127  Ptr<OutdoorPositionAllocator> position = CreateObject<OutdoorPositionAllocator>();
128  Ptr<UniformRandomVariable> xPos = CreateObject<UniformRandomVariable>();
129  xPos->SetAttribute("Min", DoubleValue(-streetWidth));
130  xPos->SetAttribute("Max", DoubleValue(maxAxisX));
131  Ptr<UniformRandomVariable> yPos = CreateObject<UniformRandomVariable>();
132  yPos->SetAttribute("Min", DoubleValue(-streetWidth));
133  yPos->SetAttribute("Max", DoubleValue(maxAxisY));
134  position->SetAttribute("X", PointerValue(xPos));
135  position->SetAttribute("Y", PointerValue(yPos));
136  mobility.SetPositionAllocator(position);
137  // install the mobility model
138  mobility.Install(nodes.Get(0));
139 
140  auto mobilityModel = nodes.Get(0)->GetObject<RandomWalk2dOutdoorMobilityModel>();
141 
142  // get maxChecks positions, check if they are outdoors
143  double testStep = 10; // s
144  int maxChecks = 1000;
145  for (int i = 0; i < maxChecks; ++i)
146  {
147  Simulator::Schedule(Seconds(i * testStep),
149  this,
150  mobilityModel);
151  }
152 
153  Simulator::Stop(Seconds(maxChecks * testStep + 1));
154  Simulator::Run();
155  Simulator::Destroy();
156 }
157 
165 {
166  public:
168 };
169 
171  : TestSuite("outdoor-random-walk-model", UNIT)
172 {
173  AddTestCase(new OutdoorRandomWalkTestCase, TestCase::QUICK);
174 }
175 
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
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.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:200
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
2D random walk mobility model which avoids buildings.
a 2d rectangle
Definition: rectangle.h:35
AttributeValue implementation for Rectangle.
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
#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:144
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
mobility
Definition: third.py:96
static OutdoorRandomWalkTestSuite OutdoorRandomWalkTestSuite
Static variable for test initialization.