A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ptr-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8
9#include "ns3/ptr.h"
10#include "ns3/test.h"
11
12/**
13 * @file
14 * @ingroup core-tests
15 * @ingroup ptr
16 * @ingroup ptr-tests
17 * Smart pointer test suite.
18 */
19
20/**
21 * @ingroup core-tests
22 * @defgroup ptr-tests Smart pointer test suite
23 */
24
25namespace ns3
26{
27
28namespace tests
29{
30
31class PtrTestCase;
32
33/**
34 * @ingroup ptr-tests
35 * Pointer base test class
36 */
38{
39 public:
40 /** Constructor. */
42 /** Destructor. */
43 virtual ~PtrTestBase();
44 /** Increment the reference count. */
45 void Ref() const;
46 /** Decrement the reference count, and delete if necessary. */
47 void Unref() const;
48
49 private:
50 mutable uint32_t m_count; //!< The reference count.
51};
52
53/**
54 * @ingroup ptr-tests
55 * No Count class
56 */
57class NoCount : public PtrTestBase
58{
59 public:
60 /**
61 * Constructor
62 *
63 * @param [in] test The object to track.
64 */
66 /**
67 * Destructor.
68 * The object being tracked will also be destroyed,
69 * by calling DestroyNotify()
70 */
71 ~NoCount() override;
72 /** Noop function. */
73 void Nothing() const;
74
75 private:
76 PtrTestCase* m_test; //!< The object being tracked.
77};
78
79/**
80 * @ingroup ptr-tests
81 * Test case for pointer
82 */
83class PtrTestCase : public TestCase
84{
85 public:
86 /** Constructor. */
88 /** Count the destruction of an object. */
89 void DestroyNotify();
90
91 private:
92 void DoRun() override;
93 /**
94 * Test that \pname{p} is a valid object, by calling a member function.
95 * @param [in] p The object pointer to test.
96 * @returns The object pointer.
97 */
99 /** @copydoc CallTest(Ptr<NoCount>) */
101 uint32_t m_nDestroyed; //!< Counter of number of objects destroyed.
102};
103
105 : m_count(1)
106{
107}
108
112
113void
115{
116 m_count++;
117}
118
119void
121{
122 m_count--;
123 if (m_count == 0)
124 {
125 delete this;
126 }
127}
128
130 : m_test(test)
131{
132}
133
138
139void
141{
142}
143
145 : TestCase("Sanity checking of Ptr<>")
146{
147}
148
149void
154
157{
158 return p;
159}
160
161const Ptr<NoCount>
163{
164 return p;
165}
166
167void
169{
170 m_nDestroyed = 0;
171 {
173 }
175
176 m_nDestroyed = 0;
177 {
178 Ptr<NoCount> p;
179 p = Create<NoCount>(this);
180#if defined(__clang__)
181#if __has_warning("-Wself-assign-overloaded")
182#pragma clang diagnostic push
183#pragma clang diagnostic ignored "-Wself-assign-overloaded"
184#endif
185#endif
186 p = p;
187#if defined(__clang__)
188#if __has_warning("-Wself-assign-overloaded")
189#pragma clang diagnostic pop
190#endif
191#endif
192 }
194
195 m_nDestroyed = 0;
196 {
197 Ptr<NoCount> p1;
198 p1 = Create<NoCount>(this);
199 Ptr<NoCount> p2 = p1;
200 }
202
203 m_nDestroyed = 0;
204 {
205 Ptr<NoCount> p1;
206 p1 = Create<NoCount>(this);
207 Ptr<NoCount> p2;
208 p2 = p1;
209 }
211
212 m_nDestroyed = 0;
213 {
214 Ptr<NoCount> p1;
215 p1 = Create<NoCount>(this);
216 Ptr<NoCount> p2 = Create<NoCount>(this);
217 p2 = p1;
218 }
220
221 m_nDestroyed = 0;
222 {
223 Ptr<NoCount> p1;
224 p1 = Create<NoCount>(this);
225 Ptr<NoCount> p2;
226 p2 = Create<NoCount>(this);
227 p2 = p1;
228 }
230
231 m_nDestroyed = 0;
232 {
233 Ptr<NoCount> p1;
234 p1 = Create<NoCount>(this);
235 p1 = Create<NoCount>(this);
236 }
238
239 m_nDestroyed = 0;
240 {
241 Ptr<NoCount> p1;
242 {
243 Ptr<NoCount> p2;
244 p1 = Create<NoCount>(this);
245 p2 = Create<NoCount>(this);
246 p2 = p1;
247 }
249 }
251
252 m_nDestroyed = 0;
253 {
254 Ptr<NoCount> p1;
255 {
256 Ptr<NoCount> p2;
257 p1 = Create<NoCount>(this);
258 p2 = Create<NoCount>(this);
259 p2 = CallTest(p1);
260 }
262 }
264
265 {
266 Ptr<NoCount> p1;
267 const Ptr<NoCount> p2 = CallTest(p1);
268 const Ptr<NoCount> p3 = CallTestConst(p1);
270 Ptr<const NoCount> p5 = p4;
271 // p4 = p5; You cannot make a const pointer be a non-const pointer.
272 // but if you use ConstCast, you can.
273 p4 = ConstCast<NoCount>(p5);
274 p5 = p1;
275 Ptr<NoCount> p;
276 if (!p)
277 {
278 }
279 if (p)
280 {
281 }
282 if (!p)
283 {
284 }
285 if (p)
286 {
287 }
288 if (p)
289 {
290 }
291 if (!p)
292 {
293 }
294 }
295
296 m_nDestroyed = 0;
297 {
298 NoCount* raw;
299 {
301 {
302 Ptr<const NoCount> p1 = p;
303 }
304 raw = GetPointer(p);
305 p = nullptr;
306 }
308 delete raw;
309 }
310
311 m_nDestroyed = 0;
312 {
314 const NoCount* v1 = PeekPointer(p);
315 NoCount* v2 = PeekPointer(p);
316 v1->Nothing();
317 v2->Nothing();
318 }
320
321 {
323 Ptr<NoCount> p1 = Create<NoCount>(this);
324 NS_TEST_EXPECT_MSG_EQ((p0 == p1), false, "operator == failed");
325 NS_TEST_EXPECT_MSG_EQ((p0 != p1), true, "operator != failed");
326 }
327}
328
329/**
330 * @ingroup ptr-tests
331 * Test suite for pointer
332 */
334{
335 public:
336 /** Constructor. */
338 : TestSuite("ptr")
339 {
341 }
342};
343
344/**
345 * @ingroup ptr-tests
346 * PtrTestSuite instance variable.
347 */
349
350} // namespace tests
351
352} // namespace ns3
Smart pointer class similar to boost::intrusive_ptr.
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
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.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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:241
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition ptr.h:443
U * GetPointer(const Ptr< U > &p)
Definition ptr.h:450
Ptr< T1 > ConstCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:573
-ns3 Test suite for the ns3 wrapper script