A Discrete-Event Network Simulator
API
ptr-test-suite.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005,2006 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 
20 #include "ns3/ptr.h"
21 #include "ns3/test.h"
22 
36 namespace ns3
37 {
38 
39 namespace tests
40 {
41 
42 class PtrTestCase;
43 
49 {
50  public:
52  PtrTestBase();
54  virtual ~PtrTestBase();
56  void Ref() const;
58  void Unref() 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() override;
84  void Nothing() const;
85 
86  private:
88 };
89 
94 class PtrTestCase : public TestCase
95 {
96  public:
98  PtrTestCase();
100  void DestroyNotify();
101 
102  private:
103  void DoRun() override;
111  const Ptr<NoCount> CallTestConst(const Ptr<NoCount> p);
112  uint32_t m_nDestroyed;
113 };
114 
116  : m_count(1)
117 {
118 }
119 
121 {
122 }
123 
124 void
126 {
127  m_count++;
128 }
129 
130 void
132 {
133  m_count--;
134  if (m_count == 0)
135  {
136  delete this;
137  }
138 }
139 
141  : m_test(test)
142 {
143 }
144 
146 {
148 }
149 
150 void
152 {
153 }
154 
156  : TestCase("Sanity checking of Ptr<>")
157 {
158 }
159 
160 void
162 {
163  m_nDestroyed++;
164 }
165 
168 {
169  return p;
170 }
171 
172 const Ptr<NoCount>
174 {
175  return p;
176 }
177 
178 void
180 {
181  m_nDestroyed = 0;
182  {
183  Ptr<NoCount> p = Create<NoCount>(this);
184  }
186 
187  m_nDestroyed = 0;
188  {
189  Ptr<NoCount> p;
190  p = Create<NoCount>(this);
191 #if defined(__clang__)
192 #if __has_warning("-Wself-assign-overloaded")
193 #pragma clang diagnostic push
194 #pragma clang diagnostic ignored "-Wself-assign-overloaded"
195 #endif
196 #endif
197  p = p;
198 #if defined(__clang__)
199 #if __has_warning("-Wself-assign-overloaded")
200 #pragma clang diagnostic pop
201 #endif
202 #endif
203  }
205 
206  m_nDestroyed = 0;
207  {
208  Ptr<NoCount> p1;
209  p1 = Create<NoCount>(this);
210  Ptr<NoCount> p2 = p1;
211  }
213 
214  m_nDestroyed = 0;
215  {
216  Ptr<NoCount> p1;
217  p1 = Create<NoCount>(this);
218  Ptr<NoCount> p2;
219  p2 = p1;
220  }
222 
223  m_nDestroyed = 0;
224  {
225  Ptr<NoCount> p1;
226  p1 = Create<NoCount>(this);
227  Ptr<NoCount> p2 = Create<NoCount>(this);
228  p2 = p1;
229  }
231 
232  m_nDestroyed = 0;
233  {
234  Ptr<NoCount> p1;
235  p1 = Create<NoCount>(this);
236  Ptr<NoCount> p2;
237  p2 = Create<NoCount>(this);
238  p2 = p1;
239  }
241 
242  m_nDestroyed = 0;
243  {
244  Ptr<NoCount> p1;
245  p1 = Create<NoCount>(this);
246  p1 = Create<NoCount>(this);
247  }
249 
250  m_nDestroyed = 0;
251  {
252  Ptr<NoCount> p1;
253  {
254  Ptr<NoCount> p2;
255  p1 = Create<NoCount>(this);
256  p2 = Create<NoCount>(this);
257  p2 = p1;
258  }
260  }
262 
263  m_nDestroyed = 0;
264  {
265  Ptr<NoCount> p1;
266  {
267  Ptr<NoCount> p2;
268  p1 = Create<NoCount>(this);
269  p2 = Create<NoCount>(this);
270  p2 = CallTest(p1);
271  }
273  }
275 
276  {
277  Ptr<NoCount> p1;
278  const Ptr<NoCount> p2 = CallTest(p1);
279  const Ptr<NoCount> p3 = CallTestConst(p1);
280  Ptr<NoCount> p4 = CallTestConst(p1);
281  Ptr<const NoCount> p5 = p4;
282  // p4 = p5; You cannot make a const pointer be a non-const pointer.
283  // but if you use ConstCast, you can.
284  p4 = ConstCast<NoCount>(p5);
285  p5 = p1;
286  Ptr<NoCount> p;
287  if (!p)
288  {
289  }
290  if (p)
291  {
292  }
293  if (!p)
294  {
295  }
296  if (p)
297  {
298  }
299  if (p)
300  {
301  }
302  if (!p)
303  {
304  }
305  }
306 
307  m_nDestroyed = 0;
308  {
309  NoCount* raw;
310  {
311  Ptr<NoCount> p = Create<NoCount>(this);
312  {
313  Ptr<const NoCount> p1 = p;
314  }
315  raw = GetPointer(p);
316  p = nullptr;
317  }
319  delete raw;
320  }
321 
322  m_nDestroyed = 0;
323  {
324  Ptr<NoCount> p = Create<NoCount>(this);
325  const NoCount* v1 = PeekPointer(p);
326  NoCount* v2 = PeekPointer(p);
327  v1->Nothing();
328  v2->Nothing();
329  }
331 
332  {
333  Ptr<PtrTestBase> p0 = Create<NoCount>(this);
334  Ptr<NoCount> p1 = Create<NoCount>(this);
335  NS_TEST_EXPECT_MSG_EQ((p0 == p1), false, "operator == failed");
336  NS_TEST_EXPECT_MSG_EQ((p0 != p1), true, "operator != failed");
337  }
338 }
339 
344 class PtrTestSuite : public TestSuite
345 {
346  public:
349  : TestSuite("ptr")
350  {
351  AddTestCase(new PtrTestCase());
352  }
353 };
354 
360 
361 } // namespace tests
362 
363 } // namespace ns3
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
No Count class.
PtrTestCase * m_test
The object being tracked.
~NoCount() override
Destructor.
void Nothing() const
Noop function.
NoCount(PtrTestCase *test)
Constructor.
Pointer base test class.
void Ref() const
Increment the reference count.
void Unref() const
Decrement the reference count, and delete if necessary.
virtual ~PtrTestBase()
Destructor.
uint32_t m_count
The reference count.
Test case for pointer.
Ptr< NoCount > CallTest(Ptr< NoCount > p)
Test that p is a valid object, by calling a member function.
uint32_t m_nDestroyed
Counter of number of objects destroyed.
void DestroyNotify()
Count the destruction of an object.
void DoRun() override
Implementation to actually run this TestCase.
const Ptr< NoCount > CallTestConst(const Ptr< NoCount > p)
Test that p is a valid object, by calling a member function.
Test suite for pointer.
static PtrTestSuite g_ptrTestSuite
PtrTestSuite instance variable.
#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:251
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * GetPointer(const Ptr< U > &p)
Definition: ptr.h:456
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:449