A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
building-position-allocator-test.cc
Go to the documentation of this file.
1
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2011, 2012 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Author: Nicola Baldo <nbaldo@cttc.es>
19
*/
20
21
22
23
#include "ns3/log.h"
24
#include "ns3/test.h"
25
#include <ns3/building-position-allocator.h>
26
#include <ns3/buildings-mobility-model.h>
27
#include <ns3/building.h>
28
#include <ns3/buildings-helper.h>
29
#include <ns3/mobility-helper.h>
30
#include <ns3/simulator.h>
31
#include <map>
32
33
NS_LOG_COMPONENT_DEFINE
(
"BuildingPositionAllocatorTest"
);
34
35
namespace
ns3 {
36
37
38
39
struct
Room
40
{
41
Room
(uint32_t xx, uint32_t yy, uint32_t zz);
42
uint32_t
x
;
43
uint32_t
y
;
44
uint32_t
z
;
45
};
46
47
Room::Room
(uint32_t xx, uint32_t yy, uint32_t zz)
48
:
x
(xx),
49
y (yy),
50
z (zz)
51
{
52
}
53
54
bool
55
operator <
(
const
Room
& a,
const
Room
& b)
56
{
57
return
( (a.
x
< b.
x
)
58
|| ( (a.
x
== b.
x
) && (a.
y
< b.
y
) )
59
|| ( (a.
x
== b.
x
) && (a.
y
== b.
y
) && (a.
z
< b.
z
) ));
60
}
61
62
63
64
class
RandomRoomPositionAllocatorTestCase
:
public
TestCase
65
{
66
public
:
67
RandomRoomPositionAllocatorTestCase
();
68
69
private
:
70
virtual
void
DoRun
(
void
);
71
72
};
73
74
75
RandomRoomPositionAllocatorTestCase::RandomRoomPositionAllocatorTestCase
()
76
:
TestCase
(
"RandomRoom, 12 rooms, 24 nodes"
)
77
{
78
}
79
80
void
81
RandomRoomPositionAllocatorTestCase::DoRun
()
82
{
83
NS_LOG_FUNCTION
(
this
);
84
85
86
87
NS_LOG_LOGIC
(
"create building"
);
88
Ptr<Building>
b = CreateObject<Building> ();
89
b->
SetBoundaries
(
Box
(1, 3, 1, 4, 1, 3));
90
b->
SetNFloors
(2);
91
b->
SetNRoomsX
(2);
92
b->
SetNRoomsY
(3);
93
94
NodeContainer
nodes;
95
nodes.
Create
(24);
96
97
MobilityHelper
mobility;
98
mobility.
SetMobilityModel
(
"ns3::BuildingsMobilityModel"
);
99
Ptr<PositionAllocator>
positionAlloc = CreateObject<RandomRoomPositionAllocator> ();
100
mobility.
SetPositionAllocator
(positionAlloc);
101
mobility.
Install
(nodes);
102
103
BuildingsHelper::MakeMobilityModelConsistent
();
104
105
std::map<Room, uint32_t> roomCounter;
106
107
for
(
NodeContainer::Iterator
it = nodes.
Begin
(); it != nodes.
End
(); ++it)
108
{
109
Ptr<MobilityModel>
mm = (*it)->
GetObject
<
MobilityModel
> ();
110
NS_ASSERT_MSG
(mm,
"no mobility model aggregated to this node"
);
111
Ptr<BuildingsMobilityModel>
bmm = DynamicCast<BuildingsMobilityModel> (mm);
112
NS_ASSERT_MSG
(bmm,
"mobility model aggregated to this node is not a BuildingsMobilityModel"
);
113
114
NS_TEST_ASSERT_MSG_EQ
(bmm->
IsIndoor
(),
true
,
"node should be indoor"
);
115
Room
r (bmm->
GetRoomNumberX
(), bmm->
GetRoomNumberY
(), bmm->
GetFloorNumber
());
116
++(roomCounter[r]);
117
118
Vector
p = bmm->
GetPosition
();
119
NS_TEST_ASSERT_MSG_GT
(p.x, bmm->
GetRoomNumberX
(),
"wrong x value"
);
120
NS_TEST_ASSERT_MSG_LT
(p.x, bmm->
GetRoomNumberX
() + 1,
"wrong x value"
);
121
NS_TEST_ASSERT_MSG_GT
(p.y, bmm->
GetRoomNumberY
(),
"wrong y value"
);
122
NS_TEST_ASSERT_MSG_LT
(p.y, bmm->
GetRoomNumberY
() + 1,
"wrong y value"
);
123
NS_TEST_ASSERT_MSG_GT
(p.z, bmm->
GetFloorNumber
(),
"wrong z value"
);
124
NS_TEST_ASSERT_MSG_LT
(p.z, bmm->
GetFloorNumber
() + 1,
"wrong z value"
);
125
126
}
127
128
for
(std::map<Room, uint32_t>::iterator it = roomCounter.begin (); it != roomCounter.end (); ++it)
129
{
130
// random selection is done without replacement until the set of
131
// eligible room is empty, at which point the set is filled
132
// again. Hence with 24 nodes and 12 rooms we expect 2 nodes per room
133
NS_TEST_ASSERT_MSG_EQ
(it->second, 2,
"expected 2 nodes per room"
);
134
}
135
136
NS_TEST_ASSERT_MSG_EQ
(roomCounter.size (), 12,
"expected 12 rooms allocated"
);
137
138
Simulator::Destroy
();
139
}
140
141
142
143
144
145
class
SameRoomPositionAllocatorTestCase
:
public
TestCase
146
{
147
public
:
148
SameRoomPositionAllocatorTestCase
();
149
150
private
:
151
virtual
void
DoRun
(
void
);
152
153
};
154
155
156
SameRoomPositionAllocatorTestCase::SameRoomPositionAllocatorTestCase
()
157
:
TestCase
(
"SameRoom 48 nodes"
)
158
{
159
}
160
161
void
162
SameRoomPositionAllocatorTestCase::DoRun
()
163
{
164
NS_LOG_FUNCTION
(
this
);
165
166
167
168
NS_LOG_LOGIC
(
"create building"
);
169
Ptr<Building>
b = CreateObject<Building> ();
170
b->
SetBoundaries
(
Box
(-10, -6, 20, 26, -1, 5));
171
b->
SetNFloors
(2);
172
b->
SetNRoomsX
(2);
173
b->
SetNRoomsY
(3);
174
175
NodeContainer
nodes;
176
nodes.
Create
(24);
177
178
MobilityHelper
mobility;
179
mobility.
SetMobilityModel
(
"ns3::BuildingsMobilityModel"
);
180
Ptr<PositionAllocator>
positionAlloc = CreateObject<RandomRoomPositionAllocator> ();
181
mobility.
SetPositionAllocator
(positionAlloc);
182
mobility.
Install
(nodes);
183
184
NodeContainer
copyNodes;
185
copyNodes.
Create
(48);
186
positionAlloc = CreateObject<SameRoomPositionAllocator> (nodes);
187
mobility.
SetPositionAllocator
(positionAlloc);
188
mobility.
Install
(copyNodes);
189
190
BuildingsHelper::MakeMobilityModelConsistent
();
191
192
std::map<Room, uint32_t> roomCounter;
193
194
for
(
NodeContainer::Iterator
it = copyNodes.
Begin
(); it != copyNodes.
End
(); ++it)
195
{
196
Ptr<MobilityModel>
mm = (*it)->
GetObject
<
MobilityModel
> ();
197
NS_ASSERT_MSG
(mm,
"no mobility model aggregated to this node"
);
198
Ptr<BuildingsMobilityModel>
bmm = DynamicCast<BuildingsMobilityModel> (mm);
199
NS_ASSERT_MSG
(bmm,
"mobility model aggregated to this node is not a BuildingsMobilityModel"
);
200
201
NS_TEST_ASSERT_MSG_EQ
(bmm->
IsIndoor
(),
true
,
"node should be indoor"
);
202
Room
r (bmm->
GetRoomNumberX
(), bmm->
GetRoomNumberY
(), bmm->
GetFloorNumber
());
203
++(roomCounter[r]);
204
}
205
206
for
(std::map<Room, uint32_t>::iterator it = roomCounter.begin (); it != roomCounter.end (); ++it)
207
{
208
209
NS_TEST_ASSERT_MSG_EQ
(it->second, 4,
"expected 4 nodes per room"
);
210
}
211
212
NS_TEST_ASSERT_MSG_EQ
(roomCounter.size (), 12,
"expected 12 rooms allocated"
);
213
214
Simulator::Destroy
();
215
}
216
217
218
219
220
221
222
class
BuildingPositionAllocatorTestSuite
:
public
TestSuite
223
{
224
public
:
225
BuildingPositionAllocatorTestSuite
();
226
};
227
228
229
BuildingPositionAllocatorTestSuite::BuildingPositionAllocatorTestSuite
()
230
:
TestSuite
(
"building-position-allocator"
, UNIT)
231
{
232
NS_LOG_FUNCTION
(
this
);
233
234
AddTestCase
(
new
RandomRoomPositionAllocatorTestCase
,
TestCase::QUICK
);
235
AddTestCase
(
new
SameRoomPositionAllocatorTestCase
,
TestCase::QUICK
);
236
237
}
238
239
static
BuildingPositionAllocatorTestSuite
buildingsPositionAllocatorTestSuiteInstance
;
240
241
}
// namespace ns3
242
src
buildings
test
building-position-allocator-test.cc
Generated on Tue May 14 2013 11:08:16 for ns-3 by
1.8.1.2