A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
src
core
test
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
p = p;
186
}
187
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"002"
);
188
189
m_nDestroyed
= 0;
190
{
191
Ptr<NoCount>
p1;
192
p1 = Create<NoCount> (
this
);
193
Ptr<NoCount>
p2 = p1;
194
}
195
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"003"
);
196
197
m_nDestroyed
= 0;
198
{
199
Ptr<NoCount>
p1;
200
p1 = Create<NoCount> (
this
);
201
Ptr<NoCount>
p2;
202
p2 = p1;
203
}
204
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"004"
);
205
206
m_nDestroyed
= 0;
207
{
208
Ptr<NoCount>
p1;
209
p1 = Create<NoCount> (
this
);
210
Ptr<NoCount>
p2 = Create<NoCount> (
this
);
211
p2 = p1;
212
}
213
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"005"
);
214
215
m_nDestroyed
= 0;
216
{
217
Ptr<NoCount>
p1;
218
p1 = Create<NoCount> (
this
);
219
Ptr<NoCount>
p2;
220
p2 = Create<NoCount> (
this
);
221
p2 = p1;
222
}
223
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"006"
);
224
225
m_nDestroyed
= 0;
226
{
227
Ptr<NoCount>
p1;
228
p1 = Create<NoCount> (
this
);
229
p1 = Create<NoCount> (
this
);
230
}
231
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"007"
);
232
233
m_nDestroyed
= 0;
234
{
235
Ptr<NoCount>
p1;
236
{
237
Ptr<NoCount>
p2;
238
p1 = Create<NoCount> (
this
);
239
p2 = Create<NoCount> (
this
);
240
p2 = p1;
241
}
242
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"008"
);
243
}
244
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"009"
);
245
246
m_nDestroyed
= 0;
247
{
248
Ptr<NoCount>
p1;
249
{
250
Ptr<NoCount>
p2;
251
p1 = Create<NoCount> (
this
);
252
p2 = Create<NoCount> (
this
);
253
p2 =
CallTest
(p1);
254
}
255
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"010"
);
256
}
257
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"011"
);
258
259
{
260
Ptr<NoCount>
p1;
261
Ptr<NoCount>
const
p2 =
CallTest
(p1);
262
Ptr<NoCount>
const
p3 =
CallTestConst
(p1);
263
Ptr<NoCount>
p4 =
CallTestConst
(p1);
264
Ptr<NoCount const>
p5 = p4;
265
//p4 = p5; You cannot make a const pointer be a non-const pointer.
266
// but if you use ConstCast, you can.
267
p4 = ConstCast<NoCount> (p5);
268
p5 = p1;
269
Ptr<NoCount>
p;
270
if
(p == 0)
271
{}
272
if
(p != 0)
273
{}
274
if
(0 == p)
275
{}
276
if
(0 != p)
277
{}
278
if
(p)
279
{}
280
if
(!p)
281
{}
282
}
283
284
m_nDestroyed
= 0;
285
{
286
NoCount
*raw;
287
{
288
Ptr<NoCount>
p = Create<NoCount> (
this
);
289
{
290
Ptr<NoCount const>
p1 = p;
291
}
292
raw =
GetPointer
(p);
293
p = 0;
294
}
295
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 0,
"012"
);
296
delete
raw;
297
}
298
299
m_nDestroyed
= 0;
300
{
301
Ptr<NoCount>
p = Create<NoCount> (
this
);
302
const
NoCount
*v1 =
PeekPointer
(p);
303
NoCount
*v2 =
PeekPointer
(p);
304
v1->Nothing ();
305
v2->
Nothing
();
306
}
307
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"013"
);
308
309
{
310
Ptr<PtrTestBase>
p0 = Create<NoCount> (
this
);
311
Ptr<NoCount>
p1 = Create<NoCount> (
this
);
312
NS_TEST_EXPECT_MSG_EQ
((p0 == p1),
false
,
"operator == failed"
);
313
NS_TEST_EXPECT_MSG_EQ
((p0 != p1),
true
,
"operator != failed"
);
314
}
315
}
316
321
class
PtrTestSuite
:
public
TestSuite
322
{
323
public
:
325
PtrTestSuite
()
326
:
TestSuite
(
"ptr"
)
327
{
328
AddTestCase
(
new
PtrTestCase
());
329
}
330
};
331
336
static
PtrTestSuite
g_ptrTestSuite
;
337
338
339
}
// namespace tests
340
341
}
// 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:325
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:336
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:321
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
Generated on Wed Nov 7 2018 10:01:51 for ns-3 by
1.8.14