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 
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 
65 {
66 public:
68 
69 private:
70  virtual void DoRun (void);
71 
72 };
73 
74 
76  : TestCase ("RandomRoom, 12 rooms, 24 nodes")
77 {
78 }
79 
80 void
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 
95  nodes.Create (24);
96 
98  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
99  Ptr<PositionAllocator> positionAlloc = CreateObject<RandomRoomPositionAllocator> ();
100  mobility.SetPositionAllocator (positionAlloc);
101  mobility.Install (nodes);
102  BuildingsHelper::Install (nodes);
103 
104  BuildingsHelper::MakeMobilityModelConsistent ();
105 
106  std::map<Room, uint32_t> roomCounter;
107 
108  for (NodeContainer::Iterator it = nodes.Begin (); it != nodes.End (); ++it)
109  {
111  NS_ASSERT_MSG (mm, "no mobility model aggregated to this node");
113  NS_ASSERT_MSG (bmm, "MobilityBuildingInfo has not been aggregated to this node mobility model");
114 
115  NS_TEST_ASSERT_MSG_EQ (bmm->IsIndoor (), true, "node should be indoor");
116  Room r (bmm->GetRoomNumberX (), bmm->GetRoomNumberY (), bmm->GetFloorNumber ());
117  ++(roomCounter[r]);
118 
119  Vector p = mm->GetPosition ();
120  NS_TEST_ASSERT_MSG_GT (p.x, bmm->GetRoomNumberX (), "wrong x value");
121  NS_TEST_ASSERT_MSG_LT (p.x, bmm->GetRoomNumberX () + 1, "wrong x value");
122  NS_TEST_ASSERT_MSG_GT (p.y, bmm->GetRoomNumberY (), "wrong y value");
123  NS_TEST_ASSERT_MSG_LT (p.y, bmm->GetRoomNumberY () + 1, "wrong y value");
124  NS_TEST_ASSERT_MSG_GT (p.z, bmm->GetFloorNumber (), "wrong z value");
125  NS_TEST_ASSERT_MSG_LT (p.z, bmm->GetFloorNumber () + 1, "wrong z value");
126 
127  }
128 
129  for (std::map<Room, uint32_t>::iterator 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 
139  Simulator::Destroy ();
140 }
141 
142 
143 
144 
145 
147 {
148 public:
150 
151 private:
152  virtual void DoRun (void);
153 
154 };
155 
156 
158  : TestCase ("SameRoom 48 nodes")
159 {
160 }
161 
162 void
164 {
165  NS_LOG_FUNCTION (this);
166 
167 
168 
169  NS_LOG_LOGIC ("create building");
170  Ptr<Building> b = CreateObject<Building> ();
171  b->SetBoundaries (Box (-10, -6, 20, 26, -1, 5));
172  b->SetNFloors (2);
173  b->SetNRoomsX (2);
174  b->SetNRoomsY (3);
175 
177  nodes.Create (24);
178 
180  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
181  Ptr<PositionAllocator> positionAlloc = CreateObject<RandomRoomPositionAllocator> ();
182  mobility.SetPositionAllocator (positionAlloc);
183  mobility.Install (nodes);
184  BuildingsHelper::Install (nodes);
185 
186  NodeContainer copyNodes;
187  copyNodes.Create (48);
188  positionAlloc = CreateObject<SameRoomPositionAllocator> (nodes);
189  mobility.SetPositionAllocator (positionAlloc);
190  mobility.Install (copyNodes);
191  BuildingsHelper::Install (copyNodes);
192 
193  BuildingsHelper::MakeMobilityModelConsistent ();
194 
195  std::map<Room, uint32_t> roomCounter;
196 
197  for (NodeContainer::Iterator it = copyNodes.Begin (); it != copyNodes.End (); ++it)
198  {
200  NS_ASSERT_MSG (mm, "no mobility model aggregated to this node");
202  NS_ASSERT_MSG (bmm, "MobilityBuildingInfo has not been aggregated to this node mobility model");
203 
204  NS_TEST_ASSERT_MSG_EQ (bmm->IsIndoor (), true, "node should be indoor");
205  Room r (bmm->GetRoomNumberX (), bmm->GetRoomNumberY (), bmm->GetFloorNumber ());
206  ++(roomCounter[r]);
207  }
208 
209  for (std::map<Room, uint32_t>::iterator it = roomCounter.begin (); it != roomCounter.end (); ++it)
210  {
211 
212  NS_TEST_ASSERT_MSG_EQ (it->second, 4, "expected 4 nodes per room");
213  }
214 
215  NS_TEST_ASSERT_MSG_EQ (roomCounter.size (), 12, "expected 12 rooms allocated");
216 
217  Simulator::Destroy ();
218 }
219 
220 
221 
222 
223 
224 
226 {
227 public:
229 };
230 
231 
233  : TestSuite ("building-position-allocator", UNIT)
234 {
235  NS_LOG_FUNCTION (this);
236 
237  AddTestCase (new RandomRoomPositionAllocatorTestCase, TestCase::QUICK);
238  AddTestCase (new SameRoomPositionAllocatorTestCase, TestCase::QUICK);
239 
240 }
241 
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void SetNRoomsY(uint16_t nroomy)
Definition: building.cc:174
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:459
A suite of tests to run.
Definition: test.h:1342
virtual void DoRun(void)
Implementation to actually run this TestCase.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Vector GetPosition(void) const
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
encapsulates test code
Definition: test.h:1155
a 3d box
Definition: box.h:34
tuple nodes
Definition: first.py:25
Keep track of the current position and velocity of an object.
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
tuple mobility
Definition: third.py:101
bool operator<(const Room &a, const Room &b)
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
void SetNFloors(uint16_t nfloors)
Definition: building.cc:160
#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:168
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
#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:90
Helper class used to assign positions and mobility models to nodes.
mobility buildings information (to be used by mobility models)
void SetNRoomsX(uint16_t nroomx)
Definition: building.cc:167
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
static BuildingPositionAllocatorTestSuite buildingsPositionAllocatorTestSuiteInstance
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
#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:997
Room(uint32_t xx, uint32_t yy, uint32_t zz)
void SetBoundaries(Box box)
Set the boundaries of the building.
Definition: building.cc:139
#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:811
virtual void DoRun(void)
Implementation to actually run this TestCase.