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
32using namespace ns3;
33
34NS_LOG_COMPONENT_DEFINE ("OutdoorRandomWalkTest");
35
44{
45public:
48
49private:
50 virtual void DoRun (void);
51
57
58 std::vector<Ptr<Building> > m_buildings;
59};
60
62 : TestCase ("Test case for the BuildingsChannelConditionModel"), m_buildings ()
63{}
64
66{}
67
68void
70{
71 auto position = model->GetPosition ();
72 for (auto building : m_buildings)
73 {
74 NS_TEST_ASSERT_MSG_EQ (building->IsInside (position), false, "Position " << position << " is inside");
75 }
76}
77
78void
80{
81 // create a grid of buildings
82 double buildingSizeX = 100; // m
83 double buildingSizeY = 50; // m
84 double streetWidth = 25; // m
85 double buildingHeight = 10; // m
86 uint32_t numBuildingsX = 20;
87 uint32_t numBuildingsY = 20;
88 double maxAxisX = (buildingSizeX + streetWidth) * numBuildingsX;
89 double maxAxisY = (buildingSizeY + streetWidth) * numBuildingsY;
90
91 for (uint32_t buildingIdX = 0; buildingIdX < numBuildingsX; ++buildingIdX)
92 {
93 for (uint32_t buildingIdY = 0; buildingIdY < numBuildingsY; ++buildingIdY)
94 {
95 Ptr < Building > building;
96 building = CreateObject<Building> ();
97
98 building->SetBoundaries (Box (buildingIdX * (buildingSizeX + streetWidth),
99 buildingIdX * (buildingSizeX + streetWidth) + buildingSizeX,
100 buildingIdY * (buildingSizeY + streetWidth),
101 buildingIdY * (buildingSizeY + streetWidth) + buildingSizeY,
102 0.0, buildingHeight));
103 building->SetNRoomsX (1);
104 building->SetNRoomsY (1);
105 building->SetNFloors (1);
106 m_buildings.push_back (building);
107 }
108 }
109
110 // create one node
112 nodes.Create (1);
113
114 // set the RandomWalk2dOutdoorMobilityModel mobility model
116 mobility.SetMobilityModel ("ns3::RandomWalk2dOutdoorMobilityModel",
117 "Bounds", RectangleValue (
118 Rectangle (-streetWidth, maxAxisX, -streetWidth, maxAxisY)));
119 // create an OutdoorPositionAllocator and set its boundaries to match those of the mobility model
120 Ptr<OutdoorPositionAllocator> position = CreateObject<OutdoorPositionAllocator> ();
121 Ptr<UniformRandomVariable> xPos = CreateObject<UniformRandomVariable>();
122 xPos->SetAttribute ("Min", DoubleValue (-streetWidth));
123 xPos->SetAttribute ("Max", DoubleValue (maxAxisX));
124 Ptr<UniformRandomVariable> yPos = CreateObject<UniformRandomVariable>();
125 yPos->SetAttribute ("Min", DoubleValue (-streetWidth));
126 yPos->SetAttribute ("Max", DoubleValue (maxAxisY));
127 position->SetAttribute ("X", PointerValue (xPos));
128 position->SetAttribute ("Y", PointerValue (yPos));
129 mobility.SetPositionAllocator (position);
130 // install the mobility model
131 mobility.Install (nodes.Get (0));
132
133 auto mobilityModel = nodes.Get (0)->GetObject<RandomWalk2dOutdoorMobilityModel>();
134
135 // get maxChecks positions, check if they are outdoors
136 double testStep = 10; // s
137 int maxChecks = 1000;
138 for (int i = 0; i < maxChecks; ++i)
139 {
140 Simulator::Schedule (Seconds (i * testStep),
142 }
143
144 Simulator::Stop (Seconds (maxChecks * testStep + 1));
145 Simulator::Run ();
146 Simulator::Destroy ();
147}
148
156{
157public:
159};
160
162 : TestSuite ("outdoor-random-walk-model", UNIT)
163{
164 AddTestCase (new OutdoorRandomWalkTestCase, TestCase::QUICK);
165}
166
Test case for the class OutdoorRandomWalkTestCase.
std::vector< Ptr< Building > > m_buildings
Buildings.
virtual void DoRun(void)
Implementation to actually run this TestCase.
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:41
Helper class used to assign positions and mobility models to nodes.
keep track of a set of node pointers.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:256
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
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:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:141
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
nodes
Definition: first.py:32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
mobility
Definition: third.py:107
static OutdoorRandomWalkTestSuite OutdoorRandomWalkTestSuite
Static variable for test initialization.