A Discrete-Event Network Simulator
API
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/mobility-building-info.h>
27 #include <ns3/constant-position-mobility-model.h>
28 #include <ns3/mobility-model.h>
29 #include <ns3/building.h>
30 #include <ns3/buildings-helper.h>
31 #include <ns3/mobility-helper.h>
32 #include <ns3/simulator.h>
33 #include <map>
34 
35 using namespace ns3;
36 
37 NS_LOG_COMPONENT_DEFINE ("BuildingPositionAllocatorTest");
38 
50 struct Room
51 {
58  Room (uint32_t xx, uint32_t yy, uint32_t zz);
59  uint32_t x;
60  uint32_t y;
61  uint32_t z;
62 };
63 
64 Room::Room (uint32_t xx, uint32_t yy, uint32_t zz)
65  : x (xx),
66  y (yy),
67  z (zz)
68 {
69 }
70 
71 bool
72 operator < (const Room& a, const Room& b)
73 {
74  return ( (a.x < b.x)
75  || ( (a.x == b.x) && (a.y < b.y) )
76  || ( (a.x == b.x) && (a.y == b.y) && (a.z < b.z) ));
77 }
78 
79 
87 {
88 public:
90 
91 private:
92  virtual void DoRun (void);
93 
94 };
95 
96 
98  : TestCase ("RandomRoom, 12 rooms, 24 nodes")
99 {
100 }
101 
102 void
104 {
105  NS_LOG_FUNCTION (this);
106 
107 
108 
109  NS_LOG_LOGIC ("create building");
110  Ptr<Building> b = CreateObject<Building> ();
111  b->SetBoundaries (Box (1, 3, 1, 4, 1, 3));
112  b->SetNFloors (2);
113  b->SetNRoomsX (2);
114  b->SetNRoomsY (3);
115 
117  nodes.Create (24);
118 
120  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
121  Ptr<PositionAllocator> positionAlloc = CreateObject<RandomRoomPositionAllocator> ();
122  mobility.SetPositionAllocator (positionAlloc);
123  mobility.Install (nodes);
124  BuildingsHelper::Install (nodes);
125 
126  std::map<Room, uint32_t> roomCounter;
127 
128  for (NodeContainer::Iterator it = nodes.Begin (); it != nodes.End (); ++it)
129  {
131  NS_ASSERT_MSG (mm, "no mobility model aggregated to this node");
133  NS_ASSERT_MSG (bmm, "MobilityBuildingInfo has not been aggregated to this node mobility model");
134 
135  NS_TEST_ASSERT_MSG_EQ (bmm->IsIndoor (), true, "node should be indoor");
136  Room r (bmm->GetRoomNumberX (), bmm->GetRoomNumberY (), bmm->GetFloorNumber ());
137  ++(roomCounter[r]);
138 
139  Vector p = mm->GetPosition ();
140  NS_TEST_ASSERT_MSG_GT (p.x, bmm->GetRoomNumberX (), "wrong x value");
141  NS_TEST_ASSERT_MSG_LT (p.x, bmm->GetRoomNumberX () + 1, "wrong x value");
142  NS_TEST_ASSERT_MSG_GT (p.y, bmm->GetRoomNumberY (), "wrong y value");
143  NS_TEST_ASSERT_MSG_LT (p.y, bmm->GetRoomNumberY () + 1, "wrong y value");
144  NS_TEST_ASSERT_MSG_GT (p.z, bmm->GetFloorNumber (), "wrong z value");
145  NS_TEST_ASSERT_MSG_LT (p.z, bmm->GetFloorNumber () + 1, "wrong z value");
146 
147  }
148 
149  for (std::map<Room, uint32_t>::iterator it = roomCounter.begin (); it != roomCounter.end (); ++it)
150  {
151  // random selection is done without replacement until the set of
152  // eligible room is empty, at which point the set is filled
153  // again. Hence with 24 nodes and 12 rooms we expect 2 nodes per room
154  NS_TEST_ASSERT_MSG_EQ (it->second, 2, "expected 2 nodes per room");
155  }
156 
157  NS_TEST_ASSERT_MSG_EQ (roomCounter.size (), 12, "expected 12 rooms allocated");
158 
159  Simulator::Destroy ();
160 }
161 
162 
163 
171 {
172 public:
174 
175 private:
176  virtual void DoRun (void);
177 
178 };
179 
180 
182  : TestCase ("SameRoom 48 nodes")
183 {
184 }
185 
186 void
188 {
189  NS_LOG_FUNCTION (this);
190 
191 
192 
193  NS_LOG_LOGIC ("create building");
194  Ptr<Building> b = CreateObject<Building> ();
195  b->SetBoundaries (Box (-10, -6, 20, 26, -1, 5));
196  b->SetNFloors (2);
197  b->SetNRoomsX (2);
198  b->SetNRoomsY (3);
199 
201  nodes.Create (24);
202 
204  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
205  Ptr<PositionAllocator> positionAlloc = CreateObject<RandomRoomPositionAllocator> ();
206  mobility.SetPositionAllocator (positionAlloc);
207  mobility.Install (nodes);
208  BuildingsHelper::Install (nodes);
209 
210  NodeContainer copyNodes;
211  copyNodes.Create (48);
212  positionAlloc = CreateObject<SameRoomPositionAllocator> (nodes);
213  mobility.SetPositionAllocator (positionAlloc);
214  mobility.Install (copyNodes);
215  BuildingsHelper::Install (copyNodes);
216 
217  std::map<Room, uint32_t> roomCounter;
218 
219  for (NodeContainer::Iterator it = copyNodes.Begin (); it != copyNodes.End (); ++it)
220  {
222  NS_ASSERT_MSG (mm, "no mobility model aggregated to this node");
224  NS_ASSERT_MSG (bmm, "MobilityBuildingInfo has not been aggregated to this node mobility model");
225 
226  NS_TEST_ASSERT_MSG_EQ (bmm->IsIndoor (), true, "node should be indoor");
227  Room r (bmm->GetRoomNumberX (), bmm->GetRoomNumberY (), bmm->GetFloorNumber ());
228  ++(roomCounter[r]);
229  }
230 
231  for (std::map<Room, uint32_t>::iterator it = roomCounter.begin (); it != roomCounter.end (); ++it)
232  {
233 
234  NS_TEST_ASSERT_MSG_EQ (it->second, 4, "expected 4 nodes per room");
235  }
236 
237  NS_TEST_ASSERT_MSG_EQ (roomCounter.size (), 12, "expected 12 rooms allocated");
238 
239  Simulator::Destroy ();
240 }
241 
242 
250 {
251 public:
253 };
254 
256  : TestSuite ("building-position-allocator", UNIT)
257 {
258  NS_LOG_FUNCTION (this);
259 
260  AddTestCase (new RandomRoomPositionAllocatorTestCase, TestCase::QUICK);
261  AddTestCase (new SameRoomPositionAllocatorTestCase, TestCase::QUICK);
262 
263 }
264 
bool operator<(const Room &a, const Room &b)
static BuildingPositionAllocatorTestSuite buildingsPositionAllocatorTestSuiteInstance
Static variable for test initialization.
RandomRoomPositionAllocator TestSuite.
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoRun(void)
Implementation to actually run this TestCase.
a 3d box
Definition: box.h:35
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.
Vector GetPosition(void) const
keep track of a set of node pointers.
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
Iterator End(void) 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.
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
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_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:88
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#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:675
#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
#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:825
nodes
Definition: first.py:32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
list x
Random number samples.
mobility
Definition: third.py:108
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.