A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
object-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 INRIA, Gustavo Carneiro
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  * Authors: Gustavo Carneiro <gjcarneiro@gmail.com>,
19  * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20  */
21 #include "ns3/test.h"
22 #include "ns3/object.h"
23 #include "ns3/object-factory.h"
24 #include "ns3/assert.h"
25 
26 namespace {
27 
28 class BaseA : public ns3::Object
29 {
30 public:
31  static ns3::TypeId GetTypeId (void) {
32  static ns3::TypeId tid = ns3::TypeId ("ObjectTest:BaseA")
33  .SetParent (Object::GetTypeId ())
35  .AddConstructor<BaseA> ();
36  return tid;
37  }
38  BaseA ()
39  {}
40  virtual void Dispose (void) {}
41 };
42 
43 class DerivedA : public BaseA
44 {
45 public:
46  static ns3::TypeId GetTypeId (void) {
47  static ns3::TypeId tid = ns3::TypeId ("ObjectTest:DerivedA")
48  .SetParent (BaseA::GetTypeId ())
51  return tid;
52  }
54  {}
55  virtual void Dispose (void) {
56  BaseA::Dispose ();
57  }
58 };
59 
60 class BaseB : public ns3::Object
61 {
62 public:
63  static ns3::TypeId GetTypeId (void) {
64  static ns3::TypeId tid = ns3::TypeId ("ObjectTest:BaseB")
65  .SetParent (Object::GetTypeId ())
67  .AddConstructor<BaseB> ();
68  return tid;
69  }
70  BaseB ()
71  {}
72  virtual void Dispose (void) {}
73 };
74 
75 class DerivedB : public BaseB
76 {
77 public:
78  static ns3::TypeId GetTypeId (void) {
79  static ns3::TypeId tid = ns3::TypeId ("ObjectTest:DerivedB")
80  .SetParent (BaseB::GetTypeId ())
83  return tid;
84  }
86  {}
87  virtual void Dispose (void) {
88  BaseB::Dispose ();
89  }
90 };
91 
93  ;
95  ;
97  ;
99  ;
100 
101 } // namespace anonymous
102 
103 namespace ns3 {
104 
105 // ===========================================================================
106 // Test case to make sure that we can make Objects using CreateObject.
107 // ===========================================================================
109 {
110 public:
112  virtual ~CreateObjectTestCase ();
113 
114 private:
115  virtual void DoRun (void);
116 };
117 
119  : TestCase ("Check CreateObject<Type> template function")
120 {
121 }
122 
124 {
125 }
126 
127 void
129 {
130  Ptr<BaseA> baseA = CreateObject<BaseA> ();
131  NS_TEST_ASSERT_MSG_NE (baseA, 0, "Unable to CreateObject<BaseA>");
132 
133  //
134  // Since baseA is a BaseA, we must be able to successfully ask for a BaseA.
135  //
136  NS_TEST_ASSERT_MSG_EQ (baseA->GetObject<BaseA> (), baseA, "GetObject() of same type returns different Ptr");
137 
138  //
139  // Since BaseA is a BaseA and not a DerivedA, we must not find a DerivedA if we look.
140  //
141  NS_TEST_ASSERT_MSG_EQ (baseA->GetObject<DerivedA> (), 0, "GetObject() of unrelated type returns nonzero pointer");
142 
143  //
144  // Since baseA is not a BaseA, we must not be able to ask for a DerivedA even if we
145  // try an implied cast back to a BaseA.
146  //
147  NS_TEST_ASSERT_MSG_EQ (baseA->GetObject<BaseA> (DerivedA::GetTypeId ()), 0, "GetObject() of unrelated returns nonzero Ptr");
148 
149  baseA = CreateObject<DerivedA> ();
150  NS_TEST_ASSERT_MSG_NE (baseA, 0, "Unable to CreateObject<DerivedA> with implicit cast to BaseA");
151 
152  //
153  // If we create a DerivedA and cast it to a BaseA, then if we do a GetObject for
154  // that BaseA we should get the same address (same Object).
155  //
156  NS_TEST_ASSERT_MSG_EQ (baseA->GetObject<BaseA> (), baseA, "Unable to GetObject<BaseA> on BaseA");
157 
158  //
159  // Since we created a DerivedA and cast it to a BaseA, we should be able to
160  // get back a DerivedA and it should be the original Ptr.
161  //
162  NS_TEST_ASSERT_MSG_EQ (baseA->GetObject<DerivedA> (), baseA, "GetObject() of the original type returns different Ptr");
163 
164  // If we created a DerivedA and cast it to a BaseA, then we GetObject for the
165  // same DerivedA and cast it back to the same BaseA, we should get the same
166  // object.
167  //
168  NS_TEST_ASSERT_MSG_EQ (baseA->GetObject<BaseA> (DerivedA::GetTypeId ()), baseA, "GetObject returns different Ptr");
169 }
170 
171 // ===========================================================================
172 // Test case to make sure that we can aggregate Objects.
173 // ===========================================================================
175 {
176 public:
178  virtual ~AggregateObjectTestCase ();
179 
180 private:
181  virtual void DoRun (void);
182 };
183 
185  : TestCase ("Check Object aggregation functionality")
186 {
187 }
188 
190 {
191 }
192 
193 void
195 {
196  Ptr<BaseA> baseA = CreateObject<BaseA> ();
197  NS_TEST_ASSERT_MSG_NE (baseA, 0, "Unable to CreateObject<BaseA>");
198 
199  Ptr<BaseB> baseB = CreateObject<BaseB> ();
200  NS_TEST_ASSERT_MSG_NE (baseB, 0, "Unable to CreateObject<BaseB>");
201 
202  Ptr<BaseB> baseBCopy = baseB;
203  NS_TEST_ASSERT_MSG_NE (baseBCopy, 0, "Unable to copy BaseB");
204 
205  //
206  // Make an aggregation of a BaseA object and a BaseB object.
207  //
208  baseA->AggregateObject (baseB);
209 
210  //
211  // We should be able to ask the aggregation (through baseA) for the BaseA part
212  // of the aggregation.
213  //
214  NS_TEST_ASSERT_MSG_NE (baseA->GetObject<BaseA> (), 0, "Cannot GetObject (through baseA) for BaseA Object");
215 
216  //
217  // There is no DerivedA in this picture, so we should not be able to GetObject
218  // for that type.
219  //
220  NS_TEST_ASSERT_MSG_EQ (baseA->GetObject<DerivedA> (), 0, "Unexpectedly found a DerivedA through baseA");
221 
222  //
223  // We should be able to ask the aggregation (through baseA) for the BaseB part
224  //
225  NS_TEST_ASSERT_MSG_NE (baseA->GetObject<BaseB> (), 0, "Cannot GetObject (through baseA) for BaseB Object");
226 
227  //
228  // There is no DerivedB in this picture, so we should not be able to GetObject
229  // for that type.
230  //
231  NS_TEST_ASSERT_MSG_EQ (baseA->GetObject<DerivedB> (), 0, "Unexpectedly found a DerivedB through baseA");
232 
233  //
234  // We should be able to ask the aggregation (through baseA) for the BaseB part
235  //
236  NS_TEST_ASSERT_MSG_NE (baseB->GetObject<BaseB> (), 0, "Cannot GetObject (through baseB) for BaseB Object");
237 
238  //
239  // There is no DerivedB in this picture, so we should not be able to GetObject
240  // for that type.
241  //
242  NS_TEST_ASSERT_MSG_EQ (baseB->GetObject<DerivedB> (), 0, "Unexpectedly found a DerivedB through baseB");
243 
244  //
245  // We should be able to ask the aggregation (through baseB) for the BaseA part
246  // of the aggregation.
247  //
248  NS_TEST_ASSERT_MSG_NE (baseB->GetObject<BaseA> (), 0, "Cannot GetObject (through baseB) for BaseA Object");
249 
250  //
251  // There is no DerivedA in this picture, so we should not be able to GetObject
252  // for that type.
253  //
254  NS_TEST_ASSERT_MSG_EQ (baseB->GetObject<DerivedA> (), 0, "Unexpectedly found a DerivedA through baseB");
255 
256  //
257  // baseBCopy is a copy of the original Ptr to the Object BaseB. Even though
258  // we didn't use baseBCopy directly in the aggregations, the object to which
259  // it points was used, therefore, we should be able to use baseBCopy as if
260  // it were baseB and get a BaseA out of the aggregation.
261  //
262  NS_TEST_ASSERT_MSG_NE (baseBCopy->GetObject<BaseA> (), 0, "Cannot GetObject (through baseBCopy) for a BaseA Object");
263 
264  //
265  // Now, change the underlying type of the objects to be the derived types.
266  //
267  baseA = CreateObject<DerivedA> ();
268  NS_TEST_ASSERT_MSG_NE (baseA, 0, "Unable to CreateObject<DerivedA> with implicit cast to BaseA");
269 
270  baseB = CreateObject<DerivedB> ();
271  NS_TEST_ASSERT_MSG_NE (baseB, 0, "Unable to CreateObject<DerivedB> with implicit cast to BaseB");
272 
273  //
274  // Create an aggregation of two objects, both of the derived types; and leave
275  // an unaggregated copy of one lying around.
276  //
277  baseBCopy = baseB;
278  baseA->AggregateObject (baseB);
279 
280  //
281  // We should be able to ask the aggregation (through baseA) for the DerivedB part
282  //
283  NS_TEST_ASSERT_MSG_NE (baseA->GetObject<DerivedB> (), 0, "Cannot GetObject (through baseA) for DerivedB Object");
284 
285  //
286  // Since the DerivedB is also a BaseB, we should be able to ask the aggregation
287  // (through baseA) for the BaseB part
288  //
289  NS_TEST_ASSERT_MSG_NE (baseA->GetObject<BaseB> (), 0, "Cannot GetObject (through baseA) for BaseB Object");
290 
291  //
292  // We should be able to ask the aggregation (through baseB) for the DerivedA part
293  //
294  NS_TEST_ASSERT_MSG_NE (baseB->GetObject<DerivedA> (), 0, "Cannot GetObject (through baseB) for DerivedA Object");
295 
296  //
297  // Since the DerivedA is also a BaseA, we should be able to ask the aggregation
298  // (through baseB) for the BaseA part
299  //
300  NS_TEST_ASSERT_MSG_NE (baseB->GetObject<BaseA> (), 0, "Cannot GetObject (through baseB) for BaseA Object");
301 
302  //
303  // baseBCopy is a copy of the original Ptr to the Object BaseB. Even though
304  // we didn't use baseBCopy directly in the aggregations, the object to which
305  // it points was used, therefore, we should be able to use baseBCopy as if
306  // it were baseB (same underlying Object) and get a BaseA and a DerivedA out
307  // of the aggregation through baseBCopy.
308  //
309  NS_TEST_ASSERT_MSG_NE (baseBCopy->GetObject<BaseA> (), 0, "Cannot GetObject (through baseBCopy) for a BaseA Object");
310  NS_TEST_ASSERT_MSG_NE (baseBCopy->GetObject<DerivedA> (), 0, "Cannot GetObject (through baseBCopy) for a BaseA Object");
311 
312  //
313  // Since the Ptr<BaseB> is actually a DerivedB, we should be able to ask the
314  // aggregation (through baseB) for the DerivedB part
315  //
316  NS_TEST_ASSERT_MSG_NE (baseB->GetObject<DerivedB> (), 0, "Cannot GetObject (through baseB) for DerivedB Object");
317 
318  //
319  // Since the DerivedB was cast to a BaseB, we should be able to ask the
320  // aggregation (through baseB) for the BaseB part
321  //
322  NS_TEST_ASSERT_MSG_NE (baseB->GetObject<BaseB> (), 0, "Cannot GetObject (through baseB) for BaseB Object");
323 
324  //
325  // Make sure reference counting works in the aggregate. Create two Objects
326  // and aggregate them, then release one of them. The aggregation should
327  // keep a reference to both and the Object we released should still be there.
328  //
329  baseA = CreateObject<BaseA> ();
330  NS_TEST_ASSERT_MSG_NE (baseA, 0, "Unable to CreateObject<BaseA>");
331 
332  baseB = CreateObject<BaseB> ();
333  NS_TEST_ASSERT_MSG_NE (baseB, 0, "Unable to CreateObject<BaseA>");
334 
335  baseA->AggregateObject (baseB);
336  baseA = 0;
337 
338  baseA = baseB->GetObject<BaseA> ();
339  NS_TEST_ASSERT_MSG_NE (baseA, 0, "Unable to GetObject on released object");
340 }
341 
342 // ===========================================================================
343 // Test case to make sure that an Object factory can create Objects
344 // ===========================================================================
346 {
347 public:
349  virtual ~ObjectFactoryTestCase ();
350 
351 private:
352  virtual void DoRun (void);
353 };
354 
356  : TestCase ("Check ObjectFactory functionality")
357 {
358 }
359 
361 {
362 }
363 
364 void
366 {
367  ObjectFactory factory;
368 
369  //
370  // Create an Object of type BaseA through an object factory.
371  //
372  factory.SetTypeId (BaseA::GetTypeId ());
373  Ptr<Object> a = factory.Create ();
374  NS_TEST_ASSERT_MSG_NE (a, 0, "Unable to factory.Create() a BaseA");
375 
376  //
377  // What we made should be a BaseA, not have anything to do with a DerivedA
378  //
379  NS_TEST_ASSERT_MSG_EQ (a->GetObject<BaseA> (DerivedA::GetTypeId ()), 0, "BaseA is unexpectedly a DerivedA also");
380 
381  //
382  // The BaseA we got should not respond to a GetObject for DerivedA
383  //
384  NS_TEST_ASSERT_MSG_EQ (a->GetObject<DerivedA> (), 0, "BaseA unexpectedly responds to GetObject for DerivedA");
385 
386  //
387  // Now tell the factory to make DerivedA Objects and create one with an
388  // implied cast back to a BaseA
389  //
390  factory.SetTypeId (DerivedA::GetTypeId ());
391  a = factory.Create ();
392 
393  //
394  // Since the DerivedA has a BaseA part, we should be able to use GetObject to
395  // dynamically cast back to a BaseA.
396  //
397  NS_TEST_ASSERT_MSG_EQ (a->GetObject<BaseA> (), a, "Unable to use GetObject as dynamic_cast<BaseA>()");
398 
399  //
400  // Since a is already a BaseA and is really a DerivedA, we should be able to
401  // GetObject for the DerivedA and cast it back to a BaseA getting the same
402  // value that is there.
403  //
404  NS_TEST_ASSERT_MSG_EQ (a->GetObject<BaseA> (DerivedA::GetTypeId ()), a, "GetObject with implied cast returns different Ptr");
405 
406  //
407  // Since a declared a BaseA, even if it is really a DerivedA, we should not
408  // be able to GetOBject for a DerivedA since this would break the type
409  // declaration.
410  //
411  NS_TEST_ASSERT_MSG_NE (a->GetObject<DerivedA> (), 0, "Unexpectedly able to work around C++ type system");
412 }
413 
414 // ===========================================================================
415 // The Test Suite that glues the Test Cases together.
416 // ===========================================================================
418 {
419 public:
420  ObjectTestSuite ();
421 };
422 
424  : TestSuite ("object", UNIT)
425 {
429 }
430 
432 
433 } // namespace ns3
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
TypeId AddConstructor(void)
Definition: type-id.h:418
A suite of tests to run.
Definition: test.h:1025
void SetTypeId(TypeId tid)
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not...
Definition: test.h:539
encapsulates test code
Definition: test.h:849
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:47
static ObjectTestSuite objectTestSuite
Ptr< Object > Create(void) const
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:173
Fast test.
Definition: test.h:857
instantiate subclasses of ns3::Object.
a base class which provides memory management and object aggregation
Definition: object.h:63
Ptr< T > GetObject(void) const
Definition: object.h:361
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
virtual void DoRun(void)
Implementation to actually run this TestCase.
#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
TypeId HideFromDocumentation(void)
Definition: type-id.cc:783
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoRun(void)
Implementation to actually run this TestCase.