A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
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
private
:
60
mutable
uint32_t
m_count
;
61
};
62
67
class
NoCount
:
public
PtrTestBase
68
{
69
public
:
75
NoCount
(
PtrTestCase
*test);
81
~NoCount
();
83
void
Nothing
(
void
)
const
;
84
private
:
85
PtrTestCase
*
m_test
;
86
};
87
88
93
class
PtrTestCase
:
public
TestCase
94
{
95
public
:
97
PtrTestCase
();
99
void
DestroyNotify
(
void
);
100
private
:
101
virtual
void
DoRun
(
void
);
107
Ptr<NoCount>
CallTest
(
Ptr<NoCount>
p);
109
Ptr<NoCount>
const
CallTestConst
(
Ptr<NoCount>
const
p);
110
uint32_t
m_nDestroyed
;
111
};
112
113
114
PtrTestBase::PtrTestBase
()
115
: m_count (1)
116
{
117
}
118
PtrTestBase::~PtrTestBase
()
119
{
120
}
121
void
122
PtrTestBase::Ref
(
void
)
const
123
{
124
m_count
++;
125
}
126
void
127
PtrTestBase::Unref
(
void
)
const
128
{
129
m_count
--;
130
if
(
m_count
== 0)
131
{
132
delete
this
;
133
}
134
}
135
136
NoCount::NoCount
(
PtrTestCase
*test)
137
: m_test (test)
138
{
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
}
154
void
155
PtrTestCase::DestroyNotify
(
void
)
156
{
157
m_nDestroyed
++;
158
}
159
Ptr<NoCount>
160
PtrTestCase::CallTest
(
Ptr<NoCount>
p)
161
{
162
return
p;
163
}
164
165
Ptr<NoCount>
const
166
PtrTestCase::CallTestConst
(
Ptr<NoCount>
const
p)
167
{
168
return
p;
169
}
170
171
172
void
173
PtrTestCase::DoRun
(
void
)
174
{
175
m_nDestroyed
=
false
;
176
{
177
Ptr<NoCount>
p = Create<NoCount> (
this
);
178
}
179
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"001"
);
180
181
m_nDestroyed
= 0;
182
{
183
Ptr<NoCount>
p;
184
p = Create<NoCount> (
this
);
185
#if defined(__clang__)
186
#if __has_warning("-Wself-assign-overloaded")
187
#pragma clang diagnostic push
188
#pragma clang diagnostic ignored "-Wself-assign-overloaded"
189
#endif
190
#endif
191
p = p;
192
#if defined(__clang__)
193
#if __has_warning("-Wself-assign-overloaded")
194
#pragma clang diagnostic pop
195
#endif
196
#endif
197
}
198
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"002"
);
199
200
m_nDestroyed
= 0;
201
{
202
Ptr<NoCount>
p1;
203
p1 = Create<NoCount> (
this
);
204
Ptr<NoCount>
p2 = p1;
205
}
206
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"003"
);
207
208
m_nDestroyed
= 0;
209
{
210
Ptr<NoCount>
p1;
211
p1 = Create<NoCount> (
this
);
212
Ptr<NoCount>
p2;
213
p2 = p1;
214
}
215
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"004"
);
216
217
m_nDestroyed
= 0;
218
{
219
Ptr<NoCount>
p1;
220
p1 = Create<NoCount> (
this
);
221
Ptr<NoCount>
p2 = Create<NoCount> (
this
);
222
p2 = p1;
223
}
224
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"005"
);
225
226
m_nDestroyed
= 0;
227
{
228
Ptr<NoCount>
p1;
229
p1 = Create<NoCount> (
this
);
230
Ptr<NoCount>
p2;
231
p2 = Create<NoCount> (
this
);
232
p2 = p1;
233
}
234
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"006"
);
235
236
m_nDestroyed
= 0;
237
{
238
Ptr<NoCount>
p1;
239
p1 = Create<NoCount> (
this
);
240
p1 = Create<NoCount> (
this
);
241
}
242
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"007"
);
243
244
m_nDestroyed
= 0;
245
{
246
Ptr<NoCount>
p1;
247
{
248
Ptr<NoCount>
p2;
249
p1 = Create<NoCount> (
this
);
250
p2 = Create<NoCount> (
this
);
251
p2 = p1;
252
}
253
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"008"
);
254
}
255
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"009"
);
256
257
m_nDestroyed
= 0;
258
{
259
Ptr<NoCount>
p1;
260
{
261
Ptr<NoCount>
p2;
262
p1 = Create<NoCount> (
this
);
263
p2 = Create<NoCount> (
this
);
264
p2 =
CallTest
(p1);
265
}
266
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"010"
);
267
}
268
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"011"
);
269
270
{
271
Ptr<NoCount>
p1;
272
Ptr<NoCount>
const
p2 =
CallTest
(p1);
273
Ptr<NoCount>
const
p3 =
CallTestConst
(p1);
274
Ptr<NoCount>
p4 =
CallTestConst
(p1);
275
Ptr<NoCount const>
p5 = p4;
276
//p4 = p5; You cannot make a const pointer be a non-const pointer.
277
// but if you use ConstCast, you can.
278
p4 = ConstCast<NoCount> (p5);
279
p5 = p1;
280
Ptr<NoCount>
p;
281
if
(p == 0)
282
{}
283
if
(p != 0)
284
{}
285
if
(0 == p)
286
{}
287
if
(0 != p)
288
{}
289
if
(p)
290
{}
291
if
(!p)
292
{}
293
}
294
295
m_nDestroyed
= 0;
296
{
297
NoCount
*raw;
298
{
299
Ptr<NoCount>
p = Create<NoCount> (
this
);
300
{
301
Ptr<NoCount const>
p1 = p;
302
}
303
raw =
GetPointer
(p);
304
p = 0;
305
}
306
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 0,
"012"
);
307
delete
raw;
308
}
309
310
m_nDestroyed
= 0;
311
{
312
Ptr<NoCount>
p = Create<NoCount> (
this
);
313
const
NoCount
*v1 =
PeekPointer
(p);
314
NoCount
*v2 =
PeekPointer
(p);
315
v1->Nothing ();
316
v2->
Nothing
();
317
}
318
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"013"
);
319
320
{
321
Ptr<PtrTestBase>
p0 = Create<NoCount> (
this
);
322
Ptr<NoCount>
p1 = Create<NoCount> (
this
);
323
NS_TEST_EXPECT_MSG_EQ
((p0 == p1),
false
,
"operator == failed"
);
324
NS_TEST_EXPECT_MSG_EQ
((p0 != p1),
true
,
"operator != failed"
);
325
}
326
}
327
332
class
PtrTestSuite
:
public
TestSuite
333
{
334
public
:
336
PtrTestSuite
()
337
:
TestSuite
(
"ptr"
)
338
{
339
AddTestCase
(
new
PtrTestCase
());
340
}
341
};
342
347
static
PtrTestSuite
g_ptrTestSuite
;
348
349
350
}
// namespace tests
351
352
}
// namespace ns3
ns3::tests::NoCount::m_test
PtrTestCase * m_test
The object being tracked.
Definition:
ptr-test-suite.cc:85
ns3::tests::PtrTestCase::PtrTestCase
PtrTestCase()
Constructor.
Definition:
ptr-test-suite.cc:150
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition:
ptr.h:73
ns3::tests::PtrTestBase::~PtrTestBase
virtual ~PtrTestBase()
Destructor.
Definition:
ptr-test-suite.cc:118
ns3::tests::PtrTestBase::m_count
uint32_t m_count
The reference count.
Definition:
ptr-test-suite.cc:60
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1342
ns3::tests::NoCount::Nothing
void Nothing(void) const
Noop function.
Definition:
ptr-test-suite.cc:145
ns3::tests::PtrTestSuite::PtrTestSuite
PtrTestSuite()
Constructor.
Definition:
ptr-test-suite.cc:336
ns3::PeekPointer
U * PeekPointer(const Ptr< U > &p)
Definition:
ptr.h:564
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:285
ns3::TestCase
encapsulates test code
Definition:
test.h:1155
ns3::tests::PtrTestBase::Ref
void Ref(void) const
Increment the reference count.
Definition:
ptr-test-suite.cc:122
ns3::GetPointer
U * GetPointer(const Ptr< U > &p)
Definition:
ptr.h:570
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition:
test.cc:299
ns3::tests::PtrTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
ptr-test-suite.cc:173
ns3::tests::PtrTestBase::PtrTestBase
PtrTestBase()
Constructor.
Definition:
ptr-test-suite.cc:114
ns3::tests::g_ptrTestSuite
static PtrTestSuite g_ptrTestSuite
PtrTestSuite instance variable.
Definition:
ptr-test-suite.cc:347
ns3::tests::NoCount
No Count class.
Definition:
ptr-test-suite.cc:67
ns3::tests::PtrTestCase
Test case for pointer.
Definition:
ptr-test-suite.cc:93
ns3::tests::PtrTestCase::DestroyNotify
void DestroyNotify(void)
Count the destruction of an object.
Definition:
ptr-test-suite.cc:155
ns3::tests::PtrTestBase
Pointer base test class.
Definition:
ptr-test-suite.cc:48
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::tests::NoCount::~NoCount
~NoCount()
Destructor.
Definition:
ptr-test-suite.cc:140
ns3::tests::PtrTestBase::Unref
void Unref(void) const
Decrement the reference count, and delete if necessary.
Definition:
ptr-test-suite.cc:127
ns3::tests::NoCount::NoCount
NoCount(PtrTestCase *test)
Constructor.
Definition:
ptr-test-suite.cc:136
ns3::tests::PtrTestCase::m_nDestroyed
uint32_t m_nDestroyed
Counter of number of objects destroyed.
Definition:
ptr-test-suite.cc:110
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:166
ns3::tests::PtrTestSuite
Test suite for pointer.
Definition:
ptr-test-suite.cc:332
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:160
src
core
test
ptr-test-suite.cc
Generated on Wed Aug 21 2019 03:37:06 for ns-3 by
1.8.14