A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
25
namespace
ns3
26
{
27
28
namespace
tests
29
{
30
31
class
PtrTestCase;
32
33
/**
34
* @ingroup ptr-tests
35
* Pointer base test class
36
*/
37
class
PtrTestBase
38
{
39
public
:
40
/** Constructor. */
41
PtrTestBase
();
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
*/
57
class
NoCount
:
public
PtrTestBase
58
{
59
public
:
60
/**
61
* Constructor
62
*
63
* @param [in] test The object to track.
64
*/
65
NoCount
(
PtrTestCase
*
test
);
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
*/
83
class
PtrTestCase
:
public
TestCase
84
{
85
public
:
86
/** Constructor. */
87
PtrTestCase
();
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
*/
98
Ptr<NoCount>
CallTest
(
Ptr<NoCount>
p);
99
/** @copydoc CallTest(Ptr<NoCount>) */
100
const
Ptr<NoCount>
CallTestConst
(
const
Ptr<NoCount>
p);
101
uint32_t
m_nDestroyed
;
//!< Counter of number of objects destroyed.
102
};
103
104
PtrTestBase::PtrTestBase
()
105
: m_count(1)
106
{
107
}
108
109
PtrTestBase::~PtrTestBase
()
110
{
111
}
112
113
void
114
PtrTestBase::Ref
()
const
115
{
116
m_count
++;
117
}
118
119
void
120
PtrTestBase::Unref
()
const
121
{
122
m_count
--;
123
if
(
m_count
== 0)
124
{
125
delete
this
;
126
}
127
}
128
129
NoCount::NoCount
(
PtrTestCase
*
test
)
130
: m_test(
test
)
131
{
132
}
133
134
NoCount::~NoCount
()
135
{
136
m_test
->
DestroyNotify
();
137
}
138
139
void
140
NoCount::Nothing
()
const
141
{
142
}
143
144
PtrTestCase::PtrTestCase
()
145
:
TestCase
(
"Sanity checking of Ptr<>"
)
146
{
147
}
148
149
void
150
PtrTestCase::DestroyNotify
()
151
{
152
m_nDestroyed
++;
153
}
154
155
Ptr<NoCount>
156
PtrTestCase::CallTest
(
Ptr<NoCount>
p)
157
{
158
return
p;
159
}
160
161
const
Ptr<NoCount>
162
PtrTestCase::CallTestConst
(
const
Ptr<NoCount>
p)
163
{
164
return
p;
165
}
166
167
void
168
PtrTestCase::DoRun
()
169
{
170
m_nDestroyed
= 0;
171
{
172
Ptr<NoCount>
p =
Create<NoCount>
(
this
);
173
}
174
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"001"
);
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
}
193
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"002"
);
194
195
m_nDestroyed
= 0;
196
{
197
Ptr<NoCount>
p1;
198
p1 =
Create<NoCount>
(
this
);
199
Ptr<NoCount>
p2 = p1;
200
}
201
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"003"
);
202
203
m_nDestroyed
= 0;
204
{
205
Ptr<NoCount>
p1;
206
p1 =
Create<NoCount>
(
this
);
207
Ptr<NoCount>
p2;
208
p2 = p1;
209
}
210
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"004"
);
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
}
219
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"005"
);
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
}
229
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"006"
);
230
231
m_nDestroyed
= 0;
232
{
233
Ptr<NoCount>
p1;
234
p1 =
Create<NoCount>
(
this
);
235
p1 =
Create<NoCount>
(
this
);
236
}
237
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"007"
);
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
}
248
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"008"
);
249
}
250
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"009"
);
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
}
261
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"010"
);
262
}
263
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"011"
);
264
265
{
266
Ptr<NoCount>
p1;
267
const
Ptr<NoCount>
p2 =
CallTest
(p1);
268
const
Ptr<NoCount>
p3 =
CallTestConst
(p1);
269
Ptr<NoCount>
p4 =
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
{
300
Ptr<NoCount>
p =
Create<NoCount>
(
this
);
301
{
302
Ptr<const NoCount>
p1 = p;
303
}
304
raw =
GetPointer
(p);
305
p =
nullptr
;
306
}
307
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 0,
"012"
);
308
delete
raw;
309
}
310
311
m_nDestroyed
= 0;
312
{
313
Ptr<NoCount>
p =
Create<NoCount>
(
this
);
314
const
NoCount
* v1 =
PeekPointer
(p);
315
NoCount
* v2 =
PeekPointer
(p);
316
v1->
Nothing
();
317
v2->
Nothing
();
318
}
319
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"013"
);
320
321
{
322
Ptr<PtrTestBase>
p0 =
Create<NoCount>
(
this
);
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
*/
333
class
PtrTestSuite
:
public
TestSuite
334
{
335
public
:
336
/** Constructor. */
337
PtrTestSuite
()
338
:
TestSuite
(
"ptr"
)
339
{
340
AddTestCase
(
new
PtrTestCase
());
341
}
342
};
343
344
/**
345
* @ingroup ptr-tests
346
* PtrTestSuite instance variable.
347
*/
348
static
PtrTestSuite
g_ptrTestSuite
;
349
350
}
// namespace tests
351
352
}
// namespace ns3
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
mpi-test-fixtures.h:37
ns3::TestCase
encapsulates test code
Definition
test.h:1050
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:292
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1267
ns3::tests::NoCount
No Count class.
Definition
ptr-test-suite.cc:58
ns3::tests::NoCount::m_test
PtrTestCase * m_test
The object being tracked.
Definition
ptr-test-suite.cc:76
ns3::tests::NoCount::~NoCount
~NoCount() override
Destructor.
Definition
ptr-test-suite.cc:134
ns3::tests::NoCount::Nothing
void Nothing() const
Noop function.
Definition
ptr-test-suite.cc:140
ns3::tests::NoCount::NoCount
NoCount(PtrTestCase *test)
Constructor.
Definition
ptr-test-suite.cc:129
ns3::tests::PtrTestBase
Pointer base test class.
Definition
ptr-test-suite.cc:38
ns3::tests::PtrTestBase::Ref
void Ref() const
Increment the reference count.
Definition
ptr-test-suite.cc:114
ns3::tests::PtrTestBase::Unref
void Unref() const
Decrement the reference count, and delete if necessary.
Definition
ptr-test-suite.cc:120
ns3::tests::PtrTestBase::PtrTestBase
PtrTestBase()
Constructor.
Definition
ptr-test-suite.cc:104
ns3::tests::PtrTestBase::~PtrTestBase
virtual ~PtrTestBase()
Destructor.
Definition
ptr-test-suite.cc:109
ns3::tests::PtrTestBase::m_count
uint32_t m_count
The reference count.
Definition
ptr-test-suite.cc:50
ns3::tests::PtrTestCase
Test case for pointer.
Definition
ptr-test-suite.cc:84
ns3::tests::PtrTestCase::CallTest
Ptr< NoCount > CallTest(Ptr< NoCount > p)
Test that p is a valid object, by calling a member function.
Definition
ptr-test-suite.cc:156
ns3::tests::PtrTestCase::PtrTestCase
PtrTestCase()
Constructor.
Definition
ptr-test-suite.cc:144
ns3::tests::PtrTestCase::m_nDestroyed
uint32_t m_nDestroyed
Counter of number of objects destroyed.
Definition
ptr-test-suite.cc:101
ns3::tests::PtrTestCase::DestroyNotify
void DestroyNotify()
Count the destruction of an object.
Definition
ptr-test-suite.cc:150
ns3::tests::PtrTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
ptr-test-suite.cc:168
ns3::tests::PtrTestCase::CallTestConst
const Ptr< NoCount > CallTestConst(const Ptr< NoCount > p)
Test that p is a valid object, by calling a member function.
Definition
ptr-test-suite.cc:162
ns3::tests::PtrTestSuite
Test suite for pointer.
Definition
ptr-test-suite.cc:334
ns3::tests::PtrTestSuite::PtrTestSuite
PtrTestSuite()
Constructor.
Definition
ptr-test-suite.cc:337
uint32_t
ns3::tests::g_ptrTestSuite
static PtrTestSuite g_ptrTestSuite
PtrTestSuite instance variable.
Definition
ptr-test-suite.cc:348
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
NS_TEST_EXPECT_MSG_EQ
#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
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::PeekPointer
U * PeekPointer(const Ptr< U > &p)
Definition
ptr.h:443
ns3::GetPointer
U * GetPointer(const Ptr< U > &p)
Definition
ptr.h:450
ns3::ConstCast
Ptr< T1 > ConstCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition
ptr.h:573
test
-ns3 Test suite for the ns3 wrapper script
src
core
test
ptr-test-suite.cc
Generated on Fri Dec 13 2024 09:20:39 for ns-3 by
1.11.0