A Discrete-Event Network Simulator
API
outdoor-random-walk-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2019 SIGNET Lab, Department of Information Engineering,
4  * University of Padova
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #include "ns3/abort.h"
21 #include "ns3/test.h"
22 #include "ns3/config.h"
23 #include "ns3/building.h"
24 #include "ns3/building-position-allocator.h"
25 #include "ns3/random-walk-2d-outdoor-mobility-model.h"
26 #include "ns3/mobility-helper.h"
27 #include "ns3/log.h"
28 #include "ns3/simulator.h"
29 #include "ns3/double.h"
30 #include "ns3/pointer.h"
31 
32 using namespace ns3;
33 
34 NS_LOG_COMPONENT_DEFINE ("OutdoorRandomWalkTest");
35 
41 {
42 public:
47 
51  virtual ~OutdoorRandomWalkTestCase ();
52 
53 private:
57  virtual void DoRun (void);
58 
59  void CheckPositionOutdoor (Ptr<RandomWalk2dOutdoorMobilityModel> model);
60 
61  std::vector<Ptr<Building> > m_buildings;
62 };
63 
65  : TestCase ("Test case for the BuildingsChannelConditionModel"), m_buildings ()
66 {}
67 
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), false, "Position " << position << " is inside");
78  }
79 }
80 
81 void
83 {
84  // create a grid of buildings
85  double buildingSizeX = 100; // m
86  double buildingSizeY = 50; // m
87  double streetWidth = 25; // m
88  double buildingHeight = 10; // m
89  uint32_t numBuildingsX = 20;
90  uint32_t numBuildingsY = 20;
91  double maxAxisX = (buildingSizeX + streetWidth) * numBuildingsX;
92  double maxAxisY = (buildingSizeY + streetWidth) * numBuildingsY;
93 
94  for (uint32_t buildingIdX = 0; buildingIdX < numBuildingsX; ++buildingIdX)
95  {
96  for (uint32_t buildingIdY = 0; buildingIdY < numBuildingsY; ++buildingIdY)
97  {
98  Ptr < Building > building;
99  building = CreateObject<Building> ();
100 
101  building->SetBoundaries (Box (buildingIdX * (buildingSizeX + streetWidth),
102  buildingIdX * (buildingSizeX + streetWidth) + buildingSizeX,
103  buildingIdY * (buildingSizeY + streetWidth),
104  buildingIdY * (buildingSizeY + streetWidth) + buildingSizeY,
105  0.0, buildingHeight));
106  building->SetNRoomsX (1);
107  building->SetNRoomsY (1);
108  building->SetNFloors (1);
109  m_buildings.push_back (building);
110  }
111  }
112 
113  // create one node
115  nodes.Create (1);
116 
117  // set the RandomWalk2dOutdoorMobilityModel mobility model
119  mobility.SetMobilityModel ("ns3::RandomWalk2dOutdoorMobilityModel",
120  "Bounds", RectangleValue (
121  Rectangle (-streetWidth, maxAxisX, -streetWidth, maxAxisY)));
122  // create an OutdoorPositionAllocator and set its boundaries to match those of the mobility model
123  Ptr<OutdoorPositionAllocator> position = CreateObject<OutdoorPositionAllocator> ();
124  Ptr<UniformRandomVariable> xPos = CreateObject<UniformRandomVariable>();
125  xPos->SetAttribute ("Min", DoubleValue (-streetWidth));
126  xPos->SetAttribute ("Max", DoubleValue (maxAxisX));
127  Ptr<UniformRandomVariable> yPos = CreateObject<UniformRandomVariable>();
128  yPos->SetAttribute ("Min", DoubleValue (-streetWidth));
129  yPos->SetAttribute ("Max", DoubleValue (maxAxisY));
130  position->SetAttribute ("X", PointerValue (xPos));
131  position->SetAttribute ("Y", PointerValue (yPos));
132  mobility.SetPositionAllocator (position);
133  // install the mobility model
134  mobility.Install (nodes.Get (0));
135 
136  auto mobilityModel = nodes.Get (0)->GetObject<RandomWalk2dOutdoorMobilityModel>();
137 
138  // get maxChecks positions, check if they are outdoors
139  double testStep = 10; // s
140  int maxChecks = 1000;
141  for (int i = 0; i < maxChecks; ++i)
142  {
143  Simulator::Schedule (Seconds (i * testStep),
145  }
146 
147  Simulator::Stop (Seconds (maxChecks * testStep + 1));
148  Simulator::Run ();
149  Simulator::Destroy ();
150 }
151 
156 {
157 public:
159 };
160 
162  : TestSuite ("outdoor-random-walk-model", UNIT)
163 {
164  AddTestCase (new OutdoorRandomWalkTestCase, TestCase::QUICK);
165 }
166 
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
void SetNRoomsY(uint16_t nroomy)
Definition: building.cc:174
A suite of tests to run.
Definition: test.h:1343
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
Test suite for the buildings channel condition model.
encapsulates test code
Definition: test.h:1153
a 3d box
Definition: box.h:34
mobility
Definition: third.py:108
AttributeValue implementation for Rectangle.
Definition: rectangle.h:97
nodes
Definition: first.py:32
virtual void DoRun(void)
Builds the simulation scenario and perform the tests.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
void CheckPositionOutdoor(Ptr< RandomWalk2dOutdoorMobilityModel > model)
void SetNFloors(uint16_t nfloors)
Definition: building.cc:160
#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:166
2D random walk mobility model which avoids buildings.
Test case for the class OutdoorRandomWalkTestCase.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
std::vector< Ptr< Building > > m_buildings
Vector GetPosition(void) const
static OutdoorRandomWalkTestSuite OutdoorRandomWalkTestSuite
Helper class used to assign positions and mobility models to nodes.
virtual ~OutdoorRandomWalkTestCase()
Destructor.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1278
void SetNRoomsX(uint16_t nroomx)
Definition: building.cc:167
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
void SetBoundaries(Box box)
Set the boundaries of the building.
Definition: building.cc:139
a 2d rectangle
Definition: rectangle.h:34