A Discrete-Event Network Simulator
API
 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/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 NS_LOG_COMPONENT_DEFINE ("BuildingPositionAllocatorTest");
36 
37 namespace ns3 {
38 
39 
40 
41 struct Room
42 {
43  Room (uint32_t xx, uint32_t yy, uint32_t zz);
44  uint32_t x;
45  uint32_t y;
46  uint32_t z;
47 };
48 
49 Room::Room (uint32_t xx, uint32_t yy, uint32_t zz)
50  : x (xx),
51  y (yy),
52  z (zz)
53 {
54 }
55 
56 bool
57 operator < (const Room& a, const Room& b)
58 {
59  return ( (a.x < b.x)
60  || ( (a.x == b.x) && (a.y < b.y) )
61  || ( (a.x == b.x) && (a.y == b.y) && (a.z < b.z) ));
62 }
63 
64 
65 
67 {
68 public:
70 
71 private:
72  virtual void DoRun (void);
73 
74 };
75 
76 
78  : TestCase ("RandomRoom, 12 rooms, 24 nodes")
79 {
80 }
81 
82 void
84 {
85  NS_LOG_FUNCTION (this);
86 
87 
88 
89  NS_LOG_LOGIC ("create building");
90  Ptr<Building> b = CreateObject<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");
101  Ptr<PositionAllocator> positionAlloc = CreateObject<RandomRoomPositionAllocator> ();
102  mobility.SetPositionAllocator (positionAlloc);
103  mobility.Install (nodes);
104  BuildingsHelper::Install (nodes);
105 
107 
108  std::map<Room, uint32_t> roomCounter;
109 
110  for (NodeContainer::Iterator it = nodes.Begin (); it != nodes.End (); ++it)
111  {
113  NS_ASSERT_MSG (mm, "no mobility model aggregated to this node");
115  NS_ASSERT_MSG (bmm, "MobilityBuildingInfo has not been aggregated to this node mobility model");
116 
117  NS_TEST_ASSERT_MSG_EQ (bmm->IsIndoor (), true, "node should be indoor");
118  Room r (bmm->GetRoomNumberX (), bmm->GetRoomNumberY (), bmm->GetFloorNumber ());
119  ++(roomCounter[r]);
120 
121  Vector p = mm->GetPosition ();
122  NS_TEST_ASSERT_MSG_GT (p.x, bmm->GetRoomNumberX (), "wrong x value");
123  NS_TEST_ASSERT_MSG_LT (p.x, bmm->GetRoomNumberX () + 1, "wrong x value");
124  NS_TEST_ASSERT_MSG_GT (p.y, bmm->GetRoomNumberY (), "wrong y value");
125  NS_TEST_ASSERT_MSG_LT (p.y, bmm->GetRoomNumberY () + 1, "wrong y value");
126  NS_TEST_ASSERT_MSG_GT (p.z, bmm->GetFloorNumber (), "wrong z value");
127  NS_TEST_ASSERT_MSG_LT (p.z, bmm->GetFloorNumber () + 1, "wrong z value");
128 
129  }
130 
131  for (std::map<Room, uint32_t>::iterator it = roomCounter.begin (); it != roomCounter.end (); ++it)
132  {
133  // random selection is done without replacement until the set of
134  // eligible room is empty, at which point the set is filled
135  // again. Hence with 24 nodes and 12 rooms we expect 2 nodes per room
136  NS_TEST_ASSERT_MSG_EQ (it->second, 2, "expected 2 nodes per room");
137  }
138 
139  NS_TEST_ASSERT_MSG_EQ (roomCounter.size (), 12, "expected 12 rooms allocated");
140 
142 }
143 
144 
145 
146 
147 
149 {
150 public:
152 
153 private:
154  virtual void DoRun (void);
155 
156 };
157 
158 
160  : TestCase ("SameRoom 48 nodes")
161 {
162 }
163 
164 void
166 {
167  NS_LOG_FUNCTION (this);
168 
169 
170 
171  NS_LOG_LOGIC ("create building");
172  Ptr<Building> b = CreateObject<Building> ();
173  b->SetBoundaries (Box (-10, -6, 20, 26, -1, 5));
174  b->SetNFloors (2);
175  b->SetNRoomsX (2);
176  b->SetNRoomsY (3);
177 
179  nodes.Create (24);
180 
181  MobilityHelper mobility;
182  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
183  Ptr<PositionAllocator> positionAlloc = CreateObject<RandomRoomPositionAllocator> ();
184  mobility.SetPositionAllocator (positionAlloc);
185  mobility.Install (nodes);
186  BuildingsHelper::Install (nodes);
187 
188  NodeContainer copyNodes;
189  copyNodes.Create (48);
190  positionAlloc = CreateObject<SameRoomPositionAllocator> (nodes);
191  mobility.SetPositionAllocator (positionAlloc);
192  mobility.Install (copyNodes);
193  BuildingsHelper::Install (copyNodes);
194 
196 
197  std::map<Room, uint32_t> roomCounter;
198 
199  for (NodeContainer::Iterator it = copyNodes.Begin (); it != copyNodes.End (); ++it)
200  {
202  NS_ASSERT_MSG (mm, "no mobility model aggregated to this node");
204  NS_ASSERT_MSG (bmm, "MobilityBuildingInfo has not been aggregated to this node mobility model");
205 
206  NS_TEST_ASSERT_MSG_EQ (bmm->IsIndoor (), true, "node should be indoor");
207  Room r (bmm->GetRoomNumberX (), bmm->GetRoomNumberY (), bmm->GetFloorNumber ());
208  ++(roomCounter[r]);
209  }
210 
211  for (std::map<Room, uint32_t>::iterator it = roomCounter.begin (); it != roomCounter.end (); ++it)
212  {
213 
214  NS_TEST_ASSERT_MSG_EQ (it->second, 4, "expected 4 nodes per room");
215  }
216 
217  NS_TEST_ASSERT_MSG_EQ (roomCounter.size (), 12, "expected 12 rooms allocated");
218 
220 }
221 
222 
223 
224 
225 
226 
228 {
229 public:
231 };
232 
233 
235  : TestSuite ("building-position-allocator", UNIT)
236 {
237  NS_LOG_FUNCTION (this);
238 
241 
242 }
243 
245 
246 } // namespace ns3
247 
virtual void DoRun(void)
Implementation to actually run this TestCase.
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
std::vector< Ptr< Node > >::const_iterator Iterator
NS_LOG_COMPONENT_DEFINE("BuildingPositionAllocatorTest")
A suite of tests to run.
Definition: test.h:1025
Vector GetPosition(void) const
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
bool operator<(const Room &a, const Room &b)
encapsulates test code
Definition: test.h:849
a 3d vector
Definition: vector.h:31
a 3d box
Definition: box.h:33
tuple nodes
Definition: first.py:25
Keep track of the current position and velocity of an object.
#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:773
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
static void MakeMobilityModelConsistent()
This method goes through the whole NodeList and, for each node in the list, calls BuildingsHelper::Ma...
virtual void DoRun(void)
Implementation to actually run this TestCase.
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
static BuildingPositionAllocatorTestSuite buildingsPositionAllocatorTestSuiteInstance
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())
Room(uint32_t xx, uint32_t yy, uint32_t zz)
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:173
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Fast test.
Definition: test.h:857
Helper class used to assign positions and mobility models to nodes.
mobility buildings information (to be used by mobility models)
#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:688
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
Ptr< T > GetObject(void) const
Definition: object.h:361
#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:137
static void Install(Ptr< Node > node)
Install the MobilityBuildingInfo to a node.