A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
building-position-allocator-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011, 2012 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Nicola Baldo <nbaldo@cttc.es>
18 */
19
20#include "ns3/log.h"
21#include "ns3/test.h"
22#include <ns3/building-position-allocator.h>
23#include <ns3/building.h>
24#include <ns3/buildings-helper.h>
25#include <ns3/constant-position-mobility-model.h>
26#include <ns3/mobility-building-info.h>
27#include <ns3/mobility-helper.h>
28#include <ns3/mobility-model.h>
29#include <ns3/simulator.h>
30
31#include <map>
32
33using namespace ns3;
34
35NS_LOG_COMPONENT_DEFINE("BuildingPositionAllocatorTest");
36
37/**
38 * \ingroup buildings
39 * \ingroup tests
40 * \defgroup building-test Buildings module tests
41 */
42
43/**
44 * \ingroup building-test
45 *
46 * Room coordinates
47 */
48struct Room
49{
50 /**
51 * Constructor
52 * \param xx X coord
53 * \param yy Y coord
54 * \param zz Z coord
55 */
56 Room(uint32_t xx, uint32_t yy, uint32_t zz);
57 uint32_t x; //!< X coord
58 uint32_t y; //!< Y coord
59 uint32_t z; //!< Z coord (floor)
60};
61
63 : x(xx),
64 y(yy),
65 z(zz)
66{
67}
68
69bool
70operator<(const Room& a, const Room& b)
71{
72 return ((a.x < b.x) || ((a.x == b.x) && (a.y < b.y)) ||
73 ((a.x == b.x) && (a.y == b.y) && (a.z < b.z)));
74}
75
76/**
77 * \ingroup building-test
78 *
79 * RandomRoomPositionAllocator test
80 */
82{
83 public:
85
86 private:
87 void DoRun() override;
88};
89
91 : TestCase("RandomRoom, 12 rooms, 24 nodes")
92{
93}
94
95void
97{
98 NS_LOG_FUNCTION(this);
99
100 NS_LOG_LOGIC("create building");
101 Ptr<Building> b = CreateObject<Building>();
102 b->SetBoundaries(Box(1, 3, 1, 4, 1, 3));
103 b->SetNFloors(2);
104 b->SetNRoomsX(2);
105 b->SetNRoomsY(3);
106
108 nodes.Create(24);
109
110 MobilityHelper mobility;
111 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
112 Ptr<PositionAllocator> positionAlloc = CreateObject<RandomRoomPositionAllocator>();
113 mobility.SetPositionAllocator(positionAlloc);
114 mobility.Install(nodes);
116
117 std::map<Room, uint32_t> roomCounter;
118
119 for (auto it = nodes.Begin(); it != nodes.End(); ++it)
120 {
121 Ptr<MobilityModel> mm = (*it)->GetObject<MobilityModel>();
122 NS_ASSERT_MSG(mm, "no mobility model aggregated to this node");
123 Ptr<MobilityBuildingInfo> bmm = mm->GetObject<MobilityBuildingInfo>();
124 NS_ASSERT_MSG(bmm,
125 "MobilityBuildingInfo has not been aggregated to this node mobility model");
126
127 NS_TEST_ASSERT_MSG_EQ(bmm->IsIndoor(), true, "node should be indoor");
128 Room r(bmm->GetRoomNumberX(), bmm->GetRoomNumberY(), bmm->GetFloorNumber());
129 ++(roomCounter[r]);
130
131 Vector p = mm->GetPosition();
132 NS_TEST_ASSERT_MSG_GT(p.x, bmm->GetRoomNumberX(), "wrong x value");
133 NS_TEST_ASSERT_MSG_LT(p.x, bmm->GetRoomNumberX() + 1, "wrong x value");
134 NS_TEST_ASSERT_MSG_GT(p.y, bmm->GetRoomNumberY(), "wrong y value");
135 NS_TEST_ASSERT_MSG_LT(p.y, bmm->GetRoomNumberY() + 1, "wrong y value");
136 NS_TEST_ASSERT_MSG_GT(p.z, bmm->GetFloorNumber(), "wrong z value");
137 NS_TEST_ASSERT_MSG_LT(p.z, bmm->GetFloorNumber() + 1, "wrong z value");
138 }
139
140 for (auto it = roomCounter.begin(); it != roomCounter.end(); ++it)
141 {
142 // random selection is done without replacement until the set of
143 // eligible room is empty, at which point the set is filled
144 // again. Hence with 24 nodes and 12 rooms we expect 2 nodes per room
145 NS_TEST_ASSERT_MSG_EQ(it->second, 2, "expected 2 nodes per room");
146 }
147
148 NS_TEST_ASSERT_MSG_EQ(roomCounter.size(), 12, "expected 12 rooms allocated");
149
151}
152
153/**
154 * \ingroup building-test
155 *
156 * SameRoomPositionAllocator test
157 */
159{
160 public:
162
163 private:
164 void DoRun() override;
165};
166
168 : TestCase("SameRoom 48 nodes")
169{
170}
171
172void
174{
175 NS_LOG_FUNCTION(this);
176
177 NS_LOG_LOGIC("create building");
178 Ptr<Building> b = CreateObject<Building>();
179 b->SetBoundaries(Box(-10, -6, 20, 26, -1, 5));
180 b->SetNFloors(2);
181 b->SetNRoomsX(2);
182 b->SetNRoomsY(3);
183
185 nodes.Create(24);
186
187 MobilityHelper mobility;
188 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
189 Ptr<PositionAllocator> positionAlloc = CreateObject<RandomRoomPositionAllocator>();
190 mobility.SetPositionAllocator(positionAlloc);
191 mobility.Install(nodes);
193
194 NodeContainer copyNodes;
195 copyNodes.Create(48);
196 positionAlloc = CreateObject<SameRoomPositionAllocator>(nodes);
197 mobility.SetPositionAllocator(positionAlloc);
198 mobility.Install(copyNodes);
199 BuildingsHelper::Install(copyNodes);
200
201 std::map<Room, uint32_t> roomCounter;
202
203 for (auto it = copyNodes.Begin(); it != copyNodes.End(); ++it)
204 {
205 Ptr<MobilityModel> mm = (*it)->GetObject<MobilityModel>();
206 NS_ASSERT_MSG(mm, "no mobility model aggregated to this node");
207 Ptr<MobilityBuildingInfo> bmm = mm->GetObject<MobilityBuildingInfo>();
208 NS_ASSERT_MSG(bmm,
209 "MobilityBuildingInfo has not been aggregated to this node mobility model");
210
211 NS_TEST_ASSERT_MSG_EQ(bmm->IsIndoor(), true, "node should be indoor");
212 Room r(bmm->GetRoomNumberX(), bmm->GetRoomNumberY(), bmm->GetFloorNumber());
213 ++(roomCounter[r]);
214 }
215
216 for (auto it = roomCounter.begin(); it != roomCounter.end(); ++it)
217 {
218 NS_TEST_ASSERT_MSG_EQ(it->second, 4, "expected 4 nodes per room");
219 }
220
221 NS_TEST_ASSERT_MSG_EQ(roomCounter.size(), 12, "expected 12 rooms allocated");
222
224}
225
226/**
227 * \ingroup building-test
228 *
229 * \brief RandomRoomPositionAllocator TestSuite
230 */
232{
233 public:
235};
236
238 : TestSuite("building-position-allocator", Type::UNIT)
239{
240 NS_LOG_FUNCTION(this);
241
242 AddTestCase(new RandomRoomPositionAllocatorTestCase, TestCase::Duration::QUICK);
243 AddTestCase(new SameRoomPositionAllocatorTestCase, TestCase::Duration::QUICK);
244}
245
246/// Static variable for test initialization
bool operator<(const Room &a, const Room &b)
static BuildingPositionAllocatorTestSuite buildingsPositionAllocatorTestSuiteInstance
Static variable for test initialization.
RandomRoomPositionAllocator TestSuite.
void DoRun() override
Implementation to actually run this TestCase.
void DoRun() override
Implementation to actually run this TestCase.
a 3d box
Definition: box.h:35
static void Install(Ptr< Node > node)
Install the MobilityBuildingInfo to a node.
mobility buildings information (to be used by mobility models)
Helper class used to assign positions and mobility models to nodes.
Keep track of the current position and velocity of an object.
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_TEST_ASSERT_MSG_LT(actual, limit, msg)
Test that an actual value is less than a limit and report and abort if not.
Definition: test.h:710
#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:145
#define NS_TEST_ASSERT_MSG_GT(actual, limit, msg)
Test that an actual value is greater than a limit and report and abort if not.
Definition: test.h:875
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Room coordinates.
uint32_t x
X coord.
uint32_t y
Y coord.
uint32_t z
Z coord (floor)
Room(uint32_t xx, uint32_t yy, uint32_t zz)
Constructor.