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 
60 private:
61  mutable uint32_t m_count;
62 };
63 
68 class NoCount : public PtrTestBase
69 {
70 public:
76  NoCount (PtrTestCase *test);
82  ~NoCount ();
84  void Nothing (void) const;
85 
86 private:
88 };
89 
90 
95 class PtrTestCase : public TestCase
96 {
97 public:
99  PtrTestCase ();
101  void DestroyNotify (void);
102 
103 private:
104  virtual void DoRun (void);
112  Ptr<NoCount> const CallTestConst (Ptr<NoCount> const p);
113  uint32_t m_nDestroyed;
114 };
115 
116 
118  : m_count (1)
119 {}
121 {}
122 void
123 PtrTestBase::Ref (void) const
124 {
125  m_count++;
126 }
127 void
128 PtrTestBase::Unref (void) const
129 {
130  m_count--;
131  if (m_count == 0)
132  {
133  delete this;
134  }
135 }
136 
138  : m_test (test)
139 {}
141 {
142  m_test->DestroyNotify ();
143 }
144 void
146 {}
147 
148 
149 
151  : TestCase ("Sanity checking of Ptr<>")
152 {}
153 void
155 {
156  m_nDestroyed++;
157 }
160 {
161  return p;
162 }
163 
164 Ptr<NoCount> const
166 {
167  return p;
168 }
169 
170 
171 void
173 {
174  m_nDestroyed = false;
175  {
176  Ptr<NoCount> p = Create<NoCount> (this);
177  }
179 
180  m_nDestroyed = 0;
181  {
182  Ptr<NoCount> p;
183  p = Create<NoCount> (this);
184 #if defined(__clang__)
185  #if __has_warning ("-Wself-assign-overloaded")
186  #pragma clang diagnostic push
187  #pragma clang diagnostic ignored "-Wself-assign-overloaded"
188  #endif
189 #endif
190  p = p;
191 #if defined(__clang__)
192  #if __has_warning ("-Wself-assign-overloaded")
193  #pragma clang diagnostic pop
194  #endif
195 #endif
196  }
198 
199  m_nDestroyed = 0;
200  {
201  Ptr<NoCount> p1;
202  p1 = Create<NoCount> (this);
203  Ptr<NoCount> p2 = p1;
204  }
206 
207  m_nDestroyed = 0;
208  {
209  Ptr<NoCount> p1;
210  p1 = Create<NoCount> (this);
211  Ptr<NoCount> p2;
212  p2 = p1;
213  }
215 
216  m_nDestroyed = 0;
217  {
218  Ptr<NoCount> p1;
219  p1 = Create<NoCount> (this);
220  Ptr<NoCount> p2 = Create<NoCount> (this);
221  p2 = p1;
222  }
224 
225  m_nDestroyed = 0;
226  {
227  Ptr<NoCount> p1;
228  p1 = Create<NoCount> (this);
229  Ptr<NoCount> p2;
230  p2 = Create<NoCount> (this);
231  p2 = p1;
232  }
234 
235  m_nDestroyed = 0;
236  {
237  Ptr<NoCount> p1;
238  p1 = Create<NoCount> (this);
239  p1 = Create<NoCount> (this);
240  }
242 
243  m_nDestroyed = 0;
244  {
245  Ptr<NoCount> p1;
246  {
247  Ptr<NoCount> p2;
248  p1 = Create<NoCount> (this);
249  p2 = Create<NoCount> (this);
250  p2 = p1;
251  }
253  }
255 
256  m_nDestroyed = 0;
257  {
258  Ptr<NoCount> p1;
259  {
260  Ptr<NoCount> p2;
261  p1 = Create<NoCount> (this);
262  p2 = Create<NoCount> (this);
263  p2 = CallTest (p1);
264  }
266  }
268 
269  {
270  Ptr<NoCount> p1;
271  Ptr<NoCount> const p2 = CallTest (p1);
272  Ptr<NoCount> const p3 = CallTestConst (p1);
273  Ptr<NoCount> p4 = CallTestConst (p1);
274  Ptr<NoCount const> p5 = p4;
275  //p4 = p5; You cannot make a const pointer be a non-const pointer.
276  // but if you use ConstCast, you can.
277  p4 = ConstCast<NoCount> (p5);
278  p5 = p1;
279  Ptr<NoCount> p;
280  if (p == 0)
281  {}
282  if (p != 0)
283  {}
284  if (0 == p)
285  {}
286  if (0 != p)
287  {}
288  if (p)
289  {}
290  if (!p)
291  {}
292  }
293 
294  m_nDestroyed = 0;
295  {
296  NoCount *raw;
297  {
298  Ptr<NoCount> p = Create<NoCount> (this);
299  {
300  Ptr<NoCount const> p1 = p;
301  }
302  raw = GetPointer (p);
303  p = 0;
304  }
306  delete raw;
307  }
308 
309  m_nDestroyed = 0;
310  {
311  Ptr<NoCount> p = Create<NoCount> (this);
312  const NoCount *v1 = PeekPointer (p);
313  NoCount *v2 = PeekPointer (p);
314  v1->Nothing ();
315  v2->Nothing ();
316  }
318 
319  {
320  Ptr<PtrTestBase> p0 = Create<NoCount> (this);
321  Ptr<NoCount> p1 = Create<NoCount> (this);
322  NS_TEST_EXPECT_MSG_EQ ((p0 == p1), false, "operator == failed");
323  NS_TEST_EXPECT_MSG_EQ ((p0 != p1), true, "operator != failed");
324  }
325 }
326 
331 class PtrTestSuite : public TestSuite
332 {
333 public:
336  : TestSuite ("ptr")
337  {
338  AddTestCase (new PtrTestCase ());
339  }
340 };
341 
347 
348 
349 } // namespace tests
350 
351 } // 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:1343
void Nothing(void) const
Noop function.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:566
#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:283
encapsulates test code
Definition: test.h:1153
void Ref(void) const
Increment the reference count.
U * GetPointer(const Ptr< U > &p)
Definition: ptr.h:572
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.