A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
32 using namespace ns3;
33 
34 NS_LOG_COMPONENT_DEFINE("OutdoorRandomWalkTest");
35 
44 {
45  public:
47  ~OutdoorRandomWalkTestCase() override;
48 
49  private:
50  void DoRun() override;
51 
56  void CheckPositionOutdoor(Ptr<RandomWalk2dOutdoorMobilityModel> model);
57 
58  std::vector<Ptr<Building>> m_buildings;
59 };
60 
62  : TestCase("Test case for the BuildingsChannelConditionModel"),
63  m_buildings()
64 {
65 }
66 
68 {
69 }
70 
71 void
73 {
74  auto position = model->GetPosition();
75  for (auto building : m_buildings)
76  {
77  NS_TEST_ASSERT_MSG_EQ(building->IsInside(position),
78  false,
79  "Position " << position << " is inside");
80  }
81 }
82 
83 void
85 {
86  // Samples to test and time steps
87  constexpr double TEST_STEP_S = 10; // s
88  constexpr int MAX_CHECKS = 1000;
89 
90  // create a grid of buildings
91  double buildingSizeX = 100; // m
92  double buildingSizeY = 50; // m
93  double streetWidth = 25; // m
94  double buildingHeight = 10; // m
95  uint32_t numBuildingsX = 20;
96  uint32_t numBuildingsY = 20;
97  double maxAxisX = (buildingSizeX + streetWidth) * numBuildingsX;
98  double maxAxisY = (buildingSizeY + streetWidth) * numBuildingsY;
99 
100  for (uint32_t buildingIdX = 0; buildingIdX < numBuildingsX; ++buildingIdX)
101  {
102  for (uint32_t buildingIdY = 0; buildingIdY < numBuildingsY; ++buildingIdY)
103  {
104  Ptr<Building> building;
105  building = CreateObject<Building>();
106 
107  building->SetBoundaries(Box(buildingIdX * (buildingSizeX + streetWidth),
108  buildingIdX * (buildingSizeX + streetWidth) + buildingSizeX,
109  buildingIdY * (buildingSizeY + streetWidth),
110  buildingIdY * (buildingSizeY + streetWidth) + buildingSizeY,
111  0.0,
112  buildingHeight));
113  building->SetNRoomsX(1);
114  building->SetNRoomsY(1);
115  building->SetNFloors(1);
116  m_buildings.push_back(building);
117  }
118  }
119 
120  // create one node
122  nodes.Create(1);
123 
124  // set the RandomWalk2dOutdoorMobilityModel mobility model
126  mobility.SetMobilityModel(
127  "ns3::RandomWalk2dOutdoorMobilityModel",
128  "Bounds",
129  RectangleValue(Rectangle(-streetWidth, maxAxisX, -streetWidth, maxAxisY)),
130  "Mode",
131  EnumValue(RandomWalk2dOutdoorMobilityModel::MODE_TIME),
132  "Time",
133  TimeValue(Seconds(TEST_STEP_S * MAX_CHECKS)));
134  // create an OutdoorPositionAllocator and set its boundaries to match those of the mobility
135  // model
136  Ptr<OutdoorPositionAllocator> position = CreateObject<OutdoorPositionAllocator>();
137  Ptr<UniformRandomVariable> xPos = CreateObject<UniformRandomVariable>();
138  xPos->SetAttribute("Min", DoubleValue(-streetWidth));
139  xPos->SetAttribute("Max", DoubleValue(maxAxisX));
140  Ptr<UniformRandomVariable> yPos = CreateObject<UniformRandomVariable>();
141  yPos->SetAttribute("Min", DoubleValue(-streetWidth));
142  yPos->SetAttribute("Max", DoubleValue(maxAxisY));
143  position->SetAttribute("X", PointerValue(xPos));
144  position->SetAttribute("Y", PointerValue(yPos));
145  mobility.SetPositionAllocator(position);
146  // install the mobility model
147  mobility.Install(nodes.Get(0));
148 
149  auto mobilityModel = nodes.Get(0)->GetObject<RandomWalk2dOutdoorMobilityModel>();
150 
151  // get MAX_CHECKS positions, check if they are outdoors
152  for (int i = 0; i < MAX_CHECKS; i++)
153  {
154  Simulator::Schedule(Seconds(i * TEST_STEP_S),
156  this,
157  mobilityModel);
158  }
159 
160  Simulator::Stop(Seconds(MAX_CHECKS * TEST_STEP_S + 1));
161  Simulator::Run();
162  Simulator::Destroy();
163 }
164 
172 {
173  public:
175 };
176 
178  : TestSuite("outdoor-random-walk-model", UNIT)
179 {
180  AddTestCase(new OutdoorRandomWalkTestCase, TestCase::QUICK);
181 }
182 
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.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:204
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:77
2D random walk mobility model which avoids buildings.
a 2d rectangle
Definition: rectangle.h:35
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:301
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:1326
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
mobility
Definition: third.py:105
static OutdoorRandomWalkTestSuite OutdoorRandomWalkTestSuite
Static variable for test initialization.