A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
48
class
PtrTestBase
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
:
87
PtrTestCase
*
m_test
;
88
};
89
94
class
PtrTestCase
:
public
TestCase
95
{
96
public
:
98
PtrTestCase
();
100
void
DestroyNotify
();
101
102
private
:
103
void
DoRun
()
override
;
109
Ptr<NoCount>
CallTest
(
Ptr<NoCount>
p);
111
const
Ptr<NoCount>
CallTestConst
(
const
Ptr<NoCount>
p);
112
uint32_t
m_nDestroyed
;
113
};
114
115
PtrTestBase::PtrTestBase
()
116
: m_count(1)
117
{
118
}
119
120
PtrTestBase::~PtrTestBase
()
121
{
122
}
123
124
void
125
PtrTestBase::Ref
()
const
126
{
127
m_count
++;
128
}
129
130
void
131
PtrTestBase::Unref
()
const
132
{
133
m_count
--;
134
if
(
m_count
== 0)
135
{
136
delete
this
;
137
}
138
}
139
140
NoCount::NoCount
(
PtrTestCase
* test)
141
: m_test(test)
142
{
143
}
144
145
NoCount::~NoCount
()
146
{
147
m_test
->
DestroyNotify
();
148
}
149
150
void
151
NoCount::Nothing
()
const
152
{
153
}
154
155
PtrTestCase::PtrTestCase
()
156
:
TestCase
(
"Sanity checking of Ptr<>"
)
157
{
158
}
159
160
void
161
PtrTestCase::DestroyNotify
()
162
{
163
m_nDestroyed
++;
164
}
165
166
Ptr<NoCount>
167
PtrTestCase::CallTest
(
Ptr<NoCount>
p)
168
{
169
return
p;
170
}
171
172
const
Ptr<NoCount>
173
PtrTestCase::CallTestConst
(
const
Ptr<NoCount>
p)
174
{
175
return
p;
176
}
177
178
void
179
PtrTestCase::DoRun
()
180
{
181
m_nDestroyed
= 0;
182
{
183
Ptr<NoCount>
p = Create<NoCount>(
this
);
184
}
185
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"001"
);
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
}
204
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"002"
);
205
206
m_nDestroyed
= 0;
207
{
208
Ptr<NoCount>
p1;
209
p1 = Create<NoCount>(
this
);
210
Ptr<NoCount>
p2 = p1;
211
}
212
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"003"
);
213
214
m_nDestroyed
= 0;
215
{
216
Ptr<NoCount>
p1;
217
p1 = Create<NoCount>(
this
);
218
Ptr<NoCount>
p2;
219
p2 = p1;
220
}
221
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"004"
);
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
}
230
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"005"
);
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
}
240
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"006"
);
241
242
m_nDestroyed
= 0;
243
{
244
Ptr<NoCount>
p1;
245
p1 = Create<NoCount>(
this
);
246
p1 = Create<NoCount>(
this
);
247
}
248
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"007"
);
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
}
259
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"008"
);
260
}
261
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"009"
);
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
}
272
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"010"
);
273
}
274
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"011"
);
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
}
318
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 0,
"012"
);
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
}
330
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"013"
);
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
:
348
PtrTestSuite
()
349
:
TestSuite
(
"ptr"
)
350
{
351
AddTestCase
(
new
PtrTestCase
());
352
}
353
};
354
359
static
PtrTestSuite
g_ptrTestSuite
;
360
361
}
// namespace tests
362
363
}
// namespace ns3
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition:
ptr.h:77
ns3::TestCase
encapsulates test code
Definition:
test.h:1060
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition:
test.cc:301
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1256
ns3::tests::NoCount
No Count class.
Definition:
ptr-test-suite.cc:69
ns3::tests::NoCount::m_test
PtrTestCase * m_test
The object being tracked.
Definition:
ptr-test-suite.cc:87
ns3::tests::NoCount::~NoCount
~NoCount() override
Destructor.
Definition:
ptr-test-suite.cc:145
ns3::tests::NoCount::Nothing
void Nothing() const
Noop function.
Definition:
ptr-test-suite.cc:151
ns3::tests::NoCount::NoCount
NoCount(PtrTestCase *test)
Constructor.
Definition:
ptr-test-suite.cc:140
ns3::tests::PtrTestBase
Pointer base test class.
Definition:
ptr-test-suite.cc:49
ns3::tests::PtrTestBase::Ref
void Ref() const
Increment the reference count.
Definition:
ptr-test-suite.cc:125
ns3::tests::PtrTestBase::Unref
void Unref() const
Decrement the reference count, and delete if necessary.
Definition:
ptr-test-suite.cc:131
ns3::tests::PtrTestBase::PtrTestBase
PtrTestBase()
Constructor.
Definition:
ptr-test-suite.cc:115
ns3::tests::PtrTestBase::~PtrTestBase
virtual ~PtrTestBase()
Destructor.
Definition:
ptr-test-suite.cc:120
ns3::tests::PtrTestBase::m_count
uint32_t m_count
The reference count.
Definition:
ptr-test-suite.cc:61
ns3::tests::PtrTestCase
Test case for pointer.
Definition:
ptr-test-suite.cc:95
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:167
ns3::tests::PtrTestCase::PtrTestCase
PtrTestCase()
Constructor.
Definition:
ptr-test-suite.cc:155
ns3::tests::PtrTestCase::m_nDestroyed
uint32_t m_nDestroyed
Counter of number of objects destroyed.
Definition:
ptr-test-suite.cc:112
ns3::tests::PtrTestCase::DestroyNotify
void DestroyNotify()
Count the destruction of an object.
Definition:
ptr-test-suite.cc:161
ns3::tests::PtrTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition:
ptr-test-suite.cc:179
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:173
ns3::tests::PtrTestSuite
Test suite for pointer.
Definition:
ptr-test-suite.cc:345
ns3::tests::PtrTestSuite::PtrTestSuite
PtrTestSuite()
Constructor.
Definition:
ptr-test-suite.cc:348
ns3::tests::g_ptrTestSuite
static PtrTestSuite g_ptrTestSuite
PtrTestSuite instance variable.
Definition:
ptr-test-suite.cc:359
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:251
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::GetPointer
U * GetPointer(const Ptr< U > &p)
Definition:
ptr.h:456
ns3::PeekPointer
U * PeekPointer(const Ptr< U > &p)
Definition:
ptr.h:449
src
core
test
ptr-test-suite.cc
Generated on Sun Mar 3 2024 17:10:56 for ns-3 by
1.9.1