A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
48
class
PtrTestBase
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
:
87
PtrTestCase
*
m_test
;
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
);
110
Ptr<NoCount>
CallTest
(
Ptr<NoCount>
p);
112
Ptr<NoCount>
const
CallTestConst
(
Ptr<NoCount>
const
p);
113
uint32_t
m_nDestroyed
;
114
};
115
116
117
PtrTestBase::PtrTestBase
()
118
: m_count (1)
119
{}
120
PtrTestBase::~PtrTestBase
()
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
137
NoCount::NoCount
(
PtrTestCase
*test)
138
: m_test (test)
139
{}
140
NoCount::~NoCount
()
141
{
142
m_test
->
DestroyNotify
();
143
}
144
void
145
NoCount::Nothing
()
const
146
{}
147
148
149
150
PtrTestCase::PtrTestCase
(
void
)
151
:
TestCase
(
"Sanity checking of Ptr<>"
)
152
{}
153
void
154
PtrTestCase::DestroyNotify
(
void
)
155
{
156
m_nDestroyed
++;
157
}
158
Ptr<NoCount>
159
PtrTestCase::CallTest
(
Ptr<NoCount>
p)
160
{
161
return
p;
162
}
163
164
Ptr<NoCount>
const
165
PtrTestCase::CallTestConst
(
Ptr<NoCount>
const
p)
166
{
167
return
p;
168
}
169
170
171
void
172
PtrTestCase::DoRun
(
void
)
173
{
174
m_nDestroyed
=
false
;
175
{
176
Ptr<NoCount>
p = Create<NoCount> (
this
);
177
}
178
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"001"
);
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
}
197
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"002"
);
198
199
m_nDestroyed
= 0;
200
{
201
Ptr<NoCount>
p1;
202
p1 = Create<NoCount> (
this
);
203
Ptr<NoCount>
p2 = p1;
204
}
205
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"003"
);
206
207
m_nDestroyed
= 0;
208
{
209
Ptr<NoCount>
p1;
210
p1 = Create<NoCount> (
this
);
211
Ptr<NoCount>
p2;
212
p2 = p1;
213
}
214
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"004"
);
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
}
223
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"005"
);
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
}
233
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"006"
);
234
235
m_nDestroyed
= 0;
236
{
237
Ptr<NoCount>
p1;
238
p1 = Create<NoCount> (
this
);
239
p1 = Create<NoCount> (
this
);
240
}
241
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"007"
);
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
}
252
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"008"
);
253
}
254
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"009"
);
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
}
265
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"010"
);
266
}
267
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"011"
);
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
}
305
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 0,
"012"
);
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
}
317
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"013"
);
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
:
335
PtrTestSuite
()
336
:
TestSuite
(
"ptr"
)
337
{
338
AddTestCase
(
new
PtrTestCase
());
339
}
340
};
341
346
static
PtrTestSuite
g_ptrTestSuite
;
347
348
349
}
// namespace tests
350
351
}
// namespace ns3
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition:
ptr.h:74
ns3::TestCase
encapsulates test code
Definition:
test.h:994
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition:
test.cc:299
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1188
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::Nothing
void Nothing(void) const
Noop function.
Definition:
ptr-test-suite.cc:145
ns3::tests::NoCount::~NoCount
~NoCount()
Destructor.
Definition:
ptr-test-suite.cc:140
ns3::tests::NoCount::NoCount
NoCount(PtrTestCase *test)
Constructor.
Definition:
ptr-test-suite.cc:137
ns3::tests::PtrTestBase
Pointer base test class.
Definition:
ptr-test-suite.cc:49
ns3::tests::PtrTestBase::Unref
void Unref(void) const
Decrement the reference count, and delete if necessary.
Definition:
ptr-test-suite.cc:128
ns3::tests::PtrTestBase::PtrTestBase
PtrTestBase()
Constructor.
Definition:
ptr-test-suite.cc:117
ns3::tests::PtrTestBase::~PtrTestBase
virtual ~PtrTestBase()
Destructor.
Definition:
ptr-test-suite.cc:120
ns3::tests::PtrTestBase::Ref
void Ref(void) const
Increment the reference count.
Definition:
ptr-test-suite.cc:123
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:96
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:159
ns3::tests::PtrTestCase::PtrTestCase
PtrTestCase()
Constructor.
Definition:
ptr-test-suite.cc:150
ns3::tests::PtrTestCase::m_nDestroyed
uint32_t m_nDestroyed
Counter of number of objects destroyed.
Definition:
ptr-test-suite.cc:113
ns3::tests::PtrTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
ptr-test-suite.cc:172
ns3::tests::PtrTestCase::DestroyNotify
void DestroyNotify(void)
Count the destruction of an object.
Definition:
ptr-test-suite.cc:154
ns3::tests::PtrTestCase::CallTestConst
Ptr< NoCount > const CallTestConst(Ptr< NoCount > const p)
Test that p is a valid object, by calling a member function.
Definition:
ptr-test-suite.cc:165
ns3::tests::PtrTestSuite
Test suite for pointer.
Definition:
ptr-test-suite.cc:332
ns3::tests::PtrTestSuite::PtrTestSuite
PtrTestSuite()
Constructor.
Definition:
ptr-test-suite.cc:335
ns3::tests::g_ptrTestSuite
static PtrTestSuite g_ptrTestSuite
PtrTestSuite instance variable.
Definition:
ptr-test-suite.cc:346
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:240
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:421
ns3::PeekPointer
U * PeekPointer(const Ptr< U > &p)
Definition:
ptr.h:415
test-ns3.tests
tests
Definition:
test-ns3.py:2134
src
core
test
ptr-test-suite.cc
Generated on Tue Feb 6 2024 19:21:17 for ns-3 by
1.9.1