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 * SPDX-License-Identifier: GPL-2.0-only
6 */
7
8#include "ns3/abort.h"
9#include "ns3/building-position-allocator.h"
10#include "ns3/building.h"
11#include "ns3/config.h"
12#include "ns3/double.h"
13#include "ns3/enum.h"
14#include "ns3/log.h"
15#include "ns3/mobility-helper.h"
16#include "ns3/pointer.h"
17#include "ns3/random-walk-2d-outdoor-mobility-model.h"
18#include "ns3/simulator.h"
19#include "ns3/test.h"
20
21using namespace ns3;
22
23NS_LOG_COMPONENT_DEFINE("OutdoorRandomWalkTest");
24
25/**
26 * \ingroup building-test
27 *
28 * Test case for the class OutdoorRandomWalkTestCase. It checks if the
29 * positions visited by the user are outside buildings
30 */
32{
33 public:
36
37 private:
38 void DoRun() override;
39
40 /**
41 * Check that the position is the expected one
42 * \param model Mobility model
43 */
45
46 std::vector<Ptr<Building>> m_buildings; //!< Buildings
47};
48
50 : TestCase("Test case for the BuildingsChannelConditionModel"),
51 m_buildings()
52{
53}
54
58
59void
61{
62 auto position = model->GetPosition();
63 for (auto building : m_buildings)
64 {
65 NS_TEST_ASSERT_MSG_EQ(building->IsInside(position),
66 false,
67 "Position " << position << " is inside");
68 }
69}
70
71void
73{
74 // Samples to test and time steps
75 constexpr double TEST_STEP_S = 10; // s
76 constexpr int MAX_CHECKS = 1000;
77
78 // create a grid of buildings
79 double buildingSizeX = 100; // m
80 double buildingSizeY = 50; // m
81 double streetWidth = 25; // m
82 double buildingHeight = 10; // m
83 uint32_t numBuildingsX = 20;
84 uint32_t numBuildingsY = 20;
85 double maxAxisX = (buildingSizeX + streetWidth) * numBuildingsX;
86 double maxAxisY = (buildingSizeY + streetWidth) * numBuildingsY;
87
88 for (uint32_t buildingIdX = 0; buildingIdX < numBuildingsX; ++buildingIdX)
89 {
90 for (uint32_t buildingIdY = 0; buildingIdY < numBuildingsY; ++buildingIdY)
91 {
92 Ptr<Building> building;
93 building = CreateObject<Building>();
94
95 building->SetBoundaries(Box(buildingIdX * (buildingSizeX + streetWidth),
96 buildingIdX * (buildingSizeX + streetWidth) + buildingSizeX,
97 buildingIdY * (buildingSizeY + streetWidth),
98 buildingIdY * (buildingSizeY + streetWidth) + buildingSizeY,
99 0.0,
100 buildingHeight));
101 building->SetNRoomsX(1);
102 building->SetNRoomsY(1);
103 building->SetNFloors(1);
104 m_buildings.push_back(building);
105 }
106 }
107
108 // create one node
110 nodes.Create(1);
111
112 // set the RandomWalk2dOutdoorMobilityModel mobility model
113 MobilityHelper mobility;
114 mobility.SetMobilityModel(
115 "ns3::RandomWalk2dOutdoorMobilityModel",
116 "Bounds",
117 RectangleValue(Rectangle(-streetWidth, maxAxisX, -streetWidth, maxAxisY)),
118 "Mode",
120 "Time",
121 TimeValue(Seconds(TEST_STEP_S * MAX_CHECKS)));
122 // create an OutdoorPositionAllocator and set its boundaries to match those of the mobility
123 // model
126 xPos->SetAttribute("Min", DoubleValue(-streetWidth));
127 xPos->SetAttribute("Max", DoubleValue(maxAxisX));
129 yPos->SetAttribute("Min", DoubleValue(-streetWidth));
130 yPos->SetAttribute("Max", DoubleValue(maxAxisY));
131 position->SetAttribute("X", PointerValue(xPos));
132 position->SetAttribute("Y", PointerValue(yPos));
133 mobility.SetPositionAllocator(position);
134 // install the mobility model
135 mobility.Install(nodes.Get(0));
136
137 auto mobilityModel = nodes.Get(0)->GetObject<RandomWalk2dOutdoorMobilityModel>();
138
139 // get MAX_CHECKS positions, check if they are outdoors
140 for (int i = 0; i < MAX_CHECKS; i++)
141 {
142 Simulator::Schedule(Seconds(i * TEST_STEP_S),
144 this,
145 mobilityModel);
146 }
147
148 Simulator::Stop(Seconds(MAX_CHECKS * TEST_STEP_S + 1));
151}
152
153/**
154 * \ingroup building-test
155 *
156 * Test suite for the buildings channel condition model
157 */
159{
160 public:
162};
163
165 : TestSuite("outdoor-random-walk-model", Type::UNIT)
166{
167 AddTestCase(new OutdoorRandomWalkTestCase, TestCase::Duration::QUICK);
168}
169
170/// 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:24
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
Hold variables of type enum.
Definition enum.h:52
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:511
AttributeValue implementation for Pointer.
Smart pointer class similar to boost::intrusive_ptr.
2D random walk mobility model which avoids buildings.
a 2d rectangle
Definition rectangle.h:24
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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:134
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static OutdoorRandomWalkTestSuite OutdoorRandomWalkTestSuite
Static variable for test initialization.