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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Nicola Baldo <nbaldo@cttc.es>
7 */
8
9#include "ns3/log.h"
10#include "ns3/test.h"
11#include <ns3/building-position-allocator.h>
12#include <ns3/building.h>
13#include <ns3/buildings-helper.h>
14#include <ns3/constant-position-mobility-model.h>
15#include <ns3/mobility-building-info.h>
16#include <ns3/mobility-helper.h>
17#include <ns3/mobility-model.h>
18#include <ns3/simulator.h>
19
20#include <map>
21
22using namespace ns3;
23
24NS_LOG_COMPONENT_DEFINE("BuildingPositionAllocatorTest");
25
26/**
27 * @ingroup buildings
28 * @ingroup tests
29 * @defgroup building-test Buildings module tests
30 */
31
32/**
33 * @ingroup building-test
34 *
35 * Room coordinates
36 */
37struct Room
38{
39 /**
40 * Constructor
41 * @param xx X coord
42 * @param yy Y coord
43 * @param zz Z coord
44 */
45 Room(uint32_t xx, uint32_t yy, uint32_t zz);
46 uint32_t x; //!< X coord
47 uint32_t y; //!< Y coord
48 uint32_t z; //!< Z coord (floor)
49};
50
52 : x(xx),
53 y(yy),
54 z(zz)
55{
56}
57
58bool
59operator<(const Room& a, const Room& b)
60{
61 return ((a.x < b.x) || ((a.x == b.x) && (a.y < b.y)) ||
62 ((a.x == b.x) && (a.y == b.y) && (a.z < b.z)));
63}
64
65/**
66 * @ingroup building-test
67 *
68 * RandomRoomPositionAllocator test
69 */
71{
72 public:
74
75 private:
76 void DoRun() override;
77};
78
83
84void
86{
87 NS_LOG_FUNCTION(this);
88
89 NS_LOG_LOGIC("create building");
91 b->SetBoundaries(Box(1, 3, 1, 4, 1, 3));
92 b->SetNFloors(2);
93 b->SetNRoomsX(2);
94 b->SetNRoomsY(3);
95
97 nodes.Create(24);
98
99 MobilityHelper mobility;
100 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
102 mobility.SetPositionAllocator(positionAlloc);
103 mobility.Install(nodes);
105
106 std::map<Room, uint32_t> roomCounter;
107
108 for (auto it = nodes.Begin(); it != nodes.End(); ++it)
109 {
110 Ptr<MobilityModel> mm = (*it)->GetObject<MobilityModel>();
111 NS_ASSERT_MSG(mm, "no mobility model aggregated to this node");
112 Ptr<MobilityBuildingInfo> bmm = mm->GetObject<MobilityBuildingInfo>();
113 NS_ASSERT_MSG(bmm,
114 "MobilityBuildingInfo has not been aggregated to this node mobility model");
115
116 NS_TEST_ASSERT_MSG_EQ(bmm->IsIndoor(), true, "node should be indoor");
117 Room r(bmm->GetRoomNumberX(), bmm->GetRoomNumberY(), bmm->GetFloorNumber());
118 ++(roomCounter[r]);
119
120 Vector p = mm->GetPosition();
121 NS_TEST_ASSERT_MSG_GT(p.x, bmm->GetRoomNumberX(), "wrong x value");
122 NS_TEST_ASSERT_MSG_LT(p.x, bmm->GetRoomNumberX() + 1, "wrong x value");
123 NS_TEST_ASSERT_MSG_GT(p.y, bmm->GetRoomNumberY(), "wrong y value");
124 NS_TEST_ASSERT_MSG_LT(p.y, bmm->GetRoomNumberY() + 1, "wrong y value");
125 NS_TEST_ASSERT_MSG_GT(p.z, bmm->GetFloorNumber(), "wrong z value");
126 NS_TEST_ASSERT_MSG_LT(p.z, bmm->GetFloorNumber() + 1, "wrong z value");
127 }
128
129 for (auto it = roomCounter.begin(); it != roomCounter.end(); ++it)
130 {
131 // random selection is done without replacement until the set of
132 // eligible room is empty, at which point the set is filled
133 // again. Hence with 24 nodes and 12 rooms we expect 2 nodes per room
134 NS_TEST_ASSERT_MSG_EQ(it->second, 2, "expected 2 nodes per room");
135 }
136
137 NS_TEST_ASSERT_MSG_EQ(roomCounter.size(), 12, "expected 12 rooms allocated");
138
140}
141
142/**
143 * @ingroup building-test
144 *
145 * SameRoomPositionAllocator test
146 */
148{
149 public:
151
152 private:
153 void DoRun() override;
154};
155
160
161void
163{
164 NS_LOG_FUNCTION(this);
165
166 NS_LOG_LOGIC("create building");
168 b->SetBoundaries(Box(-10, -6, 20, 26, -1, 5));
169 b->SetNFloors(2);
170 b->SetNRoomsX(2);
171 b->SetNRoomsY(3);
172
174 nodes.Create(24);
175
176 MobilityHelper mobility;
177 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
179 mobility.SetPositionAllocator(positionAlloc);
180 mobility.Install(nodes);
182
183 NodeContainer copyNodes;
184 copyNodes.Create(48);
186 mobility.SetPositionAllocator(positionAlloc);
187 mobility.Install(copyNodes);
188 BuildingsHelper::Install(copyNodes);
189
190 std::map<Room, uint32_t> roomCounter;
191
192 for (auto it = copyNodes.Begin(); it != copyNodes.End(); ++it)
193 {
194 Ptr<MobilityModel> mm = (*it)->GetObject<MobilityModel>();
195 NS_ASSERT_MSG(mm, "no mobility model aggregated to this node");
196 Ptr<MobilityBuildingInfo> bmm = mm->GetObject<MobilityBuildingInfo>();
197 NS_ASSERT_MSG(bmm,
198 "MobilityBuildingInfo has not been aggregated to this node mobility model");
199
200 NS_TEST_ASSERT_MSG_EQ(bmm->IsIndoor(), true, "node should be indoor");
201 Room r(bmm->GetRoomNumberX(), bmm->GetRoomNumberY(), bmm->GetFloorNumber());
202 ++(roomCounter[r]);
203 }
204
205 for (auto it = roomCounter.begin(); it != roomCounter.end(); ++it)
206 {
207 NS_TEST_ASSERT_MSG_EQ(it->second, 4, "expected 4 nodes per room");
208 }
209
210 NS_TEST_ASSERT_MSG_EQ(roomCounter.size(), 12, "expected 12 rooms allocated");
211
213}
214
215/**
216 * @ingroup building-test
217 *
218 * @brief RandomRoomPositionAllocator TestSuite
219 */
225
227 : TestSuite("building-position-allocator", Type::UNIT)
228{
229 NS_LOG_FUNCTION(this);
230
231 AddTestCase(new RandomRoomPositionAllocatorTestCase, TestCase::Duration::QUICK);
232 AddTestCase(new SameRoomPositionAllocatorTestCase, TestCase::Duration::QUICK);
233}
234
235/// 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:24
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.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
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_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:75
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
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_LT(actual, limit, msg)
Test that an actual value is less than a limit and report and abort if not.
Definition test.h:699
#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
#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:864
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Room coordinates.
uint32_t z
Z coord (floor)
Room(uint32_t xx, uint32_t yy, uint32_t zz)
Constructor.