A Discrete-Event Network Simulator
API
ptr-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) 2005,2006 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "ns3/test.h"
22 #include "ns3/ptr.h"
23 
37 namespace ns3 {
38 
39  namespace tests {
40 
41 
42 class PtrTestCase;
43 
49 {
50 public:
52  PtrTestBase ();
54  virtual ~PtrTestBase ();
56  void Ref (void) const;
58  void Unref (void) const;
59 private:
60  mutable uint32_t m_count;
61 };
62 
67 class NoCount : public PtrTestBase
68 {
69 public:
75  NoCount (PtrTestCase *test);
81  ~NoCount ();
83  void Nothing (void) const;
84 private:
86 };
87 
88 
93 class PtrTestCase : public TestCase
94 {
95 public:
97  PtrTestCase ();
99  void DestroyNotify (void);
100 private:
101  virtual void DoRun (void);
109  Ptr<NoCount> const CallTestConst (Ptr<NoCount> const p);
110  uint32_t m_nDestroyed;
111 };
112 
113 
115  : m_count (1)
116 {
117 }
119 {
120 }
121 void
122 PtrTestBase::Ref (void) const
123 {
124  m_count++;
125 }
126 void
127 PtrTestBase::Unref (void) const
128 {
129  m_count--;
130  if (m_count == 0)
131  {
132  delete this;
133  }
134 }
135 
137  : m_test (test)
138 {
139 }
141 {
142  m_test->DestroyNotify ();
143 }
144 void
146 {}
147 
148 
149 
151  : TestCase ("Sanity checking of Ptr<>")
152 {
153 }
154 void
156 {
157  m_nDestroyed++;
158 }
161 {
162  return p;
163 }
164 
165 Ptr<NoCount> const
167 {
168  return p;
169 }
170 
171 
172 void
174 {
175  m_nDestroyed = false;
176  {
177  Ptr<NoCount> p = Create<NoCount> (this);
178  }
180 
181  m_nDestroyed = 0;
182  {
183  Ptr<NoCount> p;
184  p = Create<NoCount> (this);
185 #if defined(__clang__)
186  #if __has_warning("-Wself-assign-overloaded")
187  #pragma clang diagnostic push
188  #pragma clang diagnostic ignored "-Wself-assign-overloaded"
189  #endif
190 #endif
191  p = p;
192 #if defined(__clang__)
193  #if __has_warning("-Wself-assign-overloaded")
194  #pragma clang diagnostic pop
195  #endif
196 #endif
197  }
199 
200  m_nDestroyed = 0;
201  {
202  Ptr<NoCount> p1;
203  p1 = Create<NoCount> (this);
204  Ptr<NoCount> p2 = p1;
205  }
207 
208  m_nDestroyed = 0;
209  {
210  Ptr<NoCount> p1;
211  p1 = Create<NoCount> (this);
212  Ptr<NoCount> p2;
213  p2 = p1;
214  }
216 
217  m_nDestroyed = 0;
218  {
219  Ptr<NoCount> p1;
220  p1 = Create<NoCount> (this);
221  Ptr<NoCount> p2 = Create<NoCount> (this);
222  p2 = p1;
223  }
225 
226  m_nDestroyed = 0;
227  {
228  Ptr<NoCount> p1;
229  p1 = Create<NoCount> (this);
230  Ptr<NoCount> p2;
231  p2 = Create<NoCount> (this);
232  p2 = p1;
233  }
235 
236  m_nDestroyed = 0;
237  {
238  Ptr<NoCount> p1;
239  p1 = Create<NoCount> (this);
240  p1 = Create<NoCount> (this);
241  }
243 
244  m_nDestroyed = 0;
245  {
246  Ptr<NoCount> p1;
247  {
248  Ptr<NoCount> p2;
249  p1 = Create<NoCount> (this);
250  p2 = Create<NoCount> (this);
251  p2 = p1;
252  }
254  }
256 
257  m_nDestroyed = 0;
258  {
259  Ptr<NoCount> p1;
260  {
261  Ptr<NoCount> p2;
262  p1 = Create<NoCount> (this);
263  p2 = Create<NoCount> (this);
264  p2 = CallTest (p1);
265  }
267  }
269 
270  {
271  Ptr<NoCount> p1;
272  Ptr<NoCount> const p2 = CallTest (p1);
273  Ptr<NoCount> const p3 = CallTestConst (p1);
274  Ptr<NoCount> p4 = CallTestConst (p1);
275  Ptr<NoCount const> p5 = p4;
276  //p4 = p5; You cannot make a const pointer be a non-const pointer.
277  // but if you use ConstCast, you can.
278  p4 = ConstCast<NoCount> (p5);
279  p5 = p1;
280  Ptr<NoCount> p;
281  if (p == 0)
282  {}
283  if (p != 0)
284  {}
285  if (0 == p)
286  {}
287  if (0 != p)
288  {}
289  if (p)
290  {}
291  if (!p)
292  {}
293  }
294 
295  m_nDestroyed = 0;
296  {
297  NoCount *raw;
298  {
299  Ptr<NoCount> p = Create<NoCount> (this);
300  {
301  Ptr<NoCount const> p1 = p;
302  }
303  raw = GetPointer (p);
304  p = 0;
305  }
307  delete raw;
308  }
309 
310  m_nDestroyed = 0;
311  {
312  Ptr<NoCount> p = Create<NoCount> (this);
313  const NoCount *v1 = PeekPointer (p);
314  NoCount *v2 = PeekPointer (p);
315  v1->Nothing ();
316  v2->Nothing ();
317  }
319 
320  {
321  Ptr<PtrTestBase> p0 = Create<NoCount> (this);
322  Ptr<NoCount> p1 = Create<NoCount> (this);
323  NS_TEST_EXPECT_MSG_EQ ((p0 == p1), false, "operator == failed");
324  NS_TEST_EXPECT_MSG_EQ ((p0 != p1), true, "operator != failed");
325  }
326 }
327 
332 class PtrTestSuite : public TestSuite
333 {
334 public:
337  : TestSuite ("ptr")
338  {
339  AddTestCase (new PtrTestCase ());
340  }
341 };
342 
348 
349 
350  } // namespace tests
351 
352 } // namespace ns3
PtrTestCase * m_test
The object being tracked.
PtrTestCase()
Constructor.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
virtual ~PtrTestBase()
Destructor.
uint32_t m_count
The reference count.
A suite of tests to run.
Definition: test.h:1342
void Nothing(void) const
Noop function.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:564
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:285
encapsulates test code
Definition: test.h:1155
void Ref(void) const
Increment the reference count.
U * GetPointer(const Ptr< U > &p)
Definition: ptr.h:570
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
virtual void DoRun(void)
Implementation to actually run this TestCase.
PtrTestBase()
Constructor.
static PtrTestSuite g_ptrTestSuite
PtrTestSuite instance variable.
No Count class.
Test case for pointer.
void DestroyNotify(void)
Count the destruction of an object.
Pointer base test class.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
~NoCount()
Destructor.
void Unref(void) const
Decrement the reference count, and delete if necessary.
NoCount(PtrTestCase *test)
Constructor.
uint32_t m_nDestroyed
Counter of number of objects destroyed.
Ptr< NoCount > const CallTestConst(Ptr< NoCount > const p)
Test that p is a valid object, by calling a member function.
Test suite for pointer.
Ptr< NoCount > CallTest(Ptr< NoCount > p)
Test that p is a valid object, by calling a member function.